Skip to content

Commit 4548fa0

Browse files
authored
fix: create CSS rules by style element doesn't work in salesforce (#900)
* 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 282fbb4 commit 4548fa0

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

src/slick.grid.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,24 +2306,55 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
23062306
this._style = document.createElement('style');
23072307
this._style.nonce = 'random-string';
23082308
(this._options.shadowRoot || document.head).appendChild(this._style);
2309+
2310+
const rowHeight = (this._options.rowHeight! - this.cellHeightDiff);
2311+
const rules = [
2312+
`.${this.uid} .slick-group-header-column { left: 1000px; }`,
2313+
`.${this.uid} .slick-header-column { left: 1000px; }`,
2314+
`.${this.uid} .slick-top-panel { height: ${this._options.topPanelHeight}px; }`,
2315+
`.${this.uid} .slick-preheader-panel { height: ${this._options.preHeaderPanelHeight}px; }`,
2316+
`.${this.uid} .slick-headerrow-columns { height: ${this._options.headerRowHeight}px; }`,
2317+
`.${this.uid} .slick-footerrow-columns { height: ${this._options.footerRowHeight}px; }`,
2318+
`.${this.uid} .slick-cell { height: ${rowHeight}px; }`,
2319+
`.${this.uid} .slick-row { height: ${this._options.rowHeight}px; }`,
2320+
];
2321+
23092322
const sheet = this._style.sheet;
23102323
if (sheet) {
2311-
const rowHeight = (this._options.rowHeight! - this.cellHeightDiff);
2312-
sheet.insertRule(`.${this.uid} .slick-group-header-column { left: 1000px; }`);
2313-
sheet.insertRule(`.${this.uid} .slick-header-column { left: 1000px; }`);
2314-
sheet.insertRule(`.${this.uid} .slick-top-panel { height: ${this._options.topPanelHeight}px; }`);
2315-
sheet.insertRule(`.${this.uid} .slick-preheader-panel { height: ${this._options.preHeaderPanelHeight}px; }`);
2316-
sheet.insertRule(`.${this.uid} .slick-headerrow-columns { height: ${this._options.headerRowHeight}px; }`);
2317-
sheet.insertRule(`.${this.uid} .slick-footerrow-columns { height: ${this._options.footerRowHeight}px; }`);
2318-
sheet.insertRule(`.${this.uid} .slick-cell { height: ${rowHeight}px; }`);
2319-
sheet.insertRule(`.${this.uid} .slick-row { height: ${this._options.rowHeight}px; }`);
2324+
for (const rule of rules) {
2325+
sheet.insertRule(rule);
2326+
}
23202327

23212328
for (let i = 0; i < this.columns.length; i++) {
23222329
if (!this.columns[i] || this.columns[i].hidden) { continue; }
23232330

23242331
sheet.insertRule(`.${this.uid} .l${i} { }`);
23252332
sheet.insertRule(`.${this.uid} .r${i} { }`);
23262333
}
2334+
} else {
2335+
// 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 :(
2336+
this.createCssRulesAlternative(rules);
2337+
}
2338+
}
2339+
2340+
/** Create CSS rules via template in case the first approach with createElement('style') doesn't work */
2341+
protected createCssRulesAlternative(rules: string[]) {
2342+
const template = document.createElement('template');
2343+
template.innerHTML = '<style type="text/css" rel="stylesheet" />';
2344+
this._style = template.content.firstChild as HTMLStyleElement;
2345+
(this._options.shadowRoot || document.head).appendChild(this._style);
2346+
2347+
for (let i = 0; i < this.columns.length; i++) {
2348+
if (!this.columns[i] || this.columns[i].hidden) { continue; }
2349+
2350+
rules.push(`.${this.uid} .l${i} { }`);
2351+
rules.push(`.${this.uid} .r${i} { }`);
2352+
}
2353+
2354+
if ((this._style as any).styleSheet) { // IE
2355+
(this._style as any).styleSheet.cssText = rules.join(' ');
2356+
} else {
2357+
this._style.appendChild(document.createTextNode(rules.join(' ')));
23272358
}
23282359
}
23292360

0 commit comments

Comments
 (0)