Skip to content

Avatar

Identifies a person or an organisation with an image, or with initials when there is no image.

Silkware
SW
GR
LB

Installation

pnpm dlx shadcn@latest add @silkware/avatar

To copy the component by hand, install its dependency:

pnpm add cn

Then copy the source into components/ui/avatar.tsx:

components/ui/avatar.tsx
"use client"
import * as React from "react"import { cn } from "cn"
function Avatar({  className,  size = "default",  ...props}: React.ComponentProps<"div"> & {  size?: "default" | "sm" | "lg"}) {  return (    <div      data-slot="avatar"      data-size={size}      className={cn(        "group/avatar relative flex size-8 shrink-0 rounded-full select-none after:absolute after:inset-0 after:rounded-full after:border after:border-border after:mix-blend-darken data-[size=lg]:size-10 data-[size=sm]:size-6 dark:after:mix-blend-lighten",        className      )}      {...props}    />  )}
type ImageState = "loading" | "loaded" | "error"
function AvatarImage({  className,  onLoad,  onError,  ...props}: React.ComponentProps<"img">) {  const [result, setResult] = React.useState<{    src: typeof props.src    state: ImageState  }>({ src: props.src, state: props.src ? "loading" : "error" })  const state =    result.src === props.src ? result.state : props.src ? "loading" : "error"  return (    <img      data-slot="avatar-image"      alt={props.alt || ""}      data-state={state}      className={cn(        "peer aspect-square size-full rounded-full object-cover data-[state=error]:hidden",        className      )}      {...props}      onLoad={(event) => {        setResult({ src: props.src, state: "loaded" })        onLoad?.(event)      }}      onError={(event) => {        setResult({ src: props.src, state: "error" })        onError?.(event)      }}    />  )}
function AvatarFallback({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="avatar-fallback"      className={cn(        "flex size-full items-center justify-center rounded-full bg-muted text-sm text-muted-foreground group-data-[size=sm]/avatar:text-xs peer-data-[state=error]:flex peer-[*]:hidden",        className      )}      {...props}    />  )}
function AvatarBadge({ className, ...props }: React.ComponentProps<"span">) {  return (    <span      data-slot="avatar-badge"      className={cn(        "absolute right-0 bottom-0 z-10 inline-flex items-center justify-center rounded-full bg-primary text-primary-foreground bg-blend-color ring-2 ring-background select-none",        "group-data-[size=sm]/avatar:size-2 group-data-[size=sm]/avatar:[&>svg]:hidden",        "group-data-[size=default]/avatar:size-2.5 group-data-[size=default]/avatar:[&>svg]:size-2",        "group-data-[size=lg]/avatar:size-3 group-data-[size=lg]/avatar:[&>svg]:size-2",        className      )}      {...props}    />  )}
function AvatarGroup({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="avatar-group"      className={cn(        "group/avatar-group flex -space-x-2 *:data-[slot=avatar]:ring-2 *:data-[slot=avatar]:ring-background",        className      )}      {...props}    />  )}
function AvatarGroupCount({  className,  ...props}: React.ComponentProps<"div">) {  return (    <div      data-slot="avatar-group-count"      className={cn(        "relative flex size-8 shrink-0 items-center justify-center rounded-full bg-muted text-sm text-muted-foreground ring-2 ring-background group-has-data-[size=lg]/avatar-group:size-10 group-has-data-[size=sm]/avatar-group:size-6 [&>svg]:size-4 group-has-data-[size=lg]/avatar-group:[&>svg]:size-5 group-has-data-[size=sm]/avatar-group:[&>svg]:size-3",        className      )}      {...props}    />  )}
export {  Avatar,  AvatarImage,  AvatarFallback,  AvatarGroup,  AvatarGroupCount,  AvatarBadge,}

Usage

import {  Avatar,  AvatarBadge,  AvatarFallback,  AvatarGroup,  AvatarGroupCount,  AvatarImage,} from "@/components/ui/avatar"
<Avatar>  <AvatarImage src="/people/giulia.jpg" alt="Giulia" />  <AvatarFallback>GR</AvatarFallback></Avatar>

Put AvatarFallback after AvatarImage: the fallback hides while an image follows it. When the image fails or has no src, the image hides and the fallback shows. While the image loads, neither is visible. Write the fallback as the person's initials: two letters at most.

Examples

Sizes

sm is 24px, the default is 32px and lg is 40px. Use one size per list.

GR
GR
GR

Fallback

When there is no image, or it fails to load, AvatarFallback shows the initials on the muted surface.

Marco, quality
MC

Badge

AvatarBadge places a small dot at the bottom right. It scales with the avatar and has no content of its own, so give it visually hidden text that says what it means.

GR
On shift
LB
On shift
MC
On shift

Group

AvatarGroup stacks avatars with a background ring and a negative overlap. AvatarGroupCount adds a final chip for the people who do not fit. The group is a plain div, so write the count in text that makes sense to a screen reader.

GR
LB
MC
+4

Accessibility

KeyAction
TabDoes nothing on an avatar. Put the avatar inside a button or a link to make it interactive.

React Aria provides nothing here: the avatar is built from plain elements and a small piece of state that tracks the image load.

You provide:

  • alt text on AvatarImage: the person's name. AvatarImage renders an empty alt when you pass none, which hides a meaningful image from screen readers.
  • Initials in the fallback that are readable on their own, or aria-hidden on the avatar when the name is written next to it.
  • Text for the badge, as in the example, and a text equivalent for the group.
  • A name beside the avatar wherever the avatar is the only way to tell people apart.

Guidelines

When to use

  • To show who owns, wrote or is assigned to something: an order, a quality check, a message.
  • In a list of people, next to the name, as an anchor for the eye.

When not to use

  • To show an icon or a logo. Use an img or an icon with the right size.
  • As a button. Wrap it in a Button when it must open a profile or a menu.
  • To stand for a group of more than five people. Use AvatarGroupCount or a Table of names.
  • To fill an Item when the item has no person. Use an icon.

Content

  • Fallback: initials in capitals, two at most, such as "GR" for Giulia.
  • Alt text: the name, as in "Giulia". Not "Photo of Giulia".
  • Keep one size within a group and across a list.

Names

GR
GiuliaProduction planner
Do

Write the name next to the avatar. The avatar helps the eye find the person.

GR
Don’t

Don’t let initials stand alone. Two letters do not tell people apart.

API reference

Avatar

Renders a circular div that holds the image, the fallback and the badge. It accepts every div prop and the props below.

PropTypeDefaultDescription
size?"default" | "sm" | "lg""default"The diameter: 32px by default, 24px small, 40px large.
className?stringNoneClasses merged after the base classes.

AvatarImage

Renders an img that fills the avatar. It hides itself when the image fails to load. It accepts every img prop.

PropTypeDefaultDescription
src?stringNoneThe image address. Without it the fallback shows.
alt?stringNoneThe text alternative: the person's name.

AvatarFallback

Renders a div with the initials, shown when there is no image or the image fails. It accepts every div prop.

AvatarBadge

Renders a span at the bottom right of the avatar, filled with the primary colour and ringed with the background. It accepts every span prop.

AvatarGroup

Renders a flex div that overlaps its avatars. It accepts every div prop.

AvatarGroupCount

Renders a div the size of the avatars in the group, for a count such as "+4". It accepts every div prop.