avatar.tsx

Overview

The avatar.tsx file provides a reusable and customizable Avatar component set built on top of the Radix UI Avatar primitives (@radix-ui/react-avatar). It encapsulates avatar image display, fallback UI, and grouping logic into React components with utility-based styling. The components support forwarding refs and accept standard HTML and Radix Avatar props, allowing flexible integration in React applications.

This module exports three main components:

The file leverages utility CSS classes (likely Tailwind CSS) via the helper function cn for conditional concatenation of class names.


Components and Types

1. Avatar

A wrapper around Radix's AvatarPrimitive.Root component that sets up a circular avatar container with specific styling.

Signature

const Avatar: React.ForwardRefExoticComponent<
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root> & React.RefAttributes<React.ElementRef<typeof AvatarPrimitive.Root>>
>

Props

Behavior

Usage Example

import { Avatar, AvatarImage, AvatarFallback } from './avatar';

<Avatar>
  <AvatarImage src="user.jpg" alt="User Name" />
  <AvatarFallback>UN</AvatarFallback>
</Avatar>

2. AvatarImage

Displays the avatar image inside the Avatar container. Wraps Radix's AvatarPrimitive.Image.

Signature

const AvatarImage: React.ForwardRefExoticComponent<
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Image> & React.RefAttributes<React.ElementRef<typeof AvatarPrimitive.Image>>
>

Props

Behavior


3. AvatarFallback

Shows fallback content when the image is unavailable. Wraps Radix's AvatarPrimitive.Fallback.

Signature

const AvatarFallback: React.ForwardRefExoticComponent<
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback> & React.RefAttributes<React.ElementRef<typeof AvatarPrimitive.Fallback>>
>

Props

Behavior

Usage Example

<AvatarFallback>UN</AvatarFallback>  // Displays initials "UN" with fallback styles

4. AvatarGroup

A composite component that arranges multiple Avatar components in an overlapping horizontal stack. It optionally limits the number of displayed avatars and shows a "+N" indicator for additional avatars.

Props

Name

Type

Description

children

React.ReactElement<AvatarProps>[]

Array of Avatar elements to display.

max

number (optional)

Maximum number of avatars to display before collapsing with "+N".

className

string (optional)

Additional CSS classes for the container.

...props

React.ComponentProps<'div'>

Other div container props.

Behavior

Usage Example

<AvatarGroup max={3}>
  <Avatar><AvatarImage src="a.jpg" /></Avatar>
  <Avatar><AvatarImage src="b.jpg" /></Avatar>
  <Avatar><AvatarImage src="c.jpg" /></Avatar>
  <Avatar><AvatarImage src="d.jpg" /></Avatar>
</AvatarGroup>
// Displays 3 avatars + "+1" fallback avatar

Implementation Details


Interaction with Other Parts of the System

These components are designed to be building blocks for user profile displays, comment sections, team member lists, or anywhere user identity needs visual representation.


Visual Diagram

componentDiagram
    Avatar <|-- AvatarImage : uses
    Avatar <|-- AvatarFallback : uses
    AvatarGroup o-- Avatar : contains
    AvatarPrimitive.Root <|-- Avatar
    AvatarPrimitive.Image <|-- AvatarImage
    AvatarPrimitive.Fallback <|-- AvatarFallback

Summary

Component

Purpose

Key Features

Avatar

Circular avatar container

Size, shape, overflow control, ref forwarding

AvatarImage

Display user image

Aspect ratio control, ref forwarding

AvatarFallback

Fallback UI if image missing

Centered content, muted background

AvatarGroup

Displays multiple avatars stacked

Limits visible avatars, shows "+N" count, overlapping layout


Appendix: cn Utility Function

While not defined in this file, cn is a typical utility function for classNames concatenation, commonly used to conditionally join multiple CSS class strings into a single string.

Example implementation (not included here):

function cn(...classes: (string | undefined | false)[]): string {
  return classes.filter(Boolean).join(' ');
}

End of Documentation for avatar.tsx