setStyle() expands shorthand CSS properties, causing Style Manager values to lose their preselected state #6816
Replies: 2 comments
|
Not intentional, and not GrapesJS doing it. It's the browser.
The expansion is in the CSS parser, which is the real CSSOM: const el = document.createElement('style');
el.innerHTML = str;
document.head.appendChild(el);
const sheet = el.sheet; // BrowserParserCss.ts:277then it reads back by index: for (var i = 0, len = stl.length; i < len; i++) {
const propName = stl[i]; // :102Numeric iteration of a So the trigger is a CSS round-trip, not Style Manager side: If you want shorthands preserved, |
|
This is due the the browser’s CSS parser as mentioned, it’s worth noting that there’s already a plugin for replacing it with postCSS which should solve your issue https://github.com/GrapesJS/parser-postcss |
Uh oh!
There was an error while loading. Please reload this page.
Currently, when setting styles on a component in the canvas using
setStyle(), GrapesJS expands CSS shorthand properties into their individual properties.For example, I set the style using:
This gets converted to:
Problem
This causes an issue with the Style Manager.
The original
bordershorthand property is no longer available as:Instead, it is split into the four individual properties.
As a result, when selecting the component again, the
borderproperty in the Style Manager is not correctly preselected/populated with the existing value.The CSS is visually equivalent, but the original shorthand representation is lost, which affects the editing experience.
Expected behavior
I would expect:
to preserve:
so that the
borderproperty remains available and correctly preselected in the Style Manager.Alternatively, if expanding shorthand properties is intentional, the Style Manager should be able to resolve the individual properties back to the corresponding composite property and display the existing value.
Question
Is the expansion of shorthand properties by
setStyle()intentional?If so, should the Style Manager reconstruct the shorthand value from the individual properties, or should
setStyle()preserve the shorthand property when it is provided as a string?I’d like to understand the intended behavior before working on a possible fix.
All reactions