Skip to content
Thrum

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.json

Adds 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

PropTypeDefaultDescription
ContextMenu: openboolean–Whether the menu is open, when the parent holds the state. Use it with onOpenChange.
ContextMenu: defaultOpenbooleanDefault falseWhether 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: loopFocusbooleanDefault trueWhether the arrow keys wrap from the last item to the first.
ContextMenu: disabledbooleanDefault falseThe area gives the browser's own menu back and opens nothing.
ContextMenu: soundbooleanDefault truePlay the menu's sounds, if the visitor has sound on.
ContextMenuTriggerdiv 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: classNamestring–The area's size and look. A focus ring is built in.
ContextMenuContent: classNamestring–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: childrenReactNode–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: destructivebooleanDefault falseRed text, and a red fill while highlighted, for an action that deletes or cannot be undone.
ContextMenuItem: insetbooleanDefault falseIndent the text by 24 pixels so it lines up with checkbox and radio items. ContextMenuLabel and ContextMenuSubTrigger take it too.
ContextMenuItem: disabledbooleanDefault falseDimmed, and it cannot be picked. Every item part takes it.
ContextMenuItem: closeOnClickbooleanDefault trueWhether picking the item closes the menu. Checkbox and radio items default to false.
ContextMenuCheckboxItem: checkedboolean–Whether it is ticked, when the parent holds the state. Use it with onCheckedChange.
ContextMenuCheckboxItem: defaultCheckedbooleanDefault falseWhether 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: valuestring–The value of the selected item, when the parent holds it. Use it with onValueChange.
ContextMenuRadioGroup: defaultValuestring–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: valuestringRequiredWhat the group's onValueChange reports. Unique within the group.
ContextMenuGroup, ContextMenuLabel, ContextMenuSeparatordiv props–A group of items, its heading, and a line between groups. A label must sit inside a group or a radio group.
ContextMenuShortcutspan props–Key hint text at the end of a row. The menu does not listen for the keys.
ContextMenuSub, ContextMenuSubTrigger, ContextMenuSubContentas 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.
KeysAction
Menu key / Shift F10With 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 / EndMove the highlight to the first or last item.
A to ZMove the highlight to the next item that starts with the typed letters.
Enter / SpacePick 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.
EscClose the innermost open menu. Focus returns to where it was in the area.

Source

components/ui/context-menu.tsx
"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. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor