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 current-menu-item class logic #42849

Merged
merged 1 commit into from
Aug 1, 2022

Conversation

mikachan
Copy link
Member

@mikachan mikachan commented Aug 1, 2022

What?

This PR ensures that we are comparing the menu item id as an integer against get_the_ID(), which is also an integer.

Why?

I noticed that the current-menu-item class was no longer being added to the current menu item element in the navigation link block. We need this class to identify the current menu item on the front end (e.g. for styling).

How?

It looks like we were previously comparing get_the_ID() to the menu item's attribute id using a triple ===, which also compares the data type. As the menu item id is a string, comparing these two values would never be true. By adding (int) to the menu item's $attribute['id'], we can successfully compare the type as well as the value.

Testing Instructions

  1. Add a navigation block with links to existing internal pages
  2. Navigate to a page that's included in the navigation
  3. Check that the current-menu-item class is added to the <li> of the current page in the navigation block markup

Screenshots or screencast

Before:
image

After:
image

@mikachan mikachan added [Block] Navigation Link Affects the Navigation Link Block [Block] Submenu Affects the Submenu Block - for submenus in navigation labels Aug 1, 2022
@mikachan mikachan self-assigned this Aug 1, 2022
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.

Nice spot. Nice fix 👏

@mikachan mikachan merged commit efa8a19 into trunk Aug 1, 2022
@mikachan mikachan deleted the fix/current-menu-item-logic branch August 1, 2022 15:58
@github-actions github-actions bot added this to the Gutenberg 13.9 milestone Aug 1, 2022
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants