# IconsDropdown

Button that picks an icon from a grid, for example the heading level or the text alignment in an editor toolbar. It is a `Dropdown` whose options are `IconItem`s, `{ value, label, icon, data? }`: the button shows the chosen icons as tiles and the list is a grid of tiles (`columns`, by default as many as the options up to 5) that the four arrows move through. Every option needs a `label`: it is its accessible name, the Tooltip shown while it is highlighted and what typing a letter jumps to. The `label` of the component names the button for screen readers and titles the mobile drawer, without being shown. The selection lives in `values`, always an array: one icon, or more with `multiple`. Everything else works as in `Dropdown`, including `clearable` (off by default here), `mandatory`, `placement`, `mobileDrawer`, `variant` and `size`; the button takes the `--button-*` variables and the menu the `--menu-*` ones.

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

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

## Examples

### Editor toolbar

Heading level and text alignment, the typical use. Every option has a `label`: screen readers read it, the Tooltip shows it while the tile is highlighted, and typing its first letter jumps to it. The `label` of the component names the button.

```svelte
<script lang="ts">
	import {
		mdiFormatAlignCenter,
		mdiFormatAlignJustify,
		mdiFormatAlignLeft,
		mdiFormatAlignRight,
		mdiFormatHeader1,
		mdiFormatHeader2,
		mdiFormatHeader3,
		mdiFormatParagraph
	} from '@mdi/js';
	import { IconsDropdown, type IconItem } from '@likable-hair/svelte';

	const levels: IconItem[] = [
		{ value: 'p', label: 'Paragraph', icon: mdiFormatParagraph },
		{ value: 'h1', label: 'Heading 1', icon: mdiFormatHeader1 },
		{ value: 'h2', label: 'Heading 2', icon: mdiFormatHeader2 },
		{ value: 'h3', label: 'Heading 3', icon: mdiFormatHeader3 }
	];

	const alignments: IconItem[] = [
		{ value: 'left', label: 'Align left', icon: mdiFormatAlignLeft },
		{ value: 'center', label: 'Center', icon: mdiFormatAlignCenter },
		{ value: 'right', label: 'Align right', icon: mdiFormatAlignRight },
		{ value: 'justify', label: 'Justify', icon: mdiFormatAlignJustify }
	];

	let level: IconItem[] = $state([levels[0]]);
	let alignment: IconItem[] = $state([alignments[0]]);
</script>

<div class="demo">
	<div class="toolbar" role="toolbar" aria-label="Text format">
		<IconsDropdown label="Text style" items={levels} bind:values={level} />
		<IconsDropdown label="Alignment" items={alignments} bind:values={alignment} />
	</div>
	<p>{level[0]?.label} · {alignment[0]?.label}</p>
</div>

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

	.toolbar {
		display: flex;
		gap: 6px;
	}

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

### Icon picker

Choosing the icon of a service from a grid. With more than 5 options the grid wraps to new rows: `columns` changes how many tiles fit in a row, and the up and down arrows move between rows. `placeholder` is shown while nothing is chosen. With `mobileDrawer` the grid opens in a bottom drawer on phones.

```svelte
<script lang="ts">
	import {
		mdiBottleTonicOutline,
		mdiContentCut,
		mdiFaceWomanShimmerOutline,
		mdiFlowerOutline,
		mdiHairDryerOutline,
		mdiHandBackRightOutline,
		mdiLipstick,
		mdiPaletteOutline,
		mdiSpaOutline,
		mdiWaterOutline
	} from '@mdi/js';
	import { IconsDropdown, type IconItem } from '@likable-hair/svelte';

	const icons: IconItem[] = [
		{ value: 'cut', label: 'Haircut', icon: mdiContentCut },
		{ value: 'color', label: 'Color', icon: mdiPaletteOutline },
		{ value: 'blow-dry', label: 'Blow-dry', icon: mdiHairDryerOutline },
		{ value: 'spa', label: 'Spa', icon: mdiSpaOutline },
		{ value: 'facial', label: 'Facial', icon: mdiFaceWomanShimmerOutline },
		{ value: 'make-up', label: 'Make-up', icon: mdiLipstick },
		{ value: 'manicure', label: 'Manicure', icon: mdiHandBackRightOutline },
		{ value: 'treatment', label: 'Treatment', icon: mdiBottleTonicOutline },
		{ value: 'wash', label: 'Wash', icon: mdiWaterOutline },
		{ value: 'relax', label: 'Relax', icon: mdiFlowerOutline }
	];

	let values: IconItem[] = $state([]);
</script>

<div class="demo">
	<IconsDropdown label="Service icon" placeholder="Choose an icon" items={icons} bind:values clearable mobileDrawer />
	<p>Icon: {values[0]?.label ?? 'none'}</p>
</div>

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

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

### Multiple

With `multiple` more icons can be chosen and the list stays open. The button shows up to `maxVisibleIcons` tiles, then a "+N" counter; screen readers hear every label.

