Context menu
A right-click menu on Base UI ContextMenu that reuses the dropdown menu rows.
A shortcut to actions that are also reachable another way, for example in a @khaeli/dropdown-menu on the same row: nothing on screen says a context menu is there, and macOS has no key that opens one. The rows are the dropdown menu's own.
Default
Invoice 1042Right-click, long-press or press the Menu key
import { ArchiveIcon, BinIcon, CopyIcon, PenLineIcon } from "@keyline-icons/react"
import {
ContextMenu,
ContextMenuContent,
ContextMenuItem,
ContextMenuSeparator,
ContextMenuShortcut,
ContextMenuTrigger,
} from "@/components/ui/context-menu"
export default function ContextMenuDefault() {
return (
<ContextMenu>
<ContextMenuTrigger
tabIndex={0}
role="group"
aria-label="Invoice 1042"
className="flex h-24 w-full max-w-72 flex-col items-center justify-center gap-1 rounded-xl border border-dashed text-center focus-ring"
>
<span className="font-medium">Invoice 1042</span>
<span className="text-muted-foreground">Right-click, long-press or press the Menu key</span>
</ContextMenuTrigger>
<ContextMenuContent aria-label="Invoice 1042">
<ContextMenuItem>
<PenLineIcon />
Rename
<ContextMenuShortcut>F2</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem>
<CopyIcon />
Duplicate
<ContextMenuShortcut>⌘D</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem>
<ArchiveIcon />
Archive
</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem variant="destructive">
<BinIcon />
Delete
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
)
}
Checkbox radio and submenu
Review the pricing page copyNormal priority
"use client"
import { useState } from "react"
import {
ContextMenu,
ContextMenuCheckboxItem,
ContextMenuContent,
ContextMenuGroup,
ContextMenuItem,
ContextMenuLabel,
ContextMenuRadioGroup,
ContextMenuRadioItem,
ContextMenuSeparator,
ContextMenuSub,
ContextMenuSubContent,
ContextMenuSubTrigger,
ContextMenuTrigger,
} from "@/components/ui/context-menu"
export default function ContextMenuCheckboxRadioAndSubmenu() {
const [pinned, setPinned] = useState(false)
const [priority, setPriority] = useState("Normal")
return (
<ContextMenu>
<ContextMenuTrigger
tabIndex={0}
role="group"
aria-label="Task: review the pricing page copy"
className="flex h-24 w-full max-w-72 flex-col items-center justify-center gap-1 rounded-xl border border-dashed text-center focus-ring"
>
<span className="font-medium">Review the pricing page copy</span>
<span className="text-muted-foreground">
{priority} priority{pinned ? ", pinned" : ""}
</span>
</ContextMenuTrigger>
<ContextMenuContent aria-label="Task">
<ContextMenuCheckboxItem checked={pinned} onCheckedChange={setPinned}>
Pinned
</ContextMenuCheckboxItem>
<ContextMenuSeparator />
<ContextMenuGroup>
<ContextMenuLabel>Priority</ContextMenuLabel>
<ContextMenuRadioGroup value={priority} onValueChange={setPriority}>
<ContextMenuRadioItem value="Low">Low</ContextMenuRadioItem>
<ContextMenuRadioItem value="Normal">Normal</ContextMenuRadioItem>
<ContextMenuRadioItem value="High">High</ContextMenuRadioItem>
</ContextMenuRadioGroup>
</ContextMenuGroup>
<ContextMenuSeparator />
<ContextMenuSub>
<ContextMenuSubTrigger>Move to</ContextMenuSubTrigger>
<ContextMenuSubContent>
<ContextMenuItem>Website refresh</ContextMenuItem>
<ContextMenuItem>Billing migration</ContextMenuItem>
</ContextMenuSubContent>
</ContextMenuSub>
</ContextMenuContent>
</ContextMenu>
)
}
Installation
npx shadcn@latest add @khaeli/context-menubunx --bun shadcn@latest add @khaeli/context-menuInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
Keyboard
Menu keyShift F10- The browser, not the item, turns these into the
contextmenuevent on the element that has focus, so the trigger must take focus: give ittabIndex={0}, or put a control inside it. In Chromium on macOS the Menu key opened the menu over the middle of the trigger and Shift F10 did nothing, as macOS has no such shortcut. Arrow DownArrow Up- Focus starts on the menu. Arrow Down moves to the first row; after that the keys are the dropdown menu's.
EnterSpace- Run the row and close the menu. On a checkbox or radio row they change the value and the menu stays open.
Escape- Closes the menu and returns focus to the trigger.
Accessible name
- The menu has no button to take a name from, so it has none until you give
ContextMenuContentanaria-label. ContextMenuTriggerrenders adiv. As a focus stop it needs a role and a name of its own: the examples userole="group"witharia-label.- On a touch screen a long press opens the menu.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/context-menu.json