Components
Avatar
A person's picture with an initials fallback, plus an overlapping group.
Use Avatar to show who someone is. It shows their image when there is one and initials when there isn't, or when the image fails to load. AvatarGroup stacks several and collapses the rest into a "+N" count.
Installation
pnpm dlx shadcn@latest add https://components.abdulqadirmsingi.com/r/avatar.json
This installs both Avatar and AvatarGroup.
Usage
import { Avatar, AvatarGroup } from "@/components/ui/avatar";
<Avatar name="Asha Juma" src="/asha.jpg" />
<AvatarGroup people={[{ name: "Asha Juma" }, { name: "Baraka Mushi" }]} max={4} />Group
Props
Avatar extends native <span> props (except children) and spreads them on, including ref (React 19).
| Prop | Type | Default | Description |
|---|---|---|---|
| name | string | – | The person's name. Used for the initials and as the accessible name. Required. |
| src | string | – | Image URL. If missing or broken, initials are shown. |
| size | "sm" | "md" | "lg" | "md" | 32, 40 and 56 pixels. |
| people | { name: string; src?: string }[] | – | On AvatarGroup. The people to show. |
| max | number | 4 | On AvatarGroup. How many to show before collapsing into +N. |
Accessibility
- Each avatar is a
role="img"named by the person, so screen readers say "Asha Juma, image". The picture and the initials inside are hidden so the name is not read twice. AvatarGroupis a labelled group ("6 people" by default; passaria-labelto say more). The "+N" is announced as "N more".- Initials are not a unique identifier, so don't rely on them alone where two people could look alike.
Tokens used
--muted, --foreground, --background (the separating ring).
Known limitations
- It is a client component, because it has to notice when the image fails to load.
- It uses a plain
<img>, with nonext/imageoptimisation, so the component works in any framework. - No presence or status dot yet.
Source
"use client";import { cva, type VariantProps } from "class-variance-authority";import { type ComponentProps, useEffect, useRef, useState } from "react";import { cn } from "@/lib/utils";const avatarVariants = cva( "relative inline-flex shrink-0 select-none items-center justify-center overflow-hidden rounded-full bg-muted font-medium text-foreground", { variants: { size: { sm: "size-8 text-xs", md: "size-10 text-sm", lg: "size-14 text-base" }, }, defaultVariants: { size: "md" }, },);/** First letter of the first and last words, so "Asha Juma Msingi" becomes "AM". */function initialsOf(name: string): string { const words = name.trim().split(/\s+/).filter(Boolean); const first = Array.from(words[0] ?? "")[0] ?? ""; const last = words.length > 1 ? (Array.from(words[words.length - 1] ?? "")[0] ?? "") : ""; return (first + last).toUpperCase();}export interface AvatarProps extends Omit<ComponentProps<"span">, "children">, VariantProps<typeof avatarVariants> { /** The person's name: used for the initials fallback and as the accessible name. */ name: string; /** Image URL. If it is missing or fails to load, the initials are shown instead. */ src?: string;}/** * A person's picture with an initials fallback. It is a client component only because it * must notice when the image fails to load. */export function Avatar({ name, src, size, className, ...props }: AvatarProps) { const [failed, setFailed] = useState(false); const imgRef = useRef<HTMLImageElement>(null); // An image can fail before React attaches onError during hydration; check it once mounted. // biome-ignore lint/correctness/useExhaustiveDependencies: re-check only when the source changes. useEffect(() => { setFailed(false); const img = imgRef.current; if (img?.complete && img.naturalWidth === 0) setFailed(true); }, [src]); const showImage = Boolean(src) && !failed; return ( <span role="img" aria-label={name} className={cn(avatarVariants({ size }), className)} {...props} > {showImage ? ( <img ref={imgRef} src={src} alt="" onError={() => setFailed(true)} className="size-full object-cover" /> ) : ( <span aria-hidden="true">{initialsOf(name)}</span> )} </span> );}export interface AvatarGroupProps extends Omit<ComponentProps<"div">, "children"> { people: { name: string; src?: string }[]; /** How many avatars to show before collapsing the rest into a "+N" count. Defaults to 4. */ max?: number; size?: AvatarProps["size"];}/** Overlapping avatars with a "+N" overflow count. The group is named by its members. */export function AvatarGroup({ people, max = 4, size, className, ...props }: AvatarGroupProps) { const limit = Math.max(1, Math.floor(max)); const shown = people.slice(0, limit); const extra = people.length - shown.length; const ring = "ring-2 ring-background"; return ( // biome-ignore lint/a11y/useSemanticElements: a fieldset implies form controls; this is a plain labelled group. <div role="group" aria-label={props["aria-label"] ?? `${people.length} people`} className={cn("flex items-center -space-x-2 rtl:space-x-reverse", className)} {...props} > {shown.map((p) => ( <Avatar key={`${p.name}-${p.src ?? ""}`} name={p.name} src={p.src} size={size} className={ring} /> ))} {extra > 0 ? ( <span role="img" aria-label={`${extra} more`} className={cn(avatarVariants({ size }), ring)} > <span aria-hidden="true">+{extra}</span> </span> ) : null} </div> );}export { avatarVariants };