# Avatar

## Usage

Import the component from its dedicated entry. Structural styles are loaded automatically.

Import the theme once at your app entry:

```ts
import '@astryx-vue/themes/neutral.css'
```

Component usage:

```vue
<script setup lang="ts">
import { Avatar, AvatarStatusDot } from '@astryx-vue/core/Avatar'
</script>

<template>
  <Avatar src="/user.jpg" name="John Doe" />
  <Avatar name="Jane Smith" size="xl" />
  <Avatar src="/user.jpg" name="John Doe">
    <template #status>
      <AvatarStatusDot variant="success" />
    </template>
  </Avatar>
</template>
```

### Anatomy

- **Photo**: The profile image, loaded from the src URL. Shown when available.
- **Initials**: The first letter, digit or emoji of the first and last words of the name; punctuation is skipped. Shown when no photo is available. A name with no letters, digits or emoji shows the default icon instead.
- **Default icon**: A generic person silhouette. Shown when there is no photo or name.
- **Status dot**: A small indicator in the bottom-right corner showing availability (online, away, busy). Each variant pairs colour with a distinct shape so status does not rely on colour alone.

## Best practices

| Guidance | Practice |
| --- | --- |
| Recommended | Always provide `name` for fallback text and accessible labeling. |
| Recommended | Use consistent avatar sizes in lists, comments, or chat messages. |
| Recommended | Use `AvatarStatusDot` inside the `status` slot to indicate presence or status. |
| Recommended | When the avatar acts as a link or button, provide a meaningful accessible name via `alt` or `name`. |
| Don't | Use avatars as a replacement for user names. Pair avatars with a name or label. |
| Don't | Use low-resolution images. Avatars are often clipped as circles, which magnifies blur. |

## Examples

### Fallback chain

Demonstrates the avatar fallback chain: primary image, fallback image, initials, then default icon.

```vue
<script setup lang="ts">
import { Avatar } from '@astryx-vue/core/Avatar'
import { HStack, VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <VStack :gap="4">
    <HStack align="center" :gap="3">
      <Avatar src="/template-assets/DATA-Daniela-Gimenez.png" name="Daniela Gimenez" size="lg" />
      <Text type="supporting">
        Valid src
      </Text>
    </HStack>
    <HStack align="center" :gap="3">
      <Avatar
        src="/template-assets/does-not-exist-primary.jpg"
        fallback-src="/template-assets/DATA-Ami-Pena.png"
        name="Invalid User"
        size="lg"
      />
      <Text type="supporting">
        Invalid src, valid fallbackSrc
      </Text>
    </HStack>
    <HStack align="center" :gap="3">
      <Avatar
        src="/template-assets/does-not-exist-primary.jpg"
        fallback-src="/template-assets/does-not-exist-fallback.jpg"
        name="Test User"
        size="lg"
      />
      <Text type="supporting">
        Both invalid, has name
      </Text>
    </HStack>
    <HStack align="center" :gap="3">
      <Avatar src="/template-assets/does-not-exist-primary.jpg" size="lg" />
      <Text type="supporting">
        All invalid, no name
      </Text>
    </HStack>
  </VStack>
</template>
```

### Initials

Show initials instead of a photo. The avatar extracts the first and last initials from the name automatically. Use when you only have a user name.

```vue
<script setup lang="ts">
import { Avatar } from '@astryx-vue/core/Avatar'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'

const names = [
  { name: 'John Doe', note: 'First + last' },
  { name: 'Alice', note: 'Single name' },
  { name: 'Bob Smith Johnson', note: 'Multi-word' },
  { name: 'Dr. Sarah Connor', note: 'Prefixed' },
]
</script>

<template>
  <Stack align="center" direction="horizontal" :gap="6">
    <Stack v-for="item in names" :key="item.name" direction="vertical" :gap="2" h-align="center">
      <Avatar :name="item.name" size="lg" />
      <Text type="supporting" color="secondary">
        {{ item.note }}
      </Text>
    </Stack>
  </Stack>
</template>
```

### Interactive

Pass href to make the avatar a link, or as to use a router link instead. A click listener without an address makes it a button. Both forms are focusable and show a focus-visible ring.

