Dropdown Menu
Touch-ready dropdown-menu with composable actions, checked choices, submenus, and portal positioning.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add dropdown-menuSource files
Expand a file to read the source the installer copies into your app.
components/ui/dropdown-menu/SubContentPrimitive.tsx
export { SubContent as SubContentPrimitive } from '@rn-primitives/dropdown-menu'
components/ui/dropdown-menu/SubContentPrimitive.web.tsx
import * as React from 'react'
import { Pressable, type PressableStateCallbackType } from 'react-native'
import { Slot } from '@rn-primitives/slot'
import type { SubContentProps, SubContentRef } from '@rn-primitives/dropdown-menu'
import * as Radix from '@radix-ui/react-dropdown-menu'
// The registry wrapper owns the scoped Portal; RN Primitives 1.5.2 adds an implicit body portal on web.
export const SubContentPrimitive = React.forwardRef<SubContentRef, SubContentProps & { className?: string }>(
({ asChild = false, forceMount, style, ...props }, ref) => {
const Component = asChild ? Slot : Pressable
const bounds = { width: '100%', maxWidth: '100%', minWidth: 0 } as const
return (
<Radix.SubContent
forceMount={forceMount}
avoidCollisions
collisionPadding={8}
style={{
width: 320,
maxWidth: 'min(calc(100vw - 16px), var(--radix-dropdown-menu-content-available-width, 320px))',
maxHeight: 'min(calc(100vh - 16px), var(--radix-dropdown-menu-content-available-height, 100vh))',
overflowX: 'hidden',
overflowY: 'auto',
}}
>
<Component
ref={ref}
{...props}
style={
typeof style === 'function'
? (state: PressableStateCallbackType) => [bounds, style(state)]
: [bounds, style]
}
/>
</Radix.SubContent>
)
},
)
SubContentPrimitive.displayName = 'dropdown-menu-SubContentPrimitive'
components/ui/dropdown-menu/DropdownMenu.tsx
import * as React from 'react'
import { Platform, ScrollView, StyleSheet, View, I18nManager, useWindowDimensions } from 'react-native'
import * as Primitive from '@rn-primitives/dropdown-menu'
import { cssInterop } from 'react-native-css-interop'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import { PortalWindow, usePopupDismiss, PopupLayer } from '@/lib/portal'
import { renderTextChildren } from '@/lib/label-children'
import { cn } from '@/lib/utils'
import { Check, ChevronRight } from 'lucide-react-native'
import { useThemeColors } from '@/lib/theme'
import { SubContentPrimitive } from './SubContentPrimitive'
cssInterop(Primitive.Root, { className: 'style' })
cssInterop(Primitive.Trigger, { className: 'style' })
cssInterop(Primitive.Content, { className: 'style' })
cssInterop(Primitive.Overlay, { className: 'style' })
cssInterop(Primitive.Item, { className: 'style' })
cssInterop(Primitive.Group, { className: 'style' })
cssInterop(Primitive.Label, { className: 'style' })
cssInterop(Primitive.CheckboxItem, { className: 'style' })
cssInterop(Primitive.RadioGroup, { className: 'style' })
cssInterop(Primitive.RadioItem, { className: 'style' })
cssInterop(Primitive.ItemIndicator, { className: 'style' })
cssInterop(Primitive.Separator, { className: 'style' })
cssInterop(Primitive.Sub, { className: 'style' })
cssInterop(Primitive.SubTrigger, { className: 'style' })
cssInterop(SubContentPrimitive, { className: 'style' })
export type DropdownMenuProps = Primitive.RootProps & { className?: string }
export const DropdownMenu = React.forwardRef<Primitive.RootRef, DropdownMenuProps>(({ className, ...props }, ref) => (
<Primitive.Root ref={ref} className={cn('self-start', className)} {...props} />
))
DropdownMenu.displayName = 'DropdownMenu'
export type DropdownMenuTriggerProps = Omit<Primitive.TriggerProps, 'children'> & {
children?: React.ReactNode
className?: string
}
export const DropdownMenuTrigger = React.forwardRef<Primitive.TriggerRef, DropdownMenuTriggerProps>(
({ className, children, asChild, disabled, ...props }, ref) => (
<Primitive.Trigger
accessibilityRole="button"
{...props}
ref={ref}
asChild={asChild}
disabled={disabled}
className={cn(
'min-h-[48px] min-w-[48px] flex-row items-center justify-center gap-[8px] rounded-md border border-border bg-background px-[16px] py-[10px] active:bg-accent',
disabled && 'opacity-50',
className,
)}
>
{asChild ? children : renderTextChildren(children)}
</Primitive.Trigger>
),
)
DropdownMenuTrigger.displayName = 'DropdownMenuTrigger'
export type DropdownMenuContentProps = Omit<Primitive.ContentProps, 'children'> & {
children?: React.ReactNode
className?: string
}
export const DropdownMenuContent = React.forwardRef<Primitive.ContentRef, DropdownMenuContentProps>(
(
{ className, children, asChild, insets, style, align = 'start', side = 'bottom', sideOffset = 8, ...props },
ref,
) => {
const root = Primitive.useRootContext()
const visible = root.open
const popup = usePopupDismiss(visible, () => root.onOpenChange(false))
const safe = useSafeAreaInsets()
const { width, height } = useWindowDimensions()
const bounds = { top: safe.top + 8, bottom: safe.bottom + 8, left: safe.left + 8, right: safe.right + 8, ...insets }
const maxWidth = Math.max(0, Math.min(320, width - bounds.left - bounds.right))
const maxHeight = Math.max(0, height - bounds.top - bounds.bottom)
// Native headless content must unmount to release its platform Back subscription.
if (Platform.OS !== 'web' && !visible) return null
return (
<Primitive.Portal
forceMount={props.forceMount}
hostName={popup.hostName}
{...(Platform.OS === 'web'
? { container: (popup.container ?? undefined) as unknown as Primitive.PortalProps['container'] }
: {})}
>
<PortalWindow layer={popup}>
{Platform.OS !== 'web' ? <Primitive.Overlay accessible={false} style={StyleSheet.absoluteFill} /> : null}
<Primitive.Content
{...props}
ref={ref}
asChild={asChild}
align={align}
side={side}
sideOffset={sideOffset}
insets={bounds}
className={cn('gap-[4px] rounded-lg border border-border bg-popover p-[8px]', className)}
style={
StyleSheet.flatten([
{ maxWidth, maxHeight, ...(Platform.OS !== 'web' ? { width: maxWidth } : {}) },
style,
]) ?? undefined
}
>
{asChild ? (
children
) : (
<ScrollView bounces={false} keyboardShouldPersistTaps="handled" style={{ maxHeight, flexGrow: 0 }}>
{renderTextChildren(children)}
</ScrollView>
)}
</Primitive.Content>
</PortalWindow>
</Primitive.Portal>
)
},
)
DropdownMenuContent.displayName = 'DropdownMenuContent'
export type DropdownMenuItemProps = Omit<Primitive.ItemProps, 'children'> & {
children?: React.ReactNode
className?: string
}
export const DropdownMenuItem = React.forwardRef<Primitive.ItemRef, DropdownMenuItemProps>(
({ className, children, asChild, disabled, ...props }, ref) => (
<Primitive.Item
{...props}
ref={ref}
asChild={asChild}
disabled={disabled}
className={cn(
'min-h-[48px] min-w-[48px] flex-row items-center gap-[8px] rounded-md px-[12px] py-[10px] active:bg-accent',
disabled && 'opacity-50',
className,
)}
>
{asChild ? children : <View className="min-w-0 flex-1">{renderTextChildren(children)}</View>}
</Primitive.Item>
),
)
DropdownMenuItem.displayName = 'DropdownMenuItem'
export type DropdownMenuGroupProps = Primitive.GroupProps & { className?: string }
export const DropdownMenuGroup = React.forwardRef<Primitive.GroupRef, DropdownMenuGroupProps>(
({ className, ...props }, ref) => <Primitive.Group ref={ref} className={cn('gap-[4px]', className)} {...props} />,
)
DropdownMenuGroup.displayName = 'DropdownMenuGroup'
export type DropdownMenuSeparatorProps = Primitive.SeparatorProps & { className?: string }
export const DropdownMenuSeparator = React.forwardRef<Primitive.SeparatorRef, DropdownMenuSeparatorProps>(
({ className, ...props }, ref) => (
<Primitive.Separator ref={ref} className={cn('my-[4px] h-[1px] bg-border', className)} {...props} />
),
)
DropdownMenuSeparator.displayName = 'DropdownMenuSeparator'
export type DropdownMenuLabelProps = Primitive.LabelProps & { className?: string }
export const DropdownMenuLabel = React.forwardRef<Primitive.LabelRef, DropdownMenuLabelProps>(
({ className, ...props }, ref) => (
<Primitive.Label
ref={ref}
className={cn('px-[12px] py-[8px] text-[15px] font-medium leading-[21px] text-muted-foreground', className)}
{...props}
/>
),
)
DropdownMenuLabel.displayName = 'DropdownMenuLabel'
export type DropdownMenuItemIndicatorProps = Primitive.ItemIndicatorProps & { className?: string }
export const DropdownMenuItemIndicator = React.forwardRef<Primitive.ItemIndicatorRef, DropdownMenuItemIndicatorProps>(
({ className, children, asChild, ...props }, ref) => {
const colors = useThemeColors()
return (
<Primitive.ItemIndicator
ref={ref}
asChild={asChild}
className={cn('size-[20px] items-center justify-center', className)}
{...props}
>
{asChild ? children : (children ?? <Check size={18} color={colors.foreground} aria-hidden />)}
</Primitive.ItemIndicator>
)
},
)
DropdownMenuItemIndicator.displayName = 'DropdownMenuItemIndicator'
export type DropdownMenuCheckboxItemProps = Omit<Primitive.CheckboxItemProps, 'children'> & {
children?: React.ReactNode
className?: string
}
export const DropdownMenuCheckboxItem = React.forwardRef<Primitive.CheckboxItemRef, DropdownMenuCheckboxItemProps>(
({ className, children, asChild, disabled, ...props }, ref) => (
<Primitive.CheckboxItem
{...props}
ref={ref}
asChild={asChild}
disabled={disabled}
className={cn(
'min-h-[48px] min-w-[48px] flex-row items-center gap-[8px] rounded-md px-[12px] py-[10px] active:bg-accent',
disabled && 'opacity-50',
className,
)}
>
{asChild ? (
children
) : (
<>
<View aria-hidden className="size-[20px] items-center justify-center">
<DropdownMenuItemIndicator />
</View>
<View className="min-w-0 flex-1">{renderTextChildren(children)}</View>
</>
)}
</Primitive.CheckboxItem>
),
)
DropdownMenuCheckboxItem.displayName = 'DropdownMenuCheckboxItem'
export type DropdownMenuRadioItemProps = Omit<Primitive.RadioItemProps, 'children'> & {
children?: React.ReactNode
className?: string
}
export const DropdownMenuRadioItem = React.forwardRef<Primitive.RadioItemRef, DropdownMenuRadioItemProps>(
({ className, children, asChild, disabled, ...props }, ref) => (
<Primitive.RadioItem
{...props}
ref={ref}
asChild={asChild}
disabled={disabled}
className={cn(
'min-h-[48px] min-w-[48px] flex-row items-center gap-[8px] rounded-md px-[12px] py-[10px] active:bg-accent',
disabled && 'opacity-50',
className,
)}
>
{asChild ? (
children
) : (
<>
<View aria-hidden className="size-[20px] items-center justify-center">
<DropdownMenuItemIndicator />
</View>
<View className="min-w-0 flex-1">{renderTextChildren(children)}</View>
</>
)}
</Primitive.RadioItem>
),
)
DropdownMenuRadioItem.displayName = 'DropdownMenuRadioItem'
export const DropdownMenuRadioGroup = Primitive.RadioGroup
export const DropdownMenuSub = Primitive.Sub
export type DropdownMenuSubTriggerProps = Omit<Primitive.SubTriggerProps, 'children'> & {
children?: React.ReactNode
className?: string
}
export const DropdownMenuSubTrigger = React.forwardRef<Primitive.SubTriggerRef, DropdownMenuSubTriggerProps>(
({ className, children, asChild, disabled, ...props }, ref) => {
const colors = useThemeColors()
return (
<Primitive.SubTrigger
ref={ref}
asChild={asChild}
disabled={disabled}
className={cn(
'min-h-[48px] min-w-[48px] flex-row items-center gap-[8px] rounded-md px-[12px] py-[10px] active:bg-accent',
disabled && 'opacity-50',
className,
)}
{...props}
>
{asChild ? (
children
) : (
<>
<View className="min-w-0 flex-1">{renderTextChildren(children)}</View>
<ChevronRight
size={20}
color={colors.foreground}
style={{ transform: [{ scaleX: I18nManager.isRTL ? -1 : 1 }] }}
aria-hidden
/>
</>
)}
</Primitive.SubTrigger>
)
},
)
DropdownMenuSubTrigger.displayName = 'DropdownMenuSubTrigger'
export type DropdownMenuSubContentProps = Omit<Primitive.SubContentProps, 'children'> & {
children?: React.ReactNode
className?: string
}
export const DropdownMenuSubContent = React.forwardRef<Primitive.SubContentRef, DropdownMenuSubContentProps>(
({ className, children, asChild, ...props }, ref) => {
const root = Primitive.useRootContext()
const sub = Primitive.useSubContext()
const visible = root.open && sub.open
const popup = usePopupDismiss(visible, () => sub.onOpenChange(false))
if (Platform.OS !== 'web' && !visible) return null
const content = (
<SubContentPrimitive
ref={ref}
asChild={asChild}
className={cn('gap-[4px] rounded-lg border border-border bg-popover p-[8px]', className)}
{...props}
>
{asChild ? children : renderTextChildren(children)}
</SubContentPrimitive>
)
return (
<PopupLayer layer={popup}>
{Platform.OS === 'web' ? (
<Primitive.Portal
forceMount={props.forceMount}
hostName={popup.hostName}
{...(Platform.OS === 'web'
? { container: (popup.container ?? undefined) as unknown as Primitive.PortalProps['container'] }
: {})}
>
{content}
</Primitive.Portal>
) : (
content
)}
</PopupLayer>
)
},
)
DropdownMenuSubContent.displayName = 'DropdownMenuSubContent'
components/ui/dropdown-menu/index.ts
export * from './DropdownMenu'
Dependencies
Registry items
npm packages
@rn-primitives/slot@radix-ui/react-dropdown-menu@^2.1.16@rn-primitives/dropdown-menureact-native-safe-area-contextlucide-react-nativereact-native-svgreact-native-css-interop