WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.34k stars 4.13k forks source link

Output block support values as custom css properties in style engine #51749

Open gaambo opened 1 year ago

gaambo commented 1 year ago

What problem does this address?

Block supports like color currently have a specific css property configured (in WP_Style_Engine::BLOCK_STYLE_DEFINITIONS_METADATA). That means that in CSS we don't have access to those values to use them for other properties, other selectors, in media-queries etc.

Examples:

These example include mostly very custom/specific designs, which use custom CSS and can't be handled in the editor/theme.json (and probably never will, because it's out of scope).

What is your proposed solution?

Proposed solution 1: Style Engine outputs CSS variables

I propose that in addition to that property, the style engine also sets a css variable on the block/element.

Example:

color: #ffffff;
--wp--block--color: #ffffff;
background-color: #2c2926;
--wp--block--background-color: #2c2926;

I'm just using a made up prefix here, to avoid any collisions.

In this way, theme developers can reuse the block supports value in any way they need to. Examples include pseudo elements, hover effects, changing it on media-queries (eg dark scheme). This would probably also reduce the times where __experimentalSelector is needed in blocks.

IMHO this could also solve some problems with css "layering" for core/theme/user. I think by using css variables and using CSSs cascading this would work great as well. Because for a block I can define a default value for the CSS-variable but it can be overwriten by a block support, a variation, a style-variation,...

Proposed solution 2: Style Engines output generation is filterable/extendable

I wanted to give this a go in a custom style engine implementation. Unfortunately the BLOCK_STYLE_DEFINITIONS_METADATA is hardcoded/not filterable, WP_Style_Engine is a final class and neither get_css_declarations nor get_css_declarations is filterable. I understand that the style engine is final, because its kind of still in flux. But this would be a great use case for a custom wp style engine implementation - for custom themes that could be interesting, but that's probably another issue.

So I guess this could be another solution, to allow changing the creation of styles from the style engine.

gaambo commented 1 year ago

I'd like to add, that I actually preferred the earlier blockGap implementation, which output the block gap as a css variable. This way I could decide in the CSS on which property to set this value (gap, padding, margin) and it was still overwriteable by theme/user/block etc.