# Dialog

Modal dialog on the browser's top layer (`<dialog>` with `showModal()`): it needs no z-index, the page behind is inert and does not scroll, focus stays inside and goes back where it was on close. It closes on Escape (only the topmost one, and not while a menu inside it is open), on a click on the backdrop, with the close button, with `close` from a snippet and with a `<form method="dialog">`; `persistent` blocks Escape and the backdrop. The content is mounted only while open. Every part is optional: `title` (which also names the dialog for screen readers), `closable`, the body (`children`) and `actionsSnippet`; `topRightSnippet`, `centerLeftSnippet` and `centerRightSnippet` sit on the backdrop outside the surface, for example the arrows of a lightbox. While closing, the `<dialog>` gets `data-closing`.

```ts
import { Dialog } from '@likable-hair/svelte';
```

Category: simple. Source: `src/lib/components/simple/dialogs/Dialog.svelte`.

## Examples

### Confirmation

`title` names the dialog, `closable` adds the close button and `actionsSnippet` holds the buttons; `close` from the snippet closes it and calls `onclose`. Escape and a click on the backdrop close it too, and focus goes back to the button that opened it.

```svelte
<script lang="ts">
	import { mdiTrashCanOutline } from '@mdi/js';
	import { Button, Dialog } from '@likable-hair/svelte';

	let open = $state(false);
	let result = $state('none');
</script>

<Button variant="danger" icon={mdiTrashCanOutline} onclick={() => (open = true)}>Delete client</Button>
<span class="result">Result: {result}</span>

<Dialog bind:open title="Delete client?" closable onclose={() => (result = 'dismissed')}>
	Giulia Marini and her 48 appointments will be removed for good.
	{#snippet actionsSnippet({ close })}
		<Button variant="secondary" onclick={close}>Cancel</Button>
		<Button
			variant="danger"
			onclick={() => {
				result = 'deleted';
				open = false;
			}}
		>
			Delete
		</Button>
	{/snippet}
</Dialog>

<style>
	.result {
		color: var(--global-color-text-3);
		font-size: 13px;
	}
</style>
```

### Form

A `<form method="dialog">` closes the dialog when it is submitted, after its own `onsubmit` and only if the fields are valid. The submit button sits in the footer and points to the form with the `form` attribute. `autofocus` picks the field that gets focus on open.

```svelte
<script lang="ts">
	import { mdiPlus } from '@mdi/js';
	import { Button, Dialog, SimpleTextField } from '@likable-hair/svelte';

	let open = $state(false);
	let name = $state('');
	let duration = $state('60');
	let created = $state<string[]>([]);
</script>

<Button icon={mdiPlus} onclick={() => (open = true)}>New service</Button>
<span class="result">Created: {created.join(', ') || 'none'}</span>

<Dialog bind:open title="New service" closable --dialog-max-width="400px">
	<form
		id="service-form"
		method="dialog"
		onsubmit={() => {
			created.push(name);
			name = '';
		}}
	>
		<!-- svelte-ignore a11y_autofocus -->
		<SimpleTextField label="Name" bind:value={name} required autofocus />
		<SimpleTextField label="Duration (min)" type="number" bind:value={duration} />
	</form>
	{#snippet actionsSnippet({ close })}
		<Button buttonType="text" variant="secondary" onclick={close}>Cancel</Button>
		<Button type="submit" form="service-form">Create service</Button>
	{/snippet}
</Dialog>

<style>
	.result {
		color: var(--global-color-text-3);
		font-size: 13px;
	}

	form {
		display: grid;
		gap: 12px;
	}
</style>
```

### Persistent and nested

With `persistent` Escape and the backdrop do nothing: only the buttons close it. A dialog opened from another one stacks on top, and Escape closes only the topmost.

```svelte
<script lang="ts">
	import { Button, Dialog } from '@likable-hair/svelte';

	let terms = $state(false);
	let details = $state(false);
</script>

<Button variant="secondary" onclick={() => (terms = true)}>Open terms</Button>

<Dialog bind:open={terms} persistent title="Accept the new terms">
	You need to accept the terms of service to keep using the booking calendar.
	<Button buttonType="text" onclick={() => (details = true)}>Read the details</Button>
	{#snippet actionsSnippet({ close })}
		<Button variant="secondary" onclick={close}>Not now</Button>
		<Button onclick={close}>Accept</Button>
	{/snippet}
</Dialog>

<Dialog bind:open={details} title="Terms of service" closable --dialog-max-width="520px">
	The salon keeps your appointments and contact details for as long as your account is active.
</Dialog>
```

