Avatar stack
Overlapping marks for the agents and people on a piece of work. Hover or tab in and they fan out with their names; +N lists the rest.
npx shadcn@latest add https://captain-ui.vercel.app/r/avatar-stack.jsonFix checkout race in the webhook handler
#482 · 3 files · +38 −12
Cache font files on the CDN for a year
#479 · 1 file · +9 −2
"use client";
// Captain UI · Avatar stack, MIT. Overlapping marks that fan out on hover or focus, with names on tooltips and the overflow listed behind +N.
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { useEffect, useId, useRef, useState, type KeyboardEvent } from "react";
import { EASE, SPRING_LAYOUT, cx } from "@/registry/captain/ui/captain";
import { Icon, type IconName } from "@/registry/captain/ui/icons";
export type StackItem = {
id: string;
name: string;
/** A second line on the tooltip, such as what the agent is doing. */
detail?: string;
/** An icon mark. Without one the avatar shows initials. */
mark?: IconName;
initials?: string;
tone?: "surface" | "ink" | "accent";
};
type AvatarStackProps = {
items: StackItem[];
/** How many avatars show before the +N. */
max?: number;
size?: "sm" | "md";
/** Accessible name for the group, such as "Agents on this pull request". */
label: string;
/** The colour behind the stack, used for the ring that separates each avatar. */
ring?: string;
onSelect?: (item: StackItem) => void;
className?: string;
};
const SIZES = { sm: { d: 26, overlap: 8, spread: 4, icon: 14, text: 10 }, md: { d: 34, overlap: 11, spread: 6, icon: 17, text: 12 } } as const;
const TONES = {
surface: "bg-[color-mix(in_srgb,var(--captain-ink)_7%,var(--captain-surface))] text-ink",
ink: "bg-primary text-primary-ink",
accent: "bg-accent text-white",
} as const;
const initialsOf = (name: string) =>
name
.split(/\s+/)
.map((w) => w[0])
.join("")
.slice(0, 2)
.toUpperCase();
// One shared clock so a second tooltip opens at once while the first is still showing (or just closed).
let lastTipAt = 0;
export function AvatarStack({ items, max = 4, size = "md", label, ring = "var(--captain-surface)", onSelect, className }: AvatarStackProps) {
const s = SIZES[size];
const reduce = useReducedMotion();
const shown = items.slice(0, items.length > max ? max - 1 : max);
const hidden = items.slice(shown.length);
const count = shown.length + (hidden.length ? 1 : 0);
const [hover, setHover] = useState(false);
const [focused, setFocused] = useState(false);
const [tip, setTip] = useState<number | null>(null);
const [active, setActive] = useState(0);
const wait = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
const refs = useRef<(HTMLButtonElement | null)[]>([]);
const root = useRef<HTMLDivElement>(null);
const id = useId();
const open = hover || focused;
useEffect(() => () => clearTimeout(wait.current), []);
const show = (i: number, now = false) => {
clearTimeout(wait.current);
const warm = performance.now() - lastTipAt < 400;
if (now || warm) setTip(i);
else wait.current = setTimeout(() => setTip(i), 380);
};
const hide = () => {
clearTimeout(wait.current);
if (tip !== null) lastTipAt = performance.now();
setTip(null);
};
const key = (e: KeyboardEvent) => {
const step = e.key === "ArrowRight" || e.key === "ArrowDown" ? 1 : e.key === "ArrowLeft" || e.key === "ArrowUp" ? -1 : 0;
let next = active;
if (step) next = (active + step + count) % count;
else if (e.key === "Home") next = 0;
else if (e.key === "End") next = count - 1;
else if (e.key === "Escape") return hide();
else return;
e.preventDefault();
setActive(next);
refs.current[next]?.focus();
};
const avatar = (i: number, body: React.ReactNode, tone: string, name: string, tipBody: React.ReactNode, onClick?: () => void) => (
<motion.span
key={i}
className="relative inline-flex"
style={{ zIndex: tip === i ? count + 1 : i + 1 }}
initial={false}
animate={{ marginLeft: i === 0 ? 0 : open ? s.spread : -s.overlap }}
transition={reduce ? { duration: 0 } : SPRING_LAYOUT}
>
<motion.button
ref={(el) => {
refs.current[i] = el;
}}
type="button"
tabIndex={i === active ? 0 : -1}
aria-label={name}
aria-describedby={tip === i ? `${id}-tip` : undefined}
onClick={onClick}
onPointerEnter={() => show(i)}
onPointerLeave={hide}
onFocus={() => {
setActive(i);
show(i, true);
}}
onBlur={hide}
animate={{ y: tip === i && !reduce ? -2 : 0 }}
transition={{ duration: 0.18, ease: EASE }}
className={cx(
"group pressable relative grid shrink-0 place-items-center rounded-full font-semibold tracking-[0.01em] before:absolute before:-inset-[5px] before:rounded-full before:content-['']",
tone,
)}
style={{ width: s.d, height: s.d, fontSize: s.text, boxShadow: `0 0 0 2px ${ring}` }}
>
{body}
<span aria-hidden="true" className="pointer-events-none absolute inset-0 rounded-full transition-colors duration-150 group-focus-visible:bg-accent-tint" />
</motion.button>
<AnimatePresence>
{tip === i ? (
<motion.span
id={`${id}-tip`}
role="tooltip"
className="glass pointer-events-none absolute bottom-full left-1/2 mb-2.5 w-max max-w-[280px] rounded-inner px-2.5 py-1.5 text-left text-[13px] leading-[1.35] text-ink"
style={{ x: "-50%", originY: 1 }}
initial={{ opacity: 0, scale: reduce ? 1 : 0.96, y: reduce ? 0 : 2 }}
animate={{ opacity: 1, scale: 1, y: 0 }}
exit={{ opacity: 0, scale: reduce ? 1 : 0.96, transition: { duration: 0.1 } }}
transition={{ duration: 0.16, ease: EASE }}
>
{tipBody}
</motion.span>
) : null}
</AnimatePresence>
</motion.span>
);
return (
<div
ref={root}
role="group"
aria-label={label}
onKeyDown={key}
onPointerEnter={() => setHover(true)}
onPointerLeave={() => setHover(false)}
onFocus={() => setFocused(true)}
onBlur={(e) => {
if (!root.current?.contains(e.relatedTarget as Node)) setFocused(false);
}}
className={cx("inline-flex items-center", className)}
>
{shown.map((it, i) =>
avatar(
i,
it.mark ? <Icon name={it.mark} size={s.icon} /> : <span aria-hidden="true">{it.initials ?? initialsOf(it.name)}</span>,
TONES[it.tone ?? "surface"],
it.name,
<>
<span className="block font-medium">{it.name}</span>
{it.detail ? <span className="block text-ink-2">{it.detail}</span> : null}
</>,
onSelect ? () => onSelect(it) : undefined,
),
)}
{hidden.length
? avatar(
shown.length,
<span aria-hidden="true" className="tnum">
+{hidden.length}
</span>,
"bg-[color-mix(in_srgb,var(--captain-ink)_7%,var(--captain-surface))] text-ink",
`${hidden.length} more: ${hidden.map((h) => h.name).join(", ")}`,
<ul className="grid grid-cols-[auto_1fr] gap-x-3 gap-y-0.5">
{hidden.map((h) => (
<li key={h.id} className="col-span-2 grid grid-cols-subgrid">
<span className="font-medium">{h.name}</span>
{h.detail ? <span className="text-ink-2">{h.detail}</span> : null}
</li>
))}
</ul>,
)
: null}
</div>
);
}Usage
import { AvatarStack } from "@/components/ui/avatar-stack"
<AvatarStack label="Agents on #482" items={agents} max={4} />Props
| Prop | Type | Default | What it does |
|---|---|---|---|
| items | StackItem[] | { id, name, detail?, mark?, initials?, tone? }. A mark is any Captain icon; without one, initials. | |
| max | number | 4 | Avatars shown, counting the +N. |
| label | string | Names the group for screen readers. | |
| size | "sm" | "md" | "md" | 26 or 34px. The hit area is always 44px. |
| ring | string | "var(--captain-surface)" | The colour behind the stack, so the ring between avatars matches it. |
| onSelect | (item: StackItem) => void | Called when an avatar is pressed. |
Keyboard
- Tab
- Enter the stack
- ← →
- Move between avatars
- Home / End
- First or last
- Esc
- Hide the tooltip
Accessibility
- role=group with a label; one tab stop, arrows move between avatars.
- Each avatar is a button named for its agent; the tooltip is wired with aria-describedby.
- The +N button's name lists every hidden name, so nothing is hover-only.