# MenuOrDrawerOptions

Action menu: a list of options in a `MenuOrDrawer`, so a menu anchored to `activator` on wide screens and a bottom sheet on screens up to 1024px wide. Options are `MenuOrDrawerOption` objects, `{ name, label, icon?, description?, shortcut?, variant?, checked?, divider?, disabled?, class?, data?, onclick? }`: `variant: 'danger'` paints a destructive action in the error color, `checked` adds a check mark (the option becomes a `menuitemcheckbox`), `divider` draws a line above the option, `shortcut` shows a key hint on the right. Choosing an option closes the menu, gives the focus back to the activator and calls the option's `onclick` and then `onselect`. It follows the ARIA menu pattern: the first option takes the focus on open, the arrows, Home and End move between the options, a letter jumps to the next option that starts with it, Enter and Space choose, Escape closes. Disabled options stay reachable with the keyboard, with `aria-disabled`. In the menu it is at least `--menu-or-drawer-options-min-width` wide; in the sheet the options are taller, for touch.

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

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

## Examples

### Action menu

A "⋯" button opens the actions: a menu on wide screens, a bottom sheet up to 1024px. `variant: 'danger'` paints the destructive action, `divider` separates it, `disabled` turns an option off. Choosing an option closes the menu and calls `onselect`.

