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

# mds-modal - examples

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

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

#### Basic Modal with Inline Content

The simplest form: toggle `opened` from JavaScript to show the dialog. The built-in close button and backdrop dismiss the modal automatically with `interaction="relaxed"` (default). React to the `mdsModalClose` event to sync your state.

```html
<mds-button id="btn-apri">Apri dialogo</mds-button>

<mds-modal id="dialogo">
  <div class="p-400">
    <mds-text>Sei sicuro di voler continuare?</mds-text>
  </div>
</mds-modal>

<script>
  const btn = document.getElementById('btn-apri');
  const modal = document.getElementById('dialogo');
  btn.addEventListener('click', () => { modal.opened = true; });
  modal.addEventListener('mdsModalClose', () => { modal.opened = undefined; });
</script>
```

#### Fixed Header and Footer via `top` / `bottom` Slots

Use the `top` slot for a sticky header and the `bottom` slot for a sticky footer (e.g. action buttons). The default slot renders scrollable body content between them.

```html
<mds-modal id="modifica-utente" position="right">
  <header slot="top" class="p-400 bg-surface-overlay shadow-ring-weak">
    <mds-text typography="h5">Modifica utente</mds-text>
  </header>

  <div class="p-400 grid gap-400">
    <mds-input-field label="Nome">
      <mds-input name="nome"></mds-input>
    </mds-input-field>
    <mds-input-field label="Cognome">
      <mds-input name="cognome"></mds-input>
    </mds-input-field>
  </div>

  <footer slot="bottom" class="p-400 flex gap-300 justify-end bg-surface-overlay shadow-ring-weak">
    <mds-button variant="dark" tone="outline" id="btn-annulla">Annulla</mds-button>
    <mds-button variant="primary" tone="strong" id="btn-salva">Salva</mds-button>
  </footer>
</mds-modal>
```

#### Edge and Corner Positions

`position` anchors the window to an edge or corner and drives the slide-in direction. Use `right` or `left` for side panels, `bottom` for mobile drawers.

```html
<!-- Side panel from the right (default in most app shells) -->
<mds-modal id="pannello-laterale" position="right">
  <div class="p-400">
    <mds-text>Dettagli del documento</mds-text>
  </div>
</mds-modal>

<!-- Drawer from the bottom (mobile) -->
<mds-modal id="drawer-mobile" position="bottom">
  <div class="p-400">
    <mds-text>Azioni disponibili</mds-text>
  </div>
</mds-modal>
```

#### Strict Interaction Mode

Set `interaction="strict"` when the user must explicitly confirm or cancel before the dialog closes - for example, a form with unsaved changes. Backdrop clicks, `Esc` and touch swipes are ignored; only the close button or the `close()` method can dismiss the modal.

```html
<mds-modal id="conferma-elimina" interaction="strict">
  <div class="p-400 grid gap-400">
    <mds-text typography="h5">Eliminare il documento?</mds-text>
    <mds-text>Questa operazione non e' reversibile.</mds-text>
    <div class="flex gap-300 justify-end">
      <mds-button variant="dark" tone="outline" id="btn-no">Annulla</mds-button>
      <mds-button variant="error" tone="strong" id="btn-si">Elimina</mds-button>
    </div>
  </div>
</mds-modal>

<script>
  document.getElementById('btn-no').addEventListener('click', () => {
    document.getElementById('conferma-elimina').close();
  });
</script>
```

#### Programmatic Close via `close()` Method

Call the async `close()` method from JavaScript when you need to dismiss the modal from outside its UI - for example, after a successful async operation.

```html
<mds-modal id="salvataggio">
  <div class="p-400">
    <mds-text>Salvataggio in corso...</mds-text>
  </div>
</mds-modal>

<script>
  async function salva() {
    const modal = document.getElementById('salvataggio');
    modal.opened = true;
    await salvaRisorsa();
    await modal.close();
  }
</script>
```

#### Listening to Animation Events

`mdsModalShow` fires when the intro animation completes (the modal is fully visible). Use it to focus the first interactive element. `mdsModalHide` fires when the outro animation completes; use it to detach the element or reset content.

