# Drawer

Panel that slides in from a side of the screen (`position`), modal and 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 drawer for screen readers), `closable`, the body (`children`, which scrolls) and `actionsSnippet`, pinned at the bottom. A bottom drawer (a bottom sheet) has a handle at the top: dragging it, or the header, down closes the drawer, while a short drag springs back (`handle`). The side is exposed as `data-position`; while closing, the `<dialog>` gets `data-closing`, while dragged `data-dragging`, and with the handle `data-handle`.

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

Category: simple. Source: `src/lib/components/simple/navigation/Drawer.svelte`.

## Examples

### Navigation

A drawer from the left with a title, the close button and a navigation list in the body. Choosing an item closes it with `close` from the snippet.

```svelte
<script lang="ts">
	import { mdiAccountOutline, mdiCalendarOutline, mdiMenu, mdiViewDashboardOutline } from '@mdi/js';
	import { Button, Drawer, Icon } from '@likable-hair/svelte';

	const items = [
		{ label: 'Dashboard', icon: mdiViewDashboardOutline },
		{ label: 'Calendar', icon: mdiCalendarOutline },
		{ label: 'Clients', icon: mdiAccountOutline }
	];
	let open = $state(false);
	let current = $state('Dashboard');
</script>

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

<Drawer bind:open title="Menu" closable --drawer-size="260px">
	{#snippet children({ close })}
		<nav>
			{#each items as item (item.label)}
				<a
					href="#{item.label.toLowerCase()}"
					aria-current={item.label === current ? 'page' : undefined}
					onclick={(event) => {
						event.preventDefault();
						current = item.label;
						close(event);
					}}
				>
					<Icon path={item.icon} />{item.label}
				</a>
			{/each}
		</nav>
	{/snippet}
</Drawer>

<style>
	nav {
		display: grid;
		gap: 2px;
	}

	a {
		display: flex;
		align-items: center;
		gap: 10px;
		padding: 9px 10px;
		border-radius: var(--global-radius-md);
		color: var(--global-color-text-2);
		text-decoration: none;
		--icon-size: 18px;
	}

	a:hover {
		background: var(--global-color-surface-2);
		color: var(--global-color-text);
	}

	a[aria-current='page'] {
		background: var(--global-color-primary-soft);
		color: var(--global-color-primary);
		font-weight: 600;
	}
</style>
```

### Positions

`position` picks the side. Top and bottom drawers get rounded inner corners; `--drawer-size` is the width on the sides and the height on top and bottom.

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

	const positions = ['left', 'right', 'top', 'bottom'] as const;
	let open = $state(false);
	let position = $state<(typeof positions)[number]>('left');
</script>

