# Avatar

Represents a person or an organization with an image, their initials or an icon. Use it in lists, comments, menus and headers next to the name it stands for.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-avatar`
- Source: [libs/ui/src/lib/atoms/avatar/avatar.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/atoms/avatar/avatar.ts)

```typescript title="Import"
import { Avatar } from '@vigdx/ui/avatar';
```

## Image, initials and icon

Pass `src` with an `alt`; without an image the avatar shows the initials of `name` (first and last word), and with neither it shows the `user` icon.

```typescript title="avatar-basic.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Avatar } from '@vigdx/ui/avatar';

import { PORTRAIT_ADA } from './portraits';

@Component({
  selector: 'app-avatar-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Avatar],
  template: `
    <div class="flex flex-wrap items-center gap-4">
      <vig-avatar [src]="portrait" alt="Ada Lovelace" />
      <vig-avatar name="Grace Brewster Hopper" variant="primary" />
      <vig-avatar name="Alan Turing" variant="success" />
      <vig-avatar />
    </div>
  `,
})
export class AvatarBasicDemo {
  protected readonly portrait = PORTRAIT_ADA;
}
```

```typescript title="portraits.ts"
// Inline SVG portraits so the demos render offline. In an app, `src` is
// usually a URL from your user store or CDN.
function portrait(background: string, figure: string): string {
  return (
    'data:image/svg+xml;utf8,' +
    encodeURIComponent(
      `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><rect width="64" height="64" fill="${background}"/><circle cx="32" cy="26" r="12" fill="${figure}"/><rect x="12" y="42" width="40" height="30" rx="20" fill="${figure}"/></svg>`,
    )
  );
}

export const PORTRAIT_ADA = portrait('#8faeff', '#16317e');
export const PORTRAIT_GRACE = portrait('#f5c58a', '#6b3b0c');
export const PORTRAIT_ALAN = portrait('#9ad8b4', '#0f4d2b');
```

## Sizes

`size` runs from `xs` to `xl` and is relative to the font size, so an avatar also scales with the text around it.

```typescript title="avatar-sizes.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Avatar } from '@vigdx/ui/avatar';

@Component({
  selector: 'app-avatar-sizes',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Avatar],
  template: `
    <div class="flex flex-col gap-4">
      <div class="flex flex-wrap items-end gap-4">
        <vig-avatar name="Ada Lovelace" size="xs" variant="primary" />
        <vig-avatar name="Ada Lovelace" size="sm" variant="primary" />
        <vig-avatar name="Ada Lovelace" size="md" variant="primary" />
        <vig-avatar name="Ada Lovelace" size="lg" variant="primary" />
        <vig-avatar name="Ada Lovelace" size="xl" variant="primary" />
      </div>
      <div class="flex flex-wrap items-end gap-4">
        <span class="text-xs"><vig-avatar name="Ada Lovelace" /></span>
        <span class="text-base"><vig-avatar name="Ada Lovelace" /></span>
        <span class="text-2xl"><vig-avatar name="Ada Lovelace" /></span>
      </div>
    </div>
  `,
})
export class AvatarSizesDemo {}
```

## Shapes and variants

`shape="square"` suits organizations and projects; its corners follow `--radius-avatar`. `variant` tints the initials or icon fallback; `icon` swaps the fallback glyph.

```typescript title="avatar-shapes.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Avatar } from '@vigdx/ui/avatar';

@Component({
  selector: 'app-avatar-shapes',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Avatar],
  template: `
    <div class="flex flex-col gap-4">
      <div class="flex flex-wrap items-center gap-3">
        <vig-avatar name="Northwind Traders" shape="square" variant="primary" />
        <vig-avatar name="Contoso" shape="square" variant="accent" />
        <vig-avatar shape="square" icon="home" />
      </div>
      <div class="flex flex-wrap items-center gap-3">
        <vig-avatar name="Secondary" />
        <vig-avatar name="Primary" variant="primary" />
        <vig-avatar name="Accent" variant="accent" />
        <vig-avatar name="Info" variant="info" />
        <vig-avatar name="Success" variant="success" />
        <vig-avatar name="Warning" variant="warning" />
        <vig-avatar name="Error" variant="error" />
      </div>
    </div>
  `,
})
export class AvatarShapesDemo {}
```

## Image fallback

When `src` fails to load the avatar falls back to the initials, then to the icon. A new `src` retries the image.

```typescript title="avatar-fallback.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Avatar } from '@vigdx/ui/avatar';

