# Avatar

Round picture of a person or a thing. With `src` it shows the image; until the image arrives, and when it cannot load, it shows the initials of `name` (first letter of the first and of the last word: "Mario Rossi" → "MR"), or `text` when given, or an icon when there is neither. The background is one of six gradients picked from `name` (or `colorKey`), so the same person always gets the same color; `color` fixes one. `name` is also the accessible name (`alt` changes it, `alt=""` makes the avatar decorative). `src` can also be a `Blob` or `File`, for images downloaded with authentication: the component creates the object URL and revokes it. `status` adds a dot in the bottom-right corner, `ring` a highlight ring. Its state is exposed as `data-size`, `data-shape`, `data-color`, `data-status` and `data-image` (`loading`, `success`, `error`).

```ts
import { Avatar } from '@likable-hair/svelte';
```

Category: simple. Source: `src/lib/components/simple/media/Avatar.svelte`.

## Examples

### Initials, images and icon

With `name` the avatar shows the initials of the first and last word and picks a color from the name, so the same person always has the same color. `src` shows an image over the initials, which stay visible while it loads or when it fails. `text` replaces the initials; with neither the avatar shows an icon.

```svelte
<script lang="ts">
	import { Avatar } from '@likable-hair/svelte';

	const photo = `data:image/svg+xml,${encodeURIComponent(
		'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><defs><linearGradient id="g" x2="1" y2="1"><stop offset="0" stop-color="#ffb547"/><stop offset="1" stop-color="#ff4d6d"/></linearGradient></defs><rect width="64" height="64" fill="url(#g)"/><circle cx="32" cy="26" r="11" fill="#fff" opacity=".85"/><ellipse cx="32" cy="56" rx="20" ry="14" fill="#fff" opacity=".85"/></svg>'
	)}`;
</script>

<div class="row">
	<Avatar name="Mario Rossi" />
	<Avatar name="Giulia Marini" />
	<Avatar name="Sara Bianchi" />
	<Avatar name="Luca" />
	<Avatar name="Elena Conti" src={photo} />
	<Avatar name="Acme S.p.A." text="ACM" />
	<Avatar />
</div>

<style>
	.row {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 12px;
	}
</style>
```

### Sizes

`size` goes from `xs` (24px) to `xl` (80px); the initials and the icon scale with it. Any other size with `--avatar-size`.

```svelte
<script lang="ts">
	import { Avatar } from '@likable-hair/svelte';

	const sizes = ['xs', 'sm', 'md', 'lg', 'xl'] as const;
</script>

<div class="demo">
	<div class="row">
		{#each sizes as size (size)}
			<Avatar name="Giulia Marini" {size} />
		{/each}
		<Avatar name="Giulia Marini" --avatar-size="100px" />
	</div>
	<div class="row">
		{#each sizes as size (size)}
			<Avatar {size} />
		{/each}
	</div>
</div>

<style>
	.row {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 12px;
	}

	.demo {
		display: flex;
		flex-direction: column;
		gap: 16px;
	}
</style>
```

### Colors

By default the color is one of six gradients picked from the name; `colorKey` picks it from another value, such as an id, so it does not change with the name. `color` fixes a gradient (`1` to `6`), the brand one (`primary`) or grey (`neutral`). The gradients come from the theme's `--global-color-data-*` tokens, and `--avatar-background` and `--avatar-color` change a single avatar.

```svelte
<script lang="ts">
	import { Avatar } from '@likable-hair/svelte';

	const people = ['Mario Rossi', 'Giulia Marini', 'Sara Bianchi', 'Luca Ferri', 'Anna Greco', 'Paolo Russo', 'Chiara Costa', 'Marco Gallo'];
</script>

<div class="demo">
	<div class="row">
		{#each people as name (name)}
			<Avatar {name} />
		{/each}
	</div>
	<div class="row">
		{#each [1, 2, 3, 4, 5, 6] as const as color (color)}
			<Avatar name="Mario Rossi" {color} />
		{/each}
		<Avatar name="Mario Rossi" color="primary" />
		<Avatar name="Mario Rossi" color="neutral" />
		<Avatar name="Mario Rossi" --avatar-background="#111827" --avatar-color="#facc15" />
	</div>
</div>

<style>
	.row {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 12px;
	}

	.demo {
		display: flex;
		flex-direction: column;
		gap: 16px;
	}
</style>
```

