# Dropdown

Button that opens a list of options (`items`) and picks one or, with `multiple`, more of them: for toolbars, sorting and filters. It is an `Autocomplete` without the search field, with a `Button` as trigger. The button shows `icon`, then `label` (also its accessible name, as in "Sort by Newest"), then the selection or the `placeholder`, and an arrow. With `clearable` (the default) a clear button replaces the arrow while something is selected. Options are `Item` objects, `{ value: string | number; label?: string | number; icon?: string; data?: Data }`, and the selection lives in `values`, always an array. It follows the ARIA select-only combobox pattern: Enter, Space and the arrows open the list and move through it, Enter or Space picks the highlighted option, typing a letter jumps to the first option that starts with it, Escape closes. With `mobileDrawer`, on screens up to 1024px wide the list opens in a bottom `Drawer`. The trigger takes the `--button-*` variables, the list the `--menu-*` and `--autocomplete-option-*` ones. Its state is exposed as `data-open`, `data-disabled` and `data-empty` for app CSS.

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

Category: composed. Source: `src/lib/components/composed/forms/Dropdown.svelte`.

## Examples

### Sorting

A single choice in a toolbar. `label` sits inside the button before the selection and names the control for screen readers. `mandatory` (the default) keeps the current option when it is clicked again, and `clearable={false}` hides the clear button, so there is always a sort order.

```svelte
<script lang="ts">
	import {
		mdiClockOutline,
		mdiCurrencyEur,
		mdiSort,
		mdiSortAlphabeticalAscending
	} from '@mdi/js';
	import { Dropdown, type Item } from '@likable-hair/svelte';

	const orders: Item[] = [
		{ value: 'recent', label: 'Newest', icon: mdiClockOutline },
		{ value: 'name', label: 'Name A–Z', icon: mdiSortAlphabeticalAscending },
		{ value: 'spent', label: 'Total spent', icon: mdiCurrencyEur }
	];

	let values: Item[] = $state([orders[0]]);
</script>

<div class="demo">
	<Dropdown label="Sort by" icon={mdiSort} items={orders} bind:values clearable={false} />
	<p>Sorted by: {values[0]?.value}</p>
</div>

<style>
	.demo {
		display: grid;
		justify-items: start;
		gap: 12px;
	}

	p {
		margin: 0;
		font-size: 13px;
	}
</style>
```

### Multiple selection

With `multiple` the list stays open while options are toggled. The button shows the only selected label or "N selected"; `selectionText` changes that text. The clear button empties the selection and `onchange` reports every change.

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

	const statuses: Item[] = [
		{ value: 'booked', label: 'Booked' },
		{ value: 'confirmed', label: 'Confirmed' },
		{ value: 'arrived', label: 'Arrived' },
		{ value: 'completed', label: 'Completed' },
		{ value: 'cancelled', label: 'Cancelled' },
		{ value: 'no-show', label: 'No-show' }
	];

	let values: Item[] = $state([]);
	let last = $state('');
</script>

<div class="demo">
	<div class="row">
		<Dropdown
			label="Status"
			placeholder="All"
			icon={mdiFilterVariant}
			items={statuses}
			multiple
			bind:values
			onchange={({ select, unselect, selection }) =>
				(last = `${select ? `+${select.label}` : `-${unselect?.label}`} → ${selection.length} selected`)}
		/>
		<Dropdown
			label="Status"
			placeholder="All"
			items={statuses}
			multiple
			bind:values
			selectionText={(selected) => selected.map((item) => item.label).join(', ')}
			--dropdown-max-width="260px"
		/>
	</div>
	<p>{last || 'No changes yet'}</p>
</div>

<style>
	.demo {
		display: grid;
		gap: 12px;
	}

	.row {
		display: flex;
		flex-wrap: wrap;
		gap: 12px;
	}

	p {
		margin: 0;
		font-size: 13px;
	}
