Avatar

Slant / People & chat

A photo or initials avatar with an optional presence pixel.

Demo

Usage

@tuttn/slant
import { Avatar } from '@tuttn/slant'

// name is the tooltip and what screen readers hear; status adds the presence pixel.
<Avatar initials="AK" name="Ada K." status="online" />
<Avatar initials="JR" name="Juno R." status="away" tone="accent" />

// Without initials, they come from the name: "Tal Shapiro" gives "TS".
<Avatar name="Tal Shapiro" />

// A photo covers the initials once it loads. If it fails, the initials stay.
<Avatar name="Ada K." src="/people/ada.jpg" status="online" />

// size is the diameter in pixels, 34 by default.
<Avatar initials="TS" name="Tal S." size={44} />

// tone="tint": one of five tints, picked from the name, the same everywhere.
// tint picks one yourself (and implies the tone).
<Avatar name="Ada Lovelace" tone="tint" />
<Avatar name="Ada Lovelace" tint={4} />

// No name: the dashed "Unassigned" placeholder.
<Avatar />
<Avatar name={task.assignee?.name} tone="tint" />

Props

PropTypeDefaultDescription
initialsstringthe first letters of the first two words of `name`, uppercasedOne or two letters shown in the circle.
namestring—The person's name: the tooltip, and read by screen readers with the status (`messages.presence`). Without one (or with an empty one) the avatar is the unassigned placeholder: a dashed circle with a person glyph, named `messages.unassigned`.
srcstring—A photo URL. It covers the initials once it loads; if it fails to load, the initials stay.
statusAvatarStatus—Adds a presence pixel in the matching colour.
sizenumber34The diameter in pixels.
tone'neutral' | 'accent' | 'tint''tint' when `tint` is given, else 'neutral'A surface, an accent-tinted background, or `tint`: one of five tints picked by the name, the same for that name everywhere (rendered as `data-tint`).
tint1 | 2 | 3 | 4 | 5—Picks the tint (1 to 5) instead of the name's own, for `tone="tint"`.
…restHTMLAttributes—className, style, the ref and other attributes reach the root <span>.

Accessibility

  • The initials and the pixel are hidden from assistive technology. Screen readers hear the name with the status instead, from messages.presence (“Ada K., online” in English), or the name alone when there is no status.
  • A src photo is an <img alt=""> over the initials: the name is already read from the hidden text, so the image adds nothing to hear. The initials show until it loads and stay if it fails, including a failure before hydration, which is caught on mount.
  • The presence pixel is colour only. Where presence matters, say it in text too, as the Presence piece does with its “online now” count.
  • An avatar is not interactive. To make one open a profile, wrap it in a link or button: its hidden name becomes the control’s name.
  • A tint is never the only cue: the name is still the tooltip and the hidden text, and the initials sit in --sl-text, at 4.5:1 or more on every tint in all four themes.
  • Without a name, the avatar reads messages.unassigned (“Unassigned”), as its tooltip and its hidden text, with the status after it when there is one. The person glyph is hidden.
  • Rename what screen readers hear with presence on a <Slant messages> root.

Tints

tone="tint" gives each name one of five tints, rendered as data-tint="1" to "5": a soft mix of --sl-chart-1 to --sl-chart-5 with the surface. The tint is picked by a fixed hash of the name (FNV-1a over the code points of the trimmed name, mod 5), so “Ada Lovelace” has the same tint in every list, on every page and after a reload, and server and client agree. Pass tint to pick one instead, for a person whose colour is set elsewhere; on its own it implies tone="tint", and another tone wins over it.

Unassigned

name is optional. Without one, or with only spaces, the avatar is the placeholder for nobody: a dashed hairline circle with no fill and a muted person glyph, marked data-unassigned, for the assignee of a task no one has picked up. initials and the tint don't apply; status still does.