Skip to content

Correctly apply current-menu-ancestor class in Nav block - #67169

Merged
getdave merged 1 commit into
trunkfrom
fix/current-menu-ancestor-class-application
Dec 2, 2024
Merged

getdave merged 1 commit into
trunkfrom
fix/current-menu-ancestor-class-application

Conversation

@getdave

@getdave getdave commented Nov 20, 2024 •

Copy link
Copy Markdown
Contributor

What?

Applies the current-menu-ancestor class to the <li> not the <a>

Closes #50069

Why?

Consistency with current-menu-item which is applied to the <li>.

How?

Applies the current-menu-ancestor class to the <li> not the <a>

Testing Instructions

You should be able to test this via Playground using the following link

https://playground.wordpress.net/?gutenberg-pr=67169

Note you can install any publicly available Theme for testing using the format:

https://playground.wordpress.net/?gutenberg-pr=67169&theme={theme-slug-here}

On your WordPress test site you can then...

  • Create Navigation menu
  • Add a Page
  • Add a submenu under that page and add some links.
  • Save
  • View front of site
  • Inspect HTML of top level link
  • See class is on li not a.

Testing Instructions for Keyboard

Screenshots or screencast

@getdave getdave added [Type] Code Quality Issues or PRs that relate to code quality [Block] Navigation Affects the Navigation Block labels Nov 20, 2024
@getdave getdave self-assigned this Nov 20, 2024
@getdave
getdave requested a review from ajitbohra as a code owner November 20, 2024 15:27
@getdave

getdave commented Nov 20, 2024

Copy link
Copy Markdown
Contributor Author

I'm guessing this class isn't applied in the Editor because there is no concept of this there right?

@github-actions

github-actions Bot commented Nov 20, 2024 •

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.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: getdave <get_dave@git.wordpress.org>
Co-authored-by: MaggieCabrera <onemaggie@git.wordpress.org>
Co-authored-by: carolinan <poena@git.wordpress.org>
Co-authored-by: mrwweb <mrwweb@git.wordpress.org>
Co-authored-by: juanfra <juanfra@git.wordpress.org>
Co-authored-by: draganescu <andraganescu@git.wordpress.org>
Co-authored-by: bph <bph@git.wordpress.org>
Co-authored-by: jordesign <jordesign@git.wordpress.org>
Co-authored-by: webmandesign <webmandesign@git.wordpress.org>

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

@MaggieCabrera

Copy link
Copy Markdown
Contributor

I'm guessing this class isn't applied in the Editor because there is no concept of this there right?

that sounds about right, it would be hard to map


