Issue #3000💬 AnsweredOpened September 4, 2020by alemenciones0 reactions

How to remove a style rule of CssCompoer?

快速解答by longdoan7421

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 m...

Read full answer below ↓

Question

Hi man, im trying to remove a style-rule, some antithesis of CssComposer.setRule(".selector") ?

Thanks you very much!

Answers (2)

longdoan7421September 4, 2020

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 remove of 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 filter of Backbone Collection to find the rules. Finally, remove it `.
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')
artfSeptember 8, 2020

@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.

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...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins →
Premium option

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.

All tutorials →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.