Menu
A short list of actions that opens from a button and closes once people pick one.
import { Menu } from '@halo-ds/react';
When to use it
Use a menu to tuck away actions that would crowd the page, like Edit, Duplicate and Delete on a file, or to offer a few choices, like how to sort a list. It opens from a Button, a Compact button such as "⋯", or an Avatar, and closes when people choose an item, press Esc or click elsewhere. Choosing an item does something straight away; to set a value in a form, use a Select.
Do
- Start labels with a verb, like Edit or Download.
- Keep menus short: about seven items at most.
- Put destructive items last, after a Divider.
- Open the menu next to the button that opened it.
Don't
- Put things people need often only in a menu.
- Nest submenus more than one level deep.
- Use a menu to fill in a form; use a Select.
- Mix icons and no icons in the same group.
Props
Menu props
| Prop | Type | What it does | |
|---|---|---|---|
trigger (required) | `ReactElement<unknown, string \ | JSXElementConstructor<any>>` | The element that opens the menu, like a "more" CompactButton. |
items (required) | MenuEntry[] | ||
side | `Side \ | undefined` | |
align | `Align \ | undefined` | |
open | `boolean \ | undefined` | |
defaultOpen | `boolean \ | undefined` | |
onOpenChange | `((open: boolean) => void) \ | undefined` | |
aria-label | `string \ | undefined` | Screen reader name for the menu. |
className | `string \ | undefined` | |
classNames | `Partial<Record<MenuPart, string>> \ | undefined` | Extra class names for the parts inside the component, by part name, like { header: 'my-header' }. |
styles | `Partial<Record<MenuPart, CSSProperties>> \ | undefined` | Inline styles for the parts inside the component, by part name, like { footer: { gap: 4 } }. |
It also takes the usual HTML attributes for its element.
Examples
Overview
import { CompactButton, Menu } from '@halo-ds/react';
import { Copy, DotsThree, PencilSimple, Trash } from '@phosphor-icons/react';
export default function Example() {
return (
<Menu
aria-label="File actions"
trigger={
<CompactButton aria-label="More actions">
<DotsThree />
</CompactButton>
}
items={[
{ label: 'Rename', icon: <PencilSimple /> },
{ label: 'Duplicate', icon: <Copy /> },
{ type: 'separator' },
{ label: 'Delete', icon: <Trash />, destructive: true },
]}
/>
);
}
With a selection
A chosen option shows a check mark, like a sort order. Labels and separators group the items.
import { Button, Menu } from '@halo-ds/react';
export default function Demo() {
return (
<Menu
aria-label="Sort by"
trigger={<Button variant="secondary">Sort</Button>}
items={[
{ type: 'label', label: 'Sort by' },
{ label: 'Newest first', selected: true },
{ label: 'Oldest first' },
{ label: 'Name' },
{ type: 'separator' },
{ label: 'Size', disabled: true },
]}
/>
);
}
More: https://halodesignsystem.dev/components/menu