When to use
- Shortcuts to actions on things people point at: files in a grid, cards on a board, rows in a table, a canvas.
- Power-user paths in tools where right click is expected (editors, file managers, design tools).
- Touch apps that offer press-and-hold actions on an item.
When not to use
- As the only way to reach an action: it is invisible until someone tries it. Offer the same actions in a visible Dropdown Menu or toolbar too.
- Keyboard-first flows: a context region isn't focusable; use a Dropdown Menu on a focusable button.
- Rich content: use Popover.
Installation
pnpm dlx shadcn add https://eased.dev/r/context-menu.jsonnpx shadcn add https://eased.dev/r/context-menu.jsonyarn dlx shadcn add https://eased.dev/r/context-menu.jsonbunx --bun shadcn add https://eased.dev/r/context-menu.jsonThe command adds Context Menu, everything it builds on and Eased’s tokens to your project.
Install the dependencies
pnpm add @base-ui/reactnpm install @base-ui/reactyarn add @base-ui/reactbun add @base-ui/reactAdd the Eased pieces it builds on
Copy each item’s files the same way, or install them with the CLI:
Copy the source into your project
components/eased/context-menu/context-menu.tsxTSX "use client";import { ContextMenu as BaseContextMenu } from "@base-ui/react/context-menu";import type { ComponentProps } from "react";import { FloatingLayer } from "../_primitives/floating/floating";import { HighlightGroup, menuSurface, OVERLAY_ROLES } from "../_primitives/overlay-core/overlay-core";import { DropdownMenuCheckboxItem, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuLinkItem, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, MenuFlashContext, useMenuFlash, type DropdownMenuContentProps,} from "../dropdown-menu/dropdown-menu";/** * A context menu on Base UI's ContextMenu. It appears at the pointer on right click, or on a * 500ms long press on touch, with no enter animation (rule 84: a right-click is high-frequency) * and its transform-origin at the cursor; it fades out in 100ms. Rows are Dropdown Menu's: * Base UI's ContextMenu re-exports Menu's parts, so every row, the gliding highlight and the * selection blink are shared, not copied. */export type ContextMenuProps = ComponentProps<typeof BaseContextMenu.Root>;/** * Groups a context menu's parts and owns its open state (`open` / `defaultOpen` / `onOpenChange`). * Lets rows blink before it closes; `actionsRef` still reaches Base UI's `close` and `unmount`. */export function ContextMenu({ actionsRef, children, ...props }: ContextMenuProps) { const { ref, flash } = useMenuFlash(actionsRef); return ( <MenuFlashContext value={flash}> <BaseContextMenu.Root {...props} actionsRef={ref}> {children} </BaseContextMenu.Root> </MenuFlashContext> );}export type ContextMenuTriggerProps = Omit<ComponentProps<typeof BaseContextMenu.Trigger>, "className"> & { /** The region's classes. It renders a `<div>`; pass `render` for another element. */ className?: string;};/** * The region that opens the menu on right click and on a 500ms long press on touch (moving more * than 10px cancels it, so scrolling still works). Base UI suppresses the iOS callout; text in * the region is unselectable on coarse pointers so a long press opens the menu instead of a selection. */export function ContextMenuTrigger({ className = "", ...props }: ContextMenuTriggerProps) { return <BaseContextMenu.Trigger {...props} className={`pointer-coarse:select-none ${className}`} />;}/* * Base UI places a context menu just right of and above the pointer (align start, 2px along, * −5px on the side axis) so the pointer lands on the first row. FloatingLayer always passes a * side, which turns off that default, so the same numbers are set here. */const AT_CURSOR = { sideOffset: -5, alignOffset: 2 } as const;export type ContextMenuContentProps = Pick< DropdownMenuContentProps, "finalFocus" | "container" | "zIndex" | "width" | "className" | "children">;/** The menu popup at the pointer: Dropdown Menu's surface and tracks, appearing at once and fading out in 100ms. */export function ContextMenuContent({ finalFocus, container, zIndex, width, className = "", children }: ContextMenuContentProps) { return ( <FloatingLayer parts={BaseContextMenu} motion={OVERLAY_ROLES.context} side={OVERLAY_ROLES.context.side} align="start" sideOffset={AT_CURSOR.sideOffset} alignOffset={AT_CURSOR.alignOffset} container={container} zIndex={zIndex} popupProps={{ finalFocus }} style={width ? { width } : undefined} className={`${menuSurface} ${className}`} > <HighlightGroup>{children}</HighlightGroup> </FloatingLayer> );}/** A command row; blinks, then closes the menu. See `DropdownMenuItem`. */export const ContextMenuItem = DropdownMenuItem;/** A row that navigates. See `DropdownMenuLinkItem`. */export const ContextMenuLinkItem = DropdownMenuLinkItem;/** A toggle row; the menu stays open. See `DropdownMenuCheckboxItem`. */export const ContextMenuCheckboxItem = DropdownMenuCheckboxItem;/** One choice among its radio items. See `DropdownMenuRadioGroup`. */export const ContextMenuRadioGroup = DropdownMenuRadioGroup;/** One option of a radio group. See `DropdownMenuRadioItem`. */export const ContextMenuRadioItem = DropdownMenuRadioItem;/** A nested menu. See `DropdownMenuSub`. */export const ContextMenuSub = DropdownMenuSub;/** The row that opens a submenu. See `DropdownMenuSubTrigger`. */export const ContextMenuSubTrigger = DropdownMenuSubTrigger;/** The submenu popup beside its row. See `DropdownMenuSubContent`. */export const ContextMenuSubContent = DropdownMenuSubContent;/** Related rows under a label. See `DropdownMenuGroup`. */export const ContextMenuGroup = DropdownMenuGroup;/** A group's heading. See `DropdownMenuLabel`. */export const ContextMenuLabel = DropdownMenuLabel;/** A full-bleed rule between groups. See `DropdownMenuSeparator`. */export const ContextMenuSeparator = DropdownMenuSeparator;export type { DropdownMenuCheckboxItemProps as ContextMenuCheckboxItemProps, DropdownMenuGroupProps as ContextMenuGroupProps, DropdownMenuItemProps as ContextMenuItemProps, DropdownMenuLabelProps as ContextMenuLabelProps, DropdownMenuLinkItemProps as ContextMenuLinkItemProps, DropdownMenuRadioGroupProps as ContextMenuRadioGroupProps, DropdownMenuRadioItemProps as ContextMenuRadioItemProps, DropdownMenuSubContentProps as ContextMenuSubContentProps, DropdownMenuSubTriggerProps as ContextMenuSubTriggerProps,} from "../dropdown-menu/dropdown-menu";Keep the folder layout
The files import each other by relative path (
../lib/motion), so keep each one at the path shown, undercomponents/eased/.
Installs Dropdown Menu too: the rows come from it.
Usage
import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuSeparator, ContextMenuTrigger,} from "@/components/eased/context-menu/context-menu";export function FileTile({ file }: { file: AssetFile }) { return ( <ContextMenu> <ContextMenuTrigger className="rounded-control p-1.5 data-popup-open:bg-surface-active"> <Thumbnail file={file} /> <span className="truncate text-ui-sm">{file.name}</span> </ContextMenuTrigger> <ContextMenuContent> <ContextMenuItem icon={<OpenIcon />} shortcut="⌘O" onClick={() => open(file)}> Open </ContextMenuItem> <ContextMenuItem icon={<DuplicateIcon />} shortcut="⌘D" onClick={() => duplicate(file)}> Duplicate </ContextMenuItem> <ContextMenuSeparator /> <ContextMenuItem icon={<TrashIcon />} shortcut="⌘⌫" tone="danger" onClick={() => trash(file)}> Move to Trash </ContextMenuItem> </ContextMenuContent> </ContextMenu> );}Examples
A submenu of toggles
Checkbox rows keep the menu open, so several tags can be set in one visit.
<ContextMenuSub> <ContextMenuSubTrigger icon={<TagIcon />}>Tags</ContextMenuSubTrigger> <ContextMenuSubContent> {tags.map((tag) => ( <ContextMenuCheckboxItem key={tag} checked={file.tags.includes(tag)} onCheckedChange={(on) => setTag(file, tag, on)}> {tag} </ContextMenuCheckboxItem> ))} </ContextMenuSubContent></ContextMenuSub>One menu for many items
One ContextMenu per item is simplest and tells each menu which item it is for. A trigger around the whole grid also works; read the item from the event target in onOpenChange.
API reference
ContextMenu
Base UI ContextMenu.Root: open, defaultOpen, onOpenChange, disabled, loopFocus, actionsRef and the rest. It provides the context rows use to blink before closing; your actionsRef still receives close and unmount.
ContextMenuTrigger
Base UI ContextMenu.Trigger props: a <div> region (render for another element).
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | "" | The region's classes. Style its open state with data-popup-open:. |
ContextMenuContent
| Prop | Type | Default | Description |
|---|---|---|---|
finalFocus | Base UI finalFocus | Where focus goes on close. | |
container | HTMLElement | null | document.body | Portal target. |
zIndex | number | 50 | |
width | string | fits the rows, ≥ 192px | A CSS width. |
className | string | "" | Appended to the popup's classes. |
Rows
ContextMenuItem, ContextMenuLinkItem, ContextMenuCheckboxItem, ContextMenuRadioGroup, ContextMenuRadioItem, ContextMenuSub, ContextMenuSubTrigger, ContextMenuSubContent, ContextMenuGroup, ContextMenuLabel and ContextMenuSeparator are Dropdown Menu's parts under these names (Base UI's ContextMenu re-exports Menu's parts). Their props (icon, shortcut, description, tone, closeOnClick, …) are in Dropdown Menu.
Keyboard
| Key | Action |
|---|---|
ArrowUp / ArrowDown | Moves the highlight; loops at the ends. |
| A letter | Jumps to the next row starting with it. |
Enter / Space | Chooses the row. |
ArrowRight / ArrowLeft | Opens a submenu / closes it. |
Escape | Closes the menu. |
The region itself is opened by pointer (right click) or touch (long press); make the same actions reachable by keyboard elsewhere.
Accessibility
- The popup is a
menuwithmenuitem,menuitemcheckboxandmenuitemradiorows; focus moves into it on open. - Shortcuts are read by name; icons and checks are hidden from assistive tech.
- On touch, a 500ms press and hold opens it at the finger; moving more than 10px first cancels it, so the region still scrolls. Base UI suppresses the iOS callout, and the region's text is unselectable on coarse pointers so the press doesn't start a selection.
- Rows are 32px, 40px on coarse pointers.
Motion
None on the way in (rule 84: right click is a high-frequency action, and the menu should already be there when the button comes up): it appears with its corner just beside the pointer, level with the first row, and its transform-origin at the pointer. It fades out in 100ms. Inside, everything is Dropdown Menu's: one highlight gliding between rows on the snappy spring, a 40ms-off, 40ms-on blink on the chosen command before it closes, and submenus that slide 4px out beside their row. Reduced motion keeps the 100ms fade, jumps the highlight and closes without the blink.
Notes for AI
Use ContextMenu to add right-click and long-press access to actions that already exist in a visible menu or toolbar; wrap each item (file tile, card, row) in its own ContextMenu so the handlers close over that item. Give the region a data-popup-open: fill so people see which item the menu belongs to.
Don't listen for contextmenu or touch timers yourself, and don't position the popup: Base UI places it at the pointer and handles long press. Don't add an enter animation; it is deliberately instant.
Related components
- Animated Dropdown Menu: the same rows, opened from a button.
- Animated Popover: an anchored panel for content.
- Sheet and Side Drawer