Context Menu

A context menu opens a list of actions at the pointer when someone right-clicks or long-presses an area.

A context menu opens at the pointer when someone right-clicks an area, or presses and holds it on a touch screen. It offers shortcuts to actions for the thing under the pointer. Nothing on screen shows that the menu exists, so for actions people need to find, use a Dropdown Menu on a visible button.

Playground

Q3 report.pdfRight-click for actions
Right-click the card, or press and hold it on a touch screen.

Usage

import { Card, ContextMenu, Text } from '@kushagradhawan/kookie-ui';
 
export function FileCard({ name, onRename, onDuplicate, onDelete }: { name: string; onRename: () => void; onDuplicate: () => void; onDelete: () => void }) {
  return (
    <ContextMenu.Root>
      <ContextMenu.Trigger>
        <Card variant="raised" size="2">
          <Text size="2" weight="medium">{name}</Text>
        </Card>
      </ContextMenu.Trigger>
      <ContextMenu.Content>
        <ContextMenu.Item shortcut="⌘ R" onSelect={onRename}>Rename</ContextMenu.Item>
        <ContextMenu.Item shortcut="⌘ D" onSelect={onDuplicate}>Duplicate</ContextMenu.Item>
        <ContextMenu.Separator />
        <ContextMenu.Item color="red" onSelect={onDelete}>Delete</ContextMenu.Item>
      </ContextMenu.Content>
    </ContextMenu.Root>
  );
}

Anatomy

<ContextMenu.Root>
  <ContextMenu.Trigger />
  <ContextMenu.Content>
    <ContextMenu.Label />
    <ContextMenu.Item />
    <ContextMenu.Group />
    <ContextMenu.CheckboxItem />
    <ContextMenu.RadioGroup>
      <ContextMenu.RadioItem />
    </ContextMenu.RadioGroup>
    <ContextMenu.Sub>
      <ContextMenu.SubTrigger />
      <ContextMenu.SubContent />
    </ContextMenu.Sub>
    <ContextMenu.Separator />
  </ContextMenu.Content>
</ContextMenu.Root>
  • Root: Holds the open state.
  • Trigger: Wraps one element, the area that opens the menu.
  • Content: The menu, rendered in a portal at the pointer.
  • Item: One action.
  • CheckboxItem: An option that turns on or off.
  • RadioGroup and RadioItem: A set of options where one is on.
  • Label: A heading for the items after it. It can't be chosen.
  • Group and Separator: A set of related items, and a line between sets.
  • Sub, SubTrigger and SubContent: A nested menu and the item that opens it.

Best practices

Put every action somewhere else too. People who don't know to right-click, and many keyboard and touch users, never open a context menu. Treat it as a shortcut to actions that a toolbar, a button or a Dropdown Menu also offers.

List only actions for the thing under the pointer. A context menu on a file offers rename, move and delete for that file. Actions for the whole page belong in the page's own controls.

Put the most frequent actions first. The menu opens at the pointer, so the first items are the closest. Put destructive actions last, after a separator, and set color="red".

Keep it short. Aim for about ten items. Move rarely used items into one submenu, and don't nest a submenu inside another.

Don't replace the browser menu on text and links. People right-click text to copy it and links to open them in a new tab. Attach a context menu to objects such as cards, rows and canvas items, not to the whole page.

Size

size on Content picks one of four item heights and the padding around them. Size 2 is the default. A submenu takes the size of its parent unless you set its own. size accepts a responsive object. The first item lines up with the pointer at every size.

Examples

Rows in a list

Wrap each row in its own Root and Trigger, so the menu acts on the row under the pointer. Keep the same actions in a row button for people who can't right-click.

{files.map((file) => (
  <ContextMenu.Root key={file.id}>
    <ContextMenu.Trigger>
      <Table.Row>{/* cells */}</Table.Row>
    </ContextMenu.Trigger>
    <ContextMenu.Content>
      <ContextMenu.Item onSelect={() => open(file)}>Open</ContextMenu.Item>
      <ContextMenu.Item onSelect={() => share(file)}>Share</ContextMenu.Item>
    </ContextMenu.Content>
  </ContextMenu.Root>
))}

Canvas options

Use CheckboxItem and a RadioGroup for settings that belong to the area, such as a design canvas. Each choice closes the menu unless you call event.preventDefault() in onSelect.

