Skip to content
Likable Aurora @likable-hair/svelte 5

Autocomplete

Text field that searches a list of options (items) and picks one or, with multiple, more of them. Options are Item objects, { value: string | number; label?: string | number; icon?: string; data?: Data }: value identifies the option, label is shown and searched (defaults to value), icon is an SVG path and data carries anything else. The selection lives in values, always an array, and shows as chips inside the field. icon adds an icon at the start of the field, and the item snippets add avatars, flags or extra text to the options. It follows the ARIA combobox pattern: focus stays in the input, the arrows move through the options, Enter picks the highlighted one (while typing, the first match is highlighted), Backspace on an empty field removes the last value and Escape closes the list. The list opens in a Menu as wide as the field; with mobileDrawer, on screens up to 1024px wide it opens in a bottom Drawer with its own search field. loading shows a spinner and a loading row, and a search without matches shows a "No results" row. With name, every selected value is submitted with the form. Its state is exposed as data-state, data-disabled, data-open and data-loading for app CSS.

import { Autocomplete } from '@likable-hair/svelte';

src/lib/components/simple/forms/Autocomplete.svelte · Markdown for LLMs

Single selection

Type to filter: the first match is highlighted and Enter picks it, the arrows move through the list. The search ignores case and accents ("pedi" finds "Pédicure"). bind:values holds the selection, always an array; picking another option replaces it.

The main service of the appointment

Selected: none

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

	const services: Item[] = [
		{ value: 'cut', label: 'Haircut' },
		{ value: 'blow-dry', label: 'Blow-dry' },
		{ value: 'color', label: 'Color' },
		{ value: 'balayage', label: 'Balayage' },
		{ value: 'highlights', label: 'Highlights' },
		{ value: 'keratin', label: 'Keratin treatment' },
		{ value: 'manicure', label: 'Manicure' },
		{ value: 'pedicure', label: 'Pédicure' }
	];

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

<div class="demo">
	<Autocomplete
		label="Service"
		placeholder="Search a service…"
		items={services}
		bind:values
		hint="The main service of the appointment"
	/>
	<p>Selected: {values[0]?.label ?? 'none'}</p>
</div>

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

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

Multiple selection

With multiple each pick becomes a chip and the list stays open. Backspace on an empty field removes the last chip, and maxVisibleChips collapses the others into a "+N" counter. An option's icon shows in the list and in its chip. With name every value is submitted with the form.

Haircut Balayage
Haircut Blow-dry +2
Code
<script lang="ts">
	import { mdiBrush, mdiContentCut, mdiHairDryerOutline, mdiPalette, mdiSpa } from '@mdi/js';
	import { Autocomplete, Button, 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 },
		{ value: 'balayage', label: 'Balayage', icon: mdiBrush },
		{ value: 'treatment', label: 'Scalp treatment', icon: mdiSpa }
	];

	let values: Item[] = $state([services[0], services[3]]);
	let submitted = $state('');

	function submit(event: SubmitEvent & { currentTarget: HTMLFormElement }) {
		event.preventDefault();
		submitted = new FormData(event.currentTarget).getAll('services').join(', ');
	}
</script>

