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
openbooleanControlled open state.
onOpenChange(open: boolean) => voidCallback when open state changes.
modalbooleantrueWhether the menu is modal.
dir"ltr" | "rtl"Reading direction for submenus.
childrenReactNodeRequired. Must include Trigger and Content.

ContextMenuTrigger

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

ContextMenuContent

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

ContextMenuItem

PropTypeDefaultDescription
variant"default" | "danger""default"Visual variant of the item.
iconReactNodeIcon rendered before the label.
disabledbooleanfalseDisables the item.
onSelect(e: Event) => voidCallback when the item is selected.
classNamestringAdditional CSS classes.

ContextMenuCheckboxItem

PropTypeDefaultDescription
checkedbooleanWhether the checkbox is checked.
onCheckedChange(checked: boolean) => voidCallback when the checked state changes.
disabledbooleanfalseDisables the item.
classNamestringAdditional CSS classes.

ContextMenuRadioGroup

PropTypeDefaultDescription
valuestringThe controlled selected value.
onValueChange(value: string) => voidCallback when the selected value changes.
classNamestringAdditional CSS classes.

ContextMenuRadioItem

PropTypeDefaultDescription
valuestringRequired. The value of this item.
disabledbooleanfalseDisables the item.
classNamestringAdditional CSS classes.

ContextMenuLabel

PropTypeDefaultDescription
insetbooleanfalseAdds left padding to align with items that have icons.
classNamestringAdditional CSS classes.

ContextMenuSub / ContextMenuSubTrigger / ContextMenuSubContent

PropTypeDefaultDescription
insetbooleanfalseAligns the sub-trigger with icon items.
iconReactNodeIcon 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