Unified UI

Context Menu

A right-click context menu with sub-menus, checkbox items, radio groups, and keyboard shortcuts. Built on Radix UI.

Overview

ContextMenu is a right-click (or long-press) triggered menu built on Radix UI's ContextMenu primitive. It supports nested submenus, checkbox items, radio groups, keyboard shortcuts, and full keyboard navigation.


Installation

Install the component via the CLI in one command.

npx @work-rjkashyap/unified-ui add context-menu
pnpm dlx @work-rjkashyap/unified-ui add context-menu
npx @work-rjkashyap/unified-ui add context-menu
bunx @work-rjkashyap/unified-ui add context-menu

If you haven't initialized your project yet, run npx @work-rjkashyap/unified-ui init first. See the CLI docs for details.

Or install the full package

Use this approach if you prefer to install the entire design system as a dependency instead of copying individual components.

npm install @work-rjkashyap/unified-ui
pnpm add @work-rjkashyap/unified-ui
yarn add @work-rjkashyap/unified-ui
bun add @work-rjkashyap/unified-ui

Anatomy

import {
	ContextMenu,
	ContextMenuTrigger,
	ContextMenuContent,
	ContextMenuItem,
	ContextMenuSeparator,
	ContextMenuLabel,
	ContextMenuShortcut,
	ContextMenuSub,
	ContextMenuSubTrigger,
	ContextMenuSubContent,
	ContextMenuCheckboxItem,
	ContextMenuRadioGroup,
	ContextMenuRadioItem,
	ContextMenuGroup,
} from "@work-rjkashyap/unified-ui";

Examples

Basic

Right-click here

With Labels and Groups

Right-click here

With Submenu

Right-click here

Checkbox Items

Right-click here

Radio Items

Right-click here

Danger Variant

Right-click here

Props

ContextMenu

PropTypeDefaultDescription
openboolean—Controlled open state.
onOpenChange(open: boolean) => void—Callback when open state changes.
modalbooleantrueWhether the menu is modal.
dir"ltr" | "rtl"—Reading direction for submenus.
childrenReactNode—Required. Must include Trigger and Content.

ContextMenuTrigger

PropTypeDefaultDescription
asChildbooleanfalseRender as the child element instead of a <span>.
disabledbooleanfalseDisables the right-click trigger.
classNamestring—Additional CSS classes.

ContextMenuContent

PropTypeDefaultDescription
loopbooleanfalseWhether keyboard navigation loops.
alignOffsetnumber0Offset from the alignment edge.
collisionPaddingnumber8Padding from viewport edges.
classNamestring—Additional CSS classes.

ContextMenuItem

PropTypeDefaultDescription
variant"default" | "danger""default"Visual variant of the item.
iconReactNode—Icon rendered before the label.
disabledbooleanfalseDisables the item.
onSelect(e: Event) => void—Callback when the item is selected.
classNamestring—Additional CSS classes.

ContextMenuCheckboxItem

PropTypeDefaultDescription
checkedboolean—Whether the checkbox is checked.
onCheckedChange(checked: boolean) => void—Callback when the checked state changes.
disabledbooleanfalseDisables the item.
classNamestring—Additional CSS classes.

ContextMenuRadioGroup

PropTypeDefaultDescription
valuestring—The controlled selected value.
onValueChange(value: string) => void—Callback when the selected value changes.
classNamestring—Additional CSS classes.

ContextMenuRadioItem

PropTypeDefaultDescription
valuestring—Required. The value of this item.
disabledbooleanfalseDisables the item.
classNamestring—Additional CSS classes.

ContextMenuLabel

PropTypeDefaultDescription
insetbooleanfalseAdds left padding to align with items that have icons.
classNamestring—Additional CSS classes.

ContextMenuSub / ContextMenuSubTrigger / ContextMenuSubContent

PropTypeDefaultDescription
insetbooleanfalseAligns the sub-trigger with icon items.
iconReactNode—Icon rendered before the sub-trigger label.

Anatomy

<ContextMenu>
	<ContextMenuTrigger /> {/* Right-click target */}
	<ContextMenuContent>
		{" "}
		{/* Menu panel */}
		<ContextMenuLabel /> {/* Section heading */}
		<ContextMenuGroup>
			{" "}
			{/* Logical grouping */}
			<ContextMenuItem /> {/* Standard action */}
			<ContextMenuCheckboxItem /> {/* Boolean toggle */}
		</ContextMenuGroup>
		<ContextMenuRadioGroup>
			{" "}
			{/* Exclusive selection */}
			<ContextMenuRadioItem />
		</ContextMenuRadioGroup>
		<ContextMenuSeparator /> {/* Visual divider */}
		<ContextMenuSub>
			{" "}
			{/* Nested submenu */}
			<ContextMenuSubTrigger />
			<ContextMenuSubContent>
				<ContextMenuItem />
			</ContextMenuSubContent>
		</ContextMenuSub>
	</ContextMenuContent>
</ContextMenu>

Accessibility

Context menus are inaccessible to keyboard-only users if the trigger area cannot be focused. Ensure your trigger element is keyboard-reachable (or provide an alternative mechanism).

  • Right-click / long-press — Opens the menu at the cursor position.
  • Arrow keys — Navigate between menu items.
  • Enter / Space — Select the focused item.
  • Escape — Close the menu.
  • Tab — Closes the menu and moves focus to the next focusable element.
  • Submenu navigation — Right arrow opens a submenu; Left arrow closes it.
  • role="menu" and role="menuitem" are applied automatically by Radix UI.

On this page