Context Menu
A menu of actions for an area of the page, such as a file in a list, that opens at the pointer on a right-click, a Ctrl+click on macOS or, on Android, a long press, and from the keyboard with Shift+F10 or the Menu key: the browser fires the contextmenu event for each, and Menhir cancels it, so the browser's own menu does not show. It is a Menu opened from an area instead of a button, so its items, submenus, keys and typeahead are Menu's, and an app with both components pays for them once. From the keyboard it opens below the focused element. A right-click while it is open moves it, and one inside it shows no browser menu over it. Focus goes to the first item, and returns where it was once the menu closes. The area is a plain <div> with no role, as ARIA gives a context menu none, so keep its actions reachable another way too, such as a Menu button: iOS Safari fires no contextmenu on a long press. Name the Popup with aria-label.
- Size
- 12.0 kB
Example
import { ContextMenu } from "menhir/context-menu";
const popup =
"m-0 min-w-40 rounded-lg border border-stone-200 bg-white p-1 text-sm text-inherit shadow-lg transition-opacity duration-100 data-closed:opacity-0 dark:border-stone-700 dark:bg-stone-900 starting:data-open:opacity-0";
const item =
"flex cursor-default items-center gap-2 rounded py-1.5 pr-2 pl-7 outline-none select-none data-active:bg-stone-100 data-disabled:opacity-50 dark:data-active:bg-stone-800";
export default function ContextMenuExample() {
return (
<ContextMenu.Root>
<ContextMenu.Trigger
tabindex="0"
class="grid h-32 w-64 place-items-center rounded-lg border border-dashed border-stone-300 text-sm select-none [-webkit-touch-callout:none] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 data-open:bg-stone-100 dark:border-stone-600 dark:data-open:bg-stone-800"
>
Right-click here
</ContextMenu.Trigger>
<ContextMenu.Popup aria-label="Photo" class={popup}>
<ContextMenu.Item class={item}>Open</ContextMenu.Item>
<ContextMenu.Item class={item}>Rename</ContextMenu.Item>
<ContextMenu.Item class={item} disabled>
Paste
</ContextMenu.Item>
<hr class="my-1 border-stone-200 dark:border-stone-700" />
<ContextMenu.CheckboxItem class={item} defaultChecked>
<ContextMenu.ItemIndicator class="-ml-5 w-3 data-unchecked:invisible">
✓
</ContextMenu.ItemIndicator>
Favorite
</ContextMenu.CheckboxItem>
<ContextMenu.Submenu>
<ContextMenu.SubmenuTrigger
class={`${item} after:ml-auto after:content-['›']`}
>
Share
</ContextMenu.SubmenuTrigger>
<ContextMenu.Popup class={popup}>
<ContextMenu.Item class={item}>Email</ContextMenu.Item>
<ContextMenu.Item class={item}>Messages</ContextMenu.Item>
</ContextMenu.Popup>
</ContextMenu.Submenu>
</ContextMenu.Popup>
</ContextMenu.Root>
);
}
Anatomy
<ContextMenu.Root>
<ContextMenu.Trigger />
<ContextMenu.Popup>
<ContextMenu.Item />
<ContextMenu.CheckboxItem>
<ContextMenu.ItemIndicator />
</ContextMenu.CheckboxItem>
<ContextMenu.Group>
<ContextMenu.GroupLabel />
</ContextMenu.Group>
<ContextMenu.RadioGroup>
<ContextMenu.RadioItem>
<ContextMenu.ItemIndicator />
</ContextMenu.RadioItem>
</ContextMenu.RadioGroup>
<hr />
<ContextMenu.Submenu>
<ContextMenu.SubmenuTrigger />
<ContextMenu.Popup />
</ContextMenu.Submenu>
</ContextMenu.Popup>
</ContextMenu.Root>API reference
Root
Holds the open state and where the menu was asked for, and renders no element. The Trigger and the Popup have data-open or data-closed.
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | — | Whether the menu is open, when controlled. Pair it with onOpenChange. Opened this way before any right-click, it is placed below the Trigger. |
defaultOpen | boolean | false | Whether the menu is open at first, when uncontrolled. It is placed below the Trigger. |
onOpenChange | (open: boolean, details: { reason, event }) => void | — | Called with the new value when the user opens or closes the menu, and why: "trigger-press" (the contextmenu event, or a press on the element it opened from), "item-press" (an item was activated), "escape-key", "outside-press", "focus-out" (Tab moved focus out) or "close-request" (such as another popover opening). A controlled menu changes only if open does. |
Trigger
<div> whose contextmenu event opens the menu: at the pointer, or below the focused element for a key. It has no role and no tabindex: the keyboard opens the menu from a focused element inside it, so give it tabindex="0" if nothing inside it takes focus. It has data-open or data-closed, and data-disabled.
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | — | Whether it opens nothing, so the browser's own menu shows instead. |
render | (props) => JSX.Element | — | Renders the area with your own component. Read once. |
Popup
<div popover="auto" role="menu">, Menu's Popup, placed at the pointer, or below the element a key opened it from: its top-left corner at the pointer, its bottom-left near the bottom of the viewport. Name it with aria-label. Render it right after the Trigger, or right after a submenu's SubmenuTrigger, where it is placed as in Menu.
| Prop | Type | Default | Description |
|---|---|---|---|
side | "top" | "right" | "bottom" | "left" | "bottom", in a submenu the inline end | The side of the pointer it is placed against. It moves to the other side when it does not fit, and data-side says where it went. |
align | "start" | "center" | "end" | "start" | Where along that side it sits. start and end follow the writing direction. |
sideOffset | number | 0 | The gap between its trigger and the Popup, in pixels. |
alignOffset | number | 0 | The shift along its trigger's side, in pixels. |
collisionPadding | number | 5 | The space it keeps from the viewport's edges, in pixels. |
anchor | Element | VirtualElement | (() => Element | VirtualElement | undefined) | — | What it is placed against instead of the pointer: an element, a virtual element with a getBoundingClientRect(), or a function returning one. |
returnFocus | HTMLElement | ((focused) => HTMLElement | null | undefined) | — | Where focus goes once it has closed with focus inside, instead of the element it opened from. A function gets the element the browser focused, or null. |
render | (props) => JSX.Element | — | Renders the popup with your own component. Read once. |
Item
<div role="menuitem">. A click, Enter or Space calls its onClick and closes every open menu. Render a link with render; Enter then follows it.
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | — | Whether it is disabled: the keys and the pointer still reach it, as the menu pattern asks, but it is not activated and its onClick is not called. |
label | string | — | The text typing a character matches, when the item's content is not its label, such as an icon and a shortcut beside the text. |
render | (props) => JSX.Element | — | Renders the item with your own component, such as a link. Read once. |
CheckboxItem
<div role="menuitemcheckbox"> with aria-checked and data-checked or data-unchecked. A click and Enter change it and close the menu; Space changes it and keeps the menu open.
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | — | Whether it is checked, when controlled. Pair it with onCheckedChange. |
defaultChecked | boolean | false | Whether it is checked at first, when uncontrolled. |
onCheckedChange | (checked: boolean) => void | — | Called with the new value when the user changes it. |
disabled | boolean | — | Whether it is disabled: the keys and the pointer still reach it, as the menu pattern asks, but it is not activated and its onClick is not called. |
label | string | — | The text typing a character matches, when the item's content is not its label, such as an icon and a shortcut beside the text. |
render | (props) => JSX.Element | — | Renders the item with your own component. Read once. |
RadioGroup
<div role="group"> that holds which of its RadioItems is checked, named by its GroupLabel.
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | null | — | The checked item's value, when controlled; null checks none. Pair it with onValueChange. |
defaultValue | string | null | null | The checked item's value at first, when uncontrolled. |
onValueChange | (value: string) => void | — | Called with the value of the item the user checks. |
render | (props) => JSX.Element | — | Renders the group with your own component. Read once. |
RadioItem
<div role="menuitemradio"> with aria-checked and data-checked or data-unchecked. A click and Enter check it and close the menu; Space checks it and keeps the menu open.
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | — | The value its group holds while it is checked. |
disabled | boolean | — | Whether it is disabled: the keys and the pointer still reach it, as the menu pattern asks, but it is not activated and its onClick is not called. |
label | string | — | The text typing a character matches, when the item's content is not its label, such as an icon and a shortcut beside the text. |
render | (props) => JSX.Element | — | Renders the item with your own component. Read once. |
ItemIndicator
aria-hidden <span> with its item's data-checked or data-unchecked, always rendered: show a check mark by state with CSS.
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the indicator with your own component. Read once. |
Group
<div role="group"> for items that belong together, named by its GroupLabel.
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the group with your own component. Read once. |
GroupLabel
<div> that names its Group or RadioGroup. The keys do not reach it. Takes your id, else a generated one.
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the label with your own component. Read once. |
Submenu
Holds a submenu's open state, as the Root does, and renders no element. Place it inside a Popup, around a SubmenuTrigger and the submenu's Popup.
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | — | Whether the submenu is open, when controlled. Pair it with onOpenChange. |
defaultOpen | boolean | false | Whether the submenu is open at first, when uncontrolled. |
onOpenChange | (open: boolean, details: { reason, event }) => void | — | Called with the new value, and why: "trigger-hover", "trigger-press", "arrow-key" (Right or Left Arrow, or Up, Down, Home, End or typing moving through the menu around it), "item-press", "escape-key" or "outside-press". |
delay | number | 100 | How long the pointer rests on the SubmenuTrigger before the submenu opens, in milliseconds. |
closeDelay | number | 0 | How long after the pointer has left the SubmenuTrigger and the submenu it closes, in milliseconds. The pointer may cross to the submenu diagonally without a delay. |
SubmenuTrigger
<div role="menuitem" aria-haspopup="menu"> with aria-expanded and data-open or data-closed: an item of the menu around it that opens its submenu, which it labels.
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | — | Whether it is disabled: the keys and the pointer still reach it, as the menu pattern asks, but it is not activated and its onClick is not called. |
label | string | — | The text typing a character matches, when the item's content is not its label, such as an icon and a shortcut beside the text. |
render | (props) => JSX.Element | — | Renders the item with your own component. Read once. |
Keyboard
| Keys | Action |
|---|---|
| Shift + F10, Menu key | On a focused element in the Trigger, opens the menu below it with focus on its first item. Not every platform has these keys; macOS has neither. |
| Enter, Space | On an item, activates it and closes every menu; Space on a checkbox or radio item changes it and keeps the menu open. On a SubmenuTrigger, opens the submenu with focus on its first item. |
| Down Arrow, Up Arrow | Moves to the next or previous item, wrapping from the last to the first and back. |
| Right Arrow | On a SubmenuTrigger, opens the submenu with focus on its first item. Left Arrow right to left. |
| Left Arrow | In a submenu, closes it and moves focus to its SubmenuTrigger. Right Arrow right to left. |
| Home, End | Moves to the first or last item. |
| A character | Moves to the next item whose label starts with it; characters typed within a second make one string. |
| Escape | Closes the menu that has focus, and returns focus to where it was before the menu opened, or to the SubmenuTrigger. |
| Tab, Shift + Tab | Closes every menu. |