Skip to content

[Button Block]: add support for pseudo elements for the block and its variations on theme.json - #71418

Merged
scruffian merged 18 commits into
trunkfrom
try-pseudoelements-blocks
Nov 20, 2025
Merged

[Button Block]: add support for pseudo elements for the block and its variations on theme.json#71418
scruffian merged 18 commits into
trunkfrom
try-pseudoelements-blocks

Conversation

@MaggieCabrera

@MaggieCabrera MaggieCabrera commented Aug 29, 2025

Copy link
Copy Markdown
Contributor

What?

Closes #55359

Adds support for pseudo elements on the core/button block for ( ':hover', ':focus', ':focus-visible', ':active' ) at the theme.json level. This is also allowing the block's variations to control the same pseudo elements, so now we can style hover for the outline variation too.

Why?

This is needed ahead of #38277 since right now we are only supporting pseudo elements on links and button elements, we also want to support them at the block level. This PR brings them in alignment and opens the door to support things like :hover for other blocks that don't have an element (like group for example)

How?

By allowing the pseudo elements for the specific blocks in VALID_BLOCK_PSEUDO_SELECTORS, in a similar way we do for elements.

Testing Instructions

Add the following to your theme and check that it behaves as expected on the frontend:

"styles": {
	"blocks": {
		"core/button": {
			"color": {
				"background": "blue"
			},
			":hover": {
				"color": {
					"background": "green"
				}
			},
			":focus": {
				"color": {
					"background": "purple"
				}
			},
			"variations": {
				"outline": {
					":hover": {
						"color": {
							"background": "pink"
						}
					}
				}
			}
		}
	}
}

Testing Instructions for Keyboard

Screenshots or screencast

Screen Capture on 2025-09-05 at 11-51-00

@github-actions

github-actions Bot commented Aug 29, 2025

Copy link
Copy Markdown

Flaky tests detected in af55dd7.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/17399082575
📝 Reported issues:

@MaggieCabrera MaggieCabrera changed the title WIP: getting pseudo elements support for blocks Sep 5, 2025
@MaggieCabrera MaggieCabrera added [Type] Enhancement A suggestion for improvement. [Block] Buttons Affects the Buttons Block labels Sep 5, 2025
@MaggieCabrera
MaggieCabrera marked this pull request as ready for review September 5, 2025 09:57
@github-actions

github-actions Bot commented Sep 5, 2025

Copy link
Copy Markdown

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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @pablost88, @WinsleyJ.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: pablost88, WinsleyJ.

Co-authored-by: MaggieCabrera <onemaggie@git.wordpress.org>
Co-authored-by: scruffian <scruffian@git.wordpress.org>
Co-authored-by: mikachan <mikachan@git.wordpress.org>
Co-authored-by: jasmussen <joen@git.wordpress.org>
Co-authored-by: masteradhoc <masteradhoc@git.wordpress.org>
Co-authored-by: philhoyt <philhoyt@git.wordpress.org>
Co-authored-by: Zealth57 <elrae@git.wordpress.org>
Co-authored-by: joemcgill <joemcgill@git.wordpress.org>
Co-authored-by: carolinan <poena@git.wordpress.org>
Co-authored-by: richtabor <richtabor@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@MaggieCabrera

Copy link
Copy Markdown
Contributor Author

@jasmussen I'm going on sabbatical before this is going to land, but maybe we can figure out a UI for this even if we iterate on it later? I think it would be a big win, as you know there's been a lot of feedback asking for this feature over at #38277

@jasmussen

Copy link
Copy Markdown
Contributor

I'll put it on my radar! Enjoy the break.

@scruffian
scruffian force-pushed the try-pseudoelements-blocks branch from af55dd7 to 37cb2fc Compare November 17, 2025 09:30

@mikachan mikachan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is working well! I've left a few small suggestions.

Comment thread lib/class-wp-theme-json-gutenberg.php Outdated
Comment thread lib/class-wp-theme-json-gutenberg.php Outdated
Comment thread lib/class-wp-theme-json-gutenberg.php Outdated
Comment thread lib/class-wp-theme-json-gutenberg.php Outdated
Comment thread lib/class-wp-theme-json-gutenberg.php Outdated
$style_variation_declarations[ $style_variation['selector'] ] = static::compute_style_properties( $style_variation_node, $settings, null, $this->theme_json );

// Process pseudo-selectors for this variation (e.g., :hover, :focus)
$block_name = isset( $block_metadata['name'] ) ? $block_metadata['name'] : ( in_array( 'blocks', $block_metadata['path'], true ) && count( $block_metadata['path'] ) >= 3 ? $block_metadata['path'][2] : null );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're extracting the block name in three different places (line 2994, 3028, and 3070. Maybe we could move this to a helper function so it's extracted consistently in each place?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 005f04e

Comment thread lib/class-wp-theme-json-gutenberg.php Outdated
Co-authored-by: Sarah Norris <1645628+mikachan@users.noreply.github.com>
scruffian and others added 5 commits November 20, 2025 08:21
Co-authored-by: Sarah Norris <1645628+mikachan@users.noreply.github.com>
Co-authored-by: Sarah Norris <1645628+mikachan@users.noreply.github.com>
Co-authored-by: Sarah Norris <1645628+mikachan@users.noreply.github.com>
Co-authored-by: Sarah Norris <1645628+mikachan@users.noreply.github.com>

@scruffian scruffian left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good!

@scruffian
scruffian enabled auto-merge (squash) November 20, 2025 08:33
@scruffian
scruffian merged commit a203fa4 into trunk Nov 20, 2025
34 checks passed
@scruffian
scruffian deleted the try-pseudoelements-blocks branch November 20, 2025 11:38
@github-actions github-actions Bot added this to the Gutenberg 22.2 milestone Nov 20, 2025
@iamtakashi

Copy link
Copy Markdown

Nice! Thank you for working on this. I can remove another CSS :)

@MaggieCabrera

MaggieCabrera commented Mar 4, 2026

Copy link
Copy Markdown
Contributor Author

This made it to 7.0 in the end. My draft for the dev notes is over here

@jasmussen

Copy link
Copy Markdown
Contributor

Kudos! Nice work.

* @var array
*/
const VALID_BLOCK_PSEUDO_SELECTORS = array(
'core/button' => array( ':hover', ':focus', ':focus-visible', ':active' ),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that third party blocks can't add support for styling pseudo states? Or will this support be added in the future?

if ( ! isset( $block_options['include_block_style_variations'] ) ) {
$block_options['include_block_style_variations'] = true;
}
$block_nodes = static::get_block_nodes( $theme_json, $selectors, $block_options );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm missing something, this defeats the purpose of passing include_block_style_variations as an option to get_style_nodes because now it's always going to be true. What problem were we trying to solve here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Buttons Affects the Buttons Block [Type] Enhancement A suggestion for improvement.

6 participants