<div class="row">
	{#each positions as side (side)}
		<Button
			variant="secondary"
			onclick={() => {
				position = side;
				open = true;
			}}
		>
			{side}
		</Button>
	{/each}
</div>

<Drawer bind:open {position} title="Drawer on the {position}" closable>
	Escape or a click on the backdrop closes it.
</Drawer>

<style>
	.row {
		display: flex;
		flex-wrap: wrap;
		gap: 8px;
	}
</style>
```

### Bottom sheet with actions

`actionsSnippet` stays pinned at the bottom while the body scrolls. `persistent` keeps the sheet open on Escape and on the backdrop, so the filters can only be applied or reset.

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

	const services = ['Cut', 'Color', 'Balayage', 'Perm', 'Treatment', 'Styling', 'Extensions', 'Bridal'];
	let open = $state(false);
	let selected = $state<string[]>([]);
	let applied = $state<string[]>([]);
</script>

<Button variant="secondary" icon={mdiFilterVariant} onclick={() => (open = true)}>Filters</Button>
<span class="result">Applied: {applied.join(', ') || 'none'}</span>

<Drawer
	bind:open
	position="bottom"
	persistent
	title="Filter by service"
	--drawer-size="min(420px, 85dvh)"
	--drawer-padding="18px 16px"
>
	{#each services as service (service)}
		<label>
			<input type="checkbox" value={service} bind:group={selected} />
			{service}
		</label>
	{/each}
	{#snippet actionsSnippet({ close })}
		<Button buttonType="text" variant="secondary" onclick={() => (selected = [])}>Reset</Button>
		<Button
			onclick={(event) => {
				applied = [...selected];
				close(event);
			}}
		>
			Apply
		</Button>
	{/snippet}
</Drawer>

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

	label {
		display: flex;
		align-items: center;
		gap: 10px;
		padding: 10px 6px;
		border-bottom: 1px solid var(--global-color-border);
	}
</style>
```

### Bottom sheet with a handle

A bottom drawer has a handle: dragging it, or the title, down closes the sheet, and a short drag springs back. Escape and the backdrop close it too; `handle={false}` removes it.

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

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

<Button variant="secondary" icon={mdiShareVariantOutline} onclick={() => (open = true)}>
	Share
</Button>

<Drawer bind:open position="bottom" title="Share the appointment" --drawer-size="auto">
	<p>Anyone with the link sees the date, the time and the services of the appointment.</p>
	{#snippet actionsSnippet({ close })}
		<Button buttonType="text" variant="secondary" onclick={close}>Cancel</Button>
		<Button onclick={close}>Copy link</Button>
	{/snippet}
</Drawer>

<style>
	p {
		margin: 0 6px;
		color: var(--global-color-text-2);
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `open` (bindable) | `boolean` |  | Whether the drawer is open. `undefined` counts as closed. |
| `position` | `'left' \| 'right' \| 'top' \| 'bottom'` | `'left'` | Side of the screen the drawer slides in from. |
| `persistent` | `boolean` | `false` | Keeps the drawer 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. |
| `handle` | `boolean` |  | Shows a handle at the top of a bottom drawer: dragging it, or the header (not its buttons), down past a quarter of the panel (at least 48px, at most 160px), or with a flick, closes the drawer; a shorter drag springs back. Defaults to `true` with `position="bottom"`, `false` when `persistent` (with both, the drawer only springs back). The other sides have no handle. |
| `title` | `string` |  | Title in the header. It also names the drawer 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`. |
| `drawerElement` (bindable) | `HTMLDialogElement` |  | The `<dialog>` element. |
| `class` | `{ drawer?: string; backdrop?: string; panel?: string; handle?: 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 drawer. It scrolls when it does not fit. |
| `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 pinned at the bottom of the panel, usually the buttons, aligned to the right. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onclose` | `(event: Event) => void` | Called when the drawer closes itself (Escape, backdrop, close button, drag on the handle, `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: `<Drawer --drawer-background="tomato">`. The `--drawer-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--drawer-duration` | `--drawer-default-duration`: `var(--global-duration-slow)` |
| `--drawer-easing` | `--drawer-default-easing`: `var(--global-ease)` |
| `--drawer-size` | `--drawer-default-size`: `20rem` |
| `--drawer-max-size` | `--drawer-default-max-size`: `100%` |
| `--drawer-border-width` | `--drawer-default-border-width`: `var(--global-border-width)` |
| `--drawer-border-color` | `--drawer-default-border-color`: `var(--global-color-border-strong)` |
| `--drawer-backdrop-background` | `--drawer-default-backdrop-background`: `var(--global-color-overlay)` |
| `--drawer-backdrop-filter` | `--drawer-default-backdrop-filter`: `blur(var(--global-overlay-blur))` |
| `--drawer-margin` | `--drawer-default-margin`: `0` |
| `--drawer-padding` | `--drawer-default-padding`: `18px 12px` |
| `--drawer-background` | `--drawer-default-background`: `var(--global-color-surface-pop)` |
| `--drawer-color` | `--drawer-default-color`: `var(--global-color-text)` |
| `--drawer-box-shadow` | `--drawer-default-box-shadow`: `var(--global-shadow-lg)` |
| `--drawer-border-radius` | `--drawer-default-left-border-radius`: `0`<br>`--drawer-default-right-border-radius`: `0`<br>`--drawer-default-top-border-radius`: `0 0 var(--global-radius-xl) var(--global-radius-xl)`<br>`--drawer-default-bottom-border-radius`: `var(--global-radius-xl) var(--global-radius-xl) 0 0` |
| `--drawer-handle-margin` | `--drawer-default-handle-margin`: `-18px 0 4px` |
| `--drawer-handle-padding` | `--drawer-default-handle-padding`: `8px 0` |
| `--drawer-handle-width` | `--drawer-default-handle-width`: `38px` |
| `--drawer-handle-height` | `--drawer-default-handle-height`: `5px` |
| `--drawer-handle-border-radius` | `--drawer-default-handle-border-radius`: `var(--global-radius-full)` |
| `--drawer-handle-background` | `--drawer-default-handle-background`: `var(--global-color-border-strong)` |
| `--drawer-header-gap` | `--drawer-default-header-gap`: `12px` |
| `--drawer-header-padding` | `--drawer-default-header-padding`: `0 6px` |
| `--drawer-header-margin-bottom` | `--drawer-default-header-margin-bottom`: `14px` |
| `--drawer-title-color` | `--drawer-default-title-color`: `var(--global-color-text)` |
| `--drawer-title-font-family` | `--drawer-default-title-font-family`: `var(--global-font-family-display)` |
| `--drawer-title-font-size` | `--drawer-default-title-font-size`: `16px` |
| `--drawer-title-font-weight` | `--drawer-default-title-font-weight`: `600` |
| `--drawer-title-line-height` | `--drawer-default-title-line-height`: `1.3` |
| `--drawer-title-letter-spacing` | `--drawer-default-title-letter-spacing`: `-0.01em` |
| `--drawer-overflow` | `--drawer-default-overflow`: `auto` |
| `--drawer-body-color` | `--drawer-default-body-color`: `var(--global-color-text)` |
| `--drawer-body-font-size` | `--drawer-default-body-font-size`: `14px` |
| `--drawer-body-line-height` | `--drawer-default-body-line-height`: `1.5` |
| `--drawer-actions-gap` | `--drawer-default-actions-gap`: `10px` |
| `--drawer-actions-justify-content` | `--drawer-default-actions-justify-content`: `flex-end` |
| `--drawer-actions-padding` | `--drawer-default-actions-padding`: `0 6px` |
| `--drawer-actions-margin-top` | `--drawer-default-actions-margin-top`: `14px` |