if ( strpos( $inner_blocks_html, 'current-menu-item' ) ) {
$tag_processor = new WP_HTML_Tag_Processor( $html );
while ( $tag_processor->next_tag( array( 'class_name' => 'wp-block-navigation-item__content' ) ) ) {

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.

I wonder how many websites are targeting the "wrong" selector...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure. class is not a first party API though but as we know we need to acknowledge the impact on existing Themes.

How has this been handled in the past? It's not like we can throw an error if someone is using that class.

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.

I think we just consider the risk and go with it. I can't remember when we've done this recently. Maybe a dev note?

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.

@WordPress/block-themers anyone remember an instance of class changing like this?

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.

See #67134

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If I read this right that the .wp-block-navigation-item__content class will be removed by this change, it looks like it's used by a considerable number of themes and a few plugins too.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@mrwweb it looks like that class is only used to iterate there and to determine where to place the current-menu-ancestor class. So, this specific change doesn't mean that the wp-block-navigation-item__content class will be removed.

anyone remember an instance of class changing like this?

I don't.

@MaggieCabrera

Copy link
Copy Markdown
Contributor

I'm ok tentatively approving this if we get some more feedback from themers. If this only affect block themes, I've done a search on a dump I have of block themes in the themes repo and I don't see many themes that would be affected by this change

@juanfra juanfra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is testing well for me! I really like that it brings parity with how things work in classic themes.

My fear is also that we don't know how many sites could be targeting the wrong selector. It looks like there's only a handful of themes doing this in the repository, though.

Screenshot 2024-11-22 at 10 27 17

@MaggieCabrera

Copy link
Copy Markdown
Contributor

This is testing well for me! I really like that it brings parity with how things work in classic themes.

My fear is also that we don't know how many sites could be targeting the wrong selector. It looks like there's only a handful of themes doing this in the repository, though.

Yeah, I haven't been able to find much more than that myself, I think this is a low risk change.

@MaggieCabrera

Copy link
Copy Markdown
Contributor

Ugh, sorry I misclicked there :(

@getdave getdave added the Needs Dev Note Requires a developer note for a major WordPress release cycle label Nov 22, 2024
@getdave

getdave commented Nov 22, 2024

Copy link
Copy Markdown
Contributor Author

@bph Would it be worth dropping this in the Outreach channel in Slack for feedback?

@bph

bph commented Nov 22, 2024

Copy link
Copy Markdown
Contributor

Absolutely! You can also ping the @WordPress/outreach group here on GitHub.

@getdave

getdave commented Nov 27, 2024

Copy link
Copy Markdown
Contributor Author

I'll leave this open a few more days to allow for further feedback.

Otherwise I think we just need an approving review (with the caveat for the approver that it's agreed that it's not 100% possible to know if this will have some negative side effects).

@draganescu draganescu 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 problem is valid, the code makes sense.

@draganescu

Copy link
Copy Markdown
Contributor

I approved, although I do wonder about it a bit in terms of removing the class from the anchor, what is wrong with:

  • do not remove the class on the anchor
  • add a new class to the list item current-menu-item-ancestor

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

Given how early in the release we are and how little the impact seems to be when inspecting the code for themes in the directory, I think this is good to land as is to get it out and get feedback

@getdave

getdave commented Nov 27, 2024

Copy link
Copy Markdown
Contributor Author

I approved, although I do wonder about it a bit in terms of removing the class from the anchor, what is wrong with:

  • do not remove the class on the anchor
  • add a new class to the list item current-menu-item-ancestor

I don't think we should double up. It could be very confusing. Either move it or leave it "as is".

@carolinan
carolinan requested a review from cbirdsong November 27, 2024 13:28
@getdave

getdave commented Dec 2, 2024

Copy link
Copy Markdown
Contributor Author

I'm going to merge this one as soon as I can get the PHP unit tests to run correctly.

@juanfra

juanfra commented Dec 2, 2024

Copy link
Copy Markdown
Member

@getdave I re-ran the failed tests, and they’re all passing now 🚀

@getdave
getdave merged commit 6561108 into trunk Dec 2, 2024
@getdave
getdave deleted the fix/current-menu-ancestor-class-application branch December 2, 2024 16:39
im3dabasia pushed a commit to im3dabasia/gutenberg that referenced this pull request Dec 4, 2024
…Press#67169

Co-authored-by: getdave <get_dave@git.wordpress.org>
Co-authored-by: MaggieCabrera <onemaggie@git.wordpress.org>
Co-authored-by: carolinan <poena@git.wordpress.org>
Co-authored-by: mrwweb <mrwweb@git.wordpress.org>
Co-authored-by: juanfra <juanfra@git.wordpress.org>
Co-authored-by: draganescu <andraganescu@git.wordpress.org>
Co-authored-by: bph <bph@git.wordpress.org>
Co-authored-by: jordesign <jordesign@git.wordpress.org>
Co-authored-by: webmandesign <webmandesign@git.wordpress.org>
michalczaplinski pushed a commit that referenced this pull request Dec 5, 2024
Co-authored-by: getdave <get_dave@git.wordpress.org>
Co-authored-by: MaggieCabrera <onemaggie@git.wordpress.org>
Co-authored-by: carolinan <poena@git.wordpress.org>
Co-authored-by: mrwweb <mrwweb@git.wordpress.org>
Co-authored-by: juanfra <juanfra@git.wordpress.org>
Co-authored-by: draganescu <andraganescu@git.wordpress.org>
Co-authored-by: bph <bph@git.wordpress.org>
Co-authored-by: jordesign <jordesign@git.wordpress.org>
Co-authored-by: webmandesign <webmandesign@git.wordpress.org>
@Mamaduka Mamaduka mentioned this pull request Mar 12, 2025
45 of 49 tasks
@tyrann0us

Copy link
Copy Markdown
Contributor

I apologize for commenting on a merged PR and for drifting slightly off-topic, but I would like to highlight an IMO important point:

I'm ok tentatively approving this if we get some more feedback from themers. If this only affect block themes, I've done a search on a dump I have of block themes in the themes repo and I don't see many themes that would be affected by this change

and

Given how early in the release we are and how little the impact seems to be when inspecting the code for themes in the directory, I think this is good to land as is to get it out and get feedback

Thank you for taking the time to review the publicly available block-theme repository; that analysis is valuable. Nevertheless, the data set in the .org directory covers only a fraction of the ecosystem. Many, perhaps most, production themes are private, commercially licensed. This is especially true in enterprise environments. Those installations rarely surface in public scans, yet even minor markup changes can impose significant maintenance costs.

In light of this, I believe the block editor should adopt a consistent policy for changes that relocate or rename markup or CSS hooks. Ideally, new behavior would be introduced in parallel with the legacy behavior for e.g., a full release cycle, accompanied by a developer and/or an announcement in the Field Guide. This grace period gives theme authors adequate time to adapt. Afterward, the legacy path can be removed.

For this specific change, adding current-menu-ancestor to the <li> while retaining it on the <a> for one cycle would have preserved backward compatibility, avoided breaking existing selectors, and still signaled the preferred location going forward. A supplementary Theme Check rule that warns if .current-menu-ancestor appears on <a> tags could have been introduced, further easing migration.

Thank you! 🙇🏽‍♂️

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] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Navigation block: Current menu item and ancestor class inconsistent application

8 participants