Components / Avatar
Avatar
Represent a person with a square image and a fallback. Compose identity deliberately so it remains clear when the image is unavailable.
Storybook runs locally or as a separately hosted build; no hosted Storybook is configured. Source and setup links match the install revision.
Usage playground
Switch between a self-contained portrait, a broken image and no source. Edit the source to try your own image. Observe Radix loading status and recovery; the status text is diagnostic, not a live announcement. Remount to restart the fallback delay.
Image status: idle
'use client';
import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/yads-avatar";
import { useState } from 'react';
Installation
pnpm dlx [email protected] add biehlio/yet-another-design-system/avatar#e7b31d0e0c9d8b24d53e97ca38b3b68d345e9f40This revision-pinned command installs yads-avatar and its scoped stylesheet, with Radix Avatar and JetBrains Mono. Import { Avatar, AvatarImage, AvatarFallback } from @/components/ui/yads-avatar (adjust your alias). The font is installed and imported automatically. No Tailwind, reset, global CSS or theme provider is required. Light is the default; the nearest explicit light/dark boundary wins. See API reference for native @scope browser support.
Copy sample copies a complete React module, including layout and recipe CSS, even when collapsed. Samples need only this registry item; interactive modules include a client boundary. Playground controls are documentation controls, not extra Avatar props.
Identity beside text
When the person’s name is already present, hide the entire avatar from assistive technology and use empty image alt. The visible name remains available during loading, delay and failure without duplicate identity announcements.
'use client';
import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/yads-avatar";
export default function ContributorIdentity() {
return <div style={{ display: 'flex', alignItems: 'center', gap: '1rem' }}>
className customization
The default square is 40px. Set width and height on the root; image and fallback fill it. Images crop with object-fit: cover. Adjust objectPosition on AvatarImage to preserve the important part of a portrait. This complete CSS recipe keeps scoped typography and themes, without a size API.
'use client';
import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/yads-avatar";
export default function LargePortrait() {
return <>
Accessibility
- Name meaningful images. Supply an appropriate alt on AvatarImage, such as the person’s name. Give the fallback the same identity with role="img" and aria-label; initials alone may not identify someone. Radix renders image or fallback, not both after successful loading, avoiding two names for the same portrait.
- Avoid duplicate identity. If adjacent text already identifies the person, use aria-hidden="true" on Avatar, empty alt on the image and no fallback name. Do not hide an avatar that is the only label for a control.
- Delay can leave a naming gap. Before an image loads and the fallback delay elapses, neither child may exist. Keep important identity in persistent adjacent text, or name a containing native control independently. Do not depend on delayed fallback text as a button’s only accessible name.
- Not a control or live region. Avatar adds no role, focus behavior, keyboard handlers or loading announcement. Use a named native button/link around it if interaction is needed; keep focus on that control outside the clipped root. Announce only useful application-level changes, not every image lifecycle transition.
API reference
Import { Avatar, AvatarImage, AvatarFallback, type AvatarProps, type AvatarImageProps, type AvatarFallbackProps } from @/components/ui/yads-avatar. Types follow the corresponding Radix parts. Default elements are span, img and span, with refs to those real nodes. Use one image per root. Put the fallback inside the same root context.
On narrow screens, focus the table and scroll horizontally to read all columns.
| Part / prop | Type | Default | Description |
|---|---|---|---|
Avatar | AvatarProps | span, 40 × 40px | Root context for one AvatarImage and its AvatarFallback. Children, className, style, native span props and span ref forward. |
AvatarImage src / alt | AvatarImageProps | Not set | Native img attributes and img ref forward when mounted. Supply meaningful alt or empty alt for a decorative image. Only renders after Radix preload succeeds. |
onLoadingStatusChange | (status: idle | loading | loaded | error) => void | Not set | Radix status changes, not a progress percentage or a native load event. Cached images may skip loading. |
AvatarFallback children | AvatarFallbackProps | Not set | Consumer-provided initials or other fallback. No initials or accessible name are generated. Native span props and ref forward while mounted. |
delayMs | number | Not set (immediate) | Fallback mount delay in milliseconds, not an image timeout. Once elapsed it stays eligible through source changes. Remount to restart the delay. |
asChild | boolean on each part | false | Inherited Radix composition: one compatible child must forward props/ref. Native element/ref descriptions assume default rendering. |
Loading, source changes and delay
Radix preloads src using a browser Image, propagating crossOrigin and referrerPolicy. Until loaded, AvatarImage renders no DOM image and has no mounted ref. Missing or broken src produces error; fallback renders when eligible. A changed src starts a new preload; success replaces the fallback and failure restores it. Source changes cancel old listeners; they do not restart an elapsed fallback delay. There is no guaranteed initial idle callback or visit to every status, especially for cached/data images. Native onLoad/onError concern the rendered img, not the preload; use onLoadingStatusChange for Radix state.
delayMs delays fallback eligibility from its mount; it neither delays image display nor aborts a slow request. Changing delayMs after it has elapsed does not hide an already eligible fallback. The playground deliberately remounts when changing delay or pressing Remount avatar. Reset restores the local portrait, immediate fallback and meaningful identity. Source, delay and identity controls are documentation controls, not new Avatar props.
The preloader follows src, crossOrigin and referrerPolicy, not srcSet/sizes, loading or decoding. Native image props are forwarded to the rendered image but do not configure preload scheduling or responsive selection. Supply a valid src even when using srcSet. Image/fallback refs attach and detach as their elements mount and unmount. Server markup initially has no loaded image; do not rely on AvatarImage as eager server-rendered content.
Themes, typography and browser support
Standalone is light. The nearest .dark or data-theme="dark" scope selects dark colors; .light or data-theme="light" stops an outer dark scope, including live changes and inner dark scopes. Explicit data-theme takes precedence over a conflicting class. Dark styling uses native @scope (Chrome/Edge 118+, Safari 17.4+, Firefox 146+); older browsers retain the light base. The registry installs and imports JetBrains Mono automatically. No Tailwind, reset, global tokens, site styles or provider is needed.
Root width/height default to 2.5rem; font size is .875rem. The image and fallback fill that square. Use className/style on any part for consumer dimensions and crop position. There is no group, overflow counter, generated profile service, automatic initials, size variant, request framework, retry policy or focus management API.