components / actions
Dropdown Menu
A menu of actions that opens from a button, with icons, shortcuts, search, recent items, confirm-to-run items, checkable items, and submenus.
dropdown-menu-default.tsx
"use client";
import { ChevronDownIcon, CopyIcon, LinkIcon, MailIcon, PencilIcon, Share2Icon, Trash2Icon } from "lucide-react";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuCheckboxItem,
DropdownMenuFooter,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuPopup,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuSeparator,
DropdownMenuSub,
DropdownMenuSubTrigger,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
export default function DropdownMenuDefault() {
const [grid, setGrid] = useState(true);
const [sort, setSort] = useState("name");
return (
<DropdownMenu>
<DropdownMenuTrigger render={<Button variant="outline" />}>
options <ChevronDownIcon aria-hidden="true" />
</DropdownMenuTrigger>
<DropdownMenuPopup>
<DropdownMenuGroup>
<DropdownMenuLabel>Project</DropdownMenuLabel>
<DropdownMenuItem description="Change the name and the slug" icon={<PencilIcon />} shortcut="F2">
Rename
</DropdownMenuItem>
<DropdownMenuItem icon={<CopyIcon />} shortcut="Ctrl D">
Duplicate
</DropdownMenuItem>
<DropdownMenuSub>
<DropdownMenuSubTrigger icon={<Share2Icon />}>Share</DropdownMenuSubTrigger>
<DropdownMenuPopup>
<DropdownMenuItem icon={<LinkIcon />}>Copy link</DropdownMenuItem>
<DropdownMenuItem icon={<MailIcon />}>Send by email</DropdownMenuItem>
</DropdownMenuPopup>
</DropdownMenuSub>
</DropdownMenuGroup>
<DropdownMenuSeparator />
<DropdownMenuCheckboxItem checked={grid} onCheckedChange={setGrid}>
Grid view
</DropdownMenuCheckboxItem>
<DropdownMenuSeparator />
<DropdownMenuRadioGroup onValueChange={setSort} value={sort}>
<DropdownMenuLabel>Sort by</DropdownMenuLabel>
<DropdownMenuRadioItem value="name">Name</DropdownMenuRadioItem>
<DropdownMenuRadioItem value="date">Date</DropdownMenuRadioItem>
</DropdownMenuRadioGroup>
<DropdownMenuSeparator />
<DropdownMenuItem confirm icon={<Trash2Icon />} variant="destructive">
Delete
</DropdownMenuItem>
<DropdownMenuFooter />
</DropdownMenuPopup>
</DropdownMenu>
);
}Installation
$ npx shadcn@latest add https://cd-ui.vercel.app/r/dropdown-menu.jsonfirst time? set up components.json and the theme first: see installation.
Usage
import { DropdownMenu, DropdownMenuItem, DropdownMenuPopup, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"
<DropdownMenu>
<DropdownMenuTrigger render={<Button />}>Options</DropdownMenuTrigger>
<DropdownMenuPopup>
<DropdownMenuItem onClick={rename}>Rename</DropdownMenuItem>
<DropdownMenuItem variant="destructive">Delete</DropdownMenuItem>
</DropdownMenuPopup>
</DropdownMenu>Examples
Search and recent
A search field filters the list as you type, and the last two actions you chose move to a Recent section.
dropdown-menu-command.tsx
"use client";
import { BellIcon, ChevronDownIcon, FileTextIcon, FolderPlusIcon, GitBranchIcon, SettingsIcon, UploadIcon } from "lucide-react";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuEmpty,
DropdownMenuFooter,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuPopup,
DropdownMenuSearch,
DropdownMenuTrigger,
matchesMenuQuery,
useRecentItems,
} from "@/components/ui/dropdown-menu";
const actions = [
{ id: "file", label: "New file", description: "Start from a blank page", icon: <FileTextIcon />, shortcut: "N" },
{ id: "folder", label: "New folder", description: "Group files together", icon: <FolderPlusIcon />, shortcut: "Shift N" },
{ id: "upload", label: "Upload", description: "From your computer", icon: <UploadIcon />, shortcut: "U" },
{ id: "branch", label: "Create branch", description: "Work on a copy", icon: <GitBranchIcon />, shortcut: "B" },
{ id: "alerts", label: "Notifications", description: "Choose what reaches you", icon: <BellIcon />, shortcut: "G N" },
{ id: "settings", label: "Settings", description: "Workspace preferences", icon: <SettingsIcon />, shortcut: "," },
];
export default function DropdownMenuCommand() {
const [query, setQuery] = useState("");
const { recent, push } = useRecentItems<string>(2);
const matches = actions.filter((a) => matchesMenuQuery(`${a.label} ${a.description}`, query));
const last = query ? [] : matches.filter((a) => recent.includes(a.id));
const rest = matches.filter((a) => !last.includes(a));
const renderItem = (a: (typeof actions)[number]) => (
<DropdownMenuItem description={a.description} icon={a.icon} key={a.id} label={a.label} onClick={() => push(a.id)} shortcut={a.shortcut}>
{a.label}
</DropdownMenuItem>
);
return (
<DropdownMenu onOpenChange={(open) => open || setQuery("")}>
<DropdownMenuTrigger render={<Button variant="outline" />}>
quick actions <ChevronDownIcon aria-hidden="true" />
</DropdownMenuTrigger>
<DropdownMenuPopup className="w-72">
<DropdownMenuSearch aria-label="Filter actions" onChange={(e) => setQuery(e.target.value)} placeholder="Filter actions" value={query} />
{last.length > 0 && (
<DropdownMenuGroup>
<DropdownMenuLabel>Recent</DropdownMenuLabel>
{last.map(renderItem)}
</DropdownMenuGroup>
)}
<DropdownMenuGroup>
{last.length > 0 && rest.length > 0 && <DropdownMenuLabel>All actions</DropdownMenuLabel>}
{rest.map(renderItem)}
</DropdownMenuGroup>
{matches.length === 0 && <DropdownMenuEmpty>{`Nothing matches "${query}".`}</DropdownMenuEmpty>}
<DropdownMenuFooter />
</DropdownMenuPopup>
</DropdownMenu>
);
}API
DropdownMenu
Base UI · Menu.RootRoot. Also used for each submenu, through DropdownMenuSub.
| prop | type | default |
|---|---|---|
open · defaultOpen · onOpenChangeOpen state control. | boolean / (open) => void | - |
DropdownMenuTrigger
Base UI · Menu.TriggerButton that opens the menu. Use render to use your own Button.
| prop | type | default |
|---|---|---|
renderSwaps the rendered element while keeping behavior and style (e.g. become an | ReactElement | (props) => ReactElement | - |
DropdownMenuPopup
Base UI · Menu.PopupList of actions. Scales and fades in from the trigger in 160ms, closes in 120ms, and appears instantly when opened with the keyboard.
| prop | type | default |
|---|---|---|
sidePreferred side. Submenus open to the right on their own. | "top" | "bottom" | "left" | "right" | - |
alignAlignment relative to the trigger. | "start" | "center" | "end" | - |
sideOffsetDistance from the trigger (px). | number | 6 |
classNameExtra classes, merged with | string | - |
DropdownMenuItem
Base UI · Menu.ItemAction. The menu closes after it is chosen.
| prop | type | default |
|---|---|---|
variantUse | "default" | "destructive" | "default" |
iconIcon at the start of the row. | ReactNode | - |
descriptionA second, muted line under the label. | ReactNode | - |
shortcutShortcut text at the end of the row, in mono. Only a hint. | ReactNode | - |
confirmThe first click arms the item (it turns red and asks again), the second runs it. It goes back to idle after 3 seconds. | boolean | false |
confirmLabelText shown while the item is armed. | string | "Click again to confirm" |
labelPlain text used by type to select when the children are not plain text. | string | - |
onClickCalled when the item is chosen. | () => void | - |
classNameExtra classes, merged with | string | - |
DropdownMenuSearch · matchesMenuQuery · DropdownMenuEmpty
Search field pinned to the top of the menu. It only holds the text: filter your own items with matchesMenuQuery and show DropdownMenuEmpty when nothing is left.
| prop | type | default |
|---|---|---|
value · onChangeThe text, controlled by you. | string / (event) => void | - |
aria-labelAccessible name of the field. | string | - |
classNameExtra classes, merged with | string | - |
useRecentItems · DropdownMenuFooter
useRecentItems remembers the last ids chosen, newest first, for a Recent section. DropdownMenuFooter shows the keyboard hints (hidden on touch screens) or your own children.
| prop | type | default |
|---|---|---|
limitHow many ids to keep. | number | 3 |
classNameExtra classes, merged with | string | - |
DropdownMenuCheckboxItem · DropdownMenuRadioGroup · DropdownMenuRadioItem
Base UI · Menu.CheckboxItem · Menu.RadioGroup · Menu.RadioItemItems with a check. The checkbox item toggles on its own; radio items live inside a DropdownMenuRadioGroup.
| prop | type | default |
|---|---|---|
checked · onCheckedChangeControlled or initial state. | boolean / (checked) => void | - |
value · onValueChangeControlled or initial state. | string / (value) => void | - |
DropdownMenuGroup · DropdownMenuLabel · DropdownMenuSeparator · DropdownMenuShortcut · DropdownMenuSub · DropdownMenuSubTrigger
Organize the list: DropdownMenuGroup with a DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuShortcut, and DropdownMenuSub with DropdownMenuSubTrigger (which also takes icon and description).
| prop | type | default |
|---|---|---|
classNameExtra classes, merged with | string | - |
Keyboard
| key | action |
|---|---|
| EnterSpace↓ | Opens the menu from the trigger. |
| ↑↓ | Moves between items. |
| EnterSpace | Chooses the item. |
| →← | Opens and closes a submenu. |
| Esc | Closes and returns focus to the trigger. |
| A-Z | Jumps to the item that starts with the letter. |
| Enter | On an item with `confirm`, pressing it twice runs it. |
| ↓ | In the search field, moves to the first item. |
Accessibility
DropdownMenuShortcutonly displays the hint. Wire up the real shortcut yourself.- An armed item changes its text to the confirm label, so screen readers hear it when the item is focused again. It never confirms on its own.
- Give
DropdownMenuSearchanaria-label. Typing there does not trigger type to select, and Esc still closes the menu.