Autocomplete API
Props, slots, labels and styling of the slotsmith Autocomplete.
Slots
Autocomplete has 22 slots. Pass a replacement for any of them in components; every slot you leave out keeps its fallback. Element slots receive DOM props, so a library's own element fits as it is; widget slots receive named props and usually need a few lines of adapter.
| Name | Kind | Props it receives | Default fallback | data-* attributes |
|---|---|---|---|---|
Root | <element> | DOM props:
| Fallback source |
|
Trigger | <element> | DOM props:
| Fallback source |
|
TriggerInput | <element> | DOM props:
| Fallback source | — |
Toggle | <element> | DOM props:
| Fallback source |
|
Value | {widget} |
| Fallback source | — |
Tag | {widget} |
| Fallback source | — |
Clear | {widget} |
| Fallback source | — |
Indicator | {widget} |
| Fallback source | — |
Popup | <element> | DOM props:
| Fallback source |
|
Search | <element> | DOM props:
| Fallback source | — |
List | <element> | DOM props:
| Fallback source | — |
Option | <element> | DOM props:
| Fallback source |
|
OptionLabel | {widget} |
| Fallback source | — |
Check | {widget} |
| Fallback source | — |
Group | <element> | DOM props:
| Fallback source | — |
GroupLabel | {widget} |
| Fallback source | — |
Separator | <element> | DOM props:
| Fallback source | — |
Empty | {widget} |
| Fallback source | — |
Loading | {widget} |
| Fallback source | — |
Error | {widget} |
| Fallback source | — |
Create | {widget} |
| Fallback source | — |
LoadMore | {widget} |
| Fallback source | — |
Props
Every prop of Autocomplete, grouped as in the library's documentation. Controlled state comes in pairs: pass a value with its change handler, or only the default to leave the state inside the component.
| Name | Type | Default | Description |
|---|---|---|---|
| Options | |||
optionsrequired | TOption | — | The options to render. |
get | GetOptionValue< | — | Reads an option's id. Default: |
get | GetOptionLabel< | — | Reads an option's text. Default: |
option | ( | — | Marks an option unselectable. It stays visible but cannot be landed on. |
get | GetOptionGroup< | — | Reads the group an option belongs to, which is also the group's label. The options stay one flat list: groups appear in the order their first option does, an option joins its group wherever it sits (a later page included), and the options with no group form one unlabelled section where the first of them appeared. A group the search empties disappears with its label and separator. Read whenever the options or the search change. |
filter | OptionFilter< | — | How to narrow options against the search. |
selectedsingle mode | TOption | | — | The option behind |
selectedmultiple mode | TOption | — | The options behind |
| Selection | |||
multiplesingle mode | false | — | One value at a time. |
multiplerequiredmultiple mode | true | — | Several values, with the popup staying open on each pick. |
valuesingle mode | OptionValue | | — | The selected id, or |
valuemultiple mode | OptionValue | — | The selected ids. |
defaultsingle mode | OptionValue | | — | The selected id while uncontrolled. |
defaultmultiple mode | OptionValue | — | The selected ids while uncontrolled. |
onsingle mode | ( | — | The new id and, where known, the option behind it. |
onmultiple mode | ( | — | The new ids, and the options index-aligned with them. An entry is |
clearable | boolean | — | Whether the clear control is offered. Default |
close | boolean | !multiple | Whether picking closes the popup. Defaults to |
name | string | — | Renders a hidden input per value, so a plain HTML form submits it. |
| Search | |||
searchable | boolean | — | Whether the search box renders. |
search | "popup" | | — | Where the search box sits. |
search | string | — | The search text. With |
default | string | — | The search text while uncontrolled. |
on | ( | — | Told when the search text changes, for remote options. |
min | number | — | Don't search until this many characters are typed. |
| Open state and keyboard | |||
open | boolean | — | Whether the popup is open. |
default | boolean | — | Whether the popup starts open. |
on | ( | — | Told when the popup opens or closes. |
loop | boolean | — | Whether the highlight wraps past the ends. Default |
auto | boolean | — | Highlight the first enabled option whenever the list opens or its contents change, so Enter picks the top match. A highlight the user moved themselves is kept until the search changes. Default |
disabled | boolean | — | Nothing can be opened, picked or cleared. |
invalid | boolean | — | The value failed validation. Sets |
on | ( | — | Told when focus leaves the whole control, for form libraries. |
| Remote options | |||
loading | boolean | — | A first page is in flight. |
error | string | — | The last fetch failed. Replaces the list. |
on | ( | — | Offers a way back from the error. |
has | boolean | — | Another page exists. |
on | ( | — | Asks for the next page. |
loading | boolean | — | That next page is in flight. |
| Creating | |||
creatable | boolean | — | Offers a "create <query>" row when the search matches nothing. |
on | ( | — | Creates the searched name. |
create | boolean | — | The create request is in flight. |
| Rendering | |||
components | Partial< | — | Replace any part; the rest fall back to |
labels | Partial< | — | Override any string. Wins over |
locale | LocaleInput | the provider's | The language: a locale object, or the tag of one given to |
slot | AutocompleteSlotProps | — | Extra DOM props for the element parts. |
placeholder | string | labels. | Trigger text when nothing is selected. Defaults to |
max | number | 2 | Tags shown before the rest collapse into |
virtual | AutocompleteVirtualOptions | — | Renders only the options in view, for lists of thousands: pass the virtualizer hook ( |
| Positioning | |||
placement | PopupPlacement | bottom | Which side of the trigger the popup prefers. Defaults to |
popup | number | 4 | Gap between trigger and popup, in pixels. Defaults to 4. |
match | boolean | true | Make the popup exactly as wide as the trigger. Defaults to |
popup | number | 320 | Upper bound on the popup's height, in pixels. Defaults to 320. |
portal | PortalTarget | false | Renders the popup in a portal: |
Any other prop is passed to the root <div> element.
Labels & locales
Every string Autocomplete shows or announces, with its English default. Pass labels to change some of them for one component, or locale to switch every string to another language. The demo below does both.
| Key | Type | English default | Description |
|---|---|---|---|
placeholder | string | "Select…" | Trigger text when nothing is selected. |
search | string | "Search…" | Placeholder for the search box. |
clear | string | "Clear selection" | Accessible name for the clear control. |
toggle | string | "Show options" | Accessible name for the button that opens and closes the list, with |
remove | ( | remove(prints | Accessible name for a tag's remove control. |
empty | string | "No results" | Shown when a search returned nothing. |
loading | string | "Loading…" | Shown while a first page is loading. |
min | ( | minChars(prints | Shown before |
retry | string | "Retry" | Label for the retry control. |
create | ( | create(prints | Label for the create row. |
creating | string | "Creating…" | Label for the create row while the request is in flight. |
more | ( | more(prints | The overflow badge in multiple mode. |
load | string | "Load more" | Label for the load-more control. |
results | ( | results(prints | Announced to screen readers when the result count changes. |
import { Autocomplete } from "slotsmith/autocomplete";
import { de } from "slotsmith/locales/de";
const cities = [
{ value: "berlin", label: "Berlin" },
{ value: "hamburg", label: "Hamburg" },
{ value: "koeln", label: "Köln" },
{ value: "leipzig", label: "Leipzig" },
{ value: "muenchen", label: "München" },
];
export default function Labels() {
return (
<Autocomplete
options={cities}
// Every string in German, from the ready-made pack…
locale={de}
// …except these two, replaced for this picker only.
labels={{ placeholder: "Stadt wählen…", empty: "Keine Stadt gefunden" }}
aria-label="Stadt"
style={{ maxWidth: 320 }}
/>
);
}The ready-made language packs, right-to-left support and app-wide locales are on the Languages page.
Styling
The class names the fallbacks of Autocomplete put on their elements, and the tokens its stylesheet reads. Set a token on the component, or on any element around it, to restyle it; target a class only for what no token covers.
| Class | Slot |
|---|---|
.sac | |
.sac__body | — |
.sac__overflow | — |
.sac__sr-only | — |
.sac__chevron | |
.sac__tick | |
.sac__cross | — |
.sac__plus | — |
.sac__tag | |
.sac__tag-label | — |
.sac__tag-remove | — |
.sac__clear | |
.sac__spinner | — |
.sac__label | |
.sac__message | |
.sac__error | |
.sac__button | — |
.sac__create | |
.sac__create-button | — |
.sac__more | |
.sac__more-button | — |
.sac__trigger | |
.sac__trigger-input | |
.sac__toggle | |
.sac__value | |
.sac__value--empty | — |
.sac__popup | |
.sac__search | |
.sac__list | |
.sac__option | |
.sac__group | |
.sac__group-list | — |
.sac__group-label | |
.sac__separator |
| Token | Layer |
|---|---|
--sac-accent | This component |
--sac-border | This component |
--sac-danger | This component |
--sac-font-size | This component |
--sac-hover | This component |
--sac-muted | This component |
--sac-radius | This component |
--sac-surface | This component |
--sac-text | This component |
--ss-accent | Shared by every component |
--ss-border | Shared by every component |
--ss-danger | Shared by every component |
--ss-font-size | Shared by every component |
--ss-hover | Shared by every component |
--ss-muted | Shared by every component |
--ss-radius | Shared by every component |
--ss-surface | Shared by every component |
--ss-text | Shared by every component |
Every token switches to its dark value under .dark, [data-theme="dark"], on the page or on any element around the component.