When you delete a component, duplicate classes between components will be deleted.
You should avoid putting shared styles in component-related ones. Check this discussion on how to prevent this: https://github.com/GrapesJS/grapesjs/discussions/5968
Read full answer below βQuestion
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
Chrome 126.0.6478.127(Windows)
Reproducible demo link
https://jsfiddle.net/t06s4oLb/8/
Describe the bug
How to reproduce the bug?
- Add section1 to canvas
- Add section2 to canvas under section1
- remove section2 from canvas.
- The style of section1 will be broken.
https://github.com/GrapesJS/grapesjs/assets/14024165/097dbe62-d8d3-4270-930d-5668ef10c2df
What is the expected behavior?
I would like it to not be deleted even if separate components have the same class.
What is the current behavior? The reason is that both section1 and section2 have .flex classes, and if you delete section2 from canvas, the .flex css will be deleted from gjs-css-rules.
Code of Conduct
- I agree to follow this project's Code of Conduct
Answers (4)
You should avoid putting shared styles in component-related ones. Check this discussion on how to prevent this: https://github.com/GrapesJS/grapesjs/discussions/5968
As a temporary workaround, styles for each component are prefixed with id.
import { html as hero1html, css as hero1css } from './data/hero-1';
import { html as content1html, css as content1css } from './data/content-1';
const sources = [
{
id: 'hero-1',
name: 'Hero 1',
category: 'Hero',
html: hero1html,
css: hero1css,
class: 'bg-gray-100 p-5',
},
{
id: 'content-1',
name: 'Content 1',
category: 'Content',
html: content1html,
css: content1css,
class: 'p-5',
}
]
const escapeTailwindClasses = (str, id) => {
return str.replace(/(\.[a-z0-9_-]+)(:\w+)/gi, (match, p1, p2) => {
return `.${id}${p1.replace('.', '')}\\${p2}`;
});
};
export const importBlocks = (editor, options = {}) => {
const domc = editor.DomComponents;
const blockManager = editor.BlockManager;
sources.forEach(source => {
const prefix = source.id + '-';
const prefixedHtml = source.html.replace(/class="([^"]*)"/g, (match, className) => {
return `class="${className.split(' ').map(cn => prefix + cn).join(' ')}"`;
});
const prefixedCss = source.css.replace(/\.([a-z0-9_-]+)(?=\s*[{,])/gi, (match, className) => {
return '.' + prefix + className;
});
console.log(escapeTailwindClasses(prefixedCss, prefix))
const prefixedClasses = source.class.split(' ').map(cn => prefix + cn).join(' ');
domc.addType(source.id, {
model: {
defaults: {
attributes: { class: prefixedClasses },
components: prefixedHtml,
styles: escapeTailwindClasses(prefixedCss, prefix),
},
},
});
blockManager.add(source.id, {
category: source.category,
id: source.id,
label: source.name,
content: {
type: source.id,
},
});
});
}
@artf thank you for your reply. I would like to confirm #5968
Thanks for reporting this, @ihatov08.
Great question about When you delete a component, duplicate classes between components will be deleted.. The recommended approach with StyleManager is to use the event-driven API.
Start here:
- Check the GrapesJS documentation for your specific module
- Look for the
on()event listener method - Most operations can be achieved by listening to editor and component events
Common patterns:
// Listen for changes
editor.on('change', () => console.log('something changed'));
// Component lifecycle
editor.on('component:mount', (c) => console.log('component ready', c));
editor.on('component:update', (c) => console.log('component updated', c));
If you're still stuck:
- Share a minimal CodeSandbox reproduction
- Include what you've already tried
- Mention your GrapesJS version
- The community is here to help!
Related Questions and Answers
Continue research with similar issue discussions.
Issue #4754
Closing `</script>` tag not escaped for script properties
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome Version 107.0.5304.122 (64-bit) Rep...
Issue #4576
Clicking 'wrap for style' button affects the inner components of other components inside the same text box
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Chrome 104.0.5112.101 Reproducible demo link...
Issue #6706
TypeError: e.getRoot is not a function (Race condition in Style Manager)
GrapesJS version [x] I confirm to use the latest version of GrapesJS What browser are you using? All browsers (Chrome, Safari, Firefox, Edg...
Issue #5086
Style:property:update and Property onChange trigger excessively
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome Version 112.0.5615.137 (Official Bu...
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 βRelated tutorials
In-depth guides on the same topic.
Tutorial
Find the Right GrapesJS Plugin in Seconds: Smarter Discovery Is Live
We're shipping a set of discovery upgrades. New label filters, a proper compatibility switch for GrapesJS vs Studio, one-click and a smarter sort bar.
Tutorial
GrapesJS vs Webflow vs Tilda: What to Choose for Your Business in 2026
Choosing the right website platform in 2026 is no longer just about building a site
Tutorial
GJS Market 2.0 - Donations, Tracking, Labels and Better Product Discovery
Weβve rolled out a new set of GrapesJS marketplace updates across GJS Market, focused on improving how creators distribute products
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.