Context Menu
ContextMenu opens a menu at the pointer when someone right-clicks an area, or long-presses it on a touch screen.
<ContextMenu> <ContextMenuTrigger> <Surface>The area you right-click</Surface> </ContextMenuTrigger> <ContextMenuContent> <MenuItem>Duplicate</MenuItem> <MenuItem tone="destructive">Delete</MenuItem> </ContextMenuContent></ContextMenu>Use a ContextMenu for shortcuts to actions on the thing under the pointer, such as renaming, duplicating or deleting a file. A right-click is hard to discover, so make every action in a context menu available somewhere else too, such as a button or a Menu. To open a menu from a visible button, use Menu.
You build the menu from the same parts as a Menu: MenuItem, MenuGroup, MenuLabel, MenuCheckboxItem, MenuRadioGroup, MenuSub and their children. The panel and the rows look exactly like a Menu.
Wrap the area in ContextMenuTrigger. It catches the right-click and the long press, and it stops the browser's own menu from opening. It adds no fill, border or cursor, so the area looks the same as before. To make an existing element the area, pass it to render.
The panel opens with its corner at the point you clicked and moves as needed to stay inside the window, so there are no placement props. size sets the size of the panel and its rows.
The keyboard works as it does in a Menu: the arrow keys move through the rows, Enter chooses a row, typing a letter jumps to a matching row, and Escape closes the panel.
The behaviour comes from Base UI's ContextMenu.
- Menu when the actions open from a visible button.
Topics
Every symbol this component exports, grouped by the job it does. Each one carries the props it declares; each one has an anchor of its own.
Catching the right-click
ContextMenu
ContextMenu opens a menu at the pointer when someone right-clicks an area, or long-presses it on a touch screen.
| Prop | Type | What it does |
|---|---|---|
children? | React.ReactNode | A <ContextMenuTrigger> and a <ContextMenuContent>. |
defaultOpen? | boolean | Uncontrolled starting state. |
onOpenChange? | (open: boolean, details: MenuOpenChangeDetails) => void | Fires on every open and close, including the dismissals the menu handles itself. |
open? | boolean | Controls whether the menu is open. |
size? | "1" | "2" | "3" | "4" | The size step of the menu. |
ContextMenuTrigger
The area where a right-click or a long press opens the menu. It adds nothing visible, so wrap it around the content people already see
| Prop | Type | What it does |
|---|---|---|
children? | React.ReactNode | The area that answers a right-click. |
className? | string | Your classes, appended to the ones this component resolves rather than replacing them. |
render? | RenderElement | Renders the area as an element you already have, such as a canvas or a row. |
style? | React.CSSProperties | Inline styles, merged last, so your value wins over anything the component resolved. |
It also takes every prop of a <div>.
Showing the panel
ContextMenuContent
The panel, which opens at the point that was clicked. Put MenuItem and the other menu parts inside it
| Prop | Type | What it does |
|---|---|---|
children? | React.ReactNode | The rows of the menu. |
className? | string | Your classes, appended to the ones this component resolves rather than replacing them. |
style? | React.CSSProperties | Inline styles, merged last, so your value wins over anything the component resolved. |
ContextMenuItemand a second set of parts — A context menu's rows are the same components as a menu's rows, so a second set of names would only duplicate them. UseMenuItem,MenuGroup,MenuSuband the other menu parts directly.side,alignand an offset — The panel's corner always opens at the point that was clicked, and the window decides which corner fits. There is nothing left for these props to choose.- A style for the trigger area — A right-click happens over content that is already visible, so the trigger adds no fill, border or cursor. Style the element inside it as you normally would.
- Opening on a left click — A menu that opens on a left click is a
Menuwith a visible trigger. One panel that answered two different gestures would be hard for people to predict.