# Avatar

A circle that stands for a person, team or workspace: their photo, or one or two initials on a hue pair when there is no photo or it fails to load. Soft by default; solid inside a Token or on a photo or gradient.

**Dependencies:** npm: 2 direct, 3 total; registry: 2 direct, 2 total.

Native: iOS stable, Android stable.

## Usage

### When to use

- Show who a piece of content belongs to: a comment author, an assignee, the members of a team.
- Stand in for a missing or slow photo with one or two initials on a soft hue.

### When not to use

- A logo, a product image or a picture in content. Use a native img.
- A person picked as a value, such as a recipient in a field. Use Token with a leading avatar.
- A status such as online or busy. Use Badge next to the name.
- The only place a name appears in a list or comment. Write the name next to the avatar.

### Native

**Default**

```text
WerewolfAvatar(alt: "Ada Lovelace", color: .blue) {
    Text("AL")
}
```

```text
WerewolfAvatar(alt = "Ada Lovelace", color = WerewolfAvatarColor.Blue) {
    Text("AL")
}
```

**With photo**

```text
WerewolfAvatar(alt: "Ada Lovelace", image: AnyView(Image("ada").resizable().scaledToFill())) {
    Text("AL")
}
```

```text
WerewolfAvatar(alt = "Ada Lovelace", image = { Image(painterResource(R.drawable.ada), contentDescription = null, contentScale = ContentScale.Crop) }) {
    Text("AL")
}
```

**Next to a name**

```text
WerewolfAvatar(alt: "", color: .green) {
    Text("MK")
}
```

```text
WerewolfAvatar(alt = "", color = WerewolfAvatarColor.Green) {
    Text("MK")
}
```

**Square**

```text
WerewolfAvatar(alt: "Acme", color: .purple, shape: .square, variant: .solid) {
    Text("AC")
}
```

```text
WerewolfAvatar(alt = "Acme", color = WerewolfAvatarColor.Purple, shape = WerewolfAvatarShape.Square, variant = WerewolfAvatarVariant.Solid) {
    Text("AC")
}
```

## Variants

### variant

soft: background-<hue> with text-<hue>, for lists and comment headers. solid: the <hue> solid with text-on-<hue>, inside a Token or on a photo or gradient where the soft fill would not stand out.

Values: `soft`, `solid`. Default: `soft`.

### color

The hue pair behind the initials. Never the accent. To keep one person's color across a page, pick the hue from something stable, such as their user id.

Values: `gray`, `red`, `orange`, `yellow`, `green`, `teal`, `blue`, `purple`, `pink`. Default: `gray`.

### size

xs 20px for dense lists, sm 24px for table rows, md 32px for comment headers and member lists, lg 40px, xl 48px for profile headers.

Values: `xs`, `sm`, `md`, `lg`, `xl`. Default: `md`.

### shape

circle for people; square (radius-element) for workspaces and orgs.

Values: `circle`, `square`. Default: `circle`.

### status

A dot on the bottom end edge: success, warning, danger or gray solid on a background-surface rim; away also takes a border-warning edge, since the warning fill is under 3:1 on light surfaces. Also added to the avatar's name ("Ada Lovelace, Online").

Values: `online`, `away`, `busy`, `offline`. Default: `undefined`.

## Examples

### Default

```tsx
<Avatar alt="Ada Lovelace">AL</Avatar>
```

### With photo

The initials show until the photo loads, and stay if it fails.

```tsx
<Avatar src="/avatars/ada.jpg" alt="Ada Lovelace" color="blue">AL</Avatar>
```

### Next to a name

The name is written next to the avatar, so alt is empty and the avatar is hidden from screen readers.

```tsx
<div className="flex items-center gap-sm">
  <Avatar alt="" color="teal" size="sm">GH</Avatar>
  <span className="text-sm text-text-primary">Grace Hopper</span>
</div>
```

### Without a name

An icon on the soft variant, for a guest or a deleted account.

```tsx
<Avatar alt="Guest">
  <IconUser size={16} />
</Avatar>
```

### From a name

Initials and a stable hue from the name.

```tsx
<Avatar name="Ada Lovelace" alt="Ada Lovelace" />
```

### Status

```tsx
<Avatar alt="Ada Lovelace" color="blue" status="online">AL</Avatar>
```

### Square

```tsx
<Avatar alt="Werewolf" shape="square" color="purple">W</Avatar>
```

### Extra sizes

```tsx
<div className="flex items-center gap-sm">
  <Avatar alt="Ada Lovelace" size="xs">AL</Avatar>
  <Avatar alt="Ada Lovelace" size="xl">AL</Avatar>
</div>
```

## Do and don't

### Do

- Pass the person's name as alt, or alt="" when the name is written next to the avatar.
- Always pass initials as children, even with src, so a slow or broken photo still shows something.
- Use solid inside a Token or on a photo or gradient; use soft everywhere else.
- Keep one hue per person across the page.

