Combobox
Features
- Can be controlled or uncontrolled.
- Offers 2 positioning modes.
- Supports items, labels, groups of items.
- Focus is fully managed.
- Full keyboard navigation.
- Supports custom placeholder.
- Supports Right to Left direction.
Installation
Install the component from your command line.
$ npm add reka-uiAnatomy
Import all parts and piece them together.
<script setup lang="ts">
import {
ComboboxAnchor,
ComboboxArrow,
ComboboxCancel,
ComboboxContent,
ComboboxEmpty,
ComboboxGroup,
ComboboxInput,
ComboboxItem,
ComboboxItemIndicator,
ComboboxLabel,
ComboboxPortal,
ComboboxRoot,
ComboboxSeparator,
ComboboxTrigger,
ComboboxViewport,
} from 'reka-ui'
</script>
<template>
<ComboboxRoot>
<ComboboxAnchor>
<ComboboxInput />
<ComboboxTrigger />
<ComboboxCancel />
</ComboboxAnchor>
<ComboboxPortal>
<ComboboxContent>
<ComboboxViewport>
<ComboboxEmpty />
<ComboboxItem>
<ComboboxItemIndicator />
</ComboboxItem>
<ComboboxSeparator />
<ComboboxGroup>
<ComboboxLabel />
<ComboboxItem>
<ComboboxItemIndicator />
</ComboboxItem>
</ComboboxGroup>
</ComboboxViewport>
<ComboboxArrow />
</ComboboxContent>
</ComboboxPortal>
</ComboboxRoot>
</template>API Reference
Root
Contains all the parts of a Combobox
Anchor
Used as an anchor if you set ComboboxContent's position to popper.
Input
The input component to search through the combobox items.
Trigger
The button that toggles the Combobox Content.
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
Cancel
The button that clears the search term.
Empty
Shown when none of the items match the query.
Portal
When used, portals the content part into the body.
You need to set position="popper" for ComboboxContent to make sure the position was automatically computed similar to Popover or DropdownMenu.
Content
The component that pops out when the combobox is open.
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
[data-side] | "left" | "right" | "bottom" | "top" |
[data-align] | "start" | "end" | "center" |
[data-empty] | Present when there are no items matching the filter |
| CSS Variable | Description |
|---|---|
--reka-combobox-content-transform-origin | The transform-origin computed from the content and arrow positions/offsets. Only present when position="popper". |
--reka-combobox-content-available-width | The remaining width between the trigger and the boundary edge. Only present when position="popper". |
--reka-combobox-content-available-height | The remaining height between the trigger and the boundary edge. Only present when position="popper". |
--reka-combobox-trigger-width | The width of the trigger. Only present when position="popper". |
--reka-combobox-trigger-height | The height of the trigger. Only present when position="popper". |
Viewport
The scrolling viewport that contains all of the items.
Item
The component that contains the combobox items.
| Data Attribute | Value |
|---|---|
[data-state] | "checked" | "unchecked" |
[data-highlighted] | Present when highlighted |
[data-disabled] | Present when disabled |
ItemIndicator
Renders when the item is selected. You can style this element directly, or you can use it as a wrapper to put an icon into, or both.
Group
Used to group multiple items. use in conjunction with ComboboxLabel to ensure good accessibility via automatic labelling.
Label
Used to render the label of a group. It won't be focusable using arrow keys.
Separator
Used to visually separate items in the Combobox
Arrow
An optional arrow element to render alongside the content. This can be used to help visually link the trigger with the ComboboxContent. Must be rendered inside ComboboxContent. Only available when position is set to popper.
Virtualizer
Virtual container to achieve list virtualization.
The root's filter runs over options before they are virtualized, matching textContent(option) (or the option's string form). Pass text-content when the options are objects, or set :filter="null" on ComboboxRoot when you filter options yourself. See example below.
See the virtualization guide for more general info on virtualization.
Examples
Binding objects as values
Unlike native HTML form controls which only allow you to provide strings as values, reka-ui supports binding complex objects as well.
Make sure to set the displayValue prop to set the input value on item selection. Set it on ComboboxRoot or on ComboboxInput (the input's own prop wins).
<script setup lang="ts">
import { ComboboxContent, ComboboxInput, ComboboxItem, ComboboxPortal, ComboboxRoot } from 'reka-ui'
import { ref } from 'vue'
const people = [
{ id: 1, name: 'Durward Reynolds' },
{ id: 2, name: 'Kenton Towne' },
{ id: 3, name: 'Therese Wunsch' },
{ id: 4, name: 'Benedict Kessler' },
{ id: 5, name: 'Katelyn Rohan' },
]
const selectedPeople = ref(people[0])
</script>
<template>
<ComboboxRoot v-model="selectedPeople">
<ComboboxInput :display-value="(v) => v.name" />
<ComboboxPortal>
<ComboboxContent>
<ComboboxItem
v-for="person in people"
:key="person.id"
:value="person"
:disabled="person.unavailable"
>
{{ person.name }}
</ComboboxItem>
</ComboboxContent>
</ComboboxPortal>
</ComboboxRoot>
</template>Selecting multiple values
The Combobox component allows you to select multiple values. You can enable this by providing an array of values instead of a single value.
<script setup lang="ts">
import { ComboboxRoot } from 'reka-ui'
import { ref } from 'vue'
const people = [
{ id: 1, name: 'Durward Reynolds' },
{ id: 2, name: 'Kenton Towne' },
{ id: 3, name: 'Therese Wunsch' },
{ id: 4, name: 'Benedict Kessler' },
{ id: 5, name: 'Katelyn Rohan' },
]
const selectedPeople = ref([people[0], people[1]])
</script>
<template>
<ComboboxRoot
v-model="selectedPeople"
multiple
>
…
</ComboboxRoot>
</template>Custom filtering
ComboboxRoot filters the items by the text typed in ComboboxInput. Each item is matched on its textValue, or on its rendered text, with useFilter's contains (case and accent insensitive). A non-matching item stays mounted but renders nothing, so keyboard navigation skips it, and a ComboboxGroup whose items all fail to match is hidden.
Pass a filter function to change how items match. It is called as filter(text, search, value) and returns whether the item matches. startsWith, endsWith and contains from useFilter already have that signature.
<script setup lang="ts">
import { ComboboxContent, ComboboxInput, ComboboxItem, ComboboxPortal, ComboboxRoot, useFilter } from 'reka-ui'
const { startsWith } = useFilter({ sensitivity: 'base' })
</script>
<template>
<ComboboxRoot :filter="startsWith">
<ComboboxInput />
<ComboboxPortal>
<ComboboxContent>
<ComboboxItem
v-for="fruit in ['Apple', 'Banana', 'Blueberry']"
:key="fruit"
:value="fruit"
>
{{ fruit }}
</ComboboxItem>
</ComboboxContent>
</ComboboxPortal>
</ComboboxRoot>
</template>To filter the items yourself (or on a server), set :filter="null" and render the filtered list. ignore-filter still works as an alias for :filter="null", but it is deprecated.
<script setup lang="ts">
import { ComboboxContent, ComboboxInput, ComboboxItem, ComboboxPortal, ComboboxRoot, useFilter } from 'reka-ui'
import { computed, ref } from 'vue'
const people = [
{ id: 1, name: 'Durward Reynolds' },
{ id: 2, name: 'Kenton Towne' },
{ id: 3, name: 'Therese Wunsch' },
{ id: 4, name: 'Benedict Kessler' },
{ id: 5, name: 'Katelyn Rohan' },
]
const selectedPeople = ref(people[0])
const searchTerm = ref('')
const { startsWith } = useFilter({ sensitivity: 'base' })
const filteredPeople = computed(() => people.filter(p => startsWith(p.name, searchTerm.value)))
</script>
<template>
<ComboboxRoot
v-model="selectedPeople"
:filter="null"
>
<ComboboxInput v-model="searchTerm" />
<ComboboxPortal>
<ComboboxContent>
<ComboboxItem
v-for="person in filteredPeople"
:key="person.id"
:value="person"
>
{{ person.name }}
</ComboboxItem>
</ComboboxContent>
</ComboboxPortal>
</ComboboxRoot>
</template>Custom label
By default the Combobox will use the input contents as the label for screenreaders. If you'd like more control over what is announced to assistive technologies, use the Label component.
<script setup lang="ts">
import { ComboboxInput, ComboboxRoot, Label } from 'reka-ui'
import { ref } from 'vue'
</script>
<template>
<ComboboxRoot v-model="selectedPeople">
<Label for="person">Person: </Label>
<ComboboxInput
id="person"
placeholder="Select a person"
/>
…
</ComboboxRoot>
</template>With disabled items
You can add special styles to disabled items via the data-disabled attribute.
<script setup lang="ts">
import {
ComboboxContent,
ComboboxInput,
ComboboxItem,
ComboboxPortal,
ComboboxRoot,
} from 'reka-ui'
import { ref } from 'vue'
</script>
<template>
<ComboboxRoot>
<ComboboxInput />
<ComboboxPortal>
<ComboboxContent>
<ComboboxItem
class="ComboboxItem"
disabled
>
…
</ComboboxItem>
</ComboboxContent>
</ComboboxPortal>
</ComboboxRoot>
</template>/* styles.css */
.ComboboxItem[data-disabled] {
color: "gainsboro";
}With separators
Use the Separator part to add a separator between items.
<script setup lang="ts">
import {
ComboboxContent,
ComboboxInput,
ComboboxItem,
ComboboxPortal,
ComboboxRoot,
ComboboxSeparator
} from 'reka-ui'
import { ref } from 'vue'
</script>
<template>
<ComboboxRoot>
<ComboboxInput />
<ComboboxPortal>
<ComboboxContent>
<ComboboxItem>…</ComboboxItem>
<ComboboxItem>…</ComboboxItem>
<ComboboxItem>…</ComboboxItem>
<ComboboxSeparator />
<ComboboxItem>…</ComboboxItem>
<ComboboxItem>…</ComboboxItem>
<ComboboxItem>…</ComboboxItem>
</ComboboxContent>
</ComboboxPortal>
</ComboboxRoot>
</template>With grouped items
Use the Group and Label parts to group items in a section.
<script setup lang="ts">
import {
ComboboxContent,
ComboboxGroup,
ComboboxInput,
ComboboxItem,
ComboboxLabel,
ComboboxPortal,
ComboboxRoot
} from 'reka-ui'
import { ref } from 'vue'
</script>
<template>
<ComboboxRoot>
<ComboboxInput />
<ComboboxPortal>
<ComboboxContent>
<ComboboxGroup>
<ComboboxLabel>Label</ComboboxLabel>
<ComboboxItem>…</ComboboxItem>
<ComboboxItem>…</ComboboxItem>
<ComboboxItem>…</ComboboxItem>
</ComboboxGroup>
</ComboboxContent>
</ComboboxPortal>
</ComboboxRoot>
</template>With complex items
You can use custom content in your items.
<script setup lang="ts">
import {
ComboboxContent,
ComboboxGroup,
ComboboxInput,
ComboboxItem,
ComboboxItemIndicator,
ComboboxLabel,
ComboboxPortal,
ComboboxRoot
} from 'reka-ui'
import { ref } from 'vue'
</script>
<template>
<ComboboxRoot>
<ComboboxInput />
<ComboboxPortal>
<ComboboxContent>
<ComboboxItem>
<img src="…">
Adolfo Hess
<ComboboxItemIndicator />
</ComboboxItem>
…
</ComboboxContent>
</ComboboxPortal>
</ComboboxRoot>
</template>Reacting to changes
update:modelValue and update:open receive a second argument with the reason for the change and the native event. beforeUpdate:modelValue and beforeUpdate:open fire first, and details.cancel() keeps the current state. Cancelling the value of an item press keeps the popup open; cancelling only the close keeps it open with the new value. The reasons are exported as ComboboxChangeReason ('item-press' | 'clear-press', plus 'item-keydown' | 'select-all' | 'range-select' in a multiple combobox) and ComboboxOpenChangeReason ('trigger-press' | 'input-change' | 'input-press' | 'input-focus' | 'list-navigation' | 'item-press' | 'escape-key' | 'outside-press' | 'focus-outside'), plus 'imperative-action' for programmatic changes.
<template>
<ComboboxRoot
v-model="selected"
reset-model-value-on-clear
@before-update:model-value="(value, details) => {
// a required field: keep the selection when the cancel button clears it
if (details.reason === 'clear-press')
details.cancel()
}"
>
<!-- ... -->
</ComboboxRoot>
</template>useComboboxRoot() is the headless composable behind ComboboxRoot. It returns the same models, the search term and filter state, and the role / aria-* / data-* attributes for the input, trigger, content and items.
Prevent select behavior
By default, selecting ComboboxItem would close the content, and update the modelValue with the provided value. You can prevent this behavior by preventing default @select.prevent.
<script setup lang="ts">
import { ComboboxContent, ComboboxGroup, ComboboxInput, ComboboxItem, ComboboxItemIndicator, ComboboxLabel, ComboboxPortal, ComboboxRoot } from 'reka-ui'
import { ref } from 'vue'
</script>
<template>
<ComboboxRoot>
<ComboboxInput />
<ComboboxPortal>
<ComboboxContent>
<ComboboxItem @select.prevent>
Item A
</ComboboxItem>
…
</ComboboxContent>
</ComboboxPortal>
</ComboboxRoot>
</template>Virtualized combobox with filtering
The root's filter runs over the virtualizer's options, matching textContent(option), so a filtered-out option takes no row and is skipped by keyboard navigation. Pass the full list and a text-content function. To filter options yourself, set :filter="null" on ComboboxRoot and pass the filtered list.
See the virtualization guide for more general info on virtualization.
<script setup lang="ts">
import { ComboboxContent, ComboboxInput, ComboboxItem, ComboboxPortal, ComboboxRoot, ComboboxViewport, ComboboxVirtualizer } from 'reka-ui'
import { ref } from 'vue'
const people = Array.from({ length: 100000 }).map((_, id) => ({ id, name: `Person #${id}` }))
const selectedPeople = ref(people[0])
</script>
<template>
<ComboboxRoot v-model="selectedPeople">
<ComboboxInput :display-value="(person) => person?.name ?? ''" />
<ComboboxPortal>
<ComboboxContent class="max-h-[40vh] overflow-hidden">
<ComboboxViewport>
<ComboboxVirtualizer
v-slot="{ option }"
:options="people"
:text-content="(x) => x.name"
:estimate-size="24"
>
<ComboboxItem :value="option">
{{ option.name }}
</ComboboxItem>
</ComboboxVirtualizer>
</ComboboxViewport>
</ComboboxContent>
</ComboboxPortal>
</ComboboxRoot>
</template>Large lists
When the data your v-for reads changes, Vue re-renders every item of that v-for, because each item's slot may read the new data. With hundreds of items, add v-memo to the v-for so an item re-renders only when its own values change:
<ComboboxItem v-for="item in items" :key="item.value" v-memo="[item.value, item.label]" :value="item.value">
{{ item.label }}
</ComboboxItem>v-memo must list every value the item's props and slot read, such as :disabled. A value it leaves out goes stale when only that value changes. The selected and highlighted states don't need listing: the item tracks those itself. For very long lists, see the virtualization example above.
Real-world examples
- Combobox Async: Search a remote endpoint from a Combobox, with debounced requests, a loading state, and out-of-order responses discarded.
- Combobox Virtualized: Render a Combobox over 10,000 options by mounting only the rows in view with
ComboboxVirtualizer. - Combobox TagsInput: Rendering Combobox with Tags Input.
- Inline Combobox on Textarea: Rendering Combobox as a textarea element to create an accessible multiline textbox. Inserting specific characters triggers a popup with dynamic suggestions.
- Dialog Command Menu: Combining Dialog and Combobox to enable users to search a command list in a modal.
- Command Menu with Tabs: A command palette whose results are scoped by Tabs, with Tab cycling scopes without leaving the search field.
Accessibility
Adheres to the Combobox WAI-ARIA design pattern.
See the W3C Combobox Autocomplete List example for more information.
Keyboard Interactions
| Key | Description |
|---|---|
Enter | When focus is on ComboboxItem, selects the focused item. |
ArrowDown | When focus is on ComboboxInput, opens the combobox content. When focus is on an item, moves focus to the next item. |
ArrowUp | When focus is on ComboboxInput, opens the combobox content. When focus is on an item, moves focus to the previous item. |
Esc | Closes combobox and restores the selected item in the ComboboxInput field. |
Custom APIs
Create your own API by abstracting the primitive parts into your own component.
Command Menu
Combobox can be use to build your own Command Menu.
Usage
<script setup lang="ts">
import { Command, CommandItem } from './your-command'
</script>
<template>
<Command>
<CommandItem value="1">
Item 1
</CommandItem>
<CommandItem value="2">
Item 2
</CommandItem>
<CommandItem value="3">
Item 3
</CommandItem>
</Command>
</template>Implementation
// your-command.ts
export { default as Command } from 'Command.vue'
export { default as CommandItem } from 'CommandItem.vue'<!-- Command.vue -->
<script setup lang="ts">
import type { ComboboxRootEmits, ComboboxRootProps } from 'reka-ui'
import { CheckIcon, ChevronDownIcon, ChevronUpIcon, } from '@radix-icons/vue'
import { ComboboxContent, ComboboxInput, ComboboxPortal, ComboboxRoot, useForwardPropsEmits } from 'reka-ui'
const props = defineProps<ComboboxRootProps>()
const emits = defineEmits<ComboboxRootEmits>()
const forward = useForwardPropsEmits(props, emits)
</script>
<template>
<ComboboxRoot
v-bind="forward"
:open="true"
model-value=""
>
<ComboboxInput placeholder="Type a command or search…" />
<ComboboxPortal>
<ComboboxContent
@escape-key-down.prevent
@focus-outside.prevent
@interact-outside.prevent
@pointer-down-outside.prevent
>
<ComboboxViewport>
<slot />
</ComboboxViewport>
</ComboboxContent>
</ComboboxPortal>
</ComboboxRoot>
</template><!-- ComboboxItem.vue -->
<script setup lang="ts">
import type { ComboboxItemProps } from 'reka-ui'
import { CheckIcon } from '@radix-icons/vue'
import { ComboboxItem } from 'reka-ui'
const props = defineProps<ComboboxItemProps>()
</script>
<template>
<ComboboxItem
v-bind="props"
@select.prevent
>
<slot />
</ComboboxItem>
</template>Headless composable
useComboboxRoot builds on the Listbox model: it adds the open state (open, onOpenChange()), the search term and its filtering (filter, ignoreFilter, filterSearch), the reset rules (resetSearchTermOnBlur, resetSearchTermOnSelect, resetModelValueOnClear), and the root, listbox, trigger, cancel and content part surfaces with createInputSurface() for the input. Positioning, the portal and dismissal stay in the components. It registers watchers, so call it inside setup(). The composable is experimental; see Headless composables.
