<!--
  GENERATED FILE - do not edit by hand.
  Source of truth: src/components/mds-avatar/usage/ in the magma monorepo.
-->

# mds-avatar - examples

> The API of this component: [`AGENTS.md`](AGENTS.md).

Correct and idiomatic ways to use the `<mds-avatar>` component, ordered from most common to most specialized. The examples assume a working knowledge of the variant / tone ladders documented in [`agents/variants.md`](../../../../agents/variants.md) and the shared component rules in [`agents/conventions.md`](../../../../agents/conventions.md).

#### Profile Image via `src`

The most common form: supply a URL and the component loads the image lazily. If the image fails to load the component falls back to `initials` (or `icon`) when set, else to the generic person glyph - no extra wiring needed.

```html
<!-- the initials show if the photo fails to load -->
<mds-avatar src="https://example.com/foto-utente.jpg" initials="MR"></mds-avatar>
```

```html
<mds-avatar src="https://example.com/foto-utente.jpg"></mds-avatar>
```

#### Initials Fallback

When no photo is available, provide the user's initials. The component displays the first two characters, uppercased, so pass the initials rather than the full name. It also derives a deterministic color from the characters (ignoring non-alphanumeric ones) so the same person always maps to the same hue.

```html
<mds-avatar initials="MR"></mds-avatar>
<!-- displayed as "MR" with a stable identity color -->

<mds-avatar initials="ab"></mds-avatar>
<!-- displayed as "AB" -->
```

#### Manual Variant and Tone (no initials)

When `initials` is not set, you can control the avatar background and icon color through `variant` and `tone`. Use this for non-personal entity avatars where identity color is not required.

```html
<!-- Primary brand color, filled -->
<mds-avatar variant="primary" tone="strong" icon="mi/baseline/support-agent"></mds-avatar>

<!-- Success semantic, weak tint -->
<mds-avatar variant="success" tone="weak"></mds-avatar>
```

#### Icon Avatar for Non-Personal Entities

Use `icon` when the avatar represents a system, service, or non-human entity. Reference icons by their slug (no `.svg` extension). The generic person fallback is replaced by the provided icon.

```html
<mds-avatar icon="mi/baseline/business" variant="primary"></mds-avatar>
<mds-avatar icon="mi/baseline/pets" variant="info"></mds-avatar>
```

#### Overflow Count in Stacked Groups

Use `count` to render a "+N" overflow badge for the last slot in an avatar stack (e.g. "3 more"). The count display overrides every other source (`src`, `initials`, `icon`) and, unlike `initials`, derives no color: it keeps the `variant`. Use inside [`mds-avatar-stack`](../mds-avatar-stack/AGENTS.md) and [`mds-avatar-stack-item`](../mds-avatar-stack-item/AGENTS.md).

```html
<mds-avatar-stack>
  <mds-avatar-stack-item src="https://example.com/foto-01.jpg"></mds-avatar-stack-item>
  <mds-avatar-stack-item src="https://example.com/foto-02.jpg"></mds-avatar-stack-item>
  <mds-avatar-stack-item count="3"></mds-avatar-stack-item>
</mds-avatar-stack>
```

#### Sizing via Utility Classes

`<mds-avatar>` does not expose a `size` prop - its size is controlled by the host element's `width` (the component uses `aspect-ratio: 1/1` internally). Apply a Magma spacing utility class on the host.

```html
<!-- Small avatar -->
<mds-avatar src="https://example.com/foto.jpg" class="w-600"></mds-avatar>

<!-- Default / medium avatar -->
<mds-avatar src="https://example.com/foto.jpg" class="w-1200"></mds-avatar>

<!-- Large avatar -->
<mds-avatar src="https://example.com/foto.jpg" class="w-2400"></mds-avatar>
```

#### Styling Customization

Style the avatar only through its public `--mds-avatar-*` CSS custom properties. Use semantic color roles wrapped in `rgb(var(--magma-<role>))` so dark mode and high-contrast modes work correctly.

```css
.profilo-utente mds-avatar {
  --mds-avatar-background-color: rgb(var(--magma-accent-emphasis));
  --mds-avatar-color: rgb(var(--magma-accent-on-emphasis));
  --mds-avatar-radius: var(--magma-radius-md);

  corner-shape: var(--magma-corner-shape);
}
```

#### Square Avatar via `--mds-avatar-radius`

The default shape is fully circular. Override the CSS custom property to get a square or rounded-square avatar - for example when representing a product or brand logo rather than a person.

The circle stays out of the corner axis, so the host declares no corner shape. A step of the `--magma-radius-*` scale is tuned for the shape of the axis: give the host `corner-shape: var(--magma-corner-shape)` with it, and the inner image and initials follow.

```css
.logo-azienda mds-avatar {
  --mds-avatar-radius: var(--magma-radius-md);

  corner-shape: var(--magma-corner-shape);
}
```

```html
<mds-avatar
  class="logo-azienda w-1600"
  src="https://example.com/logo-azienda.png"
></mds-avatar>
```
