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

# mds-toast-region - examples

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

Correct and idiomatic ways to use the `<mds-toast-region>` component, ordered from most common to most specialized. The toasts follow the examples of `<mds-toast>`; the shared component rules live in [`agents/conventions.md`](../../../../agents/conventions.md).

#### One Region in the Application Shell

The region holds the `<mds-toast>` elements of the application: every toast of the application goes into it, and the region lines them up at its edge and announces them. A toast shown in it leaves after its `duration`.

```html
<mds-toast-region>
  <mds-toast visible variant="success">Documento salvato</mds-toast>
</mds-toast-region>
```

Place one region in the shell of the application, outside the content that changes from page to page, so it is in the page before any message; the toasts are added to it from code (see the next pattern).

```html
<body>
  <main>
    <!-- contenuto dell'applicazione -->
  </main>
  <mds-toast-region></mds-toast-region>
</body>
```

#### Showing a Toast From Code

Create the toast, append it to the region and show it; remove it from the page once it has closed. The region places it, announces it, and makes room for it.

```javascript
const region = document.querySelector('mds-toast-region');

const showToast = (text, variant = 'success') => {
  const toast = document.createElement('mds-toast');
  toast.variant = variant;
  toast.textContent = text;
  toast.addEventListener('mdsToastClose', () => toast.remove());
  region.append(toast);
  toast.visible = true;
};

showToast('Documento salvato');
```

#### A Message That Stays Among Confirmations

An error that the person must act on stays (`duration="0"`) with its action and the hide button, while the confirmations around it come and go: the region never hides a toast that stays to make room.

```html
<mds-toast-region>
  <mds-toast visible variant="error" duration="0" dismissible>
    Invio non riuscito
    <mds-button slot="action" size="sm" variant="error" tone="weak">Riprova</mds-button>
  </mds-toast>
  <mds-toast visible variant="success">Bozza salvata</mds-toast>
</mds-toast-region>
```

#### At the Top Edge

Set `position` on the region, not on the toasts: it lines the toasts up at that edge, the newest one at the top, and gives them the position, so they come in and leave from that edge.

```html
<mds-toast-region position="top-right"></mds-toast-region>
```

#### Fewer Toasts at Once

Lower `max` for an application with frequent confirmations or little room: when one more toast shows, the oldest one with a timer leaves first.

```html
<mds-toast-region max="2"></mds-toast-region>
```

#### Spacing Between the Toasts

Change the space between the toasts with `--mds-toast-region-gap`.

```css
mds-toast-region {
  --mds-toast-region-gap: 12px;
}
```
