Issue #391Opened October 7, 2017by tomichal7 reactions

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)

danfitz36October 10, 20173 reactions

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.

artfMay 24, 20182 reactions

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);
      	}
    });
devmohitagarwalNovember 4, 20192 reactions

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:

  1. enclose the cssString with your custom class. say MyCustomClass.
  2. 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.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.