### Status, ring and shape

`status` adds a dot in the bottom-right corner (`online`, `away`, `busy`, `offline`) and its meaning to the accessible name. `ring` highlights an avatar, for example the current user. `shape="square"` rounds the corners instead of drawing a circle. The thin gap around the dot and the ring takes `--avatar-gap-color`: set it to the background behind the avatar.

```svelte
<script lang="ts">
	import { Avatar } from '@likable-hair/svelte';
</script>

<div class="row">
	<Avatar name="Mario Rossi" status="online" />
	<Avatar name="Giulia Marini" status="away" />
	<Avatar name="Sara Bianchi" status="busy" />
	<Avatar name="Luca Ferri" status="offline" />
	<Avatar name="Giulia Marini" size="lg" ring status="online" />
	<Avatar name="Acme S.p.A." text="AC" shape="square" />
	<Avatar name="Acme S.p.A." text="AC" shape="square" size="lg" status="online" />
</div>

<style>
	.row {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 16px;
	}
</style>
```

### Loading and protected images

An image that cannot load leaves the initials in place; `bind:imageLoadingStatus` tells `loading`, `success` or `error`. Images downloaded with authentication can be passed as a `Blob` or `File`: the avatar creates the object URL and revokes it when it is no longer needed.

```svelte
<script lang="ts">
	import { Avatar } from '@likable-hair/svelte';

	let status = $state<'loading' | 'success' | 'error'>();

	const blob = new Blob(
		[
			'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><rect width="64" height="64" fill="#2fb6c9"/><circle cx="32" cy="26" r="11" fill="#fff" opacity=".85"/><ellipse cx="32" cy="56" rx="20" ry="14" fill="#fff" opacity=".85"/></svg>'
		],
		{ type: 'image/svg+xml' }
	);
</script>

<div class="demo">
	<div class="row">
		<Avatar name="Mario Rossi" src="/missing-photo.jpg" bind:imageLoadingStatus={status} />
		<span>Image status: {status}</span>
	</div>
	<div class="row">
		<Avatar name="Sara Bianchi" src={blob} />
		<span>From a Blob</span>
	</div>
</div>

<style>
	.row {
		display: flex;
		align-items: center;
		gap: 12px;
	}

	.demo {
		display: flex;
		flex-direction: column;
		gap: 16px;
	}
</style>
```

### Custom content

`children` replaces the initials inside the circle, for example with an icon, and keeps the color and the image; `icon` changes the icon shown when there is no name. `badgeSnippet` puts anything in the bottom-right corner in place of the status dot.

