[ Overlays ]
Dropdown
A menu of actions that opens from a trigger button. Supports icons, dividers, disabled and danger items, full keyboard navigation and closing on outside click.
Import#
import { Dropdown } from 'reusify-ui';When to use#
- Grouping secondary actions behind one button: row actions ("⋯"), account menus.
- For choosing a value in a form, use Select instead.
Examples#
Account menu#
trigger is any element (usually a Button). Each item can have an icon, be disabled, or be marked as danger.
<Dropdown
trigger={<Button variant="secondary" leftIcon={<FiUser />}>Account</Button>}
items={[
{ label: 'Profile', icon: <FiUser />, onClick: openProfile },
{ label: 'Settings', icon: <FiSettings />, onClick: openSettings },
{ divider: true },
{ label: 'Billing', disabled: true },
{ label: 'Sign out', icon: <FiLogOut />, danger: true, onClick: signOut },
]}
/>Align to the end#
Use align="end" for triggers near the right edge, like a "⋯" button at the end of a table row.
<Dropdown
align="end"
trigger={<Button variant="ghost" aria-label="Row actions"><FiMoreVertical /></Button>}
items={[
{ label: 'Edit', icon: <FiEdit2 /> },
{ label: 'Duplicate', icon: <FiCopy /> },
{ divider: true },
{ label: 'Delete', icon: <FiTrash2 />, danger: true },
]}
/>Used in designs#
Dropdown in real layouts: these 3 designs use it. Open one to try it and copy the code.
API reference#
<Dropdown />
| Prop | Type | Default | Description |
|---|---|---|---|
trigger* | ReactElement | - | Element that toggles the menu. Its own onClick still runs. |
items* | DropdownItem[] | - | Menu entries (see below). |
align | 'start' | 'end' | 'start' | Align the menu to the left or right edge of the trigger. |
closeOnSelect | boolean | true | Close after an item is chosen. |
className | string | - | Class for the wrapper. |
trigger*-ReactElementElement that toggles the menu. Its own onClick still runs.
items*-DropdownItem[]Menu entries (see below).
align'start''start' | 'end'Align the menu to the left or right edge of the trigger.
closeOnSelecttruebooleanClose after an item is chosen.
className-stringClass for the wrapper.
<DropdownItem />
| Prop | Type | Default | Description |
|---|---|---|---|
label | ReactNode | - | Item text. |
onClick | () => void | - | Called when the item is chosen. |
icon | ReactNode | - | Icon before the label. |
disabled | boolean | - | Item cannot be chosen. |
danger | boolean | - | Shows the item in the danger color. |
divider | boolean | - | Renders a separator line instead of an item. |
key | string | - | Optional stable React key. |
label-ReactNodeItem text.
onClick-() => voidCalled when the item is chosen.
icon-ReactNodeIcon before the label.
disabled-booleanItem cannot be chosen.
danger-booleanShows the item in the danger color.
divider-booleanRenders a separator line instead of an item.
key-stringOptional stable React key.
Accessibility#
- The trigger gets aria-haspopup="menu", aria-expanded and aria-controls.
- Arrow Down on the trigger opens the menu; Arrow Up/Down, Home and End move between items; Escape closes and returns focus to the trigger.
- Items use role="menuitem"; dividers use role="separator".