### Lightbox

`topRightSnippet`, `centerLeftSnippet` and `centerRightSnippet` sit on the backdrop, outside the surface. Here the surface becomes transparent and wider through CSS variables, and the transition variables turn the default rise into a zoom.

```svelte
<script lang="ts">
	import { mdiChevronLeft, mdiChevronRight, mdiClose, mdiImageMultipleOutline } from '@mdi/js';
	import { Button, Dialog } from '@likable-hair/svelte';

	const photos = ['Balayage', 'Copper bob', 'Silver blonde'];
	let open = $state(false);
	let index = $state(0);
</script>

<Button variant="secondary" icon={mdiImageMultipleOutline} onclick={() => (open = true)}>Open gallery</Button>

<Dialog
	bind:open
	aria-label="Gallery"
	--dialog-max-width="720px"
	--dialog-padding="0"
	--dialog-background="transparent"
	--dialog-border-width="0"
	--dialog-box-shadow="none"
	--dialog-transition-y="0px"
	--dialog-transition-scale="0.7"
>
	<figure class="photo" data-index={index}>
		<figcaption>{photos[index]} · {index + 1} of {photos.length}</figcaption>
	</figure>
	{#snippet topRightSnippet({ close })}
		<Button buttonType="icon" variant="secondary" icon={mdiClose} aria-label="Close" onclick={close} />
	{/snippet}
	{#snippet centerLeftSnippet()}
		<Button
			buttonType="icon"
			variant="secondary"
			icon={mdiChevronLeft}
			aria-label="Previous photo"
			disabled={index === 0}
			onclick={() => index--}
		/>
	{/snippet}
	{#snippet centerRightSnippet()}
		<Button
			buttonType="icon"
			variant="secondary"
			icon={mdiChevronRight}
			aria-label="Next photo"
			disabled={index === photos.length - 1}
			onclick={() => index++}
		/>
	{/snippet}
</Dialog>

<style>
	.photo {
		display: grid;
		align-items: end;
		margin: 0;
		aspect-ratio: 4 / 3;
		border-radius: var(--global-radius-xl);
		background: linear-gradient(135deg, var(--global-color-data-1), var(--global-color-data-3));
	}

	.photo[data-index='1'] {
		background: linear-gradient(135deg, var(--global-color-data-5), var(--global-color-data-6));
	}

	.photo[data-index='2'] {
		background: linear-gradient(135deg, var(--global-color-data-2), var(--global-color-data-4));
	}

	figcaption {
		padding: 14px 18px;
		color: #fff;
		font-weight: 600;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `open` (bindable) | `boolean` |  | Whether the dialog is open. `undefined` counts as closed. |
| `persistent` | `boolean` | `false` | Keeps the dialog open on Escape and on a click on the backdrop. The close button, `close` from a snippet and a `<form method="dialog">` still close it. |
| `title` | `string` |  | Title in the header. It also names the dialog for screen readers: without it, pass `aria-label`. |
| `closable` | `boolean` | `false` | Shows a close button in the header. |
| `closeLabel` | `string` |  | Accessible name of the close button. Defaults to "Close", in the language of `locale`. |
| `locale` | `string` |  | BCP 47 locale of the texts: `it` and `en` are built in, other languages fall back to English. Defaults to the one set with `setDefaultLocale`, else `en`. |
| `dialogElement` (bindable) | `HTMLDialogElement` |  | The `<dialog>` element. |
| `class` | `{ dialog?: string; backdrop?: string; surface?: string; header?: string; title?: string; body?: string; actions?: string; }` | `{}` | Extra classes for each part. |

It also accepts the native attributes and events of `<dialog>` (for example `id`, `title`, `aria-label`, `onkeydown`), which are forwarded to the element.

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `children` | `{ close: Close }` | Body of the dialog. It scrolls when the dialog is taller than the viewport. |
| `titleSnippet` | `{ title: string \| undefined }` | Replaces the title text, inside the heading. |
| `closeSnippet` | `{ close: Close; closeLabel: string }` | Replaces the close button. Shown even without `closable`. |
| `actionsSnippet` | `{ close: Close }` | Footer, usually the buttons, aligned to the right. |
| `topRightSnippet` | `{ close: Close }` | Content in the top right corner of the screen, on the backdrop. |
| `centerLeftSnippet` | `{ close: Close }` | Content on the left of the surface, centered vertically, on the backdrop. |
| `centerRightSnippet` | `{ close: Close }` | Content on the right of the surface, centered vertically, on the backdrop. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onclose` | `(event: Event) => void` | Called when the dialog closes itself (Escape, backdrop, close button, `close` from a snippet, `<form method="dialog">`), with the event that caused it. Not called when `open` is set to `false` from outside. |

