Skip to content

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

Open
MaggieCabrera wants to merge 1 commit intoWordPress: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 1 commit intoWordPress:trunkfrom
MaggieCabrera:backport-nav-current-item

Conversation

@MaggieCabrera
Copy link

@MaggieCabrera MaggieCabrera commented Mar 5, 2026

What?

Closes #42299
Related #38277
Alternative to #54460

Adds theme.json support for styling the current navigation item (@current) and its interactive states in the core/navigation-link block.

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 states from real CSS pseudo-selectors.

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.

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:

"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 both items with submenus and without. 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
Copy link

github-actions bot commented Mar 5, 2026

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.

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

@github-actions
Copy link

github-actions bot commented Mar 5, 2026

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.

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