Theme JSON: Define preset CSS vars for blocks based on feature selectors#10869
Theme JSON: Define preset CSS vars for blocks based on feature selectors#10869aaronrobertshaw wants to merge 4 commits intoWordPress:trunkfrom
Conversation
Backport of WordPress/gutenberg#75226 Updates the global styles engine to generate preset CSS custom properties on feature-specific selectors rather than only the block's root selector. This enables blocks whose root selector targets an inner element to have preset CSS vars output on the appropriate outer wrapper selector.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
The test failure is related. This will need to be rebased after #10628 lands. Alternatively, I might be able to update the test to use an older preset in theme json, e.g. aspect ratio or something. If I go that route I'll update the Gutenberg PR to match |
Co-authored-by: Weston Ruter <westonruter@gmail.com>
81e988f to
027feba
Compare
| * @param array<string, string|array> $feature_selectors The block's feature selectors map. | ||
| * @param string $feature_key The feature to look up (e.g. 'dimensions'). | ||
| * @param string $default_selector Fallback selector. |
There was a problem hiding this comment.
Array values, string|array, can array be made more specific? If not then it should probably be just:
| * @param array<string, string|array> $feature_selectors The block's feature selectors map. | |
| * @param string $feature_key The feature to look up (e.g. 'dimensions'). | |
| * @param string $default_selector Fallback selector. | |
| * @param array<string, mixed> $feature_selectors The block's feature selectors map. | |
| * @param string $feature_key The feature to look up (e.g. 'dimensions'). | |
| * @param string $default_selector Fallback selector. |
There was a problem hiding this comment.
The feature selectors could be an array of string key/value pairs, where the value could be a string or a further array of string key value pairs for block support sub features. The block.json schema outlines it here but it would looks something like:
{
"root": string,
"border": string | array<string, string>,
"color": string | array<string, string>,
"dimensions": string | array<string, string>,
"spacing": string | array<string, string>,
"typography": string | array<string, string>
}
I will update this to array<string, string|array<string, string>> for complete accuracy. If it is too verbose we can switch it up to the array<string, mixed> if desired.
| return $feature; | ||
| } | ||
|
|
||
| return $feature['root'] ?? $default_selector; |
There was a problem hiding this comment.
Is it possible that $feature['root'] could possibly not be a string? If not, this would be safer as:
| return $feature['root'] ?? $default_selector; | |
| if ( isset( $feature['root'] ) && is_string( $feature['root'] ) ) { | |
| return $feature['root']; | |
| } | |
| return $default_selector; |
Trac ticket: https://core.trac.wordpress.org/ticket/64598
This PR brings the changes from the following Gutenberg PR to core:
WordPress/gutenberg#75226
Description
Updates the global styles engine and theme JSON processing to generate preset CSS custom properties (vars) for blocks based on their feature-specific selectors rather than only the block's root selector. This enables blocks whose root selector targets an inner element to have preset CSS vars output on the appropriate outer wrapper selector.
See: WordPress/gutenberg#74242
Testing
phpunit --filter=WP_Theme_JSON_Testtest_get_stylesheet_preset_css_vars_use_feature_selectortest.dimensions.rootselector different from the block's root selector), definedimensionSizespresets for that block in theme.json, and confirm the CSS vars are output on the feature selector rather than the block's root selector.