UIPackage React Native
Search
Menu

Popover

Themed popover with press access, portal rendering, viewport bounds, and safe-area collision insets.

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 popover

Source files

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

components/ui/popover/Popover.tsx
import * as React from 'react'
import { Platform, ScrollView, StyleSheet, useWindowDimensions } from 'react-native'
import * as Primitive from '@rn-primitives/popover'
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.Trigger, { className: 'style' })
cssInterop(Primitive.Content, { className: 'style' })
cssInterop(Primitive.Overlay, { className: 'style' })
cssInterop(Primitive.Close, { className: 'style' })

export type PopoverProps = Primitive.RootProps & { className?: string }

export const Popover = React.forwardRef<Primitive.RootRef, PopoverProps>(({ className, ...props }, ref) => (
  <Primitive.Root ref={ref} className={cn('self-start', className)} {...props} />
))
Popover.displayName = 'Popover'
export type PopoverTriggerProps = Omit<Primitive.TriggerProps, 'children'> & {
  children?: React.ReactNode
  className?: string
}
export const PopoverTrigger = React.forwardRef<Primitive.TriggerRef, PopoverTriggerProps>(
  ({ 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 border border-border bg-background px-[16px] py-[10px] active:bg-accent',
        disabled && 'opacity-50',
        className,
      )}
    >
      {asChild ? children : renderTextChildren(children)}
    </Primitive.Trigger>
  ),
)
PopoverTrigger.displayName = 'PopoverTrigger'
export type PopoverContentProps = Omit<Primitive.ContentProps, 'children'> & {
  children?: React.ReactNode
  className?: string
}
export const PopoverContent = React.forwardRef<Primitive.ContentRef, PopoverContentProps>(
  (
    { 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('rounded-lg border border-border bg-popover p-[12px]', className)}
            style={StyleSheet.flatten([{ maxWidth, maxHeight }, style]) ?? undefined}
          >
            {asChild ? (
              children
            ) : (
              <ScrollView bounces={false} keyboardShouldPersistTaps="handled" style={{ maxHeight, flexGrow: 0 }}>
                {renderTextChildren(children)}
              </ScrollView>
            )}
          </Primitive.Content>
        </PortalWindow>
      </Primitive.Portal>
    )
  },
)
PopoverContent.displayName = 'PopoverContent'
export type PopoverCloseProps = Omit<Primitive.CloseProps, 'children'> & {
  children?: React.ReactNode
  className?: string
}
export const PopoverClose = React.forwardRef<Primitive.CloseRef, PopoverCloseProps>(
  ({ className, children, asChild, disabled, ...props }, ref) => (
    <Primitive.Close
      {...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.Close>
  ),
)
PopoverClose.displayName = 'PopoverClose'
components/ui/popover/index.ts
export * from './Popover'

Dependencies

Registry items

npm packages

  • @rn-primitives/popover
  • react-native-safe-area-context
  • react-native-css-interop
Browse overlays and feedback