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

# mds-toast - examples

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

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

#### Basic Text Toast

The simplest form. Set `visible` to show the toast; the default slot holds the message text only. The component auto-dismisses after the default 5000 ms.

```html
<mds-toast visible>
  Operazione completata con successo
</mds-toast>
```

#### Status Variants

Use `variant` to communicate the meaning of the feedback. The status family (`error`, `warning`, `info`, `success`) maps directly to the nature of the event. Do not pick a variant for its color alone.

```html
<!-- Conferma positiva -->
<mds-toast visible variant="success">
  Documento salvato correttamente
</mds-toast>

<!-- Errore bloccante -->
<mds-toast visible variant="error">
  Errore durante il salvataggio. Riprova.
</mds-toast>

<!-- Avviso non bloccante -->
<mds-toast visible variant="warning">
  La sessione scade tra 5 minuti
</mds-toast>

<!-- Informazione neutrale -->
<mds-toast visible variant="info">
  Aggiornamento disponibile
</mds-toast>
```

#### Tone for Emphasis

Pair `variant` with `tone` to adjust visual weight. `strong` (default) uses the stronger tint of the variant and a larger shadow; `weak` uses a softer tint and a smaller shadow for lower emphasis.

```html
<!-- Forte enfasi: errore urgente -->
<mds-toast visible variant="error" tone="strong">
  Connessione persa
</mds-toast>

<!-- Enfasi ridotta: info secondaria -->
<mds-toast visible variant="info" tone="weak">
  Sincronizzazione in background
</mds-toast>
```

#### Toast with Icon

Use the `icon` slot with an `<mds-icon>` to add a leading glyph. The icon color is tuned per variant.

```html
<mds-toast visible variant="success">
  <mds-icon slot="icon" name="mi/baseline/check-circle"></mds-icon>
  File caricato con successo
</mds-toast>
```

#### Toast with Action

Use the `action` slot with an `<mds-button>` to offer a follow-up. The action region appears only while a `[slot="action"]` child is present, also when it is added after the first render - no empty layout is produced otherwise.

```html
<mds-toast visible variant="error">
  <mds-icon slot="icon" name="mi/baseline/error-outline"></mds-icon>
  Invio non riuscito
  <mds-button slot="action" size="sm" variant="error" tone="weak">
    Riprova
  </mds-button>
</mds-toast>
```

#### Persistent Toast (No Auto-Dismiss)

Set `duration="0"` to keep the toast visible until the user explicitly acts. Use this when the message carries an action the user must not miss. Hide it from your action handler by removing `visible`, and add `dismissible` so the person can send the message away without using the action.

```html
<mds-toast visible variant="warning" duration="0" dismissible>
  <mds-icon slot="icon" name="mi/baseline/warning"></mds-icon>
  Sessione in scadenza
  <mds-button slot="action" size="sm" variant="warning" tone="weak">
    Rinnova
  </mds-button>
</mds-toast>
```

#### Custom Auto-Dismiss Duration

Override the 5000 ms default when the message needs more reading time (e.g. it carries an action) or can disappear faster (e.g. a trivial confirmation). Duration is in milliseconds.

```html
<!-- Messaggio breve: 2 secondi -->
<mds-toast visible variant="success" duration="2000">
  Copiato negli appunti
</mds-toast>

<!-- Con azione: 8 secondi -->
<mds-toast visible variant="info" duration="8000">
  Importazione completata con 3 avvisi
  <mds-button slot="action" size="sm" variant="info" tone="weak">
    Vedi report
  </mds-button>
</mds-toast>
```

#### Several Toasts at Once

Two toasts visible at the same time with the same `position` are drawn at the same place, one over the other. Append the toasts of the application to one `<mds-toast-region>`, placed once in its shell: it lines them up at its edge, announces them, and makes room when too many show at once.

```html
<mds-toast-region position="bottom-center">
  <mds-toast visible variant="success">Documento salvato</mds-toast>
  <mds-toast visible variant="success">Link copiato negli appunti</mds-toast>
</mds-toast-region>
```

#### Positioning

Use `position` to anchor the toast to one of the six viewport edges. Pick the edge that matches the application's layout convention - bottom-center is the default and works for most surfaces.

```html
<!-- Predefinito: centro in basso -->
<mds-toast visible variant="info" position="bottom-center">
  Messaggio salvato
</mds-toast>

<!-- Angolo in alto a destra (toolbar / panel) -->
<mds-toast visible variant="success" position="top-right">
  Impostazioni aggiornate
</mds-toast>
```

#### Listening for the Close Event

Listen for `mdsToastClose` to remove the toast from the DOM or update queue state after the outro animation completes. It is emitted once for every hiding, whoever hid the toast, and `event.detail.reason` says who: `timeout`, `close` (the hide button) or `hide` (your code). Do not react to `visible` becoming `false` directly - the animation is still running at that point.

```javascript
document.querySelector('mds-toast').addEventListener('mdsToastClose', (event) => {
  // rimuovi il toast dalla coda o aggiorna lo stato dell'applicazione
  toastQueue.shift();
  if (event.detail.reason === 'close') {
    // la persona ha mandato via il messaggio senza usare l'azione
  }
});
```

#### Styling Customization

Style the toast only through its public `--mds-toast-*` CSS custom properties: the background, the text colour, the width and the shadow. Set them on the host element or a parent selector; use the semantic color roles via `rgb(var(--magma-<role>))` so dark mode and high-contrast modes keep working.

```css
.app-notifications mds-toast {
  --mds-toast-background: rgb(var(--magma-surface-inverse));
  --mds-toast-color: rgb(var(--magma-on-inverse));
  --mds-toast-width: 480px;
  --mds-toast-shadow: var(--shadow-xl);
}
```
