Skip to content
UI

Context Menu

A menu of actions for an element, opened with a right-click or a long press.

@zenoxai/ui/components/context-menuBase UI Markdown
Right-click this alert
<ContextMenu>
  <ContextMenuTrigger className="flex h-36 w-full max-w-sm items-center justify-center rounded-lg border border-dashed border-border-strong text-text-secondary">
    Right-click this alert
  </ContextMenuTrigger>
  <ContextMenuContent className="w-52">
    <ContextMenuItem>
      <EyeIcon />
      View details
    </ContextMenuItem>
    <ContextMenuItem>
      <UserPlusIcon />
      Assign to me
      <ContextMenuShortcut>A</ContextMenuShortcut>
    </ContextMenuItem>
    <ContextMenuItem>
      <CopyIcon />
      Copy alert ID
      <ContextMenuShortcut>⌘C</ContextMenuShortcut>
    </ContextMenuItem>
    <ContextMenuSeparator />
    <ContextMenuItem variant="destructive">
      <Trash2Icon />
      Delete alert
    </ContextMenuItem>
  </ContextMenuContent>
</ContextMenu>

Usage

import {
  ContextMenu,
  ContextMenuContent,
  ContextMenuItem,
  ContextMenuTrigger,
} from "@zenoxai/ui/components/context-menu"
<ContextMenu>
  <ContextMenuTrigger>{/* the alert row, card or chart */}</ContextMenuTrigger>
  <ContextMenuContent>
    <ContextMenuItem onClick={viewDetails}>View details</ContextMenuItem>
    <ContextMenuItem onClick={assign}>Assign to me</ContextMenuItem>
  </ContextMenuContent>
</ContextMenu>

A context menu opens at the pointer when the user right-clicks the trigger area, or long-presses it on a touch screen. It is a shortcut for people who know it is there: a faster way to reach the actions of a table row, an asset card or a chart.

  • Every action in a context menu must also be reachable from a visible control, usually a Dropdown Menu on the same row. Keyboard users and many touch users never open a context menu.
  • For the menus at the top of an editor or app, use Menubar.

ContextMenuTrigger renders a <div> around the area that responds to the right-click and disables text selection inside it. The menu offers the same items as Dropdown Menu: items with icons and shortcuts, a destructive variant, checkbox and radio items, labels, separators and submenus.

Examples

ContextMenuSub nests a second menu that opens to the side.

Right-click srv-db-01.prod
<ContextMenu>
  <ContextMenuTrigger className="flex h-36 w-full max-w-sm items-center justify-center rounded-lg border border-dashed border-border-strong text-text-secondary">
    Right-click srv-db-01.prod
  </ContextMenuTrigger>
  <ContextMenuContent className="w-52">
    <ContextMenuItem>Open asset</ContextMenuItem>
    <ContextMenuItem>Run vulnerability scan</ContextMenuItem>
    <ContextMenuSub>
      <ContextMenuSubTrigger>Move to tenant</ContextMenuSubTrigger>
      <ContextMenuSubContent>
        <ContextMenuItem>Acme Corp</ContextMenuItem>
        <ContextMenuItem>Globex</ContextMenuItem>
        <ContextMenuItem>Initech</ContextMenuItem>
      </ContextMenuSubContent>
    </ContextMenuSub>
    <ContextMenuSeparator />
    <ContextMenuItem variant="destructive">
      Remove from inventory
    </ContextMenuItem>
  </ContextMenuContent>
</ContextMenu>

Checkbox and radio items

Settings of the element under the pointer, such as the display options of a chart, fit checkbox and radio items. They keep the menu open when toggled.

