Combobox
The Combobox streamlines single and multiple selections in large datasets of over 15 options. Its autocomplete feature filters options as users type, improving speed and usability. For multiple selections, it displays choices as tags in a tag list, enabling easy management and clear visibility of selected items.
<sl-combobox multiple value='["0","1"]'>
<sl-listbox>
<sl-option value="0">Mathematics</sl-option>
<sl-option value="1">Geography</sl-option>
<sl-option value="2">Physics</sl-option>
<sl-option value="3">History</sl-option>
</sl-listbox>
</sl-combobox>
When to Use
Use a Combobox to help users easily search, filter, and select options from long list of options.
Large Datasets
Comboboxes are a better choice than checkbox or radio button groups when dealing with more than 15 options. Unlike static groups that can overwhelm users with lengthy lists, Comboboxes save space and simplify the interface by enabling search and filtering. This makes it easier and faster for users to find their desired option, especially in large datasets like cities, products, or categories, without scrolling through extensive lists.
Multiple Choices
Our combobox comes with multi-selection capabilities that provide a clear overview of selected options by displaying them within a tag list in the input field of the combobox. This is ideal when users need to choose multiple items, as it ensures selections are visible and easy to manage, allowing users to remove or modify choices effortlessly while keeping the interface organized and intuitive.
When Not to Use
Avoid a Combobox when users would benefit more from seeing all options at once for quick selection.
Fully Visible Selection:
For lists with fewer than 5 options and when your interface has plenty of space, choose Checkbox or Radio Button. These components display every option upfront, enabling users to quickly scan and select without extra clicks.
Efficient Selection:
When the options range from 5 to 15 items or when screen space is limited, using a Select component is ideal. It presents all available choices in a concise, accessible format, keeping your UI clean and straightforward. For lists exceeding 15 options, a combobox becomes beneficial by offering search and filtering capabilities to help users navigate a bigger set of choices.
Anatomy
Item | Name | Description | Optional |
---|---|---|---|
1 | Input Field | Acts as the primary area for user interaction. Users can type to search, or view the selected options in this field. | no |
2 | Indicator | A visual indicator showing the current state of the dropdown (expanded or collapsed) and guiding users to interact with the Combobox. | no |
2 | Tag List | Displays the chosen options within the input field or as separate tags, ensuring clarity about the current selection. | Yes |
3 | Menu | Displays available options dynamically. These can be split in groups to make navigation through long lists easier, and the choices will be displayed at the top. | no |
Variants
Combobox comes in two versions, each suited for specific situations:
Single Selection
This variant allows users to choose one option from the list. It’s ideal for scenarios where only one value is required, such as selecting a country or choosing a payment method. Single selection Comboboxes simplify decision-making by focusing the user on one clear choice at a time.
Multiple Selection
Enables users to select more than one option, typically representing choices as chips or stacked labels. This variant is useful in contexts like filters, tagging systems, or multi-category selection, where users need the flexibility to combine options without restriction.
Options
With these options you can tweak the appearance of the combobox in Figma. They are available in the Design Panel so you can compose the button to exactly fit the user experience need for the uses case you are working on.
Combobox Input
Item | Options | Description |
---|---|---|
Open | 'on' & 'off' | Toggle to display the dropdown list. |
Size | 'md' & 'lg' | Defines the size of the Combobox, with medium and large, adjusting its height and padding. |
Show Placeholder | 'on' & 'off' | Toggles the visibility of placeholder text in the input field when no value is selected. |
Show Tags | 'on' & 'off' | Determines whether selected items are displayed as tags in the input field for multi-selection. |
Input Value | input | Represents the current text entered in the input field, allowing dynamic filtering or user-provided values. |
Placeholder | input | Sets the placeholder text displayed in the input field when it is empty, providing a hint or instruction. |
Show Focus Ring | 'on', 'of' | Toggles the focus ring visibility around the Combobox, indicating when it is focused for accessibility. |
Combobox Dropdown
Item | Options | Description |
---|---|---|
Size | 'md' & 'lg' | Defines the size of the Combobox, with medium and large, adjusting its height and padding. |
Emphasis | 'subtle' & 'bold' | Adjusts the visual style of the dropdown selection, with subtle for minimal emphasis and bold for a stronger presence. |
Items | '1' to '3' | Defines the number of visible items in the dropdown list at once. |
Behaviors
Let's explore the behavior of the Combobox
Grouping Choices
Provides organizational structure for selected items by categorizing them under specific groups or labels. This behavior is especially beneficial in complex datasets where selections belong to distinct categories.
Stacked Selections
Displays multiple selected items as a visually stacked list at the top of the dropdow list or within tag list. This approach keeps the interface tidy and ensures users can quickly review or edit their choices without scrolling through a long dropdown.
Dynamic Filtering
Filters the options list in real time based on user input, displaying only relevant matches. This functionality enhances usability, particularly for lists with many similar or closely related items.
Autocomplete Suggestions
The Combobox provides real-time autocomplete suggestions as users type. This behavior helps narrow down options by displaying only relevant matches, making the selection process faster and more efficient, especially in large datasets. Users can select a suggestion or continue typing to refine their input further.
Related components
<sl-combobox multiple value='["0","1"]'>
<sl-listbox>
<sl-option-group label="Math">
<sl-option value="0">Algebra</sl-option>
<sl-option value="1">Geometry</sl-option>
<sl-option value="2">Calculus</sl-option>
</sl-option-group>
<sl-option-group label="Languages">
<sl-option value="3">English</sl-option>
<sl-option value="4">German</sl-option>
<sl-option value="5">French</sl-option>
<sl-option value="6">Spanish</sl-option>
<sl-option value="7">Chinese</sl-option>
</sl-option-group>
</sl-listbox>
</sl-combobox>
Listbox
Unlike the native <select>
element, you have to wrap the options in an sl-listbox
element. This is because the listbox needs to be associated with the <input>
element of the embedded text field for accessibility purposes (aria-controls
, aria-expanded
and aria-haspopup
). That can only be done if the listbox is in the same shadow root as the input.
If you are not rendering the options yourself (see below), the combobox component will automatically wrap the options in an sl-listbox
element for you.
Options
There are two ways you can define the options for the combobox:
- By using the
sl-option
element inside thesl-listbox
element yourself - By using the
options
property on thesl-combobox
element
There is no difference in behavior of the combobox between the two methods.
Using sl-option
elements
Use this method if you have a small to medium number of options and you want to define them directly in your HTML.
<sl-combobox>
<sl-listbox>
<sl-option value="0">Mathematics</sl-option>
<sl-option value="1">Geography</sl-option>
<sl-option value="2">Physics</sl-option>
<sl-option value="3">History</sl-option>
</sl-listbox>
</sl-combobox>
This is usually the simpler method, but it can become a performance issue if you have a large number of options (more than 100 options).
Using the options
property
Use this method if you have a large number of options. This method is more performant because it only renders the options that are currently visible. This method can be used with thousands of options, without any degradation in performance (this does not mean you should use the combobox component if you have thousands of options, but there are cases where the amount of options is determined by outside factors).
const options = [
{ value: "0", label: "Mathematics" },
{ value: "1", label: "Geography" },
{ value: "2", label: "Physics" },
{ value: "3", label: "History" }
];
<sl-combobox
.options=${options}
option-label-path="label"
option-value-path="value"
></sl-combobox>
By using the option-group-path
, option-label-path
, option-selected-path
and option-value-path
properties, you can specify which properties on the option objects should be used to render the options.
If you only specify a string array as the options
property, the combobox will use the strings as both the label and value.
Grouping options
You can group options using the sl-option-group
element. This element can contain sl-option
elements. It is not recommended to have nested sl-option-group
elements.
<sl-combobox>
<sl-listbox>
<sl-option-group label="Math">
<sl-option value="0">Algebra</sl-option>
<sl-option value="1">Geometry</sl-option>
<sl-option value="2">Calculus</sl-option>
</sl-option-group>
<sl-option-group label="Languages">
<sl-option value="3">English</sl-option>
<sl-option value="4">German</sl-option>
<sl-option value="5">French</sl-option>
<sl-option value="6">Spanish</sl-option>
<sl-option value="7">Chinese</sl-option>
</sl-option-group>
</sl-listbox>
</sl-combobox>
If you are using the options
property, you can group options by setting the option-group-path
property. The component will automatically group options based on the value of the specified property. The value of the group property will be used to label the group.
const options = [
{ value: "0", label: "Algebra", group: "Math" },
{ value: "1", label: "Geometry", group: "Math" },
{ value: "2", label: "Calculus", group: "Math" },
{ value: "3", label: "English", group: "Languages" },
{ value: "4", label: "German", group: "Languages" },
{ value: "5", label: "French", group: "Languages" },
{ value: "6", label: "Spanish", group: "Languages" },
{ value: "7", label: "Chinese", group: "Languages" }
];
<sl-combobox
.options=${options}
option-group-path="group"
option-label-path="label"
option-value-path="value"
></sl-combobox>
Selected
The combobox component is in single selection mode by default. If you want to enable multiple selection, you can set the multiple
property on the sl-combobox
element. The value
property is, depending on the selection mode, either a single value or an array of values.
There are two ways you can set the selected options for the combobox:
- By using the
value
property on thesl-combobox
element - By using the
selected
property on thesl-option
elements
It is sometimes easier to set the selected
property since you are already rendering the options yourself. If you are not rendering the options yourself, you can specify the group-selected-path
property instead.
In both cases you could also use the value
property on the sl-combobox
element to set the selected options.
Do not use both methods at the same time.
API
The Combobox streamlines single and multiple selections in large datasets of over 15 options. Its autocomplete feature filters options as users type, improving speed and usability. For multiple selections, it displays choices as tags in a tag list, enabling easy management and clear visibility of selected items.
Properties
Name | Attribute | Type | Default | Description |
---|---|---|---|---|
allowCustomValues | allow-custom-values | boolean | undefined | Will allow custom values not in the listbox when set. | |
autocomplete | autocomplete | 'off' | 'inline' | 'list' | 'both' | undefined | 'both' | The behavior of the combobox when it comes to suggesting options based on user input. - 'off': Suggest is off - 'inline': Only suggest options inside the input - 'list': Filter options in the list based on user input - 'both': Use both inline and list suggestions |
customValidity | custom-validity | string | undefined | The error message to display when the control is invalid. | |
dirty | - | boolean | false | A control is dirty if the user has changed the value in the UI. |
disabled | disabled | boolean | undefined | Whether the text field is disabled; when set no interaction is possible. | |
filterResults | filter-results | boolean | undefined | When set, will filter the results in the listbox based on user input. | |
form | - | HTMLFormElement | null | The form associated with the control. | |
formValue | - | unknown | The value used when submitting the form. | |
groupSelected | group-selected | boolean | undefined | When set will group all the selected options at the top of the listbox. | |
labels | - | `NodeListOf<HTMLLabelElement>` | null | The labels associated with the control. | |
multiple | multiple | boolean | undefined | Will allow the selection of multiple options if true. | |
name | name | string | undefined | The name of the form control. | |
nativeFormValue | - | FormValue | Returns the form value as used in a native `<form>`. This is always a string, File, FormData or null. | |
optionGroupPath | option-group-path | PathKeys<T> | undefined | The path to use for grouping the options. | |
optionLabelPath | option-label-path | PathKeys<T> | undefined | The path to use for the label of the option. | |
optionSelectedPath | option-selected-path | PathKeys<T> | undefined | The path to use for the selected state of the option. | |
optionValuePath | option-value-path | PathKeys<T> | undefined | The path to use for the value of the option. | |
options | options | T[] | undefined | There are 2 ways to provide options to the combobox: 1. By using this `options` property to provide an array of options. 2. By rendering a listbox element in the light DOM and populate it with `<sl-option>` elements. This options property is used to provide options for the first method. | |
placeholder | placeholder | string | undefined | Placeholder text in the input. | |
required | required | boolean | undefined | Whether the text field is a required field. | |
selectOnly | select-only | boolean | undefined | Whether the component is select only. This means you cannot type in the text field, but you can still select options. | |
showValid | show-valid | boolean | false | When set will cause the control to show it is valid after reportValidity is called. |
showValidity | show-validity | 'valid' | 'invalid' | undefined | Whether to show the validity state. | |
size | size | 'md' | 'lg' | undefined | 'md' | The size of the combobox. |
touched | - | boolean | false | A control is marked touched once the user has triggered a blur event on it. |
valid | - | boolean | Returns whether the form control is valid or not. | |
validationMessage | - | string | String representing a localized (by the browser) message that describes the validation constraints that the control does not satisfy (if any). The string is empty if the control is not a candidate for constraint validation, or it satisfies its constraints. For true localization, see `getLocalizedValidationMessage()` instead. | |
validity | - | ValidityState | Returns the validity state the control is in. | |
validityState | - | 'valid' | 'invalid' | 'pending' | Returns the current validity state. | |
value | value | unknown | undefined | The value of the combobox. If `multiple` selection is enabled, then this will be an array of values. Otherwise, it will be a single value. |
Methods
Name | Parameters | Return | Description |
---|---|---|---|
getLocalizedValidationMessage | string | This returns a localized validation message. It does not support all `ValidityState` properties, since some require more context than we have here. If you need to support more, you can override this method in your own form control. | |
reportValidity | boolean | Returns whether the control is valid. If the control is invalid, calling this will also cause an `invalid` event to be dispatched. After calling this, the control will also report the validity to the user. | |
setCustomValidity | message: string | Promise<string> | void | Sets a custom validation message for the form control. If the message is not an empty string, that will make the control invalid. By setting it to an empty string again, you can make the control valid again. |
Events
Name | Event type | Description |
---|---|---|
sl-blur | SlBlurEvent | Emits when the focus leaves the component. |
sl-change | SlChangeEvent<U | U[] | undefined> | Emits when the value changes. |
sl-focus | SlFocusEvent | Emits when the component gains focus. |
sl-form-control | SlFormControlEvent | Emits when the form control is added to the DOM. |
sl-update-state | SlUpdateStateEvent | Emits when the UI state (dirty, pristine, touched or untouched) of the form control changes. |
sl-update-validity | SlUpdateValidityEvent | Emits when the validity of the form control changes. |
sl-validate | SlValidateEvent | Emits when the form control can be validated. |
Slots
Name | Description |
---|---|
default | The input field |
options | Contains the listbox with options |
Keyboard interactions
Command | Description |
---|---|
Tab | Moves the focus into/outside of the component. |
Arrow up/down | Moves the active option 1 position up/down. The focus indicator loops, so when you are at the last option and press "down" it will focus on the first option. |
Arrow left/right | When the combobox supports multiple selection, moves the focus to the tag on the left/right, assuming the caret is already at the beginning of the input. |
Home/End | Moves the active option to the first/last option. |
Enter | Toggles the active option. |
Backspace | When a tag is focused, removes the tag. This is effectively the same as toggling the selected option using the Enter key. |
WAI-ARIA
WAI-ARIA Roles, States, and Properties for a combobox component provide essential information to assistive technologies and screen readers. They convey the combobox component's role, and additional properties to ensure accessibility and a better user experience for individuals using assistive technology.
Input
Attribute | Value | Description | User supplied |
---|---|---|---|
role | 'combobox' | Identifies the input as a combobox. | no |
aria-activedescendant | string | When an option in the listbox is visually indicated as having keyboard focus, refers to that option (by id ) or when it is already selected. | no |
aria-autocomplete | both | Indicates that the combobox can be used for both input and suggestions. The value both indicates that the combobox is a multi-function combobox. | no |
aria-controls | string | Identifies the listbox controlled by the combobox. | no |
aria-expanded | boolean | Set to true if the listbox (list of options) is visible, false if it is hidden. | no |
aria-haspopup | listbox | Indicates that the combobox has a listbox popup. | no |
aria-label | string | Defines a string that labels the action that will be performed when the user interacts with the combobox. Needs to be added when the combobox is not wrapped with sl-form-field and there is no sl-label . | yes |
aria-labelledby | string | Used to connect with single header/element that describes the combobox, when there is no label component connected to. | yes |
aria-owns | string | Identifies the listbox controlled by the combobox. | no |
aria-describedby | string | Indicates the elements that describe the combobox. Used for helper text and validation. | no |
Listbox
Attribute | Value | Description | User supplied |
---|---|---|---|
role | 'listbox' | Identifies the element as a listbox. | no |
aria-multiselectable | string | Indicates whether the combobox supports multiple selection (true ), otherwise not present. | no |