<form class="demo" onsubmit={submit}>
	<Autocomplete
		label="Services"
		placeholder="Add a service…"
		items={services}
		multiple
		bind:values
		name="services"
	/>
	<Autocomplete
		label="Services, at most 2 chips"
		placeholder="Add…"
		items={services}
		multiple
		maxVisibleChips={2}
		values={services.slice(0, 4)}
	/>
	<Button type="submit" variant="secondary">Submit</Button>
	{#if submitted}<p>Submitted services: {submitted}</p>{/if}
</form>

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

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

States

state colors the border and the hint and shows an icon; error also sets aria-invalid. disabled keeps the list closed and the chips fixed. mandatory keeps at least one value: the last chip has no remove button.

Choose a stylist
Giulia Marini
Available at 10:30
Giulia Marini Marco Rossi
Sara Bianchi
Code
<script lang="ts">
	import { Autocomplete, type Item } from '@likable-hair/svelte';

	const stylists: Item[] = [
		{ value: 1, label: 'Giulia Marini' },
		{ value: 2, label: 'Marco Rossi' },
		{ value: 3, label: 'Sara Bianchi' },
		{ value: 4, label: 'Luca Conti' }
	];
</script>

<div class="demo">
	<Autocomplete
		label="Stylist"
		placeholder="Search…"
		items={stylists}
		state="error"
		hint="Choose a stylist"
	/>
	<Autocomplete
		label="Stylist"
		items={stylists}
		values={[stylists[0]]}
		state="success"
		hint="Available at 10:30"
	/>
	<Autocomplete label="Stylists" items={stylists} multiple values={stylists.slice(0, 2)} disabled />
	<Autocomplete
		label="Stylists, at least one"
		items={stylists}
		multiple
		mandatory
		values={[stylists[2]]}
	/>
</div>

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

Custom content

icon puts an icon at the start of the field. itemAppendSnippet adds content at the end of each option, here the price from data, and the label keeps the highlighted match; itemIconSnippet and chipIconSnippet replace the icons of options and chips, for example with avatars or flags. chipLabelSnippet changes the chip text and emptySnippet the row shown without matches. itemLabelSnippet replaces the label alone and itemSnippet the whole content of an option.

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

	const services: Item<{ price: number }>[] = [
		{ value: 'cut', label: 'Haircut', icon: mdiContentCut, data: { price: 30 } },
		{ value: 'long', label: 'Long blow-dry', icon: mdiHairDryerOutline, data: { price: 25 } },
		{ value: 'short', label: 'Short blow-dry', icon: mdiHairDryerOutline, data: { price: 18 } },
		{ value: 'color', label: 'Color', icon: mdiPalette, data: { price: 45 } },
		{ value: 'balayage', label: 'Balayage', icon: mdiBrush, data: { price: 90 } }
	];
</script>

<div class="demo">
	<Autocomplete
		label="Services"
		placeholder="Search…"
		icon={mdiMagnify}
		items={services}
		multiple
	>
		{#snippet itemAppendSnippet({ item })}
			<span class="price">€ {item.data?.price}</span>
		{/snippet}
		{#snippet chipLabelSnippet({ selection })}
			{selection.label} · €{selection.data?.price}
		{/snippet}
		{#snippet emptySnippet({ searchText })}
			No service matches “{searchText}”
		{/snippet}
	</Autocomplete>
</div>

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

	.price {
		font-family: var(--global-font-family-mono);
		font-size: 11px;
		opacity: 0.7;
	}
</style>

Mobile drawer

With mobileDrawer, on screens up to 1024px wide the list opens in a bottom drawer instead of a menu: it has its own search field at the top, larger rows and the label as title. Narrow the window or open this page on a phone to try it.

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

	const cities: Item[] = [
		'Bergamo',
		'Bologna',
		'Brescia',
		'Firenze',
		'Genova',
		'Milano',
		'Napoli',
		'Padova',
		'Roma',
		'Torino',
		'Venezia',
		'Verona'
	].map((city) => ({ value: city, label: city }));
</script>

<div class="demo">
	<Autocomplete label="Branches" placeholder="Search a city…" items={cities} multiple mobileDrawer />
</div>

<style>
	.demo {
		width: 100%;
		max-width: 340px;
	}
</style>

Props

NameTypeDefaultDescription
values bindable Item<Data>[]Selected options. With single selection it holds at most one item.
items Item<Data>[][]Options to choose from.
multiple booleanfalseAllows selecting more than one option.
searchText bindable stringText typed in the field.
open bindable booleanWhether the list is open. undefined counts as closed.
label stringVisible label above the field, linked to the input. In the mobile drawer it is the title.
hint stringText 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.
placeholder stringPlaceholder of the search input.
icon stringSVG path of an icon at the start of the field, for example a magnifier from @mdi/js.
disabled booleanfalseDisables the field: the list does not open and the values cannot be removed.
mandatory booleanfalsePrevents removing the last selected value.
loading booleanfalseShows a spinner in the field and a loading row in place of the options, for example while results are fetched.
id stringid of the input. Generated when missing, so the label always points to the input.
name stringForm field name: each selected value is submitted under this name.
maxVisibleChips numberShows at most this many chips, followed by a "+N" counter whose Tooltip lists the hidden values.
searchFunction (item: Item<Data>, searchText: string) => booleanReplaces the default search, which matches the label ignoring case and accents. Called for each item while searchText is not empty. Return true for every item when the filtering happens elsewhere, for example on a server.
closeOnSelect booleanCloses the list after an option is selected. Defaults to true with single selection and to false with multiple.
clearSearchOnClose booleantrueEmpties searchText when the list closes.
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 field the list opens on. It flips when there is not enough space.
mobileDrawer booleanfalseOn screens up to 1024px wide, opens the list in a bottom drawer with its own search field instead of a menu.
drawerSearch booleantrueShows the search field at the top of the mobile drawer. Without it the list itself takes the focus and the keyboard, as with a trigger that cannot type.
drawerTitle stringTitle of the mobile drawer. Defaults to label.
columns numberLays the options out in a grid with this many columns, for example for icons: the left and right arrows move to the previous and next option, the up and down arrows to the one above and below. In a text field the left and right arrows keep moving the caret. The menu takes the width of the grid instead of the one of the field.
loadingText stringText of the loading row. Defaults to "Loading…", in the language of locale.
noResultsText stringText shown when no option matches. Defaults to "No results", in the language of locale.
removeLabel stringAccessible name of the chip remove buttons, followed by the option label. Defaults to "Remove", in the language of locale.
closeLabel stringAccessible name of the close button of the mobile drawer. Defaults to "Close", in the language of locale.
locale stringBCP 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.
input bindable HTMLInputElementThe native <input> element.
class { container?: string; label?: string; field?: string; input?: string; chip?: string; menu?: string; option?: string; hint?: string; }{}Extra classes for each part.

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

Snippets

NameParametersDescription
labelSnippet{ label: string | undefined }Replaces the label content. It stays linked to the input.
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.
selectionContainerSnippet{ values: Item<Data>[]; searchText: string | undefined; disabled: boolean; openMenu: () => void; handleKeyDown: (event: KeyboardEvent) => void; select: (item: Item<Data>) => void; unselect: (item: Item<Data>) => void; attributes: HTMLAttributes<HTMLElement>; }Replaces the whole field with a custom trigger, such as a button. Spread attributes on its focusable element and call handleKeyDown from its onkeydown, so it works as a combobox for keyboards and screen readers.
selectionSnippet{ selection: Item<Data>; unselect: (item: Item<Data>) => void }Replaces each selected chip.
chipLabelSnippet{ selection: Item<Data> }Replaces the text inside each selected chip.
chipIconSnippet{ selection: Item<Data> }Replaces the icon of each selected chip (item.icon), for example with an avatar or a flag.
iconSnippetReplaces the icon at the start of the field.
exceedCounterSnippet{ notVisibleChipNumber: number; maxVisibleChips: number; values: Item<Data>[]; searchText: string | undefined; disabled: boolean; }Replaces the "+N" counter shown with maxVisibleChips.
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.
itemIconSnippet{ item: Item<Data> }Replaces the icon of each option (item.icon), for example with an avatar or a flag.
itemAppendSnippet{ item: Item<Data>; selected: boolean }Content at the end of each option, before the check mark, for example a price or a code.
loadingSnippetReplaces the content of the loading row.
emptySnippet{ searchText: string | undefined }Replaces the content of the row shown when no option matches.

Events

NameTypeDescription
onchange(change: { select?: Item<Data>; unselect?: Item<Data>; selection: Item<Data>[]; }) => voidCalled when an option is selected or removed, with the option added (select), the one removed (unselect, with single selection also the one replaced) and the new selection.
onclose() => voidCalled 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> = { … }

NameTypeDescription
value requiredstring | numberIdentity of the option: two items are the same option when their values are equal (===).
label string | numberText shown and searched. Defaults to value.
icon stringSVG path of an icon shown before the label, for example from @mdi/js.
data DataAny extra data, available in snippets and callbacks.

CSS variables

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

Per-instance variableDefault
--autocomplete-border-color
--autocomplete-default-border-color var(--global-input-border-color)
--autocomplete-hover-border-color
--autocomplete-default-hover-border-color var(--global-input-hover-border-color)
--autocomplete-focus-border-color
--autocomplete-default-focus-border-color var(--global-input-focus-border-color)
--autocomplete-focus-ring-color
--autocomplete-default-focus-ring-color var(--global-input-focus-ring-color)
--autocomplete-hint-color
--autocomplete-default-hint-color var(--global-input-hint-color)
--autocomplete-icon-size
--autocomplete-default-icon-size 18px
--autocomplete-gap
--autocomplete-default-gap 6px
--autocomplete-width
--autocomplete-default-width 100%
--autocomplete-max-width
--autocomplete-default-max-width none
--autocomplete-error-color
--autocomplete-default-error-color var(--global-input-error-color)
--autocomplete-success-color
--autocomplete-default-success-color var(--global-input-success-color)
--autocomplete-label-color
--autocomplete-default-label-color var(--global-input-label-color)
--autocomplete-label-font-size
--autocomplete-default-label-font-size var(--global-input-label-font-size)
--autocomplete-label-font-weight
--autocomplete-default-label-font-weight var(--global-input-label-font-weight)
--autocomplete-inner-gap
--autocomplete-default-inner-gap var(--global-input-gap)
--autocomplete-min-height
--autocomplete-default-min-height var(--global-input-height)
--autocomplete-padding
--autocomplete-default-padding 4px 6px
--autocomplete-background
--autocomplete-default-background var(--global-input-background)
--autocomplete-icon-color
--autocomplete-default-icon-color var(--global-input-icon-color)
--autocomplete-border-width
--autocomplete-default-border-width var(--global-input-border-width)
--autocomplete-border-radius
--autocomplete-default-border-radius var(--global-input-radius)
--autocomplete-box-shadow
--autocomplete-default-box-shadow var(--global-input-shadow)
--autocomplete-focus-ring-width
--autocomplete-default-focus-ring-width var(--global-focus-ring-width)
--autocomplete-chip-gap
--autocomplete-default-chip-gap 6px
--autocomplete-input-min-width
--autocomplete-default-input-min-width 60px
--autocomplete-input-padding
--autocomplete-default-input-padding 0 6px
--autocomplete-color
--autocomplete-default-color var(--global-input-color)
--autocomplete-font-size
--autocomplete-default-font-size var(--global-input-font-size)
--autocomplete-placeholder-color
--autocomplete-default-placeholder-color var(--global-input-placeholder-color)
--autocomplete-counter-color
--autocomplete-default-counter-color var(--global-color-text-3)
--autocomplete-counter-font-size
--autocomplete-default-counter-font-size 12px
--autocomplete-counter-font-weight
--autocomplete-default-counter-font-weight 500
--autocomplete-start-padding
--autocomplete-default-start-padding 6px
--autocomplete-end-padding
--autocomplete-default-end-padding 6px
--autocomplete-hint-font-size
--autocomplete-default-hint-font-size var(--global-input-hint-font-size)
--autocomplete-disabled-background
--autocomplete-default-disabled-background var(--global-input-disabled-background)
--autocomplete-disabled-opacity
--autocomplete-default-disabled-opacity var(--global-input-disabled-opacity)
--autocomplete-spinner-size
--autocomplete-default-spinner-size 16px
--autocomplete-spinner-color
--autocomplete-default-spinner-color var(--global-color-primary)
--autocomplete-spinner-border-width
--autocomplete-default-spinner-border-width 2px
--autocomplete-menu-max-height
--autocomplete-default-menu-max-height 300px
--autocomplete-drawer-size
--autocomplete-default-drawer-size 85dvh
--autocomplete-drawer-gap
--autocomplete-default-drawer-gap 10px
--autocomplete-option-spacing
--autocomplete-default-option-spacing 1px
--autocomplete-option-icon-size
--autocomplete-default-option-icon-size 17px
--autocomplete-option-gap
--autocomplete-default-option-gap 8px
--autocomplete-option-padding
--autocomplete-default-option-padding 7px 10px
--autocomplete-option-border-radius
--autocomplete-default-option-border-radius var(--global-radius-sm)
--autocomplete-option-color
--autocomplete-default-option-color var(--global-color-text-2)
--autocomplete-option-font-size
--autocomplete-default-option-font-size 13px
--autocomplete-option-highlighted-background
--autocomplete-default-option-highlighted-background var(--global-color-surface-2)
--autocomplete-option-highlighted-color
--autocomplete-default-option-highlighted-color var(--global-color-text)
--autocomplete-option-selected-background
--autocomplete-default-option-selected-background var(--global-color-primary-soft)
--autocomplete-option-selected-color
--autocomplete-default-option-selected-color var(--global-color-text)
--autocomplete-option-selected-highlighted-background
--autocomplete-default-option-selected-highlighted-background color-mix(in oklab, var(--global-color-primary) 20%, transparent)
--autocomplete-drawer-option-padding
--autocomplete-default-drawer-option-padding 11px 12px
--autocomplete-drawer-option-font-size
--autocomplete-default-drawer-option-font-size 15px
--autocomplete-check-color
--autocomplete-default-check-color var(--global-color-primary)
--autocomplete-match-color
--autocomplete-default-match-color var(--global-color-primary)
--autocomplete-match-font-weight
--autocomplete-default-match-font-weight 700
--autocomplete-status-padding
--autocomplete-default-status-padding 9px 10px
--autocomplete-status-color
--autocomplete-default-status-color var(--global-color-text-3)
--autocomplete-status-font-size
--autocomplete-default-status-font-size 13px