@dax-ui/menu
v0.8.0
Published
dax-ui MenuButton: a button that opens a menu of actions with icons, titles and descriptions
Maintainers
Readme
@dax-ui/menu
MenuButton: a button that opens a menu of actions, each with an icon, a title and a
muted description, like the calendar's "+ Add New" menu. Solid, outlined, ghost or
icon-only, in three sizes, with links, dividers, disabled items and your own colors. It
uses MUI's Menu, so it has menu semantics and full keyboard support.
Installation
pnpm add @dax-ui/menuMUI and Emotion are included; the app only needs React 18 or 19.
One-time setup per app (skip if the app already uses dax-ui): install
@dax-ui/theme and wrap the app in its UIProvider.
pnpm add @dax-ui/themeAny icon component works; the examples use lucide-react.
Usage
import { MenuButton, type MenuButtonItem } from '@dax-ui/menu';
import { Bell, CalendarDays, Plus, SquareCheck } from 'lucide-react';
const items: MenuButtonItem[] = [
{ id: 'reminder', label: 'Add Reminder', description: 'Just for me', icon: <Bell strokeWidth={1.5} /> },
{ id: 'follow-up', label: 'Add Follow-Up', description: 'a tracked action in a study or lead', icon: <SquareCheck strokeWidth={1.5} /> },
{ id: 'event', label: 'Add Event', description: 'a scheduled activity', icon: <CalendarDays strokeWidth={1.5} /> },
];
<MenuButton label="Add New" icon={<Plus />} items={items} onItemClick={(item) => openForm(item.id)} />;Links
Items with href render as links. Pass your router's link as linkComponent:
import Link from 'next/link';
<MenuButton
label="Export"
linkComponent={Link}
items={[
{ id: 'csv', label: 'Download CSV', href: '/export.csv' },
{ id: 'ics', label: 'Download .ics', href: '/export.ics', divider: true },
{ id: 'pdf', label: 'Print to PDF', description: 'Coming soon', disabled: true },
]}
/>;Icon-only row actions
import { Ellipsis, Pencil, Trash2 } from 'lucide-react';
<MenuButton
variant="icon"
icon={<Ellipsis />}
aria-label="Event actions"
placement="bottom-start"
menuWidth={240}
items={[
{ id: 'edit', label: 'Edit', icon: <Pencil />, onClick: () => edit(row) },
{ id: 'delete', label: 'Delete', description: 'This cannot be undone', icon: <Trash2 />, onClick: () => remove(row) },
]}
/>;Colors
Every color is a CSS variable (--dax-menu-<key>) set from colors, on the button and on
the menu panel:
<MenuButton label="Add New" items={items} colors={{ buttonBackground: '#4338ca', buttonHover: '#3730a3', itemIcon: '#4f46e5' }} />Accessibility
The button has aria-haspopup="menu", aria-expanded and aria-controls; the menu is
named by the button. ArrowDown/ArrowUp on the button open the menu, arrow keys move between
items, Enter chooses, Escape closes and returns focus to the button. An icon-only button
needs an aria-label (a warning is logged in development without one).
MenuButton props
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| items (required) | MenuButtonItem[] | | The menu's items. |
| aria-label | string | | Accessible name of the button. Required for variant="icon" unless label is a string. |
| className | string | | Class name of the button. |
| closeOnItemClick | boolean | true | Closes the menu after an item is chosen. |
| colors | Partial<MenuButtonColors> | | Overrides any of the colors, e.g. { buttonBackground: '#4f46e5' }. Unset keys keep the defaults. |
| defaultOpen | boolean | false | Initial open state when open isn't controlled. |
| disabled | boolean | false | Disables the button. |
| icon | ReactNode | | Icon in front of the label, e.g. <PlusIcon />. The only content of an icon button. |
| id | string | | Id of the button; the menu's id is derived from it. Generated when not set. |
| label | ReactNode | | Button text, e.g. "Add New". With variant="icon" it becomes the accessible name when no aria-label is set. |
| linkComponent | ElementType | | Component used for items with href, e.g. Next.js Link or a React Router link. Defaults to <a>. |
| menuFooter | ReactNode | | Content below the items, e.g. a link to settings. |
| menuHeader | ReactNode | | Content above the items, e.g. a small heading. |
| menuSx | SxProps<Theme> | | Style overrides for the menu panel. |
| menuWidth | string \| number | | Width of the menu panel: a number in px or any CSS width. Defaults to fitting the items (at least the button's width). |
| onItemClick | ((item: MenuButtonItem, event: MouseEvent<HTMLElement, MouseEvent>) => void) | | Called when any item is chosen, after the item's own onClick. |
| onOpenChange | ((open: boolean) => void) | | Called when the menu opens or closes. |
| open | boolean | | Whether the menu is open. Pair with onOpenChange to control it. |
| placement | "bottom-start" \| "bottom" \| "bottom-end" \| "top-start" \| "top" \| "top-end" | bottom-end | Where the menu opens relative to the button. |
| renderItem | ((item: MenuButtonItem) => ReactNode) | | Replaces the content of every item (the menu keeps the item element, focus handling and click). |
| showChevron | boolean | true | Shows the chevron after the label, which flips while the menu is open. Never shown on icon buttons. |
| size | "sm" \| "md" \| "lg" | md | Button height: sm 36px, md 44px, lg 52px. |
| sx | SxProps<Theme> | | Style overrides for the button. |
| variant | "solid" \| "outlined" \| "ghost" \| "icon" | solid | Button style. |
MenuButtonItem
| Field | Type | Description |
| --- | --- | --- |
| id (required) | string | Unique id, passed back to onItemClick. |
| label (required) | ReactNode | Item title; also its accessible name. |
| description | ReactNode | Muted second line under the title. |
| icon | ReactNode | Icon before the title, sized to 28px and colored colors.itemIcon. |
| onClick | (item, event) => void | Called when the item is chosen, before onItemClick. |
| href | string | Makes the item a link, rendered with linkComponent. |
| disabled | boolean | Shows the item but makes it unselectable. |
| divider | boolean | Draws a line below the item. |
MenuButtonColors
All optional in the colors prop. Any CSS color.
| Key | Used for |
| --- | --- |
| buttonBackground | solid/icon button background; text and border of outlined and ghost |
| buttonText | Text and icon on solid and icon buttons |
| buttonHover | solid/icon background on hover and while open |
| menuBackground | Menu panel |
| itemTitle | Item titles |
| itemDescription | Item descriptions, header and footer |
| itemIcon | Item icons |
| itemHover | Item background on hover and keyboard focus |
| divider | Item dividers |
| focusRing | Keyboard focus ring around the button |
Other exports
| Export | What it is |
| --- | --- |
| DEFAULT_MENU_BUTTON_COLORS | The default colors, e.g. to build a palette from |
