Avatar
@codeui/solid v0.3.0
A person: their picture, or their initials on a colour from their id.
When to use
Use Avatar wherever the interface shows a person: the owner of a task, the author of a comment, the members of a group. It shows the person's picture when there is one. Otherwise it shows one or two initials on a fill whose hue comes from a stable id, so the same person keeps the same colour on every screen and two people with the same name do not share one.
Use one Avatar for every person in the app. Do not build a separate badge or initials chip per feature.
When to choose another component
- Use
Badgefor a status or a category. An avatar always stands for a person or an owner. - Use the item's own mark, such as a provider or repository logo in a rounded tile, for something that is not a person.
Live demo
import { Avatar, Stack } from "@codeui/solid";
import { picture } from "./people";
export default function AvatarBasic() {
return (
<Stack direction="row" gap="2" align="center">
<Avatar size="sm" name="Ada Lovelace" identifier="user_ada" src={picture} />
<Avatar size="sm" name="Grace Hopper" identifier="user_grace" />
<Avatar size="sm" name="Alan Turing" identifier="user_alan" />
<Avatar size="sm" unassigned />
</Stack>
);
}Hover an avatar to see the name in the kit Tooltip.
Anatomy
Tap a part to outline it in the demo.
- 01Avatar
avatarclasses.root - 02Fallback
avatar-fallbackclasses.fallback - 03Image
avatar-imageclasses.image
| Export | Renders | classes key | data-cui | Role |
|---|---|---|---|---|
Avatar | span | root | avatar | img, named by name |
| Initials | span, hidden from assistive technology | fallback | avatar-fallback | None |
| Picture | img with an empty alt, only when src is set | image | avatar-image | None |
The root also renders data-size (xs, sm, md or lg, default xs) and data-unassigned when the avatar is unassigned. The picture covers the initials while it loads, so a slow or failed picture never leaves an empty circle.
Variants
Hue from the id
import { Avatar, Stack, Text } from "@codeui/solid";
export default function AvatarHue() {
return (
<Stack gap="3">
<Stack direction="row" gap="2" align="center">
<Avatar size="sm" name="Sam Lee" identifier="user_1041" />
<Avatar size="sm" name="Sam Lee" identifier="user_2288" />
<Text size="xs" tone="muted">
Two people named Sam Lee: two ids, two hues.
</Text>
</Stack>
<Stack direction="row" gap="2" align="center">
<Avatar size="sm" name="Sam Lee" identifier="user_1041" />
<Avatar size="sm" name="Samantha Lee" identifier="user_1041" />
<Text size="xs" tone="muted">
One person who changed their name: one id, one hue.
</Text>
</Stack>
<Stack direction="row" gap="2" align="center">
<Avatar size="sm" name="Guest" />
<Text size="xs" tone="muted">
No id: a neutral fill.
</Text>
</Stack>
</Stack>
);
}identifier takes a stable id, such as a user id. avatarHue(identifier) hashes it to a hue between 0 and 359, and the fill uses that hue at a fixed lightness and chroma per theme. Never pass the name: a name changes and two people can share it. Without identifier the fill is neutral grey.
Size
import { Avatar, Stack, Text } from "@codeui/solid";
import { For } from "solid-js";
export default function AvatarSizes() {
return (
<Stack gap="3">
<For each={["compact", "comfortable", "touch"] as const}>
{density => (
<Stack direction="row" gap="3" align="center" data-cui-density={density}>
<Text size="xs" tone="muted" style={{ width: "6rem" }}>
{density}
</Text>
<For each={["xs", "sm", "md", "lg"] as const}>
{size => <Avatar size={size} name="Grace Hopper" identifier="user_grace" />}
</For>
</Stack>
)}
</For>
</Stack>
);
}size takes a step of the shared control scale, so an avatar lines up with the buttons and fields next to it, and it follows data-cui-density.
size | Compact | Comfortable | Touch | Initials |
|---|---|---|---|---|
xs (default) | 24px | 28px | 32px | 11px (--cui-font-size-badge) |
sm | 32px | 36px | 40px | 13px (--cui-font-size-caption) |
md | 38px | 44px | 50px | 14px (--cui-font-size-sm) |
lg | 46px | 52px | 58px | 16px (--cui-font-size-md) |
Initials
avatarInitials(name) takes the first letter of the first and of the last word: "Ada King Lovelace" becomes AL, and "Linus" becomes L. It counts code points, so accented and non-Latin letters stay whole. Pass initials when your data has better ones.
States
import { Avatar, Stack, Text } from "@codeui/solid";
import { picture } from "./people";
export default function AvatarStates() {
return (
<Stack direction="row" gap="6" align="center">
<Stack gap="2" align="center">
<Avatar size="md" name="Ada Lovelace" identifier="user_ada" src={picture} />
<Text size="xs" tone="muted">
Picture
</Text>
</Stack>
<Stack gap="2" align="center">
<Avatar
size="md"
name="Ada Lovelace"
identifier="user_ada"
src="/missing-avatar.png"
/>
<Text size="xs" tone="muted">
Picture failed
</Text>
</Stack>
<Stack gap="2" align="center">
<Avatar size="md" unassigned />
<Text size="xs" tone="muted">
Unassigned
</Text>
</Stack>
</Stack>
);
}Picture
Set src. The picture fills the circle with object-fit: cover, under the same ring as the initials.
Picture failed
When the picture fails to load, Avatar removes it and the initials show.
Unassigned
unassigned draws an empty circle with a dashed edge in secondary text colour. It has no fill and no initials. Its accessible name and tooltip are "Unassigned" unless you pass name.
Ready API
Avatar
Prop
Type
Every other prop goes to the root span. Avatar wraps no Kobalte primitive. With tooltip, the root sits inside the kit Tooltip trigger.
avatarHue
avatarHue(identifier: string): number returns the hue Avatar uses for an id, an FNV-1a hash folded to 0–359. Use it to colour something else that belongs to the same person.
avatarInitials
avatarInitials(name: string): string returns the one or two initials Avatar shows for a name, in upper case.
Styled parts API
Import the parts from @codeui/solid/avatar/parts. The entry also exports avatarParts, avatarHue, avatarInitials and the types AvatarPart, AvatarClasses, AvatarSize and AvatarVariants.
Root
The circle. It takes size, unassigned, hue and every span attribute. Give it role="img" and the name as aria-label.
Prop
Type
Image
The picture. It renders while src is set and removes itself on error. Its alt defaults to empty, because the root carries the name.
Fallback
The initials. It sets aria-hidden="true".
Styles export
avatarStyles
avatarStyles.root(), avatarStyles.image() and avatarStyles.fallback() return cui-avatar, cui-avatar-image and cui-avatar-fallback. On custom markup, set data-size and --cui-avatar-hue.
Accessibility
The root has role="img" and the name as its accessible name, so a screen reader reads "Ada Lovelace, image". The initials are hidden, so they are not read letter by letter, and the picture has an empty alt.
The name also shows in the kit Tooltip on hover. The avatar is not focusable, so put it next to text or inside a control that names the person for keyboard users, such as a row title or a button label.
The initials reach 4.5:1 against their fill on every hue in both themes: at least 5.5:1 in dark and 10:1 in light. The fill itself can sit close to the surface, which is why the circle has the neutral ring.
SSR notes
Avatar renders completely on the server, with the picture and the initials. The hue is computed from the id, so the server and the client agree. The tooltip content renders on the client when it opens.
Theming hooks
| Variable | Default | Used for |
|---|---|---|
--cui-avatar-hue | Set from identifier | Fill hue |
--cui-avatar-lightness | light-dark(0.86, 0.48) | Fill lightness in OKLCH |
--cui-avatar-chroma | light-dark(0.06, 0.07); 0 without an id | Fill chroma in OKLCH |
--cui-avatar-color | --cui-text-default | Initials |
--cui-avatar-ring | --cui-text-default at 20% | The 1px inner ring |
--cui-avatar-unassigned-color | --cui-text-muted | Dashed edge of an unassigned avatar |
--cui-avatar-size, --cui-avatar-font-size | From data-size | Diameter and initials size |
--cui-radius-full | Full radius |
Composition examples
In a row
import { Avatar, Stack, Text } from "@codeui/solid";
import { For } from "solid-js";
const reviews = [
{
title: "Fix flaky hydration test",
owner: "Grace Hopper",
id: "user_grace",
time: "2h",
},
{
title: "Port the tooltip to Solid 2",
owner: "Alan Turing",
id: "user_alan",
time: "5h",
},
{ title: "Draft the release notes", owner: undefined, id: undefined, time: "1d" },
];
export default function AvatarInRow() {
return (
<Stack gap="0" style={{ width: "100%", "max-width": "360px" }}>
<For each={reviews}>
{review => (
<Stack
direction="row"
gap="2"
align="center"
style={{ "min-height": "var(--cui-control-height-sm)" }}
>
<Avatar
size="xs"
name={review.owner}
identifier={review.id}
unassigned={!review.owner}
/>
<Text style={{ flex: "1", "min-width": "0" }}>{review.title}</Text>
<Text
size="xs"
tone="muted"
style={{ "font-variant-numeric": "tabular-nums" }}
>
{review.time}
</Text>
</Stack>
)}
</For>
</Stack>
);
}In a list row the avatar sits in the leading slot and tells the rows apart by owner. An item with no owner shows the unassigned circle, so every row keeps the same shape.