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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions packages/menu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Check out [storybook](https://zendeskgarden.github.io/react-containers) for live

### useMenu

#### Menu items

```jsx
import { useMenu } from '@zendeskgarden/container-menu';

Expand Down Expand Up @@ -50,6 +52,40 @@ const Menu = () => {
};
```

#### Menu links

```jsx
import { useMenu } from '@zendeskgarden/container-menu';

const Menu = () => {
const triggerRef = useRef();
const menuRef = useRef();
const items = [
{ value: 'home', label: 'Home', href="#", selected: true },
{ value: 'about', label: 'About', href="www.example.com/about" },
{ value: 'support', label: 'Support', href="www.support.example.com", external: true }
];
const { isExpanded, getTriggerProps, getMenuProps, getItemProps, getAnchorProps } = useMenu({
triggerRef,
menuRef,
items
});

return (
<>
<button {...getTriggerProps()}>Menu</button>
<ul {...getMenuProps()} style={{ visibility: isExpanded ? 'visible' : 'hidden' }}>
{items.map(item => (
<li key={item.value} {...getItemProps({ item })}>
<a {...getAnchorProps({ item })}>{item.label}</a>
</li>
))}
</ul>
</>
);
};
```

### MenuContainer

```jsx
Expand All @@ -61,27 +97,20 @@ const Menu = () => {
const items = [
{ value: 'value-1', label: 'One' },
{ value: 'value-2', label: 'Two' },
{ value: 'value-3', label: 'Three', href: '#0' },
{ value: 'value-4', label: 'Four' }
{ value: 'value-3', label: 'Three' }
];

return (
<MenuContainer triggerRef={triggerRef} menuRef={menuRef} items={items}>
{({ isExpanded, getTriggerProps, getMenuProps, getItemProps, getSeparatorProps }) => (
{({ isExpanded, getTriggerProps, getMenuProps, getItemProps }) => (
<>
<button {...getTriggerProps()}>Menu</button>
<ul {...getMenuProps()} style={{ visibility: isExpanded ? 'visible' : 'hidden' }}>
{items.map(item =>
item.href ? (
<li key={item.value} role="none">
<a {...getItemProps({ item })}>{item.label}</a>
</li>
) : (
<li key={item.value} {...getItemProps({ item })}>
{item.label}
</li>
)
)}
{items.map(item => (
<li key={item.value} {...getItemProps({ item })}>
{item.label}
</li>
))}
</ul>
</>
)}
Expand Down
2 changes: 2 additions & 0 deletions packages/menu/demo/menu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export const Controlled: Story = {
return (
<MenuStory
{...args}
// Storybook sets args to null when they are removed from controls. This breaks useMenu since getControlledValue treats null as intentional input.
selectedItems={args.selectedItems === null ? undefined : args.selectedItems}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onChange={({ type, ...rest }) => {
updateArgs(rest);
Expand Down
41 changes: 30 additions & 11 deletions packages/menu/demo/stories/MenuStory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/

import React, { AnchorHTMLAttributes, LiHTMLAttributes, useRef } from 'react';
import React, { useEffect, useRef } from 'react';
import { StoryFn } from '@storybook/react';
import classNames from 'classnames';
import {
Expand All @@ -29,18 +29,20 @@ interface IUseMenuComponentProps extends MenuReturnValue {
type MenuItemProps = {
item: IMenuItemBase;
getItemProps: IUseMenuComponentProps['getItemProps'];
getAnchorProps: IUseMenuComponentProps['getAnchorProps'];
focusedValue: IUseMenuComponentProps['focusedValue'];
isSelected?: boolean;
};

const Item = ({ item, getItemProps, focusedValue, isSelected }: MenuItemProps) => {
const itemProps = getItemProps({ item });
const Item = ({ item, getAnchorProps, getItemProps, focusedValue, isSelected }: MenuItemProps) => {
const itemProps = getItemProps<HTMLLIElement>({ item });
const anchorProps = getAnchorProps({ item });

const itemChildren = (
<>
<span className="inline-flex justify-center items-center w-4">
{item?.type === 'radio' && !!isSelected && '•'}
{item?.type === 'checkbox' && !!isSelected && '✓'}
{!!isSelected && item.type === 'radio' && '•'}
{!!isSelected && (item.type === 'checkbox' || !!item.href) && '✓'}
</span>
{item.label || item.value}
</>
Expand All @@ -54,16 +56,21 @@ const Item = ({ item, getItemProps, focusedValue, isSelected }: MenuItemProps) =
'cursor-pointer': !item.disabled,
'cursor-default': item.disabled
})}
role={itemProps.href ? 'none' : undefined}
{...(!itemProps.href && (itemProps as LiHTMLAttributes<HTMLLIElement>))}
{...itemProps}
>
{itemProps.href ? (
{anchorProps ? (
<a
{...(itemProps as AnchorHTMLAttributes<HTMLAnchorElement>)}
className="w-full rounded-sm outline-offset-0 transition-none border-width-none"
{...anchorProps}
className={classNames(
' w-full rounded-sm outline-offset-0 transition-none border-width-none',
{
'text-grey-400': item.disabled,
'cursor-default': item.disabled
}
)}
>
{itemChildren}
{!!item.isExternal && (
{anchorProps.target === '_blank' && (
<>
<span aria-hidden="true"> ↗</span>
<span className="sr-only">(opens in new window)</span>
Expand All @@ -85,11 +92,20 @@ const Component = ({
getTriggerProps,
getMenuProps,
getItemProps,
getAnchorProps,
getItemGroupProps,
getSeparatorProps
}: MenuReturnValue & UseMenuProps) => {
const selectedValues = selection.map(item => item.value);

useEffect(() => {
const originalWindowOpen = window.open;
window.open = () => null;
return () => {
window.open = originalWindowOpen;
};
}, []);

return (
<div className="relative">
<button className="px-2 py-1" type="button" {...getTriggerProps()}>
Expand All @@ -116,6 +132,7 @@ const Component = ({
key={groupItem.value}
item={{ ...groupItem }}
getItemProps={getItemProps}
getAnchorProps={getAnchorProps}
focusedValue={focusedValue}
isSelected={selectedValues.includes(groupItem.value)}
/>
Expand All @@ -141,6 +158,8 @@ const Component = ({
item={item}
focusedValue={focusedValue}
getItemProps={getItemProps}
getAnchorProps={getAnchorProps}
isSelected={selectedValues.includes(item.value)}
/>
);
})}
Expand Down
11 changes: 9 additions & 2 deletions packages/menu/demo/stories/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ export const ITEMS: MenuItem[] = [
value: 'plant-04',
label: 'Aloe Vera',
href: 'https://en.wikipedia.org/wiki/Aloe_vera',
isExternal: false
external: false
},
{ value: 'plant-05', label: 'Succulent' },
{
value: 'plant-05',
label: 'Palm tree',
href: 'https://en.wikipedia.org/wiki/Palm_tree',
external: true,
disabled: true
},
{ value: 'plant-06', label: 'Succulent' },
{
label: 'Choose favorites',
items: [
Expand Down
Loading