Skip to content

Navigation: Consider a different markup structure for submenus. #39392

Open

Description

@scruffian

At the moment nested navigation items generate markup like this:

<!-- wp:navigation -->
	<!-- wp:navigation-submenu -->
		<!-- wp:navigation-submenu -->
	 		<!-- wp:navigation-link /-->
		<!-- /wp:navigation-submenu -->
	<!-- /wp:navigation-submenu -->
<!-- wp:navigation -->

This structure outputs markup like this:

<ul>
	<li class="wp-block-navigation-submenu">
		<a></a>
		<ul>
			<li  class="wp-block-navigation-submenu">
				<a></a>
				<ul>
					<li>
						<a></a>
					</li>
				</ul>
			</li>
		</ul>
	</li>
</ul>

Now if I want to target the dropdown portions of the navigation block using theme.json I might define something like this:

"core/navigation-submenu": {
	"typography": {
		"fontSize": "24px"
	}
}

This will generate CSS like this:

.wp-block-navigation-submenu { font-size: 24px; }

This problem is this CSS targets the navigation item above the drop down as well as the items within it, which is unexpected, and makes it impossible to target just the dropdown on its own.

To overcome this I am proposing we use this structure for the navigation:

<!-- wp:navigation -->
	<!-- wp:navigation-link -->
		<!-- wp:navigation-submenu -->
			<!-- wp:navigation-link -->
				<!-- wp:navigation-submenu -->
	 				<!-- wp:navigation-link /-->
				<!-- /wp:navigation-submenu -->
			<!-- /wp:navigation-link -->
		<!-- /wp:navigation-submenu -->
	<!-- /wp:navigation-link -->
<!-- wp:navigation -->

Which would output markup like this:

<ul>
	<li>
		<a></a>
		<ul class="wp-block-navigation-submenu">
			<li >
				<a></a>
				<ul class="wp-block-navigation-submenu">
					<li>
						<a></a>
					</li>
				</ul>
			</li>
		</ul>
	</li>
</ul>

This allows us to effectively target dropdown using theme.json.

Does this create other problems? Are there other ways to solve this?

Originally noticed in #39277

