Skip to content

Global Styles: Scope custom block state styles to feature selectors - #15915

Open
aaronrobertshaw wants to merge 3 commits into
WordPress:trunkfrom
aaronrobertshaw:backport/86640-custom-state-feature-selectors
Open

aaronrobertshaw wants to merge 3 commits into
WordPress:trunkfrom
aaronrobertshaw:backport/86640-custom-state-feature-selectors

Conversation

@aaronrobertshaw

@aaronrobertshaw aaronrobertshaw commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Backport of Gutenberg PR WordPress/gutenberg#86640

Trac ticket: https://core.trac.wordpress.org/ticket/66667

Styles in a custom block state, such as Navigation Link's -current, now stay scoped to that state. This covers features with their own selector. Before, they applied to every instance of the block, and -current:hover lost its :hover.

This PR backports:

  • state-scoped feature selectors for custom state and custom state pseudo nodes in WP_Theme_JSON::get_block_nodes(),
  • the new WP_Theme_JSON::append_to_feature_selectors() helper, also used by pseudo state nodes with no output change,
  • and the accompanying WP_Theme_JSON tests.

Testing instructions

Navigation Link has no feature selectors in Core yet, so a filter adds one. No editor or package update is needed.

  1. Activate Twenty Twenty-Five and add this mu-plugin:
<?php
add_filter( 'block_type_metadata', function ( $metadata ) {
	if ( 'core/navigation-link' === $metadata['name'] ) {
		$metadata['selectors']['dimensions'] = '.wp-block-navigation-link';
	}
	return $metadata;
} );

add_filter( 'wp_theme_json_data_theme', function ( $theme_json ) {
	return $theme_json->update_with( array(
		'version' => 3,
		'styles'  => array(
			'blocks' => array(
				'core/navigation-link' => array(
					'-current' => array(
						'color'      => array( 'text' => 'red' ),
						'dimensions' => array( 'minWidth' => '200px' ),
						':hover'     => array( 'dimensions' => array( 'minHeight' => '60px' ) ),
					),
				),
			),
		),
	) );
} );
  1. Give the Sample Page (ID 2) a Navigation block that links to itself:
wp post update 2 --post_content='<!-- wp:navigation --><!-- wp:navigation-link {"label":"Sample Page","type":"page","id":2,"kind":"post-type"} /--><!-- /wp:navigation -->'
  1. View the Sample Page on the front end and search the page source for color: red, min-width: 200px and min-height: 60px. With this PR:
:root :where(.wp-block-navigation .current-menu-item){color: red;}
:root :where(.wp-block-navigation-link:is(.wp-block-navigation .current-menu-item)){min-width: 200px;}
:root :where(.wp-block-navigation-link:is(.wp-block-navigation .current-menu-item):hover){min-height: 60px;}
  1. On trunk, the last two rules lose the state and :hover, so every link gets both sizes:
:root :where(.wp-block-navigation .current-menu-item){color: red;}
:root :where(.wp-block-navigation-link){min-width: 200px;}
:root :where(.wp-block-navigation-link){min-height: 60px;}

Use of AI

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: The port, tests and output check were done by Claude Code; I reviewed them.

Custom block states in theme.json, such as Navigation Link's `-current`, passed the block's feature selectors through unchanged, so a style with its own feature selector applied to every instance of the block, and `-current:hover` lost its `:hover`. This appends the state selector, wrapped in `:is()`, to each feature and subfeature selector, and the pseudo selector on top for custom state pseudo nodes.

Follow-up to [62641].
@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@aaronrobertshaw
aaronrobertshaw marked this pull request as ready for review November 12, 2026 03:00
@github-actions

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.

Core Committers: Use this line as a base for the props when committing in SVN:

Props aaronrobertshaw.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant