Issue #3000Opened September 4, 2020by alemenciones0 reactions

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)

longdoan7421September 4, 20200 reactions

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, 20200 reactions

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

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.