ContextMenu
Menu opened by right-click, long-press or keyboard
Usage
import {ContextMenu} from "@peakui/react";A context menu must never be the only path to an action: pair it with a visible control.
Anatomy
import {ContextMenu, Label} from "@peakui/react";
// Opens from: right-click, touch long-press, Shift+F10 or the ContextMenu key.
// Long-press: 500 ms (the underlying hook's default), touch only; a 10 px movement cancels it,
// and releasing the finger does not activate the item under it.
// Long-press is not discoverable. Automated tests cannot cover iOS Safari:
// verify it by hand on iOS Safari and Android Chrome.
// One Trigger per root. It is a plain div: not focusable, no role.
// Focusable descendants make the region reachable by keyboard.
// The Popover may be nested in the Trigger or sit beside it.
export default () => (
<ContextMenu>
<ContextMenu.Trigger>
<ContextMenu.Popover>
<ContextMenu.Menu aria-label="Actions">
<ContextMenu.Section aria-label="Section">
<ContextMenu.Item id="item" textValue="Item">
<Label />
<ContextMenu.ItemIndicator />
</ContextMenu.Item>
</ContextMenu.Section>
</ContextMenu.Menu>
</ContextMenu.Popover>
</ContextMenu.Trigger>
</ContextMenu>
);Examples
With Sections
With Native Escape
data-context-menu-native on a descendant keeps the browser menu for pointer events; keyboard open still works there.
Disabled
isDisabled leaves the browser menu alone.
Controlled
An open without a pointer anchors at the Trigger's bottom-start corner.
Customization
Tailwind CSS
<ContextMenu.Trigger className="rounded-xl border border-dashed p-12">Region</ContextMenu.Trigger>
<ContextMenu.Popover className="min-w-64 rounded-xl">
<ContextMenu.Menu aria-label="Actions">{/* items */}</ContextMenu.Menu>
</ContextMenu.Popover>Global CSS
@layer components {
.context-menu__trigger {
@apply rounded-xl border border-dashed;
}
.context-menu__popover {
@apply min-w-64 rounded-xl;
}
}Styling Reference
PeakUI follows the BEM methodology, so variants and states are plain class names you can target.
CSS Classes
The ContextMenu component uses these CSS classes (view source styles):
Element Classes
.context-menu__trigger- The region; draws nothing of its own.context-menu__anchor- Zero-size fixed element the popover positions against, portaled tobody.context-menu__popover- The popover surface
Shared Menu Classes
.menu- The menu container.menu-item- Menu item.menu-section- Menu section.menu-item__indicator- Selection indicator of an item.menu-item__indicator--submenu- Submenu indicator of an item.menu-item--default.menu-item--danger
Inside .context-menu__popover, [data-slot="context-menu-menu"] gets p-1.5 and [data-slot="menu-item"] gets px-2.5.
Slots
[data-slot="context-menu-trigger"][data-slot="context-menu-anchor"][data-slot="context-menu-popover"][data-slot="context-menu-menu"][data-slot="menu-item"][data-slot="menu-section"][data-slot="menu-item-indicator"][data-slot="menu-item-indicator--checkmark"][data-slot="menu-item-indicator--dot"][data-slot="submenu-indicator"]
Interactive States
The component supports both CSS pseudo-classes and data attributes:
- Item hover:
:hoveror[data-hovered="true"]on an item - Item focus:
:focus-visibleor[data-focus-visible="true"]on an item - Item pressed:
:activeor[data-pressed="true"]on an item - Item selected:
[aria-checked="true"],[aria-selected="true"]or[data-selected="true"]on an item - Item disabled:
[data-disabled="true"]on an item - Item submenu:
[data-has-submenu="true"]on an item - Entering:
[data-entering="true"]on the popover - Exiting:
[data-exiting="true"]on the popover - Focus:
:focus-visibleor[data-focus-visible="true"]on the popover - Coarse pointer:
@media (pointer: coarse)setsuser-select: noneon the trigger;[data-context-menu-native]descendants keepuser-select: auto - Forced colors:
@media (forced-colors: active)draws aButtonBorderborder on the popover
API Reference
ContextMenu
| Prop | Type | Default | Description |
|---|---|---|---|
isOpen | boolean | - | Whether the menu is open (controlled). Stays true if the Trigger unmounts: the parent owns the state. A parent that accepts an open asynchronously loses the keyboard focus target and anchor position |
defaultOpen | boolean | false | Whether the menu is open on first render (uncontrolled) |
onOpenChange | (isOpen: boolean) => void | - | Called when the menu opens or closes |
isDisabled | boolean | false | Leaves the browser menu alone, closes an open menu, and does not intercept clicks or drags of descendants |
children | ReactNode | - | Trigger and Popover |
ContextMenu.Trigger
| Prop | Type | Default | Description |
|---|---|---|---|
render | DOMRenderFunction<E, undefined> | - | Custom render function; E defaults to "div" |
className | string | - | Additional CSS classes |
children | ReactNode | - | The region content |
onKeyDown | (event: KeyboardEvent) => void | - | Shift+F10 or the ContextMenu key opens the menu anchored at the focused descendant, including an input, and focuses the first item; Escape closes it and focus returns. Ctrl, Alt and Meta chords, plain F10, a held key (repeat) and IME composition are ignored |
onContextMenu | (event: MouseEvent) => void | - | Runs first. A descendant that calls stopPropagation or preventDefault on contextmenu suppresses the menu |
...rest | JSX.IntrinsicElements[E] | - | Native attributes of the rendered element |
ContextMenu.Popover
| Prop | Type | Default | Description |
|---|---|---|---|
placement | Placement | "bottom start" | Placement against the anchor |
offset | number | 0 | Distance from the anchor in pixels |
className | string | ((values) => string) | - | Additional CSS classes |
children | ReactNode | - | Popover content; required |
...rest | PopoverProps | - | React Aria Popover attributes, except isOpen, onOpenChange and triggerRef |
ContextMenu.Menu
| Prop | Type | Default | Description |
|---|---|---|---|
onAction | (key: Key) => void | - | Called when an item is activated; the menu then closes |
onClose | () => void | - | Called when the menu closes itself |
autoFocus | boolean | "first" | "last" | - | Set by the open method: a keyboard open focuses the first item, a pointer open the menu |
aria-label | string | - | Accessible name of the menu |
className | string | ((values) => string) | - | Additional CSS classes |
children | ReactNode | - | Menu content |
...rest | MenuProps<T> | - | React Aria Menu attributes |
ContextMenu.Item
Shared with Dropdown and Menu: the same menu-item part.
| Prop | Type | Default | Description |
|---|---|---|---|
id | Key | - | Unique identifier for the item |
textValue | string | - | Text content of the item for typeahead |
variant | "default" | "danger" | "default" | Visual variant of the item |
isDisabled | boolean | - | Whether the item is disabled |
href | string | - | Renders the item as a link to this URL |
className | string | - | Additional CSS classes |
children | ReactNode | RenderFunction | - | Item content or render function |
...rest | MenuItemProps | - | React Aria MenuItem attributes |
ContextMenu.Section
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes |
children | ReactNode | - | Section content |
...rest | MenuSectionProps | - | React Aria MenuSection attributes |
ContextMenu.ItemIndicator
| Prop | Type | Default | Description |
|---|---|---|---|
type | "checkmark" | "dot" | "checkmark" | Type of indicator to display |
className | string | - | Additional CSS classes |
children | ReactNode | RenderFunction | - | Custom indicator content or render function |
ContextMenu.SubmenuIndicator
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes |
children | ReactNode | - | Custom indicator content |
ContextMenu.SubmenuTrigger
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | The item and a ContextMenu.Popover holding the submenu; opening and selecting a submenu item are tested in jsdom only, with no story or browser test |
...rest | SubmenuTriggerProps | - | React Aria SubmenuTrigger attributes |