# Avatar

- **[Atoms](/components/)**

A person or team as a photo, or as initials on a colour picked from the name.

## When to use

- **Use for**: Next to a name in lists, comments, headers and assignee pickers.
- **Not for**: Logos and product images. Use Media.
- **Pairs with**: Badge, Card, a name in text.

## Preview

avatar.tsx

```
import { Theme } from '@/components/klarhimmel/theme/style';
import { Avatar } from '@/components/klarhimmel/ui/avatar';

export default function Example() {
  return (
    <Theme theme="brand">
      <Avatar name="Maja Lindqvist" src="/people/maja.jpg" />
    </Theme>
  );
}
```

## Import

- **CLI**

  ```
  npx shadcn add @klarhimmel/avatar
  ```
- **Import**

  ```
  import { Avatar } from '@/components/klarhimmel/ui/avatar';
  ```

- **Files**: avatar.tsx, in components/klarhimmel/ui/. With it: theme, utils.
- **Render in**: Render it inside <Theme>, once near the root of your app. Tokens and fonts are scoped to it, so outside it the component is unstyled.

## Examples

- **Sizes**

  ```
  import { Avatar } from '@/components/klarhimmel/ui/avatar';
  
  export default function AvatarSizes() {
    return (
      <div className="flex flex-wrap items-center gap-4">
        <Avatar name="Maja Lindqvist" size="xs" />
        <Avatar name="Maja Lindqvist" size="sm" />
        <Avatar name="Maja Lindqvist" />
        <Avatar name="Maja Lindqvist" size="lg" />
        <Avatar name="Exempelhotellet" size="lg" square />
      </div>
    );
  }
  ```
- **Fallback**

  ```
  import { Avatar } from '@/components/klarhimmel/ui/avatar';
  
  const people = ['Maja Lindqvist', 'Nils Berg', 'Ada Ek', 'Oskar Holm', 'Sofia Nord'];
  
  export default function AvatarFallback() {
    return (
      <div className="flex flex-col gap-4">
        <ul className="flex flex-wrap gap-4">
          {people.map((name) => (
            <li key={name} className="flex items-center gap-2 text-kh-small">
              <Avatar name={name} decorative />
              {name}
            </li>
          ))}
        </ul>
        <div className="flex items-center gap-3 text-kh-small text-kh-muted">
          <Avatar name="Guest without a photo" />
          No photo, or one that fails to load, shows initials.
        </div>
        <div className="flex -space-x-2">
          {people.slice(0, 4).map((name) => (
            <Avatar key={name} name={name} size="sm" className="ring-2 ring-kh-canvas" />
          ))}
        </div>
      </div>
    );
  }
  ```

## Props

- **name**: Whose face this is. Becomes the accessible name and the initials.
- **size**: 24, 32, 40 and 56 px.
- **square**: A rounded square, for teams and organisations.
- **src**: A photo. Falls back to initials if it is missing or fails to load.
- **srcSet**: A photo. Falls back to initials if it fails to load.
- **decorative**: Hide it from assistive tech when the name is written next to it.
- **className**: Extra classes, merged after the component's own.

## Accessibility

- Tab · Skips it: an avatar is not interactive
- Screen reader · Reads the name. With decorative set, it reads nothing

[Accessibility](/get-started/accessibility)

Not interactive. Wrap it in a link or button yourself if it should be.

[Previous](/components/icon-button)

[Next](/components/banner)
