Avatar
FreeA person as a coin of colored glass in their own color: initials, a figure, a photo or an empty seat, with a presence dot, its color picked from the name so a person looks the same everywhere.
npx shadcn@latest add https://pixilui.com/r/avatar.jsonPreview
import { IconDisclosure } from "@/components/pixilui/icons/disclosure";
import { Button } from "@/components/pixilui/button";
import { Avatar, type AvatarStatus, type AvatarTone } from "@/components/pixilui/avatar";
// The first option of each knob is the demo's default; the docs preview starts there. The tones are the lock's seven,
// in its order; "by name" lets each name pick its own.
export const knobs = {
face: ["initials", "figure", "empty"],
tone: ["by name", "grey", "blue", "violet", "fuchsia", "pink", "red", "yellow"],
status: ["none", "on", "away", "busy", "off"],
} as const;
const people = ["Lena Ortiz", "Dev Patel", "Hana Ito", "Ben Ito"];
/**
* The lock's three treatments, one row each: the glass coin at 48, a small coin at 24 (it shows less of the glass), and
* quiet's pastel. Four people in their own colors, or all in one tone, and the Account button carrying the coin.
*/
export default function AvatarDemo({ face = "initials", tone = "by name", status = "none" }: KnobValues<typeof knobs>) {
const dot = status === "none" ? undefined : (status as AvatarStatus);
const color = tone === "by name" ? undefined : (tone as AvatarTone);
const row = (size: number) => (
<div className="flex items-center gap-3">
{people.map((name) =>
face === "figure" ? (
<Avatar key={name} name={name} tone={color} figure size={size} status={dot} />
) : (
<Avatar key={name} name={name} tone={color} empty={face === "empty"} size={size} status={dot} />
),
)}
</div>
);
return (
<div className="flex w-full max-w-md flex-col items-start gap-5">
{row(48)}
{row(24)}
<div data-style="quiet">{row(32)}</div>
<Button iconEnd={<IconDisclosure />}>
<Avatar name="Maya Rao" tone={color} size={24} aria-hidden />
<span>Maya Rao</span>
</Button>
</div>
);
}When to use
Use it when
- A person beside their name: the account button, comments, approval rows, the requester on an approval.
- Presence on a person (status).
- An empty seat to invite someone.
Use something else when
- An agent: agents keep their own shaped faces, so a person never reads as an agent.
- Several people in a row: use Avatar group.
Installation
npx shadcn@latest add https://pixilui.com/r/avatar.jsonAdds avatar.tsx, and installs clsx and tailwind-merge. The URL works in any app with a components.json, with no registry to add first.
/*
The person avatar, the glass coin, locked 1 Dec 2026 (design/explorations/components/locked/user-avatar).
Its look is foundation.css section 8 (.pixilui-user-avatar); this component writes the locked markup, as
the locked UserAvatar.make does:
Face a photo when src is given; else initials from the name (one letter at 20 and below); else the
figure, a person in relief. An empty seat is a dashed edge with a plus.
Color the name picks the tone (djb2 over the six person colors), so a person keeps one color
everywhere; grey is for no one in particular.
Glass shows less on small coins; quiet makes it a flat pastel with letters in its ink (the full
color in dark). Inside a Button it takes the button's height less 4px on every side.
Motion none of its own: it holds still.
*/
import { forwardRef, type ComponentPropsWithoutRef, type CSSProperties } from "react";
import { cn } from "./lib/utils";
export type AvatarTone = "grey" | "blue" | "violet" | "fuchsia" | "pink" | "red" | "yellow";
export type AvatarStatus = "on" | "away" | "busy" | "off";
/** The tones a name can pick, in the locked order (the hash depends on it). */
export const AvatarTones = ["blue", "violet", "fuchsia", "pink", "red", "yellow"] as const satisfies readonly AvatarTone[];
/** The sizes the coin draws at; any other size takes the coin's 32px look scaled to that box. */
const SIZES = [16, 20, 24, 40, 48];
/** The tone a name picks: djb2 over the six person colors (the locked UserAvatar.toneOf), so a person keeps one color. */
export function avatarTone(name: string): AvatarTone {
let h = 5381;
for (const c of name) h = ((h << 5) + h + c.charCodeAt(0)) >>> 0;
return AvatarTones[h % AvatarTones.length]!;
}
/** First and last initials, one letter at 20px and below (the locked UserAvatar.initials). */
export function avatarInitials(name: string, size?: number): string {
const w = name.trim().split(/\s+/);
const s = ((w[0]?.[0] ?? "") + (w.length > 1 ? (w[w.length - 1]?.[0] ?? "") : "")).toUpperCase();
return size && size <= 20 ? s.slice(0, 1) : s;
}
export type AvatarProps = Omit<ComponentPropsWithoutRef<"span">, "children"> & {
/** The person's name: it picks the color, gives the initials and is the coin's accessible name. Leave it out for no one in particular (the figure). */
name?: string;
/** A photo. It fills the coin; a lens and a hairline sit over it. */
src?: string;
/** The color. Without it the name picks one (grey when there is no name). */
tone?: AvatarTone;
/** The coin's size in px: 16, 20, 24, 32 (default), 40 or 48. At 20 and below it carries one letter. Inside a Button pass the button's height less 8. */
size?: number;
/** Draw a person in relief instead of initials. */
figure?: boolean;
/** An empty seat: someone to invite. */
empty?: boolean;
/** The empty seat's accessible name. */
emptyLabel?: string;
/** A presence dot at the lower right. */
status?: AvatarStatus;
/** The dot's accessible name, such as "Online". */
statusLabel?: string;
};
const STATUS_LABEL: Record<AvatarStatus, string> = { on: "Online", away: "Away", busy: "Busy", off: "Offline" };
/** A person's face: a coin of colored glass in their own color, with initials, a figure, a photo or an empty seat. */
export const Avatar = forwardRef<HTMLSpanElement, AvatarProps>(function Avatar(
{ name = "", src, tone, size = 32, figure = false, empty = false, emptyLabel = "Invite someone", status, statusLabel, className, style, ...props },
ref,
) {
const dataSize = SIZES.includes(size) ? size : undefined;
const wrapped = !empty && status !== undefined;
// given aria-hidden (the coin beside the name, or in a button), the coin is not named again
const hidden = props["aria-hidden"] === true || props["aria-hidden"] === "true";
if (empty) {
return (
<span ref={ref} className={cn("pixilui-user-avatar font-body", className)} style={style} data-size={dataSize} data-empty="" aria-label={emptyLabel} {...props}>
+
</span>
);
}
const face = src ? (
<img src={src} alt="" />
) : name && !figure ? (
avatarInitials(name, size)
) : null;
const toneOf = src ? undefined : tone ?? (name ? avatarTone(name) : "grey");
const label = hidden ? {} : name ? { role: "img", "aria-label": name } : { "aria-hidden": true };
const coin = (
<span
className={cn("pixilui-user-avatar font-body", wrapped ? undefined : className)}
data-size={dataSize}
data-tone={toneOf}
data-figure={!src && !(name && !figure) ? "" : undefined}
{...label}
{...(wrapped ? {} : { ref, style, ...props })}
>
{face}
</span>
);
if (!wrapped) return coin;
return (
<span ref={ref} className={cn("pixilui-user-avatar-wrap font-body", className)} style={{ "--pixilui-ua": `${size}px`, ...style } as CSSProperties} {...props}>
{coin}
<span className="pixilui-user-avatar-dot" data-status={status} aria-label={statusLabel ?? STATUS_LABEL[status]} />
</span>
);
});
Then npm i clsx tailwind-merge, and add the foundation once per app, as in Installation.
Usage
import { Avatar } from "@/components/pixilui/avatar";
<Avatar />Fixed sizes: 16, 20, 24, 32 (default), 40, 48; one letter at 20 and below. Inside a Button pass the button's height less 8.
API reference
namestring""The person's name: it picks the color, gives the initials and is the coin's accessible name. Leave it out for no one in particular (the figure).srcstring–A photo. It fills the coin; a lens and a hairline sit over it.tone"grey" | "blue" | "violet" | "fuchsia" | "pink" | "red" | "yellow"–The color. Without it the name picks one (grey when there is no name).sizenumber32The coin's size in px: 16, 20, 24, 32 (default), 40 or 48. At 20 and below it carries one letter. Inside a Button pass the button's height less 8.figurebooleanfalseDraw a person in relief instead of initials.emptybooleanfalseAn empty seat: someone to invite.emptyLabelstring"Invite someone"The empty seat's accessible name.status"on" | "away" | "busy" | "off"–A presence dot at the lower right.statusLabelstring–The dot's accessible name, such as "Online".Also takes every native <span> attribute, and a ref.
Keyboard
Accessibility
- With a name it is role="img" named by the name; beside the written name, pass aria-hidden so it is not read twice.
- Without a name (the figure) it is hidden from assistive tech.
- The presence dot is named ("Online", or statusLabel).
- The letters keep 5.2:1 or more on every color.
Motion
- None of its own: it holds still. A person arriving or leaving a stack uses the foundation's arrive and leave.