# Avatar

> A profile picture with initials or an icon as its fallback.

```tsx title="hero.tsx"
import {
  Avatar,
  AvatarFallback,
  AvatarGroup,
  AvatarGroupCount,
  AvatarImage,
} from '@langgenius/dify-ui/avatar'

const portrait = `data:image/svg+xml,${encodeURIComponent(
  '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 80 80"><rect width="80" height="80" fill="#fde4cf"/><path d="M6 80c3-14 17-22 34-22s31 8 34 22z" fill="#3b5bdb"/><rect x="33" y="46" width="14" height="14" rx="5" fill="#d9976b"/><ellipse cx="40" cy="36" rx="15" ry="17" fill="#f1bf94"/><path d="M24 37c-3-16 6-26 16-26 11 0 19 10 16 25-3-8-9-12-16-12s-13 5-16 13z" fill="#2b2118"/></svg>',
)}`

export function AvatarHeroDemo() {
  return (
    <div className="flex flex-wrap items-center gap-8">
      <Avatar size="lg" role="img" aria-label="Evan Chen">
        <AvatarFallback>EC</AvatarFallback>
        <AvatarImage src={portrait} alt="" />
      </Avatar>
      <Avatar size="lg" role="img" aria-label="Sofia Rossi">
        <AvatarFallback className="bg-components-icon-bg-orange-dark-solid">SR</AvatarFallback>
      </Avatar>
      <AvatarGroup size="lg" role="group" aria-label="Workspace members">
        <Avatar role="img" aria-label="Riku Tanaka">
          <AvatarFallback className="bg-components-icon-bg-teal-solid">RT</AvatarFallback>
        </Avatar>
        <Avatar role="img" aria-label="Mia Johnson">
          <AvatarFallback className="bg-components-icon-bg-violet-solid">MJ</AvatarFallback>
        </Avatar>
        <AvatarGroupCount role="img" aria-label="3 more members">
          +3
        </AvatarGroupCount>
      </AvatarGroup>
    </div>
  )
}
```

## Usage guidelines

- **Name a standalone avatar**: Give `Avatar` `role="img"` and the person's full name through `aria-label`, and give `AvatarImage` an empty `alt`. The name stays stable while the image loads or after it fails.
- **Hide an avatar next to its name**: When adjacent text or the surrounding control already identifies the person, set `aria-hidden="true"` on `Avatar` so the name is not announced twice.
- **Supply the fallback content**: `Avatar` does not derive initials from a name. Pass initials or an icon to `AvatarFallback`, and put a custom background color on `AvatarFallback`, not on the root, so transparent images keep their transparency.
- **Set the size on the root**: `size` on `Avatar` sizes the image and the fallback text together, from `xxs` (16px) to `3xl` (64px). The default is `md` (32px).
- **Compute overflow yourself**: `AvatarGroup` only overlaps its children and shares a size. The caller picks which avatars to show, renders the `+N` count in `AvatarGroupCount`, and names that count.

## Anatomy

```tsx
import {
  Avatar,
  AvatarFallback,
  AvatarGroup,
  AvatarGroupCount,
  AvatarImage,
} from '@langgenius/dify-ui/avatar'
```

```tsx
<AvatarGroup>
  <Avatar>
    <AvatarFallback />
    <AvatarImage />
  </Avatar>
  <AvatarGroupCount />
</AvatarGroup>
```

Place `AvatarFallback` before `AvatarImage`. `AvatarGroup` and `AvatarGroupCount` are Dify additions and render plain `<span>` elements.

## Examples

### Sizes

Eight sizes cover dense table rows to profile headers. The fallback text scales with the avatar.

```tsx title="sizes.tsx"
import { Avatar, AvatarFallback } from '@langgenius/dify-ui/avatar'

const sizes = ['xxs', 'xs', 'sm', 'md', 'lg', 'xl', '2xl', '3xl'] as const

export function AvatarSizesDemo() {
  return (
    <div className="flex flex-wrap items-end justify-center gap-x-3 gap-y-5">
      {sizes.map((size) => (
        <div key={size} className="flex flex-col items-center gap-2">
          <Avatar size={size} role="img" aria-label={`Evan Chen (${size})`}>
            <AvatarFallback>EC</AvatarFallback>
          </Avatar>
          <span className="font-mono system-xs-regular text-text-tertiary">{size}</span>
        </div>
      ))}
    </div>
  )
}
```

