Actions
Avatar
An image that keeps a graceful stand-in while it loads or fails.
When to use it: Use it for profile pictures and other images that may be slow or missing.
Example
import { Avatar, AvatarFallback, AvatarImage } from "@comp0/react";
const portrait =
"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 40 40'%3E%3Crect width='40' height='40' fill='%230f766e'/%3E%3Ccircle cx='20' cy='15' r='7' fill='%23f4f4f5'/%3E%3Cpath d='M6 40a14 12 0 0 1 28 0z' fill='%23f4f4f5'/%3E%3C/svg%3E";
export function Example() {
return (
<div className="flex items-center gap-4">
<Avatar className="flex size-12 items-center justify-center overflow-hidden rounded-full bg-zinc-200 dark:bg-zinc-800">
<AvatarImage
src={portrait}
alt="Illustrated portrait of Ada Lovelace"
className="size-full object-cover"
/>
<AvatarFallback className="text-base font-medium text-zinc-600 sm:text-sm dark:text-zinc-300">
AL
</AvatarFallback>
</Avatar>
<Avatar className="flex size-12 items-center justify-center overflow-hidden rounded-full bg-zinc-200 dark:bg-zinc-800">
<AvatarImage src="data:," alt="Ada Kaplan" className="size-full object-cover" />
<AvatarFallback className="text-base font-medium text-zinc-600 sm:text-sm dark:text-zinc-300">
AK
</AvatarFallback>
</Avatar>
</div>
);
}Anatomy
Dashed frames are invisible state providers; shaded shapes own real DOM. Numbered pins match the list below.
A wireframe sketch of the assembled component. Each numbered marker matches a part in the list that follows.
Avatar
Span that tracks the image status and exposes it as data attributes. Owns a DOM element.
AvatarImage
Native img, hidden until a load succeeds. Owns a DOM element.
AvatarFallback
Stand-in such as initials or an icon, hidden once the image loads. Owns a DOM element.
Step by step
- 1
Add the main part
Wrap Avatar around one AvatarImage and one AvatarFallback.
- 2
Add the supporting parts
Give AvatarImage a src and an alt that names the person; put initials or an icon in AvatarFallback.
- 3
Make the behavior clear
Style the settled states through [data-loaded] and [data-error] on Avatar.
Exampletsx <Avatar> <AvatarImage src={photoUrl} alt="Ada Kaplan" /> <AvatarFallback>AK</AvatarFallback> </Avatar>;
Keyboard
Forms and accessibility
No form behavior; an avatar only displays an image.
Accessibility checklist
- Give AvatarImage an alt that names the person or entity; use alt="" only when adjacent text already names them.
- Keep the fallback recognizable: initials or an icon, not an empty box.
- The image stays hidden until it loads, so a broken-image glyph is never announced or shown.
API reference
import { Avatar, AvatarFallback, AvatarImage } from "@comp0/react";Avatar
DOM elementSpan that tracks the image status and exposes it as data attributes.
| Prop | Type | Description |
|---|---|---|
children | ReactNode | One AvatarImage and one AvatarFallback, in either order. |
AvatarImage
DOM elementNative img, hidden until a load succeeds.
| Prop | Type | Description |
|---|---|---|
src | string | Image source; a data: URI works for inline artwork. |
alt | string | Names the person or entity; "" only when adjacent text already does. |
onLoad | (event: SyntheticEvent) => void | Run before the avatar reacts; preventDefault keeps the status unchanged. |
onError | (event: SyntheticEvent) => void | Run before the avatar reacts; preventDefault keeps the status unchanged. |
AvatarFallback
DOM elementStand-in such as initials or an icon, hidden once the image loads.
| Prop | Type | Description |
|---|---|---|
children | ReactNode | The initials or icon to show while there is no image. |
Style hooks
Attributes that appear while a state is true. Target them with Tailwind data variants such as data-open:bg-zinc-100, or with any CSS selector.
Avatar
| Style hook | Meaning |
|---|---|
[data-loaded] | The image loaded; the fallback is hidden. |
[data-error] | The image failed to load; the fallback stays visible. |