A picture of a person or a thing, with something to show in its place when there is no picture or it does not load. It has no action and nothing pending: its one state, whether the image loaded, is a fact the browser owns, and the avatar mirrors it as data-status. The <img> is in the server HTML from the first render, the fallback is under it, and the name is on the Root, so the same avatar works before hydration, without script, and when the picture fails.
idleidleidle"use client";
// Three people: a picture that loads, one whose picture fails (initials take its place), and one
// with no picture at all. Each Root is named by `alt`; the status under each avatar comes from
// the Root's `render` function, so it sits outside the role="img" element. "Change picture"
// swaps Ada's source; the Root is keyed by it, so the new picture is a new avatar that fades in.
import { Avatar } from "@funstack/ui/avatar";
import { useState } from "react";
/** A self-contained picture (an SVG data URL), so the example needs no image host. */
function picture(from: string, to: string): string {
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="80" height="80" viewBox="0 0 80 80">
<defs><linearGradient id="g" x2="1" y2="1"><stop offset="0" stop-color="${from}"/><stop offset="1" stop-color="${to}"/></linearGradient></defs>
<rect width="80" height="80" fill="url(#g)"/><circle cx="40" cy="32" r="14" fill="#fff" fill-opacity=".85"/>
<path d="M14 80c4-18 14-26 26-26s22 8 26 26z" fill="#fff" fill-opacity=".85"/></svg>`;
return `data:image/svg+xml,${encodeURIComponent(svg)}`;
}
const pictures = [picture("#6366f1", "#ec4899"), picture("#0ea5e9", "#22c55e")];
const people = [
{ id: "ada", name: "Ada Lovelace", initials: "AL" },
// Bytes that are no image: decoding fails, as for a URL that is gone, and the Fallback stays.
{ id: "grace", name: "Grace Hopper", initials: "GH", src: "data:image/png;base64,AAAA" },
{ id: "alan", name: "Alan Turing", initials: "AT" },
];
export function AvatarPeople() {
const [picked, setPicked] = useState(0);
return (
<div className="demo-stack">
<ul className="demo-avatar-people">
{people.map((person) => {
const src = person.id === "ada" ? pictures[picked] : person.src;
return (
<li key={person.id}>
<Avatar.Root
// Keyed by the source: with the fade-in recipe (avatar.css) a new picture on the
// same avatar would first fade the old one out; a new avatar fades in instead.
key={src}
alt={person.name}
className="demo-avatar"
// The function form wraps the avatar in a figure, so the caption is outside the
// role="img" element (its children are presentational).
render={(props, { status }) => (
<figure className="demo-avatar-figure">
<span {...props} />
<figcaption>
{person.name}
<code>{status}</code>
</figcaption>
</figure>
)}
>
<Avatar.Image className="demo-avatar-image" src={src} width={80} height={80} />
<Avatar.Fallback className="demo-avatar-fallback">
{person.initials}
</Avatar.Fallback>
</Avatar.Root>
</li>
);
})}
</ul>
<p>
<button
type="button"
className="demo-button"
onClick={() => setPicked((p) => (p + 1) % pictures.length)}
>
Change Ada's picture
</button>
</p>
</div>
);
}/* Avatar: the image and the fallback share one grid cell, the image above. A loaded picture
paints over the initials; a broken <img alt=""> paints nothing, so the initials show, with or
without script. */
.demo-avatar {
display: inline-grid;
inline-size: 4rem;
aspect-ratio: 1;
border-radius: 50%;
overflow: clip;
}
.demo-avatar > * {
grid-area: 1 / 1;
}
.demo-avatar-image {
inline-size: 100%;
block-size: 100%;
object-fit: cover;
}
.demo-avatar-fallback {
display: grid;
place-items: center;
background: var(--color-bg-secondary);
color: var(--color-text);
font-weight: 600;
}
/* No flash of initials during a fast load: while an image may still arrive (:has(img)), the
fallback becomes visible only after 400ms. Works before hydration too. */
.demo-avatar:has(img) .demo-avatar-fallback {
animation: demo-avatar-fallback-in 1ms 400ms backwards;
}
@keyframes demo-avatar-fallback-in {
from {
opacity: 0;
}
}
/* Fade the picture in, only where script runs (without it the status never leaves "idle").
It would also fade a loaded picture out when its src changes (the status goes back to
"loading"), so the example keys each Root by its source. */
@media (scripting: enabled) {
.demo-avatar-image {
transition: opacity 150ms;
}
.demo-avatar-image[data-status="loading"] {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
.demo-avatar-image {
transition: none;
}
}
/* The demo's layout. */
.demo-avatar-people {
display: flex;
flex-wrap: wrap;
gap: 1.5rem;
margin: 0;
padding: 0;
list-style: none;
}
.demo-avatar-figure {
display: grid;
justify-items: center;
gap: 0.5rem;
margin: 0;
}
.demo-avatar-figure figcaption {
display: grid;
justify-items: center;
font-size: 0.875rem;
}
.demo-avatar-figure code {
color: var(--color-text-secondary);
}import { Avatar } from "@funstack/ui/avatar";
<Avatar.Root alt={user.name} className="avatar"> {/* span role="img" aria-label */}
<Avatar.Image src={user.photoUrl} className="avatar-image" /> {/* img alt="" */}
<Avatar.Fallback className="avatar-fallback">{initials(user.name)}</Avatar.Fallback>
</Avatar.Root>
// Next to the visible name: decorative.
<a href={`/users/${user.id}`}>
<Avatar.Root alt="" className="avatar"> {/* span aria-hidden="true" */}
<Avatar.Image src={user.photoUrl} />
<Avatar.Fallback><UserIcon /></Avatar.Fallback>
</Avatar.Root>
{user.name}
</a>| Part | Element | Rendered when |
|---|---|---|
Avatar.Root | span | always |
Avatar.Image | img, alt="" | it has a src (or srcSet) and that source has not failed |
Avatar.Fallback | span | the status is not "loaded" |
The library ships no CSS: stack the image over the fallback in one grid cell (see Recipes). While the image loads both are in the DOM and the picture paints over the initials when it arrives; nothing is swapped, so there is no delay prop.
Avatar.Root| Prop | Type | Default | Description |
|---|---|---|---|
alt | string | – | Required. The avatar's text alternative, with the contract of <img alt>: who or what is pictured ("Jane Doe", not "avatar"), or "" when the avatar is decoration next to text that already says it. Non-empty: the Root is role="img" with this aria-label, in every status. Empty: aria-hidden="true". |
className | string | ((state: AvatarState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: AvatarState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
children | ReactNode | ((state: AvatarState) => ReactNode) | – | The Avatar.Image and Avatar.Fallback, or a function of the state. |
render | ReactElement | ((props, state: AvatarState) => ReactNode) | – | Replace the default <span> (element form) or take over rendering (function form). |
AvatarState is { status }, shared by every part.role, aria-label, aria-labelledby (always absent, also on a render element: the one naming route is alt), aria-hidden, data-status.The name comes from alt. Everything else passes through to the span, ref included.
Avatar.Image| Prop | Type | Default | Description |
|---|---|---|---|
src | string | – | The picture. Without src and srcSet (or with "") the part renders nothing and the Fallback is the avatar. A new src keeps the same element: the browser paints the old picture until the new one is available. |
className | string | ((state: AvatarState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: AvatarState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
render | ReactElement | ((props, state: AvatarState) => ReactNode) | – | Replace the <img> (element form) or take over rendering (function form). It must render an <img> and pass the ref on: the part listens through it. |
AvatarState.alt (always "": the Root carries the name), data-status.Every other <img> prop passes through: srcSet, sizes, width, height, loading, decoding, fetchPriority, crossOrigin, referrerPolicy, and your own onLoad / onError (see Suspense for what an onLoad costs). No children.
Avatar.Fallback| Prop | Type | Default | Description |
|---|---|---|---|
className | string | ((state: AvatarState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: AvatarState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
children | ReactNode | ((state: AvatarState) => ReactNode) | – | Initials, an icon, or a function of the state. |
render | ReactElement | ((props, state: AvatarState) => ReactNode) | – | Replace the default <span> (element form) or take over rendering (function form). |
AvatarState.data-status.Not rendered at all once the status is "loaded"; a function render or children does not run then.
| `status` | Meaning | Image | Fallback |
|---|---|---|---|
"idle" | No image has reported: the server render, the hydration render, no Avatar.Image, or one with no source. | rendered if it has a source | rendered |
"loading" | The image has a source and is not complete (also a loading="lazy" image off screen). | rendered | rendered, under it |
"loaded" | The image is available. | rendered | not rendered |
"error" | The image's current source failed. It is not tried again; a different src mounts a new <img>. | not rendered | rendered |
| Attribute | On | Meaning |
|---|---|---|
data-status | every part | "idle", "loading", "loaded" or "error": the same value as state.status. |
| Case | Rendered | Announced |
|---|---|---|
alt="Jane Doe", any status | span[role=img][aria-label="Jane Doe"] | "Jane Doe, image", once |
alt="", any status | span[aria-hidden=true] | nothing |
role="img" are presentational: the inner <img alt=""> and the fallback's text never add a second name, and the name does not change when the picture fails. With the name on the <img> instead, a failed picture would leave "JD" as the only thing a screen reader can read.Concept: Transitions & ViewTransition
None, and no transition prop: the status changes in an urgent update, which starts no view transition. Animate the swap with CSS (the fade-in recipe below). A <ViewTransition> of your own around the avatar animates its mount, and makes React wait for the image (Suspense, below).
Concept: Pending & optimistic state
None: no action, nothing pending, nothing optimistic. The status is written with a plain setState from the image's native load / error events, never in a transition, so an unrelated pending action elsewhere in the app does not hold initials over a picture that has loaded.
Concept: Suspense
The avatar does not suspend and owns no boundary. React 19.3 already waits for images: when an <img> is inserted (or its src changes) in a transition under a <ViewTransition>, react-dom holds the commit until the image is ready or a short timeout passes. Avatar.Image is built so that this applies to it.
// The card is revealed with the picture in place, or after the timeout with the initials.
<ViewTransition>
<Suspense fallback={<ProfileSkeleton />}>
<Profile /> {/* suspends on data, renders <Avatar.Root> */}
</Suspense>
</ViewTransition>onLoad prop or loading="lazy". The part listens with native listeners through its ref and never renders onLoad, so the wait applies. Your own onLoad or loading="lazy" on Avatar.Image opts that image out, which is React's documented way to do so."loaded": the fallback is never painted. When the timeout won, the avatar starts at "loading" with the initials under it.transition prop (a Tabs.Panel, a Dialog.Popup) are under a <ViewTransition>, so switching to a panel waits for its avatars, bounded by the timeout, with data-pending on the trigger meanwhile. The timeout is one clock for the whole commit, not one per image.Concept: Browser-owned state
The image's load state. The avatar mirrors it from load / error and from img.complete at mount, so a picture that finished before hydration (whose event no listener heard) is right in the hydration commit: "loaded", or "error" for one that failed. A complete image with no natural width is decided by img.decode().
Concept: Server Components
The server HTML has the Root with its name, the <img alt=""> and the Fallback, all data-status="idle", so the browser starts the request from the HTML (react-dom also emits a <link rel="preload" as="image">). Without script the picture paints over the initials when it arrives, and a broken image with alt="" paints nothing, so the initials stay. Every part is a client module; a Server Component renders them with serialisable props.
The status stays "idle" without script, so a stylesheet that hides the image while [data-status="loading"] never hides it before hydration. Use @media (scripting: enabled) for anything that depends on the status leaving "idle".
One grid cell, the image above the fallback:
.avatar { display: inline-grid; inline-size: 2.5rem; aspect-ratio: 1; border-radius: 50%; overflow: clip; }
.avatar > * { grid-area: 1 / 1; }
.avatar-image { inline-size: 100%; block-size: 100%; object-fit: cover; }
.avatar-fallback { display: grid; place-items: center; background: var(--avatar-bg); }:has(img) is true exactly while a picture may still arrive (the part removes the <img> on error and renders none without a source), so the delay applies only then, and works before hydration:
.avatar:has(img) .avatar-fallback { animation: avatar-fallback-in 1ms 400ms backwards; }
@keyframes avatar-fallback-in { from { opacity: 0; } }@media (scripting: enabled) {
.avatar-image { transition: opacity 150ms; }
.avatar-image[data-status="loading"] { opacity: 0; }
}This hides every image whose status is "loading", so it also fades a loaded picture out when its src changes (the status goes back to "loading" on the same element) and shows the initials until the new one arrives. CSS cannot tell a first load from a change. If a mounted avatar can change its picture, leave the fade out, or key the Root by the source (<Avatar.Root key={src} …>): each picture is then a new avatar that fades in like a first load, as in the example above.
Transparent pictures (a PNG logo) let the initials show through until hydration removes the fallback: give .avatar-image a background. Tailwind: data-[status=loading]:opacity-0, has-[img]:….
/* Avatar: the image and the fallback share one grid cell, the image above. A loaded picture
paints over the initials; a broken <img alt=""> paints nothing, so the initials show, with or
without script. */
.demo-avatar {
display: inline-grid;
inline-size: 4rem;
aspect-ratio: 1;
border-radius: 50%;
overflow: clip;
}
.demo-avatar > * {
grid-area: 1 / 1;
}
.demo-avatar-image {
inline-size: 100%;
block-size: 100%;
object-fit: cover;
}
.demo-avatar-fallback {
display: grid;
place-items: center;
background: var(--color-bg-secondary);
color: var(--color-text);
font-weight: 600;
}
/* No flash of initials during a fast load: while an image may still arrive (:has(img)), the
fallback becomes visible only after 400ms. Works before hydration too. */
.demo-avatar:has(img) .demo-avatar-fallback {
animation: demo-avatar-fallback-in 1ms 400ms backwards;
}
@keyframes demo-avatar-fallback-in {
from {
opacity: 0;
}
}
/* Fade the picture in, only where script runs (without it the status never leaves "idle").
It would also fade a loaded picture out when its src changes (the status goes back to
"loading"), so the example keys each Root by its source. */
@media (scripting: enabled) {
.demo-avatar-image {
transition: opacity 150ms;
}
.demo-avatar-image[data-status="loading"] {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
.demo-avatar-image {
transition: none;
}
}
/* The demo's layout. */
.demo-avatar-people {
display: flex;
flex-wrap: wrap;
gap: 1.5rem;
margin: 0;
padding: 0;
list-style: none;
}
.demo-avatar-figure {
display: grid;
justify-items: center;
gap: 0.5rem;
margin: 0;
}
.demo-avatar-figure figcaption {
display: grid;
justify-items: center;
font-size: 0.875rem;
}
.demo-avatar-figure code {
color: var(--color-text-secondary);
}viewBox the default object size (300 × 150) as its natural size; it reports "loaded" like any other picture.onStatusChange handler (the status is in every function prop; your native onError passes through).