Skip to content

Commit 0d66cfb

Browse files
committed
- Support gridstack element node reparenting without losing the stylesheets
1 parent 401399a commit 0d66cfb

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
@@ -1934,4 +1934,24 @@ describe('gridstack', function() {
19341934
});
19351935
*/
19361936
});
1937+
1938+
describe('stylesheet', function() {
1939+
let grid: GridStack;
1940+
let root: HTMLElement;
1941+
beforeEach(function() {
1942+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
1943+
grid = GridStack.init({ cellHeight: 30 });
1944+
root = document.getElementById('gs-cont')!;
1945+
});
1946+
afterEach(function() {
1947+
document.body.removeChild(root);
1948+
});
1949+
it('not getting lost in case of node detach/attach', function() {
1950+
expect(window.getComputedStyle(grid.el.querySelector("#item1")!).height).toBe("60px");
1951+
const oldParent = root.parentElement;
1952+
root.remove();
1953+
oldParent!.appendChild(root);
1954+
expect(window.getComputedStyle(grid.el.querySelector("#item1")!).height).toBe("60px");
1955+
});
1956+
});
19371957
});

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

@@ -248,7 +248,7 @@ export class GridStack {
248248
/** @internal */
249249
public _gsEventHandler = {};
250250
/** @internal */
251-
protected _styles: GridCSSStyleSheet;
251+
protected _styles: GridHTMLStyleElement;
252252
/** @internal flag to keep cells square during resize */
253253
protected _isAutoCellHeight: boolean;
254254
/** @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)