How to remove a style rule of CssCompoer?
Question
Hi man, im trying to remove a style-rule, some antithesis of CssComposer.setRule(".selector") ?
Thanks you very much!
Answers (2)
Unfortunately, GrapesJS does not have any built-in function like CssComposer.removeRule.
However, you could do that with a workaround like this:
- Get all rules by using
CssComposer.getAll(), it will return you a Backbone Collection of all rules. - I assume that you have the rule model which should be removed, you can use method
removeof Backbone Collection to remove it.
const ruleModel = /* the model of rule which you want to remove */;
const allRules = editor.CssComposer.getAll();
allRules.remove(ruleModel);
- If you don't already have rule model, you first need to get the model of selector. Then, you could use method
filterof Backbone Collection to find the rules. Finally,removeit `.
function removeRule(willBeRemoveSelector) {
const willBeRemoveSelectorModel = editor.SelectorManager.get(willBeRemoveSelector);
const allRules = editor.CssComposer.getAll();
const willBeRemoveRules = allRules.filter(rule =>
return rule.get('selectors').has(willBeRemoveSelectorModel)
});
allRules.remove(willBeRemoveRules);
}
removeRule('.some-class')
@longdoan7421 is correct but in the second solution I'd use CssComposer.getRule directly
const willBeRemoveRules = editor.CssComposer.getRule(willBeRemoveSelector);
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3421
[QUESTION] How to remove class selector css rule
I'm using componentFirst set to true to let my users apply needed style to component they selected. Selecting a component which is applied...
Issue #630
(question) Is it possible to set categories dinamically?
Thanks for your component, I like it very much. I have some dinamic blocks (I have tabs and when I change tab, I add group of blocks and re...
Issue #2387
How to change the default selected panel?
I'm trying to use defaultCommand: 'open-blocks' but the style manager is the default selected one Thanks!
Issue #1899
React with GrapeJS- without plugin
Hi, How can I use grapesjs with react? I don't want to use the plugin- I have made some changes and I want to use the editor with react and...
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.