# ToggleList

Group of toggle buttons to pick one or more options, such as the services of a booking or the days of the week. Each option is an `ActivableButton`, pressed when selected, so screen readers hear the state through `aria-pressed`; the group is a `role="group"` that needs an `aria-label` (or `aria-labelledby`). With `multiple` (the default) every button turns on and off by itself; with `multiple={false}` a click replaces the selection and, since `mandatory` is on by default there, clicking the pressed button keeps it on. Its state is exposed as `data-multiple` and `data-disabled` for app CSS.

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

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

## Examples

### Multiple choice

Each button turns on and off by itself; `values` holds the selected options and `onchange` reports what changed.

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

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

	let values = $state<Item[]>([services[0], services[1]]);
</script>

<div class="demo">
	<ToggleList items={services} bind:values aria-label="Services" />
	<p>Selected: {values.map((item) => item.label).join(', ') || 'none'}</p>
</div>

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

	p {
		margin: 0;
	}
</style>
```

### Single choice

With `multiple={false}` a click replaces the selection and the pressed button stays on, like a group of radio buttons; `mandatory={false}` lets a second click turn it off. Text-only buttons show the state dot of `ActivableButton`.

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

	const periods: Item[] = [
		{ value: 'day', label: 'Day' },
		{ value: 'week', label: 'Week' },
		{ value: 'month', label: 'Month' }
	];
	const statuses: Item[] = [
		{ value: 'confirmed', label: 'Confirmed' },
		{ value: 'pending', label: 'Pending' },
		{ value: 'cancelled', label: 'Cancelled' }
	];

	let period = $state<Item[]>([periods[1]]);
	let status = $state<Item[]>([]);
</script>

<div class="demo">
	<ToggleList items={periods} bind:values={period} multiple={false} aria-label="Period" />
	<ToggleList items={statuses} bind:values={status} multiple={false} mandatory={false} aria-label="Status" />
	<p>Period: {period[0]?.label ?? 'none'} · Status: {status[0]?.label ?? 'any'}</p>
</div>

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

	p {
		margin: 0;
	}
</style>
```

### Sizes and states

`size` changes the height of the buttons, `disabled` turns the whole group off, `dot={false}` hides the state dot of the text-only buttons.

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

	const days: Item[] = [
		{ value: 1, label: 'Mon' },
		{ value: 2, label: 'Tue' },
		{ value: 3, label: 'Wed' },
		{ value: 4, label: 'Thu' },
		{ value: 5, label: 'Fri' }
	];

	let small = $state<Item[]>([days[0], days[2]]);
	let large = $state<Item[]>([days[1]]);
	let off = $state<Item[]>([days[4]]);
</script>

<div class="demo">
	<ToggleList items={days} bind:values={small} size="sm" dot={false} aria-label="Opening days, small" />
	<ToggleList items={days} bind:values={large} size="lg" aria-label="Opening days, large" />
	<ToggleList items={days} bind:values={off} disabled aria-label="Opening days, disabled" />
</div>

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

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `items` (required) | `Item<Data>[]` |  | The options, one button each, in this order. |
| `values` (bindable) | `Item<Data>[]` |  | The selected options, compared with `items` by `value`. |
| `multiple` | `boolean` | `true` | Lets more options be selected together. With `false` a click replaces the selection. |
| `mandatory` | `boolean` |  | Prevents removing the last selected option, so a click on it keeps it pressed. Defaults to `true` with `multiple={false}` (it works like a group of radio buttons) and to `false` otherwise. |
| `disabled` | `boolean` | `false` | Disables every button. |
| `size` | `'sm' \| 'md' \| 'lg'` |  | Height, padding and text size of the buttons: `sm` 28px, `md` 34px, `lg` 40px. |
| `dot` | `boolean` | `true` | Shows the state dot on the buttons with text and no icon, as in `ActivableButton`. |
| `class` | `string` | `''` | Extra classes on the root element. |
| `toggleListElement` (bindable) | `HTMLDivElement` |  | The root element. |

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 |
| --- | --- | --- |
| `itemSnippet` | `{ item: Item<Data>; active: boolean }` | Replaces the content of each button (its icon stays, if the item has one). |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onchange` | `(change: { select?: Item<Data>; unselect?: Item<Data>; selection: Item<Data>[]; }) => void` | Called after every change of the selection: `select` is the option turned on, `unselect` the one turned off (with `multiple={false}`, the one replaced), `selection` the new `values`. |

## 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: `<ToggleList --toggle-list-gap="…">`. The `--toggle-list-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--toggle-list-gap` | `--toggle-list-default-gap`: `8px` |
| `--toggle-list-width` | `--toggle-list-default-width`: `auto` |
| `--toggle-list-max-width` | `--toggle-list-default-max-width`: `none` |
| `--toggle-list-height` | `--toggle-list-default-height`: `auto` |