<ContextMenu.Content>
  <ContextMenu.CheckboxItem checked={showGrid} onCheckedChange={setShowGrid}>
    Show grid
  </ContextMenu.CheckboxItem>
  <ContextMenu.Separator />
  <ContextMenu.Label>Zoom</ContextMenu.Label>
  <ContextMenu.RadioGroup value={zoom} onValueChange={setZoom}>
    <ContextMenu.RadioItem value="fit">Fit to screen</ContextMenu.RadioItem>
    <ContextMenu.RadioItem value="100">100%</ContextMenu.RadioItem>
  </ContextMenu.RadioGroup>
</ContextMenu.Content>

Nested actions

Group related targets under a Sub. Submenus always open beside the menu.

<ContextMenu.Sub>
  <ContextMenu.SubTrigger>Move to</ContextMenu.SubTrigger>
  <ContextMenu.SubContent>
    <ContextMenu.Item>Drafts</ContextMenu.Item>
    <ContextMenu.Item>Archive</ContextMenu.Item>
  </ContextMenu.SubContent>
</ContextMenu.Sub>

Shortcut hints

shortcut shows a key hint at the end of the item and sets aria-keyshortcuts. It doesn't bind the key, so register the shortcut in your app as well.

Props

Root

PropTypeDefaultDescription
onOpenChange(open: boolean) => voidCalled when the menu opens or closes
modalbooleantrueBlocks the rest of the page while the menu is open
dir'ltr' | 'rtl'The reading direction

The open state can't be controlled. The menu opens only from the trigger.

Trigger

Trigger takes one child element and accepts disabled, which lets the browser menu open instead.

Content

PropTypeDefaultDescription
size'1' | '2' | '3' | '4', responsive'2'The item height step
colorAccentColorTheme accentThe colour of the highlight
alignOffsetnumberMinus the content paddingLines the first item up with the pointer
collisionPaddingnumber10The space kept from the edge of the screen
loopbooleanfalseWraps the arrow keys from the last item to the first
virtualizedbooleanfalseLeaves out the scroll area, for a Virtual Menu that scrolls itself
containerHTMLElementdocument.bodyThe element the portal renders into

Item

PropTypeDefaultDescription
onSelect(event: Event) => voidCalled when the item is chosen. Call event.preventDefault() to keep the menu open
colorAccentColorThe colour of the item
shortcutstringA key hint at the end of the item
disabledbooleanfalseStops the item from being chosen
textValuestringThe text for typeahead, when the children aren't plain text
asChildbooleanfalseRenders the child element, such as a link, with the item's look

CheckboxItem

PropTypeDefaultDescription
checkedboolean | 'indeterminate'Whether the option is on
onCheckedChange(checked: boolean) => voidCalled when the option turns on or off
color, shortcut, disabled, textValue, onSelectAs on Item

RadioGroup and RadioItem

PropTypeDefaultDescription
valuestringOn RadioGroup, the option that is on. On RadioItem, the value it sets. Required on RadioItem
onValueChange(value: string) => voidOn RadioGroup, called when another option turns on
color, disabled, textValue, onSelectOn RadioItem, as on Item

Sub, SubTrigger and SubContent

PropTypeDefaultDescription
open, defaultOpen, onOpenChangeOn Sub, the open state of the submenu
disabled, textValueOn SubTrigger, as on Item
size, colorParent menu'sOn SubContent, as on Content
virtualized, container, loopOn SubContent, as on Content

Content, the items, Label, Group, Separator and SubContent also accept the attributes of the HTML div element.

Accessibility

KeyAction
Arrow Down or Arrow UpMoves focus through the items
Home or EndMoves focus to the first or last item
Enter or SpaceChooses the focused item, or opens its submenu
Arrow RightOpens the submenu of the focused item
Arrow LeftCloses the submenu
A letter keyMoves focus to the next item that starts with it
EscapeCloses the menu
  • Opening: The menu opens on the contextmenu event. Most browsers also fire it for the Menu key and Shift F10 on a focused element, so make the trigger focusable when keyboard users need the menu.
  • Announcements: Items have the menuitem role, and checkbox and radio items announce whether they're on. A shortcut hint is hidden from screen readers, which read aria-keyshortcuts instead.
  • Groups: A Group or RadioGroup that holds a Label is named by that Label, so a screen reader announces the section as focus enters it.
  • Touch: A long press opens the menu on touch and pen input.
  • Focus: Focus moves into the menu when it opens and stays there until it closes.