Raft Design System

Components · Overlays

ContextMenu

The menu of actions on one thing that opens at the pointer on right-click, or on press and hold on a touch screen.

StableAlso called Right-click menu
import { ContextMenu } from "@botiverse/raft-ui";

A context menu is the menu of actions on one thing that opens where people right-click on desktop, or where they press and hold on a touch screen. It is the same menu that the thing's ⋯ button opens as a DropdownMenu: the same items, in the same order.

context-menu/context-menu

When to use

SituationUse
Right-click or press and hold on a message, a channel, a task or an agentContextMenu
The same actions, from a visible buttonDropdownMenu
Acting on something on a phoneContextMenu on press and hold, never a bottom sheet
A value submitted with a formSelect

Which overlay fits which job is in Choosing a component.

Anatomy

ContextMenu has the same parts as DropdownMenu, with the ContextMenu prefix: ContextMenuItem, ContextMenuItemLabel, ContextMenuShortcut, ContextMenuRadioGroup, ContextMenuRadioItem, ContextMenuCheckboxItem, ContextMenuLabel, ContextMenuSeparator, ContextMenuSubmenu, ContextMenuSubmenuTrigger, ContextMenuFooter, ContextMenuPinnedFooter (experimental, from @botiverse/raft-ui/wip), ContextMenuPortal and ContextMenuPopup. They look and behave as described in DropdownMenu. It has no item count part.

  • ContextMenu: the root. Required.
  • ContextMenuTrigger: the area that opens the menu: the message, the row, the card. Required.
  • ContextMenuContent: the menu, portaled and opened at the pointer. Its items scroll inside it when they don't fit. Required.

States

The highlight, the current value and disabled items are the same as in DropdownMenu.

Behavior

  • The menu opens at the pointer, or at the finger after press and hold, with its top-left corner there.
  • When it would cross the window edge, it shifts to stay at least 8px inside the window instead of flipping.
  • A submenu opens beside its item as in a DropdownMenu: to the end side, its first item level with the item, flipping to the other side of the item where only that side has room, and sliding along the window's edge, still level with its item, where neither side has. It never drops above or below its item.
  • Where a shell keeps part of the window for itself (--raft-frame-inset-top, --raft-frame-inset-start, --raft-frame-inset-end), it stays 8px inside that part instead of inside the window's edge. The start and end insets are the corners of the top band, so they count only for a menu opened at a point in the band; one opened under it keeps its 8px from the window's edge, as on the web (Page structure). The web sets none.
  • It stays where it opened while the page behind it scrolls.
  • It is at least 240px wide and as wide as its longest item, never wider than the room the window leaves it; an item longer than that wraps or is cut, as in a DropdownMenu.
  • A menu longer than the room it has scrolls inside it, keeps the highlighted item in view, fades at its cut edges and keeps a ContextMenuPinnedFooter below its items, as a DropdownMenu does.
  • Choosing an action or a radio item closes the menu, and a checkbox item leaves it open, as in DropdownMenu. Esc and a click outside close it too.
  • An item is as tall as its content, with constant padding, as in a DropdownMenu (Forms).

Content

The item labels follow DropdownMenu: sentence case, and the same words as the dialog an item opens.

Accessibility

  • ContextMenu wraps the Base UI ContextMenu. It provides the menu role, the item roles, arrow keys, typeahead and Esc.
  • Name the menu after the thing it acts on with aria-label on ContextMenuContent ("Message from Wug").
  • Right-click and press and hold are not available from the keyboard. Offer every action of a context menu from a visible control as well, usually the ⋯ DropdownMenu.

Rules

  • Build the context menu and the ⋯ menu from one list of items, so they never drift apart.
  • Use the same menu on desktop and on a phone. Don't build a separate phone sheet for the same actions.
  • Follow the rules of DropdownMenu: no color on destructive items, and the quick reactions first in the message menu.

API

ContextMenu