```vue
<script setup lang="ts">
import { Avatar } from '@astryx-vue/core/Avatar'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'

function openProfile() {
  // The upstream example uses a browser alert so the click is visible.
  // eslint-disable-next-line no-alert
  window.alert('Opening Margot Schroder’s profile')
}
</script>

<template>
  <Stack align="start" direction="horizontal" :gap="6">
    <Stack direction="vertical" :gap="2" h-align="center">
      <Avatar
        src="/template-assets/DATA-Itai-Jordaan.png"
        name="Itai Jordaan"
        size="xl"
        href="https://example.com/people/itai-jordaan"
      />
      <Text type="supporting" color="secondary">
        Link (href)
      </Text>
    </Stack>
    <Stack direction="vertical" :gap="2" h-align="center">
      <Avatar
        src="/template-assets/DATA-Margot-Schroder.png"
        name="Margot Schroder"
        size="xl"
        @click="openProfile"
      />
      <Text type="supporting" color="secondary">
        Button (onClick)
      </Text>
    </Stack>
  </Stack>
</template>
```

### Tooltip

By default an avatar shows its name in a tooltip on hover and keyboard focus. Pass a string to show custom text instead, or false to turn the tooltip off.

```vue
<script setup lang="ts">
import { Avatar } from '@astryx-vue/core/Avatar'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <Stack direction="vertical" :gap="5">
    <Stack direction="vertical" :gap="2">
      <Text type="supporting" color="secondary">
        Hover or focus to reveal each name
      </Text>
      <Stack align="center" direction="horizontal" :gap="4">
        <Avatar src="/template-assets/DATA-Ana-Thomas.png" name="Ana Thomas" size="xl" />
        <Avatar src="/template-assets/DATA-Drew-Young.png" name="Drew Young" size="xl" />
        <Avatar src="/template-assets/DATA-Jihoo-Song.png" name="Jihoo Song" size="xl" />
      </Stack>
    </Stack>
    <Stack direction="vertical" :gap="2">
      <Text type="supporting" color="secondary">
        Custom tooltip text
      </Text>
      <Avatar
        src="/template-assets/DATA-Itai-Jordaan.png"
        name="Itai Jordaan"
        size="xl"
        tooltip="Itai Jordaan · Engineering Lead"
      />
    </Stack>
  </Stack>
</template>
```

### User card

Place an avatar next to a name and role to create a user card row. Use for comment headers, contact lists, profile sections, or anywhere you need to identify a person.

```vue
<script setup lang="ts">
import { Avatar, AvatarStatusDot } from '@astryx-vue/core/Avatar'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'

const users = [
  { name: 'Itai Jordaan', src: '/template-assets/DATA-Itai-Jordaan.png', role: 'Engineering Lead', variant: 'success' as const, status: 'Online' },
  { name: 'Margot Schroder', src: '/template-assets/DATA-Margot-Schroder.png', role: 'Product Designer', variant: 'neutral' as const, status: 'Away' },
  { name: 'Daniela Gimenez', src: '/template-assets/DATA-Daniela-Gimenez.png', role: 'Engineering Manager', variant: 'error' as const, status: 'Busy' },
]
</script>

<template>
  <Stack direction="vertical" :gap="4">
    <Stack v-for="user in users" :key="user.name" align="center" direction="horizontal" :gap="3">
      <Avatar :src="user.src" :name="user.name" size="lg">
        <template #status>
          <AvatarStatusDot :variant="user.variant" :label="user.status" />
        </template>
      </Avatar>
      <Stack direction="vertical" :gap="0">
        <Text type="body" weight="bold">
          {{ user.name }}
        </Text>
        <Text type="supporting" color="secondary">
          {{ user.role }}
        </Text>
      </Stack>
    </Stack>
  </Stack>
</template>
```

### Photo

Show a profile photo at different sizes. Use when you have a user photo URL. If the image fails to load, initials are shown instead.

