Context Menu

Actions for one element, opened by right click, long press or agent.

Source
$npx @dinachi/cli@latest add context-menu
Right click here
disabled
ContextMenuShortcut
ContextMenuSeparator

Usage

import {
  ContextMenu,
  ContextMenuContent,
  ContextMenuItem,
  ContextMenuTrigger,
} from "@/components/ui/context-menu"

export function FileMenu() {
  return (
    <ContextMenu>
      <ContextMenuTrigger className="rounded-md border border-dashed p-8 text-sm">
        Report.pdf
      </ContextMenuTrigger>
      <ContextMenuContent>
        <ContextMenuItem>Open</ContextMenuItem>
        <ContextMenuItem>Rename</ContextMenuItem>
        <ContextMenuItem>Delete</ContextMenuItem>
      </ContextMenuContent>
    </ContextMenu>
  )
}

Examples

Right click here
Items with shortcuts and a disabled item.
import {
  ContextMenu,
  ContextMenuContent,
  ContextMenuItem,
  ContextMenuTrigger,
  ContextMenuShortcut,
  ContextMenuSeparator,
} from '@/components/ui/context-menu';

export function Example() {
  return (
    <ContextMenu>
      <ContextMenuTrigger className="flex h-[150px] w-[300px] items-center justify-center rounded-md border border-dashed text-sm bg-card hover:bg-accent/50 transition-colors">
        Right click here
      </ContextMenuTrigger>
      <ContextMenuContent className="w-64">
        <ContextMenuItem>
          Back
          <ContextMenuShortcut>⌘[</ContextMenuShortcut>
        </ContextMenuItem>
        <ContextMenuItem disabled>
          Forward
          <ContextMenuShortcut>⌘]</ContextMenuShortcut>
        </ContextMenuItem>
        <ContextMenuItem>
          Reload
          <ContextMenuShortcut>⌘R</ContextMenuShortcut>
        </ContextMenuItem>
        <ContextMenuSeparator />
        <ContextMenuItem>
          Developer Tools
          <ContextMenuShortcut>F12</ContextMenuShortcut>
        </ContextMenuItem>
      </ContextMenuContent>
    </ContextMenu>
  );
}

Notes

Checkbox and radio items

ContextMenuCheckboxItem and ContextMenuRadioItem keep the menu open when chosen. Pass closeOnClick to close on choice.

API

openboolean
Controlled open state.
defaultOpenboolean = false
Starting open state when uncontrolled.
onOpenChange
agent
(open, details) => void
Runs on open and close, from either side. details.cancel() refuses it.
disabled
agent
boolean = false
Ignores right clicks and long presses, and blocks agents.
loopFocusboolean = true
Wraps the keyboard highlight from the last item to the first.
highlightItemOnHoverboolean = true
Moves the highlight with the pointer.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as menu.

Reads

label

The trigger's aria-label or text while closed.

open

Whether the menu is showing.

disabled

True when the whole menu is disabled.

Actions

open() / close()

Whichever fits the current state. Needs no pointer.

press()

On an item, once the menu is open. Items are listed as menuitem.

setChecked(true)

On a checkbox item. Presses it only if the state differs.

Opening and closing run through onOpenChange. isAgentChange(details) is true for an agent's change, and details.cancel() refuses it; the agent is told the app refused. Item presses arrive as ordinary clicks; refuse those in the item's handler. A disabled menu and disabled items refuse on their own.

await agent.act({
  steps: [{ ref: "Right click here", action: "open" }],
})

Accessibility

  • The popup is a menu; items are menuitem, menuitemcheckbox or menuitemradio, with checked state.
  • On touch, a 500ms long press opens the menu.
  • Right click is hard to discover and absent on many devices. Offer the actions elsewhere too.
  • Disabled items stay announced but are skipped by the keyboard.