```svelte
<script lang="ts">
	import {
		mdiCodeTags,
		mdiFormatBold,
		mdiFormatItalic,
		mdiFormatStrikethrough,
		mdiFormatUnderline
	} from '@mdi/js';
	import { IconsDropdown, type IconItem } from '@likable-hair/svelte';

	const marks: IconItem[] = [
		{ value: 'bold', label: 'Bold', icon: mdiFormatBold },
		{ value: 'italic', label: 'Italic', icon: mdiFormatItalic },
		{ value: 'underline', label: 'Underline', icon: mdiFormatUnderline },
		{ value: 'strike', label: 'Strikethrough', icon: mdiFormatStrikethrough },
		{ value: 'code', label: 'Code', icon: mdiCodeTags }
	];

	let values: IconItem[] = $state(marks.slice(0, 4));
</script>

<div class="demo">
	<IconsDropdown label="Marks" items={marks} bind:values multiple mandatory={false} maxVisibleIcons={2} />
	<p>{values.map((mark) => mark.label).join(', ') || 'No marks'}</p>
</div>

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

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

### Sizes and states

`size` follows the buttons, so the dropdown lines up with them in a toolbar. `disabled` blocks it, `clearable` swaps the arrow for a clear button while something is chosen. The tiles take the `--icons-dropdown-*` variables, here the background of the chosen one.

```svelte
<script lang="ts">
	import { mdiFormatAlignCenter, mdiFormatAlignLeft, mdiFormatAlignRight } from '@mdi/js';
	import { IconsDropdown, type IconItem } from '@likable-hair/svelte';

	const alignments: IconItem[] = [
		{ value: 'left', label: 'Align left', icon: mdiFormatAlignLeft },
		{ value: 'center', label: 'Center', icon: mdiFormatAlignCenter },
		{ value: 'right', label: 'Align right', icon: mdiFormatAlignRight }
	];
</script>

<div class="demo">
	<IconsDropdown label="Small" size="sm" items={alignments} values={[alignments[0]]} />
	<IconsDropdown label="Medium" items={alignments} values={[alignments[1]]} />
	<IconsDropdown label="Large" size="lg" items={alignments} values={[alignments[2]]} />
	<IconsDropdown label="Disabled" items={alignments} values={[alignments[0]]} disabled />
	<IconsDropdown label="Clearable" items={alignments} values={[alignments[1]]} clearable />
	<IconsDropdown
		label="Custom"
		items={alignments}
		values={[alignments[2]]}
		--icons-dropdown-value-background="var(--global-color-accent)"
	/>
</div>

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

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `items` | `IconItem<Data>[]` | `[]` | Options to choose from. Each one needs an `icon` and a `label`. |
| `values` (bindable) | `IconItem<Data>[]` |  | Selected options. With single selection it holds at most one item. |
| `label` | `string` |  | Accessible name of the button, also the title of the mobile drawer. It is not shown. |
| `placeholder` | `string` |  | Text shown in the button while nothing is selected. Without it the button shows only the arrow, and screen readers hear "Select", in the language of `locale`. |
| `clearable` | `boolean` | `false` | Shows a button that clears the selection, in place of the arrow, while something is selected. |
| `columns` | `number` |  | Number of tiles per row in the list. Defaults to the number of options, up to 5. |
| `maxVisibleIcons` | `number` | `3` | With `multiple`, the button shows at most this many tiles, followed by a "+N" counter. |
| `selectionText` | `(values: IconItem<Data>[]) => string` |  | Text that screen readers hear for the selection. Defaults to the labels of the selected options. |
| `class` | `{ container?: string; button?: string; label?: string; value?: string; clear?: string; menu?: string; option?: string; tile?: string; }` | `{}` | Extra classes for each part: those of `Dropdown`, plus `tile` on every tile, in the button and in the list. |

### From Dropdown

It accepts the props, snippets and events of Dropdown (`/components/dropdown.md`), listed in the tables "From Dropdown". Except `icon`, `iconSnippet`, `labelSnippet`, `itemLabelSnippet`.

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `multiple` | `boolean` | `false` | Allows selecting more than one option. |
| `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. |
| `variant` | `'primary' \| 'secondary' \| 'danger' \| 'gradient'` | `'secondary'` | Color of the button. |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Height, padding and text size of the button. |
| `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. |

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 |
| --- | --- | --- |
| `valueSnippet` | `{ values: IconItem<Data>[]; text: string; placeholder: string }` | Replaces the content of the button before the arrow: the tiles of the selection or the placeholder. |
| `itemSnippet` | `{ item: IconItem<Data>; index: number; selected: boolean; highlighted: boolean }` | Replaces the icon inside each tile of the list. The tile keeps its label, Tooltip and state. |

### From Dropdown

