Dropdown menu
A trigger opens a list of actions the traveller runs once: rename, duplicate, share, delete, or go somewhere.
Installation
npx shadcn@latest add @flyingsalmon/dropdown-menuUsage
import { Button, ButtonVariant } from '@/components/ui/button'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'
interface DropdownMenuUsageProps {
onRename: () => void
onDuplicate: () => void
}
export function DropdownMenuUsage({
onRename,
onDuplicate,
}: DropdownMenuUsageProps) {
return (
<DropdownMenu>
<DropdownMenuTrigger>
<Button variant={ButtonVariant.Outline}>Trip actions</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem onSelect={onRename}>Rename</DropdownMenuItem>
<DropdownMenuItem onSelect={onDuplicate}>Duplicate</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)
}Examples
Without icons
An icon-free menu starts flush at the left edge, because the icon slot exists only on an item that gets an icon.
Labelled groups
A group with a heading puts a DropdownMenuLabel first. A separator divides groups; the group itself has no visual of its own.
A disabled item
The item stays in the list, dimmed, and the arrow keys skip it, so the menu never shifts between opens.
Placement
side and align choose where the panel opens. Aligning to the end keeps a menu on the right edge of the page from running off it.
Actions on a trip card
Duplicate adds a card straight away. Delete opens a confirmation, and the card disappears when it closes. Each result shows on the item that changed.
Kyoto in autumn
Lisbon long weekend
Guidelines
When to use
- For the actions on one item, gathered behind an overflow button: rename, duplicate, share, delete.
- For a short list of places to go from one trigger.
When not to use
Do and don’t
- DoShow the result on the item the action changed.The menu closes on select, so the traveller is looking at the item again. A duplicated trip appearing in the list is the confirmation. When the item has no visible home, a notice carries the result.
- DoOpen a confirmation dialog from a destructive item.The menu offers the choice and the dialog asks for the decision, so one stray click cannot delete a trip.
- DoPut an overflow trigger at the trailing edge of its row, as a ghost icon button, and open the menu aligned to its end.The row reads first and its actions trail it. An end-aligned menu opens over the row rather than past the edge of the page.
- Don’tShow an overflow trigger on an item with no actions.A trigger that opens nothing teaches the traveller to ignore every other one. Leave the trailing edge empty.
- DoGive every item in a menu an icon, or none of them.Mixed rows leave the labels ragged.
- DoHead the list with a quiet label for context, such as the trip name above the trip actions.The traveller sees which trip the actions apply to before they choose. Muted text never looks like something to press.
- Don’tAdd checkbox items, radio items, or a submenu.The menu is for actions only. A choice that persists belongs on the page as a field.
Accessibility
| Key | Description |
|---|---|
| EnterSpaceArrowDown | On the trigger, opens the menu with the first item highlighted. A pointer click opens it with no item highlighted. |
| ArrowUpArrowDown | Moves the highlight. It stops at the first and last item rather than wrapping. |
| HomeEnd | Jumps to the first or last item. |
| EnterSpace | On an item, runs it and closes the menu. |
| Escape | Closes the menu and returns focus to the trigger. |
Typing a letter jumps to the next item starting with it. Tab does nothing inside the menu, and disabled items are skipped. An outside click closes the menu and returns focus to the trigger, including after a link item navigates.
The trigger exposes aria-haspopup="menu" and aria-expanded. An icon-only trigger has no name of its own, so its aria-label names what the menu acts on. The highlight moves with focus and is the focus indicator, so an item draws no ring of its own.
API
DropdownMenu
The root. The menu is always modal.
| Prop | Type | Default | Description |
|---|---|---|---|
| open | boolean | – | The controlled open state. |
| defaultOpen | boolean | false | The initial open state when uncontrolled. |
| onOpenChange | (open: boolean) => void | – | Called when the menu opens or closes. |
DropdownMenuTrigger
| Prop | Type | Default | Description |
|---|---|---|---|
| children | ReactElement | Required | One element, a registry Button, that opens the menu. An icon-only button takes an aria-label that names what the menu acts on, such as "Actions for Kyoto in autumn". |
DropdownMenuContent
The panel. Everything else about its positioning is fixed.
| Prop | Type | Default | Description |
|---|---|---|---|
| side | PanelSide | PanelSide.Bottom | Which edge of the trigger the panel opens from. It flips when there is no room. |
| align | PanelAlign | PanelAlign.Center | How the panel lines up along that edge. |
DropdownMenuItem
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | DropdownMenuItemVariant | DropdownMenuItemVariant.Default | Destructive marks an item that removes something. |
| icon | ReactNode | – | Fills the slot before the label. Give every item an icon or none. |
| onSelect | (event: Event) => void | – | Runs the action. The menu closes after it. |
| asChild | boolean | false | Renders your element, such as a router Link, in place of the item. |
| disabled | boolean | false | Dims the item, skips it in the arrow path, and ignores selection. |
DropdownMenuGroup, DropdownMenuLabel, DropdownMenuSeparator, and DropdownMenuShortcut take the props of the element they render, typed as DropdownMenuGroupProps, DropdownMenuLabelProps, DropdownMenuSeparatorProps, and DropdownMenuShortcutProps. DropdownMenuShortcut displays the shortcut at the trailing edge and binds no key, so your app owns the shortcut.