HeadlessCombobox
VueA renderless Vue 3 component with a scoped slot. Bind v-model for a controlled value, or omit it and let the component keep the selection.
Install
pnpm add @pdanpdan/headless-combobox-vue
Binding
The component emits update:modelValue; v-model writes it back. The slot scope contains refs.
<HeadlessCombobox
v-slot="scope"
v-model="value"
:options="options"
>
<div :ref="scope.setContainerRef">
<button :ref="scope.setTriggerRef" v-bind="scope.triggerProps">
{{ value ?? 'Select…' }}
</button>
<ul v-if="scope.isOpen" v-bind="scope.listboxProps">
<li
v-for="(option, index) in scope.filteredOptions"
:key="option"
v-bind="scope.getOptionProps(option, index)"
>
{{ option }}
</li>
</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 v-bind.
Trigger + popup: a button opens a list below it. Most examples use this layout.
HeadlessCombobox (default slot scope)
└─ div :ref="setContainerRef" widget boundary (optional)
├─ button :ref="setTriggerRef" the trigger
│ │ v-bind="triggerProps"
│ └─ selected label
├─ ul :ref="setDropdownRef" the popup
│ │ v-bind="listboxProps" :style="popupStyle"
│ │ popover="manual" optional: native Popover API
│ └─ li v-for="(option, i) in filteredOptions"
│ └─ button :ref="setOptionRef(option, el)" one option
│ v-bind="getOptionProps(option, i)"
└─ input :ref="setInputRef" search input (optional)
v-bind="inputProps"Typeahead: the text input itself is the combobox. Typing reopens the popup and filters.
HeadlessCombobox (default slot scope)
└─ div :ref="setContainerRef" widget boundary (optional)
├─ input :ref="(el) => { setTriggerRef(el); setInputRef(el) }"
│ │ v-bind="comboboxInputProps" the combobox input
└─ ul :ref="setDropdownRef" the popup
│ v-bind="listboxProps" :style="popupStyle"
│ popover="manual" optional: native Popover API
└─ li v-for="(option, i) in filteredOptions"
└─ button :ref="setOptionRef(option, el)" one option
v-bind="getOptionProps(option, i)"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 and the code use the same Vue component.
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 and useHeadlessCombobox use the same state machine. The component is a thin wrapper that renders its default slot with the scope; the composable returns that scope directly. Use the composable for programmatic control, wrapper components, or layouts that cannot use a slot.
Props accept plain values, refs, or getters. Pass the modelValue ref directly; it is unwrapped and tracked internally. The second argument receives every update:modelValue payload.
The return value mirrors the slot scope: refs for state, plain functions for actions and ref setters.
<script setup lang="ts">
import { useHeadlessCombobox } from '@pdanpdan/headless-combobox-vue';
import { ref } from 'vue';
interface User {
id: number;
name: string;
}
const users: User[] = [
{ id: 1, name: 'Wade Cooper' },
{ id: 2, name: 'Arlene Mccoy' },
];
const selected = ref<User | null>(null);
const {
isOpen,
filteredOptions,
comboboxInputProps,
listboxProps,
getOptionProps,
select,
setContainerRef,
setTriggerRef,
setInputRef,
setDropdownRef,
setListRef,
setOptionRef,
} = useHeadlessCombobox<User>(
{
modelValue: selected,
options: users,
optionLabel: (user: User) => user.name,
},
(value) => {
selected.value = value as User | null;
},
);
</script>
<template>
<div :ref="setContainerRef">
<input
:ref="(el) => { setTriggerRef(el); setInputRef(el); }"
v-bind="comboboxInputProps"
aria-label="Search users"
type="text"
/>
<ul
v-if="isOpen"
:ref="(el) => { setDropdownRef(el); setListRef(el); }"
v-bind="listboxProps"
>
<li
v-for="(user, index) in filteredOptions"
:key="user.id"
>
<button
:ref="(el) => setOptionRef(user, el)"
type="button"
v-bind="getOptionProps(user, index)"
@click="select(user)"
>
{{ user.name }}
</button>
</li>
</ul>
</div>
</template>
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 |
|---|---|---|---|
| v-model | V | V[] | null | internal | Binds modelValue and writes update:modelValue back to your state. |
| modelValue | V | V[] | null | internal | Selected value(s). Omit it to let the component keep the selection internally. |
| 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. |
Emits
| Name | Type | Description |
|---|---|---|
| update:modelValue | V | V[] | null | Emitted when the selection changes. v-model writes it back to the bound ref. |
Scope
The component renders its default slot with this scope. State entries are refs; actions and ref setters are plain functions.
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 `v-bind`.
| 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. |