UIPackage React Native
Search

Hover Card

Themed hover-card 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 hover-card

Source files

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

components/ui/hover-card/HoverCard.tsx
import * as React from 'react'
import { Platform, Pressable, ScrollView, StyleSheet, useWindowDimensions } from 'react-native'
import * as Primitive from '@rn-primitives/hover-card'
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' })

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

export const HoverCard = React.forwardRef<Primitive.RootRef, HoverCardProps>(({ className, ...props }, ref) => (
  <Primitive.Root ref={ref} className={cn('self-start', className)} {...props} />
))
HoverCard.displayName = 'HoverCard'
export type HoverCardTriggerProps = Omit<Primitive.TriggerProps, 'children'> & {
  children?: React.ReactNode
  className?: string
}
export const HoverCardTrigger = React.forwardRef<Primitive.TriggerRef, HoverCardTriggerProps>(
  ({ className, children, asChild, disabled, onPress, ...props }, ref) => {
    const { open, onOpenChange } = Primitive.useRootContext()
    return (
      <Primitive.Trigger
        accessibilityRole="button"
        {...props}
        ref={ref}
        asChild={asChild}
        disabled={disabled}
        pointerEvents={disabled ? 'none' : props.pointerEvents}
        onPress={(event) => {
          if (disabled) return
          onPress?.(event)
          if (Platform.OS === 'web' && !event.isDefaultPrevented()) onOpenChange(!open)
        }}
        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>
    )
  },
)
HoverCardTrigger.displayName = 'HoverCardTrigger'
export type HoverCardContentProps = Omit<Primitive.ContentProps, 'children'> & {
  children?: React.ReactNode
  className?: string
}
export const HoverCardContent = React.forwardRef<Primitive.ContentRef, HoverCardContentProps>(
  (
    { 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>
    )
  },
)
HoverCardContent.displayName = 'HoverCardContent'
export type HoverCardCloseProps = Omit<React.ComponentProps<typeof Pressable>, 'children'> & {
  children?: React.ReactNode
  className?: string
}
export const HoverCardClose = React.forwardRef<React.ComponentRef<typeof Pressable>, HoverCardCloseProps>(
  ({ className, children, disabled, onPress, ...props }, ref) => {
    const { onOpenChange } = Primitive.useRootContext()
    return (
      <Pressable
        {...props}
        ref={ref}
        accessibilityRole="button"
        disabled={disabled}
        accessibilityState={{ ...props.accessibilityState, disabled: !!disabled }}
        onPress={(event) => {
          if (disabled) return
          onPress?.(event)
          if (!event.isDefaultPrevented()) onOpenChange(false)
        }}
        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,
        )}
      >
        {renderTextChildren(children)}
      </Pressable>
    )
  },
)
HoverCardClose.displayName = 'HoverCardClose'
components/ui/hover-card/index.ts
export * from './HoverCard'

Dependencies

Registry items

npm packages

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