# MenuOrDrawer

Shows its content in a `Menu` anchored to `activator` on wide screens and in a bottom `Drawer` on screens up to 1024px wide (`breakpoints.mAndDown`, the breakpoint of the whole library); `mode` forces one of the two. The bottom drawer is as tall as its content, up to 85% of the screen (`--drawer-size` fixes the height). `children` receives `isDrawer` and `isMenu`, to adapt spacing or content, and `close`. Both close on Escape and on a click outside; Escape and `close` give the focus back to the activator in the menu, the drawer gives it back to the element that had it. The other props of the two go in `menuProps` and `drawerProps`, and their CSS variables (`--menu-*`, `--drawer-*`) can be set on this component. The menu element has the class `aurora-menu-or-drawer-menu`, the drawer `aurora-menu-or-drawer-drawer`.

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

Category: composed. Source: `src/lib/components/composed/common/MenuOrDrawer.svelte`.

## Examples

### Account menu

On a wide screen the content opens in a menu below the button; up to 1024px wide it opens in a bottom sheet as tall as the content. `isDrawer` adapts the layout, `close` closes from inside. Narrow the window to see the sheet.

```svelte
<script lang="ts">
	import { mdiAccountOutline, mdiCogOutline, mdiLogout } from '@mdi/js';
	import { Avatar, Button, MenuOrDrawer } from '@likable-hair/svelte';

	let open = $state(false);
	let activator = $state<HTMLButtonElement | HTMLAnchorElement>();
	let signedOut = $state(false);
</script>

<Button
	buttonType="text"
	variant="secondary"
	aria-haspopup="true"
	aria-expanded={open}
	bind:buttonElement={activator}
	onclick={() => (open = !open)}
>
	<Avatar name="Giulia Marini" size="sm" />
	Giulia Marini
</Button>
{#if signedOut}<span class="note">Signed out</span>{/if}

<MenuOrDrawer bind:open {activator} placement="bottom-start" drawerProps={{ 'aria-label': 'Account' }}>
	{#snippet children({ isDrawer, close })}
		<div class="account" class:sheet={isDrawer}>
			<div class="who">
				<Avatar name="Giulia Marini" size={isDrawer ? 'lg' : 'md'} />
				<div>
					<strong>Giulia Marini</strong>
					<span>giulia@example.com</span>
				</div>
			</div>
			<Button buttonType="text" variant="secondary" icon={mdiAccountOutline} href="#profile">Profile</Button>
			<Button buttonType="text" variant="secondary" icon={mdiCogOutline} href="#settings">Settings</Button>
			<Button
				buttonType="text"
				variant="danger"
				icon={mdiLogout}
				onclick={() => {
					signedOut = true;
					close();
				}}
			>
				Sign out
			</Button>
		</div>
	{/snippet}
</MenuOrDrawer>

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

	.account {
		display: flex;
		flex-direction: column;
		align-items: stretch;
		gap: 2px;
		width: 240px;
		padding: 4px;
	}

	.account.sheet {
		width: auto;
		padding: 0;
	}

	.account :global(.aurora-button) {
		justify-content: flex-start;
	}

	.who {
		display: flex;
		align-items: center;
		gap: 10px;
		margin-bottom: 6px;
		padding: 6px 8px 10px;
		border-bottom: 1px solid var(--global-color-border);
	}

	.who div {
		display: grid;
	}

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

### Forcing the menu or the sheet

`mode="menu"` and `mode="drawer"` open always the same one, whatever the screen. The sheet can have a title and a close button through `drawerProps`; `--drawer-size` fixes its height.

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

	let menuOpen = $state(false);
	let drawerOpen = $state(false);
	let menuButton = $state<HTMLButtonElement | HTMLAnchorElement>();
</script>

<div class="demo">
	<Button variant="secondary" bind:buttonElement={menuButton} onclick={() => (menuOpen = !menuOpen)}>
		Always a menu
	</Button>
	<Button variant="secondary" onclick={() => (drawerOpen = true)}>Always a sheet</Button>
</div>

<MenuOrDrawer bind:open={menuOpen} activator={menuButton} mode="menu" menuProps={{ 'aria-label': 'Info' }}>
	<p class="text">A menu even on a phone, for content that must stay next to its button.</p>
</MenuOrDrawer>

<MenuOrDrawer
	bind:open={drawerOpen}
	mode="drawer"
	drawerProps={{ title: 'Delivery', closable: true }}
	--drawer-size="min(320px, 85dvh)"
>
	<p class="text">A bottom sheet even on a desktop, with a fixed height.</p>
</MenuOrDrawer>

<style>
	.demo {
		display: flex;
		flex-wrap: wrap;
		gap: 12px;
	}

	.text {
		max-width: 260px;
		margin: 0;
		padding: 8px 10px;
		font-size: 13px;
	}
</style>
```

