AdoptStylesheet not supported in grapesJS
This issue is due to how AdoptStylesheet works and it's similar to this one from lit-element. Basically, you can't share the same CSSStyleSheet across multiple documents, so you have to recreate it manually, here below is a code for custom elements, in order to support adoptedStyleSheets.
Read full answer below ↓Question
Version:
You can get the version by typing grapesjs.version into the console
0.17.22
Are you able to reproduce the bug from the demo?
- Yes
- No
What is the expected behavior? Should be able to see the web component in similar fashion as it is outside the grapesJS container
Describe the bug detailed Grapesjs not able render the css from constructable stylesheet.
What is the current behavior? Able to load the component without styling
Are you able to attach screenshots, screencasts or a live demo?
CodeSandbox Starter template https://codesandbox.io/embed/frosty-payne-3fhd7?fontsize=14&hidenavigation=1&theme=dark
Answers (3)
This issue is due to how AdoptStylesheet works and it's similar to this one from lit-element. Basically, you can't share the same CSSStyleSheet across multiple documents, so you have to recreate it manually, here below is a code for custom elements, in order to support adoptedStyleSheets.
editor.Components.addType('custom-element', {
isComponent: (el) => (el.tagName || '').includes('-') && window.customElements.get(el.tagName.toLowerCase()),
view: {
init() {
// On init, the element is still part of the main document, so
// the original adoptedStyleSheets still exists
const shadowStyles = this.el?.shadowRoot?.adoptedStyleSheets;
if (shadowStyles) {
// By using setTimeout we'll jump on the step when the element is already rendered
// in the iframe document, here shadowRoot.adoptedStyleSheets is cleared
setTimeout(() => {
// We need to use the window of the iframe in order to
// avoid "stylesheets in multiple documents is not allowed" error
const win = this.el.ownerDocument.defaultView;
const adoptedStyles = shadowStyles
.map(s => Array.from(s.cssRules).map(r => r.cssText || '').join('\n'))
.map(css => {
const cssSheet = new win.CSSStyleSheet();
cssSheet.replaceSync(css);
return cssSheet;
})
this.el.shadowRoot.adoptedStyleSheets = adoptedStyles;
})
}
},
}
});
Is there any solution for using lit-element components 3.X with grapesJS?
Thanks for reporting this, @GaikwadShubham.
The issue with AdoptStylesheet not supported in grapesJS appears to be a race condition or state management timing problem. This typically happens when component lifecycle events and DOM modifications overlap, creating an inconsistent state.
What to try:
- Add a setTimeout wrapper to ensure the DOM has settled:
setTimeout(() => {
// your operation here
}, 0);
-
Check initialization order — make sure components are fully loaded before you interact with them
-
Use the editor's event system — listen to completion events:
editor.on('component:mount', (component) => {
// safe to interact with component here
});
Recommended next steps:
- Test with the latest GrapesJS version if you haven't
- Provide a minimal reproducible example (CodeSandbox) — this helps the team identify the root cause faster
- Include GrapesJS version, browser, and console errors in your report
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3454
RTE insert link does not update DOM
Version: "0.17.3" Are you able to reproduce the bug from the demo?[X] Yes[ ] No What is the expected behavior? After inserting link using d...
Issue #3613
Inconsistent Image Selection
Version: 0.17.19 Are you able to reproduce the bug from the demo?[X] Yes[ ] No Reproducible here. What is the expected behavior? Selecting...
Issue #3612
The grapesjs slider buttons, tabs are not working as expected
Version 0.17.19(grapesjs) Version 0.1.5(grapesjs-lory-slider) Version 1.0.6(grapesjs-tabs) Are you able to reproduce the bug from the demo?...
Issue #3131
Paste as plain text into Text component adds <font /> wrapper to pasted text
Version: 0.16.27 Are you able to reproduce the bug from the demo? [] Yes [X] No What is the expected behavior? Paste as plain text (text no...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.
Browse premium plugins →Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.