### Image fallback

The fallback shows while the image loads and stays when it fails. Use an icon when there are no initials to show.

```tsx title="image-fallback.tsx"
import { Avatar, AvatarFallback, AvatarImage } from '@langgenius/dify-ui/avatar'

export function AvatarImageFallbackDemo() {
  return (
    <div className="flex flex-wrap items-start justify-center gap-8">
      <div className="flex flex-col items-center gap-2">
        <Avatar size="xl" role="img" aria-label="Noah Kim">
          <AvatarFallback className="bg-components-icon-bg-orange-dark-solid">NK</AvatarFallback>
          <AvatarImage src="data:image/png;base64,broken" alt="" />
        </Avatar>
        <span className="system-xs-regular text-text-tertiary">Broken image</span>
      </div>
      <div className="flex flex-col items-center gap-2">
        <Avatar size="xl" role="img" aria-label="Deleted member">
          <AvatarFallback className="bg-components-icon-bg-midnight-solid">
            <span aria-hidden="true" className="i-ri-user-line size-5" />
          </AvatarFallback>
        </Avatar>
        <span className="system-xs-regular text-text-tertiary">Icon fallback</span>
      </div>
    </div>
  )
}
```

### Group

`AvatarGroup` overlaps its children from the inline start and passes its `size` to them; an explicit `size` on a child wins. Put `AvatarGroupCount` last so it sits behind the avatars. When a visible label describes the group, give it `role="group"` and point `aria-labelledby` at the label.

```tsx title="group.tsx"
import { Avatar, AvatarFallback, AvatarGroup, AvatarGroupCount } from '@langgenius/dify-ui/avatar'
import * as React from 'react'

const editors = [
  { name: 'Evan Chen', initials: 'EC', color: 'bg-components-icon-bg-blue-solid' },
  { name: 'Sofia Rossi', initials: 'SR', color: 'bg-components-icon-bg-orange-dark-solid' },
  { name: 'Riku Tanaka', initials: 'RT', color: 'bg-components-icon-bg-teal-solid' },
  { name: 'Mia Johnson', initials: 'MJ', color: 'bg-components-icon-bg-violet-solid' },
  { name: 'Noah Kim', initials: 'NK', color: 'bg-components-icon-bg-pink-solid' },
]

const visible = editors.slice(0, 3)
const hidden = editors.length - visible.length

export function AvatarGroupDemo() {
  const labelId = React.useId()

  return (
    <div className="flex items-center gap-3">
      <span id={labelId} className="system-xs-medium text-text-tertiary">
        Editors
      </span>
      <AvatarGroup role="group" aria-labelledby={labelId} size="sm">
        {visible.map((editor) => (
          <Avatar key={editor.name} role="img" aria-label={editor.name}>
            <AvatarFallback className={editor.color}>{editor.initials}</AvatarFallback>
          </Avatar>
        ))}
        <AvatarGroupCount role="img" aria-label={`${hidden} more editors`}>
          +{hidden}
        </AvatarGroupCount>
      </AvatarGroup>
    </div>
  )
}
```

### Beside a name

In a member list the visible name identifies the person, so the avatar is hidden from assistive technology.

