UIPackage React Native
Search

Avatar

Round identity image with an initials fallback that shows while loading or after an image error. Four sizes; name the person on the surrounding row for assistive tech.

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 avatar

Source files

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

components/ui/avatar/Avatar.tsx
import * as React from 'react'
import { Image, Text, View, type ImageProps, type ViewProps } from 'react-native'
import { cn } from '@/lib/utils'
import { avatarFallbackTextVariants, avatarVariants, type AvatarVariants } from './avatar.variants'

export interface AvatarProps extends ViewProps, AvatarVariants {
  className?: string
}

/** Round identity container. Compose AvatarFallback first, then
 *  AvatarImage above it — a failed or missing image reveals the fallback. */
function Avatar({ className, size, ...props }: AvatarProps) {
  return <View className={cn(avatarVariants({ size }), className)} {...props} />
}

export interface AvatarImageProps extends ImageProps {
  className?: string
}

function AvatarImage({ className, style, onError, ...props }: AvatarImageProps) {
  const [failed, setFailed] = React.useState(false)
  if (failed) return null
  return (
    <Image
      aria-hidden
      className={cn('absolute inset-0 h-full w-full', className)}
      // Explicit fill style alongside the classes: Image is not class-interop'd
      // on every platform (react-native-web passes className through raw), and
      // an unstyled Image renders at natural size instead of covering the
      // avatar. The style prop guarantees the fill everywhere.
      style={[{ position: 'absolute', top: 0, left: 0, width: '100%', height: '100%' }, style]}
      onError={(event) => {
        setFailed(true)
        onError?.(event)
      }}
      {...props}
    />
  )
}

export interface AvatarFallbackProps extends ViewProps, AvatarVariants {
  className?: string
  textClassName?: string
  /** Initials or short label rendered centered when no image covers it. */
  children?: React.ReactNode
}

function AvatarFallback({ className, textClassName, size, children, ...props }: AvatarFallbackProps) {
  return (
    <View
      aria-hidden
      className={cn('absolute inset-0 items-center justify-center', className)}
      {...props}
    >
      {typeof children === 'string' ? (
        <Text className={cn(avatarFallbackTextVariants({ size }), textClassName)}>{children}</Text>
      ) : (
        children
      )}
    </View>
  )
}

export { Avatar, AvatarFallback, AvatarImage }
components/ui/avatar/avatar.variants.ts
import type { VariantProps } from 'class-variance-authority'
import { cva } from 'class-variance-authority'

export const avatarVariants = cva('bg-muted relative shrink-0 overflow-hidden rounded-full', {
  variants: {
    size: {
      sm: 'h-8 w-8',
      default: 'h-10 w-10',
      lg: 'h-12 w-12',
      xl: 'h-16 w-16',
    },
  },
  defaultVariants: {
    size: 'default',
  },
})

export const avatarFallbackTextVariants = cva('text-muted-foreground font-medium', {
  variants: {
    size: {
      sm: 'text-xs',
      default: 'text-sm',
      lg: 'text-base',
      xl: 'text-lg',
    },
  },
  defaultVariants: {
    size: 'default',
  },
})

export type AvatarVariants = VariantProps<typeof avatarVariants>
components/ui/avatar/index.ts
export {
  Avatar,
  AvatarFallback,
  AvatarImage,
  type AvatarFallbackProps,
  type AvatarImageProps,
  type AvatarProps,
} from './Avatar'
export { avatarFallbackTextVariants, avatarVariants, type AvatarVariants } from './avatar.variants'

Dependencies

npm packages

  • class-variance-authority
Browse content and media