### Don't

- Don't put an icon on a solid avatar; there is no icon token for a hue solid.
- Don't use the accent or a status color as the fill.
- Don't let the avatar replace a visible name in lists, comments or assignee fields.
- Don't use it for logos or product images; use a native img.

## Props

| Name | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `variant` | `enum` | `soft`, `solid` | `soft` | soft: background-<hue> with text-<hue>, for lists and comment headers. solid: the <hue> solid with text-on-<hue>, inside a Token or on a photo or gradient where the soft fill would not stand out. |
| `color` | `enum` | `gray`, `red`, `orange`, `yellow`, `green`, `teal`, `blue`, `purple`, `pink` | `gray` | The hue pair behind the initials. Never the accent. To keep one person's color across a page, pick the hue from something stable, such as their user id. |
| `size` | `enum` | `xs`, `sm`, `md`, `lg`, `xl` | `md` | xs 20px for dense lists, sm 24px for table rows, md 32px for comment headers and member lists, lg 40px, xl 48px for profile headers. |
| `src` | `string` |  |  | Photo URL. The initials show until it loads, and stay if it fails. Not generated for iOS and Android. |
| `alt` (required) | `string` |  |  | Name of the person, team or workspace: Ada Lovelace. Pass an empty string when the name is written next to the avatar. |
| `shape` | `enum` | `circle`, `square` | `circle` | circle for people; square (radius-element) for workspaces and orgs. |
| `name` | `string` |  |  | Derives the initials (one or two letters) when there are no children and, without color, a stable hue from the name. Not generated for iOS and Android. |
| `delayMs` | `number` |  |  | With src, milliseconds to wait before the fallback shows, so a fast photo never flashes the initials. Not generated for iOS and Android. |
| `status` | `enum` | `online`, `away`, `busy`, `offline` |  | A dot on the bottom end edge: success, warning, danger or gray solid on a background-surface rim; away also takes a border-warning edge, since the warning fill is under 3:1 on light surfaces. Also added to the avatar's name ("Ada Lovelace, Online"). Not generated for iOS and Android. |
| `statusLabel` | `string` |  |  | The status word read out; defaults to Online, Away, Busy or Offline. Translate it here. Not generated for iOS and Android. |
| `image` | `node` |  |  | Native only: the photo, a view you supply. It is drawn over the fallback and clipped to the shape. Remote loading is up to the app. Only on iOS, Android. |
| `icon` | `node` |  |  | Native only: an icon shown in the circle on the soft variant, sized by the avatar. Pass it instead of the initials for someone without a name. Only on iOS, Android. |

### Slots

| Name | Required | Description |
| --- | --- | --- |
| `fallback` | yes | One or two initials, such as AL. On the soft variant, an icon for someone without a name. |

## Anatomy

| Part | Description |
| --- | --- |
| `root` | The circle: radius-full, the hue fill, and the name of the avatar. 24, 32 or 40px. |
| `image` | The photo, cropped to fill the circle. Rendered only once it has loaded, then it fades in with duration-medium (no fade under reduced motion). |
| `fallback` | The children: one or two initials in medium weight, shown until the photo loads and when it fails or is missing. |
| `icon` | Optional Tabler icon as the fallback on the soft variant, 12px in sm, 16px in md, 20px in lg. |
| `status` | Optional presence dot, data-slot avatar-status, aria-hidden, on a background-surface rim; the status is in the root's name, or in visually hidden text when alt is "". |

## Accessibility

Role: `img`

- alt names the avatar on the root (role img with aria-label), so the name is the same whether the photo loads or the initials show, and the initials are never read out letter by letter. The type requires alt.
- When the name is written next to the avatar, as in a comment header or a member list, pass alt="": the avatar is hidden from screen readers, so the name is not read twice.
- Avatar is not interactive. To open a profile, put the avatar and the name inside one link, and name the link.
- The hue is decoration. Never let it carry meaning such as a role or a status on its own.
- Solid initials use text-on-<hue>, which meets 4.5:1 on every hue in both modes. There is no icon token for a hue solid, so an icon fallback is for the soft variant only.
- The status dot is never the only signal: its word is part of the avatar's aria-label. With alt="" the avatar is not hidden; the word is read from visually hidden text.

## Install

```bash
npx werewolf-ui add avatar
```

Run `npx werewolf-ui init` once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See [Installation](https://www.werewolf-ui.dev/docs/installation.md) for requirements and the alternative registry setup.

```bash
npx werewolf-ui add avatar --platform ios
npx werewolf-ui add avatar --platform android
```

For iOS and Android, run `npx werewolf-ui init --platform ios` or `npx werewolf-ui init --platform android` once, then add with `--platform`. See [Installation](https://www.werewolf-ui.dev/docs/installation.md).