### Inside a drawer

A MenuOrDrawer opened from inside a Drawer, as in the mobile drawer of SidebarLayout. On a wide screen the menu opens next to its button, above the drawer; up to 1024px wide the bottom sheet covers the whole width at the bottom of the screen, as tall as its content, with its own padding: it does not take the size of the drawer around it. Narrow the window to see the sheet.

```svelte
<script lang="ts">
	import { mdiAccountOutline, mdiCogOutline, mdiLogout, mdiMenu } from '@mdi/js';
	import { Avatar, Button, Drawer, MenuOrDrawer } from '@likable-hair/svelte';

	let drawerOpen = $state(false);
	let accountOpen = $state(false);
	let activator = $state<HTMLButtonElement | HTMLAnchorElement>();
</script>

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

<Drawer bind:open={drawerOpen} title="Menu" closable>
	<div class="body">
		<p class="text">The account button opens a MenuOrDrawer from inside this drawer.</p>
		<Button
			buttonType="text"
			variant="secondary"
			aria-haspopup="true"
			aria-expanded={accountOpen}
			bind:buttonElement={activator}
			onclick={() => (accountOpen = !accountOpen)}
		>
			<Avatar name="Giulia Marini" size="sm" />
			Giulia Marini
		</Button>
	</div>

	<MenuOrDrawer
		bind:open={accountOpen}
		{activator}
		placement="top-start"
		drawerProps={{ title: 'Account' }}
		menuProps={{ 'aria-label': 'Account' }}
	>
		{#snippet children({ close })}
			<div class="account">
				<Button buttonType="text" variant="secondary" icon={mdiAccountOutline} onclick={close}>Profile</Button>
				<Button buttonType="text" variant="secondary" icon={mdiCogOutline} onclick={close}>Settings</Button>
				<Button buttonType="text" variant="danger" icon={mdiLogout} onclick={close}>Sign out</Button>
			</div>
		{/snippet}
	</MenuOrDrawer>
</Drawer>

<style>
	.body {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		gap: 12px;
	}

	.text {
		margin: 0;
		color: var(--global-color-text-2);
		font-size: 13px;
	}

	.account {
		display: flex;
		flex-direction: column;
		align-items: stretch;
		gap: 2px;
		min-width: 200px;
	}

	.account :global(.aurora-button) {
		justify-content: flex-start;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `open` (bindable) | `boolean` |  | Whether the menu or the drawer is open. `undefined` counts as closed. |
| `activator` | `HTMLElement \| VirtualElement` |  | What the menu is anchored to: usually the button that opens it, or any object with `getBoundingClientRect()`. The drawer ignores it. |
| `placement` | `'bottom-start' \| 'bottom' \| 'bottom-end' \| 'top-start' \| 'top' \| 'top-end' \| 'left-start' \| 'left' \| 'left-end' \| 'right-start' \| 'right' \| 'right-end'` | `'bottom'` | Side of the activator and alignment of the menu. Flips to the opposite side when there is not enough space. |
| `mode` | `'auto' \| 'menu' \| 'drawer'` | `'auto'` | `auto` opens a drawer on screens up to 1024px wide and a menu on wider ones; `menu` and `drawer` always open that one. |
| `menuProps` | `Omit< ComponentProps<typeof Menu>, 'open' \| 'activator' \| 'placement' \| 'menuElement' \| 'children' >` | `{}` | Other props of the `Menu`, such as `offset`, `matchActivatorWidth`, `class` or `aria-label`. |
| `drawerProps` | `Omit<ComponentProps<typeof Drawer>, 'open' \| 'drawerElement' \| 'children'>` | `{}` | Other props of the `Drawer`, such as `title`, `closable`, `position` (default `bottom`) or `aria-label`. |
| `menuElement` (bindable) | `HTMLDivElement` |  | The menu element, while the menu is open. |
| `drawerElement` (bindable) | `HTMLDialogElement` |  | The `<dialog>` element of the drawer, while the drawer is open. |

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `children` | `{ isDrawer: boolean; isMenu: boolean; close: () => void }` | Content of the menu or of the drawer. `close` closes it. |
