Navigation Menu
Composable touch navigation menu with native Back dismissal, selected links, and themed portal content.
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 navigation-menuSource files
Expand a file to read the source the installer copies into your app.
components/ui/navigation-menu/NavigationMenu.tsx
import * as React from 'react'
import { Platform, Pressable, ScrollView, StyleSheet, View, useWindowDimensions } from 'react-native'
import * as Primitive from '@rn-primitives/navigation-menu'
import { cssInterop } from 'react-native-css-interop'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import { PortalWindow, usePopupDismiss } from '@/lib/portal'
import { renderTextChildren } from '@/lib/label-children'
import { cn } from '@/lib/utils'
cssInterop(Primitive.Root, { className: 'style' })
cssInterop(Primitive.List, { className: 'style' })
cssInterop(Primitive.Item, { className: 'style' })
cssInterop(Primitive.Trigger, { className: 'style' })
cssInterop(Primitive.Content, { className: 'style' })
cssInterop(Primitive.Link, { className: 'style' })
cssInterop(Primitive.Indicator, { className: 'style' })
cssInterop(Primitive.Viewport, { className: 'style' })
export type NavigationMenuProps = Primitive.RootProps & { className?: string }
export const NavigationMenu = React.forwardRef<Primitive.RootRef, NavigationMenuProps>(
({ className, value, ...props }, ref) => (
<Primitive.Root
ref={ref}
value={Platform.OS === 'web' ? (value ?? '') : value}
className={cn('relative w-full', className)}
{...props}
/>
),
)
NavigationMenu.displayName = 'NavigationMenu'
export type NavigationMenuListProps = Primitive.ListProps & { className?: string }
export const NavigationMenuList = React.forwardRef<Primitive.ListRef, NavigationMenuListProps>(
({ className, ...props }, ref) => (
<Primitive.List ref={ref} className={cn('flex-row flex-wrap items-start gap-[8px]', className)} {...props} />
),
)
NavigationMenuList.displayName = 'NavigationMenuList'
export type NavigationMenuItemProps = Primitive.ItemProps & { className?: string }
export const NavigationMenuItem = React.forwardRef<Primitive.ItemRef, NavigationMenuItemProps>(
({ className, ...props }, ref) => (
<Primitive.Item ref={ref} className={cn('min-w-0 max-w-full', className)} {...props} />
),
)
NavigationMenuItem.displayName = 'NavigationMenuItem'
export type NavigationMenuTriggerProps = Omit<Primitive.TriggerProps, 'children'> & {
children?: React.ReactNode
className?: string
}
export const NavigationMenuTrigger = React.forwardRef<Primitive.TriggerRef, NavigationMenuTriggerProps>(
({ 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>
),
)
NavigationMenuTrigger.displayName = 'NavigationMenuTrigger'
export type NavigationMenuContentProps = Omit<Primitive.ContentProps, 'children'> & {
children?: React.ReactNode
className?: string
}
export const NavigationMenuContent = React.forwardRef<Primitive.ContentRef, NavigationMenuContentProps>(
({ className, children, asChild, insets, style, sideOffset = 8, ...props }, ref) => {
const safe = useSafeAreaInsets()
const { width, height } = useWindowDimensions()
const { value, onValueChange } = Primitive.useRootContext()
const item = Primitive.useItemContext()
const visible = value !== undefined && value === item.value
const popup = usePopupDismiss(visible, () => onValueChange(undefined))
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' ? (
<Pressable accessible={false} style={StyleSheet.absoluteFill} onPress={() => onValueChange(undefined)} />
) : null}
<Primitive.Content
{...props}
ref={ref}
asChild={asChild}
sideOffset={sideOffset}
insets={bounds}
className={cn(
'z-50 gap-[4px] rounded-lg border border-border bg-popover p-[8px]',
Platform.OS === 'web' && 'relative mt-[8px]',
className,
)}
style={
StyleSheet.flatten([
{ width: maxWidth, maxWidth: Platform.OS === 'web' ? '100%' : maxWidth, maxHeight },
style,
]) ?? undefined
}
>
{asChild ? (
children
) : (
<ScrollView bounces={false} keyboardShouldPersistTaps="handled" style={{ maxHeight, flexGrow: 0 }}>
{renderTextChildren(children)}
</ScrollView>
)}
</Primitive.Content>
</PortalWindow>
</Primitive.Portal>
)
},
)
NavigationMenuContent.displayName = 'NavigationMenuContent'
export type NavigationMenuLinkProps = Omit<Primitive.LinkProps, 'children'> & {
children?: React.ReactNode
className?: string
}
export const NavigationMenuLink = React.forwardRef<Primitive.LinkRef, NavigationMenuLinkProps>(
({ className, children, asChild, disabled, onPress, active, ...props }, ref) => {
const { onValueChange } = Primitive.useRootContext()
return (
<Primitive.Link
{...props}
ref={ref}
asChild={asChild}
active={active}
disabled={disabled}
accessibilityState={{ ...props.accessibilityState, disabled: !!disabled, selected: !!active }}
onPress={(event) => {
if (disabled) return
onPress?.(event)
if (Platform.OS !== 'web' && !event.isDefaultPrevented()) onValueChange(undefined)
}}
className={cn(
'min-h-[48px] min-w-[48px] flex-row items-center gap-[8px] rounded-md px-[12px] py-[10px] active:bg-accent',
active && 'bg-secondary',
disabled && 'opacity-50',
className,
)}
>
{asChild ? children : <View className="min-w-0 flex-1">{renderTextChildren(children)}</View>}
</Primitive.Link>
)
},
)
NavigationMenuLink.displayName = 'NavigationMenuLink'
export type NavigationMenuIndicatorProps = Primitive.IndicatorProps & { className?: string }
export const NavigationMenuIndicator = React.forwardRef<Primitive.IndicatorRef, NavigationMenuIndicatorProps>(
({ className, ...props }, ref) => (
<Primitive.Indicator ref={ref} className={cn('h-[2px] rounded-full bg-primary', className)} {...props} />
),
)
NavigationMenuIndicator.displayName = 'NavigationMenuIndicator'
export type NavigationMenuViewportProps = Primitive.ViewportProps & { className?: string }
export const NavigationMenuViewport = React.forwardRef<Primitive.ViewportRef, NavigationMenuViewportProps>(
({ className, ...props }, ref) => <Primitive.Viewport ref={ref} className={cn('relative', className)} {...props} />,
)
NavigationMenuViewport.displayName = 'NavigationMenuViewport'
components/ui/navigation-menu/index.ts
export * from './NavigationMenu'
Dependencies
Registry items
npm packages
@rn-primitives/navigation-menureact-native-safe-area-contextreact-native-css-interop