Dropdown menu
A menu of actions with checkbox and radio items and full keyboard support, built on Radix.
Use DropdownMenu for a list of actions opened from a button: "Copy reference", "Download receipt", "Report a problem". It is for actions. For choosing a value in a form use Select, and for moving between pages use links or a navigation.
Installation
pnpm dlx shadcn@latest add https://components.abdulqadirmsingi.com/r/dropdown-menu.json
This adds one dependency, @radix-ui/react-dropdown-menu, which the command installs for you.
Usage
import {
DropdownMenu, DropdownMenuContent, DropdownMenuItem,
DropdownMenuSeparator, DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
<DropdownMenu>
<DropdownMenuTrigger>Actions</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem onSelect={copy}>Copy reference</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem destructive onSelect={report}>Report a problem</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>Use onSelect, not onClick, on items: it also fires for keyboard selection.
Checkbox and radio items
Props
DropdownMenu takes open, defaultOpen, onOpenChange and modal. Content takes side, align and sideOffset.
| Prop | Type | Default | Description |
|---|---|---|---|
| modal | boolean | false | On DropdownMenu. When true the rest of the page is locked and hidden from assistive technology while the menu is open. See the note below. |
| destructive | boolean | – | On DropdownMenuItem. Danger styling for actions that delete or can't be undone. |
| disabled | boolean | – | On items. Skipped by the keyboard and not selectable. |
| onSelect | (event: Event) => void | – | On items. Called when chosen. Call event.preventDefault() to keep the menu open. |
| checked / onCheckedChange | boolean / (checked: boolean) => void | – | On DropdownMenuCheckboxItem. |
| value / onValueChange | string / (value: string) => void | – | On DropdownMenuRadioGroup. Each DropdownMenuRadioItem has its own value. |
Also exported: DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuGroup and DropdownMenuShortcut.
Accessibility
- After a mouse click, focus returns to the button that opened it without drawing a focus ring (no stray border); after a key press the ring shows as normal. This comes from the small
focus-modalityhelper, which installs with this component. - The trigger is a button with
aria-haspopup="menu"andaria-expanded. Enter, Space or Arrow Down open it. - Inside: Arrow keys move, Home and End jump to the first and last item, typing a letter jumps to a matching item, Enter or Space chooses, and Escape closes and returns focus to the trigger. Disabled items are skipped.
- Items have
role="menuitem", checkboxesmenuitemcheckboxand radiosmenuitemradio, with their checked state announced. The check mark and dot are decorative. - Each item is at least 36 pixels tall. If the screen is too short for the whole menu, it scrolls inside itself instead of running off the screen.
- Shortcut text is only a hint. It does not bind any keys, so bind them yourself.
The menu is not modal by default, following the WAI-ARIA menu button pattern. Radix's modal mode hides the rest of the page from screen readers while its links and buttons stay focusable, which accessibility checkers (axe's aria-hidden-focus rule) report as an error. Without it the menu still closes on Escape or an outside click. Set modal if you need the page locked.
Tokens used
--background, --foreground, --muted, --muted-foreground, --border, --danger, --duration-fast, --ease-out, --z-dropdown.
Known limitations
- No submenus yet.
- It does not animate out.
- A menu is for short lists. For many options, or to search, a combobox is the better fit (not built yet).
Source
"use client";import * as MenuPrimitive from "@radix-ui/react-dropdown-menu";import type { ComponentProps } from "react";import { useQuietFocusRestore } from "@/lib/focus-modality";import { cn } from "@/lib/utils";/** Compose with DropdownMenuTrigger and DropdownMenuContent, then items inside the content. *//** * Non-modal by default, as in the WAI-ARIA menu button pattern. Radix's modal mode marks the rest * of the page `aria-hidden` while leaving its links and buttons focusable, which axe reports as * `aria-hidden-focus`. The menu still closes on Escape or an outside click. * Pass `modal` to get the locked-page behaviour back. */export function DropdownMenu({ modal = false, ...props}: ComponentProps<typeof MenuPrimitive.Root>) { return <MenuPrimitive.Root modal={modal} {...props} />;}export const DropdownMenuTrigger = MenuPrimitive.Trigger;export const DropdownMenuGroup = MenuPrimitive.Group;export const DropdownMenuRadioGroup = MenuPrimitive.RadioGroup;/** * A menu of actions opened from a button. Radix provides the keyboard model: Arrow keys move, * Home and End jump, typing a letter jumps to a matching item, Enter or Space chooses, Escape * closes and returns focus to the trigger. Use it for actions, not for navigation between pages. */export function DropdownMenuContent({ className, sideOffset = 6, collisionPadding = 8, children, onCloseAutoFocus, ...props}: ComponentProps<typeof MenuPrimitive.Content>) { const quiet = useQuietFocusRestore(onCloseAutoFocus); return ( <MenuPrimitive.Portal> <MenuPrimitive.Content sideOffset={sideOffset} collisionPadding={collisionPadding} className={cn( "z-[var(--z-dropdown)] min-w-44 max-w-[calc(100vw-1rem)] max-h-[var(--radix-dropdown-menu-content-available-height)] overflow-y-auto rounded-xl border border-border bg-background p-1 text-sm text-foreground shadow-lg transition-[opacity,transform,translate,scale,rotate] duration-[var(--duration-fast)] ease-[var(--ease-out)] starting:scale-95 starting:opacity-0", className, )} onCloseAutoFocus={quiet.onCloseAutoFocus} {...props} > <span hidden ref={quiet.probe} /> {children} </MenuPrimitive.Content> </MenuPrimitive.Portal> );}const itemBase = "relative flex min-h-9 cursor-pointer select-none data-[disabled]:cursor-not-allowed items-center gap-2 rounded-md px-2.5 py-1.5 outline-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50 data-[highlighted]:bg-muted [&_svg]:size-4 [&_svg]:shrink-0";export interface DropdownMenuItemProps extends ComponentProps<typeof MenuPrimitive.Item> { /** Danger styling for actions that delete or cannot be undone. */ destructive?: boolean;}export function DropdownMenuItem({ className, destructive, ...props }: DropdownMenuItemProps) { return ( <MenuPrimitive.Item className={cn(itemBase, destructive && "text-danger", className)} {...props} /> );}const tick = ( <svg aria-hidden="true" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" > <path d="M3.5 8.5l3 3 6-7" /> </svg>);const dot = ( <svg aria-hidden="true" viewBox="0 0 16 16" fill="currentColor"> <circle cx="8" cy="8" r="3" /> </svg>);/** A toggle item. The check mark is hidden from screen readers; the checked state is announced. */export function DropdownMenuCheckboxItem({ className, children, ...props}: ComponentProps<typeof MenuPrimitive.CheckboxItem>) { return ( <MenuPrimitive.CheckboxItem className={cn(itemBase, "ps-8", className)} {...props}> <span className="absolute start-2 inline-flex size-4 items-center justify-center"> <MenuPrimitive.ItemIndicator>{tick}</MenuPrimitive.ItemIndicator> </span> {children} </MenuPrimitive.CheckboxItem> );}/** One choice in a DropdownMenuRadioGroup. */export function DropdownMenuRadioItem({ className, children, ...props}: ComponentProps<typeof MenuPrimitive.RadioItem>) { return ( <MenuPrimitive.RadioItem className={cn(itemBase, "ps-8", className)} {...props}> <span className="absolute start-2 inline-flex size-4 items-center justify-center"> <MenuPrimitive.ItemIndicator>{dot}</MenuPrimitive.ItemIndicator> </span> {children} </MenuPrimitive.RadioItem> );}/** A non-interactive heading for a group of items. */export function DropdownMenuLabel({ className, ...props}: ComponentProps<typeof MenuPrimitive.Label>) { return ( <MenuPrimitive.Label className={cn("px-2.5 py-1.5 text-xs font-medium text-muted-foreground", className)} {...props} /> );}export function DropdownMenuSeparator({ className, ...props}: ComponentProps<typeof MenuPrimitive.Separator>) { return ( <MenuPrimitive.Separator className={cn("-mx-1 my-1 h-px bg-border", className)} {...props} /> );}/** A shortcut hint such as "Ctrl K" on the end of an item. It is text only; it binds no keys. */export function DropdownMenuShortcut({ className, ...props }: ComponentProps<"span">) { return <span className={cn("ms-auto text-xs text-muted-foreground", className)} {...props} />;}