```tsx title="decorative.tsx"
import { Avatar, AvatarFallback } from '@langgenius/dify-ui/avatar'

const members = [
  {
    name: 'Evan Chen',
    email: 'evan@example.com',
    initials: 'EC',
    role: 'Owner',
    color: 'bg-components-icon-bg-blue-solid',
  },
  {
    name: 'Sofia Rossi',
    email: 'sofia@example.com',
    initials: 'SR',
    role: 'Editor',
    color: 'bg-components-icon-bg-orange-dark-solid',
  },
]

export function AvatarDecorativeDemo() {
  return (
    <ul className="flex w-full max-w-80 flex-col gap-3">
      {members.map((member) => (
        <li key={member.email} className="flex items-center gap-3">
          <Avatar size="lg" aria-hidden="true">
            <AvatarFallback className={member.color}>{member.initials}</AvatarFallback>
          </Avatar>
          <div className="flex min-w-0 flex-1 flex-col">
            <span className="truncate system-sm-medium text-text-primary">{member.name}</span>
            <span className="truncate system-xs-regular text-text-tertiary">{member.email}</span>
          </div>
          <span className="shrink-0 system-xs-regular text-text-tertiary">{member.role}</span>
        </li>
      ))}
    </ul>
  )
}
```

## API reference

### Avatar

Displays a user's profile picture, initials, or fallback icon. Renders a `<span>` element.

Set `role="img"` and `aria-label` with an empty image `alt` for standalone identities, or `aria-hidden="true"` when adjacent text already names the person.

**Props**

| Prop    | Type                                                              | Default | Description                                                                                |
| ------- | ----------------------------------------------------------------- | ------- | ------------------------------------------------------------------------------------------ |
| `size?` | `'2xl' \| '3xl' \| 'lg' \| 'md' \| 'sm' \| 'xl' \| 'xs' \| 'xxs'` | `'md'`  | The size of the avatar. Inherits the size set on a surrounding `AvatarGroup` when omitted. |

Inherits `className`, `render`, `style` from [Base UI Avatar.Root](https://base-ui.com/react/components/avatar#root).

**Data attributes**

| Attribute                 | Description               |
| ------------------------- | ------------------------- |
| `data-imageloadingstatus` | The image loading status. |

**CSS variables**

| Variable             | Type     | Description                                                                             |
| -------------------- | -------- | --------------------------------------------------------------------------------------- |
| `--avatar-size`      | `length` | The avatar diameter. The `size` prop sets it; override it for a size outside the scale. |
| `--avatar-font-size` | `length` | The font size of the fallback initials, set together with `--avatar-size`.              |

### AvatarFallback

Rendered when the image fails to load or when no image is provided. Renders a `<span>` element.

Inherits `delay`, `className`, `render`, `style` from [Base UI Avatar.Fallback](https://base-ui.com/react/components/avatar#fallback).

**Data attributes**

| Attribute                 | Description               |
| ------------------------- | ------------------------- |
| `data-imageloadingstatus` | The image loading status. |

### AvatarGroup

Overlaps a row of avatars and sets their default `size`; an explicit `size` on a child wins. Renders a `<span>` element.

**Props**

| Prop    | Type                                                              | Default | Description                                   |
| ------- | ----------------------------------------------------------------- | ------- | --------------------------------------------- |
| `size?` | `'2xl' \| '3xl' \| 'lg' \| 'md' \| 'sm' \| 'xl' \| 'xs' \| 'xxs'` | `'md'`  | The default size of the avatars in the group. |

### AvatarGroupCount

Displays caller-provided overflow text such as `+2` alongside an `AvatarGroup`. Renders a `<span>` element.

**Props**

| Prop    | Type                                                              | Default | Description                                                                                     |
| ------- | ----------------------------------------------------------------- | ------- | ----------------------------------------------------------------------------------------------- |
| `size?` | `'2xl' \| '3xl' \| 'lg' \| 'md' \| 'sm' \| 'xl' \| 'xs' \| 'xxs'` | `'md'`  | The size of the count badge. Inherits the size set on a surrounding `AvatarGroup` when omitted. |

### AvatarImage

The image to be displayed in the avatar. Renders an `<img>` element.

Inherits `onLoadingStatusChange`, `keepMounted`, `className`, `render`, `style` from [Base UI Avatar.Image](https://base-ui.com/react/components/avatar#image).

**Data attributes**

| Attribute             | Description                                 |
| --------------------- | ------------------------------------------- |
| `data-loading`        | Present while the image is loading.         |
| `data-error`          | Present when the image failed to load.      |
| `data-starting-style` | Present when the image begins animating in. |
| `data-ending-style`   | Present when the image is animating out.    |
