Avatar stack

Slant / People & chat

Overlapping avatars; the count for the rest opens the full list.

Demo

Presence

Usage

@tuttn/slant
import { AvatarStack, type AvatarPerson } from '@tuttn/slant'

const people: AvatarPerson[] = [
  { id: 'ak', initials: 'AK', name: 'Ada K.', status: 'online' },
  { id: 'jr', initials: 'JR', name: 'Juno R.', status: 'away' },
  { id: 'ts', initials: 'TS', name: 'Tal S.' },
]

// Past max, the rest collapse into one "+N" bubble, a button that opens a
// list of everyone under the stack. It keeps its own open state.
<AvatarStack people={people} label="People on this project" max={4} />

// Control it with open and onOpenChange, or keep a static bubble.
<AvatarStack people={reviewers} label="Reviewers" max={3} open={open} onOpenChange={setOpen} />
<AvatarStack people={watchers} label="Watchers" max={3} expandable={false} />

// overflow draws the bubble's text; screen readers still hear "17 more".
<AvatarStack people={watchers} label="Watchers" max={3}
  overflow={hidden => hidden > 9 ? '9+' : `+${hidden}`} />

Props

PropTypeDefaultDescription
peopleAvatarPerson[]—The people, in order: `{ id, name, initials?, src?, status? }`. Every third one takes the accent tone.
labelstring—The list's accessible name.
maxnumber—How many avatars to show before the rest collapse into a "+N" bubble. All of them when omitted.
overflow(hidden: number) => ReactNodehidden => `+${hidden}`Replaces the bubble's visible "+N". Screen readers hear `messages.more(hidden)` either way.
expandablebooleantrueMakes the "+N" bubble a button that opens a list of everyone (avatar, name and status) right after the stack. False keeps a static bubble.
openboolean—Whether the full list is open. Pass it to control the stack; leave it out and it keeps its own state.
defaultOpenbooleanfalseWhether the full list starts open when uncontrolled.
onOpenChange(open: boolean) => void—Called with the requested state when the bubble is pressed.
…restHTMLAttributes—className, style, the ref and other attributes reach the root <ul>.

Keyboard

Tabreaches the "+N" bubble, the only stop in a stack
Enter / Spaceopens or closes the list of everyone

Accessibility

  • The stack is a list (<ul>) named by label, with one item per avatar and one for the bubble.
  • Each avatar reads as Avatar does: the name with the status, from messages.presence (“Ada K., online”).
  • The bubble reads messages.more (“2 more”) whatever overflow draws, so a short visible label such as “9+” loses nothing.
  • When people are hidden, the bubble is a button (expandable, on by default) with aria-expanded and aria-controls. Enter or Space opens an inline list of everyone right after the stack, each row read once through messages.presence; focus stays on the bubble. The list is the stack’s next sibling, so put the stack in a column for it to open underneath.
  • Presence is colour only. Where it matters, say it in text too, such as a count of who is online.
Full source