Global Styles Engine: fix variation element selector for multi-token block roots
What
Fixes #79318.
When a block style variation defines elements.button styles in a theme.json partial (e.g. a tonal button variation with custom colors), the Global Styles Engine generates a wrong descendant selector that never matches anything in the DOM:
```css /* ✕ Before — never matches */ :root :where(.wp-block-button.is-style-tonal .wp-block-button__link .wp-element-button) { background-color: #e0f2fe; color: #0369a1; } ```
The problem: core/button's root selector is .wp-block-button .wp-block-button__link (two tokens). When the variation element path called scopeSelector(variationSelector, ELEMENTS['button']), it used the full variation selector (.wp-block-button.is-style-tonal .wp-block-button__link) as the scope, producing a descendant combinator between .wp-block-button__link and .wp-element-button. But both classes live on the same <a> element — there is no .wp-element-button nested inside .wp-block-button__link.
Fix
Scope element selectors to only the first token of the variation selector (the block wrapper, e.g. .wp-block-button.is-style-tonal) instead of the full variation selector:
```css /* ✓ After — correctly matches the */ :root :where( .wp-block-button.is-style-tonal .wp-element-button, .wp-block-button.is-style-tonal .wp-block-button__link ) { background-color: #e0f2fe; color: #0369a1; } ```
The same fix is applied to the analogous path for inner-block element styles within a variation (e.g. a group variation that styles elements of an inner button block).
This brings the JS output into parity with lib/class-wp-theme-json-gutenberg.php, which already uses get_block_style_variation_feature_selector (compound) for the same path.
Changes
-
packages/global-styles-engine/src/core/render.tsx— two call sites changed:- Variation inner-element path:
scopeSelector(variationSelector, ...)→scopeSelector(variationSelector.split(' ')[0], ...) - Variation inner-block element path: scopes to inner block's wrapper token rather than its full root selector
- Variation inner-element path:
-
packages/global-styles-engine/src/test/render.test.ts— regression test asserting the compound selector form for acore/buttontonal variation withelements.buttoncolor styles. Fails before the fix, passes after.
Testing
- Register a block style variation for
core/buttonand addelements.button.colorstyles in a theme.json partial or the Styles panel. - Add a Button block in the editor, apply the variation.
- Before: variation colors do not apply (wrong selector). After: colors apply correctly.
Or run the unit test directly:
```bash
npx jest packages/global-styles-engine/src/test/render.test.ts --no-coverage
-t "should use a compound (not descendant) selector"
```
Before / After
Before — tonal button variation colors not applied (wrong descendant selector):
After — tonal button variation colors correctly applied:
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 props-bot label.
If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
Co-authored-by: MicahelE <[email protected]>
Co-authored-by: ozgursar <[email protected]>
Co-authored-by: Jiwoon-Kim <[email protected]>
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.
:wave: Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @MicahelE! In case you missed it, we'd love to have you join us in our Slack community.
If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.