suiss/ui · Component
Context Menu
A menu of actions that opens at the pointer when people right-click an area.
Context Menu is an open-source component from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
Context menu wraps the context menu primitive of the chosen base. The ContextMenu root manages the open state and ContextMenuTrigger defines the area that opens the menu. ContextMenuContent renders the menu in a portal at the pointer position. Inside it, use ContextMenuItem for actions, ContextMenuCheckboxItem for toggles, and ContextMenuRadioGroup with ContextMenuRadioItem for a single choice. Organize items with ContextMenuGroup, ContextMenuLabel and ContextMenuSeparator, and nest a submenu with ContextMenuSub, ContextMenuSubTrigger and ContextMenuSubContent. ContextMenuShortcut displays a keyboard shortcut at the end of an item.
Use a context menu for secondary actions on an object. Because it is hidden until a right-click or long press, make the same actions available elsewhere. For a menu opened from a visible button, use a dropdown menu.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies and add the
cnhelper:
-
Add the pieces it builds on:
use-platform. -
Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Composition
Use the following composition to build a ContextMenu:
Examples
Inset
Set inset on items and labels that have no icon so their text aligns with items that do, or with the indicators of checkbox and radio items. The last item uses variant="destructive".
Accessibility
- The menu has the
menurole. Items have themenuitem,menuitemcheckboxormenuitemradiorole, and checkbox and radio items setaria-checked. - Disabled items set
aria-disabledand cannot be chosen. - Arrow keys move between items. On a submenu trigger, the Right Arrow opens the submenu and moves focus into it, and the Left Arrow returns to the trigger. The arrow directions follow the reading direction.
- Escape closes the menu, and choosing a regular item closes it.
ContextMenuShortcutonly displays a shortcut; it does not register one. It is hidden on small screens.
API Reference
ContextMenu
The root that manages the menu's open state.
Also accepts the props of the primitive part it wraps.
ContextMenuPortal
Renders its children into a portal outside the current DOM hierarchy.
Also accepts the props of the primitive part it wraps.
ContextMenuTrigger
The area that opens the menu on right-click.
Also accepts the props of the primitive part it wraps.
The trigger sets select-none so long-pressing does not select its text.
ContextMenuContent
The menu panel, rendered in a portal at the pointer position.
| Prop | Type | Default |
|---|---|---|
side | "top" | "bottom" | "left" | "right" | "inline-start" | "inline-end" | "inline-end" |
sideOffset | number | 0 |
align | "start" | "center" | "end" | "start" |
alignOffset | number | 4 |
| Prop | Type | Default |
|---|---|---|
alignOffset | number | 4 |
| Prop | Type | Default |
|---|---|---|
placement | Placement | "bottom start" |
offset | number | 0 |
crossOffset | number | 4 |
Also accepts the props of the primitive parts it wraps.
side and align place the menu against the pointer; by default it opens on the inline-end side, aligned to start. sideOffset and alignOffset are in pixels.
alignOffset is the offset along the alignment axis, in pixels.
placement sets where the menu opens relative to the pointer. offset and crossOffset are in pixels.
ContextMenuGroup
Groups related items.
Also accepts the props of the primitive part it wraps.
ContextMenuLabel
A heading for a group of items.
| Prop | Type | Default |
|---|---|---|
inset | boolean | - |
Also accepts the props of the primitive part it wraps.
ContextMenuItem
An action in the menu.
| Prop | Type | Default |
|---|---|---|
inset | boolean | - |
variant | "default" | "destructive" | "default" |
Also accepts the props of the primitive part it wraps.
Respond to an action with the item's onClick.
Respond to an action with the item's onAction.
Selecting a regular item closes the menu. inset adds leading padding so the item aligns with items that have an icon or indicator. variant="destructive" colors the item and its icon with the destructive color; use it on items that delete or remove something. Items set data-inset and data-variant, and disabled items get data-disabled.
ContextMenuSub
The root of a nested submenu.
Also accepts the props of the primitive part it wraps.
ContextMenuSubTrigger
The item that opens a submenu, shown with a trailing caret.
| Prop | Type | Default |
|---|---|---|
inset | boolean | - |
Also accepts the props of the primitive part it wraps.
An open submenu trigger gets data-popup-open.
An open submenu trigger gets data-state="open".
ContextMenuSubContent
The panel of a submenu, opened beside its trigger.
Also accepts the props of React.ComponentProps<typeof ContextMenuContent>.
ContextMenuCheckboxItem
An item that toggles a setting on or off and shows a check mark when on.
| Prop | Type | Default |
|---|---|---|
inset | boolean | - |
| Prop | Type | Default |
|---|---|---|
defaultChecked | boolean | false |
closeOnClick | boolean | false |
| Prop | Type | Default |
|---|---|---|
isSelected | boolean | - |
defaultSelected | boolean | false |
onChange | (isSelected: boolean) => void | - |
Also accepts the props of the primitive part it wraps.
Checkbox items are uncontrolled with defaultChecked, or controlled with checked and onCheckedChange.
Set closeOnClick to close the menu when the item is selected.
Checkbox items are uncontrolled with defaultSelected, or controlled with isSelected and onChange.
Checkbox and radio items keep the menu open when selected.
ContextMenuRadioGroup
Groups radio items so that only one can be selected.
| Prop | Type | Default |
|---|---|---|
value | string | - |
defaultValue | string | - |
onChange | (value: string) => void | - |
Also accepts the props of the primitive part it wraps.
Radio groups take defaultValue, or value and onValueChange.
Radio groups take defaultValue, or value and onChange, which is called with the value of the newly selected item.
ContextMenuRadioItem
An item in a radio group that shows a check mark when selected.
| Prop | Type | Default |
|---|---|---|
inset | boolean | - |
| Prop | Type | Default |
|---|---|---|
closeOnClick | boolean | false |
| Prop | Type | Default |
|---|---|---|
value | string | - |
Also accepts the props of the primitive part it wraps.
Each radio item needs a value, which the group reports when it is selected.
Set closeOnClick to close the menu when the item is selected.
ContextMenuSeparator
A divider between groups of items.
Also accepts the props of the primitive part it wraps.
ContextMenuShortcut
Displays a keyboard shortcut at the end of an item.
| Prop | Type | Default |
|---|---|---|
mod | boolean | - |
Also accepts the props of React.ComponentProps<"span">.
Set mod to prefix the shortcut with the platform modifier key, ⌘ on Apple platforms and Ctrl+ elsewhere.
Each part sets a data-slot, such as context-menu-item or context-menu-sub-trigger, for styling.
Web preview
ContextMenu attaches a menu to a ContextMenuTrigger area. A long press opens ContextMenuContent at the position of the finger with a medium impact haptic. The content renders through a portal (inside a full-window overlay on iOS) as a raised, rounded card.
Items can be plain actions, checkbox items, radio items in a group, or a submenu. ContextMenuSubContent expands in place under its trigger, and the caret turns to point down while it is open.
Installation
- Install the following dependencies:
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/context-menu.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/context-menu.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/context-menu.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
Examples
Default
Web preview
Simple
A short list of plain items.