```svelte
<script lang="ts">
	import { Avatar, Icon } from '@likable-hair/svelte';
	import { mdiCurrencyEur, mdiDomain, mdiCheckDecagram } from '@mdi/js';
</script>

<div class="row">
	<Avatar name="Loan request" color={4}>
		<Icon path={mdiCurrencyEur} />
	</Avatar>
	<Avatar icon={mdiDomain} />
	<Avatar name="Giulia Marini" size="lg">
		{#snippet badgeSnippet()}
			<span class="verified"><Icon path={mdiCheckDecagram} /></span>
		{/snippet}
	</Avatar>
</div>

<style>
	.row {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 16px;
	}

	.verified {
		display: grid;
		place-items: center;
		border-radius: 50%;
		background: var(--global-color-bg);
		--icon-size: 18px;
		--icon-color: var(--global-color-primary);
		transform: translate(4px, 4px);
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `src` | `string \| Blob` |  | Image: a URL, or a `Blob`/`File` (for images downloaded with authentication), turned into an object URL that is revoked when it changes or the avatar goes away. An empty string counts as no image. |
| `name` | `string` |  | Full name of the person or thing. It gives the initials, the color and the accessible name. |
| `text` | `string` |  | Text shown instead of the initials of `name`, for example a short company name. |
| `alt` | `string` |  | Accessible name of the avatar. Defaults to `name`; an empty string makes the avatar decorative, as it should be when the name is written next to it. |
| `colorKey` | `string \| number` |  | Value that picks the color instead of `name`, for example the id of the person, so the color does not change when the name does. |
| `color` | `'auto' \| 'primary' \| 'neutral' \| 1 \| 2 \| 3 \| 4 \| 5 \| 6` | `'auto'` | Background: `auto` one of six gradients picked from `colorKey` or `name` (neutral when there is neither), `1` to `6` a fixed one, `primary` the brand gradient, `neutral` grey. |
| `size` | `'xs' \| 'sm' \| 'md' \| 'lg' \| 'xl'` | `'md'` | Size: `xs` 24px, `sm` 32px, `md` 40px, `lg` 56px, `xl` 80px. Any other size with `--avatar-size`. |
| `shape` | `'circle' \| 'square'` | `'circle'` | `circle` or `square` with rounded corners. |
| `ring` | `boolean` | `false` | Draws a ring around the avatar, for example for the current user or a selected person. |
| `status` | `'online' \| 'away' \| 'busy' \| 'offline'` |  | Shows a status dot in the bottom-right corner; its meaning is added to the accessible name. |
| `statusLabel` | `string` |  | Accessible text of the status. Defaults to "Online", "Away", "Busy" or "Offline", in the language of `locale`. |
| `icon` | `string` |  | SVG path of the icon shown when there is no image, `name` or `text`, for example from `@mdi/js`. Defaults to a person. |
| `referrerpolicy` | `ReferrerPolicy` | `'no-referrer'` | Native `referrerpolicy` of the image. `no-referrer` lets profile pictures from Google load. |
| `imageLoadingStatus` (bindable) | `'loading' \| 'success' \| 'error'` |  | State of the image: `loading` until it arrives, then `success` or `error`; `undefined` without `src`. Bind it to react to a broken image. |
| `locale` | `string` |  | BCP 47 locale of the texts (`it` and `en` are built in, other languages fall back to English). Defaults to the one set with `setDefaultLocale`, else `en`. |
| `avatarElement` (bindable) | `HTMLSpanElement` |  | The root `<span>` element. |
| `class` | `string` | `''` | Extra classes on the root `<span>` element. |

It also accepts the native attributes and events of `<span>` (for example `id`, `title`, `aria-label`, `onkeydown`), which are forwarded to the element.

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `children` |  | Replaces the initials or the icon inside the avatar, under the image. |
| `badgeSnippet` |  | Replaces the status dot in the bottom-right corner, for example with a count or a small icon. |

## CSS variables

For a single instance, pass them as style props: `<Avatar --avatar-background="tomato">`. The `--avatar-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--avatar-gap-width` | `0px` |
| `--avatar-size` | `--avatar-default-size`: `40px`<br>`--avatar-default-xs-size`: `24px`<br>`--avatar-default-sm-size`: `32px`<br>`--avatar-default-lg-size`: `56px`<br>`--avatar-default-xl-size`: `80px` |
| `--avatar-icon-scale` | `--avatar-default-icon-scale`: `0.6` |
| `--avatar-border` | `none` |
| `--avatar-border-radius` | `--avatar-default-border-radius`: `50%`<br>`--avatar-default-square-border-radius`: `30%` |
| `--avatar-background` | `--avatar-default-primary-background`: `var(--global-fill-gradient)`<br>`--avatar-default-neutral-background`: `color-mix(in oklab, var(--global-color-surface-solid), var(--global-color-text) 12%)` |
| `--avatar-color` | `--avatar-default-color`: `light-dark(var(--global-color-on-primary), var(--global-color-bg))`<br>`--avatar-default-primary-color`: `var(--global-color-on-primary)`<br>`--avatar-default-neutral-color`: `var(--global-color-text-2)` |
| `--avatar-gap-color` | `--avatar-default-gap-color`: `var(--global-color-bg)` |
| `--avatar-ring-color` | `--avatar-default-ring-color`: `var(--global-color-primary)` |
| `--avatar-font-size` |  |
| `--avatar-font-weight` | `--avatar-default-font-weight`: `600` |
| `--avatar-ring-width` | `--avatar-default-ring-width`: `2px` |
| `--avatar-status-size` | `--avatar-default-status-size`: `28%` |
| `--avatar-status-border-width` | `--avatar-default-status-border-width`: `2px` |
| `--avatar-status-color` | `--avatar-default-online-status-color`: `var(--global-color-success)`<br>`--avatar-default-away-status-color`: `var(--global-color-warning)`<br>`--avatar-default-busy-status-color`: `var(--global-color-error)`<br>`--avatar-default-offline-status-color`: `var(--global-color-text-3)` |

### Other defaults

| Variable | Value |
| --- | --- |
| `--avatar-default-font-scale` | `0.38` |
| `--avatar-default-background-1` | `linear-gradient(135deg, light-dark(color-mix(in oklab, var(--global-color-data-1), black 14%), color-mix(in oklab, var(--global-color-data-1), white 22%)), light-dark(color-mix(in oklab, color-mix(in oklab, var(--global-color-data-1), var(--global-color-data-3) 55%), black 14%), color-mix(in oklab, color-mix(in oklab, var(--global-color-data-1), var(--global-color-data-3) 55%), white 22%)))` |
| `--avatar-default-background-2` | `linear-gradient(135deg, light-dark(color-mix(in oklab, var(--global-color-data-2), black 14%), color-mix(in oklab, var(--global-color-data-2), white 22%)), light-dark(color-mix(in oklab, color-mix(in oklab, var(--global-color-data-2), var(--global-color-data-1) 55%), black 14%), color-mix(in oklab, color-mix(in oklab, var(--global-color-data-2), var(--global-color-data-1) 55%), white 22%)))` |
| `--avatar-default-background-3` | `linear-gradient(135deg, light-dark(color-mix(in oklab, var(--global-color-data-3), black 14%), color-mix(in oklab, var(--global-color-data-3), white 22%)), light-dark(color-mix(in oklab, color-mix(in oklab, var(--global-color-data-3), var(--global-color-data-6) 50%), black 14%), color-mix(in oklab, color-mix(in oklab, var(--global-color-data-3), var(--global-color-data-6) 50%), white 22%)))` |
| `--avatar-default-background-4` | `linear-gradient(135deg, light-dark(color-mix(in oklab, var(--global-color-data-4), black 14%), color-mix(in oklab, var(--global-color-data-4), white 22%)), light-dark(color-mix(in oklab, color-mix(in oklab, var(--global-color-data-4), var(--global-color-data-2) 55%), black 14%), color-mix(in oklab, color-mix(in oklab, var(--global-color-data-4), var(--global-color-data-2) 55%), white 22%)))` |
| `--avatar-default-background-5` | `linear-gradient(135deg, light-dark(color-mix(in oklab, var(--global-color-data-5), black 14%), color-mix(in oklab, var(--global-color-data-5), white 22%)), light-dark(color-mix(in oklab, color-mix(in oklab, var(--global-color-data-5), var(--global-color-data-6) 45%), black 14%), color-mix(in oklab, color-mix(in oklab, var(--global-color-data-5), var(--global-color-data-6) 45%), white 22%)))` |
| `--avatar-default-background-6` | `linear-gradient(135deg, light-dark(color-mix(in oklab, var(--global-color-data-6), black 14%), color-mix(in oklab, var(--global-color-data-6), white 22%)), light-dark(color-mix(in oklab, color-mix(in oklab, var(--global-color-data-6), var(--global-color-data-5) 40%), black 14%), color-mix(in oklab, color-mix(in oklab, var(--global-color-data-6), var(--global-color-data-5) 40%), white 22%)))` |
| `--avatar-default-ring-gap` | `2px` |
