PeakUI

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 to body
  • .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: :hover or [data-hovered="true"] on an item
  • Item focus: :focus-visible or [data-focus-visible="true"] on an item
  • Item pressed: :active or [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-visible or [data-focus-visible="true"] on the popover
  • Coarse pointer: @media (pointer: coarse) sets user-select: none on the trigger; [data-context-menu-native] descendants keep user-select: auto
  • Forced colors: @media (forced-colors: active) draws a ButtonBorder border on the popover

API Reference

ContextMenu

PropTypeDefaultDescription
isOpenboolean-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
defaultOpenbooleanfalseWhether the menu is open on first render (uncontrolled)
onOpenChange(isOpen: boolean) => void-Called when the menu opens or closes
isDisabledbooleanfalseLeaves the browser menu alone, closes an open menu, and does not intercept clicks or drags of descendants
childrenReactNode-Trigger and Popover

ContextMenu.Trigger

PropTypeDefaultDescription
renderDOMRenderFunction<E, undefined>-Custom render function; E defaults to "div"
classNamestring-Additional CSS classes
childrenReactNode-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
...restJSX.IntrinsicElements[E]-Native attributes of the rendered element

ContextMenu.Popover

PropTypeDefaultDescription
placementPlacement"bottom start"Placement against the anchor
offsetnumber0Distance from the anchor in pixels
classNamestring | ((values) => string)-Additional CSS classes
childrenReactNode-Popover content; required
...restPopoverProps-React Aria Popover attributes, except isOpen, onOpenChange and triggerRef

ContextMenu.Menu

PropTypeDefaultDescription
onAction(key: Key) => void-Called when an item is activated; the menu then closes
onClose() => void-Called when the menu closes itself
autoFocusboolean | "first" | "last"-Set by the open method: a keyboard open focuses the first item, a pointer open the menu
aria-labelstring-Accessible name of the menu
classNamestring | ((values) => string)-Additional CSS classes
childrenReactNode-Menu content
...restMenuProps<T>-React Aria Menu attributes

ContextMenu.Item

Shared with Dropdown and Menu: the same menu-item part.

PropTypeDefaultDescription
idKey-Unique identifier for the item
textValuestring-Text content of the item for typeahead
variant"default" | "danger""default"Visual variant of the item
isDisabledboolean-Whether the item is disabled
hrefstring-Renders the item as a link to this URL
classNamestring-Additional CSS classes
childrenReactNode | RenderFunction-Item content or render function
...restMenuItemProps-React Aria MenuItem attributes

ContextMenu.Section

PropTypeDefaultDescription
classNamestring-Additional CSS classes
childrenReactNode-Section content
...restMenuSectionProps-React Aria MenuSection attributes

ContextMenu.ItemIndicator

PropTypeDefaultDescription
type"checkmark" | "dot""checkmark"Type of indicator to display
classNamestring-Additional CSS classes
childrenReactNode | RenderFunction-Custom indicator content or render function

ContextMenu.SubmenuIndicator

PropTypeDefaultDescription
classNamestring-Additional CSS classes
childrenReactNode-Custom indicator content

ContextMenu.SubmenuTrigger

PropTypeDefaultDescription
childrenReactNode-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
...restSubmenuTriggerProps-React Aria SubmenuTrigger attributes

On this page