Skip to content

feat(container-menu)!: Add getAnchorProps for explicit anchor handling #695

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

Merged
merged 13 commits into from
May 12, 2025

Conversation

ze-flo
Copy link
Contributor

@ze-flo ze-flo commented May 12, 2025

Description

This PR updates the useMenu API by decoupling anchor-related logic from getItemProps. It introduces a new getAnchorProps getter for handling anchor-specific attributes in navigational menus. This PR is to supersede #693.

BREAKING CHANGES

  • getItemProps no longer includes anchor-related props. Use getAnchorProps for <a> tags.
  • Continue using getItemProps for <li>-level attributes around anchor items.
  • useMenu no longer tracks selected link state internally. Consumers must now:
    • Mark items as selected by default with selected: true, or
    • Use props.selectedItems (overrides items selected by default)

Details

getAnchorProps behavior:

  • Returns anchor attributes (href, target, rel, etc.) for items with an href.
  • Adds target="_blank" and rel="noreferrer noopener" when external is true.
  • Omits anchor attributes for disabled items.
  • Disables interactions (click, keydown) for selected anchors.
  • Enforces a single selected anchor per menu.

See zendeskgarden/react-components#2019 for implementation preview in Menu.

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 May 12, 2025 04:06
@coveralls
Copy link

coveralls commented May 12, 2025

Coverage Status

coverage: 94.884% (-0.05%) from 94.934%
when pulling a93b7d3 on ze-flo/menu-get-anchor-props
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.

useMenu no longer tracks selected link state internally. Consumers must now...

How does this impact react-components with respect to a breaking change?

getItemGroupProps,
getSeparatorProps
}: MenuReturnValue & UseMenuProps) => {
const selectedValues = selection.map(item => item.value);

React.useEffect(() => {
Copy link
Member

Choose a reason for hiding this comment

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

[nit] prefer import { useEffect } from 'react'; similar to useRef above

@ze-flo
Copy link
Contributor Author

ze-flo commented May 12, 2025

useMenu no longer tracks selected link state internally. Consumers must now...

How does this impact react-components with respect to a breaking change?

No impact. The PR introducing navigational menu items was never merged. 😌

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.

From the story deployment, it appears I can set initial selected to true for the anchor item in the uncontrolled demo. But the same does not appear to be true for the controlled demo: https://68224a63b85f0898b77419aa--zendeskgarden-react-containers.netlify.app/?path=/story/packages-menu--controlled&args=items[4].external:!true;items[4].selected:!true;isExpanded:!true;focusedValue:plant-04

@ze-flo
Copy link
Contributor Author

ze-flo commented May 12, 2025

https://68224a63b85f0898b77419aa--zendeskgarden-react-containers.netlify.app/?path=/story/packages-menu--controlled&args=items[4].external:!true;items[4].selected:!true;isExpanded:!true;focusedValue:plant-04

This spec tests that use-case.

selectedItems overrides any default selected items. Removing the selectedItems control shows the anchor as selected.

Note: By design, default selected items are only applied on initial mount. Subsequent changes to the items array won't affect the selection. If you're updating default selections and selectedItems is set to null, make sure to rerun the story to see the changes.

Screenshot 2025-05-12 at 9 19 59 AM

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.

Thanks for the clarification. LGTM

@ze-flo ze-flo merged commit 15d4e5c into main May 12, 2025
4 checks passed
@ze-flo ze-flo deleted the ze-flo/menu-get-anchor-props branch May 12, 2025 21:41
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