Activity

  1. added
    [Block] Navigation
    on Mar 11, 2022
  2. Humanify-nl commented on Mar 11, 2022

    @Humanify-nl

    I hope this proposal may also open up the possibility of using the submenu container for colors, instead of placing these styling classes (has-background etc.) on the links inside it.

  3. tellthemachines commented on Mar 13, 2022

    @tellthemachines
    Contributor

    Currently the Submenu block contains its parent element, because that element is not always a link. Depending on the "open on click" and "show arrow" settings, it may display a button, a link or a link with a button. This is the main reason we're not using the Nav Link block for the parent; you can find more context in #33775.

    Does this create other problems?

    If I understand correctly, the proposal is making the Submenu an inner block of a Nav Link block. That's not straightforward because, as mentioned above, the parent isn't always a Link. We'd have to either introduce some very hacky logic in the Link block to treat it as a button depending on whether it has children, or we'd need another Link-or-Button-depending type of block, which also doesn't feel ideal.

    In terms of styling, we may also want to target the parent element, which will be harder to do if it's not part of the submenu.

    Are there other ways to solve this?

    Current default targetting of the block wrapper for styles (whether they come from the editor or theme.json) doesn't work for all cases; we had similar problems with the Navigation block itself when implementing layout on it - see #37473. Table is another challenging block in this respect - see #30791. This is a problem that won't always be possible to solve by fiddling with the block markup; we need to have a way to target its inner elements. Part of the Style Engine work will be looking at ways of doing this (#38167) - contributions to those discussions very welcome! 🙂

  4. scruffian commented on Mar 14, 2022

    @scruffian
    ContributorAuthor

    You're proposing that we use something like __experimentalSelector to modify the color properties of theme.json to target only those items within the UL? That makes sense to me :)

  5. tellthemachines commented on Mar 15, 2022

    @tellthemachines
    Contributor

    If it's likely that themes will only want to style the items within the ul, then yes, __experimentalSelector works!

  6. Humanify-nl commented on Mar 15, 2022

    @Humanify-nl

    If I understand correctly, the proposal is making the Submenu an inner block of a Nav Link block. That's not straightforward because, as mentioned above, the parent isn't always a Link. We'd have to either introduce some very hacky logic in the Link block to treat it as a button depending on whether it has children, or we'd need another Link-or-Button-depending type of block, which also doesn't feel ideal.

    I understand this is why removing a submenu block, also removes the parent link. Which is intuitively unexpected behavior. Or is that another issue?

  7. tellthemachines commented on Mar 16, 2022

    @tellthemachines
    Contributor

    I understand this is why removing a submenu block, also removes the parent link. Which is intuitively unexpected behavior. Or is that another issue?

    That is the expected behaviour, yes. If you wish to remove the dropdown while preserving the parent link, you can remove the submenu items, and transform the submenu into a link.

    As I mentioned above, there's an open on click setting on the Navigation block whereby submenu top-level items become buttons. In that context, it doesn't make sense to preserve them once the submenu is removed.

  8. Humanify-nl commented on Mar 16, 2022

    @Humanify-nl

    That is the expected behaviour, yes. If you wish to remove the dropdown while preserving the parent link, you can remove the submenu items, and transform the submenu into a link.

    I understand why it works like this. Yet, from a user psychology perspective it is not intuitive because:

    image

    Here it says "add submenu". Which implies a menu is added to a link (but it actually is a transform). A user would expect that when removing the submenu, the same revert action applies (the submenu is removed, but actually transformed to a link).

    But it does not. It simply removes. You have to have prerequisite knowledge about the transform but a new users doesn't 'intuitively know' we transformed in the first step so we assume we don't have to.

    See how this simple signalling of "add" instead of "transform" can be confusing for someone who has never used the menu?

  9. tellthemachines commented on Mar 17, 2022

    @tellthemachines
    Contributor

    Here it says "add submenu". Which implies a menu is added to a link (but it actually is a transform). A user would expect that when removing the submenu, the same revert action applies

    Good point! Let's change the copy to say "Transform to submenu".

  10. added
    [Type] Enhancement
    on Aug 30, 2023
  11. jasmussen commented on Sep 21, 2026

    @jasmussen
    Contributor

    Coming back to this one with an update.

    Changing the block's markup (nesting the Submenu inside a Navigation Link) still seems too costly, for the reasons @tellthemachines gave above: the parent item can be a link, a button or both, and the saved markup would change. But the alternative suggested here: a custom selector for typography, is now possible with a stable API: block.json selectors, which core blocks like Table already use. I think that's the simple path forward. I closed #39263 and #39277 in favour of this.

    It's not clear the following will work, but it could be worth a proof of concept to feel it out. My AI tool suggested the following could work as a prompt:

    Problem. theme.json styles for core/navigation-submenu apply to the block wrapper .wp-block-navigation-submenu, which is the <li> that holds both the parent item (link and/or button) and the dropdown <ul class="wp-block-navigation__submenu-container">. So setting styles.blocks["core/navigation-submenu"].typography.fontSize also resizes the top-level parent item, and a theme can't size only the dropdown.

    Fix. Add a feature-level typography selector to packages/block-library/src/navigation-submenu/block.json so `theme.jso and Global Styles typography target only the dropdown container:

    "selectors": {
    	"typography": ".wp-block-navigation-submenu .wp-block-navigation__submenu-container"
    }
    

    Don't change root, so other features keep targeting the wrapper and the parent item can still be styled separately later. Color is out of scope, because submenu colors come from the Navigation block's overlay color context.

    Why it works in both places. On the front end, render_block_core_navigation_submenu() in navigation-submenu/index.php already outputs the <ul> with wp-block-navigation__submenu-container. In the editor, getNavigationChildBlockProps() in navigation/edit/utils.js adds the same class to the inner blocks wrapper. Links inside the dropdown inherit the size from the <ul>, and a theme.json size on core/navigation-link still overrides it, because it targets the link block itself.

    Testing.

    In a block theme's theme.json, set a font size on core/navigation (e.g. 2rem) and a smaller one on core/navigation-submenu (e.g. 1rem).
    Add a Navigation block with a submenu, including a nested submenu.
    In both the editor and the front end, top-level items (including the submenu's parent item) use the Navigation size, and the dropdown items and nested submenu parents use the submenu size.
    Repeat with "Open on click" on and off, and with the submenu arrow shown and hidden.
    Check that the Global Styles typography panel for Submenu shows the same result.

    Things to decide in the PR.

    Existing themes will change. A theme that sets a font size on core/navigation-submenu today will see its top-level parent items go back to the Navigation block's size. Check the recent default themes and note the change in the block-library changelog.
    Only theme.json and Global Styles change. A font size set on a single Submenu block in the editor still applies to both the <li> and the <ul>, because the render callback calls get_block_wrapper_attributes() for both. Decide whether that difference is acceptable or should be aligned.

    If anyone has a chance to look at the brief above and feel out if it's valid, the issue title may need updating, since this would no longer be about changing the block's markup.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Block] Navigation[Type] Enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions