# Avatar

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

Tier: free. Type: component. Category: data. Tags: approval.

## Install

```bash
npx shadcn@latest add https://pixilui.com/r/avatar.json
```

One-time setup: add the registry to `components.json`:

```json
{
  "registries": {
    "@pixilui": "https://pixilui.com/r/{name}.json"
  }
}
```

Files: `components/pixilui/avatar.tsx`.

## Preview

Live preview: https://pixilui.com/components/avatar

Knobs:

- `face`: `initials`, `figure`, `empty`
- `tone`: `by name`, `grey`, `blue`, `violet`, `fuchsia`, `pink`, `red`, `yellow`
- `status`: `none`, `on`, `away`, `busy`, `off`

Signature: Each person's coin is glass in their own color, picked from their name, so Lena is the same pink in a comment, a stack and the account button.

Styles: `lively` (default), `quiet`, set with `data-style` on <html>. The item reads the style through the foundation; it needs no prop. Standalone files: lively https://pixilui.com/r/standalone/avatar.tsx, quiet https://pixilui.com/r/standalone/avatar.quiet.tsx.

## Code

`components/pixilui/avatar.tsx`

```tsx
/*
  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>
  );
});
```

## Usage

When to use:

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

When not to use:

- An agent: agents keep their own shaped faces, so a person never reads as an agent.
- Several people in a row: use Avatar group.

```tsx
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>
  );
}
```

## Examples

None.

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `name` | `string` | `""` | 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). |
| `src` | `string` |  | 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). |
| `size` | `number` | `32` | 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. |
| `figure` | `boolean` | `false` | Draw a person in relief instead of initials. |
| `empty` | `boolean` | `false` | An empty seat: someone to invite. |
| `emptyLabel` | `string` | `"Invite someone"` | The empty seat's accessible name. |
| `status` | `"on" \| "away" \| "busy" \| "off"` |  | A presence dot at the lower right. |
| `statusLabel` | `string` |  | The dot's accessible name, such as "Online". |

Also accepts every native `<span>` attribute, and a ref to it.

## Keyboard

- `Tab`: Never takes focus on its own: focus the row or button it sits in.

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

## Responsive

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

## Performance

- One span (two with a status); the glass is CSS in the foundation (section 8).

## Notes for AI

- <Avatar name="Lena Ortiz" /> picks Lena's color from her name (djb2 over blue, violet, fuchsia, pink, red, yellow); keep the hash so people keep their color.
- tone overrides it; grey is for no one in particular (a guest, +N). figure draws a person in relief; empty is a seat to invite (emptyLabel names it).
- status: on, away, busy or off, with statusLabel for words other than Online, Away, Busy, Offline.
- In the Account button: <Button><Avatar name={name} size={24} aria-hidden /><span>{name}</span></Button>.
- avatarTone(name) and avatarInitials(name, size) are exported for places that draw the coin without React.

## Related

- [Avatar group](https://pixilui.com/components/avatar-group/markdown): Who is here, as overlapping glass coins each cut out of the one behind it by a ring of the surface, ending in a grey +N.
- [Chip](https://pixilui.com/components/chip/markdown): The small labelled pill of an AI product: context in a composer, a filter, a fact behind a decision and the citation after a claim, in one shape and one light grey.
- [Button](https://pixilui.com/components/button/markdown): The PixilUI button in four main styles and four more, five sizes from 24 to 44, with icons, loading, disabled, a count and a shortcut.
