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

# mds-entity - examples

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

Correct and idiomatic ways to use the `<mds-entity>` 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).

#### Person Entity with Image

The most common form. Supply `src` for the avatar image and put the entity name in the default slot. Use `<mds-text>` with `typography="h6"` for the primary label so typography tokens are applied consistently.

```html
<mds-entity src="/avatars/mario-rossi.png">
  <mds-text truncate="word" typography="h6">Mario Rossi</mds-text>
</mds-entity>
```

#### Entity with Initials Fallback

When no image is available, provide `initials` instead of `src`. The avatar chip renders the initials text.

```html
<mds-entity initials="mr">
  <mds-text truncate="word" typography="h6">Mario Rossi</mds-text>
</mds-entity>
```

#### Entity with Icon

Use `icon` for non-person entities (locations, files, organizations). Reference icons by their slug from the Magma icon library.

```html
<mds-entity icon="mi/baseline/location-on">
  <mds-text truncate="word" typography="h6">Maggioli Headquarters</mds-text>
</mds-entity>
```

#### Secondary Details via `detail` Slot

Project additional information (role, email, status) into the `detail` slot. It renders below the primary name in a subdued color. Use `<mds-text typography="caption">` for plain text details.

```html
<mds-entity src="/avatars/giulia-bianchi.png">
  <mds-text truncate="word" typography="h6">Giulia Bianchi</mds-text>
  <mds-text truncate="word" slot="detail" typography="caption">giulia@example.com</mds-text>
  <mds-badge slot="detail" variant="success" tone="weak" label="Attiva"></mds-badge>
</mds-entity>
```

#### Trailing Action Buttons via `action` Slot

Use the `action` slot for icon-only `<mds-button>` elements. Each button is sized and spaced automatically by the component. Always provide `title` on icon-only buttons for accessibility.

```html
<mds-entity src="/avatars/luca-verdi.png">
  <mds-text truncate="word" typography="h6">Luca Verdi</mds-text>
  <mds-text truncate="word" slot="detail" typography="caption">luca@example.com</mds-text>
  <mds-button slot="action" icon="mdi/pencil" variant="primary" tone="strong" title="Modifica"></mds-button>
  <mds-button slot="action" icon="mdi/delete" variant="error" tone="strong" title="Elimina"></mds-button>
</mds-entity>
```

#### Colored Avatar with `variant` and `tone`

Pair `variant` and `tone` to color the avatar chip by entity category. `tone` is limited to `strong` and `weak`; `variant` accepts the full avatar color set.

```html
<!-- Strong fill, primary variant -->
<mds-entity icon="mi/baseline/business" variant="primary" tone="strong">
  <mds-text truncate="word" typography="h6">Divisione Commerciale</mds-text>
</mds-entity>

<!-- Weak tint, warning variant -->
<mds-entity icon="mi/baseline/warning" variant="warning" tone="weak">
  <mds-text truncate="word" typography="h6">Alert configurazione</mds-text>
</mds-entity>
```

#### Await / Loading State

Set the `await` boolean attribute while the entity data is being fetched. The avatar is hidden and a spinner appears in its place. Remove the attribute when loading is complete - do not set `await="false"`.

```html
<mds-entity await icon="mi/baseline/done" variant="success">
  <mds-text truncate="word" typography="h6">Caricamento in corso...</mds-text>
  <mds-text truncate="word" slot="detail" typography="caption">Attendere prego</mds-text>
</mds-entity>
```

#### Upload / Async Workflow

Combine `await` with `icon` and `variant` to drive a state machine: show the spinner while uploading, then reveal the result icon when done.

```html
<!-- While uploading -->
<mds-entity await>
  <mds-text truncate="word" typography="h6">Report finanziario 2024.docx</mds-text>
  <mds-text truncate="word" slot="detail" typography="caption">Upload in corso...</mds-text>
  <mds-button slot="action" icon="mi/baseline/cancel" variant="light" title="Annulla upload"></mds-button>
</mds-entity>

<!-- After success -->
<mds-entity icon="mi/baseline/done" variant="success" tone="weak">
  <mds-text truncate="word" typography="h6">Report finanziario 2024.docx</mds-text>
  <mds-text truncate="word" slot="detail" typography="caption">Upload completato con successo</mds-text>
</mds-entity>
```

#### Styling Customization

Style the entity only through its public `--mds-entity-*` CSS custom properties. Set them on the host or a parent selector; use the semantic color roles via `rgb(var(--magma-<role>))` so dark mode and high-contrast modes keep working. The avatar takes its colours from `variant` and `tone`, not from a custom property.

```css
.entity-highlight mds-entity {
  --mds-entity-background: rgb(var(--magma-accent-surface));
  --mds-entity-color: rgb(var(--magma-accent-fg));
}
```

#### Spacing Between the Name and the Details

The name and the `detail` row stack in the `infos` part, 2px apart, so a badge or a caption in `detail` never touches the name. When a layout needs more room between them, set the gap of the part; the nodes inside it are not API.

```css
.entity-roomy mds-entity::part(infos) {
  gap: calc(var(--spacing) * 100);
}
```