Right-click the alerts chart
function ContextMenuSettings() {
  const [showGrid, setShowGrid] = React.useState(true)
  const [showLegend, setShowLegend] = React.useState(false)
  const [range, setRange] = React.useState("7d")

  return (
    <ContextMenu>
      <ContextMenuTrigger className="flex h-36 w-full max-w-sm items-center justify-center rounded-lg border border-dashed border-border-strong text-text-secondary">
        Right-click the alerts chart
      </ContextMenuTrigger>
      <ContextMenuContent className="w-52">
        <ContextMenuCheckboxItem
          checked={showGrid}
          onCheckedChange={setShowGrid}
        >
          Show grid lines
        </ContextMenuCheckboxItem>
        <ContextMenuCheckboxItem
          checked={showLegend}
          onCheckedChange={setShowLegend}
        >
          Show legend
        </ContextMenuCheckboxItem>
        <ContextMenuSeparator />
        <ContextMenuLabel>Time range</ContextMenuLabel>
        <ContextMenuRadioGroup value={range} onValueChange={setRange}>
          <ContextMenuRadioItem value="24h">Last 24 hours</ContextMenuRadioItem>
          <ContextMenuRadioItem value="7d">Last 7 days</ContextMenuRadioItem>
          <ContextMenuRadioItem value="30d">Last 30 days</ContextMenuRadioItem>
        </ContextMenuRadioGroup>
      </ContextMenuContent>
    </ContextMenu>
  )
}

Accessibility

  • Inside the open menu, arrow keys move between items, Enter runs the focused item and Escape closes the menu. Opening it from the keyboard depends on the browser and operating system (Shift+F10 or the context menu key), which is why the actions need a visible alternative.
  • ContextMenuLabel is a visual heading and is not announced as the group’s name.

API reference

Generated from the TypeScript source. className, style and children are accepted everywhere and omitted below.

ContextMenu

ContextMenu is the menu that opens on right-click or long press over its ContextMenuTrigger, with actions for what is under the pointer. For a menu opened from a visible button, use DropdownMenu.

PropTypeDefault
actionsRefRefObject<MenuRootActions | null>

A ref to imperative actions. - `unmount`: Ends the closing phase of the menu after an externally controlled closing animation finishes. Call `preventUnmountOnClose()` in `onOpenChange` first, otherwise the menu completes closing on its own. Whether it leaves the DOM is decided by `keepMounted` on the portal. - `close`: Closes the menu imperatively when called.

-
closeParentOnEscboolean

@ignore @deprecated This prop has no effect on Context Menu.

-
defaultOpenboolean

Whether the menu is initially open.

false
disabledboolean

Whether the component should ignore user interaction.

false
highlightItemOnHoverboolean

Whether moving the pointer over items should highlight them. Disabling this prop allows CSS `:hover` to be differentiated from the `:focus` (`data-highlighted`) state.

true
loopFocusboolean

Whether to loop keyboard focus back to the first item when the end of the list is reached while using the arrow keys.

true
onItemHighlighted((highlightedItem: HTMLElement, eventDetails: MenuRootHighlightEventDetails) => void)

Callback fired when an item is highlighted or unhighlighted. Receives the highlighted item element (or `undefined` if no item is highlighted) and details containing the reason for the change, the event, and the item's text label. The `reason` can be: - `'keyboard'`: the highlight changed due to keyboard navigation. - `'pointer'`: the highlight changed due to pointer hovering. The event may be a `MouseEvent` rather than a `PointerEvent`. - `'none'`: the highlight changed for another reason, such as automatic highlighting while filtering, the item list changing, or the popup opening or closing.

-
onOpenChange((open: boolean, eventDetails: ContextMenuRootChangeEventDetails) => void)

Event handler called when the menu is opened or closed.

-
onOpenChangeComplete((open: boolean) => void)

Event handler called after any animations complete when the menu is opened or closed.

-
openboolean

Whether the menu is currently open.

-
orientation"horizontal" | "vertical"

The visual orientation of the menu. Controls whether roving focus uses up/down or left/right arrow keys.

'vertical'

ContextMenuTrigger

ContextMenuTrigger is the area that opens the menu on right-click.

