UIPackage React Native
Search
Menu

Menubar

Touch-ready menubar 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 menubar

Source files

Expand a file to read the source the installer copies into your app.

components/ui/menubar/SubContentPrimitive.tsx
export { SubContent as SubContentPrimitive } from '@rn-primitives/menubar'
components/ui/menubar/SubContentPrimitive.web.tsx
import * as React from 'react'
import { View } from 'react-native'
import { Slot } from '@rn-primitives/slot'
import type { SubContentProps, SubContentRef } from '@rn-primitives/menubar'
import * as Radix from '@radix-ui/react-menubar'

// 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 : View
    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-menubar-content-available-width, 320px))',
          maxHeight: 'min(calc(100vh - 16px), var(--radix-menubar-content-available-height, 100vh))',
          overflowX: 'hidden',
          overflowY: 'auto',
        }}
      >
        <Component ref={ref} {...props} style={[bounds, style]} />
      </Radix.SubContent>
    )
  },
)
SubContentPrimitive.displayName = 'menubar-SubContentPrimitive'
components/ui/menubar/Menubar.tsx
import * as React from 'react'
import { Platform, ScrollView, StyleSheet, View, I18nManager, useWindowDimensions } from 'react-native'
import * as Primitive from '@rn-primitives/menubar'
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.Menu, { 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 MenubarProps = Primitive.RootProps & { className?: string }

export const Menubar = React.forwardRef<Primitive.RootRef, MenubarProps>(({ className, ...props }, ref) => (
  <Primitive.Root
    ref={ref}
    className={cn(
      'flex-row flex-wrap items-center gap-[8px] rounded-lg border border-border bg-card p-[4px]',
      className,
    )}
    {...props}
  />
))
Menubar.displayName = 'Menubar'
export const MenubarMenu = Primitive.Menu
export type MenubarTriggerProps = Omit<Primitive.TriggerProps, 'children'> & {
  children?: React.ReactNode
  className?: string
}
export const MenubarTrigger = React.forwardRef<Primitive.TriggerRef, MenubarTriggerProps>(
  ({ className, children, asChild, disabled, ...props }, ref) => (
    <Primitive.Trigger
      {...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 px-[12px] py-[10px] active:bg-accent',
        disabled && 'opacity-50',
        className,
      )}
    >
      {asChild ? children : renderTextChildren(children)}
    </Primitive.Trigger>
  ),
)
MenubarTrigger.displayName = 'MenubarTrigger'
export type MenubarContentProps = Omit<Primitive.ContentProps, 'children'> & {
  children?: React.ReactNode
  className?: string
}
export const MenubarContent = React.forwardRef<Primitive.ContentRef, MenubarContentProps>(
  (
    { className, children, asChild, insets, style, align = 'start', side = 'bottom', sideOffset = 8, ...props },
    ref,
  ) => {
    const root = Primitive.useRootContext()
    const menu = Primitive.useMenuContext()
    const visible = root.value !== undefined && root.value === menu.value
    const popup = usePopupDismiss(visible, () => root.onValueChange(undefined))
    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>
    )
  },
)
MenubarContent.displayName = 'MenubarContent'
export type MenubarItemProps = Omit<Primitive.ItemProps, 'children'> & {
  children?: React.ReactNode
  className?: string
}
export const MenubarItem = React.forwardRef<Primitive.ItemRef, MenubarItemProps>(
  ({ 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>
  ),
)
MenubarItem.displayName = 'MenubarItem'
export type MenubarGroupProps = Primitive.GroupProps & { className?: string }
export const MenubarGroup = React.forwardRef<Primitive.GroupRef, MenubarGroupProps>(({ className, ...props }, ref) => (
  <Primitive.Group ref={ref} className={cn('gap-[4px]', className)} {...props} />
))
MenubarGroup.displayName = 'MenubarGroup'
export type MenubarSeparatorProps = Primitive.SeparatorProps & { className?: string }
export const MenubarSeparator = React.forwardRef<Primitive.SeparatorRef, MenubarSeparatorProps>(
  ({ className, ...props }, ref) => (
    <Primitive.Separator ref={ref} className={cn('my-[4px] h-[1px] bg-border', className)} {...props} />
  ),
)
MenubarSeparator.displayName = 'MenubarSeparator'
export type MenubarLabelProps = Primitive.LabelProps & { className?: string }
export const MenubarLabel = React.forwardRef<Primitive.LabelRef, MenubarLabelProps>(({ className, ...props }, ref) => (
  <Primitive.Label
    ref={ref}
    className={cn('px-[12px] py-[8px] text-[15px] font-medium leading-[21px] text-muted-foreground', className)}
    {...props}
  />
))
MenubarLabel.displayName = 'MenubarLabel'
export type MenubarItemIndicatorProps = Primitive.ItemIndicatorProps & { className?: string }
export const MenubarItemIndicator = React.forwardRef<Primitive.ItemIndicatorRef, MenubarItemIndicatorProps>(
  ({ 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>
    )
  },
)
MenubarItemIndicator.displayName = 'MenubarItemIndicator'
export type MenubarCheckboxItemProps = Omit<Primitive.CheckboxItemProps, 'children'> & {
  children?: React.ReactNode
  className?: string
}
export const MenubarCheckboxItem = React.forwardRef<Primitive.CheckboxItemRef, MenubarCheckboxItemProps>(
  ({ 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">
            <MenubarItemIndicator />
          </View>
          <View className="min-w-0 flex-1">{renderTextChildren(children)}</View>
        </>
      )}
    </Primitive.CheckboxItem>
  ),
)
MenubarCheckboxItem.displayName = 'MenubarCheckboxItem'
export type MenubarRadioItemProps = Omit<Primitive.RadioItemProps, 'children'> & {
  children?: React.ReactNode
  className?: string
}
export const MenubarRadioItem = React.forwardRef<Primitive.RadioItemRef, MenubarRadioItemProps>(
  ({ 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">
            <MenubarItemIndicator />
          </View>
          <View className="min-w-0 flex-1">{renderTextChildren(children)}</View>
        </>
      )}
    </Primitive.RadioItem>
  ),
)
MenubarRadioItem.displayName = 'MenubarRadioItem'
export const MenubarRadioGroup = Primitive.RadioGroup
export const MenubarSub = Primitive.Sub
export type MenubarSubTriggerProps = Omit<Primitive.SubTriggerProps, 'children'> & {
  children?: React.ReactNode
  className?: string
}
export const MenubarSubTrigger = React.forwardRef<Primitive.SubTriggerRef, MenubarSubTriggerProps>(
  ({ 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>
    )
  },
)
MenubarSubTrigger.displayName = 'MenubarSubTrigger'

export type MenubarSubContentProps = Omit<Primitive.SubContentProps, 'children'> & {
  children?: React.ReactNode
  className?: string
}
export const MenubarSubContent = React.forwardRef<Primitive.SubContentRef, MenubarSubContentProps>(
  ({ className, children, asChild, ...props }, ref) => {
    const root = Primitive.useRootContext()
    const menu = Primitive.useMenuContext()
    const sub = Primitive.useSubContext()
    const visible = root.value !== undefined && root.value === menu.value && 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>
    )
  },
)
MenubarSubContent.displayName = 'MenubarSubContent'
components/ui/menubar/index.ts
export * from './Menubar'

Dependencies

Registry items

npm packages

  • @rn-primitives/slot
  • @radix-ui/react-menubar@^1.1.16
  • @rn-primitives/menubar
  • react-native-safe-area-context
  • lucide-react-native
  • react-native-svg
  • react-native-css-interop
Browse overlays and feedback