Skip to content

fix(menu): correctly identify menu item link with same URL as web page #693

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

ze-flo
Copy link
Contributor

@ze-flo ze-flo commented Apr 23, 2025

Description

useMenu correctly identifies the menu item link with the same URL as the current web page.

Detail

  • getItemProps sets aria-current="page" to the menu item link with the same URL as the current web page.
  • Disabled menu item links do not get unnecessary target and ref attributes.

Checklist

  • 🌐 demo is up-to-date (npm start)
  • 💂‍♂️ includes new unit tests
  • ♿ tested for WCAG 2.1 AA compliance
  • 📝 tested in Chrome, Firefox, Safari, and Edge

@ze-flo ze-flo requested a review from a team as a code owner April 23, 2025 23:37
@coveralls
Copy link

coveralls commented Apr 23, 2025

Coverage Status

coverage: 94.966% (+0.03%) from 94.934%
when pulling 56e5179 on ze-flo/menu-aria-current
into a79dac8 on main.

Copy link
Member

@jzempel jzempel left a comment

Choose a reason for hiding this comment

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

Some 🐛 ...

  1. with the uncontrolled menu story:
  • Click "Aloe Vera" link in the story
  • Use back button to return to the story
  • Click "Aloe Vera" link again
    Result: nothing happens
  1. with the controlled menu story:
  • Change "Aloe Vera" to isExternal={true}
  • Click the "Aloe Vera" link
    Result: nothing happens
  1. adding selected to the "Aloe Vera" item (and remounting) doesn't work as expected. Items with href should be able to be initialized selected outside the context of an item group.

@@ -60,7 +69,14 @@ const Item = ({ item, getItemProps, focusedValue, isSelected }: MenuItemProps) =
{itemProps.href ? (
<a
{...(itemProps as AnchorHTMLAttributes<HTMLAnchorElement>)}
className="w-full rounded-sm outline-offset-0 transition-none border-width-none"
href={item.disabled ? undefined : itemProps.href}
Copy link
Member

Choose a reason for hiding this comment

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

This href prop manipulation should be happening within the hook so that spreading ...itemProps Just Works ™️

elementProps.href = href;

if (!itemDisabled) {
if (isExternal) {
Copy link
Member

Choose a reason for hiding this comment

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

Should probably be named external to keep parity with the other aspects of the container API (selected, disabled). Compare with other container APIs – we don't enforce the isXxx naming like we do in react-components. Also, the documentation for item.href and item.external is missing from the types interface.

Copy link
Member

@jzempel jzempel left a comment

Choose a reason for hiding this comment

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

Approving as these items can either be addressed here in this PR or in a follow-on.

Comment on lines +57 to 62
role={item.href ? 'none' : undefined}
{...(!item.href && (itemProps as LiHTMLAttributes<HTMLLIElement>))}
>
{itemProps.href ? (
{item.href ? (
<a
{...(itemProps as AnchorHTMLAttributes<HTMLAnchorElement>)}
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I don't want to block this PR, but normally a container would expose a prop spread per element. So depending on whether there's a defined href, the hook would produce itemProps and defined/undefined/null anchorProps. This prevents the consumer from needing to go through the logic seen here. I'll continue with an approval review. You can either solve here or we can revisit later.

If you decide to render with a getAnchorProps, please just remember to add it to the Storybook API docs.

Copy link
Member

Choose a reason for hiding this comment

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

Also, should the README (https://zendeskgarden.github.io/react-containers/?path=/docs/packages-menu--docs#usemenu) be updated with an anchor example? Again, might be a lot cleaner if we wait to have getAnchorProps.

Copy link
Member

Choose a reason for hiding this comment

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

Is there an item treatment that can be done for anchor items if selected is true? It would be nice to see a visual change here in the containers storybook (as opposed to react-components where we don't want any visual artifact).

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

Successfully merging this pull request may close these issues.

4 participants