PropTypeDefault
actionsRefA 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.RefObject<MenuRootActions | null>
childrenReactNode
closeParentOnEscDeprecated. boolean
defaultOpenWhether the menu is initially open.booleanfalse
disabledWhether the component should ignore user interaction.booleanfalse
highlightItemOnHoverWhether moving the pointer over items should highlight them. Disabling this prop allows CSS `:hover` to be differentiated from the `:focus` (`data-highlighted`) state.booleantrue
loopFocusWhether to loop keyboard focus back to the first item when the end of the list is reached while using the arrow keys.booleantrue
onItemHighlightedCallback 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.(highlightedItem: HTMLElement | undefined, eventDetails: MenuRoot.HighlightEventDetails) => void
onOpenChangeEvent handler called when the menu is opened or closed.(open: boolean, eventDetails: BaseContextMenu.Root.ChangeEventDetails) => void
onOpenChangeCompleteEvent handler called after any animations complete when the menu is opened or closed.(open: boolean) => void
openWhether the menu is currently open.boolean
orientationThe visual orientation of the menu. Controls whether roving focus uses up/down or left/right arrow keys.MenuRootOrientation'vertical'

ContextMenuCheckboxItem

PropTypeDefault
checkedWhether the checkbox item is currently ticked.boolean
closeOnClickWhether to close the menu when the item is clicked.booleanfalse
defaultCheckedWhether the checkbox item is initially ticked.booleanfalse
disabledWhether the component should ignore user interaction.booleanfalse
idstring
labelOverrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided.string
nativeButtonWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `true` if the rendered element is a native button.booleanfalse
onCheckedChangeEvent handler called when the checkbox item is ticked or unticked.(checked: boolean, eventDetails: BaseContextMenu.CheckboxItem.ChangeEventDetails) => void
onClickThe click handler for the menu item.(event: BaseUIEvent<MouseEvent<HTMLDivElement, MouseEvent>>) => void
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

ContextMenuCheckboxItemIndicator

PropTypeDefault
keepMountedWhether to keep the HTML element in the DOM when the checkbox item is not checked.booleanfalse
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

ContextMenuContent

The menu: its items scroll inside the room the positioner leaves it (`--available-height`), with their edges faded, and a `ContextMenuPinnedFooter` stays below them. `className` is the popup's (its width); the items are laid out in a column inside the scroller.
PropTypeDefault
alignOffsetOffset along the alignment axis in pixels. A submenu's takes back the popup's 4px inset, so that its first item is level with its item.number | OffsetFunction0 (-4 in a submenu)
collisionAvoidanceHow the popup keeps inside the window: the menu shifts and never flips; a submenu flips to the other side of its item, never above or below it.CollisionAvoidanceCONTENT_COLLISION_AVOIDANCE (SUBMENU_POSITION.collisionAvoidance in a submenu)
collisionPaddingAdditional space to maintain from the edge of the collision boundary: 8px from the window's edge, and 8px from a frame inset where a shell keeps one. A value you pass is kept as it is.Padding8
portalPropsContextMenuPortalProps
sideWhich side of its anchor the popup goes on: the menu to the right of the point, a submenu beside its item, to the end side.Side"right" ("inline-end" in a submenu)
sideOffsetDistance between the anchor and the popup in pixels.number | OffsetFunction0 (4 in a submenu)
stickyKeep the popup in the window even when its anchor leaves it. A submenu is sticky, so that where it fits on neither side of its item it slides along the window's edge.booleanfalse (true in a submenu)
alignHow to align the popup relative to the specified side.Align"start"
anchorAn element to position the popup against. By default, root context menus are positioned at the pointer, and submenus are positioned against their trigger.Element | VirtualElement | RefObject<Element | null> | (() => Element | VirtualElement | null)
arrowPaddingMinimum distance to maintain between the arrow and the edges of the popup.number
childrenReactNode
collisionBoundaryAn element or a rectangle that delimits the area that the popup is confined to.Boundary'clipping-ancestors'
disableAnchorTrackingWhether to disable the popup from tracking any layout shift of its positioning anchor.booleanfalse
finalFocusDetermines the element to focus when the menu is closed.boolean | RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | HTMLElement | null | void)
idstring
positionMethodDeprecated. "fixed" | "absolute""fixed"
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

