Combobox
A text input that filters a list of options as you type.
Example
<div class="stack"> <focsd-combobox label="Country" placeholder="Start typing" clearable> <focsd-option-group label="Nordics"> <focsd-option value="dk">Denmark</focsd-option> <focsd-option value="fi">Finland</focsd-option> </focsd-option-group> <focsd-option-group label="Western Europe"> <focsd-option value="fr">France</focsd-option> <focsd-option value="de">Germany</focsd-option> <focsd-option value="gb">United Kingdom</focsd-option> </focsd-option-group> </focsd-combobox></div>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-combobox.js';</script>
<focsd-combobox label="Country" placeholder="Start typing" clearable>…</focsd-combobox>import { Combobox } from '@focsd-dev/react';
export function Example() { return <Combobox onChange={(e) => console.log(e.target)} label="Country" placeholder="Start typing" clearable>…</Combobox>;}<script setup>import '@focsd-dev/components/focsd-combobox.js';</script>
<template> <focsd-combobox @change="onChange" label="Country" placeholder="Start typing" clearable>…</focsd-combobox></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-combobox.js';
// template<focsd-combobox (change)="onChange($event)" label="Country" placeholder="Start typing" clearable>…</focsd-combobox><script> import '@focsd-dev/components/focsd-combobox.js';</script>
<focsd-combobox onchange={(e) => console.log(e.target)} label="Country" placeholder="Start typing" clearable>…</focsd-combobox>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
name | name | string | — |
disabled | disabled | boolean | false |
required | required | boolean | false |
readonly | readOnly | boolean | false |
aria-label | hostAriaLabel | string | null | null |
aria-description | hostAriaDescription | string | null | null |
aria-labelledby | hostAriaLabelledBy | string | null | null |
aria-describedby | hostAriaDescribedBy | string | null | null |
label-position | labelPosition | 'top' | 'start' | 'end' | undefined | — |
hint | hint | string | — |
error | error | — | — |
warning | warning | string | undefined | — |
success | success | string | undefined | — |
error-message | errorMessage | string | undefined | — |
warning-message | warningMessage | string | undefined | — |
warning-pattern | warningPattern | string | undefined | — |
label | label | string | — |
value | value | string | — |
placeholder | placeholder | string | — |
allow-custom | allowCustom | boolean | false |
autocomplete | autocomplete | AutoFill | off |
multiple | multiple | boolean | false |
clearable | clearable | boolean | false |
select-all | selectAll | boolean | false |
max | max | number | 0 |
Methods
| Method | Description |
|---|---|
describedByIds() | IDs to add to the inner control's aria-describedby: the hint, then the shown message. |
checkValidity() | True when the value is valid; otherwise fires invalid and returns false, like native. |
reportValidity() | Like checkValidity(), and also shows the browser's validation message when invalid. |
setCustomValidity(message) | Set (non-empty) or clear (empty string) a custom validation error, like native controls. |
setValidityState(flags, message, anchor) | For subclasses: set the validity flags and message. anchor is the element the browser points its message at. |
validate() | Show (or clear) the error from the control's own checks: required, type, pattern, min,
max and the like. Uses error-message, else the browser's localized message. An error the
author set (a server error, say) is left as it is. |
clearValidation() | Clear the error and warning that validate() and warning-pattern showed (a form reset does). |
emitValue(commit) | For subclasses: fire the value events, as a native control does on a user edit: input
(bubbles, composed), then with commit also change (bubbles, stays in its tree). |
idrefText(ids) | For subclasses: the text of the elements an ID reference list names, resolved in the host's tree (ID references cannot cross a shadow root), joined by spaces. |
open() | Open the list (does nothing while disabled or read-only). |
close() | Close the list. |
clear() | Clear the value and the typed text (the clear button). |
Events
| Event | Description |
|---|---|
input | Fired on every user edit of the value. |
change | Fired when the user commits a change to the value or state. |
CSS parts
Style the inside of the component with ::part(), for example focsd-combobox::part(active).
| Part | Description |
|---|---|
active | Added to the highlighted option. |
layout | The wrapper holding the label and the body; its `data-position` is the label position. |
body | The field, then the hint and the message. |
hint | The author's hint. |
message | The shown error, warning or success message (also named by its kind). |
message-icon | The icon in the message. |
announcer | The live region that announces a changed message. |
control | The control element. |
field | The bordered row holding the chosen tags, the input and the clear button. |
label | The label element. |
popup | The popup holding the listbox. |
listbox | The listbox element. |
option | The option element. |
group | An option group. |
group-label | An option group's label. |
select-all | The Select all row (with `multiple select-all`). |
clear | The clear button (with `clearable`). |
status | The status element. |
tags | The chosen values (with `multiple`). |
tag | A chosen value. |
remove | A chosen value's remove button. |
Accessibility
autocompleteis forwarded to the input (for examplecountry-name, WCAG 1.3.5).- APG combobox with list autocomplete: focus stays in the input and the active option is referenced with
aria-activedescendant. - Slotted
focsd-options are data; the visible list is rendered inside the same shadow root as the input, so the reference never crosses a shadow boundary. - A polite status announces the result count ("3 results", "No results").
focsd-option-groupchildren render as named groups in the list; a group hides when none of its options match.multipleturns it into a multi-select (the same asfocsd-multi-select).clearableadds a "Clear <label>" button.- Typed text that does not match an option is discarded on blur; clearing the text clears the value. Set
allow-customto accept free text. - Form-associated: participates in
FormData, validation, reset, fieldsetdisabledand back/forward restore.
Keyboard
| Key | Action |
|---|---|
| Arrow Down / Up | Open and move through options |
| Alt + Arrow Down | Open without moving |
| Enter | Choose the active option |
| Escape | Close; press again to clear |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.