Allowing user to add a global stylesheet to canvas dynamically
Question
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
Answers (3)
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
I got it! I removed it by using an id on the link tag.
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
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2936
QUESTION: Inline style loads on id instead of class
Hello, I have been using your tool for a while and I decided to upgrade to a newer version(from v0.12.37 to v0.16.3 + gjs-preset-newsletter...
Issue #3401
Can style manager always generate IDs instead of using already defined element's class ?
When we style an element in the editor, if this element doesn't have neither a class or an id, the style manager generate a new id with the...
Issue #3004
QUESTION : custom style properties to change class
Hi, Thanks for this good editor. In my project, I want to adjust classes of components using the style manager. For example, I am using boo...
Issue #2378
[QUESTION] Modifying global CSS in style manager
We want to achieve the following:Mark our block HTML elements with some classes like:Add some styles that define CSS classes like:Using Gra...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.