Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a current-menu-ancestor class to navigation items #40778

Merged
merged 3 commits into from
Dec 1, 2022

Conversation

aristath
Copy link
Member

@aristath aristath commented May 3, 2022

What?

Fixes #39663

When a submenu item is active, this will add a current-menu-ancestor to its parent menu-item(s), allowing theme-developers to style these items more efficiently.

Why?

To make styling easier, and for consistency with the old navigation

How?

Adds a simple check in the navigation-submenu block. If the inner-blocks HTML contains current-menu-item, then the parent gets a current-menu-ancestor class.

Testing Instructions

Add a menu with submenus. Visit one of the pages in a submenu, and check that the parent menu-item gets the current-menu-ancestor class. Check with nested submenus as well (2 level deep submenus)

Copy link
Contributor

@draganescu draganescu left a comment

Choose a reason for hiding this comment

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

Since we depend on current-menu-item and wp-block-navigation-item__content, let's add a test to make sure this works in case anybody fiddles with those class names.

@aristath
Copy link
Member Author

Since we depend on current-menu-item and wp-block-navigation-item__content, let's add a test to make sure this works in case anybody fiddles with those class names.

I would assume that when someone changes something like a class, they'd search the repo to see if it's used anywhere else... 🤔
I agree, it would be nice to add a test for that. Unfortunately, I don't know how to do that 😅

@getdave
Copy link
Contributor

getdave commented Jun 30, 2022

I would assume that when someone changes something like a class, they'd search the repo to see if it's used anywhere else...

But that's assuming everyone is as thoughtful and diligent as you are @aristath! I think it might be highly optimistic.

Unfortunately, I don't know how to do that 😅

The class is defined in

$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => $css_classes . ' wp-block-navigation-item' . ( $has_submenu ? ' has-child' : '' ) .
( $is_active ? ' current-menu-item' : '' ),
'style' => $style_attribute,
)
);

So we could add a e2e test for that block which checks if the class exists. Usually I'd say we shouldn't test developer APIs such as classes, but in this case it makes sense.

@Marc-pi
Copy link

Marc-pi commented Jul 11, 2022

i've just cross linked duplicated issues, so you can close all fo them when merged ;-) less is more

@coreyworrell
Copy link
Contributor

coreyworrell commented Nov 11, 2022

I'm pretty sure classic menus also applied the class for a link to the Posts page as well when viewing a single post. If you viewed a post, the link in a menu for the "Posts page" would get the ancestor class as well. Assume this would apply for any custom post as well, if linking to the custom post type archive, and viewing a singular post of that type.

Copy link
Contributor

@getdave getdave left a comment

Choose a reason for hiding this comment

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

Thank you for continuing to iterate on this.

I gave it a test

  • Created Post
  • Add Nav block
  • Add 2 levels of submenu
  • Published
  • Checked classes
  • Saw current-menu-ancestor applied to
    • lowest submenu item
    • parent of lowest submenu item
    • nothing on top level

I'm not sure that's intended behaviour but I could be wrong...

Screen Shot 2022-11-30 at 13 51 32

@aristath aristath force-pushed the add/current-menu-ancestor branch from b3853fb to baef261 Compare December 1, 2022 07:29
@aristath
Copy link
Member Author

aristath commented Dec 1, 2022

@getdave thank you for testing this!
I pushed a fix, it should be working fine now 👍

Copy link
Contributor

@getdave getdave left a comment

Choose a reason for hiding this comment

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

You are welcome.

It seems to be now we have no classes?

Screen Shot 2022-12-01 at 09 39 33

I'm testing this by

  • creating a menu with several levels of nesting
  • publishing
  • doing to front of site
  • checking for classes on parents of nested items

@aristath
Copy link
Member Author

aristath commented Dec 1, 2022

It seems to be now we have no classes?

That's weird... Seems to be working fine on my end 🤔

Screenshot 2022-12-01 at 11 47 56 AM

checking for classes on parents of nested items

Is the child the active page? When it is, I can see the classes fine on my localhost

@aristath
Copy link
Member Author

aristath commented Dec 1, 2022

@getdave on your last screenshot I don't see any items with the .current-menu-item, so it makes sense that the ancestors don't get the .current-menu-ancestor class. Unless I missed something on that screenshot? 🤔

Copy link
Contributor

@getdave getdave left a comment

Choose a reason for hiding this comment

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

Screen Shot 2022-12-01 at 13 46 21

Tested (again) following the instructions more carefully this time. I can see that the ancestor class is applied to the anchor for ancestors of the current page.

Thank you for persisting with this 🙇

@aristath aristath enabled auto-merge (squash) December 1, 2022 13:59
@aristath aristath requested a review from draganescu December 1, 2022 13:59
@aristath aristath disabled auto-merge December 1, 2022 14:00
@aristath aristath merged commit 93d317a into trunk Dec 1, 2022
@aristath aristath deleted the add/current-menu-ancestor branch December 1, 2022 14:00
@github-actions github-actions bot added this to the Gutenberg 14.8 milestone Dec 1, 2022
@Marc-pi
Copy link

Marc-pi commented Dec 1, 2022

@aristath many thanks
i think you can also close those i cross referenced above, here is a screenshot

image

mpkelly pushed a commit to mpkelly/gutenberg that referenced this pull request Dec 7, 2022
* Add a current-menu-ancestor class to menus

* Use WP_HTML_Tag_Processor

* bugfix
@bph bph added the Needs Dev Note Requires a developer note for a major WordPress release cycle label Feb 17, 2023
@bph
Copy link
Contributor

bph commented Feb 17, 2023

Flagging for Dev Notes 6.2. - @aristath do you think it would be a suitable candidate for a blurb to a Miscellaneous post?

@scruffian
Copy link
Contributor

@bph yeah I think that's a good idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Affects the Navigation Block Needs Dev Note Requires a developer note for a major WordPress release cycle [Type] Enhancement A suggestion for improvement.
Projects
None yet
8 participants