ContextMenuFooter

Takes the attributes of its element.

ContextMenuItem

PropTypeDefault
closeOnClickWhether to close the menu when the item is clicked.booleantrue
disabledWhether the component should ignore user interaction.booleanfalse
idstring
labelOverrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided.string
nativeButtonWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `true` if the rendered element is a native button.booleanfalse
onClickThe click handler for the menu item.(event: BaseUIEvent<MouseEvent<HTMLDivElement, MouseEvent>>) => void
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

ContextMenuItemLabel, ContextMenuLabel, ContextMenuPopup

Takes the attributes of its element.

ContextMenuPortal

PropTypeDefault
containerA parent element to render the portal element into.HTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null>
keepMountedWhether to keep the portal mounted in the DOM while the popup is hidden.booleanfalse
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

ContextMenuRadioGroup

PropTypeDefault
childrenThe content of the component.ReactNode
defaultValueThe uncontrolled value of the radio item that should be initially selected.any
disabledWhether the component should ignore user interaction.booleanfalse
onValueChangeFunction called when the selected value changes.(value: any, eventDetails: BaseContextMenu.RadioGroup.ChangeEventDetails) => void
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)
valueThe controlled value of the radio item that should be currently selected.any

Also takes the attributes of its element.

ContextMenuRadioItem

PropTypeDefault
closeOnClickWhether to close the menu when the item is clicked.booleantrue
disabledWhether the component should ignore user interaction.booleanfalse
idstring
labelOverrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided.string
nativeButtonWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `true` if the rendered element is a native button.booleanfalse
onClickThe click handler for the menu item.(event: BaseUIEvent<MouseEvent<HTMLDivElement, MouseEvent>>) => void
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)
value (required)Value of the radio item. This is the value that will be set in the MenuRadioGroup when the item is selected.any

Also takes the attributes of its element.

ContextMenuRadioItemIndicator

PropTypeDefault
keepMountedWhether to keep the HTML element in the DOM when the radio item is inactive.booleanfalse
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

ContextMenuSeparator

PropTypeDefault
orientationThe orientation of the separator.Orientation'horizontal'
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

ContextMenuShortcut

Takes the attributes of its element.

ContextMenuSubmenu

PropTypeDefault
actionsRefA 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.RefObject<MenuRootActions | null>
childrenThe content of the submenu.ReactNode
closeParentOnEscWhen in a submenu, determines whether pressing the Escape key closes the entire menu, or only the current child menu.booleanfalse
defaultOpenWhether the menu is initially open.booleanfalse
disabledWhether the component should ignore user interaction.booleanfalse
highlightItemOnHoverWhether moving the pointer over items should highlight them. Disabling this prop allows CSS `:hover` to be differentiated from the `:focus` (`data-highlighted`) state.booleantrue
loopFocusWhether to loop keyboard focus back to the first item when the end of the list is reached while using the arrow keys.booleantrue
onItemHighlightedCallback 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.(highlightedItem: HTMLElement | undefined, eventDetails: MenuRoot.HighlightEventDetails) => void
onOpenChangeEvent handler called when the menu is opened or closed.(open: boolean, eventDetails: BaseContextMenu.SubmenuRoot.ChangeEventDetails) => void
onOpenChangeCompleteEvent handler called after any animations complete when the menu is opened or closed.(open: boolean) => void
openWhether the menu is currently open.boolean
orientationThe visual orientation of the menu. Controls whether roving focus uses up/down or left/right arrow keys.MenuRootOrientation'vertical'

ContextMenuSubmenuTrigger

PropTypeDefault
closeDelayHow long to wait before closing the menu that was opened on hover. Specified in milliseconds.number0
delayHow long to wait before the menu may be opened on hover. Specified in milliseconds.number100
disabledWhether the component should ignore user interaction.booleanfalse
idstring
labelOverrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided.string
nativeButtonWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `true` if the rendered element is a native button.booleanfalse
onClick(event: BaseUIEvent<MouseEvent<HTMLDivElement, MouseEvent>>) => void
openOnHoverWhether the menu should also open when the trigger is hovered.booleantrue
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

