# Select

Field that picks one option from a list, built on the native `<select>`: forms, `required`, the keyboard and the native picker on phones work as in HTML, and so do events (`onchange`, `oninput`, ...). Options are `Item` objects, `{ value: string | number; label?: string | number; icon?: string; data?: Data }`, and `value` holds the `value` of the selected one. With `placeholder`, `value` stays `undefined` until an option is chosen; without it the first option is selected, as in HTML. Where the browser supports `appearance: base-select` (Chrome and Edge 135+, Safari 27+) the list opens in a styled popup with icons, a check mark and custom content (`itemSnippet`), and the field shows the content of the selected option; other browsers open their native list, which shows only the labels. For multiple selection or a button trigger use `Dropdown`. Its state is exposed as `data-state`, `data-disabled` and `data-empty` for app CSS.

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

Category: simple. Source: `src/lib/components/simple/forms/Select.svelte`.

## Examples

### Basic

`bind:value` holds the `value` of the selected item, with its type (here a number). With `placeholder` the field starts empty and `value` stays `undefined` until a choice is made; without it the first item is selected, as in a native select.

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

	const durations: Item[] = [
		{ value: 15, label: '15 minutes' },
		{ value: 30, label: '30 minutes' },
		{ value: 45, label: '45 minutes' },
		{ value: 60, label: '1 hour' },
		{ value: 90, label: '1 hour 30 minutes' }
	];

	let duration: number | string | undefined = $state();
	let room: number | string | undefined = $state();
</script>

<div class="demo">
	<Select
		label="Duration"
		placeholder="Choose a duration"
		items={durations}
		bind:value={duration}
		hint="Length of the appointment"
	/>
	<Select
		label="Room"
		items={[
			{ value: 'a', label: 'Room A' },
			{ value: 'b', label: 'Room B' },
			{ value: 'c', label: 'Room C' }
		]}
		bind:value={room}
	/>
	<p>Duration: {duration ?? 'undefined'} · Room: {room}</p>
</div>

<style>
	.demo {
		display: grid;
		gap: 16px;
		width: 100%;
		max-width: 340px;
	}

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

### In a form

`name` submits the selected `value` and `required` blocks the submission while the placeholder is shown, as with a native select. Here the form data is printed instead of being sent.

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

	let sent = $state('');

	function submit(event: SubmitEvent & { currentTarget: HTMLFormElement }) {
		event.preventDefault();
		sent = new URLSearchParams(new FormData(event.currentTarget) as never).toString();
	}
</script>