</style>
```

### Sizes, colors and content

`size` and `variant` are the ones of `Button`, and the `--button-*` variables restyle the trigger. `valueSnippet` replaces the text of the selection, here with a colored dot from `data`; `itemLabelSnippet` does the same in the list.

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

	const views: Item[] = [
		{ value: 'day', label: 'Day' },
		{ value: 'week', label: 'Week' },
		{ value: 'month', label: 'Month' }
	];

	const rooms: Item<{ color: string }>[] = [
		{ value: 'a', label: 'Room A', data: { color: '#3c70ce' } },
		{ value: 'b', label: 'Room B', data: { color: '#0e7a4f' } },
		{ value: 'c', label: 'Room C', data: { color: '#cf2f36' } }
	];

	let room: Item<{ color: string }>[] = $state([rooms[0]]);
</script>

<div class="demo">
	<Dropdown size="sm" items={views} values={[views[1]]} clearable={false} />
	<Dropdown items={views} values={[views[1]]} clearable={false} />
	<Dropdown size="lg" variant="primary" items={views} values={[views[1]]} clearable={false} />
	<Dropdown label="Room" items={rooms} bind:values={room} --button-border-radius="999px">
		{#snippet valueSnippet({ values, text })}
			<span class="room">
				{#if values[0]}<span class="dot" style:background={values[0].data?.color}></span>{/if}
				{text}
			</span>
		{/snippet}
		{#snippet itemLabelSnippet({ item })}
			<span class="room"><span class="dot" style:background={item.data?.color}></span>{item.label}</span>
		{/snippet}
	</Dropdown>
</div>

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

	.room {
		display: inline-flex;
		align-items: center;
		gap: 8px;
	}

	.dot {
		width: 8px;
		height: 8px;
		border-radius: 50%;
	}
</style>
```

### Mobile drawer

With `mobileDrawer`, on screens up to 1024px wide the list opens in a bottom drawer, with bigger rows and no search field; on wider screens it stays a menu. Resize the window to try it.

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

	const operators: Item[] = [
		'Luca Rossi',
		'Giulia Marini',
		'Sara Bianchi',
		'Marco Conti',
		'Elena Greco',
		'Paolo Ricci',
		'Chiara Bruno',
		'Davide Gallo'
	].map((name) => ({ value: name.toLowerCase().replace(' ', '-'), label: name }));
</script>

<Dropdown label="Operator" placeholder="Anyone" items={operators} mobileDrawer />
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `items` | `Item<Data>[]` | `[]` | Options to choose from. |
| `values` (bindable) | `Item<Data>[]` |  | Selected options. With single selection it holds at most one item. |
| `multiple` | `boolean` | `false` | Allows selecting more than one option. |
| `label` | `string` |  | Text before the selection inside the button, and the accessible name of the control. |
| `placeholder` | `string` |  | Text shown in place of the selection while nothing is selected. Defaults to "Select", in the language of `locale`. |
| `icon` | `string` |  | SVG path of an icon at the start of the button. |
| `clearable` | `boolean` | `true` | Shows a button that clears the selection, in place of the arrow, while something is selected. |
| `mandatory` | `boolean` | `true` | Prevents removing the last selected option from the list. The clear button still clears it. |
| `disabled` | `boolean` | `false` | Disables the button and hides the clear button. |
| `open` (bindable) | `boolean` |  | Whether the list is open. `undefined` counts as closed. |
| `name` | `string` |  | Form field name: each selected `value` is submitted under this name. |
| `closeOnSelect` | `boolean` |  | Closes the list after an option is selected. Defaults to `true` with single selection and to `false` with `multiple`. |
| `placement` | `'bottom-start' \| 'bottom' \| 'bottom-end' \| 'top-start' \| 'top' \| 'top-end' \| 'left-start' \| 'left' \| 'left-end' \| 'right-start' \| 'right' \| 'right-end'` | `'bottom-start'` | Side of the button the list opens on. It flips when there is not enough space. |
| `mobileDrawer` | `boolean` | `false` | On screens up to 1024px wide, opens the list in a bottom drawer instead of a menu. |
| `columns` | `number` |  | Lays the options out in a grid with this many columns, for example for icons: all four arrows move through it. |
| `variant` | `'primary' \| 'secondary' \| 'danger' \| 'gradient'` | `'secondary'` | Color of the button. |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Height, padding and text size of the button. |
| `selectionText` | `(values: Item<Data>[]) => string` |  | Text of the button while something is selected. Defaults to the label of the option, or "N selected" in the language of `locale` with more than one. |
| `clearLabel` | `string` |  | Accessible name of the clear button. Defaults to "Clear selection", in the language of `locale`. |
| `closeLabel` | `string` |  | Accessible name of the close button of the mobile drawer. Defaults to "Close", in the language of `locale`. |
| `noResultsText` | `string` |  | Text shown when there are no options. Defaults to "No options", 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`. |
| `buttonElement` (bindable) | `HTMLButtonElement` |  | The native `<button>` element of the trigger. |
| `class` | `{ container?: string; button?: string; label?: string; value?: string; clear?: string; menu?: string; option?: string; }` | `{}` | Extra classes for each part. |

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

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `iconSnippet` | `{ icon: string \| undefined }` | Replaces the icon at the start of the button. |
| `labelSnippet` | `{ label: string \| undefined }` | Replaces the label inside the button. It stays the accessible name. |
| `valueSnippet` | `{ values: Item<Data>[]; text: string; placeholder: string }` | Replaces the selection text (or the placeholder) inside the button. |
| `chevronSnippet` | `{ open: boolean }` | Replaces the arrow at the end of the button. |
| `clearSnippet` |  | Replaces the icon of the clear button. |
| `itemSnippet` | `{ item: Item<Data>; index: number; selected: boolean; highlighted: boolean }` | Replaces the content of each option (icon, label and check mark). The option keeps its click handling, role and state. |
| `itemLabelSnippet` | `{ item: Item<Data> }` | Replaces the label of each option, keeping its icon and check mark. |
| `emptySnippet` |  | Replaces the content of the row shown when there are no options. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onchange` | `(change: { select?: Item<Data>; unselect?: Item<Data>; selection: Item<Data>[]; }) => void` | Called when an option is selected or removed, with the option added (`select`), the one removed (`unselect`, with single selection also the one replaced; on clear the first one) and the new `selection`. |
| `onclose` | `() => void` | Called whenever the list closes. |

