[Feature suggestion] optionally insert styles from StyleManager with !important
Question
I'd like to suggest a feature that can be enabled through config: styleAsImportant: true (defaults to false, i.e. non-breaking). I thought this was already possible by configuring the cssComposer, though I've now learned that this is not the case (see below for an explanation).
I am working with templates that sometimes were generated from pre-compilers that excessively use the !important flag. For example a button in a template might be styled as:
._style_wxcCz {
border-style: solid !important;
margin: 0 !important;
padding-right: 20px !important;
padding-top: 11px !important;
padding-bottom: 11px !important;
background-color: #377037 !important;
font-family: ff-clan-web-pro, "Helvetica Neue", Helvetica, sans-serif !important;
font-weight: 600 !important;
border-color: #377037 !important;
border-radius: 0 !important;
padding-left: 20px !important;
border-width: 2px !important;
color: #ffffff !important;
cursor: pointer !important;
display: inline-block !important;
font-size: 14px !important;
line-height: 18px !important;
outline: none !important;
overflow: visible !important;
position: relative !important;
text-align: center !important;
text-decoration: none !important;
text-transform: uppercase !important;
vertical-align: middle !important
}
As a result, styling this button with Grapes has absolutely no effect. The most obvious way to make it work with grapes, is to ensure that:
- the grapes styling also has
!important(this feature) - the grapes styling comes after the template's styling (should already be the case, right?)
I guessed that the proper way would be to make CssRule.js#36 configureable. I tried to set this important flag using:
var editor = grapesjs.init(
{
cssComposer: { important: true },
...
}
);
But this does not seem to have an effect. Moreover, when I hardcode it to true in the code, I see that there are two issues with this implementation:
- it also adds
!importantto rules that come from the template. This feature suggestion is specifically for rules that are manually added through the style manager, because these should overwrite the template's styles. Otherwise there is no added value. - it adds
!importanttwice if there is already an important statement in rules from the template, causing the rendering to fail due to having invalid css properties:
Therefore my suggestion is to create a new config value styleAsImportant: true (defaults to false, i.e. non-breaking). After some initial debugging I found that this change makes this happen:
style_manager/index.js line 283:
from:
rule = cssC.add(valid, state, deviceW);
to:
rule = cssC.add(valid, state, deviceW, { important: config.styleAsImportant });
This seems to work well. It does not add !important to rules from the template, and it does add it to rules set by the style manager. This is the desired behavior.
Do you agree? Should I submit a PR?
Cheers
Answers (3)
added PR at #1056
It might be a good idea, but what about improving those events (eg. pass model/pr to callbacks) to allow such customizations
@tommedema Components and CssRules implement Styleable, therefore you would get it in all case.
do you mean that we'd have to then write plugins to listen to those events and add additional styling to the component?
Not additional, you overwrite the style triggered on that model, eg.
// eg. your configs
const styleImportant = 1;
// ...
editor.on('styleable:change', (model, property) => {
const value = model.getStyle()[property];
console.log('Styled ', property, value);
if (styleImportant) {
model.addStyle({ [property]: value + ` !important` });
}
});
Related Questions and Answers
Continue research with similar issue discussions.
Issue #996
[QUESTIONS] change attribute value that is generated through trait
Now, let's consider that I wrote new into the trait value but in attribute :items, I want to append prefix like item.text. to that value So...
Issue #859
[QUESTIONS] Change canvas style
Hi there, I have some question about canvas style, in this case i have config like this. By default i use css with name builder-desktop.css...
Issue #908
[FEATURE REQUEST] Add custom classes to panels and other editor elements
When using a UI framework that provides custom classes, it would be nice to be able to use these classes on things like panels, so you woul...
Issue #2011
Building gjs-components
Hello all, Im using grapesjs-webpage-preset plugin and I would like to combine my gjs-html with my gjs-css so that all of the styles are in...
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.