Skip to the content
human-kit design
49 — Data display

Avatar

A person or a place as a picture, or their initials in a colour of their own.

AM

Installation

pnpm dlx @human-kit/design add avatar
npx @human-kit/design add avatar
yarn dlx @human-kit/design add avatar
bunx @human-kit/design add avatar

The 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.

AM JO LF +2

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 tone out. 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 sm beside a row’s text, default beside a button, and lg at the head of a card.
  • Give src when there is a picture. The initials show until it loads, and stay when it fails, so a broken image never shows.
  • Set decorative when the name is written right beside the avatar, so it is not read twice.

Accessibility

  • The picture is an <img> with the name as its alt. Without a picture, the initials are an image named with the name: a screen reader says "Ana María Ruiz", not "A M".
  • decorative empties the alt and 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.Group is a group named by your aria-label, and its count is named "N more".

API reference

Avatar.Root

PropTypeDefault
namestring

Required. Who or what it shows. It gives the initials, the tone and the name read out.

—
srcstring

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.

—
decorativeboolean

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.

—
elementHTMLSpanElement | null

The rendered element. Bindable.

—

Plus every attribute of a <span>.

Avatar.Group

PropTypeDefault
maxnumber

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-labelstring

The name of the group, such as "Care team".

—
elementHTMLDivElement | null

The rendered element. Bindable.

—

Plus every attribute of a <div>.