Context Menu
Right-click menus built on the Dropdown Menu parts.
Built on Dropdown Menu.
Installation
npx shadcn@latest add https://neonkit.heyaadi.com/r/context-menu.jsonUsage
import {
ContextMenu,
ContextMenuContent,
ContextMenuItem,
ContextMenuTrigger,
} from "@/components/ui/context-menu"
<ContextMenu>
<ContextMenuTrigger className="h-40 rounded-xl border border-white/10">
Right-click here
</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuItem shortcut="⌘D">Duplicate</ContextMenuItem>
<ContextMenuItem variant="danger">Move to trash</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>Examples
Canvas
One menu for a whole area, with a radio submenu and checkbox items that drive the canvas.
Messages
Long-press a message on touch, or focus one and press Shift+F10.
API reference
ContextMenu
Holds the state and the pointer position. Renders no element of its own. Built on DropdownMenu with anchorPoint.
| Prop | Type | Default |
|---|---|---|
onOpenChange | (open: boolean) => voidCalled when the menu opens or closes. | None |
color | NeonColorPalette name or hex. Without it the menu takes the trigger area's color, then --neon-accent, then cyan. | inherited |
ContextMenuTrigger
The area that answers right-click. Renders a div and accepts its native attributes.
| Prop | Type | Default |
|---|---|---|
asChild | booleanUses the single child as the area instead of rendering a div. | false |
disabled | booleanLets the browser's own context menu through. | false |
ContextMenuContent
The same panel as DropdownMenuContent, opened at the pointer.
| Prop | Type | Default |
|---|---|---|
side | "top"Which side of the pointer to open on. Flips near the viewport edge. | "right" |
align | "start"Alignment against the pointer. | "start" |
sideOffset | numberGap from the pointer in px, so the item under the cursor isn't hovered on open. | 2 |
loop | booleanArrow keys wrap around. | true |
Items
ContextMenuItem, ContextMenuCheckboxItem, ContextMenuRadioGroup, ContextMenuRadioItem, ContextMenuLabel, ContextMenuGroup, ContextMenuSeparator, ContextMenuSub, ContextMenuSubTrigger and ContextMenuSubContent are the Dropdown Menu parts under new names, with the same props.
| Prop | Type | Default |
|---|---|---|
icon | ReactNodeLeading icon on items and sub triggers. | None |
shortcut | ReactNodeRight-aligned hint. | None |
variant | "default"danger turns the item red. | "default" |
onSelect | (event: Event) => voidCalled on select. The menu closes unless you call event.preventDefault(). | None |
Accessibility
- Opens at the pointer on right-click, after a 500ms long-press on touch (moving more than 10px cancels it), and from the keyboard with Shift+F10 or the ContextMenu key at the area's top-left corner.
- The browser's own context menu is suppressed inside the trigger and the menu. Set disabled to give it back.
- For Shift+F10 to reach the area, it or something inside it must be focusable: give the area tabIndex={0} or use focusable children.
- The keyboard model is the Dropdown Menu's: arrows, Home and End, typeahead, Enter or Space to select, ArrowRight and ArrowLeft for submenus, Escape and Tab to close.
- Focus returns to whatever had it before the menu opened. The menu has no trigger to name it, so pass aria-label to ContextMenuContent.