<form class="demo" onsubmit={submit}>
	<Select
		label="Payment method"
		name="payment"
		placeholder="Choose…"
		required
		items={[
			{ value: 'card', label: 'Card' },
			{ value: 'cash', label: 'Cash' },
			{ value: 'voucher', label: 'Gift voucher' }
		]}
	/>
	<Button type="submit">Send</Button>
	{#if sent}<p>Sent: <code>{sent}</code></p>{/if}
</form>

<style>
	.demo {
		display: grid;
		justify-items: start;
		gap: 12px;
		width: 100%;
		max-width: 340px;
	}

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

### States

`state` colors the border and the hint and shows an icon; with `error` the select also gets `aria-invalid`. `disabled` is the native attribute.

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

	const plans: Item[] = [
		{ value: 'monthly', label: 'Monthly' },
		{ value: 'yearly', label: 'Yearly' }
	];
</script>

<div class="demo">
	<Select label="Plan" placeholder="Choose a plan" items={plans} state="error" hint="Choose a plan to continue" />
	<Select label="Plan" items={plans} value="yearly" state="success" hint="Two months free" />
	<Select label="Plan" items={plans} value="monthly" disabled />
</div>

<style>
	.demo {
		display: grid;
		gap: 16px;
		width: 100%;
		max-width: 340px;
	}
</style>
```

### Icons and custom content

Each item can have an `icon`. `itemSnippet` replaces the content of the options, here with an avatar from `data`; the field shows the content of the selected option. This needs `appearance: base-select` (Chrome and Edge 135+, Safari 27+): other browsers show the labels in their native list.

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

	const services: Item[] = [
		{ value: 'cut', label: 'Haircut', icon: mdiContentCut },
		{ value: 'blow-dry', label: 'Blow-dry', icon: mdiHairDryerOutline },
		{ value: 'color', label: 'Color', icon: mdiPalette }
	];

	const operators: Item<{ initials: string; hue: number }>[] = [
		{ value: 'luca', label: 'Luca Rossi', data: { initials: 'LR', hue: 220 } },
		{ value: 'giulia', label: 'Giulia Marini', data: { initials: 'GM', hue: 330 } },
		{ value: 'sara', label: 'Sara Bianchi', data: { initials: 'SB', hue: 150 } }
	];
</script>

<div class="demo">
	<Select label="Service" items={services} value="color" />
	<Select label="Operator" items={operators} value="giulia">
		{#snippet itemSnippet({ item })}
			<span class="avatar" style:--hue={item.data?.hue}>{item.data?.initials}</span>
			{item.label}
		{/snippet}
	</Select>
</div>

<style>
	.demo {
		display: grid;
		gap: 16px;
		width: 100%;
		max-width: 340px;
	}

	.avatar {
		display: inline-grid;
		place-items: center;
		width: 22px;
		height: 22px;
		border-radius: 50%;
		background: oklch(0.62 0.15 var(--hue));
		color: white;
		font-size: 10px;
		font-weight: 600;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `items` | `Item<Data>[]` | `[]` | Options to choose from. |
| `value` (bindable) | `string \| number` |  | `value` of the selected option. `undefined` while the placeholder is shown. |
| `label` | `string` |  | Visible label above the field, linked to the select. |
| `placeholder` | `string` |  | Text shown in the field until an option is chosen. It is not in the list, and with `required` the form cannot be submitted while it is shown. |
| `hint` | `string` |  | Text below the field. With `state` it becomes the error or success message. |
| `state` | `'error' \| 'success'` |  | Validation state: colors the border and the hint and shows an icon. `error` also sets `aria-invalid`. |
| `disabled` | `boolean` | `false` | Native `disabled`. |
| `id` | `string` |  | `id` of the select. Generated when missing, so the label always points to the select. |
| `name` | `string` |  | Native `name`: the selected `value` is submitted under it. |
| `select` (bindable) | `HTMLSelectElement` |  | The native `<select>` element. |
| `class` | `{ container?: string; label?: string; field?: string; select?: string; option?: string; hint?: string; }` | `{}` | Extra classes for each part. |

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

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `labelSnippet` | `{ label: string \| undefined }` | Replaces the label content. It stays linked to the select. |
| `hintSnippet` | `{ hint: string \| undefined }` | Replaces the hint below the field. |
| `stateIconSnippet` | `{ state: 'error' \| 'success' }` | Replaces the icon shown for `state`. Render nothing to hide it. |
| `chevronSnippet` |  | Replaces the arrow on the right of the field. |
| `itemSnippet` | `{ item: Item<Data>; index: number }` | Replaces the content of each option (icon and label) in the styled list, and of the field when the option is selected. Browsers without `appearance: base-select` show the label instead. |
| `checkSnippet` |  | Replaces the check mark of the selected option in the styled list. |

## 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: `<Select --select-background="tomato">`. The `--select-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--select-border-color` | `--select-default-border-color`: `var(--global-input-border-color)` |
| `--select-hover-border-color` | `--select-default-hover-border-color`: `var(--global-input-hover-border-color)` |
| `--select-focus-border-color` | `--select-default-focus-border-color`: `var(--global-input-focus-border-color)` |
| `--select-focus-ring-color` | `--select-default-focus-ring-color`: `var(--global-input-focus-ring-color)` |
| `--select-hint-color` | `--select-default-hint-color`: `var(--global-input-hint-color)` |
| `--select-padding-x` | `--select-default-padding-x`: `var(--global-input-padding-x)` |
| `--select-inner-gap` | `--select-default-inner-gap`: `var(--global-input-gap)` |
| `--select-icon-size` | `--select-default-icon-size`: `18px` |
| `--select-gap` | `--select-default-gap`: `6px` |
| `--select-width` | `--select-default-width`: `100%` |
| `--select-max-width` | `--select-default-max-width`: `none` |
| `--select-error-color` | `--select-default-error-color`: `var(--global-input-error-color)` |
| `--select-success-color` | `--select-default-success-color`: `var(--global-input-success-color)` |
| `--select-label-color` | `--select-default-label-color`: `var(--global-input-label-color)` |
| `--select-label-font-size` | `--select-default-label-font-size`: `var(--global-input-label-font-size)` |
| `--select-label-font-weight` | `--select-default-label-font-weight`: `var(--global-input-label-font-weight)` |
| `--select-height` | `--select-default-height`: `var(--global-input-height)` |
| `--select-background` | `--select-default-background`: `var(--global-input-background)` |
| `--select-border-width` | `--select-default-border-width`: `var(--global-input-border-width)` |
| `--select-border-radius` | `--select-default-border-radius`: `var(--global-input-radius)` |
| `--select-box-shadow` | `--select-default-box-shadow`: `var(--global-input-shadow)` |
| `--select-focus-ring-width` | `--select-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--select-color` | `--select-default-color`: `var(--global-input-color)` |
| `--select-font-size` | `--select-default-font-size`: `var(--global-input-font-size)` |
| `--select-font-weight` | `--select-default-font-weight`: `400` |
| `--select-placeholder-color` | `--select-default-placeholder-color`: `var(--global-input-placeholder-color)` |
| `--select-icon-color` | `--select-default-icon-color`: `var(--global-input-icon-color)` |
| `--select-hint-font-size` | `--select-default-hint-font-size`: `var(--global-input-hint-font-size)` |
| `--select-disabled-background` | `--select-default-disabled-background`: `var(--global-input-disabled-background)` |
| `--select-disabled-opacity` | `--select-default-disabled-opacity`: `var(--global-input-disabled-opacity)` |
| `--select-option-icon-size` | `--select-default-option-icon-size`: `17px` |
| `--select-option-gap` | `--select-default-option-gap`: `8px` |
| `--select-picker-offset` | `--select-default-picker-offset`: `6px` |
| `--select-picker-padding` | `--select-default-picker-padding`: `4px` |
| `--select-picker-max-height` | `--select-default-picker-max-height`: `300px` |
| `--select-picker-background` | `--select-default-picker-background`: `var(--global-color-surface-pop)` |
| `--select-picker-color` | `--select-default-picker-color`: `var(--global-color-text)` |
| `--select-picker-border-width` | `--select-default-picker-border-width`: `var(--global-border-width)` |
| `--select-picker-border-color` | `--select-default-picker-border-color`: `var(--global-color-border-strong)` |
| `--select-picker-border-radius` | `--select-default-picker-border-radius`: `var(--global-radius-lg)` |
| `--select-picker-box-shadow` | `--select-default-picker-box-shadow`: `var(--global-shadow-lg)` |
| `--select-picker-backdrop-filter` | `--select-default-picker-backdrop-filter`: `blur(24px)` |
| `--select-picker-transition-distance` | `--select-default-picker-transition-distance`: `6px` |
| `--select-picker-duration` | `--select-default-picker-duration`: `var(--global-duration)` |
| `--select-option-padding` | `--select-default-option-padding`: `7px 10px` |
| `--select-option-border-radius` | `--select-default-option-border-radius`: `var(--global-radius-sm)` |
| `--select-option-color` | `--select-default-option-color`: `var(--global-color-text-2)` |
| `--select-option-font-size` | `--select-default-option-font-size`: `13px` |
| `--select-option-spacing` | `--select-default-option-spacing`: `1px` |
| `--select-option-highlighted-background` | `--select-default-option-highlighted-background`: `var(--global-color-surface-2)` |
| `--select-option-highlighted-color` | `--select-default-option-highlighted-color`: `var(--global-color-text)` |
| `--select-option-selected-background` | `--select-default-option-selected-background`: `var(--global-color-primary-soft)` |
| `--select-option-selected-color` | `--select-default-option-selected-color`: `var(--global-color-text)` |
| `--select-option-selected-highlighted-background` | `--select-default-option-selected-highlighted-background`: `color-mix(in oklab, var(--global-color-primary) 20%, transparent)` |
| `--select-check-color` | `--select-default-check-color`: `var(--global-color-primary)` |
