Allowing user to add a global stylesheet to canvas dynamically
Hi, I wanted to provide the user the option to load a custom stylesheet to the canvas. This is so that the user can apply the custom defined styles to the control as class instead of inline style and also to maintain uniformity among the various users using the grapesjs based editor tool to generate the html template. Does Grapesjs already support loading stylesheet dynamically? Any guidance with this issue will be of great help to me.
Thank you, Anusha
Hi @anushabivera if you want to load the stylesheets on init (local and with cdn) you can simply add the css/js links to the canvas property on editor init function, like this:
const editor = grapesjs.init({
...
canvas: {
styles: [
'example-of-css-file.css',
'another-css-goes-here'
],
scripts: [
'example-of-js-file.js',
'another-js-goes-here'
]
}
});
If you want to add them dynamically you can try this (example with materialize):
const head = editor.Canvas.getDocument().head;
head.insertAdjacentHTML('beforeend', `<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">`);
head.insertAdjacentHTML('beforeend', `<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">`);
head.insertAdjacentHTML('beforeend', `<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>`);
BTW: This question has already been asked, any doubts you can get more info below (mostly 66 and 1739): #66 #195 #444 #473 #966 #1739
Thank you @RJCAM . My plan is to provide the functionality as an attribute of the canvas and the user can enter the url of the custom stylesheet as value of the attribute
Hi @RJCAM. Thank you for this solution. If I want to remove the dynamically added styles and scripts, how can I do that?
@theSC0RP in my case I made a head edit function. So now I'm able not just to add external content but also add/edit meta tags and other tags inside the head. I made it similar to the edit html function (if you search you see the code) the only difference is this when I click to save and edit in the modal:
const gjs_head = editor.Canvas.getDocument().head;
btnEditHEAD.onclick = function() {
let header = codeViewerHEAD.editor.getValue();
gjs_head.innerHTML = header;
head = header; // SAVING LAST CHANGES IN GLOBAL VARIABLE
modal.close();
};

@RJCAM Thanks for the suggestion. I am uploading a css file asset and then I want to add it to the template and remove it whenever I want (Using a checkbox) When the checkbox is ticked the file should be added to the template and when unchecked it should be removed.
I got it! I removed it by using an id on the link tag.
Hi, we have done global header plugin - https://gjs.market/products/super-header-for-grapesjs
Hi,
I have implemented grape JS editor in my project. My project admin panel have settings to switch themes or adjust the styling like primary color etc for the site.
This is how the code looks for the CSS variables const onPrimary = getContrastTextColor(themeStyle.primaryColor); const onSecondary = getContrastTextColor(themeStyle.secondaryColor); const onBackground = getContrastTextColor(themeStyle.backgroundColor);
const variableMap = { "--primary-color": themeStyle.primaryColor, "--secondary-color": themeStyle.secondaryColor, "--background-color": themeStyle.backgroundColor, "--heading-color": themeStyle.headingColor, "--button-color": themeStyle.primaryColor, "--heading-size": themeStyle.headingSize, "--text-size": themeStyle.textSize, "--on-primary-color": onPrimary, "--on-secondary-color": onSecondary, "--on-background-color": onBackground, // Heading sizes for h1-h6 "--h1-size": themeStyle.h1Size, "--h2-size": themeStyle.h2Size, "--h3-size": themeStyle.h3Size, "--h4-size": themeStyle.h4Size, "--h5-size": themeStyle.h5Size, "--h6-size": themeStyle.h6Size, };
Object.entries(variableMap).forEach(([key, value]) => { if (value !== undefined && value !== null) root.style.setProperty(key, value); }); };
These variables are used in the theme CSS files. Now I have the Grape JS editor in my admi panel where I can generate any content and on first I want the buttons, text etc to use the theme colours.
How can I integrate that in the Grape JS editor as currently the Grape JS editor marked them as undefined.
Any help would be appreciated.
Thanks.
1 - Inject your CSS variables directly into the iframe document
After you initialize your editor, grab the iframe’s document and add the variables to its :root or body.
editor.on('load', () => { const iframe = editor.Canvas.getFrameEl(); const iframeDoc = iframe.contentDocument;
Object.entries(variableMap).forEach(([key, value]) => { if (value !== undefined && value !== null) { iframeDoc.documentElement.style.setProperty(key, value); } }); });
This means your iframe content will have:
:root { --primary-color: #....; --secondary-color: #....; }
So any CSS inside the GrapesJS canvas that uses var(--primary-color) will work.
✅ Advantage: Reuses your existing theme variables.
2 - Alternatively, inject your theme CSS file into GrapesJS
If your app loads a theme CSS file that already defines variables (or has classes using them), you can add that stylesheet to the GrapesJS canvas.
editor.on('load', () => { const iframeHead = editor.Canvas.getDocument().head; const link = document.createElement('link'); link.rel = 'stylesheet'; link.href = '/path/to/your/theme.css'; iframeHead.appendChild(link); });
✅ Advantage: Keeps all your styles centralized.