## Types

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

### Item

An option of a picker (Autocomplete, Select, ...).

`type Item<Data = unknown> = { … }`

| Name | Type | Description |
| --- | --- | --- |
| `value` (required) | `string \| number` | Identity of the option: two items are the same option when their `value`s are equal (`===`). |
| `label` | `string \| number` | Text shown and searched. Defaults to `value`. |
| `icon` | `string` | SVG path of an icon shown before the label, for example from `@mdi/js`. |
| `data` | `Data` | Any extra data, available in snippets and callbacks. |

## CSS variables

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

| Per-instance variable | Default |
| --- | --- |
| `--dropdown-width` | `--dropdown-default-width`: `fit-content` |
| `--dropdown-max-width` | `--dropdown-default-max-width`: `100%` |
| `--dropdown-min-width` | `--dropdown-default-min-width`: `0` |
| `--dropdown-menu-min-width` | `--dropdown-default-menu-min-width`: `220px` |
| `--dropdown-label-color` | `--dropdown-default-label-color`: `color-mix(in oklab, currentColor 70%, transparent)` |
| `--dropdown-label-font-weight` | `--dropdown-default-label-font-weight`: `400` |
| `--dropdown-placeholder-color` | `--dropdown-default-placeholder-color`: `color-mix(in oklab, currentColor 70%, transparent)` |
| `--dropdown-chevron-size` | `--dropdown-default-chevron-size`: `18px` |
| `--dropdown-chevron-color` | `--dropdown-default-chevron-color`: `color-mix(in oklab, currentColor 70%, transparent)` |
| `--dropdown-clear-icon-size` | `--dropdown-default-clear-icon-size`: `16px` |
| `--dropdown-clear-size` | `--dropdown-default-clear-size`: `22px` |
| `--dropdown-clear-border-radius` | `--dropdown-default-clear-border-radius`: `var(--global-radius-sm)` |
| `--dropdown-clear-color` | `--dropdown-default-clear-color`: `color-mix(in oklab, currentColor 70%, transparent)` |
| `--dropdown-clear-hover-background` | `--dropdown-default-clear-hover-background`: `color-mix(in oklab, currentColor 12%, transparent)` |
| `--dropdown-clear-hover-color` | `--dropdown-default-clear-hover-color`: `currentColor` |
