Dropdown menu
A menu of actions that opens from a button: rename, share, delete, with settings and submenus when a view needs them. Use it for things to do. To pick one value that a field then shows, use a select; for the actions of an area under the pointer, a context menu.
Install
npx shadcn@latest add https://thrumui.dev/r/dropdown-menu.jsonAdds components/ui/dropdown-menu.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.
import { playMenuOpen } from "@/components/ui/dropdown-menu";Automatically installs icons check and chevron-right from the icon set.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
DropdownMenu: open | boolean | – | Whether the menu is open, when the parent holds the state. Use it with onOpenChange. |
DropdownMenu: defaultOpen | boolean | Default false | Whether it starts open, when the menu holds its own state. |
DropdownMenu: onOpenChange | (open: boolean, details) => void | – | Called when the menu opens or closes. details.reason says why: a press on the trigger, Escape, a pick, a press outside. |
DropdownMenu: modal | boolean | Default true | While open, the page behind does not scroll and cannot be pressed. Set false to leave the page usable. |
DropdownMenu: loopFocus | boolean | Default true | Whether the arrow keys wrap from the last item to the first. |
DropdownMenu: disabled | boolean | Default false | The trigger is dimmed and the menu cannot be opened. |
DropdownMenu: sound | boolean | Default true | Play the menu's sounds, if the visitor has sound on. |
DropdownMenuTrigger | button props | – | The button that opens the menu. Style it with buttonClass(). With only an icon inside, give it an aria-label. |
DropdownMenuContent: side | "top" | "bottom" | "left" | "right" | "inline-start" | "inline-end" | Default "bottom" | The side of the trigger the list opens on. It flips when there is no room. |
DropdownMenuContent: align | "start" | "center" | "end" | Default "start" | How the list lines up with the trigger along that side. |
DropdownMenuContent: sideOffset | number | Default 4 | The gap between the trigger and the list, in pixels. |
DropdownMenuContent: alignOffset | number | Default 0 | A shift along the trigger's edge, in pixels. |
DropdownMenuContent: className | string | – | Extra classes for the list. It is at least 160 pixels wide and grows with its longest row; w-56 fixes the width. |
DropdownMenuItem: onClick | (event) => void | – | Runs when the item is picked, by pointer or by Enter or Space. |
DropdownMenuItem: children | ReactNode | – | The row's text. An icon before it is drawn at 16 pixels, and the text then lines up with checkbox and radio items without inset. DropdownMenuSubTrigger takes one too. |
DropdownMenuItem: destructive | boolean | Default false | Red text, and a red fill while highlighted, for an action that deletes or cannot be undone. |
DropdownMenuItem: inset | boolean | Default false | Indent the text by 24 pixels so it lines up with checkbox and radio items. DropdownMenuLabel and DropdownMenuSubTrigger take it too. |
DropdownMenuItem: disabled | boolean | Default false | Dimmed, and it cannot be picked. The arrow keys still stop on it, so a screen reader can read it. Every item part takes it. |
DropdownMenuItem: closeOnClick | boolean | Default true | Whether picking the item closes the menu. Checkbox and radio items default to false. |
DropdownMenuItem: label | string | – | The text type-ahead matches, when the row's own text does not start with its name. |
DropdownMenuCheckboxItem: checked | boolean | – | Whether it is ticked, when the parent holds the state. Use it with onCheckedChange. |
DropdownMenuCheckboxItem: defaultChecked | boolean | Default false | Whether it starts ticked, when the item holds its own state. The list is removed while the menu is closed, so it starts from this value at every opening: hold the state yourself to keep it. |
DropdownMenuCheckboxItem: onCheckedChange | (checked: boolean) => void | – | Called with the new state when the item is picked. |
DropdownMenuRadioGroup: value | string | – | The value of the selected item, when the parent holds it. Use it with onValueChange. |
DropdownMenuRadioGroup: defaultValue | string | – | The value selected at first, when the group holds it itself. Like defaultChecked, it applies again at every opening. |
DropdownMenuRadioGroup: onValueChange | (value: string) => void | – | Called with the new value when another radio item is picked. |
DropdownMenuRadioItem: value | string | Required | What the group's onValueChange reports. Unique within the group. |
DropdownMenuGroup | div props | – | Items that belong together. A label inside it names the group for screen readers. |
DropdownMenuLabel | div props | – | The heading of a group. It must sit inside a DropdownMenuGroup or a DropdownMenuRadioGroup. |
DropdownMenuSeparator | div props | – | A line between groups. |
DropdownMenuShortcut | span props | – | Key hint text at the end of a row. The menu does not listen for the keys: bind them yourself. |
DropdownMenuSub: open, defaultOpen, onOpenChange | as DropdownMenu | – | The state of a submenu. It is silent: its parent menu already made the opening sound. |
DropdownMenuSubTrigger: openOnHover | boolean | Default true | Open the submenu when the pointer rests on its row for 100 ms. delay and closeDelay set the times in milliseconds. |
DropdownMenuSubContent | as DropdownMenuContent | – | The list of a submenu. It opens beside its row, with sideOffset -4 and alignOffset -9 so the first item is level with the row. |
Motion and sound
Motion
- The list fades in and grows from 96% to full size over 150 ms, from the corner that touches the trigger. A submenu does the same from its row.
- Closing plays the same change backwards, and a second press during it reverses it.
- Reduced motion: lists appear and leave at once.
Sound
- A soft rising tone when the menu opens and a falling one when it closes without a pick.
- A click when an item or a radio item is picked. That press makes no second sound for the closing menu.
- A rising pair of clicks when a checkbox item is ticked and a falling pair when it is cleared.
- Submenus are silent, because hovering opens them and nothing sounds on hover.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- It follows the WAI-ARIA menu button pattern: the trigger has aria-haspopup and aria-expanded, the list has the menu role, and rows are menuitem, menuitemcheckbox or menuitemradio.
- The trigger is named by its text. Give an icon-only trigger an aria-label.
- A group is named by the DropdownMenuLabel inside it.
- A ticked checkbox item carries a tick and aria-checked, the selected radio item a dot: state never rests on the highlight colour.
- A destructive item is red and should also say what it removes, as colour alone does not reach every reader.
- A long row wraps inside the list, which never grows wider than the space beside the trigger. A list taller than the screen scrolls.
- Under a glass material the list is translucent. Its tint keeps the text at 4.5 to 1 over any page, and every row sits 8 pixels inside the edge.
| Keys | Action |
|---|---|
| Enter / Space / ↓ | On the trigger: open the menu with the first item highlighted. |
| ↑ | On the trigger: open the menu with the last item highlighted. |
| ↓ / ↑ | Move the highlight to the next or previous item. Past the end it wraps. |
| Home / End | Move the highlight to the first or last item. |
| A to Z | Move the highlight to the next item that starts with the typed letters. |
| Enter / Space | Pick the highlighted item. A checkbox or radio item changes and the menu stays open. |
| → | On a submenu row: open the submenu and highlight its first item. In right-to-left text it is ←. |
| ← | In a submenu: close it and return to its row. In right-to-left text it is →. |
| Esc | Close the innermost open menu: a submenu first, then the menu. Focus returns to the row or to the trigger. |
| Tab | Close the menu and move on from the trigger. |
Source
"use client";
import { createContext, useContext, type ComponentProps } from "react";
import { Menu as MenuPrimitive } from "@base-ui/react/menu";
import { cn } from "@/lib/utils";
import { play } from "@/lib/thrum-sound";
import { CheckIcon } from "@/components/icons/check";
import { ChevronRightIcon } from "@/components/icons/chevron-right";
/** Whether the items of the menu around them may play sounds. Every menu root sets it: this one, the context menu, a menubar menu. */
export const MenuSoundContext = createContext(true);
/**
* The sound of a menu opening or closing. Silent when a pick closed it (the pick has its own click), when a neighbour
* in a menubar took over (that one's opening is the sound), and when the pointer opened it by hovering.
*/
export function playMenuOpen(open: boolean, reason: string): void {
if (reason === "item-press" || reason === "sibling-open" || reason === "trigger-hover") return;
play(open ? "open" : "close");
}
/** The popup of a menu: a floating surface whose items sit 8 pixels inside its edge. */
export const MENU_CONTENT =
"max-h-(--available-height) max-w-(--available-width) min-w-40 origin-(--transform-origin) overflow-y-auto overscroll-contain rounded-lg border bg-popover p-2 text-popover-foreground shadow-md outline-none " +
"transition-[opacity,scale] duration-150 ease-out data-ending-style:scale-[0.96] data-ending-style:opacity-0 data-instant:transition-none data-starting-style:scale-[0.96] data-starting-style:opacity-0 motion-reduce:transition-none";
/** One row of a menu. `data-inset` indents it, `data-destructive` turns it red. */
export const MENU_ITEM =
"relative flex min-h-8 cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-none select-none [&>svg]:size-4 [&>svg]:flex-none " +
"data-disabled:opacity-50 data-highlighted:bg-accent data-highlighted:text-accent-foreground data-inset:ps-8 " +
"data-destructive:text-destructive data-destructive:data-highlighted:bg-destructive data-destructive:data-highlighted:text-(--thrum-base-background,var(--background))";
/** The heading of a group of rows. */
export const MENU_LABEL = "px-2 py-1.5 text-xs font-medium text-muted-foreground data-inset:ps-8";
/** The line between groups of rows. It stops 8 pixels short of the popup's edge, like the rows. */
export const MENU_SEPARATOR = "my-1 h-px bg-border";
/** The key hint at the end of a row. On a highlighted row it takes the row's text colour. */
export const MENU_SHORTCUT = "ms-auto ps-4 text-xs whitespace-nowrap text-muted-foreground tabular-nums in-data-highlighted:text-current";
export type DropdownMenuProps = MenuPrimitive.Root.Props & {
/** Play a soft sound when the menu opens or closes, a click when an item is picked and a pair of clicks when a checkbox item changes, if the visitor has sound on. */
sound?: boolean;
};
/**
* A list of actions that opens from a button: rename, share, delete. `DropdownMenu` holds the state,
* `DropdownMenuTrigger` opens it and `DropdownMenuContent` is the list. For picking a value that a field then shows, use a select.
*/
export function DropdownMenu({ sound = true, onOpenChange, ...props }: DropdownMenuProps) {
return (
<MenuSoundContext.Provider value={sound}>
<MenuPrimitive.Root
onOpenChange={(open, details) => {
if (sound) playMenuOpen(open, details.reason);
onOpenChange?.(open, details);
}}
{...props}
/>
</MenuSoundContext.Provider>
);
}
/** The button that opens the menu. Style it with `buttonClass()`. With only an icon inside, give it an `aria-label`. */
export function DropdownMenuTrigger(props: MenuPrimitive.Trigger.Props) {
return <MenuPrimitive.Trigger data-slot="dropdown-menu-trigger" {...props} />;
}
export type DropdownMenuContentProps = MenuPrimitive.Popup.Props & Pick<MenuPrimitive.Positioner.Props, "side" | "align" | "sideOffset" | "alignOffset">;
/**
* The list. It opens under the trigger, lined up with its start edge, and moves to stay on screen.
* It is a floating surface, so it turns to glass when the page's material asks for it.
*/
export function DropdownMenuContent({ side, align = "start", sideOffset = 4, alignOffset, className, ...props }: DropdownMenuContentProps) {
return (
<MenuPrimitive.Portal>
<MenuPrimitive.Positioner side={side} align={align} sideOffset={sideOffset} alignOffset={alignOffset} className="z-50 outline-none">
<MenuPrimitive.Popup data-slot="dropdown-menu" data-surface="floating" className={cn(MENU_CONTENT, className)} {...props} />
</MenuPrimitive.Positioner>
</MenuPrimitive.Portal>
);
}
export type DropdownMenuItemProps = MenuPrimitive.Item.Props & {
/** An action that deletes or cannot be undone: red text, and a red fill while it is highlighted. */
destructive?: boolean;
/** Indent the text so it lines up with checkbox and radio items in the same menu. */
inset?: boolean;
};
/** One action. Picking it runs `onClick` and closes the menu. An icon goes before the text, a `DropdownMenuShortcut` after it. */
export function DropdownMenuItem({ destructive = false, inset = false, className, onClick, ...props }: DropdownMenuItemProps) {
const sound = useContext(MenuSoundContext);
return (
<MenuPrimitive.Item
data-slot="dropdown-menu-item"
data-destructive={destructive ? "" : undefined}
data-inset={inset ? "" : undefined}
className={cn(MENU_ITEM, className)}
onClick={(event) => {
if (sound) play("select");
onClick?.(event);
}}
{...props}
/>
);
}
/** A setting that is on or off. Picking it flips it and leaves the menu open. A tick marks it while it is on. */
export function DropdownMenuCheckboxItem({ className, children, onCheckedChange, ...props }: MenuPrimitive.CheckboxItem.Props) {
const sound = useContext(MenuSoundContext);
return (
<MenuPrimitive.CheckboxItem
data-slot="dropdown-menu-checkbox-item"
className={cn(MENU_ITEM, "ps-8", className)}
onCheckedChange={(checked, details) => {
if (sound) play(checked ? "toggleOn" : "toggleOff");
onCheckedChange?.(checked, details);
}}
{...props}
>
<MenuPrimitive.CheckboxItemIndicator className="absolute start-2 flex size-4 items-center justify-center">
<CheckIcon size={16} />
</MenuPrimitive.CheckboxItemIndicator>
{children}
</MenuPrimitive.CheckboxItem>
);
}
export type DropdownMenuRadioGroupProps = Omit<MenuPrimitive.RadioGroup.Props, "value" | "defaultValue" | "onValueChange"> & {
/** The value of the selected item, when the parent holds it. Use it with `onValueChange`. */
value?: string;
/** The value selected at first, when the group holds it itself. */
defaultValue?: string;
/** Called with the new value when another item is picked. */
onValueChange?: (value: string, details: MenuPrimitive.RadioGroup.ChangeEventDetails) => void;
};
/** A set of radio items of which one is selected. Name it with a `DropdownMenuLabel` inside it. */
export function DropdownMenuRadioGroup({ onValueChange, ...props }: DropdownMenuRadioGroupProps) {
const sound = useContext(MenuSoundContext);
return (
<MenuPrimitive.RadioGroup
data-slot="dropdown-menu-radio-group"
onValueChange={(value: string, details) => {
if (sound) play("select");
onValueChange?.(value, details);
}}
{...props}
/>
);
}
export type DropdownMenuRadioItemProps = Omit<MenuPrimitive.RadioItem.Props, "value"> & {
/** What the group's `onValueChange` reports. Unique within the group. */
value: string;
};
/** One choice of a radio group. Picking it selects it and leaves the menu open. A dot marks the selected one. */
export function DropdownMenuRadioItem({ className, children, ...props }: DropdownMenuRadioItemProps) {
return (
<MenuPrimitive.RadioItem data-slot="dropdown-menu-radio-item" className={cn(MENU_ITEM, "ps-8", className)} {...props}>
<MenuPrimitive.RadioItemIndicator className="absolute start-2 flex size-4 items-center justify-center">
<span className="size-2 rounded-full bg-current" />
</MenuPrimitive.RadioItemIndicator>
{children}
</MenuPrimitive.RadioItem>
);
}
/** Items that belong together. A `DropdownMenuLabel` inside it gives the group its name. */
export function DropdownMenuGroup(props: MenuPrimitive.Group.Props) {
return <MenuPrimitive.Group data-slot="dropdown-menu-group" {...props} />;
}
export type DropdownMenuLabelProps = MenuPrimitive.GroupLabel.Props & {
/** Indent the text so it lines up with checkbox and radio items below it. */
inset?: boolean;
};
/** The heading of a group. It must sit inside a `DropdownMenuGroup` or a `DropdownMenuRadioGroup`, which it names. */
export function DropdownMenuLabel({ inset = false, className, ...props }: DropdownMenuLabelProps) {
return <MenuPrimitive.GroupLabel data-slot="dropdown-menu-label" data-inset={inset ? "" : undefined} className={cn(MENU_LABEL, className)} {...props} />;
}
export function DropdownMenuSeparator({ className, ...props }: MenuPrimitive.Separator.Props) {
return <MenuPrimitive.Separator data-slot="dropdown-menu-separator" className={cn(MENU_SEPARATOR, className)} {...props} />;
}
/** The keys that run the item from outside the menu, shown at the end of the row. It is a hint only: the menu does not listen for them. */
export function DropdownMenuShortcut({ className, ...props }: ComponentProps<"span">) {
return <span data-slot="dropdown-menu-shortcut" className={cn(MENU_SHORTCUT, className)} {...props} />;
}
/** A menu inside the menu. `DropdownMenuSubTrigger` is its row in the parent, `DropdownMenuSubContent` its list. */
export function DropdownMenuSub(props: MenuPrimitive.SubmenuRoot.Props) {
return <MenuPrimitive.SubmenuRoot {...props} />;
}
export type DropdownMenuSubTriggerProps = MenuPrimitive.SubmenuTrigger.Props & {
/** Indent the text so it lines up with checkbox and radio items in the same menu. */
inset?: boolean;
};
/** The row that opens a submenu, on hover, on a press, and with the arrow key that points at it. */
export function DropdownMenuSubTrigger({ inset = false, className, children, ...props }: DropdownMenuSubTriggerProps) {
return (
<MenuPrimitive.SubmenuTrigger
data-slot="dropdown-menu-sub-trigger"
data-inset={inset ? "" : undefined}
// While its submenu is open and the highlight has moved into it, the row keeps a quiet fill, so the path stays visible.
className={cn(MENU_ITEM, "data-popup-open:bg-accent data-popup-open:text-accent-foreground", className)}
{...props}
>
{children}
<ChevronRightIcon size={16} className="ms-auto flex-none rtl:rotate-180" />
</MenuPrimitive.SubmenuTrigger>
);
}
/** The list of a submenu. It opens beside its row, with its first item level with the row. */
export function DropdownMenuSubContent({ sideOffset = -4, alignOffset = -9, ...props }: DropdownMenuContentProps) {
return <DropdownMenuContent data-slot="dropdown-menu-sub-content" sideOffset={sideOffset} alignOffset={alignOffset} {...props} />;
}Files and dependencies
- Adds
components/ui/dropdown-menu.tsx- Packages
@base-ui/react@1.9.0- Brings along
- Sound engine
- Icons
checkchevron-right
Changelog
- 1.0.0 · 2026-12-03First release.