Backport: Navigation link: add support to style current menu item via theme.json#11174
Backport: Navigation link: add support to style current menu item via theme.json#11174MaggieCabrera wants to merge 1 commit intoWordPress:trunkfrom
Conversation
8ef2465 to
5ff8b02
Compare
|
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. |
What?
Closes #42299
Related #38277
Alternative to #54460
Adds theme.json support for styling the current navigation item (
@current) and its interactive states in thecore/navigation-linkblock.Why?
Theme authors need a way to style the active/current menu item differently from the rest of the navigation, including controlling its hover, focus, and active states.
How?
No UI changes are included; this is theme.json only.
A new
VALID_BLOCK_CUSTOM_STATESconstant whitelists@-prefixed states per block in theme.json, playing the same role thatVALID_BLOCK_PSEUDO_SELECTORSplays for CSS pseudo-selectors — the@prefix distinguishes these class-based states from real CSS pseudo-selectors.Each block declares its state CSS selectors in
block.jsonunderselectors.states, following the existing Selectors API pattern, whichget_blocks_metadata()reads into block metadata andget_block_nodes()uses to generate CSS.Blocks listed in
VALID_BLOCK_CUSTOM_STATESalso inherit theirVALID_BLOCK_PSEUDO_SELECTORSas valid sub-states, producing compound selectors like.wp-block-navigation-item.current-menu-item:hover.We've had initial discussions on how the UI for this scenario would look like over here.
Testing Instructions
Add the following snippets to your theme.json under styles.blocks and verify the correct CSS is generated on the frontend:
Current item base styles:
Current item hover:
Current item focus:
All combined:
Test for both items with submenus and without. Try other styles that aren't just the text color.
Testing Instructions for Keyboard
Screenshots or screencast
Trac ticket:
https://core.trac.wordpress.org/ticket/64806