- Label
- Icon label
- Field
- Form
- Frontmatter form
- Questionnaire
- Input
- Input group
- Input OTP
- Password input
- Password strength
- Code input
- Number field
- Tag input
- Textarea
- Checkbox
- Radio group
- Language toggle group
- Emoji appearance toggle group
- Emoji picker
- Avatar picker
- AI provider picker
- Select
- Native select
- Combobox
- Language combobox
- Slider
- Calendar
- Switch
- Alert
- Avatar
- Badge
- Card
- AI provider card
- Model info card
- Model list
- Empty
- Item
- Tree item
- File tree
- Icon media
- File type icon
- Font preview
- Image preview
- Kbd
- Table
- Data table
- Data table column header
- Chart
- Code Block
- Code card
- Markdown view
- Attachment
- Bubble
- Marker
- Message
- Chat message
- Chat suggestion item
- Message scroller
- Trace
- Progress
- Spinner
- Sonner
- Toast
- Unsaved changes toolbar
- Unsaved indicator
- Status indicator
- Permission card
- Separator
- Skeleton
- Animated grid pattern
- Meteors
Installation#
pnpm dlx shadcn@latest add @zui/context-menuThe CLI writes components/ui/context-menu.tsx, lib/menu.ts, lib/motion.ts and lib/overlay.ts, installs Direction, Icon, Text and Utils, and adds
@rn-primitives/context-menu, @rn-primitives/portal, lucide-react-native, react-native-reanimated and
react-native-worklets. The menu draws in the app's PortalHost; see Add a
portal host. Without one it draws nothing, and logs an error saying
so while the app is in development.
Usage#
import {
ContextMenu,
ContextMenuContent,
ContextMenuItem,
ContextMenuShortcut,
ContextMenuTrigger,
} from '@/components/ui/context-menu';
import { Text } from '@/components/ui/text';<ContextMenu>
<ContextMenuTrigger className="h-36 items-center justify-center rounded-md border border-dashed border-border">
<Text>Long press here</Text>
</ContextMenuTrigger>
<ContextMenuContent className="w-52">
<ContextMenuItem onPress={goBack}>
<Text>Back</Text>
<ContextMenuShortcut>{'\u2318['}</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem variant="destructive" onPress={remove}>
<Text>Delete</Text>
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>API reference#
ContextMenu#
Takes every prop of React Native's View, plus onOpenChange, called with true when the menu opens and false
when it closes.
ContextMenuTrigger#
Takes every prop of React Native's Pressable. It is the area the menu belongs to: a long press opens the menu
below the finger on a device, and a right click or a long press opens it at the pointer on the web. Screen
readers reach it as a button, announced as expanded or collapsed, whose long-press action opens the menu. Focus
returns to it when the menu closes.
ContextMenuContent#
Takes every prop of React Native's View, plus:
| Prop | Type | Default |
|---|---|---|
side | 'top' | 'right' | 'bottom' | 'left' | 'right' |
sideOffset | number | 0 |
align | 'start' | 'center' | 'end' | 'start' |
alignOffset | number | 4 |
portalHost | string | - |
On a device the menu opens above the long press for top, and below it for any other side, alignOffset px
along from it. It is a menu, and accessibility focus moves into it as it opens. Like upstream's, it is modal:
VoiceOver stays inside it, and TalkBack does once the app hides its screens while an overlay is open (see Add a
portal host). A press outside it, the Android back button, the iOS
escape gesture (a two-finger Z) and Escape on the web close it. It fades in while growing from 95% and sliding
0.5rem over 100ms, and out the same way. When the system asks for reduced motion it appears and goes at once.
The items#
ContextMenuItem, ContextMenuCheckboxItem, ContextMenuRadioGroup, ContextMenuRadioItem, ContextMenuSub,
ContextMenuSubTrigger, ContextMenuSubContent, ContextMenuGroup, ContextMenuLabel, ContextMenuSeparator,
ContextMenuShortcut and ContextMenuPortal take the props of their
Dropdown menu counterparts and behave as they do:
an item runs onPress and closes the menu unless closeOnClick is false, and a checkbox or radio item leaves it
open.
Right to left#
The chevron of a ContextMenuSubTrigger is mirrored when Direction
says text reads right to left, as upstream's cn-rtl-flip does.
Differences from upstream#
- Upstream's
renderprop becomesasChild, andonClickbecomesonPress, as on every native item. - The menu takes no
openordefaultOpen: neither the device's primitive nor the web's Radix one lets its owner open it, so it opens only from its area.onOpenChangereports what it does. - A device opens the menu where the finger pressed, above or below it;
leftandrightopen it below. The web opens it at the pointer, as upstream does, whateversidesays. - On a device a sub-menu opens in place, under its trigger, and a second press on the trigger closes it. The web opens it beside the trigger.
- React Native has no
menuitemcheckboxormenuitemradiorole, so on a device a checkbox item is announced as a checkbox and a radio item as a radio button, each with its checked state. The web gives them upstream's roles. - The back button and the iOS escape gesture remove the menu at once, without its exit animation.
- Upstream's animation runs whatever the system's motion setting. The native one is skipped when the system asks for reduced motion.
portalHostis added, for an app that draws overlays into aPortalHostof its own.