Skip to content

Commit 513871e

Browse files
authored
fix: create CSS rules by style element doesn't work in salesforce (#899)
* fix: create CSS rules by style element doesn't work in salesforce - for some reason the new approach to use `const style = document.createElement('style')` and then `style.sheet` doesn't work in Salesforce, so let's add back the previous code as a fallback in case option 1 doesn't work
1 parent bb977c3 commit 513871e

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

slick.grid.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,27 +2057,56 @@ if (typeof Slick === "undefined") {
20572057
function createCssRules() {
20582058
_style = document.createElement('style');
20592059
_style.nonce = 'random-string';
2060-
document.head.appendChild(_style);
20612060
(options.shadowRoot || document.head).appendChild(_style);
20622061

2062+
const rowHeight = (options.rowHeight - cellHeightDiff);
2063+
const rules = [
2064+
`.${uid} .slick-group-header-column { left: 1000px; }`,
2065+
`.${uid} .slick-header-column { left: 1000px; }`,
2066+
`.${uid} .slick-top-panel { height: ${options.topPanelHeight}px; }`,
2067+
`.${uid} .slick-preheader-panel { height: ${options.preHeaderPanelHeight}px; }`,
2068+
`.${uid} .slick-headerrow-columns { height: ${options.headerRowHeight}px; }`,
2069+
`.${uid} .slick-footerrow-columns { height: ${options.footerRowHeight}px; }`,
2070+
`.${uid} .slick-cell { height: ${rowHeight}px; }`,
2071+
`.${uid} .slick-row { height: ${options.rowHeight}px; }`,
2072+
];
2073+
20632074
const sheet = _style.sheet;
20642075
if (sheet) {
2065-
const rowHeight = (options.rowHeight - cellHeightDiff);
2066-
sheet.insertRule(`.${uid} .slick-group-header-column { left: 1000px; }`);
2067-
sheet.insertRule(`.${uid} .slick-header-column { left: 1000px; }`);
2068-
sheet.insertRule(`.${uid} .slick-top-panel { height: ${options.topPanelHeight}px; }`);
2069-
sheet.insertRule(`.${uid} .slick-preheader-panel { height: ${options.preHeaderPanelHeight}px; }`);
2070-
sheet.insertRule(`.${uid} .slick-headerrow-columns { height: ${options.headerRowHeight}px; }`);
2071-
sheet.insertRule(`.${uid} .slick-footerrow-columns { height: ${options.footerRowHeight}px; }`);
2072-
sheet.insertRule(`.${uid} .slick-cell { height: ${rowHeight}px; }`);
2073-
sheet.insertRule(`.${uid} .slick-row { height: ${options.rowHeight}px; }`);
2076+
for (let rule of rules) {
2077+
sheet.insertRule(rule);
2078+
}
20742079

20752080
for (let i = 0; i < columns.length; i++) {
20762081
if (!columns[i] || columns[i].hidden) { continue; }
20772082

20782083
sheet.insertRule(`.${uid} .l${i} { }`);
20792084
sheet.insertRule(`.${uid} .r${i} { }`);
20802085
}
2086+
} else {
2087+
// fallback in case the 1st approach doesn't work, let's use our previous way of creating the css rules which is what works in Salesforce :(
2088+
createCssRulesAlternative(rules);
2089+
}
2090+
}
2091+
2092+
/** Create CSS rules via template in case the first approach with createElement('style') doesn't work */
2093+
function createCssRulesAlternative(rules) {
2094+
const template = document.createElement('template');
2095+
template.innerHTML = '<style type="text/css" rel="stylesheet" />';
2096+
_style = template.content.firstChild;
2097+
(options.shadowRoot || document.head).appendChild(_style);
2098+
2099+
for (let i = 0; i < columns.length; i++) {
2100+
if (!columns[i] || columns[i].hidden) continue;
2101+
2102+
rules.push(`.${uid} .l${i} { }`);
2103+
rules.push(`.${uid} .r${i} { }`);
2104+
}
2105+
2106+
if (_style.styleSheet) { // IE
2107+
_style.styleSheet.cssText = rules.join(" ");
2108+
} else {
2109+
_style.appendChild(document.createTextNode(rules.join(" ")));
20812110
}
20822111
}
20832112

0 commit comments

Comments
 (0)