Context Menu
A menu that users open with a right-click or a long press on an area.
A context menu opens at the pointer when users right-click an area or long-press it on a touch screen. It offers the actions that belong to that area, so users act on an entry without moving to a toolbar. An item runs an action, toggles an option, selects one of several values or opens a submenu. A ContextMenuLabel needs a ContextMenuGroup or a ContextMenuRadioGroup around it and names that group for assistive technology.
Usage
Examples
Destructive
Cancelling an order or discarding a stock count often cannot be reversed. Inside the menu, variant set to "destructive" signals that consequence, so users recognize the risk of an item before they choose it.
With Icons
Menus that users open many times a day benefit from a visual anchor for each action. An icon lets users find a familiar action at a glance, while the label still names the outcome and keeps the action clear wherever the icon alone would leave doubt.
Inset
Not every action has a fitting icon. With inset, an item without one and the label of its group begin their text where the items with an icon begin theirs, so users scan every label along one line.
With Shortcuts
Frequent actions deserve a faster route than the menu. Shown in the menu, a shortcut teaches users the key combination of an item, so next time they trigger the action straight from the keyboard. Responding to those keys remains the task of the application, since the shortcut only displays them.
With Separator
When a menu mixes actions of different kinds, such as viewing an entry and changing it, users take it in faster in sections. Each separator sets one section apart from the next and marks the boundary for assistive technology as well.
With Groups
Some menus gather actions that concern different things, such as a product and the supplier that delivers it. A ContextMenuGroup with a ContextMenuLabel gives each set a name, so users know what its actions refer to, and assistive technology names the group the same way.
With Submenu
Related actions, such as the formats in which a list can be exported, would lengthen the menu if each stood on its own. Collected in a submenu, they appear only once users rest the pointer on its trigger, click it or reach it with the keyboard, and the main menu stays short.
With Checkbox Items
Several options can apply to one entry at the same time, and users switch each of them on or off independently. The menu stays open after every change, so users adjust them all in one pass, and through defaultChecked an option starts out switched on.
With Radio Items
Some settings hold exactly one value out of several, such as the unit in which a stock list shows its quantities. Selecting a radio item replaces the previous choice, and the menu stays open so users see the result. Through defaultValue, the radio group starts with a value already selected.
Top Side
Areas close to the lower edge of a screen, such as a bar with totals, leave little room below the pointer. Set to "top", side opens the menu above the pointer, where the space is.
Bottom Side
Headers of tables and pages sit at the top of a screen and lead into the content below them. Through side set to "bottom", the menu opens beneath the spot users clicked, toward the content its actions concern.
Left Side
Along the right edge of a screen, such as in a side panel, little room remains to the right of the pointer. Set to "left", side opens the menu on the other side of the pointer, toward the middle of the screen.
Right Side
Most areas offer enough room to the right of the pointer, where the menu opens by default. The value "right" of side states this placement explicitly.
Controlled
A list whose figures update live should stay still while users choose an action for one of its entries. With open and onOpenChange, the application holds the open state of the menu, so it knows when to pause the updates and can close the menu itself once the entry changes.
Controlled Submenu
Options inside a submenu sometimes depend on data worth loading only on demand, such as the warehouses that currently hold stock of a product. Because ContextMenuSub accepts open and onOpenChange, the application knows when users open the submenu and fetches that data at the right moment.
Controlled Checkbox Item
An option in the menu often mirrors a setting that also appears elsewhere, such as a flag in the detail view of an order. Once the item receives checked and onCheckedChange, the application holds its state, so both places always agree and the change can be saved at once.
Controlled Radio Group
Choices made in the menu can take effect right away, such as the priority that decides when the warehouse picks an order. Through value and onValueChange on the radio group, the application holds the selected value and passes each new choice on without delay.
Disabled
While an entry is locked, for example because another user is editing it, its actions do not apply. Set on the context menu, disabled keeps it from opening, and a right-click then brings up the menu of the browser instead.
Disabled Item
Individual actions may not apply to an entry yet, such as booking a delivery that has not arrived. The item keeps its place with disabled, so users learn that the action exists, yet it cannot be chosen and assistive technology announces it as unavailable.
Disabled Submenu Trigger
Sometimes a whole set of related actions is out of reach, such as exporting a report before its figures are complete. A submenu trigger with disabled no longer opens its submenu, while users still see that these actions exist.
Disabled Radio Group
After an order has been released to the warehouse, its delivery method is fixed. With disabled on the radio group, users still see the value that applies, but none of the radio items accepts another choice.
API Reference
ContextMenu
| Prop | open |
|---|---|
| Type | boolean |
| Default | – |
| Description | Sets whether the context menu is open and makes it controlled. Update it in onOpenChange. |
| Prop | defaultOpen |
|---|---|
| Type | boolean |
| Default | false |
| Description | Opens the context menu at first render. The context menu then tracks its open state itself. |
| Prop | onOpenChange |
|---|---|
| Type | (open: boolean, eventDetails: ContextMenuRootChangeEventDetails) => void |
| Default | – |
| Description | Runs when users open or close the context menu. It receives the open state that follows. |
| Prop | onOpenChangeComplete |
|---|---|
| Type | (open: boolean) => void |
| Default | – |
| Description | Runs after the context menu has opened or closed and its animations have finished. |
| Prop | disabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from opening the context menu. A right-click on the trigger then opens the menu of the browser. |
| Prop | loopFocus |
|---|---|
| Type | boolean |
| Default | true |
| Description | Moves the highlight back to the first item when users pass the last one with the arrow keys. |
| Prop | highlightItemOnHover |
|---|---|
| Type | boolean |
| Default | true |
| Description | Highlights an item when the pointer moves over it. Set it to false to style hover apart from the keyboard highlight. |
| Prop | orientation |
|---|---|
| Type | "horizontal" | "vertical" |
| Default | "vertical" |
| Description | Sets whether the up and down or the left and right arrow keys move through the items. |
| Prop | actionsRef |
|---|---|
| Type | RefObject<ContextMenuRootActions | null> |
| Default | – |
| Description | Receives the functions close and unmount. close closes the context menu from code, and unmount removes it from the DOM after a closing animation that the code controls itself. |
ContextMenuPortal
| Prop | keepMounted |
|---|---|
| Type | boolean |
| Default | false |
| Description | Keeps the portal in the DOM while the context menu is closed. |
| Prop | container |
|---|---|
| Type | HTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null> | null |
| Default | – |
| Description | Sets the element that the portal is rendered into. Without it, the portal is appended to the body. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the portal with another component. |
ContextMenuTrigger
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the trigger with another component. |
ContextMenuContent
| Prop | side |
|---|---|
| Type | "top" | "bottom" | "left" | "right" | "inline-end" | "inline-start" |
| Default | "right" |
| Description | Sets the side of the pointer that the menu opens on. inline-end and inline-start follow the text direction. Near the edge of the viewport, the menu shifts to stay visible. |
| Prop | align |
|---|---|
| Type | "start" | "center" | "end" |
| Default | "start" |
| Description | Sets whether the start, the center or the end of the menu lines up with the pointer. |
| Prop | sideOffset |
|---|---|
| Type | number | OffsetFunction |
| Default | 0 |
| Description | Sets the distance between the pointer and the menu in pixels. A function receives the side, the alignment and the dimensions of the anchor and the positioner, and returns the distance. |
| Prop | alignOffset |
|---|---|
| Type | number | OffsetFunction |
| Default | 4 |
| Description | Moves the menu along the alignment axis by a number of pixels. A function receives the same data as the one for sideOffset and returns the offset. |
| Prop | finalFocus |
|---|---|
| Type | boolean | RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | HTMLElement | null | void) |
| Default | – |
| Description | Determines the element that receives focus when the context menu closes. Without it, focus returns to the trigger or to the element that had focus before. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the content with another component. |
ContextMenuGroup
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the group with another component. |
ContextMenuLabel
| Prop | inset |
|---|---|
| Type | boolean |
| Default | – |
| Description | Indents the label, so that its text lines up with the text of items that have an icon. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the label with another component. |
ContextMenuItem
| Prop | variant |
|---|---|
| Type | "default" | "destructive" |
| Default | "default" |
| Description | Sets the color of the item. Destructive colors the text and the icon of an action that deletes something or cannot be undone. |
| Prop | inset |
|---|---|
| Type | boolean |
| Default | – |
| Description | Indents the item, so that its text lines up with the text of items that have an icon. |
| Prop | onClick |
|---|---|
| Type | (event: BaseUIEvent<MouseEvent<HTMLDivElement>>) => void |
| Default | – |
| Description | Runs when users choose the item. |
| Prop | closeOnClick |
|---|---|
| Type | boolean |
| Default | true |
| Description | Closes the context menu when users choose the item. |
| Prop | disabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from choosing the item and exposes the disabled state to assistive technology. |
| Prop | label |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the text that finds the item when users type its first letters. Without it, the text of the item is used. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the item with another component. |
| Prop | nativeButton |
|---|---|
| Type | boolean |
| Default | false |
| Description | Declares whether the rendered element is a native button. Set it to true when render returns a button. |
ContextMenuSub
| Prop | open |
|---|---|
| Type | boolean |
| Default | – |
| Description | Sets whether the submenu is open and makes it controlled. Update it in onOpenChange. |
| Prop | defaultOpen |
|---|---|
| Type | boolean |
| Default | false |
| Description | Opens the submenu at first render. The submenu then tracks its open state itself. |
| Prop | onOpenChange |
|---|---|
| Type | (open: boolean, eventDetails: MenuSubmenuRootChangeEventDetails) => void |
| Default | – |
| Description | Runs when users open or close the submenu. It receives the open state that follows. |
| Prop | onOpenChangeComplete |
|---|---|
| Type | (open: boolean) => void |
| Default | – |
| Description | Runs after the submenu has opened or closed and its animations have finished. |
| Prop | disabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from opening the submenu. |
| Prop | closeParentOnEsc |
|---|---|
| Type | boolean |
| Default | false |
| Description | Closes the whole context menu when users press Escape in the submenu. Without it, Escape closes only the submenu. |
| Prop | loopFocus |
|---|---|
| Type | boolean |
| Default | true |
| Description | Moves the highlight back to the first item of the submenu when users pass the last one with the arrow keys. |
| Prop | highlightItemOnHover |
|---|---|
| Type | boolean |
| Default | true |
| Description | Highlights an item of the submenu when the pointer moves over it. Set it to false to style hover apart from the keyboard highlight. |
| Prop | orientation |
|---|---|
| Type | "horizontal" | "vertical" |
| Default | "vertical" |
| Description | Sets whether the up and down or the left and right arrow keys move through the items of the submenu. |
| Prop | actionsRef |
|---|---|
| Type | RefObject<MenuRootActions | null> |
| Default | – |
| Description | Receives the functions close and unmount. close closes the submenu from code, and unmount removes it from the DOM after a closing animation that the code controls itself. |
ContextMenuSubTrigger
| Prop | inset |
|---|---|
| Type | boolean |
| Default | – |
| Description | Indents the submenu trigger, so that its text lines up with the text of items that have an icon. |
| Prop | onClick |
|---|---|
| Type | (event: BaseUIEvent<MouseEvent<HTMLDivElement>>) => void |
| Default | – |
| Description | Runs when users click the submenu trigger. |
| Prop | openOnHover |
|---|---|
| Type | boolean |
| Default | true |
| Description | Opens the submenu when the pointer rests on the submenu trigger. With false, users open the submenu with a click or with the keyboard. |
| Prop | delay |
|---|---|
| Type | number |
| Default | 100 |
| Description | Sets how many milliseconds the pointer rests on the submenu trigger before the submenu opens. It has no effect without openOnHover. |
| Prop | closeDelay |
|---|---|
| Type | number |
| Default | 0 |
| Description | Sets how many milliseconds a submenu that was opened by hover waits before it closes. It has no effect without openOnHover. |
| Prop | disabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from opening the submenu with this trigger. |
| Prop | label |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the text that finds the submenu trigger when users type its first letters. Without it, the text of the submenu trigger is used. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the submenu trigger with another component. |
| Prop | nativeButton |
|---|---|
| Type | boolean |
| Default | false |
| Description | Declares whether the rendered element is a native button. Set it to true when render returns a button. |
ContextMenuSubContent
| Prop | side |
|---|---|
| Type | "top" | "bottom" | "left" | "right" | "inline-end" | "inline-start" |
| Default | "right" |
| Description | Sets the side of the submenu trigger that the submenu opens on. inline-end and inline-start follow the text direction. The submenu switches to the opposite side when it doesn't fit. |
| Prop | align |
|---|---|
| Type | "start" | "center" | "end" |
| Default | "start" |
| Description | Sets whether the start, the center or the end of the submenu lines up with the submenu trigger. |
| Prop | sideOffset |
|---|---|
| Type | number | OffsetFunction |
| Default | 0 |
| Description | Sets the distance between the submenu trigger and the submenu in pixels. A function receives the side, the alignment and the dimensions of the anchor and the positioner, and returns the distance. |
| Prop | alignOffset |
|---|---|
| Type | number | OffsetFunction |
| Default | 4 |
| Description | Moves the submenu along the alignment axis by a number of pixels. A function receives the same data as the one for sideOffset and returns the offset. |
| Prop | finalFocus |
|---|---|
| Type | boolean | RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | HTMLElement | null | void) |
| Default | – |
| Description | Determines the element that receives focus when the submenu closes. Without it, focus returns to the trigger or to the element that had focus before. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the submenu content with another component. |
ContextMenuCheckboxItem
| Prop | checked |
|---|---|
| Type | boolean |
| Default | – |
| Description | Sets whether the item is checked and makes it controlled. Update it in onCheckedChange. |
| Prop | defaultChecked |
|---|---|
| Type | boolean |
| Default | false |
| Description | Checks the item at first render. The item then tracks its state itself. |
| Prop | onCheckedChange |
|---|---|
| Type | (checked: boolean, eventDetails: MenuCheckboxItemChangeEventDetails) => void |
| Default | – |
| Description | Runs when users check or uncheck the item. It receives the state that follows. |
| Prop | inset |
|---|---|
| Type | boolean |
| Default | – |
| Description | Indents the item, so that its text lines up with the text of items that have an icon. |
| Prop | onClick |
|---|---|
| Type | (event: BaseUIEvent<MouseEvent<HTMLDivElement>>) => void |
| Default | – |
| Description | Runs when users click the item. |
| Prop | closeOnClick |
|---|---|
| Type | boolean |
| Default | false |
| Description | Closes the context menu when users check or uncheck the item. |
| Prop | disabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from checking or unchecking the item and exposes the disabled state to assistive technology. |
| Prop | label |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the text that finds the item when users type its first letters. Without it, the text of the item is used. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the item with another component. |
| Prop | nativeButton |
|---|---|
| Type | boolean |
| Default | false |
| Description | Declares whether the rendered element is a native button. Set it to true when render returns a button. |
ContextMenuRadioGroup
| Prop | value |
|---|---|
| Type | any |
| Default | – |
| Description | Sets the value of the selected radio item and makes the radio group controlled. Update it in onValueChange. |
| Prop | defaultValue |
|---|---|
| Type | any |
| Default | – |
| Description | Sets the value of the radio item that is selected at first render. The radio group then tracks the selection itself. |
| Prop | onValueChange |
|---|---|
| Type | (value: any, eventDetails: MenuRadioGroupChangeEventDetails) => void |
| Default | – |
| Description | Runs when users select a radio item. It receives the value of that item. |
| Prop | disabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from selecting any radio item of the group. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the radio group with another component. |
ContextMenuRadioItem
| Prop | value |
|---|---|
| Type | any |
| Default | – |
| Description | Sets the value that the radio group holds while this item is selected. Every radio item needs one. |
| Prop | inset |
|---|---|
| Type | boolean |
| Default | – |
| Description | Indents the item, so that its text lines up with the text of items that have an icon. |
| Prop | onClick |
|---|---|
| Type | (event: BaseUIEvent<MouseEvent<HTMLDivElement>>) => void |
| Default | – |
| Description | Runs when users click the item. |
| Prop | closeOnClick |
|---|---|
| Type | boolean |
| Default | false |
| Description | Closes the context menu when users select the item. |
| Prop | disabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from selecting the item and exposes the disabled state to assistive technology. |
| Prop | label |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the text that finds the item when users type its first letters. Without it, the text of the item is used. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the item with another component. |
| Prop | nativeButton |
|---|---|
| Type | boolean |
| Default | false |
| Description | Declares whether the rendered element is a native button. Set it to true when render returns a button. |
ContextMenuSeparator
| Prop | orientation |
|---|---|
| Type | "horizontal" | "vertical" |
| Default | "horizontal" |
| Description | Sets the orientation that the separator exposes to assistive technology. The line itself stays horizontal. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the separator with another component. |
ContextMenuShortcut
ContextMenuShortcut shows the key combination of an item at the end of its row, and the application handles the keys itself. It renders a span and has no props of its own.