Avatar
A picture of a person or a team, with a fallback such as initials. Avatar.Fallback shows while Avatar.Image loads, and stays when the image fails or there is none. The image draws over the fallback, which is removed once the image has loaded.
On this page
import { Avatar, Stack } from '@tounsoo/todae';
// A drawn portrait stands in for the person's photo URL.
const PORTRAIT =
'data:image/svg+xml,' +
encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40"><rect width="40" height="40" fill="#d8d4cc"/><circle cx="20" cy="16" r="7" fill="#6b645a"/><path d="M6 40c2-9 8-13 14-13s12 4 14 13z" fill="#6b645a"/></svg>',
);
export default function AvatarStates() {
return (
<Stack.Root direction="row">
<Avatar.Root label="Ada Lovelace">
<Avatar.Image src={PORTRAIT} />
<Avatar.Fallback>AL</Avatar.Fallback>
</Avatar.Root>
<Avatar.Root label="Grace Hopper">
<Avatar.Fallback>GH</Avatar.Fallback>
</Avatar.Root>
<Avatar.Root label="Alan Turing">
<Avatar.Image src="data:image/png;base64,missing" />
<Avatar.Fallback>AT</Avatar.Fallback>
</Avatar.Root>
</Stack.Root>
);
}
Naming
With label, the avatar is one image named by label, whichever of the image and the fallback shows, so a screen reader reads "Ada Lovelace" rather than "AL". Avatar.Image takes no alt for that reason.
When the name is written next to the avatar, leave out label. The avatar is then decorative and hidden from assistive technology, so the name is not read twice.
import { Avatar, Stack, Text } from '@tounsoo/todae';
// A drawn portrait stands in for the person's photo URL.
const PORTRAIT =
'data:image/svg+xml,' +
encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40"><rect width="40" height="40" fill="#d8d4cc"/><circle cx="20" cy="16" r="7" fill="#6b645a"/><path d="M6 40c2-9 8-13 14-13s12 4 14 13z" fill="#6b645a"/></svg>',
);
export default function AvatarBesideName() {
return (
<Stack.Root direction="row" style={{ alignItems: 'center' }}>
<Avatar.Root>
<Avatar.Image src={PORTRAIT} />
<Avatar.Fallback>AL</Avatar.Fallback>
</Avatar.Root>
<Text.Root>Ada Lovelace</Text.Root>
</Stack.Root>
);
}
Ada Lovelace
Sizes
Avatar has no size prop. Todae does not ship a size scale. An avatar draws at avatar.size, 2rem. To change one, set --todae-avatar-size, or inline-size and block-size, on the avatar or on an ancestor with no [data-appearance], [data-density] or [data-motion] element and no scoped theme root between them. A ThemeProvider, a DensityProvider or a MotionProvider carries one of those attributes. Such an element, or a scoped theme root, in between declares the token again and drops the ancestor's value. The fallback text scales with --todae-avatar-size through avatar.font.scale.
import { Avatar, Stack } from '@tounsoo/todae';
import styles from './AvatarSizes.module.css';
export default function AvatarSizes() {
return (
<Stack.Root direction="row">
<Avatar.Root className={styles.small} label="Ada Lovelace">
<Avatar.Fallback>AL</Avatar.Fallback>
</Avatar.Root>
<Avatar.Root label="Ada Lovelace">
<Avatar.Fallback>AL</Avatar.Fallback>
</Avatar.Root>
<Avatar.Root className={styles.large} label="Ada Lovelace">
<Avatar.Fallback>AL</Avatar.Fallback>
</Avatar.Root>
</Stack.Root>
);
}
.small {
--todae-avatar-size: 1.5rem;
}
.large {
--todae-avatar-size: 2.5rem;
}
To change every avatar in a product, set avatar.size in your theme or in ThemeProvider overrides.
Initials are yours to choose: how to shorten a name depends on the language and the person. Restyle the shape through avatar.radius.
Colors
tone sets the avatar's colors, which show behind the fallback. neutral, the default, reads avatar.bg and avatar.fg. category-1 to category-8 read the avatar.category-<n> contract tokens, from color.category.<n>. To give each person a color that stays the same, pass a stable key such as a user id to categoryTone:
import { Avatar, categoryTone, Stack } from '@tounsoo/todae';
const PEOPLE = [
{ id: 'ada', name: 'Ada Lovelace', initials: 'AL' },
{ id: 'grace', name: 'Grace Hopper', initials: 'GH' },
{ id: 'alan', name: 'Alan Turing', initials: 'AT' },
{ id: 'katherine', name: 'Katherine Johnson', initials: 'KJ' },
{ id: 'edsger', name: 'Edsger Dijkstra', initials: 'ED' },
];
export default function AvatarTones() {
return (
<Stack.Root direction="row" style={{ flexWrap: 'wrap' }}>
{PEOPLE.map((person) => (
<Avatar.Root key={person.id} label={person.name} tone={categoryTone(person.id)}>
<Avatar.Fallback>{person.initials}</Avatar.Fallback>
</Avatar.Root>
))}
</Stack.Root>
);
}
Stacked avatars
A few avatars stacked, such as assignees, are the Avatar group recipe: a div with role="group", some Avatar.Roots, and CSS you copy.
Accessible composition
Todae handles: With label, Avatar.Root is role="img" named by aria-label, and Avatar.Image renders alt="", so the image and the initials share one name. Without label, the root is aria-hidden. The types leave out role, aria-label and aria-labelledby on the root and alt on the image, so a wrapper cannot add a second name by mistake.
You supply: The choice between the two. A wrapper that shows a person should pass label when the avatar stands alone, such as in a row of participants, and leave it out when the name is written next to it. The name comes from your data; Todae does not read it from the fallback.
Watch for: An avatar with no label as the only content of a button, a link or a Menu trigger: the avatar is hidden, so the control has no name. Give the control aria-label, or the avatar label. A presence dot or ring the wrapper adds shows status by color alone; put the status in text as well.
When to use
When: A person, team or workspace shown in a list, a header or next to a message.
When not: A picture with content of its own → <img> with real alt text. A status or count → Badge. A control that opens a profile menu → put the avatar inside a Menu trigger, which carries the name.
Props
| Name | Type | Description |
|---|---|---|
label | string | Names the avatar, whichever of the image and the fallback shows. Leave it out when the name is written next to the avatar. |
tone | "neutral"| "category-1"| "category-2"| "category-3"| "category-4"| "category-5"| "category-6"| "category-7"| "category-8" | Colors behind the fallback. neutral reads avatar.bg and avatar.fg; other tones read the avatar.<tone> contract tokens. categoryTone(id) picks a stable category per person. Default: "neutral" |
children | React.ReactNode | Avatar.Image and Avatar.Fallback. |
| Name | Type | Description |
|---|---|---|
src | string | The image, or srcSet in its place. Without either, or when it fails to load, the fallback stays. |
| Name | Type | Description |
|---|---|---|
children | React.ReactNode | Initials or an icon. |