ComboBox
A combination of a text input and dropdown that allows typing to filter options.
- HTML
- CSS
- JavaScript
- TypeScript
Usage
Use standalone, or inside Pagination.
<ComboBox {...props} />
<Pagination>
<ComboBox {...props} />
</Pagination>Anatomy
The class attribute values represent part names, and conditional indicates elements that are rendered conditionally. Other attributes represent their corresponding props.
<span class="whole" style>
<input class="main" {...rest} type="text" bind:value bind:this={element} />
<div class="extra" conditional>{extra}</div>
<ul class="bottom">
{#each options as option}
<li class="label">{option}</li>
{/each}
</ul>
</span>Props
| Name | Type | Default | Description |
|---|---|---|---|
options | SvelteSet | - | Values displayed as selectable options; empty options prevent rendering |
extra | Snippet | - | Extra content; receives expanded state and variant |
value | string | - | Bindable input value |
expanded | boolean | - | Bindable listbox open state |
search | boolean | true | Enables forward-matching search; set false for very large option sets |
cssvar | object | - | Custom-property names for the listbox x/y offsets and z-index; absent key uses the default name |
attach | Attachment | - | Svelte attachment on the input |
element | HTMLInputElement | - | Bindable input element |
styling | SVSClass | - | Styling override |
variant | SVSVariant | NEUTRAL | Variant |
Other HTMLInputAttributes are passed to <input> via rest props; class is merged onto the root.
Styling
To learn more, see Styling.
Variant Management
The currently selected option will apply the active. ul element as bottom (listbox) will apply the active when expanded. Others are not automatically switched.
Default Class Name
svs-combo-box
Behavior
- When embedded in a
ComboBoxContext,options,value,variant, and fallbackstylingcome from the context. - Closing the listbox invokes the context's
commithook when present. - The
wholewrapper isposition:relative;display:inline-block;because the listbox anchors against it; a caller overriding itsdisplayowns the resulting anchor geometry. - The listbox defaults below/left, flips per axis above/right on viewport overflow, exposes
data-svs-flip-x/data-svs-flip-yon flipped axes, and reads its offsets fromcssvarx/y custom properties (defaults--svs-position-x/--svs-position-y). - The listbox carries
z-index: var(--svs-position-z, 1)so it paints above adjacent positioned content. Override it from caller CSS or rename it via thecssvarz key to fit a caller's own stacking order.
Open/close animations should be layered on the bottom part variant classes (active = open) with opacity, transform, or scale; the component keeps visibility as its structural default.
Accessibility
Keyboard Interactions
| Key | Description |
|---|---|
ArrowDown | When closed, opens the listbox and highlights the current value if it is one of the options, otherwise the first option; when open, moves the highlight down one option |
ArrowUp | When closed, opens the listbox and highlights the current value if it is one of the options, otherwise the last option; when open, moves the highlight up one option |
Alt + ArrowDown | Opens the listbox, highlighting the current value if it is one of the options (otherwise no option is highlighted) |
Alt + ArrowUp | Closes the open listbox |
Enter | Selects the highlighted option, fills the input, and closes the listbox |
Escape | Closes the open listbox |
Typing filters the options by forward match (see the search prop).
ARIA Roles and Attributes
The component applies these automatically:
- Input (
main):role="combobox",aria-haspopup="listbox",aria-autocomplete="list",aria-controls(the listbox),aria-expanded(open state), andaria-activedescendant(the highlighted option). - Listbox (
bottom<ul>):role="listbox". - Option (
label<li>):role="option", witharia-selectedon the highlighted option.
Provide an accessible name yourself because there is no built-in visible label. Pass aria-label or aria-labelledby through the rest props, or associate a <label> with the input. The managed ARIA attributes above are omitted from ComboBoxProps and cannot be overridden.
Exports
Types
interface ComboBoxProps extends Omit<
HTMLInputAttributes,
"type" | "value" | "list" | "role" | "aria-haspopup" | "aria-autocomplete" | "aria-controls" | "aria-expanded" | "aria-activedescendant"
> {
options?: SvelteSet<string> | Set<string>;
extra?: Snippet<[boolean, string]>; // Snippet<[expanded,variant]>
value?: string; // bindable
expanded?: boolean; // bindable
search?: boolean; // (true)
cssvar?: Partial<Record<ComboBoxCssVar, string>>; // custom-property names for the listbox x/y offsets and z-index; absent key uses default name
attach?: Attachment<HTMLInputElement>;
element?: HTMLInputElement; // bindable
styling?: SVSClass;
variant?: SVSVariant; // (VARIANT.NEUTRAL)
// class & other HTMLInputAttributes are passed to <input> via ...rest
}
type ComboBoxCssVar = "x" | "y" | "z";
type ComboBoxReqdProps = never;
type ComboBoxBindProps = "value" | "expanded" | "element";Others
No additional exports are available.
Examples
The example code uses Tailwind CSS along with SVSClass.
Basic
- HTML
- CSS
- JavaScript
- TypeScript
With Interactive Variant
- HTML
- CSS
- JavaScript
- TypeScript