PropTypeDefault
renderReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, ContextMenuTriggerState>

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

-

ContextMenuContent

ContextMenuContent is the menu popup, opened at the pointer.

PropTypeDefault
align"center" | "start" | "end"

How to align the popup relative to the specified side.

start
alignOffsetnumber | OffsetFunction

Additional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment.

4
finalFocusboolean | RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | void | HTMLElement | null)

Determines the element to focus when the menu is closed.

-
idstring

@ignore

-
renderReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuPopupState>

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

-
side"left" | "right" | "top" | "bottom" | "inline-end" | "inline-start"

Which side of the anchor element to align the popup against. May automatically change to avoid collisions.

right
sideOffsetnumber | OffsetFunction

Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment.

0

ContextMenuItem

PropTypeDefault
closeOnClickboolean

Whether to close the menu when the item is clicked.

true
disabledboolean

Whether the component should ignore user interaction.

false
idstring

@ignore

-
insetboolean

Indents the text to line up with items that have a leading icon.

-
labelstring

Overrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided.

-
nativeButtonboolean

Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`).

true
onClick((event: BaseUIEvent<MouseEvent<HTMLDivElement, MouseEvent>>) => void)

The click handler for the menu item.

-
renderReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuItemState>

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

-
variant"default" | "destructive"

`destructive` colors the item red, for delete and other risky actions.

default

ContextMenuCheckboxItem

PropTypeDefault
checkedboolean

Whether the checkbox item is currently ticked.

-
closeOnClickboolean

Whether to close the menu when the item is clicked.

false
defaultCheckedboolean

Whether the checkbox item is initially ticked.

false
disabledboolean

Whether the component should ignore user interaction.

false
idstring

@ignore

-
insetboolean

Indents the text to line up with items that have a leading icon.

-
labelstring

Overrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided.

-
nativeButtonboolean

Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`).

true
onCheckedChange((checked: boolean, eventDetails: MenuRootChangeEventDetails) => void)

Event handler called when the checkbox item is ticked or unticked.

-
onClick((event: BaseUIEvent<MouseEvent<HTMLDivElement, MouseEvent>>) => void)

The click handler for the menu item.

-
renderReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuCheckboxItemState>

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

-

ContextMenuRadioItem

PropTypeDefault
closeOnClickboolean

Whether to close the menu when the item is clicked.

false
disabledboolean

Whether the component should ignore user interaction.

false
idstring

@ignore

-
insetboolean

Indents the text to line up with items that have a leading icon.

-
labelstring

Overrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided.

