Skip to content

Backport: Navigation link: add support to style current menu item via theme.json#11174

Open
MaggieCabrera wants to merge 6 commits into
WordPress:trunkfrom
MaggieCabrera:backport-nav-current-item
Open

Backport: Navigation link: add support to style current menu item via theme.json#11174
MaggieCabrera wants to merge 6 commits into
WordPress:trunkfrom
MaggieCabrera:backport-nav-current-item

Conversation

@MaggieCabrera

@MaggieCabrera MaggieCabrera commented Mar 5, 2026

Copy link
Copy Markdown

What?

This backports the relevant changes from WordPress/gutenberg#75736, updated to match the current Gutenberg trunk state after:

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_STATES constant whitelists --prefixed states per block in theme.json, playing the same role that VALID_BLOCK_PSEUDO_SELECTORS plays for CSS pseudo-selectors. The - prefix distinguishes these class-based custom states from real CSS pseudo-selectors and responsive breakpoint states.

Each block declares its state CSS selectors in block.json under selectors.states, following the existing Selectors API pattern, which get_blocks_metadata() reads into block metadata and get_block_nodes() uses to generate CSS.

Blocks listed in VALID_BLOCK_CUSTOM_STATES also inherit their VALID_BLOCK_PSEUDO_SELECTORS as valid sub-states, producing compound selectors like .wp-block-navigation-item.current-menu-item:hover.

The PHPUnit tests use get_styles_for_block() with explicit style nodes so they do not depend on block metadata that will arrive through the Gutenberg sync.

We've had initial discussions on how the UI for this scenario would look like over here.

Testing Instructions

Run the relevant PHP unit tests for WP_Theme_JSON, especially the custom-state tests added in this PR.

The following theme.json snippets show the intended public API once the corresponding selectors.states metadata for core/navigation-link is available in Core through the Gutenberg sync.

Current item base styles:

"core/navigation-link": {
  "-current": {
    "color": { "text": "#ff0000" }
  }
}

Current item hover:

"core/navigation-link": {
  "-current": {
    ":hover": {
      "color": { "text": "#0000ff" }
    }
  }
}

Current item focus:

"core/navigation-link": {
  "-current": {
    ":focus": {
      "color": { "text": "#00aa00" }
    }
  }
}

All combined:

"core/navigation-link": {
  "-current": {
    "color": { "text": "#ff0000" },
    "typography": { "fontWeight": "700" },
    ":hover": {
      "color": { "text": "#0000ff" }
    },
    ":focus": {
      "color": { "text": "#00aa00" }
    },
    ":active": {
      "color": { "text": "#ff6600" }
    }
  }
}

Test for navigation link items with and without submenus. Try other styles that aren't just the text color.

Testing Instructions for Keyboard

Screenshots or screencast

Screenshot 2026-02-19 at 18 02 17 Screenshot 2026-02-19 at 18 02 23

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

@MaggieCabrera MaggieCabrera force-pushed the backport-nav-current-item branch from 8ef2465 to 5ff8b02 Compare March 5, 2026 10:30
@MaggieCabrera MaggieCabrera marked this pull request as ready for review March 5, 2026 10:33
@github-actions

github-actions Bot commented Mar 5, 2026

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.

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

Props onemaggie, isabel_brison.

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

@github-actions

github-actions Bot commented Mar 5, 2026

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.

@tellthemachines tellthemachines force-pushed the backport-nav-current-item branch from b393ff3 to 534fdaa Compare June 1, 2026 05:42
Comment thread src/wp-includes/class-wp-theme-json.php Outdated

@tellthemachines tellthemachines 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.

Aside from the submenu addition which I don't believe we should add here without adding first in Gutenberg, the rest of the changeset looks consistent with #75736.

The PHP unit tests are failing because they're testing get_stylesheet, which relies on data from the actual block attributes, and we won't get that in core until gutenberg is synced.

A good alternative would be to test get_styles_for_block instead and pass it the style nodes, so it's independent of the gutenberg sync and I think makes sense for this changeset because it's closer to where the changes actually are (we haven't changed get_stylesheet so we shouldn't have to test it). You can check the tests in #11706 for a similar approach.

@MaggieCabrera MaggieCabrera force-pushed the backport-nav-current-item branch from 40909ba to 9ee02ee Compare June 29, 2026 09:30
@MaggieCabrera

Copy link
Copy Markdown
Author

I updated this PR with the changes discussed plus rebased it and made it sure it was up to date with what is in Gutenberg right now. It should be ready for review

@tellthemachines tellthemachines 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.

The changes won't be testable in the core dev env until the editor files are updated to include the change from WordPress/gutenberg#79210.

The code mostly looks good, just a couple things I commented on below!

Comment thread src/wp-includes/class-wp-theme-json.php Outdated
'selectors' => $feature_selectors,
'duotone' => $duotone_selector,
'variations' => $variation_selectors,
'css' => $custom_css_selector,

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.

Comparing with gutenberg we're missing 'elements' => $selectors[ $name ]['elements'] ?? array(), in this array as well as in the one from the loop below (those were added as part of WordPress/gutenberg#77513)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

good catch, thanks!

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.

2 participants