Skip to Content
NarakUI

Context Menu

A menu of actions for an item that opens where the pointer right-clicks.

Basic usage

Wrap the target in ContextMenu.Trigger asChild; right-clicking it or long-pressing it on a touch screen opens the menu at the pointer. Items take the same start, end, destructive, and href props as Dropdown Menu items. An item with href renders as a link, so it supports opening in a new tab.

Right-clicking is hard to discover and unavailable to many keyboard users, so offer the same actions somewhere visible too, usually an ellipsis dropdown on the same card or row.

Weekly visibility reportRight-click to see actions

Labels, shortcuts, and submenus

Give each row its own ContextMenu.Root so the menu knows which item it acts on; the trigger gets data-state="open" while its menu is open, which you can use to highlight the row. ContextMenu.Label names a section, ContextMenu.Shortcut shows a key hint at the end of an item, and ContextMenu.Sub nests a submenu that opens on hover or with the right arrow key.

A disabled item with disabledReason stays hoverable and shows the reason in a tooltip, so people learn why the action isn't available.

Best espresso machines for home baristasCoffee gear
Which decaf coffee tastes the most like regular coffee?Coffee beans
Is a coffee subscription worth it?Subscriptions

Installation

pnpm dlx shadcn@latest add @narakui/context-menu

Add "@narakui": "https://ui.narakcode.dev/r/{name}.json" to components.json, or install directly from https://ui.narakcode.dev/r/context-menu.json.

Usage

import { ContextMenu } from '@/components/ui/context-menu';

<ContextMenu.Root>
  <ContextMenu.Trigger asChild>
    <div>Weekly visibility report</div>
  </ContextMenu.Trigger>
  <ContextMenu.Content>
    <ContextMenu.Item start="pencil">Rename</ContextMenu.Item>
    <ContextMenu.Separator />
    <ContextMenu.Item start="trash" destructive>
      Delete
    </ContextMenu.Item>
  </ContextMenu.Content>
</ContextMenu.Root>

ContextMenu.Trigger asChild needs a child that forwards its ref and props, such as a div. When the target is a link or a clickable row, stop the menu's click and key events from reaching it with onClick and onKeyDown on ContextMenu.Content.

ContextMenu.Root Properties

PropTypeDefaultDescription
onOpenChange(open: boolean) => void—Called when the menu opens or closes
modalbooleantrueBlocks interaction outside the menu while it is open
dir'ltr' | 'rtl'—Reading direction, which also sets which side submenus open on

ContextMenu.Trigger Properties

PropTypeDefaultDescription
asChildbooleanfalseUses the child element as the right-click target instead of a wrapping span
disabledbooleanfalseLets the browser's own context menu open instead

ContextMenu.Content Properties

PropTypeDefaultDescription
sizenumber249Minimum width in pixels
alignOffsetnumber0Offset from the pointer along the alignment axis
collisionPaddingnumber | Partial<Record<Side, number>>0Space to keep from the viewport edges
onCloseAutoFocus(event: Event) => void—Called when focus returns to the trigger after closing. Call preventDefault to keep focus elsewhere
classNamestring—Classes for the menu surface

ContextMenu.Item Properties

PropTypeDefaultDescription
startIconName | IconProps | ReactNode—Icon or element before the label
endIconName | IconProps | ReactNode—Icon or element after the label, aligned to the end
destructivebooleanfalseShows the item in red for actions that remove data
hrefstring—Renders the item as a link
targetstring—Link target, such as _blank
relstring—Link relationship. Use noopener noreferrer with target="_blank"
onSelect(event: Event) => void—Called when the item is chosen. Call preventDefault to keep the menu open
disabledbooleanfalseDims the item and prevents choosing it
disabledReasonstring—Tooltip explaining why a disabled item is unavailable. Keeps the item hoverable

ContextMenu.Label Properties

PropTypeDefaultDescription
size'small' | 'medium''medium'Height and text size of the section label

ContextMenu.Sub Properties

PropTypeDefaultDescription
openboolean—Controlled open state of the submenu
onOpenChange(open: boolean) => void—Called when the submenu opens or closes

ContextMenu.SubTrigger Properties

PropTypeDefaultDescription
startIconIconName | IconProps | ReactNode—Icon or element before the label. A chevron is always shown at the end
disabledbooleanfalseDims the trigger and prevents opening the submenu

ContextMenu.SubContent Properties

PropTypeDefaultDescription
sizenumber249Minimum width in pixels
sideOffsetnumber8Distance from the parent menu in pixels
alignOffsetnumber-4Vertical offset from the submenu trigger
pinnedBottomReactNode—Content fixed below the submenu's scrolling items, such as a create action

Other Parts

PartTypeDescription
SeparatorcomponentDivider line between groups of items
ShortcutcomponentKey hint placed inside an item, after its label
GroupcomponentGroups related items without adding visible chrome
PortalcomponentRadix portal. Content and SubContent already render in one