BUG: editor.Css.setRule API not working as expected, it is not updating the rule existing rule.
Question
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
Version 112.0.5615.137 (Official Build) (arm64)
Reproducible demo link
https://jsfiddle.net/yhj6qa1u/6/
Describe the bug
How to reproduce the bug?
- Select a component and add some style using Style Manager.
- Use
editor.Css.setRuleto set some style for that particular component, notice the previous styles are removed but as per the documentation it should update the CSS rule.
What is the expected behavior? It should update the existing rule.
What is the current behavior? It removes the existing rule rather than updating the existing rule.
https://github.com/GrapesJS/grapesjs/assets/102785785/28560d08-156a-46da-b901-3aec0636a62a
Code of Conduct
- I agree to follow this project's Code of Conduct
Answers (2)
Hi @FaisalShaikhHA this is expected behaviour but I guess the documentation of setRule is a bit misleading. The rule is actually updated (it doesn't create a new one with the same selector) but styles are simply replaced.
This is what you should do if you need to add new styles instead of replacing them
const selector = `#${this.getId()}`;
const rule = editor.Css.getRule(selector);
editor.Css.setRule(selector, {
...rule?.getStyle(),
background: 'black'
});
I guess I can add a new addStyles?: boolean option to handle similar use cases.
Thanks @artf for the clarification
Related Questions and Answers
Continue research with similar issue discussions.
Issue #5086
BUG: 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...
Issue #4323
BUG: Performance issue while dragging component to canvas when you have 2k component on canvas
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Google Chrome Version 101.0.4951.41 (Officia...
Issue #4752
BUG: Component with pointer-events: 'all' (except wrapper) or editable component are draggable & droppable to itself.
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Chrome 107.0.5304.121 (Official Build) (arm6...
Issue #5689
BUG: <> is introduced at the beginning editor.getHtml()
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Chrome Version 121.0.6167.185 (Official Buil...
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.