```svelte
<script lang="ts">
	import {
		mdiArchiveOutline,
		mdiContentCopy,
		mdiDotsHorizontal,
		mdiPencilOutline,
		mdiTrashCanOutline
	} from '@mdi/js';
	import { Button, MenuOrDrawerOptions, type MenuOrDrawerOption } from '@likable-hair/svelte';

	let open = $state(false);
	let activator = $state<HTMLButtonElement | HTMLAnchorElement>();
	let last = $state('none');

	const options: MenuOrDrawerOption[] = [
		{ name: 'edit', label: 'Edit', icon: mdiPencilOutline },
		{ name: 'duplicate', label: 'Duplicate', icon: mdiContentCopy },
		{ name: 'archive', label: 'Archive', icon: mdiArchiveOutline, disabled: true },
		{ name: 'delete', label: 'Delete', icon: mdiTrashCanOutline, variant: 'danger', divider: true }
	];
</script>

<Button
	buttonType="icon"
	variant="secondary"
	icon={mdiDotsHorizontal}
	aria-label="Invoice actions"
	aria-haspopup="menu"
	aria-expanded={open}
	bind:buttonElement={activator}
	onclick={() => (open = !open)}
/>
<span class="last">Last action: {last}</span>

<MenuOrDrawerOptions
	bind:open
	{activator}
	placement="bottom-start"
	aria-label="Invoice actions"
	{options}
	onselect={({ option }) => (last = option.label)}
/>

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

### Descriptions, shortcuts and check marks

`description` adds a second line, `shortcut` a key hint on the right (the shortcut itself is up to the app), `checked` a check mark that turns the option into a `menuitemcheckbox`. Each option can run its own `onclick`.

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

	let open = $state(false);
	let activator = $state<HTMLButtonElement | HTMLAnchorElement>();
	let favorite = $state(true);
	let log = $state('—');

	let options: MenuOrDrawerOption[] = $derived([
		{
			name: 'share',
			label: 'Share',
			icon: mdiShareVariantOutline,
			description: 'Send a link to the client',
			shortcut: '⌘S',
			onclick: () => (log = 'Link copied')
		},
		{
			name: 'favorite',
			label: 'Favorite',
			icon: mdiStarOutline,
			checked: favorite,
			onclick: () => (favorite = !favorite)
		},
		{
			name: 'print',
			label: 'Print',
			icon: mdiPrinterOutline,
			shortcut: '⌘P',
			onclick: () => (log = 'Printing…')
		}
	]);
</script>

<Button variant="secondary" bind:buttonElement={activator} onclick={() => (open = !open)}>
	Appointment
</Button>
<span class="log">{log} · favorite: {favorite ? 'yes' : 'no'}</span>

<MenuOrDrawerOptions bind:open {activator} placement="bottom-start" {options} />

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

### One menu for many rows

A single menu serves every row: the clicked button becomes the `activator` and the row goes in `data`, so the options are built once and `onselect` knows which row they apply to.

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

	type Client = { id: number; name: string };

	let clients = $state<Client[]>([
		{ id: 1, name: 'Luca Rossi' },
		{ id: 2, name: 'Sara Bianchi' },
		{ id: 3, name: 'Marco Ferri' }
	]);
	let open = $state(false);
	let activator = $state<HTMLElement>();
	let current = $state<Client>();
	let viewed = $state('—');

	let options: MenuOrDrawerOption<Client>[] = $derived([
		{ name: 'view', label: 'View', icon: mdiEyeOutline, data: current },
		{ name: 'delete', label: 'Delete', icon: mdiTrashCanOutline, variant: 'danger', data: current }
	]);

	function toggle(event: MouseEvent, client: Client) {
		const button = event.currentTarget as HTMLElement;
		open = !(open && activator === button);
		activator = button;
		current = client;
	}

	function choose({ option }: { option: MenuOrDrawerOption<Client> }) {
		const client = option.data!;
		if (option.name === 'view') viewed = client.name;
		else clients = clients.filter((item) => item.id !== client.id);
	}
</script>

<ul class="rows">
	{#each clients as client (client.id)}
		<li>
			<span>{client.name}</span>
			<Button
				buttonType="icon"
				variant="secondary"
				size="sm"
				icon={mdiDotsVertical}
				aria-label="Actions for {client.name}"
				aria-haspopup="menu"
				aria-expanded={open && current === client}
				onclick={(event) => toggle(event, client)}
			/>
		</li>
	{/each}
</ul>
<span class="viewed">Viewed: {viewed}</span>

<MenuOrDrawerOptions
	bind:open
	{activator}
	placement="bottom-end"
	aria-label="Actions for {current?.name}"
	{options}
	onselect={choose}
/>

<style>
	.rows {
		display: grid;
		gap: 4px;
		width: min(100%, 320px);
		margin: 0;
		padding: 0;
		list-style: none;
	}

	li {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 6px 6px 6px 12px;
		border: 1px solid var(--global-color-border);
		border-radius: var(--global-radius-md);
		font-size: 14px;
	}

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

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `options` | `MenuOrDrawerOption<Data>[]` | `[]` | Options of the menu, in order. |
| `aria-label` | `string \| null` |  | Accessible name of the menu, and of the sheet. Defaults to "Actions", in the language of `locale`, unless `aria-labelledby` names it. |
| `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`. |

### From MenuOrDrawer

It accepts the props, snippets and events of MenuOrDrawer (`/components/menu-or-drawer.md`), listed in the tables "From MenuOrDrawer". Except `children`.

| 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. |

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

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `optionSnippet` | `{ option: MenuOrDrawerOption<Data>; isDrawer: boolean }` | Replaces the content of each option (icon, texts, shortcut, check mark). |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onselect` | `(event: { option: MenuOrDrawerOption<Data>; event: MouseEvent }) => void` | Called when an option is chosen, after the menu closed and after the option's `onclick`. |

## Types

Exported by the package: `import type { MenuOrDrawerOption } from '@likable-hair/svelte';`

### MenuOrDrawerOption

`type MenuOrDrawerOption<Data = any> = { … }`

| Name | Type | Description |
| --- | --- | --- |
| `name` (required) | `string \| number` | Unique value among the options of the menu. |
| `label` (required) | `string` | Visible text. |
| `icon` | `string` | SVG path of the icon before the text, for example from `@mdi/js`. |
| `description` | `string` | Second line of smaller text below the label. |
| `shortcut` | `string` | Key hint shown on the right, such as `⌘D`. It is only a hint: the shortcut itself is up to the app. |
| `variant` | `'default' \| 'danger'` | `danger` for destructive actions, in the error color. |
| `checked` | `boolean` | Shows a check mark when `true`. Setting it, `true` or `false`, makes the option a `menuitemcheckbox`. |
| `divider` | `boolean` | Draws a line above the option, to separate a group. |
| `disabled` | `boolean` | The option cannot be chosen. It stays reachable with the keyboard. |
| `class` | `string` | Extra classes for the option. |
| `data` | `Data` | Any data for the app, for example the row the menu belongs to. |
| `onclick` | `(event: MouseEvent) => void` | Called when the option is chosen, before `onselect`. |