```vue
<script setup lang="ts">
import { Avatar } from '@astryx-vue/core/Avatar'
import { Stack } from '@astryx-vue/core/Stack'
</script>

<template>
  <Stack align="center" direction="horizontal" :gap="4">
    <Avatar src="/template-assets/DATA-Ami-Pena.png" name="Ami Pena" size="xsm" />
    <Avatar src="/template-assets/DATA-Ana-Thomas.png" name="Ana Thomas" size="md" />
    <Avatar src="/template-assets/DATA-Daniela-Gimenez.png" name="Daniela Gimenez" size="lg" />
    <Avatar src="/template-assets/DATA-Gabriela-Fernandez.png" name="Gabriela Fernandez" size="xl" />
  </Stack>
</template>
```

### Status dot

Add a status dot to an avatar to show whether someone is online, away, or busy. Use in chat, messaging, or any UI where knowing availability matters.

```vue
<script setup lang="ts">
import { Avatar, AvatarStatusDot } from '@astryx-vue/core/Avatar'
import { Stack } from '@astryx-vue/core/Stack'
</script>

<template>
  <Stack align="center" direction="horizontal" :gap="4">
    <Avatar src="/template-assets/DATA-Itai-Jordaan.png" name="Itai Jordaan" size="xl">
      <template #status>
        <AvatarStatusDot variant="success" label="Online" />
      </template>
    </Avatar>
    <Avatar src="/template-assets/DATA-Margot-Schroder.png" name="Margot Schroder" size="xl">
      <template #status>
        <AvatarStatusDot variant="neutral" label="Offline" />
      </template>
    </Avatar>
    <Avatar src="/template-assets/DATA-Pablo-Morales.png" name="Pablo Morales" size="xl">
      <template #status>
        <AvatarStatusDot variant="error" label="Busy" />
      </template>
    </Avatar>
  </Stack>
</template>
```

## Avatar Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `src` | `string` | - | The avatar image URL. |
| `fallbackSrc` | `string` | - | Backup image URL used when the primary image fails to load. |
| `name` | `string` | - | User name. Used for initials fallback and accessible labeling. |
| `alt` | `string` | - | Accessible label. Takes precedence over `name`. |
| `size` | `AvatarSize` | `'md'` | Avatar size: a named size or one of the numeric pixel values listed below. Inside an `AvatarGroup` the group's size always wins, including the group's default. |
| `shape` | `AvatarShape` | `'circle'` | Avatar shape: `circle`, `rounded`, or `square`. |
| `href` | `string` | - | Makes the avatar a link. |
| `as` | `LinkRoot` | - | Link root used when `href` is set. It receives `href` and `to`. Without an address the avatar stays a button or a `span`. |
| `target` | `string` | - | Browsing context of the link. Applies only when `href` is set. |
| `rel` | `string` | - | Link relationship. `noopener noreferrer` is added when `target` is `_blank`. |

### Named sizes

| Name | Pixel value |
| --- | --- |
| `xsm` | 20px |
| `sm` | 24px |
| `md` | 36px |
| `lg` | 48px |
| `xl` | 128px |

### Numeric sizes

A numeric `size` must be one of the pixel values the `AvatarSize` type lists:

`16 | 20 | 24 | 32 | 36 | 40 | 48 | 60 | 64 | 72 | 96 | 128 | 144 | 180`

Any other number is a type error.

## Avatar Slots

| Name | Description |
| --- | --- |
| `default` | Custom content that replaces the default person icon. Only used when there is no image and no name. |
| `status` | Status indicator placed at the corner of the avatar, typically an `AvatarStatusDot`. |

## Avatar Events

| Name | Description |
| --- | --- |
| `error` | Emitted when the primary image fails to load. |

## AvatarStatusDot Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `AvatarStatusDotVariant` | `'success'` | Semantic status: `success` (filled dot), `neutral` (hollow ring), or `error` (dot with a minus bar). `info` and `warning` are older names and still draw as `success` and `error`. |
| `label` | `string` | — | Accessible name for the status, composed into the avatar's own name (`Jane Doe, Online`). |

## Native attributes

The root is a `<span>` by default. With `href` it is an `<a>` or the component passed to `as`. A click listener without an address makes it a `<button>`.

The following are passed to the root element:

- HTML attributes.
- `aria-*` attributes.
- `data-*` attributes.
- `class`.
- `style`.
