Skip to content

Commit 180e785

Browse files
authored
Merge pull request #2852 from iongroup/fix/style-detach
Support gridstack element node reparenting without losing the stylesheets
2 parents 95d2cbc + 0d66cfb commit 180e785

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

spec/gridstack-spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,4 +1982,24 @@ describe('gridstack >', function() {
19821982
});
19831983
*/
19841984
});
1985+
1986+
describe('stylesheet', function() {
1987+
let grid: GridStack;
1988+
let root: HTMLElement;
1989+
beforeEach(function() {
1990+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
1991+
grid = GridStack.init({ cellHeight: 30 });
1992+
root = document.getElementById('gs-cont')!;
1993+
});
1994+
afterEach(function() {
1995+
document.body.removeChild(root);
1996+
});
1997+
it('not getting lost in case of node detach/attach', function() {
1998+
expect(window.getComputedStyle(grid.el.querySelector("#item1")!).height).toBe("60px");
1999+
const oldParent = root.parentElement;
2000+
root.remove();
2001+
oldParent!.appendChild(root);
2002+
expect(window.getComputedStyle(grid.el.querySelector("#item1")!).height).toBe("60px");
2003+
});
2004+
});
19852005
});

src/gridstack.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export interface CellPosition {
5151
y: number;
5252
}
5353

54-
interface GridCSSStyleSheet extends CSSStyleSheet {
54+
interface GridHTMLStyleElement extends HTMLStyleElement {
5555
_max?: number; // internal tracker of the max # of rows we created
5656
}
5757

@@ -251,7 +251,7 @@ export class GridStack {
251251
/** @internal */
252252
public _gsEventHandler = {};
253253
/** @internal */
254-
protected _styles: GridCSSStyleSheet;
254+
protected _styles: GridHTMLStyleElement;
255255
/** @internal flag to keep cells square during resize */
256256
protected _isAutoCellHeight: boolean;
257257
/** @internal limit auto cell resizing method */

src/utils.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export class Utils {
196196
* @param parent to insert the stylesheet as first child,
197197
* if none supplied it will be appended to the document head instead.
198198
*/
199-
static createStylesheet(id: string, parent?: HTMLElement, options?: { nonce?: string }): CSSStyleSheet {
199+
static createStylesheet(id: string, parent?: HTMLElement, options?: { nonce?: string }): HTMLStyleElement {
200200
const style: HTMLStyleElement = document.createElement('style');
201201
const nonce = options?.nonce
202202
if (nonce) style.nonce = nonce
@@ -216,7 +216,7 @@ export class Utils {
216216
} else {
217217
parent.insertBefore(style, parent.firstChild);
218218
}
219-
return style.sheet as CSSStyleSheet;
219+
return style;
220220
}
221221

222222
/** removed the given stylesheet id */
@@ -227,12 +227,10 @@ export class Utils {
227227
}
228228

229229
/** inserts a CSS rule */
230-
static addCSSRule(sheet: CSSStyleSheet, selector: string, rules: string): void {
231-
if (typeof sheet.addRule === 'function') {
232-
sheet.addRule(selector, rules);
233-
} else if (typeof sheet.insertRule === 'function') {
234-
sheet.insertRule(`${selector}{${rules}}`);
235-
}
230+
static addCSSRule(sheet: HTMLStyleElement, selector: string, rules: string): void {
231+
// Rather than using sheet.insertRule, use text since it supports
232+
// gridstack node reparenting around in the DOM
233+
sheet.textContent += `${selector} { ${rules} } `;
236234
}
237235

238236
// eslint-disable-next-line @typescript-eslint/no-explicit-any

0 commit comments

Comments
 (0)