Context menu
The menu of an area: it opens at the pointer on a right-click, at the finger on a long press, and from the keyboard with the menu key or Shift+F10. Use it for shortcuts to actions that are also reachable some other way, because nothing on screen says it is there. For a menu behind a visible button, use the dropdown menu.
Install
npx shadcn@latest add https://thrumui.dev/r/context-menu.jsonAdds components/ui/context-menu.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.
import { ContextMenu } from "@/components/ui/context-menu";Props
| Prop | Type | Default | Description |
|---|---|---|---|
ContextMenu: open | boolean | – | Whether the menu is open, when the parent holds the state. Use it with onOpenChange. |
ContextMenu: defaultOpen | boolean | Default false | Whether it starts open, when the menu holds its own state. |
ContextMenu: onOpenChange | (open: boolean, details) => void | – | Called when the menu opens or closes. details.reason says why. |
ContextMenu: loopFocus | boolean | Default true | Whether the arrow keys wrap from the last item to the first. |
ContextMenu: disabled | boolean | Default false | The area gives the browser's own menu back and opens nothing. |
ContextMenu: sound | boolean | Default true | Play the menu's sounds, if the visitor has sound on. |
ContextMenuTrigger | div props | – | The area. Give it tabIndex={0}, a role and a name when nothing inside it can take the focus, so the keyboard can open the menu. |
ContextMenuTrigger: className | string | – | The area's size and look. A focus ring is built in. |
ContextMenuContent: className | string | – | Extra classes for the list. It is at least 160 pixels wide and grows with its longest row. |
ContextMenuItem: onClick | (event) => void | – | Runs when the item is picked, by pointer or by Enter or Space. |
ContextMenuItem: 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. ContextMenuSubTrigger takes one too. |
ContextMenuItem: destructive | boolean | Default false | Red text, and a red fill while highlighted, for an action that deletes or cannot be undone. |
ContextMenuItem: inset | boolean | Default false | Indent the text by 24 pixels so it lines up with checkbox and radio items. ContextMenuLabel and ContextMenuSubTrigger take it too. |
ContextMenuItem: disabled | boolean | Default false | Dimmed, and it cannot be picked. Every item part takes it. |
ContextMenuItem: closeOnClick | boolean | Default true | Whether picking the item closes the menu. Checkbox and radio items default to false. |
ContextMenuCheckboxItem: checked | boolean | – | Whether it is ticked, when the parent holds the state. Use it with onCheckedChange. |
ContextMenuCheckboxItem: 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. |
ContextMenuCheckboxItem: onCheckedChange | (checked: boolean) => void | – | Called with the new state when the item is picked. |
ContextMenuRadioGroup: value | string | – | The value of the selected item, when the parent holds it. Use it with onValueChange. |
ContextMenuRadioGroup: defaultValue | string | – | The value selected at first, when the group holds it itself. |
ContextMenuRadioGroup: onValueChange | (value: string) => void | – | Called with the new value when another radio item is picked. |
ContextMenuRadioItem: value | string | Required | What the group's onValueChange reports. Unique within the group. |
ContextMenuGroup, ContextMenuLabel, ContextMenuSeparator | div props | – | A group of items, its heading, and a line between groups. A label must sit inside a group or a radio group. |
ContextMenuShortcut | span props | – | Key hint text at the end of a row. The menu does not listen for the keys. |
ContextMenuSub, ContextMenuSubTrigger, ContextMenuSubContent | as the dropdown menu's | – | A submenu: its state, its row in the parent, and its list. |
Motion and sound
Motion
- The list fades in and grows from 96% to full size over 150 ms, from the pointer.
- Closing plays the same change backwards.
- 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.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- The list follows the WAI-ARIA menu pattern: role menu, with rows that are menuitem, menuitemcheckbox or menuitemradio.
- A context menu is invisible until it is opened. Keep every action in it reachable another way, and say in the interface that the area has a menu if it matters.
- The keyboard opens it through the browser's own context-menu event, so the area, or something inside it, must be focusable. The demo's area has tabIndex 0, the group role and a name.
- On a touch screen a press of 500 ms opens the menu. Moving the finger more than 10 pixels cancels it, so scrolling still works.
- While the menu is open, a second right-click inside the area moves it to the new place.
- 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 |
|---|---|
| Menu key / Shift F10 | With the focus on or inside the area: open the menu. |
| ↓ / ↑ | 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. |
| → / ← | Open the submenu of the highlighted row, and close it again. In right-to-left text the keys swap. |
| Esc | Close the innermost open menu. Focus returns to where it was in the area. |
Source
"use client";
import type { ComponentProps } from "react";
import { ContextMenu as ContextMenuPrimitive } from "@base-ui/react/context-menu";
import { cn } from "@/lib/utils";
import {
DropdownMenuCheckboxItem,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuSeparator,
DropdownMenuShortcut,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
MENU_CONTENT,
MenuSoundContext,
playMenuOpen,
} from "@/components/ui/dropdown-menu";
export type ContextMenuProps = ContextMenuPrimitive.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;
};
/**
* The menu of an area: it opens where the pointer is on a right-click, and on a long press on a touch screen.
* `ContextMenuTrigger` is the area, `ContextMenuContent` the list. Its rows are the dropdown menu's rows under their own names.
*/
export function ContextMenu({ sound = true, onOpenChange, ...props }: ContextMenuProps) {
return (
<MenuSoundContext.Provider value={sound}>
<ContextMenuPrimitive.Root
onOpenChange={(open, details) => {
if (sound) playMenuOpen(open, details.reason);
onOpenChange?.(open, details);
}}
{...props}
/>
</MenuSoundContext.Provider>
);
}
/**
* The area the menu belongs to. A right-click or a 500 ms press anywhere inside it opens the menu.
* For the keyboard it must hold the focus or something that can: give it `tabIndex={0}` when nothing inside it is focusable.
* The menu key and Shift+F10 then open the menu.
*/
export function ContextMenuTrigger({ className, ...props }: ContextMenuPrimitive.Trigger.Props) {
return (
<ContextMenuPrimitive.Trigger
data-slot="context-menu-trigger"
className={cn("focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring", className)}
{...props}
/>
);
}
/**
* The list. It opens at the pointer 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 ContextMenuContent({ className, ...props }: ContextMenuPrimitive.Popup.Props) {
return (
<ContextMenuPrimitive.Portal>
<ContextMenuPrimitive.Positioner className="z-50 outline-none">
<ContextMenuPrimitive.Popup data-slot="context-menu" data-surface="floating" className={cn(MENU_CONTENT, className)} {...props} />
</ContextMenuPrimitive.Positioner>
</ContextMenuPrimitive.Portal>
);
}
/** One action. Picking it runs `onClick` and closes the menu. `destructive` turns it red, `inset` lines it up with checkbox and radio items. */
export function ContextMenuItem(props: ComponentProps<typeof DropdownMenuItem>) {
return <DropdownMenuItem data-slot="context-menu-item" {...props} />;
}
/** A setting that is on or off. Picking it flips it and leaves the menu open. */
export function ContextMenuCheckboxItem(props: ComponentProps<typeof DropdownMenuCheckboxItem>) {
return <DropdownMenuCheckboxItem data-slot="context-menu-checkbox-item" {...props} />;
}
/** A set of radio items of which one is selected. */
export function ContextMenuRadioGroup(props: ComponentProps<typeof DropdownMenuRadioGroup>) {
return <DropdownMenuRadioGroup data-slot="context-menu-radio-group" {...props} />;
}
/** One choice of a radio group. Picking it selects it and leaves the menu open. */
export function ContextMenuRadioItem(props: ComponentProps<typeof DropdownMenuRadioItem>) {
return <DropdownMenuRadioItem data-slot="context-menu-radio-item" {...props} />;
}
/** Items that belong together. A `ContextMenuLabel` inside it gives the group its name. */
export function ContextMenuGroup(props: ComponentProps<typeof DropdownMenuGroup>) {
return <DropdownMenuGroup data-slot="context-menu-group" {...props} />;
}
/** The heading of a group. It must sit inside a `ContextMenuGroup` or a `ContextMenuRadioGroup`, which it names. */
export function ContextMenuLabel(props: ComponentProps<typeof DropdownMenuLabel>) {
return <DropdownMenuLabel data-slot="context-menu-label" {...props} />;
}
export function ContextMenuSeparator(props: ComponentProps<typeof DropdownMenuSeparator>) {
return <DropdownMenuSeparator data-slot="context-menu-separator" {...props} />;
}
/** Key hint text at the end of a row. The menu does not listen for the keys. */
export function ContextMenuShortcut(props: ComponentProps<typeof DropdownMenuShortcut>) {
return <DropdownMenuShortcut data-slot="context-menu-shortcut" {...props} />;
}
/** A menu inside the menu. `ContextMenuSubTrigger` is its row in the parent, `ContextMenuSubContent` its list. */
export function ContextMenuSub(props: ComponentProps<typeof DropdownMenuSub>) {
return <DropdownMenuSub {...props} />;
}
export function ContextMenuSubTrigger(props: ComponentProps<typeof DropdownMenuSubTrigger>) {
return <DropdownMenuSubTrigger data-slot="context-menu-sub-trigger" {...props} />;
}
export function ContextMenuSubContent(props: ComponentProps<typeof DropdownMenuSubContent>) {
return <DropdownMenuSubContent data-slot="context-menu-sub-content" {...props} />;
}Files and dependencies
- Adds
components/ui/context-menu.tsx- Packages
@base-ui/react@1.9.0- Brings along
- Sound engineDropdown menu
Changelog
- 1.0.0 · 2026-12-03First release.