## CSS variables

For a single instance, pass them as style props: `<MenuOrDrawerOptions --menu-or-drawer-options-item-color="tomato">`. The `--menu-or-drawer-options-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

It also takes the variables of MenuOrDrawer (`/components/menu-or-drawer.md`).

| Per-instance variable | Default |
| --- | --- |
| `--menu-or-drawer-options-gap` | `--menu-or-drawer-options-default-gap`: `1px` |
| `--menu-or-drawer-options-min-width` | `--menu-or-drawer-options-default-min-width`: `180px` |
| `--menu-or-drawer-options-icon-size` | `--menu-or-drawer-options-default-icon-size`: `17px`<br>`--menu-or-drawer-options-default-drawer-icon-size`: `20px` |
| `--menu-or-drawer-options-item-color` | `--menu-or-drawer-options-default-item-color`: `var(--global-color-text-2)` |
| `--menu-or-drawer-options-item-hover-background` | `--menu-or-drawer-options-default-item-hover-background`: `var(--global-color-surface-2)` |
| `--menu-or-drawer-options-item-gap` | `--menu-or-drawer-options-default-item-gap`: `8px` |
| `--menu-or-drawer-options-item-padding` | `--menu-or-drawer-options-default-item-padding`: `7px 10px`<br>`--menu-or-drawer-options-default-drawer-item-padding`: `12px 12px` |
| `--menu-or-drawer-options-item-border-radius` | `--menu-or-drawer-options-default-item-border-radius`: `var(--global-radius-sm)` |
| `--menu-or-drawer-options-item-font-size` | `--menu-or-drawer-options-default-item-font-size`: `13px`<br>`--menu-or-drawer-options-default-drawer-item-font-size`: `15px` |
| `--menu-or-drawer-options-checked-color` | `--menu-or-drawer-options-default-checked-color`: `var(--global-color-text)` |
| `--menu-or-drawer-options-checked-background` | `--menu-or-drawer-options-default-checked-background`: `var(--global-color-primary-soft)` |
| `--menu-or-drawer-options-danger-color` | `--menu-or-drawer-options-default-danger-color`: `var(--global-color-error)` |
| `--menu-or-drawer-options-danger-hover-background` | `--menu-or-drawer-options-default-danger-hover-background`: `var(--global-color-error-soft)` |
| `--menu-or-drawer-options-disabled-opacity` | `--menu-or-drawer-options-default-disabled-opacity`: `0.4` |
| `--menu-or-drawer-options-item-hover-color` | `--menu-or-drawer-options-default-item-hover-color`: `var(--global-color-text)` |
| `--menu-or-drawer-options-description-color` | `--menu-or-drawer-options-default-description-color`: `var(--global-color-text-3)` |
| `--menu-or-drawer-options-description-font-size` | `--menu-or-drawer-options-default-description-font-size`: `11.5px` |
| `--menu-or-drawer-options-shortcut-color` | `--menu-or-drawer-options-default-shortcut-color`: `var(--global-color-text-3)` |
| `--menu-or-drawer-options-shortcut-font-family` | `--menu-or-drawer-options-default-shortcut-font-family`: `var(--global-font-family-mono)` |
| `--menu-or-drawer-options-shortcut-font-size` | `--menu-or-drawer-options-default-shortcut-font-size`: `11px` |
| `--menu-or-drawer-options-check-color` | `--menu-or-drawer-options-default-check-color`: `var(--global-color-primary)` |
| `--menu-or-drawer-options-divider-margin` | `--menu-or-drawer-options-default-divider-margin`: `4px 6px` |
| `--menu-or-drawer-options-divider-color` | `--menu-or-drawer-options-default-divider-color`: `var(--global-color-border)` |
