Make WordPress Core

Opened 9 months ago

Closed 4 months ago

Last modified 3 weeks ago

#64806 closed enhancement (fixed)

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

Reported by: onemaggie Owned by: isabel_brison
Priority: normal Milestone: 7.1
Component: Editor Version:
Severity: normal Keywords: gutenberg-merge has-patch
Cc: Focuses:

Description (last modified by onemaggie)

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

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.

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.

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.

Change History (7)

This ticket was mentioned in ​PR #11174 on ​WordPress/wordpress-develop by ​@onemaggie.


9 months ago
#1

  • Keywords has-patch has-unit-tests added

#2 @onemaggie
9 months ago

  • Description modified (diff)
  • Keywords has-patch has-unit-tests removed

#3 @wildworks
5 months ago

  • Component General → Editor
  • Keywords gutenberg-merge has-patch added
  • Milestone Awaiting Review → 7.1

This ticket appears to be targeting 7.1, so I will update the milestone.

​@onemaggie commented on ​PR #11174:


5 months ago
#4

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

#5 @isabel_brison
4 months ago

  • Owner set to isabel_brison
  • Resolution → fixed
  • Status new → closed

In 62641:

Editor: add support to style current menu item via theme.json.

Adds processing of custom states from theme.json and a new -current custom state type that is enabled for navigation link block.

Props onemaggie, isabel_brison.
Fixes #64806.

#6 @audrasjb
4 weeks ago

In 64600:

Editor: Support the current state for Navigation Submenu in theme.json.

This changeset backports the WP_Theme_JSON changes from GB76367, and is a follow-up to #64806.

Themes can style the current link inside a Navigation Submenu dropdown with -current on core/navigation-submenu in theme.json, including its :hover, :focus, :focus-visible and :active states.

The block's selectors.states selector arrives with the Gutenberg package sync, so the new test uses get_styles_for_block() with explicit style nodes, like the existing custom state tests.

Developed in:

Follow-up to #62641.

Props onemaggie.
Fixes #66312.
See #64806.

#7 @audrasjb
3 weeks ago

In 64801:

Editor: Revert [64600].

This changeset was committed ahead of the related Gutenberg PR.

Unprops audrasjb.
See #66312, #64806.

Note: See TracTickets for help on using tickets.