Avatar
The shadcn/ui Avatar with a soft image fade-in, a hairline edge and an on-theme initials fallback — plus two animated React avatar blocks built with Motion and Radix: an overlapping avatar group that fans out on hover with a +N overflow chip, and a status avatar with a pulsing online, away or busy indicator.
- Drop-in shadcn API
- Radix Avatar
- Motion blocks
- prefers-reduced-motion
import { AvatarGroup } from "@/components/dusk/avatar/avatar-group";
const team = [
{ name: "Maya Chen", src: "https://i.pravatar.cc/150?img=47" },
{ name: "Leo Park", src: "https://i.pravatar.cc/150?img=12" },
{ name: "Ines Duarte", src: "https://i.pravatar.cc/150?img=32" },
{ name: "Sam Okafor" },
{ name: "Ava Lindqvist", src: "https://i.pravatar.cc/150?img=5" },
{ name: "Noah Becker", src: "https://i.pravatar.cc/150?img=15" },
{ name: "Priya Nair", src: "https://i.pravatar.cc/150?img=44" },
];
export default function AvatarGroupDemo() {
return (
<div className="flex flex-col items-center gap-10 pt-6">
<AvatarGroup items={team} max={5} label="Project members" />
<AvatarGroup items={team.slice(0, 4)} size="sm" label="Reviewers" />
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/avatar.json1. Install the dependencies.
npm install @radix-ui/react-avatar2. Copy the source into
components/ui/avatar.tsx.avatar.tsx "use client" import * as React from "react" import * as AvatarPrimitive from "@radix-ui/react-avatar" import { cn } from "@/lib/utils" /** * DuskUI Avatar — a drop-in upgrade of the shadcn/ui Avatar. * * Same parts, props and data-slot attributes as shadcn, plus: * - The image fades in once it has loaded, instead of popping in over the * fallback. * - A hairline inner border so photos don't bleed into dark backgrounds. * - A neutral fallback for initials and icons. * - Under `prefers-reduced-motion` the image appears instantly. * CSS only (tw-animate-css). Radix renders the image only after it loads and * shows the fallback while it loads or if it fails. */ function Avatar({ className, ...props }: React.ComponentProps<typeof AvatarPrimitive.Root>) { return ( <AvatarPrimitive.Root data-slot="avatar" className={cn( "relative flex size-8 shrink-0 overflow-hidden rounded-full select-none", // Hairline edge drawn above the image, following the avatar's radius. "after:pointer-events-none after:absolute after:inset-0 after:rounded-[inherit] after:border after:border-white/10", className )} {...props} /> ) } /** The photo. Rendered only once it has loaded, then fades in. Always pass `alt`. */ function AvatarImage({ className, ...props }: React.ComponentProps<typeof AvatarPrimitive.Image>) { return ( <AvatarPrimitive.Image data-slot="avatar-image" className={cn( "aspect-square size-full object-cover", "animate-in fade-in-0 duration-200 ease-out motion-reduce:animate-none", className )} {...props} /> ) } /** Shown while the image loads or when it fails. Usually initials or an icon. */ function AvatarFallback({ className, ...props }: React.ComponentProps<typeof AvatarPrimitive.Fallback>) { return ( <AvatarPrimitive.Fallback data-slot="avatar-fallback" className={cn( "flex size-full items-center justify-center rounded-full text-xs font-medium uppercase", "bg-muted text-muted-foreground", className )} {...props} /> ) } export { Avatar, AvatarImage, AvatarFallback }
Replaces components/ui/avatar.tsx with the same exports and props. The fade-in is CSS (tw-animate-css, included with shadcn/ui), so the base needs no Motion.
Usage
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
export function UserAvatar() {
return (
<Avatar>
<AvatarImage src="https://i.pravatar.cc/150?img=47" alt="Maya Chen" />
<AvatarFallback>MC</AvatarFallback>
</Avatar>
)
}Examples
Default
Radix renders the image only once it has loaded, so it fades and settles in over the fallback instead of popping. A broken URL keeps the initials. Resize with size-* and change the shape with rounded-* on both the avatar and the fallback.
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
export default function AvatarBasicDemo() {
return (
<div className="flex items-center gap-4">
<Avatar className="size-12">
<AvatarImage src="https://i.pravatar.cc/150?img=47" alt="Maya Chen" />
<AvatarFallback>MC</AvatarFallback>
</Avatar>
<Avatar className="size-10">
<AvatarImage src="https://i.pravatar.cc/150?img=12" alt="Leo Park" />
<AvatarFallback>LP</AvatarFallback>
</Avatar>
<Avatar>
<AvatarImage src="https://i.pravatar.cc/150?img=32" alt="Ines Duarte" />
<AvatarFallback>ID</AvatarFallback>
</Avatar>
{/* A broken URL shows the fallback. */}
<Avatar className="size-10">
<AvatarImage src="/missing-avatar.png" alt="Sam Okafor" />
<AvatarFallback>SO</AvatarFallback>
</Avatar>
<Avatar className="size-10 rounded-lg">
<AvatarFallback className="rounded-lg">DU</AvatarFallback>
</Avatar>
</div>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Avatar Group
An overlapping stack that fans out symmetrically when you hover it. The avatar under the pointer lifts above its neighbours and shows a name tag, and anything past max collapses into a “+N” chip. It only moves with transforms, so nothing around it shifts.
import { AvatarGroup } from "@/components/dusk/avatar/avatar-group";
const team = [
{ name: "Maya Chen", src: "https://i.pravatar.cc/150?img=47" },
{ name: "Leo Park", src: "https://i.pravatar.cc/150?img=12" },
{ name: "Ines Duarte", src: "https://i.pravatar.cc/150?img=32" },
{ name: "Sam Okafor" },
{ name: "Ava Lindqvist", src: "https://i.pravatar.cc/150?img=5" },
{ name: "Noah Becker", src: "https://i.pravatar.cc/150?img=15" },
{ name: "Priya Nair", src: "https://i.pravatar.cc/150?img=44" },
];
export default function AvatarGroupDemo() {
return (
<div className="flex flex-col items-center gap-10 pt-6">
<AvatarGroup items={team} max={5} label="Project members" />
<AvatarGroup items={team.slice(0, 4)} size="sm" label="Reviewers" />
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/avatar-group.json1. Install the dependencies.
npm install motion @radix-ui/react-avatar2. Copy the source into
components/dusk/avatar/avatar-group.tsx.avatar-group.tsx "use client"; import * as React from "react"; import { AnimatePresence, motion, useReducedMotion, type Transition } from "motion/react"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { cn } from "@/lib/utils"; const spring: Transition = { type: "spring", duration: 0.3, bounce: 0.1 }; const instant: Transition = { duration: 0 }; const sizes = { sm: { avatar: "size-8 text-[0.65rem]", overlap: "-ml-2.5", spread: 10 }, default: { avatar: "size-10 text-xs", overlap: "-ml-3", spread: 14 }, lg: { avatar: "size-12 text-sm", overlap: "-ml-4", spread: 18 }, } as const; export type AvatarGroupItem = { /** Full name. Used for the accessible label, the hover name tag and the initials. */ name: string; /** Image URL. Falls back to initials while loading or if it fails. */ src?: string; /** Custom fallback content (defaults to the initials of `name`). */ fallback?: React.ReactNode; }; export interface AvatarGroupProps extends Omit<React.ComponentProps<"ul">, "children"> { items: AvatarGroupItem[]; /** How many avatars to show before collapsing the rest into “+N”. */ max?: number; size?: keyof typeof sizes; /** Extra pixels each avatar moves apart on hover. Defaults per size; 0 disables. */ spread?: number; /** Show the hovered person's name above their avatar. */ showNames?: boolean; /** Accessible label for the whole group, e.g. "Project members". */ label?: string; } function initials(name: string) { const parts = name.trim().split(/\s+/); return ((parts[0]?.[0] ?? "") + (parts.length > 1 ? parts[parts.length - 1][0] : "")).toUpperCase(); } /** * Overlapping stack of avatars that fans out when you hover it. The hovered * avatar lifts above its neighbours and shows a name tag; anything past `max` * collapses into a “+N” chip. Spreading uses transforms only, so nothing * around the group moves. Each avatar is labelled for screen readers and the * motion is instant under reduced motion. */ export function AvatarGroup({ items, max = 4, size = "default", spread, showNames = true, label = "People", className, onPointerLeave, ...props }: AvatarGroupProps) { const reduceMotion = useReducedMotion() ?? false; const [hovered, setHovered] = React.useState<number | null>(null); const [expanded, setExpanded] = React.useState(false); const config = sizes[size]; const gap = spread ?? config.spread; const visible = items.slice(0, max); const hidden = items.length - visible.length; const count = visible.length + (hidden > 0 ? 1 : 0); const transition = reduceMotion ? instant : spring; // Fan out symmetrically around the centre so the group grows in both directions. const offset = (index: number) => (expanded ? (index - (count - 1) / 2) * gap : 0); return ( <ul aria-label={label} className={cn("relative isolate flex items-center pl-0", className)} onPointerEnter={() => setExpanded(true)} onPointerLeave={(event) => { onPointerLeave?.(event); setExpanded(false); setHovered(null); }} {...props} > {visible.map((item, index) => { const isHovered = hovered === index; return ( <motion.li key={`${item.name}-${index}`} className={cn("relative", index > 0 && config.overlap)} style={{ zIndex: isHovered ? count + 1 : index }} initial={false} animate={{ x: offset(index), y: isHovered && !reduceMotion ? -4 : 0, scale: isHovered && !reduceMotion ? 1.04 : 1 }} transition={transition} onPointerEnter={() => setHovered(index)} onPointerLeave={() => setHovered((h) => (h === index ? null : h))} > <Avatar role="img" aria-label={item.name} className={cn(config.avatar, "ring-2 ring-background")} > {item.src && <AvatarImage src={item.src} alt="" />} <AvatarFallback aria-hidden>{item.fallback ?? initials(item.name)}</AvatarFallback> </Avatar> {showNames && ( <AnimatePresence> {isHovered && ( <motion.span key="name" aria-hidden className="pointer-events-none absolute bottom-full left-1/2 mb-2 -translate-x-1/2 rounded-md border bg-popover px-2 py-1 text-xs font-medium whitespace-nowrap text-popover-foreground shadow-lg" initial={{ opacity: 0, y: reduceMotion ? 0 : 4, scale: reduceMotion ? 1 : 0.96 }} animate={{ opacity: 1, y: 0, scale: 1 }} exit={{ opacity: 0, y: reduceMotion ? 0 : 2, scale: reduceMotion ? 1 : 0.96, transition: { duration: 0.1 } }} transition={reduceMotion ? { duration: 0.15 } : { duration: 0.15, ease: "easeOut" }} > {item.name} </motion.span> )} </AnimatePresence> )} </motion.li> ); })} {hidden > 0 && ( <motion.li className={cn("relative", config.overlap)} style={{ zIndex: visible.length }} initial={false} animate={{ x: offset(count - 1) }} transition={transition} > <span role="img" aria-label={`${hidden} more`} className={cn( config.avatar, "flex items-center justify-center rounded-full bg-secondary font-medium text-secondary-foreground tabular-nums ring-2 ring-background" )} > <span aria-hidden>+{hidden}</span> </span> </motion.li> )} </ul> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| items | { name: string; src?: string; fallback?: ReactNode }[] | — | The people. name is used for the label, the name tag and the initials. |
| max | number | 4 | Avatars shown before the rest collapse into +N. |
| size | "sm" | "default" | "lg" | "default" | Avatar size and overlap. |
| spread | number | — | Extra pixels between avatars on hover. Defaults per size; 0 disables. |
| showNames | boolean | true | Show a name tag above the hovered avatar. |
| label | string | "People" | Accessible name for the list. |
Status Avatar
An avatar with a presence dot and a matching ring. Changing the status scales the new dot in and crossfades the ring; set pulse for a faint ring on live statuses (online, busy). Click the button to cycle through the statuses.
"use client";
import * as React from "react";
import { Button } from "@/components/ui/button";
import { StatusAvatar, type AvatarStatus } from "@/components/dusk/avatar/status-avatar";
const order: AvatarStatus[] = ["online", "away", "busy", "offline"];
export default function StatusAvatarDemo() {
const [status, setStatus] = React.useState<AvatarStatus>("online");
return (
<div className="flex flex-col items-center gap-8">
<div className="flex items-center gap-6">
<StatusAvatar name="Maya Chen" src="https://i.pravatar.cc/150?img=47" status="online" />
<StatusAvatar name="Leo Park" src="https://i.pravatar.cc/150?img=12" status="away" />
<StatusAvatar name="Ines Duarte" src="https://i.pravatar.cc/150?img=32" status="busy" />
<StatusAvatar name="Sam Okafor" status="offline" />
</div>
<div className="flex items-center gap-4">
<StatusAvatar name="Ava Lindqvist" src="https://i.pravatar.cc/150?img=5" status={status} size="lg" />
<Button
variant="outline"
size="sm"
onClick={() => setStatus((s) => order[(order.indexOf(s) + 1) % order.length])}
>
Status: <span className="capitalize">{status}</span>
</Button>
</div>
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/status-avatar.json1. Install the dependencies.
npm install motion @radix-ui/react-avatar2. Copy the source into
components/dusk/avatar/status-avatar.tsx.status-avatar.tsx "use client"; import * as React from "react"; import { AnimatePresence, motion, useReducedMotion } from "motion/react"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { cn } from "@/lib/utils"; export type AvatarStatus = "online" | "away" | "busy" | "offline"; const statuses: Record<AvatarStatus, { label: string; dot: string; ring: string; pulse: boolean }> = { online: { label: "Online", dot: "bg-emerald-400", ring: "ring-emerald-400/50", pulse: true }, away: { label: "Away", dot: "bg-amber-400", ring: "ring-amber-400/50", pulse: false }, busy: { label: "Busy", dot: "bg-rose-500", ring: "ring-rose-500/50", pulse: true }, offline: { label: "Offline", dot: "bg-zinc-500", ring: "ring-zinc-500/40", pulse: false }, }; const sizes = { sm: { avatar: "size-8 text-[0.65rem]", dot: "size-2.5", ring: "ring-[1.5px] ring-offset-[1.5px]" }, default: { avatar: "size-10 text-xs", dot: "size-3", ring: "ring-2 ring-offset-2" }, lg: { avatar: "size-14 text-sm", dot: "size-3.5", ring: "ring-2 ring-offset-[3px]" }, } as const; export interface StatusAvatarProps extends Omit<React.ComponentProps<"span">, "children"> { /** Full name. Used for the accessible label and the default initials. */ name: string; src?: string; /** Custom fallback content (defaults to initials). */ fallback?: React.ReactNode; status?: AvatarStatus; size?: keyof typeof sizes; /** Draw a colored ring around the avatar in the status color. */ showRing?: boolean; /** Pulse the dot for live statuses (online, busy). Off by default. */ pulse?: boolean; } function initials(name: string) { const parts = name.trim().split(/\s+/); return ((parts[0]?.[0] ?? "") + (parts.length > 1 ? parts[parts.length - 1][0] : "")).toUpperCase(); } /** * Avatar with a presence indicator. The status dot scales in when the status * changes, the optional ring crossfades to the new color, and `pulse` adds a * faint ring to live statuses. Labelled as “Name (Status)” for screen readers; * the pulse stops under reduced motion. */ export function StatusAvatar({ name, src, fallback, status = "online", size = "default", showRing = true, pulse = false, className, ...props }: StatusAvatarProps) { const reduceMotion = useReducedMotion() ?? false; const config = statuses[status]; const sizing = sizes[size]; return ( <span role="img" aria-label={`${name} (${config.label})`} className={cn("relative inline-flex shrink-0", className)} {...props} > <Avatar className={cn( sizing.avatar, showRing && [sizing.ring, config.ring, "ring-offset-background transition-[box-shadow] duration-300"] )} > {src && <AvatarImage src={src} alt="" />} <AvatarFallback aria-hidden>{fallback ?? initials(name)}</AvatarFallback> </Avatar> <span aria-hidden className={cn("absolute right-0 bottom-0 grid place-items-center", sizing.dot)}> {pulse && config.pulse && !reduceMotion && ( <motion.span key={`pulse-${status}`} className={cn("absolute inset-0 rounded-full", config.dot)} initial={{ scale: 1, opacity: 0.3 }} animate={{ scale: 1.8, opacity: 0 }} transition={{ duration: 2, ease: "easeOut", repeat: Infinity, repeatDelay: 1 }} /> )} <AnimatePresence initial={false}> <motion.span key={status} className={cn("relative col-start-1 row-start-1 size-full rounded-full ring-2 ring-background", config.dot)} initial={reduceMotion ? { opacity: 0 } : { scale: 0.6, opacity: 0 }} animate={{ scale: 1, opacity: 1 }} exit={reduceMotion ? { opacity: 0 } : { scale: 0.6, opacity: 0 }} transition={reduceMotion ? { duration: 0.15 } : { type: "spring", duration: 0.25, bounce: 0.15 }} /> </AnimatePresence> </span> </span> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| name | string | — | Full name, for the label and the initials. Required. |
| src | string | — | Image URL. Falls back to initials. |
| status | "online" | "away" | "busy" | "offline" | "online" | Presence state; sets the dot and ring color. |
| size | "sm" | "default" | "lg" | "default" | Avatar and dot size. |
| showRing | boolean | true | Draw a ring in the status color. |
| pulse | boolean | false | Add a faint pulse ring to the dot for online and busy. |
| fallback | ReactNode | — | Replaces the initials. |
API Reference
Same parts as shadcn/ui: Avatar, AvatarImage and AvatarFallback. Each forwards the Radix Avatar props.
| Prop | Type | Default | Description |
|---|---|---|---|
| className | string | — | Size and shape, e.g. size-12 rounded-lg (on Avatar). |
| src / alt | string | — | Image source and alternative text (on AvatarImage). |
| onLoadingStatusChange | (status: "idle" | "loading" | "loaded" | "error") => void | — | Called as the image loads (on AvatarImage). |
| delayMs | number | — | Wait before showing the fallback, to avoid a flash on fast connections (on AvatarFallback). |
Accessibility
- Always give
AvatarImageanalt; when only initials show, add anaria-labelor nearby text with the person's name. - Avatar Group is a labelled list; each avatar is
role="img"with the person's name, and the overflow chip reads “N more”. - Status Avatar is labelled “Name (Status)”, so the presence state is never conveyed by color alone.
- Under prefers-reduced-motion the image only fades, the group spreads instantly and the status pulse stops.