## CSS variables

For a single instance, pass them as style props: `<Dialog --dialog-background="tomato">`. The `--dialog-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--dialog-inset` | `--dialog-default-inset`: `16px` |
| `--dialog-duration` | `--dialog-default-duration`: `var(--global-duration-slow)` |
| `--dialog-easing` | `--dialog-default-easing`: `var(--global-ease-spring)` |
| `--dialog-backdrop-background` | `--dialog-default-backdrop-background`: `var(--global-color-overlay)` |
| `--dialog-backdrop-filter` | `--dialog-default-backdrop-filter`: `blur(var(--global-overlay-blur))` |
| `--dialog-transition-x` | `--dialog-default-transition-x`: `0px` |
| `--dialog-transition-y` | `--dialog-default-transition-y`: `16px` |
| `--dialog-transition-scale` | `--dialog-default-transition-scale`: `0.96` |
| `--dialog-width` | `--dialog-default-width`: `100%` |
| `--dialog-max-width` | `--dialog-default-max-width`: `440px` |
| `--dialog-height` | `--dialog-default-height`: `auto` |
| `--dialog-max-height` | `--dialog-default-max-height`: `100%` |
| `--dialog-padding` | `--dialog-default-padding`: `20px` |
| `--dialog-background` | `--dialog-default-background`: `var(--global-color-surface-pop)` |
| `--dialog-color` | `--dialog-default-color`: `var(--global-color-text)` |
| `--dialog-border-width` | `--dialog-default-border-width`: `var(--global-border-width)` |
| `--dialog-border-color` | `--dialog-default-border-color`: `var(--global-color-border-strong)` |
| `--dialog-border-radius` | `--dialog-default-border-radius`: `var(--global-radius-xl)` |
| `--dialog-box-shadow` | `--dialog-default-box-shadow`: `var(--global-shadow-lg), 0 0 80px -30px var(--global-color-primary-glow)` |
| `--dialog-header-gap` | `--dialog-default-header-gap`: `12px` |
| `--dialog-header-padding` | `--dialog-default-header-padding`: `0` |
| `--dialog-header-margin-bottom` | `--dialog-default-header-margin-bottom`: `6px` |
| `--dialog-title-color` | `--dialog-default-title-color`: `var(--global-color-text)` |
| `--dialog-title-font-family` | `--dialog-default-title-font-family`: `var(--global-font-family-display)` |
| `--dialog-title-font-size` | `--dialog-default-title-font-size`: `21px` |
| `--dialog-title-font-weight` | `--dialog-default-title-font-weight`: `700` |
| `--dialog-title-line-height` | `--dialog-default-title-line-height`: `1.25` |
| `--dialog-title-letter-spacing` | `--dialog-default-title-letter-spacing`: `-0.02em` |
| `--dialog-body-color` | `--dialog-default-body-color`: `var(--global-color-text-2)` |
| `--dialog-body-font-size` | `--dialog-default-body-font-size`: `14px` |
| `--dialog-body-line-height` | `--dialog-default-body-line-height`: `1.5` |
| `--dialog-actions-gap` | `--dialog-default-actions-gap`: `10px` |
| `--dialog-actions-justify-content` | `--dialog-default-actions-justify-content`: `flex-end` |
| `--dialog-actions-padding` | `--dialog-default-actions-padding`: `0` |
| `--dialog-actions-margin-top` | `--dialog-default-actions-margin-top`: `22px` |
