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
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
| Prop | Type | Default | Description |
|---|---|---|---|
onOpenChange | (open: boolean) => void | Called when the menu opens or closes | |
modal | boolean | true | Blocks 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
| Prop | Type | Default | Description |
|---|---|---|---|
size | '1' | '2' | '3' | '4', responsive | '2' | The item height step |
color | AccentColor | Theme accent | The colour of the highlight |
alignOffset | number | Minus the content padding | Lines the first item up with the pointer |
collisionPadding | number | 10 | The space kept from the edge of the screen |
loop | boolean | false | Wraps the arrow keys from the last item to the first |
virtualized | boolean | false | Leaves out the scroll area, for a Virtual Menu that scrolls itself |
container | HTMLElement | document.body | The element the portal renders into |
Item
| Prop | Type | Default | Description |
|---|---|---|---|
onSelect | (event: Event) => void | Called when the item is chosen. Call event.preventDefault() to keep the menu open | |
color | AccentColor | The colour of the item | |
shortcut | string | A key hint at the end of the item | |
disabled | boolean | false | Stops the item from being chosen |
textValue | string | The text for typeahead, when the children aren't plain text | |
asChild | boolean | false | Renders the child element, such as a link, with the item's look |
CheckboxItem
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | 'indeterminate' | Whether the option is on | |
onCheckedChange | (checked: boolean) => void | Called when the option turns on or off | |
color, shortcut, disabled, textValue, onSelect | As on Item |
RadioGroup and RadioItem
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | On RadioGroup, the option that is on. On RadioItem, the value it sets. Required on RadioItem | |
onValueChange | (value: string) => void | On RadioGroup, called when another option turns on | |
color, disabled, textValue, onSelect | On RadioItem, as on Item |
Sub, SubTrigger and SubContent
| Prop | Type | Default | Description |
|---|---|---|---|
open, defaultOpen, onOpenChange | On Sub, the open state of the submenu | ||
disabled, textValue | On SubTrigger, as on Item | ||
size, color | Parent menu's | On SubContent, as on Content | |
virtualized, container, loop | On SubContent, as on Content |
Content, the items, Label, Group, Separator and SubContent also accept the attributes of the HTML div element.
Accessibility
| Key | Action |
|---|---|
| Arrow Down or Arrow Up | Moves focus through the items |
| Home or End | Moves focus to the first or last item |
| Enter or Space | Chooses the focused item, or opens its submenu |
| Arrow Right | Opens the submenu of the focused item |
| Arrow Left | Closes the submenu |
| A letter key | Moves focus to the next item that starts with it |
| Escape | Closes the menu |
- Opening: The menu opens on the
contextmenuevent. 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
menuitemrole, and checkbox and radio items announce whether they're on. A shortcut hint is hidden from screen readers, which readaria-keyshortcutsinstead. - 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.