Avatar
A person or a place as a picture, or their initials in a colour of their own.
Installation
pnpm dlx @human-kit/design add avatarnpx @human-kit/design add avataryarn dlx @human-kit/design add avatarbunx @human-kit/design add avatarThe command copies the files to src/lib/components/ui/avatar. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Avatar from '$lib/components/ui/avatar';
</script>
<Avatar.Root name="Ana María Ruiz" /> Examples
Group
Avatar.Group sets the avatars in a row, 2px apart. Past max, the rest are counted at the end: "+2", read out as "2 more". Name the group with aria-label.
Guidelines
- Give the full
name, not the initials: the avatar takes the initials and the tone from it, and reads the name out whole. - Leave
toneout. The same name always gets the same tone, so a person keeps one colour across the app. Set it only for a mark that means something of its own. - Use
smbeside a row’s text,defaultbeside a button, andlgat the head of a card. - Give
srcwhen there is a picture. The initials show until it loads, and stay when it fails, so a broken image never shows. - Set
decorativewhen the name is written right beside the avatar, so it is not read twice.
Accessibility
- The picture is an
<img>with the name as itsalt. Without a picture, the initials are an image named with the name: a screen reader says "Ana María Ruiz", not "A M". decorativeempties thealtand hides the avatar from screen readers.- The initials are white on a muted fill, at 4.7:1 or more on every tone in light and 5.6:1 in dark. The +N count is grey on a light grey chip, at 6:1.
- Nothing in an avatar takes the focus. To make one open a profile, put it inside a link or a button that has a name.
Avatar.Groupis agroupnamed by youraria-label, and its count is named "N more".
API reference
Avatar.Root
| Prop | Type | Default |
|---|---|---|
name | string Required. Who or what it shows. It gives the initials, the tone and the name read out. | — |
src | string A picture, in the place of the initials. | — |
size | 'sm' | 'default' | 'lg' The side: 20, 24 or 32px. | 'default' |
tone | 'gray' | 'blue' | 'cyan' | 'teal' | 'green' | 'amber' | 'orange' | 'red' | 'pink' | 'violet' A tone of its own, in place of the one the name gives. | — |
decorative | boolean Leaves it unnamed, for when the name is written beside it. | false |
onStatusChange | (status: 'loading' | 'loaded' | 'error') => void Called when the picture starts to load, loads or fails. | — |
element | HTMLSpanElement | null The rendered element. Bindable. | — |
Plus every attribute of a <span>.
Avatar.Group
| Prop | Type | Default |
|---|---|---|
max | number How many avatars show. The rest are counted at the end. Without it, all show. | — |
size | 'sm' | 'default' | 'lg' The size of the count at the end: give the size of the avatars. | 'default' |
aria-label | string The name of the group, such as "Care team". | — |
element | HTMLDivElement | null The rendered element. Bindable. | — |
Plus every attribute of a <div>.