HeadlessCombobox
SolidA render-prop component for Solid 2. The render prop receives an accessor, so call scope() inside JSX to keep bindings reactive.
Install
pnpm add @pdanpdan/headless-combobox-solid
Binding
The component and useHeadlessCombobox return an accessor. Call it inside JSX, not once at the top.
<HeadlessCombobox
options={options}
value={value()}
onChange={setValue}
>
{(scope) => (
<div ref={scope().setContainerRef}>
<button ref={scope().setTriggerRef} {...toSolidProps(scope().triggerProps)}>
{value() ?? 'Select…'}
</button>
{scope().isOpen && (
<ul {...toSolidProps(scope().listboxProps)}>
<For each={scope().filteredOptions}>
{(option, index) => (
<li {...toSolidProps(scope().getOptionProps(option, index()))}>
{option}
</li>
)}
</For>
</ul>
)}
</div>
)}
</HeadlessCombobox>
Structure
Each part of the markup receives a piece of the scope. Bind only the parts you render. Spread a prop bag with toSolidProps.
Trigger + popup: a button opens a list below it. Most examples use this layout.
HeadlessCombobox (scope)
└─ div ref={scope().setContainerRef} widget boundary (optional)
├─ button ref={scope().setTriggerRef} the trigger
│ │ {...toSolidProps(scope().triggerProps)}
│ └─ selected label
├─ div ref={scope().setDropdownRef} the popup
│ │ style={scope().popupStyle} popover="manual"
│ └─ ul {...toSolidProps(scope().listboxProps)}
│ └─ <For each={scope().filteredOptions}>
│ {(option, i) => <li {...toSolidProps(scope().getOptionProps(option, i()))}
│ ref={(el) => scope().setOptionRef(option, el)} />}
└─ input ref={scope().setInputRef} search input (optional)
{...toSolidProps(scope().inputProps)}Typeahead: the text input itself is the combobox. Typing reopens the popup and filters.
HeadlessCombobox (scope)
└─ div ref={scope().setContainerRef} widget boundary (optional)
├─ input ref={(el) => {
│ scope().setTriggerRef(el);
│ scope().setInputRef(el);
│ }}
│ {...toSolidProps(scope().comboboxInputProps)} the combobox input
└─ div ref={scope().setDropdownRef} the popup
│ style={scope().popupStyle} popover="manual"
└─ ul {...toSolidProps(scope().listboxProps)}
└─ <For each={scope().filteredOptions}>
{(option, i) => <li {...toSolidProps(scope().getOptionProps(option, i()))}
ref={(el) => scope().setOptionRef(option, el)} />}Markup → scope
| Name | Type | Description |
|---|---|---|
| wrapper element | setContainerRef | Widget boundary for click-outside and focus. Optional, recommended. |
| trigger button | triggerProps, setTriggerRef | role="combobox". Toggles the popup and handles keyboard navigation. |
| typeahead input | comboboxInputProps, setTriggerRef, setInputRef | The input is the combobox: typing reopens and filters. |
| search input | inputProps, setInputRef | role="searchbox". Filters while the popup stays open. |
| popup element | popupStyle, setDropdownRef | Positioning; add popover="manual" to use the native Popover API. |
| list element | listboxProps, setListRef | role="listbox". The options container. |
| each option element | getOptionProps(option, index), setOptionRef(option, el) | Option semantics and select/highlight handlers. |
Ref setters are optional. setOptionRef enables alignSelected and scroll-into-view; setContainerRef extends the click-outside boundary, for example to chips outside the field.
For CSS anchor positioning and the popover entry animation, see Styling.
Examples
The preview runs the Vue implementation. The code tab shows the same example for Solid.
The preview uses the native Popover API. Keyboard and hover share the same highlight, and the selected option is marked with a check.
Text options · single · no filter
A list of plain string options, single selection, no search input.
Text options · single · internal state
An initial value seeds the internal selection; without a change handler the component keeps it internally.
Text options · single · select on tab
Press Tab with an option highlighted: it is selected and the popup closes (selectOnTab).
Object options · single · with filter
Object options with a searchable input.
Object options · single · separate value
Options carry a label and a value; the model holds the value only.
Text options · multiple · with filter
Multiple selection capped with max; selecting toggles, dropdown stays open.
Text options · multiple · removable chips
Selected items as removable chips. The remove buttons live outside the combobox trigger, so the trigger stays a single focusable control.
Text options · multiple · custom options
Each option renders its own checkbox for the selection state; the trigger shows the selected items as chips.
Object options · multiple · validated
Multiple selection with required + min/max validation and a rendered message.
Select alignment
No search input; the dropdown aligns so the selected option overlays the trigger.
Typeahead (editable combobox)
Canonical APG editable pattern: the text input itself is the combobox (role=combobox); type to filter.
Typeahead · chips inside the field
GitHub-style topic input: chips and the text input share one bordered field. The field is a plain container, so each chip can carry a remove button without nesting controls.
Composable · single · typeahead
The same behavior without the component. useHeadlessCombobox exposes the state, ARIA bags, and actions; wire the markup with plain refs.
Composable · programmatic control
open, close, select, and clear are plain functions, so the buttons outside the widget can call them. clickOutsideFilter keeps the popup open while the panel is used.
Drive it from anywhere:
Selected: none · popup closed
useHeadlessCombobox
The component wraps useHeadlessCombobox. Use the hook when you do not need the render-prop wrapper.
Pass an options accessor. value is controlled; defaultValue seeds an uncontrolled store. onChange receives every change.
The hook returns an accessor. Call scope() inside JSX so bindings stay reactive; spread bags with toSolidProps.
import { For, createSignal } from 'solid-js';
import { toSolidProps, useHeadlessCombobox } from '@pdanpdan/headless-combobox-solid';
interface User {
id: number;
name: string;
}
const users: User[] = [
{ id: 1, name: 'Wade Cooper' },
{ id: 2, name: 'Arlene Mccoy' },
];
export function Example() {
const [ value, setValue ] = createSignal<User | null>(null);
const scope = useHeadlessCombobox(() => ({
options: users,
value: value(),
optionLabel: (user) => user.name,
onChange: (next) => setValue(next as User | null),
}));
return (
<div ref={scope().setContainerRef}>
<input
ref={(el) => {
scope().setTriggerRef(el);
scope().setInputRef(el);
}}
{...toSolidProps(scope().comboboxInputProps)}
aria-label="Search users"
/>
{scope().isOpen && (
<ul {...toSolidProps(scope().listboxProps)}>
<For each={scope().filteredOptions}>
{(user, index) => (
<li
ref={(el) => scope().setOptionRef(user, el)}
{...toSolidProps(scope().getOptionProps(user, index()))}
>
<button type="button" onClick={() => scope().select(user)}>
{user.name}
</button>
</li>
)}
</For>
</ul>
)}
</div>
);
}
The Composable · single · typeahead and Composable · programmatic control examples above show full usage.
API
Props
These are component and hook options. The value binding is listed first.
| Name | Type | Default | Description |
|---|---|---|---|
| value | V | V[] | null | n/a | Controlled value. Omit it for uncontrolled usage. |
| defaultValue | V | V[] | null | null | Initial value when value is omitted. |
| onChange | (value: V | V[] | null) => void | n/a | Called for every selection change. |
| options | O[] | n/a | List of options. |
| multiple | boolean | false | Enable multiple selection. |
| minLength | number | n/a | Multiple: minimum number of selected options (validation). |
| maxLength | number | n/a | Multiple: maximum selected. Blocks adding beyond it. |
| required | boolean | false | Require a selection (single: a value; multiple: at least one). |
| disabled | boolean | false | Disable the control: not focusable, cannot open or change. |
| readonly | boolean | false | Read-only: focusable, shows the value, but cannot open or change. |
| closeOnSelect | boolean | null | !multiple | Close the dropdown after selecting. |
| closeOnClickOutside | boolean | true | Close when clicking outside the widget boundary. |
| clickOutsideFilter | (target: EventTarget | null) => boolean | n/a | Return false to keep the dropdown open for an outside target. |
| selectOnTab | boolean | false | On Tab, when focus leaves the widget: select the highlighted option and close. |
| optionValue | (option: O) => V | option itself | Maps an option to the value stored in the model. |
| optionLabel | (option: O) => string | String(option) | Maps an option to a string for filtering and rendering. |
| optionFilter | (option: O, query: Q) => boolean | substring | Custom filter function. Q defaults to string. |
| id | string | generated | Base id for accessibility attributes. |
| alignSelected | boolean | false | Align the dropdown so the selected option covers the trigger. |
| errorMessages | Partial<Record<HeadlessComboboxErrorCode, string>> | n/a | Override default validation messages. |
Scope
The render prop receives an accessor. Call scope() to read these values; the hook returns the same accessor.
State
Live values for the current selection and popup.
| Name | Type | Description |
|---|---|---|
| isOpen | boolean | Whether the dropdown is open. |
| multiple | boolean | Whether multiple selection is enabled. |
| disabled | boolean | Whether the control is disabled. |
| readonly | boolean | Whether the control is read-only. |
| searchQuery | Q | undefined | Current search query. Q defaults to string. |
| filteredOptions | T[] | Options after applying the filter. |
| highlightedIndex | number | Index of the highlighted option (-1 when none). |
| alignmentOffset | number | Pixel offset used for alignment. |
| cssAnchorName | string | Unique CSS anchor-name for popover positioning. |
| popupStyle | HeadlessComboboxPopupStyle | Default popup positioning style. Spread or merge it onto the dropdown. |
| selectedCount | number | Number of selected options. |
| selectedList | V[] | Selected values (single mode: a single-element array). |
| canSelectMore | boolean | False when at maxLength (multiple). |
| isSelected | (option: T) => boolean | Whether an option is selected. |
| valid | boolean | Whether the current selection passes validation. |
| errors | HeadlessComboboxErrorCode[] | Active validation errors ('required' | 'minlength' | 'maxlength'). |
| validationMessage | string | Human-readable message for the first error. |
ARIA prop bags
Spread a prop bag with `toSolidProps`.
| Name | Type | Description |
|---|---|---|
| triggerProps | HeadlessComboboxTriggerProps | role=combobox, aria-expanded, aria-activedescendant, toggle and keydown handlers. |
| inputProps | HeadlessComboboxInputProps | Search input attributes, aria-activedescendant, and keydown handling. |
| comboboxInputProps | HeadlessComboboxComboboxInputProps | Typeahead input attributes, open and focus handlers. |
| listboxProps | HeadlessComboboxListboxProps | role=listbox and aria-multiselectable. |
| getOptionProps | (option, index) => HeadlessComboboxOptionProps | role=option, aria-selected, aria-disabled, and select and highlight handlers. |
Actions
Methods that drive the combobox.
| Name | Type | Description |
|---|---|---|
| toggle | () => void | Toggle the dropdown open or closed. |
| open | () => void | Open the dropdown. |
| close | (returnFocus?: boolean) => void | Close the dropdown. |
| select | (option: T) => void | Select (single) or toggle (multiple) an option. |
| clear | () => void | Clear the selection (null or []). |
| setSearchQuery | (value: Q | undefined) => void | Update the search query. |
| setHighlightedIndex | (index: number) => void | Set the highlighted option. Wire it to hover to match keyboard behavior. |
| focusInput | () => void | Focus the filter input while the popup stays open. |
| handleKeydown | (event: KeyboardEvent) => void | Keyboard navigation handler. |
Ref setters
Attach elements to the store. Bind only the elements your markup uses.
| Name | Type | Description |
|---|---|---|
| setContainerRef | ref fn | The root container element. |
| setTriggerRef | ref fn | The trigger button or typeahead input. |
| setDropdownRef | ref fn | The dropdown popup element. |
| setInputRef | ref fn | The search input element. |
| setListRef | ref fn | The options list element. |
| setOptionRef | (option, el) => void | Each option element. Needed for alignSelected and scroll-into-view. |