@Component({
  selector: 'app-avatar-fallback',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Avatar],
  template: `
    <div class="flex flex-wrap items-center gap-6 text-sm text-foreground">
      <span class="flex items-center gap-2">
        <vig-avatar [src]="brokenSrc" alt="" name="Grace Hopper" variant="primary" />
        Image fails → initials of <code>name</code>
      </span>
      <span class="flex items-center gap-2">
        <vig-avatar [src]="brokenSrc" alt="Deleted user" />
        No <code>name</code> → <code>icon</code>
      </span>
    </div>
  `,
})
export class AvatarFallbackDemo {
  /** Stands in for an upload that no longer exists. */
  protected readonly brokenSrc = 'data:image/png;base64,broken';
}
```

## Groups and profiles

Overlap avatars in a list for a member stack, with a ring in the page color to separate them. Next to a visible name, give the image an empty `alt` so the name is not read twice.

```typescript title="avatar-group.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Avatar } from '@vigdx/ui/avatar';

import { PORTRAIT_ADA, PORTRAIT_ALAN, PORTRAIT_GRACE } from './portraits';

@Component({
  selector: 'app-avatar-group',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Avatar],
  template: `
    <div class="flex flex-col gap-6">
      <ul class="flex items-center" aria-label="Project members">
        @for (member of members; track member.name) {
          <li class="-ms-2 flex rounded-full ring-2 ring-background first:ms-0">
            <vig-avatar [src]="member.src" [alt]="member.name" />
          </li>
        }
        <li
          class="-ms-2 inline-flex size-[2.5em] items-center justify-center rounded-full bg-muted text-xs font-semibold text-foreground-soft ring-2 ring-background"
        >
          <span aria-hidden="true">+4</span>
          <span class="sr-only">and 4 more members</span>
        </li>
      </ul>

      <div class="flex items-center gap-3">
        <vig-avatar [src]="members[0].src" alt="" size="lg" />
        <div class="flex flex-col">
          <span class="text-sm font-semibold text-foreground">Ada Lovelace</span>
          <span class="text-xs text-muted-foreground">ada&#64;example.com</span>
        </div>
      </div>
    </div>
  `,
})
export class AvatarGroupDemo {
  protected readonly members = [
    { name: 'Ada Lovelace', src: PORTRAIT_ADA },
    { name: 'Grace Hopper', src: PORTRAIT_GRACE },
    { name: 'Alan Turing', src: PORTRAIT_ALAN },
  ];
}
```

```typescript title="portraits.ts"
// Inline SVG portraits so the demos render offline. In an app, `src` is
// usually a URL from your user store or CDN.
function portrait(background: string, figure: string): string {
  return (
    'data:image/svg+xml;utf8,' +
    encodeURIComponent(
      `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><rect width="64" height="64" fill="${background}"/><circle cx="32" cy="26" r="12" fill="${figure}"/><rect x="12" y="42" width="40" height="30" rx="20" fill="${figure}"/></svg>`,
    )
  );
}

export const PORTRAIT_ADA = portrait('#8faeff', '#16317e');
export const PORTRAIT_GRACE = portrait('#f5c58a', '#6b3b0c');
export const PORTRAIT_ALAN = portrait('#9ad8b4', '#0f4d2b');
```

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="avatar"][data-part="image"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

Scope: `[data-scope="avatar"]`

| Part | Element | data-state |
| --- | --- | --- |
| `image` | The `<img>`. |   |
| `initials` | The initials fallback. |   |
| `icon` | The icon fallback. |   |

## Accessibility

- With an image, `alt` is the accessible name: pass the person's name, or `""` when the name is already shown next to the avatar. In dev mode the avatar warns when `src` has no `alt`.
- The initials and icon fallbacks are exposed as `role="img"` labelled with the full `name` (or `alt`), never with the bare initials.
- An avatar with neither image nor name is decorative and hidden from assistive technology.
- Avatars are not interactive. To make one a link or menu trigger, wrap it in an `<a>` or `<button>` with its own accessible name.

## API

### Avatar

Selector: `vig-avatar` · Kind: component

Image of a person or entity, falling back to initials from `name` and then to an icon when the image is missing or fails to load.

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `alt` | `string \| undefined` | `undefined` | Image alternative text — required with `src` (`''` marks it decorative). |
| `icon` | `string` | `'user'` | Registered icon shown when there is neither an image nor a name. |
| `name` | `string \| undefined` | `undefined` | Full name: initials fallback and accessible name when no image shows. |
| `shape` | `AvatarShape` | `'circle'` | — |
| `size` | `AvatarSize` | `'md'` | — |
| `src` | `string \| undefined` | `undefined` | Image URL; when missing or broken the avatar falls back to initials. |
| `variant` | `AvatarVariant` | `'secondary'` | Tone of the initials/icon fallback background. |