| Name | Parameters | Description |
| --- | --- | --- |
| `chevronSnippet` | `{ open: boolean }` | Replaces the arrow at the end of the button. |
| `clearSnippet` |  | Replaces the icon of the clear button. |
| `emptySnippet` |  | Replaces the content of the row shown when there are no options. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onchange` | `(change: { select?: IconItem<Data>; unselect?: IconItem<Data>; selection: IconItem<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`. |

### From Dropdown

| Name | Type | Description |
| --- | --- | --- |
| `onclose` | `() => void` | Called whenever the list closes. |

## Types

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

### IconItem

An option of an `IconsDropdown`: an `Item` whose icon and label are required.

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

| Name | Type | Description |
| --- | --- | --- |
| `label` (required) | `string` | Name of the option: read by screen readers, shown in its Tooltip and matched when typing its first letters. |
| `icon` (required) | `string` | SVG path of the icon, for example from `@mdi/js`. |

### 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: `<IconsDropdown --icons-dropdown-value-color="tomato">`. The `--icons-dropdown-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

It also takes the variables of Dropdown (`/components/dropdown.md`).

| Per-instance variable | Default |
| --- | --- |
| `--icons-dropdown-width` | `--icons-dropdown-default-width`: `fit-content` |
| `--icons-dropdown-min-width` | `--icons-dropdown-default-min-width`: `0` |
| `--icons-dropdown-padding` | `--icons-dropdown-default-padding`: `0 6px 0 3px`<br>`--icons-dropdown-default-sm-padding`: `0 4px 0 3px`<br>`--icons-dropdown-default-lg-padding`: `0 8px 0 4px` |
| `--icons-dropdown-gap` | `--icons-dropdown-default-gap`: `4px` |
| `--icons-dropdown-value-gap` | `--icons-dropdown-default-value-gap`: `3px` |
| `--icons-dropdown-value-icon-size` | `--icons-dropdown-default-value-icon-size`: `16px`<br>`--icons-dropdown-default-sm-value-icon-size`: `14px`<br>`--icons-dropdown-default-lg-value-icon-size`: `18px` |
| `--icons-dropdown-value-size` | `--icons-dropdown-default-value-size`: `26px`<br>`--icons-dropdown-default-sm-value-size`: `20px`<br>`--icons-dropdown-default-lg-value-size`: `30px` |
| `--icons-dropdown-value-border-radius` | `--icons-dropdown-default-value-border-radius`: `var(--global-radius-sm)` |
| `--icons-dropdown-value-background` | `--icons-dropdown-default-value-background`: `var(--global-fill-gradient)` |
| `--icons-dropdown-value-color` | `--icons-dropdown-default-value-color`: `var(--global-color-on-primary)` |
| `--icons-dropdown-value-box-shadow` | `--icons-dropdown-default-value-box-shadow`: `0 4px 12px -6px var(--global-color-primary-glow)` |
| `--icons-dropdown-placeholder-indent` | `--icons-dropdown-default-placeholder-indent`: `7px` |
| `--icons-dropdown-counter-color` | `--icons-dropdown-default-counter-color`: `var(--global-color-text-2)` |
| `--icons-dropdown-counter-font-size` | `--icons-dropdown-default-counter-font-size`: `12px` |
| `--icons-dropdown-menu-width` | `--icons-dropdown-default-menu-width`: `auto` |
| `--icons-dropdown-menu-gap` | `--icons-dropdown-default-menu-gap`: `4px` |
| `--icons-dropdown-drawer-menu-gap` | `--icons-dropdown-default-drawer-menu-gap`: `8px` |
| `--icons-dropdown-option-icon-size` | `--icons-dropdown-default-option-icon-size`: `18px` |
| `--icons-dropdown-option-size` | `--icons-dropdown-default-option-size`: `36px` |
| `--icons-dropdown-option-border-color` | `--icons-dropdown-default-option-border-color`: `var(--global-color-border)` |
| `--icons-dropdown-option-border-radius` | `--icons-dropdown-default-option-border-radius`: `var(--global-radius-sm)` |
| `--icons-dropdown-option-background` | `--icons-dropdown-default-option-background`: `var(--global-color-surface-2)` |
| `--icons-dropdown-option-color` | `--icons-dropdown-default-option-color`: `var(--global-color-text-2)` |
| `--icons-dropdown-drawer-option-size` | `--icons-dropdown-default-drawer-option-size`: `44px` |
| `--icons-dropdown-option-highlighted-border-color` | `--icons-dropdown-default-option-highlighted-border-color`: `transparent` |
| `--icons-dropdown-option-highlighted-background` | `--icons-dropdown-default-option-highlighted-background`: `var(--global-color-primary-soft)` |
| `--icons-dropdown-option-highlighted-color` | `--icons-dropdown-default-option-highlighted-color`: `var(--global-color-primary)` |
| `--icons-dropdown-option-highlighted-transform` | `--icons-dropdown-default-option-highlighted-transform`: `translateY(-2px)` |
| `--icons-dropdown-option-selected-background` | `--icons-dropdown-default-option-selected-background`: `var(--global-fill-gradient)` |
| `--icons-dropdown-option-selected-color` | `--icons-dropdown-default-option-selected-color`: `var(--global-color-on-primary)` |
| `--icons-dropdown-option-selected-box-shadow` | `--icons-dropdown-default-option-selected-box-shadow`: `0 8px 18px -8px var(--global-color-primary-glow)` |