Accessibility
- The trigger exposes a
longpressaccessibility action, so VoiceOver and TalkBack users can open the menu, and reports itsexpandedstate. - The popup has the
menurole; items usemenuitem,checkboxandradioroles, and radio groups useradiogroup. - Separators have the
separatorrole.
API Reference
ContextMenu
The root that manages the open state.
| Prop | Type | Default |
|---|---|---|
open | boolean | - |
defaultOpen | boolean | false |
onOpenChange | (open: boolean, details) => void | - |
disabled | boolean | false |
Use open with onOpenChange for controlled state, or defaultOpen for uncontrolled. disabled prevents the menu from opening. Wrap the long-pressable area in ContextMenuTrigger.
ContextMenuContent
Floating menu card.
| Prop | Type | Default |
|---|---|---|
portalHost | string | - |
Also accepts the props of React.ComponentProps<typeof ContextMenuPrimitive.Popup>, PositionerProps.
portalHost names the portal host to render into. The fade-in respects the system Reduce Motion setting.
ContextMenuSubTrigger
Row that expands the submenu.
| Prop | Type | Default |
|---|---|---|
children | React.ReactNode | - |
iconClassName | string | - |
inset | boolean | - |
Also accepts the props of React.ComponentProps<typeof ContextMenuPrimitive.SubmenuTrigger>.
iconClassName adds classes to the caret icon.
ContextMenuSubContent
Items of the submenu.
Also accepts the props of React.ComponentProps<typeof ContextMenuPrimitive.Popup>.
ContextMenuItem
Action row.
| Prop | Type | Default |
|---|---|---|
inset | boolean | - |
variant | "default" | "destructive" | "default" |
Also accepts the props of React.ComponentProps<typeof ContextMenuPrimitive.Item>.
Items close the menu when pressed unless closeOnPress={false}. Set variant="destructive" on an item that deletes or removes. inset adds start padding to align an item without an icon with items that have one; the sub trigger, checkbox, radio and label parts take it too. A highlighted item fades to opacity-80 and a disabled one to opacity-50.
ContextMenuCheckboxItem
Row with a check mark that toggles.
| Prop | Type | Default |
|---|---|---|
children | React.ReactNode | - |
inset | boolean | - |
Also accepts the props of React.ComponentProps<typeof ContextMenuPrimitive.CheckboxItem>.
Control it with checked and onCheckedChange.
ContextMenuRadioItem
Row that selects one value in its group.
| Prop | Type | Default |
|---|---|---|
children | React.ReactNode | - |
inset | boolean | - |
Also accepts the props of React.ComponentProps<typeof ContextMenuPrimitive.RadioItem>.
Place radio items inside a ContextMenuRadioGroup with value and onValueChange.
ContextMenuLabel
Heading for a group.
| Prop | Type | Default |
|---|---|---|
inset | boolean | - |
Also accepts the props of React.ComponentProps<typeof ContextMenuPrimitive.GroupLabel>.
Group items with ContextMenuGroup and a ContextMenuLabel.
ContextMenuSeparator
Line between sections.
Also accepts the props of React.ComponentProps<typeof ContextMenuPrimitive.Separator>.
.contextMenu
| Parameter | Type | Default |
|---|---|---|
items | () -> Items | - |
preview | () -> Preview | - |
previewRadius | CGFloat | - |
Overloads: contextMenu(items:preview:previewRadius:), contextMenu(previewRadius:items:).
ContextMenu
The root that manages the open state.
| Parameter | Type | Default |
|---|---|---|
expanded | Boolean | - |
onExpandedChange | (Boolean) -> Unit | - |
trigger | @Composable () -> Unit | - |
modifier | Modifier | Modifier |
enabled | Boolean | true |
content | @Composable ColumnScope.() -> Unit | - |