-
nativeButtonboolean

Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`).

true
onClick((event: BaseUIEvent<MouseEvent<HTMLDivElement, MouseEvent>>) => void)

The click handler for the menu item.

-
renderReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuRadioItemState>

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

-
value*any

Value of the radio item. This is the value that will be set in the MenuRadioGroup when the item is selected.

-

ContextMenuLabel

ContextMenuLabel is a section heading inside the menu. It is a plain div, not linked to the items under it, because Base UI's GroupLabel throws outside a Group. When the items need that aria-labelledby association, wrap them in a ContextMenuGroup with `ContextMenu.GroupLabel` from "@base-ui/react/context-menu".

PropTypeDefault
insetboolean

Indents the text to line up with items that have a leading icon.

-

ContextMenuSeparator

PropTypeDefault
orientation"horizontal" | "vertical"

The orientation of the separator.

vertical
renderReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, SeparatorState>

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

-

ContextMenuShortcut

ContextMenuShortcut is the key hint at the right edge of an item. It is display text only: bind the keys yourself.

No props of its own. Accepts the props of the element it renders.

ContextMenuGroup

PropTypeDefault
renderReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuGroupState>

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

-

ContextMenuPortal

PropTypeDefault
containerHTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null> | null

A parent element to render the portal element into.

-
keepMountedboolean

Whether to keep the portal mounted in the DOM while the popup is hidden.

false
renderReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuPortalState>

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

-

ContextMenuSub

PropTypeDefault
actionsRefRefObject<MenuRootActions | null>

A ref to imperative actions. - `unmount`: Ends the closing phase of the menu after an externally controlled closing animation finishes. Call `preventUnmountOnClose()` in `onOpenChange` first, otherwise the menu completes closing on its own. Whether it leaves the DOM is decided by `keepMounted` on the portal. - `close`: Closes the menu imperatively when called.

-
closeParentOnEscboolean

When in a submenu, determines whether pressing the Escape key closes the entire menu, or only the current child menu.

false
defaultOpenboolean

Whether the menu is initially open.

false
disabledboolean

Whether the component should ignore user interaction.

false
highlightItemOnHoverboolean

Whether moving the pointer over items should highlight them. Disabling this prop allows CSS `:hover` to be differentiated from the `:focus` (`data-highlighted`) state.

true
loopFocusboolean

Whether to loop keyboard focus back to the first item when the end of the list is reached while using the arrow keys.

true
onItemHighlighted((highlightedItem: HTMLElement, eventDetails: MenuRootHighlightEventDetails) => void)

Callback fired when an item is highlighted or unhighlighted. Receives the highlighted item element (or `undefined` if no item is highlighted) and details containing the reason for the change, the event, and the item's text label. The `reason` can be: - `'keyboard'`: the highlight changed due to keyboard navigation. - `'pointer'`: the highlight changed due to pointer hovering. The event may be a `MouseEvent` rather than a `PointerEvent`. - `'none'`: the highlight changed for another reason, such as automatic highlighting while filtering, the item list changing, or the popup opening or closing.

-
onOpenChange((open: boolean, eventDetails: MenuRootChangeEventDetails) => void)

Event handler called when the menu is opened or closed.

-
onOpenChangeComplete((open: boolean) => void)

Event handler called after any animations complete when the menu is opened or closed.

-
openboolean

Whether the menu is currently open.

-
orientation"horizontal" | "vertical"

The visual orientation of the menu. Controls whether roving focus uses up/down or left/right arrow keys.

'vertical'

ContextMenuSubContent

PropTypeDefault
align"center" | "start" | "end"

How to align the popup relative to the specified side.

start
alignOffsetnumber | OffsetFunction

Additional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment.

4
finalFocusboolean | RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | void | HTMLElement | null)

Determines the element to focus when the menu is closed.

-
idstring

@ignore

-
renderReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuPopupState>

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

-
side"left" | "right" | "top" | "bottom" | "inline-end" | "inline-start"

Which side of the anchor element to align the popup against. May automatically change to avoid collisions.

right
sideOffsetnumber | OffsetFunction

Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment.

0

ContextMenuSubTrigger

PropTypeDefault
closeDelaynumber

How long to wait before closing the menu that was opened on hover. Specified in milliseconds.

0
delaynumber

How long to wait before the menu may be opened on hover. Specified in milliseconds.

100
disabledboolean

Whether the component should ignore user interaction.

false
idstring

@ignore

-
insetboolean

Indents the text to line up with items that have a leading icon.

-
labelstring

Overrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided.

-
nativeButtonboolean

Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`).

true
onClick((event: BaseUIEvent<MouseEvent<HTMLDivElement, MouseEvent>>) => void)-
openOnHoverboolean

Whether the menu should also open when the trigger is hovered.

true
renderReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuSubmenuTriggerState>

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

-

ContextMenuRadioGroup

PropTypeDefault
defaultValueany

The uncontrolled value of the radio item that should be initially selected.

-
disabledboolean

Whether the component should ignore user interaction.

false
onValueChange((value: any, eventDetails: MenuRootChangeEventDetails) => void)

Function called when the selected value changes.

-
renderReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuRadioGroupState>

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

-
valueany

The controlled value of the radio item that should be currently selected.

-