Dinachi

Avatar

An image element with a fallback for representing a user. Supports multiple sizes and graceful loading states.

View Source

#Installation

npx @dinachi/cli@latest add avatar

#Usage

tsx
import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar'

#Examples

#Image optimizers

AvatarImage preloads src and renders once it loads, which breaks with next/image and loading="lazy". Add keepMounted to render the image right away. The fallback shows beneath it until the image loads, and stays if it fails.

tsx
import Image from 'next/image'
 
<Avatar>
  <AvatarFallback>JD</AvatarFallback>
  <AvatarImage
    keepMounted
    render={<Image src="/avatars/jane.png" width={40} height={40} alt="Jane Doe" />}
  />
</Avatar>

#API Reference

PropTypeDefaultDescription
size'sm' | 'md' | 'lg''md'The size of the avatar
srcstring—The image source URL for the avatar
altstring—Alternative text for the avatar image
keepMountedbooleanfalseRender the image immediately and let it load in place. Needed for next/image and loading="lazy".
fallbackReact.ReactNode—Content to display when the image fails to load or is not provided