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

Navigation: Fix bug in calculating the active menu item #49195

Merged
merged 1 commit into from
Mar 20, 2023

Conversation

aristath
Copy link
Member

What?

Fixes #49153

Why?

As described in #49153, when a post and a term have the same ID and both exist in the menu, the code that determines the active menu item is wrong. As a result, both the term and the post get marked as active because we only check the ID and not the context.

How?

Tweaking the check to determine if a menu item is active.
$attributes['kind'] can be things like taxonomy, post-type etc. Using get_queried_object() in a term will return an object which will have the taxonomy defined. In a post, the post_type will be defined and so on.
This PR will check that the ID is the same, AND that the context (post/term etc) is right before marking a menu item as active.

Testing Instructions

  1. On a fresh installation create some terms and some pages.
  2. Find a page and a term that have the same ID, and add them both in a navigation block.

Before the PR, visiting the term will highlight both the post and the term as active, and the same will happen when visiting the page.
After the PR, the current-menu-item class will only be added to the right element.

Note: The same code was applied both to the navigation-link and the navigation-submenu blocks since they were using identical checks.

Testing Instructions for Keyboard

Not applicable.

@aristath aristath requested a review from Mamaduka March 20, 2023 09:54
@Mamaduka Mamaduka added [Block] Navigation Link Affects the Navigation Link Block [Type] Bug An existing feature does not function as intended [Block] Submenu Affects the Submenu Block - for submenus in navigation labels Mar 20, 2023
@github-actions
Copy link

Flaky tests detected in aeb1335.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4466778560
📝 Reported issues:

@Mamaduka Mamaduka changed the title Fix bug in calculating the active menu item Navigation: Fix bug in calculating the active menu item Mar 20, 2023
Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

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

The fix works as expected. Thank you, Ari!

@aristath aristath merged commit 9c90e70 into trunk Mar 20, 2023
@aristath aristath deleted the fix/active-menu-check-type branch March 20, 2023 13:46
@github-actions github-actions bot added this to the Gutenberg 15.5 milestone Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Link Affects the Navigation Link Block [Block] Submenu Affects the Submenu Block - for submenus in navigation [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bad "current-menu-item" on navigation-link
2 participants