Context Menu
Show actions for an element on right-click or long press.
Installation
pnpm dlx shadcn@4.18.0 add @siteplane/context-menuComplete the one-time setup first. Registry JSON lists source files and dependencies.
For this page’s examples, also install:
pnpm dlx shadcn@4.18.0 add @siteplane/cardUsage
import { PencilIcon } from "lucide-react";
import {
ContextMenu,
ContextMenuTrigger,
ContextMenuPopup,
ContextMenuItem,
} from "@/siteplane/components/context-menu";
<ContextMenu>
<ContextMenuTrigger
className="flex h-28 w-full items-center justify-center rounded-lg border border-dashed"
tabIndex={0}
>
Right-click this area
</ContextMenuTrigger>
<ContextMenuPopup>
<ContextMenuItem>
<PencilIcon />
Edit
</ContextMenuItem>
</ContextMenuPopup>
</ContextMenu>;Composition
ContextMenu
├── ContextMenuTrigger
└── ContextMenuPopup
├── ContextMenuGroup → ContextMenuGroupLabel + items
├── ContextMenuItem / CheckboxItem / RadioGroup
├── ContextMenuSeparator
└── ContextMenuSub → SubTrigger + ContextMenuSubPopupGroups and submenus
Label related actions with ContextMenuGroup and ContextMenuGroupLabel, and move secondary actions into a submenu.
Checkbox and radio items
Use checkbox items for view toggles and a radio group for a single choice, each under a group label. variant="switch" only changes the indicator. The check draws in and out like the Checkbox check and appears instantly under reduced motion. A radio item shows its check in the leading slot; for a row that leads with a logo or an avatar, indicatorPosition="end" moves the check to the end of the row, as on Menu.
Link items
Use ContextMenuLinkItem for navigation; it renders a real link and closes the menu after activation.
Glass
Set variant="glass" on ContextMenuPopup when the menu opens over a photo, a video or a color surface, and on a ContextMenuSubPopup so its submenu matches. Rows, icons and their states stay the same. Glass shows every component with a glass variant.
The gradient only makes the glass visible. It is not part of the component.
API reference
ContextMenu, ContextMenuPortal and the group, radio and submenu parts follow Base UI Context Menu. Popup includes its Portal, Positioner and a scrolling viewport; do not add another Portal around it. LinkItem renders a native link. Shortcut renders kbd and does not register a shortcut. SubPopup opens on the inline-end side. ContextMenuPrimitive exports the Base UI namespace. Styled className props are strings.
| Prop | Type | Default | Description |
|---|---|---|---|
Context | Positioner side | bottom | Preferred edge. |
Context | start | center | end | start | Alignment. |
Context | Positioner offset | 0 | Gap. |
Context | Positioner offset | unset | Alignment offset. |
Context | auto | trigger | content | at-least-trigger | auto | trigger matches the width of the ContextMenuTrigger area; at-least-trigger is at least that wide. With a custom anchor, both use the anchor's width. Every width but trigger is at most 20rem, or the trigger area's width for at-least-trigger when it is wider, and stays within the available space; a longer row label ends in an ellipsis and a group label wraps. |
Context | Positioner anchor | pointer position | Custom anchor. |
Context | Portal.Props | unset | Portal options. |
Context | default | glass | default | glass shows a tinted, blurred view of what lies behind the popup. |
Context | default | destructive | default | Action tone. A destructive item colors its label and its icon red. |
Item / Group | boolean | unset (off) | Reserve the icon slot so the label lines up with icon rows. |
Context | default | switch | default | Check or switch indicator; retains menuitemcheckbox semantics. |
Context | start | end | start | Where the check shows: in the leading slot, or at the end of an identity row that leads with a logo or an avatar. |
Context | sm | md | lg | xl | lg | Switch size. Menu rows stay compact at every control size, so the switch does not follow the provider. |
Context | alignment / offset | start / 0 | Submenu positioning. |
Context | offset | -5 unless align=center | Submenu alignment correction. |
Context | default | glass | default | Set glass to match a glass parent menu. |
open / default | boolean | false | Visibility. |
on | (open, eventDetails) => void | unset | Visibility callback. |
Item. | MouseEventHandler | unset | Application action. |
Checkbox | boolean / callback | uncontrolled / unset | Controlled checked state. |
Radio | string / callback | uncontrolled / unset | Controlled choice. |
Context | boolean | true | Closes the menu after activating a link. |
Context | tone / boolean | default / unset | Link-row presentation. |
Modal, disabled, focus, dismissal and close-on-click behavior follow Base UI. A disabled item cannot be activated; the submenu trigger supplies the chevron. SubPopup accepts width plus the listed offsets, not arbitrary portal or anchor options.
Usage notes
Start every action item with a fitting lucide icon, such as PencilIcon for Edit or Trash2Icon for Delete; checkbox and radio items show their own indicator instead. Provide the same actions through a visible button menu for users who do not discover right-click. Context Menu is not a replacement for all primary actions. Arrow keys move through the items, including disabled ones, which cannot be activated. Enter activates an item, and Escape closes the menu with focus restoration. Use real links for navigation. Selection here is menu state, not native form submission.