Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support gridstack element node reparenting without losing the stylesheets #2852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions spec/gridstack-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1934,4 +1934,24 @@ describe('gridstack', function() {
});
*/
});

describe('stylesheet', function() {
let grid: GridStack;
let root: HTMLElement;
beforeEach(function() {
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
grid = GridStack.init({ cellHeight: 30 });
root = document.getElementById('gs-cont')!;
});
afterEach(function() {
document.body.removeChild(root);
});
it('not getting lost in case of node detach/attach', function() {
expect(window.getComputedStyle(grid.el.querySelector("#item1")!).height).toBe("60px");
const oldParent = root.parentElement;
root.remove();
oldParent!.appendChild(root);
expect(window.getComputedStyle(grid.el.querySelector("#item1")!).height).toBe("60px");
});
});
});
4 changes: 2 additions & 2 deletions src/gridstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface CellPosition {
y: number;
}

interface GridCSSStyleSheet extends CSSStyleSheet {
interface GridHTMLStyleElement extends HTMLStyleElement {
_max?: number; // internal tracker of the max # of rows we created
}

Expand Down Expand Up @@ -248,7 +248,7 @@ export class GridStack {
/** @internal */
public _gsEventHandler = {};
/** @internal */
protected _styles: GridCSSStyleSheet;
protected _styles: GridHTMLStyleElement;
/** @internal flag to keep cells square during resize */
protected _isAutoCellHeight: boolean;
/** @internal limit auto cell resizing method */
Expand Down
14 changes: 6 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export class Utils {
* @param parent to insert the stylesheet as first child,
* if none supplied it will be appended to the document head instead.
*/
static createStylesheet(id: string, parent?: HTMLElement, options?: { nonce?: string }): CSSStyleSheet {
static createStylesheet(id: string, parent?: HTMLElement, options?: { nonce?: string }): HTMLStyleElement {
const style: HTMLStyleElement = document.createElement('style');
const nonce = options?.nonce
if (nonce) style.nonce = nonce
Expand All @@ -216,7 +216,7 @@ export class Utils {
} else {
parent.insertBefore(style, parent.firstChild);
}
return style.sheet as CSSStyleSheet;
return style;
}

/** removed the given stylesheet id */
Expand All @@ -227,12 +227,10 @@ export class Utils {
}

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

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down