```html
<mds-modal id="modal-focus" position="right">
  <div class="p-400">
    <mds-input-field label="Cerca">
      <mds-input id="input-ricerca" name="ricerca"></mds-input>
    </mds-input-field>
  </div>
</mds-modal>

<script>
  const modal = document.getElementById('modal-focus');
  const input = document.getElementById('input-ricerca');

  modal.addEventListener('mdsModalShow', () => {
    input.setFocus();
  });
  modal.addEventListener('mdsModalHide', () => {
    // safe point to detach or reset content
    modal.opened = undefined;
  });
</script>
```

#### Custom Animation Styles

`animation` selects the motion style. `'slide'` (default) translates from the entry edge; `'3d'` adds a perspective tilt; `'custom'` delegates entirely to the CSS custom properties `--mds-modal-custom-closed-transform` and `--mds-modal-custom-window-distance`.

```html
<!-- 3D perspective tilt animation -->
<mds-modal id="modal-3d" animation="3d" position="center">
  <div class="p-400">
    <mds-text>Animazione 3D</mds-text>
  </div>
</mds-modal>

<!-- Custom animation driven by CSS vars -->
<mds-modal id="modal-custom" animation="custom" position="center">
  <div class="p-400">
    <mds-text>Animazione personalizzata</mds-text>
  </div>
</mds-modal>
```

#### The Window of a Dialog via the `window` Slot

Slot an element with `slot="window"` to replace the built-in chrome (the window surface and the close button). A dialog slots an [`mds-banner`](../mds-banner/AGENTS.md) `variant="light"` `tone="box"` `dismissible`: its own close button closes the modal. The slotted element gets no `role` of its own: the `<dialog>` the component renders is already the dialog, named by the `aria-label` of the host. The actions follow the rules of a dialog: the secondary one `dark` `outline`, the main one last.

The window stops at the width of a dialog, the column of a form plus its inset (`--magma-width-dialog`, 580px at 16px, `widths/dialog` of the design language), and shrinks with a narrower screen. Write no width on it: a form keeps in the window the shape it has in the page.

The headline of the banner is the title of the dialog, in `h5` (`titles/dialog`): the banner takes the style from its place, so write no `typography` on it. It is hidden from screen readers, so the `aria-label` of the host carries the same text.

```html
<mds-modal id="modal-banner" aria-label="Azione richiesta">
  <mds-banner slot="window" variant="light" tone="box" dismissible headline="Azione richiesta">
    <mds-text typography="detail">Conferma la tua identita' per procedere.</mds-text>
    <mds-button slot="action" variant="dark" tone="outline">Annulla</mds-button>
    <mds-button slot="action" variant="primary" tone="strong">Conferma</mds-button>
  </mds-banner>
</mds-modal>
```

A bespoke surface, such as a banner as wide as the screen, writes its own width with a class: from the light DOM it wins over the width of a dialog.

```html
<mds-modal id="modal-novita" aria-label="Novita'">
  <mds-banner slot="window" class="max-w-none" variant="light" tone="box" dismissible headline="Novita'">
    <mds-text typography="detail">Da oggi puoi firmare i documenti dall'app.</mds-text>
  </mds-banner>
</mds-modal>
```

#### Disabling the Backdrop

Add `hide-backdrop` to render the modal without the dimmed overlay - useful for non-blocking side panels that coexist with interactive page content.

```html
<mds-modal id="pannello-info" position="right" hide-backdrop>
  <div class="p-400">
    <mds-text>Informazioni contestuali</mds-text>
  </div>
</mds-modal>
```

```javascript
// Remove the backdrop from JavaScript (it applies from the next opening)
document.getElementById('pannello-info').hideBackdrop = true;
```

#### CSS Customization

Size and place the window only through the public `--mds-modal-*` CSS custom properties. Set them on the host element; the window takes its background, radius and shadow from the component. They size the built-in window: a slotted window takes the width of a dialog, or the one its class gives it.

```css
/* Narrow center modal */
#modal-card {
  --mds-modal-window-distance: calc(var(--spacing) * 600);
  --mds-modal-window-overflow: hidden;
  --mds-modal-window-max-width: 480px;
}

/* Full-width drawer */
#drawer-bottom {
  --mds-modal-window-max-width: 100%;
  --mds-modal-window-min-width: 100%;
}
```
