Namespace CSS?
Question
Hi there, Is there a way to setup the grapesjs editor such that the CSS generated for the HTML content is namespaced? In other words, how to avoid the style rules defined for the content created/edited in the editor to leak out to the rest of the website page?
In the case of my website the user can edit a large part of the page, but not all of it - the rest of the page HTML content and CSS is dynamically generated by the server (this is a Rails + Angular site).
I find that if e.g. I use a row with columns, the CSS generated by grapesjs editor will have a definition for a "row" class (i.e. ".row { ... }"). However, I use a CSS library that provides its own definitions for a "row" class, and so the grapesjs definition conflicts with my site-wide CSS.
Thanks for any help and tips to this issue!
Answers (3)
do you think you could just prefix all class names generated by grapejs? if instead of .row you use .grape-row or whatever (i'd probably make the prefix a variable), you'll mostly get the effect that @tomichal is after.
Actually, you can add a listener on new selectors and add a prefix there
const prefix = 'myprefix-';
editor.on('selector:add', selector => {
const name = selector.get('name');
if (selector.get('type') === editor.SelectorManager.Selector.TYPE_CLASS &&
name.indexOf(prefix) !== 0) {
selector.set('name', prefix + name);
}
});
HI, People looking to sandbox, prefix the exported CSS can give this a try, we had the same use case and took the sass approach.
To do so, I used sass.js (https://github.com/medialize/sass.js/)
Steps:
- enclose the cssString with your custom class. say MyCustomClass.
- Run sass.compile from the above library to convert the scss to css which is now sandboxed with the class given() ,
This way you wont face the issue of conflicts.
This answer was copied from another question. #2363
Related Questions and Answers
Continue research with similar issue discussions.
Issue #826
[Bug] Element auto generated id not added back after custom id is used and then cleared
Hi, I think there is a bug related to the generation of element id attributes. When modifying element's style the value of the element's id...
Issue #2383
Custom component not pulling through style on first add.
Fiddle replicating the issue: https://jsfiddle.net/gd8mo3e4/1/ I have a custom component for creating a gallery of pdf documents. There's a...
Issue #364
Sort How Block Categories are displayed.
Is there a method to set how the categories are shown in the Block tab?
Issue #1368
Resize canvas
Is there a way to have a dynamic resizing of the canvas? i.e not using the Device Manager, but having a drag and drop of the width, similar...
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.