Custom component styles are not applied after deletion and re-addition
I'm facing a similar issue, where the re-added component has the correct styles property: <img width="1112" height="17" alt="Image" src="https://github.com/user-attachments/assets/474011c4-b1be-48df-9aa8-14c3dcdfc68d" /> But if I use the "View Code" button, the CSS was not injected, in my case they aren't even in the...
Read full answer below βQuestion
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
Chrome v143.0.7499.193
Reproducible demo link
https://codesandbox.io/p/sandbox/2l3887
Describe the bug
How to reproduce the bug?
- Open the page and load the GrapesJS editor
- Select the Row component and delete it
- Click the Row component in the left sidebar to add a Row to the canvas
What is the expected behavior? The newly added Row component should display correct styles (white background, border, border-radius, padding, etc.)
What is the current behavior? After re-adding the Row component, the .gjs-row CSS class defined in the styles property is not applied to the DOM element, resulting in missing styles and the component displaying as an unstyled plain text container
If is necessary to execute some code in order to reproduce the bug, paste it here below:
// Row component definition
editor.Components.addType('row', {
model: {
defaults: {
name: 'Row',
tagName: 'div',
draggable: true,
droppable: true,
attributes: { class: 'gjs-row' },
styles: `
.gjs-row {
display: flex;
flex-wrap: wrap;
gap: 16px;
padding: 16px;
background-color: #fff;
border: 1px solid #e5e7eb;
border-radius: 8px;
margin-bottom: 16px;
}
`,
components: [
{ type: 'column' },
{ type: 'column' },
{ type: 'column' }
]
}
}
});
Symptoms:
- The first added Row component displays styles correctly
- After deletion and re-addition, the Row component styles are lost
- The class attribute exists in the DOM but the CSS rules are not injected
Code of Conduct
- I agree to follow this project's Code of Conduct
Answers (4)
I'm facing a similar issue, where the re-added component has the correct styles property:
<img width="1112" height="17" alt="Image" src="https://github.com/user-attachments/assets/474011c4-b1be-48df-9aa8-14c3dcdfc68d" />But if I use the "View Code" button, the CSS was not injected, in my case they aren't even in the DOM. The component itself works, because when it was first added, the styles were in fact injected and working ok. It's only after removing and re-adding that it fails.
I've also tested using the "component.replaceWith(newComponent)" function instead of "component.remove()" and "editor.addComponents(newComponent)".
Code example:
//Component Definition
domComponents.addType('my-type', {
model: {
defaults: {
tagName: 'div',
name: 'My Type',
attributes: { 'data-gjs-type': 'my-type' },
}
}
});
//Adding component (here the styles work)
editorInstance.Components.addComponent({
type: 'my-type',
attributes: { id: 'my-id' },
components: //several text and image components,
styles: '#my-id #ir2o {\n padding: 5px;\n left: 94px;\n top: 120px;\n position: absolute;\n}\n\n ....'
}, { at: 0 });
//Retrieving the component in another operation
let myComponent = editorInstance.Components.getComponents().find((component: Component) => component.getType() === 'my-type');
//Replacing it (or removing and re-adding) with an updated one
myComponent.replaceWith({
type: 'my-type',
attributes: { id: 'my-id' },
components: //Updated several text and image components,
styles: '#my-id #ir2o {\n padding: 5px;\n left: 94px;\n top: 140px;\n position: absolute;\n}\n\n ....' //Updated styles
});
//The components are successfully added but the updated styles are not injected.
@mdmontesinos your usage is not correct, styles has to be defined at the component definition, and it's not expected to change. Also, it's wrong to use IDs for component definition if the component is expected to be reused, the ID will be recreated, rely on classes, especially for styles.
But the report from @shery seems to be legit; there might be an issue with the caching of styles.
@artf I'm using an ID because there will always be a single instance of that component in my project. My use case is that a component is designed externally to be used as a "background" and then imported into projects that use it. Therefore, when the external component is updated, I need to also update it in my project, which is why I'm trying to recreate it, and it might have its components or styles modified. Is there any way that I can force the styles to be refreshed?
Thanks for reporting this, @shery.
Great question about Custom component styles are not applied after deletion and re-addition. 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 #4573
parseStyle is not a function
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Firefox 104.0.1Reproducible demo link https:...
Issue #4196
Ids gets changed while trying to set components on the canvas using json
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome Reproducible demo link no link Desc...
Issue #5990
When you delete a component, duplicate classes between components will be deleted.
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome 126.0.6478.127(Windows) Reproducibl...
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...
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
GrapesJS vs Webflow vs Builder.io vs Puck: Which Visual Builder to Choose in 2026
A practitioner's 2026 comparison of GrapesJS, Webflow, Builder.io, and Puck β pricing, AI features, lock-in, and a one-line rule for picking the right one
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
Why use grapesjs shadcn for your template builder
Skip months of editor plumbing and start with a fully working visual builder β polished UI included.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.