ContextMenuTrigger

PropTypeDefault
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

ContextMenuPinnedFooter

ExperimentalItems kept below the menu's scrolling items, after a separator, however long the list is: an action on the whole list. Write it among the items, as the menu's last child; it is drawn below the scroller and comes last for the arrow keys too.

Takes the attributes of its element.

Changes

  • 0.106.2 · FixedContextMenu: a submenu opens beside its item as a DropdownMenu's does ([ContextMenu › Behavior](https://raft-design.botiverse.build/components/context-menu/#behavior)): a ContextMenuContent inside a ContextMenuSubmenu takes the same position as a DropdownMenu submenu: the end side of its item (inline-end, so it follows the writing direction), its first item level with it, flipped to the other side where only that side has room, slid along the window's edge where neither has.
  • 0.106.0 · Design SystemLong menus and popovers scroll inside the room they have ([DropdownMenu › Long menus](https://raft-design.botiverse.build/components/dropdown-menu/#long-menus), [Popover › A long popover](https://raft-design.botiverse.build/components/popover/#a-long-popover), [Elevation › Popups](https://raft-design.botiverse.build/foundations/elevation/#popups)): a popup is capped at the space left between its trigger and the window's edge, and what doesn't fit scrolls inside it, never clipped. The highlighted item of a menu stays in view and clear of the fade; the cut edges fade over up to 24px; an action on the whole list stays below the items (DropdownMenuPinnedFooter); a popover keeps its header and buttons and scrolls its body. Changed in this release: DropdownMenuContent, ContextMenuContent, PopoverContent, PopoverBody, PreviewCardContent, PreviewCardBody.
  • 0.106.0 · AddedDropdownMenuPinnedFooter and ContextMenuPinnedFooter (work in progress, from @botiverse/raft-ui/wip): items kept below a long menu's scrolling items, after a separator, at any scroll. Write it as the menu's last child; the arrow keys reach it after the last item.
  • 0.104.0 · Design SystemTooltips on a touch screen, and who owns a long press ([Tooltip › Touch screens](https://raft-design.botiverse.build/components/tooltip/#touch-screens), [States › Dragging on a touch screen](https://raft-design.botiverse.build/foundations/states/#dragging-on-a-touch-screen)): on a touch screen a tooltip opens on a press and hold, on a disabled button too (focusableWhenDisabled); a long press belongs to the layer that owns it (a long-press menu, a touch drag, a link), and the tooltip takes it only when nothing else wants it. Changed in this release: TooltipTrigger, and the long-press owners ContextMenuTrigger, SortableTabsTab, SortableComposerAttachment and TaskDraggable.
  • 0.103.4 · FixedFrame insets (Popover, DropdownMenu, ContextMenu, Select, Combobox, ComboboxSelect, Tooltip, PreviewCard, CoachMark, NotificationCenter, MobileNavServerPopover, ToastPositioner, ComposerSuggestionList): --raft-frame-inset-start and --raft-frame-inset-end are the corners of the window's top band (--raft-frame-inset-top), not columns down its sides. A popup anchored under the band keeps its own padding from the window's edge; only one anchored in the band (a title-bar control, or a context menu opened in the bar) keeps 8px clear of a corner.
  • 0.103.3 · FixedFrame insets (useFrameInsets, frameCollisionBoundary): opening a positioned popup (Popover, DropdownMenu, Select, Combobox, ComboboxSelect, ContextMenu, Tooltip, PreviewCard, NotificationCenter, MobileNavServerPopover, ComposerSuggestionList) no longer stalls the main thread when --raft-frame-inset-* holds a calc() or env(titlebar-area-*) expression on an overlay title bar.
  • 0.1.0 · AddedExtended Lightbox with thumbnail strip and related chrome parts; added ComboboxTriggerCount and ContextMenuPopup.
  • 0.0.20 · AddedAdded ContextMenuItemLabel, ContextMenuFooter, DropdownMenuItemLabel, and DropdownMenuFooter for truncatable menu labels and structured footer content.