Context Menu
Displays a menu activated by right-click or long press, such as a set of actions or functions.
Anatomy
Import and assemble the component:
1import { ContextMenu } from '@raystack/apsara'23<ContextMenu>4 <ContextMenu.Trigger />5 <ContextMenu.Content>6 <ContextMenu.Item />7 </ContextMenu.Content>8</ContextMenu>
Usage
A context menu opens on right-click or long press. Its contents are composed the same way as Menu.
Basic usage
A simple context menu with basic functionality. Right-click the trigger area to open.
1<ContextMenu>2 <ContextMenu.Trigger3 style={{4 padding: "2em 4em",5 border: "1px dashed var(--rs-color-border-base-primary)",6 borderRadius: "var(--rs-radius-2)",7 }}8 >9 <Text>Right click here</Text>10 </ContextMenu.Trigger>11 <ContextMenu.Content>12 <ContextMenu.Item>Profile</ContextMenu.Item>13 <ContextMenu.Item>Settings</ContextMenu.Item>14 <ContextMenu.Separator />
With icons
You can add icons to the menu items. Supports both leading and trailing icons.
1<ContextMenu>2 <ContextMenu.Trigger3 style={{4 padding: "2em 4em",5 border: "1px dashed var(--rs-color-border-base-primary)",6 borderRadius: "var(--rs-radius-2)",7 }}8 >9 <Text>Right click here</Text>10 </ContextMenu.Trigger>11 <ContextMenu.Content>12 <ContextMenu.Item leadingIcon={<>📝</>}>Edit</ContextMenu.Item>13 <ContextMenu.Item leadingIcon={<>📋</>} trailingIcon={<>⌘C</>}>14 Copy
With groups and labels
Organize related menu items into sections with descriptive headers.
1<ContextMenu>2 <ContextMenu.Trigger3 style={{4 padding: "2em 4em",5 border: "1px dashed var(--rs-color-border-base-primary)",6 borderRadius: "var(--rs-radius-2)",7 }}8 >9 <Text>Right click here</Text>10 </ContextMenu.Trigger>11 <ContextMenu.Content>12 <ContextMenu.Group>13 <ContextMenu.Label>Actions</ContextMenu.Label>14 <ContextMenu.Item>New File</ContextMenu.Item>
Submenu
Use ContextMenu.Submenu, ContextMenu.SubmenuTrigger, and ContextMenu.SubmenuContent to create nested menus with multiple levels.
1<ContextMenu>2 <ContextMenu.Trigger3 style={{4 padding: "2em 4em",5 border: "1px dashed var(--rs-color-border-base-primary)",6 borderRadius: "var(--rs-radius-2)",7 }}8 >9 <Text>Right click here</Text>10 </ContextMenu.Trigger>11 <ContextMenu.Content>12 <ContextMenu.Item>Profile</ContextMenu.Item>13 <ContextMenu.Item>Settings</ContextMenu.Item>14 <ContextMenu.Separator />
Checkbox, radio, and link items
ContextMenu.CheckboxItem, ContextMenu.RadioGroup, ContextMenu.RadioItem, and ContextMenu.LinkItem work the same way as in Menu. Checkbox and radio items keep the menu open on click. Link items render an <a> element.
1(function SelectionContextMenu() {2 const [showGrid, setShowGrid] = React.useState(true);3 const [sort, setSort] = React.useState("name");45 return (6 <ContextMenu>7 <ContextMenu.Trigger8 style={{9 padding: "2em 4em",10 border: "1px dashed var(--rs-color-border-base-primary)",11 borderRadius: "var(--rs-radius-2)",12 }}13 >14 <Text>Right click here</Text>
Autocomplete
Pass autocomplete to the ContextMenu root. Each menu manages its own autocomplete state.
autocompleteMode="auto"is the default. Items filter as the user types. Each item matches by its rendered text, or by itslabelwhen set. Matching ignores case and accents.- Submenus opt in separately: pass
autocompletetoContextMenu.Submenu.
1<ContextMenu autocomplete>2 <ContextMenu.Trigger3 style={{4 padding: "2em 4em",5 border: "1px dashed var(--rs-color-border-base-primary)",6 borderRadius: "var(--rs-radius-2)",7 }}8 >9 <Text>Right click here</Text>10 </ContextMenu.Trigger>11 <ContextMenu.Content searchPlaceholder="Search">12 <ContextMenu.Group>13 <ContextMenu.Label>Heading</ContextMenu.Label>14 <ContextMenu.Item>Assign member...</ContextMenu.Item>
Controlled
Pass open with onOpenChange to own the state. Do this when the menu has to close on something other than a selection, or when only one menu may be open at a time.
1(function ControlledContextMenu() {2 const [open, setOpen] = React.useState(false);34 return (5 <Flex direction="column" align="center" gap={5}>6 <ContextMenu open={open} onOpenChange={setOpen}>7 <ContextMenu.Trigger>8 <Flex9 align="center"10 justify="center"11 style={{12 width: 220,13 height: 80,14 border: "1px dashed var(--rs-color-border-base-primary)",
API Reference
The ContextMenu component is composed of several parts, each with their own props.
Root
The root element is the parent component that holds the context menu. Using the autocomplete prop, you can enable search functionality.
Prop
Type
Trigger
The area that opens the context menu on right-click or long press. Renders a <div> element.
Prop
Type
Content
The container that holds the menu items. When autocomplete is enabled, renders an inline search input inside the popup.
Prop
Type
Item
Individual clickable options within the menu. Built on top of Base UI ContextMenu.Item.
In a searchable menu, the item matches by its rendered text, including text in leadingIcon and trailingIcon. Pass label to match other text in its place.
Prop
Type
CheckboxItem
A menu item that toggles a setting on or off. Renders a check mark before the label when checked.
Prop
Type
RadioGroup
Groups related radio items and holds the selected value.
Prop
Type
RadioItem
A menu item that works like a radio button in a ContextMenu.RadioGroup. Renders a dot before the label when checked.
Prop
Type
LinkItem
A menu item that renders an <a> element. Pass a router link to render to use client-side routing.
Prop
Type
Group
A way to group related menu items together. When filtering is active, a group with no matching items is hidden.
Prop
Type
Label
Renders a label in a menu group. This component should be wrapped with ContextMenu.Group so the aria-labelledby is correctly set on the group element.
Prop
Type
Separator
Visual divider between menu items or groups. In autocompleteMode="auto", it is hidden while the search input has text.
Prop
Type
EmptyState
Placeholder content when there are no menu items to display. In a searchable menu, it shows only when no item matches the search.
Prop
Type
Submenu
Wraps a submenu root. Use with ContextMenu.SubmenuTrigger and ContextMenu.SubmenuContent to create nested menus.
Supports its own autocomplete prop to enable search functionality within the submenu independently from the parent menu.
Prop
Type
SubmenuTrigger
The trigger item for a submenu. Renders with a trailing chevron icon by default. Accepts leadingIcon and trailingIcon props.
In a searchable parent menu, the trigger matches by its text, or by its label when set. The parent search does not match items inside the submenu.
Prop
Type
SubmenuContent
The content container for a submenu. Shares the same API as ContextMenu.Content with a default sideOffset of 2.
Prop
Type
Slots
Every rendered part carries a stable data-slot attribute for styling and testing:
| Slot | Element |
|---|---|
context-menu-trigger | The ContextMenu.Trigger element |
context-menu-positioner | Positioning wrapper around the content popup |
context-menu-content | The ContextMenu.Content / ContextMenu.SubmenuContent popup |
context-menu-search-input | The autocomplete search input (when autocomplete) |
context-menu-search-list | Wrapper around items in autocomplete mode (when autocomplete) |
context-menu-item | Each ContextMenu.Item |
context-menu-group | ContextMenu.Group wrapper |
context-menu-label | ContextMenu.Label |
context-menu-separator | ContextMenu.Separator |
context-menu-checkbox-item | Each ContextMenu.CheckboxItem |
context-menu-checkbox-item-indicator | Check mark inside a checkbox item |
context-menu-radio-group | ContextMenu.RadioGroup wrapper |
context-menu-radio-item | Each ContextMenu.RadioItem |
context-menu-radio-item-indicator | Dot inside a radio item |
context-menu-link-item | Each ContextMenu.LinkItem |
context-menu-empty-state | ContextMenu.EmptyState |
menu-subtrigger | ContextMenu.SubmenuTrigger (shared with Menu's internals) |
menu-cell-leading-icon | Leading icon inside an item or submenu trigger (when provided; shared with Menu's internals) |
menu-cell-trailing-icon | Trailing icon inside an item or submenu trigger (when provided; shared with Menu's internals) |
Accessibility
- Built on Base UI Context Menu:
the popup uses
role="menu"and items userole="menuitem", following the WAI-ARIA Menu pattern. - Opens on right-click or long press (touch); focus moves into the menu when it opens and returns to the page when it closes with Escape.
- Arrow keys move through items, Enter activates the highlighted item, and ArrowRight/ArrowLeft open and close submenus.
- With
autocomplete, the popup becomes arole="dialog"containing a search input (role="searchbox", labelled bysearchPlaceholder) and arole="menu"list. Items keep their menu roles. Focus stays in the input while arrow keys move the highlight. Escape in a submenu closes it and returns focus to the parent input. ContextMenu.Labelmust be wrapped inContextMenu.Groupso the group is labelled viaaria-labelledby.- Checkbox items use
role="menuitemcheckbox"and radio items userole="menuitemradio", both witharia-checked.