Combobox

PohonGitHub
Choose from a list of suggested values with full keyboard support.
<script setup lang="ts">
import { AComboboxAnchor, AComboboxContent, AComboboxEmpty, AComboboxGroup, AComboboxInput, AComboboxItem, AComboboxItemIndicator, AComboboxLabel, AComboboxPortal, AComboboxRoot, AComboboxSeparator, AComboboxTrigger, AComboboxViewport } from 'akar';

const options = [
  {
    name: 'Fruit',
    children: [
      { name: 'Apple' },
      { name: 'Banana' },
      { name: 'Orange' },
      { name: 'Honeydew' },
      { name: 'Grapes' },
      { name: 'Watermelon' },
      { name: 'Cantaloupe' },
      { name: 'Pear' },
    ],
  },
  {
    name: 'Vegetable',
    children: [
      { name: 'Cabbage' },
      { name: 'Broccoli' },
      { name: 'Carrots' },
      { name: 'Lettuce' },
      { name: 'Spinach' },
      { name: 'Bok Choy' },
      { name: 'Cauliflower' },
      { name: 'Potatoes' },
    ],
  },
];
</script>

<template>
  <AComboboxRoot
    class="inline-flex items-center relative"
  >
    <AComboboxAnchor
      class="text-sm color-text-highlighted px-2.5 py-1.5 pe-9 border-0 rounded-md bg-background gap-1.5 w-full ring ring-ring-accented ring-inset transition-colors-280 placeholder:color-text-dimmed focus:outline-none disabled:opacity-75 disabled:cursor-not-allowed focus-visible:(ring-2 ring-primary ring-inset)"
      as-child
    >
      <AComboboxInput
        placeholder="Placeholder..."
      />
      <AComboboxTrigger class="group pe-2.5 flex items-center end-0 inset-y-0 absolute disabled:opacity-75 disabled:cursor-not-allowed">
        <i
          class="i-lucide:chevron-down color-text-dimmed shrink-0 size-5"
        />
      </AComboboxTrigger>
    </AComboboxAnchor>

    <AComboboxPortal>
      <AComboboxContent
        side="bottom"
        :side-offset="8"
        position="popper"
        class="rounded-md bg-background flex flex-col max-h-60 w-$akar-combobox-trigger-width pointer-events-auto ring ring-ring shadow-lg origin-$akar-combobox-content-transform-origin overflow-hidden data-[state=closed]:(animate-out fade-out-0 zoom-out-95) data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95"
      >
        <AComboboxViewport class="flex-1 relative overflow-y-auto scroll-py-1">
          <AComboboxEmpty class="color-text-muted text-center" />
          <template
            v-for="(group, index) in options"
            :key="group.name"
          >
            <AComboboxGroup class="p-1 isolate">
              <AComboboxSeparator
                v-if="index !== 0"
                class="my-1 bg-border h-px -mx-1"
              />
              <AComboboxLabel class="text-xs color-text-highlighted font-semibold p-1.5 gap-1.5">
                {{ group.name }}
              </AComboboxLabel>
              <AComboboxItem
                v-for="option in group.children"
                :key="option.name"
                :value="option.name"
                class="group text-sm color-text p-1.5 outline-none flex gap-1.5 w-full cursor-pointer select-none transition-colors-280 items-start relative before:(rounded-md content-empty transition-colors-280 inset-px absolute -z-1) data-[disabled]:(opacity-75 cursor-not-allowed) data-[highlighted]:not-[[data-disabled]]:color-text-highlighted data-[highlighted]:not-[[data-disabled]]:before:bg-background-elevated/50"
              >
                <span class="flex flex-1 flex-col min-w-0">
                  {{ option.name }}
                </span>

                <span class="ms-auto inline-flex gap-1.5 items-center">
                  <AComboboxItemIndicator
                    as-child
                  >
                    <i class="i-lucide:check shrink-0 size-5" />
                  </AComboboxItemIndicator>
                </span>
              </AComboboxItem>
            </AComboboxGroup>
          </template>
        </AComboboxViewport>
      </AComboboxContent>
    </AComboboxPortal>
  </AComboboxRoot>
</template>

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.

Anatomy

Import all parts and piece them together.

<script setup lang="ts">
import {
  AComboboxAnchor,
  AComboboxArrow,
  AComboboxCancel,
  AComboboxContent,
  AComboboxGroup,
  AComboboxInput,
  AComboboxItem,
  AComboboxItemIndicator,
  AComboboxLabel,
  AComboboxPortal,
  AComboboxRoot,
  AComboboxSeparator,
  AComboboxTrigger,
  AComboboxViewport,
} from 'akar';
</script>

<template>
  <AComboboxRoot>
    <AComboboxAnchor>
      <AComboboxInput />
      <AComboboxTrigger />
      <AComboboxCancel />
    </AComboboxAnchor>

    <AComboboxPortal>
      <AComboboxContent>
        <AComboboxViewport>
          <AComboboxItem>
            <AComboboxItemIndicator />
          </AComboboxItem>

          <AComboboxGroup>
            <AComboboxLabel />
            <AComboboxItem>
              <AComboboxItemIndicator />
            </AComboboxItem>
          </AComboboxGroup>
          <AComboboxSeparator />
        </AComboboxViewport>

        <AComboboxArrow />
      </AComboboxContent>
    </AComboboxPortal>
  </AComboboxRoot>
</template>

Pohon

One benefit of using Akar is its flexibility and low-level control over the components. However, this also means that you may need to manually construct more complex UI elements by combining multiple Akar components together.

If you feel there's a lot of elements that needs to be constructed manually using Akar, consider using Pohon UI instead. It provides a higher-level abstraction over Akar components with pre-defined styles and behaviors that can help you build UIs faster.

API Reference

Root

Contains all the parts of a Combobox

Props

Prop Default Type
as'div'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

bystring | ((a: AcceptableValue, b: AcceptableValue) => boolean)

Use this to compare objects by a particular field, or pass your own comparison function for complete control over how objects are compared.

defaultOpenboolean

The open state of the combobox when it is initially rendered.
Use when you do not need to control its open state.

defaultValueAcceptableValue | AcceptableValue[]

The value of the listbox when initially rendered. Use when you do not need to control the state of the Listbox

dir'ltr' | 'rtl'

The reading direction of the listbox when applicable.
If omitted, inherits globally from ConfigProvider or assumes LTR (left-to-right) reading mode.

disabledboolean

When true, prevents the user from interacting with listbox

highlightOnHoverboolean

When true, hover over item will trigger highlight

ignoreFilterboolean

When true, disable the default filters

modelValueAcceptableValue | AcceptableValue[]
multipleboolean

Whether multiple options can be selected or not.

namestring

The name of the field. Submitted with its owning form as part of a name/value pair.

openboolean
openOnClickfalseboolean

Whether to open the combobox when the input is clicked

openOnFocusfalseboolean

Whether to open the combobox when the input is focused

requiredboolean

When true, indicates that the user must set the value before the owning form can be submitted.

resetSearchTermOnBlurtrueboolean

Whether to reset the searchTerm when the Combobox input blurred

resetSearchTermOnSelecttrueboolean

Whether to reset the searchTerm when the Combobox value is selected

Emits

Event Type
highlight[payload: { ref: HTMLElement; value: AcceptableValue; }]

Event handler when highlighted element changes.

update:modelValue[value: AcceptableValue]

Event handler called when the value changes.

update:open[value: boolean]

Event handler called when the open state of the combobox changes.

Slots

Slot Type
openboolean

The controlled open state of the Combobox. Can be binded with with v-model:open.

modelValueAcceptableValue | AcceptableValue[]

The controlled value of the listbox. Can be binded with with v-model.

Anchor

Used as an anchor if you set AComboboxContent's position to popper.

Props

Prop Default Type
as'div'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

referenceReferenceElement

The reference (or anchor) element that is being referred to for positioning.

If not provided will use the current component as anchor.

Input

The input component to search through the combobox items.

Props

Prop Default Type
as'input'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

autoFocusboolean

Focus on element when mounted.

disabledboolean

When true, prevents the user from interacting with listbox

displayValue((val: any) => string)

The display value of input for selected item. Does not work with multiple.

modelValuestring

The controlled value of the listbox. Can be binded with with v-model.

Emits

Event Type
update:modelValue[string]

Event handler called when the value changes.

Trigger

The button that toggles the Combobox Content.

Props

Prop Default Type
as'button'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

disabledboolean

When true, prevents the user from interacting with listbox

Data Attributes

Attribute Value
[data-state]'open' | 'closed'
[data-disabled]Present when disabled

Cancel

The button that clears the search term.

Props

Prop Default Type
as'button'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

Empty

Shown when none of the items match the query.

Props

Prop Default Type
as'div'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

Portal

When used, portals the content part into the body.

You need to set position="popper" for AComboboxContent to make sure the position was automatically computed similar to Popover or DropdownMenu.

Props

Prop Default Type
deferboolean

Defer the resolving of a Teleport target until other parts of the application have mounted (requires Vue 3.5.0+)

{@link https://vuejs.org/guide/built-ins/teleport.html#deferred-teleport}

disabledboolean

When true, prevents the user from interacting with listbox

forceMountboolean

Used to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries.

tostring | HTMLElement

Vue native teleport component prop :to

{@link https://vuejs.org/guide/built-ins/teleport.html#basic-usage}

Content

The component that pops out when the combobox is open.

Built with Presence component - supports any animation techniques while maintaining access to presence emitted events.

Props

Prop Default Type
as'div'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

align'start' | 'center' | 'end'

The preferred alignment against the trigger. May change when collisions occur.

alignFlipboolean

Flip alignment when colliding with boundary. May only occur when prioritizePosition is true.

alignOffsetnumber

An offset in pixels from the start or end alignment options.

arrowPaddingnumber

The padding between the arrow and the edges of the content. If your content has border-radius, this will prevent it from overflowing the corners.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

avoidCollisionsboolean

When true, overrides the side and align preferences to prevent collisions with boundary edges.

bodyLockboolean

The document.body will be lock, and scrolling will be disabled.

collisionBoundaryElement | (Element | null)[] | null

The element used as the collision boundary. By default this is the viewport, though you can provide additional element(s) to be included in this check.

collisionPaddingnumber | Partial<Record<Side, number>>

The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { top: 20, left: 20 }.

disableOutsidePointerEventsboolean

When true, hover/focus/click interactions will be disabled on elements outside the DismissableLayer. Users will need to click twice on outside elements to interact with them: once to close the DismissableLayer, and again to trigger the element.

disableUpdateOnLayoutShiftboolean

Whether to disable the update position for the content when the layout shifted.

forceMountboolean

Used to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries.

hideWhenDetachedboolean

Whether to hide the content when the trigger becomes fully occluded.

position'inline' | 'popper'

The positioning mode to use,
inline is the default and you can control the position using CSS.
popper positions content in the same way as our other primitives, for example Popover or DropdownMenu.

positionStrategy'fixed' | 'absolute'

The type of CSS position property to use.

prioritizePositionboolean

Force content to be position within the viewport.

Might overlap the reference element, which may not be desired.

referenceReferenceElement

The reference (or anchor) element that is being referred to for positioning.

If not provided will use the current component as anchor.

side'top' | 'right' | 'bottom' | 'left'

The preferred side of the trigger to render against when open. Will be reversed when collisions occur and avoidCollisions is enabled.

sideFlipboolean

Flip to the opposite side when colliding with boundary.

sideOffsetnumber

The distance in pixels from the trigger.

sticky'always' | 'partial'

The sticky behavior on the align axis. partial will keep the content in the boundary as long as the trigger is at least partially in the boundary whilst 'always' will keep the content in the boundary regardless.

updatePositionStrategy'always' | 'optimized'

Strategy to update the position of the floating element on every animation frame.

Emits

Event Type
escapeKeyDown[event: KeyboardEvent]
focusOutside[event: FocusOutsideEvent]
interactOutside[event: PointerDownOutsideEvent | FocusOutsideEvent]
pointerDownOutside[event: PointerDownOutsideEvent]

Data Attributes

Attribute Value
[data-state]'open' | 'closed'
[data-side]'left' | 'right' | 'bottom' | 'top'
[data-align]'start' | 'end' | 'center'

CSS Variables

Variable Description
--akar-combobox-content-transform-origin

The transform-origin computed from the content and arrow positions/offsets. Only present when position="popper"

--akar-combobox-content-available-width

The remaining width between the trigger and the boundary edge. Only present when position="popper"

--akar-combobox-content-available-height

The remaining height between the trigger and the boundary edge. Only present when position="popper"

--akar-combobox-trigger-width

The width of the trigger. Only present when position="popper"

--akar-combobox-trigger-height

The height of the trigger. Only present when position="popper"

Viewport

The scrolling viewport that contains all of the items.

Props

Prop Default Type
as'div'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

noncestring

Will add nonce attribute to the style tag which can be used by Content Security Policy.
If omitted, inherits globally from AConfigProvider.

Item

The component that contains the combobox items.

Props

Prop Default Type
as'div'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

disabledboolean

When true, prevents the user from interacting with listbox

textValuestring

A string representation of the item contents.

If the children are not plain text, then the textValue prop must also be set to a plain text representation, which will be used for autocomplete in the ComboBox.

value*AcceptableValue

The value given as data when submitted with a name.

Emits

Event Type
select[event: SelectEvent<AcceptableValue>]

Data Attributes

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 AComboboxLabel to ensure good accessibility via automatic labelling.

Props

Prop Default Type
as'div'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

Label

Used to render the label of a group. It won't be focusable using arrow keys.

Props

Prop Default Type
as'div'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

forstring

Separator

Used to visually separate items in the Combobox

Props

Prop Default Type
as'div'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

Arrow

An optional arrow element to render alongside the content. This can be used to help visually link the trigger with the AComboboxContent. Must be rendered inside AComboboxContent. Only available when position is set to popper.

Props

Prop Default Type
as'svg'APrimitiveAsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChildboolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

height5number

The height of the arrow in pixels.

roundedboolean

When true, render the rounded version of arrow. Do not work with as/asChild

width10number

The width of the arrow in pixels.

Virtualizer

Virtual container to achieve list virtualization.

Combobox items must be filtered manually before passing them over to the virtualizer. See example below.

See the virtualization guide for more general info on virtualization.

Props

Prop Default Type
estimateSizenumber

Estimated size (in px) of each item

options*AcceptableValue[]

List of items

overscannumber

Number of items rendered outside the visible area

textContent((option: AcceptableValue) => string)

Text content for each item to achieve type-ahead feature

Slots

Slot Type
optionnull | string | number | bigint | Record<string, any>
virtualizerVirtualizer<HTMLElement, Element>
virtualItemVirtualItem

Examples

Binding objects as values

Unlike native HTML form controls which only allow you to provide strings as values, akar supports binding complex objects as well.

Make sure to set the displayValue prop to set the input value on item selection.

<script setup lang="ts">
import { AComboboxContent, AComboboxInput, AComboboxItem, AComboboxPortal, AComboboxRoot } from 'akar';
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>
  <AComboboxRoot v-model="selectedPeople">
    <AComboboxInput :display-value="(v) => v.name" />
    <AComboboxPortal>
      <AComboboxContent>
        <AComboboxItem
          v-for="person in people"
          :key="person.id"
          :value="person"
          :disabled="person.unavailable"
        >
          {{ person.name }}
        </AComboboxItem>
      </AComboboxContent>
    </AComboboxPortal>
  </AComboboxRoot>
</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 { AComboboxRoot } from 'akar';
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>
  <AComboboxRoot
    v-model="selectedPeople"
    multiple
  >
    …
  </AComboboxRoot>
</template>

Custom filtering

Internally, AComboboxRoot will filter the item based on the rendered text.

However, you may also provide your own custom filtering logic together with setting ignoreFilter="true".

<script setup lang="ts">
import { AComboboxContent, AComboboxInput, AComboboxItem, AComboboxPortal, AComboboxRoot, useFilter } from 'akar';
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]);
const searchTerm = ref('');

const { startsWith } = useFilter({ sensitivity: 'base' });
const filteredPeople = computed(() => people.filter((p) => startsWith(p.name, searchTerm.value)));
</script>

<template>
  <AComboboxRoot
    v-model="selectedPeople"
    :ignore-filter="true"
  >
    <AComboboxInput v-model="searchTerm" />
    <AComboboxPortal>
      <AComboboxContent>
        <AComboboxItem
          v-for="person in filteredPeople"
          :key="person.id"
          :value="person"
        >
          {{ person.name }}
        </AComboboxItem>
      </AComboboxContent>
    </AComboboxPortal>
  </AComboboxRoot>
</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 { AComboboxInput, AComboboxRoot, Label } from 'akar';
</script>

<template>
  <AComboboxRoot v-model="selectedPeople">
    <Label for="person">Person: </Label>
    <AComboboxInput
      id="person"
      placeholder="Select a person"
    />
    …
  </AComboboxRoot>
</template>

With disabled items

You can add special styles to disabled items via the data-disabled attribute.

<script setup lang="ts">
import {
  AComboboxContent,
  AComboboxInput,
  AComboboxItem,
  AComboboxPortal,
  AComboboxRoot,
} from 'akar';
</script>

<template>
  <AComboboxRoot>
    <AComboboxInput />
    <AComboboxPortal>
      <AComboboxContent>
        <AComboboxItem
          class="AComboboxItem"
          disabled
        >
          …
        </AComboboxItem>
      </AComboboxContent>
    </AComboboxPortal>
  </AComboboxRoot>
</template>
/* styles.css */
.AComboboxItem[data-disabled] {
  color: 'gainsboro';
}

With separators

Use the Separator part to add a separator between items.

<script setup lang="ts">
import {
  AComboboxContent,
  AComboboxInput,
  AComboboxItem,
  AComboboxPortal,
  AComboboxRoot,
  AComboboxSeparator
} from 'akar';
</script>

<template>
  <AComboboxRoot>
    <AComboboxInput />
    <AComboboxPortal>
      <AComboboxContent>
        <AComboboxItem>…</AComboboxItem>
        <AComboboxItem>…</AComboboxItem>
        <AComboboxItem>…</AComboboxItem>
        <AComboboxSeparator />
        <AComboboxItem>…</AComboboxItem>
        <AComboboxItem>…</AComboboxItem>
        <AComboboxItem>…</AComboboxItem>
      </AComboboxContent>
    </AComboboxPortal>
  </AComboboxRoot>
</template>

With grouped items

Use the Group and Label parts to group items in a section.

<script setup lang="ts">
import {
  AComboboxContent,
  AComboboxGroup,
  AComboboxInput,
  AComboboxItem,
  AComboboxLabel,
  AComboboxPortal,
  AComboboxRoot
} from 'akar';
</script>

<template>
  <AComboboxRoot>
    <AComboboxInput />
    <AComboboxPortal>
      <AComboboxContent>
        <AComboboxGroup>
          <AComboboxLabel>Label</AComboboxLabel>
          <AComboboxItem>…</AComboboxItem>
          <AComboboxItem>…</AComboboxItem>
          <AComboboxItem>…</AComboboxItem>
        </AComboboxGroup>
      </AComboboxContent>
    </AComboboxPortal>
  </AComboboxRoot>
</template>

With complex items

You can use custom content in your items.

<script setup lang="ts">
import {
  AComboboxContent,
  AComboboxInput,
  AComboboxItem,
  AComboboxItemIndicator,
  AComboboxPortal,
  AComboboxRoot
} from 'akar';
</script>

<template>
  <AComboboxRoot>
    <AComboboxInput />
    <AComboboxPortal>
      <AComboboxContent>
        <AComboboxItem>
          <img src="…">
          Adolfo Hess
          <AComboboxItemIndicator />
        </AComboboxItem>
        …
      </AComboboxContent>
    </AComboboxPortal>
  </AComboboxRoot>
</template>

Prevent select behavior

By default, selecting AComboboxItem 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 { AComboboxContent, AComboboxInput, AComboboxItem, AComboboxPortal, AComboboxRoot } from 'akar';
</script>

<template>
  <AComboboxRoot>
    <AComboboxInput />
    <AComboboxPortal>
      <AComboboxContent>
        <AComboboxItem @select.prevent>
          Item A
        </AComboboxItem>
        …
      </AComboboxContent>
    </AComboboxPortal>
  </AComboboxRoot>
</template>

Virtualized combobox with working filtering

ACombobox items must be filtered manually before passing them over to the virtualizer.

See the virtualization guide for more general info on virtualization.

<script setup lang="ts">
import { AComboboxContent, AComboboxInput, AComboboxItem, AComboboxPortal, AComboboxRoot, AComboboxViewport, AComboboxVirtualizer, useFilter } from 'akar';
import { computed, ref } from 'vue';

const people = Array.from({ length: 100000 }).map((_, id) => ({ id, name: `Person #${id}` }));
const selectedPeople = ref(people[0]);
const searchTerm = ref('');

const { contains } = useFilter({ sensitivity: 'base' });
const filteredPeople = computed(() => people.filter((p) => contains(p.name, searchTerm.value)));
</script>

<template>
  <AComboboxRoot v-model="selectedPeople">
    <AComboboxInput v-model="searchTerm" />
    <AComboboxPortal>
      <AComboboxContent class="max-h-[40vh] overflow-hidden">
        <AComboboxViewport>
          <AComboboxVirtualizer
            v-slot="{ option }"
            :options="filteredPeople"
            :text-content="(x) => x.name"
            :estimate-size="24"
          >
            <AComboboxItem :value="option">
              {{ option.name }}
            </AComboboxItem>
          </AComboboxVirtualizer>
        </AComboboxViewport>
      </AComboboxContent>
    </AComboboxPortal>
  </AComboboxRoot>
</template>

Accessibility

Adheres to the ACombobox WAI-ARIA design pattern.

See the W3C ACombobox Autocomplete List example for more information.

Keyboard Interactions

Key Description
Enter

When focus is on AComboboxItem, selects the focused item.

ArrowDown

When focus is on AComboboxInput, opens the ACombobox content.
When focus is on an item, moves focus to the next item.

ArrowUp

When focus is on AComboboxInput, opens the ACombobox content.
When focus is on an item, moves focus to the previous item.

Esc

Closes ACombobox and restores the selected item in the AComboboxInput field.

Custom APIs

Create your own API by abstracting the primitive parts into your own component.

Command Menu

ACombobox 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 { AComboboxRootEmits, AComboboxRootProps } from 'akar';
import { AComboboxContent, AComboboxInput, AComboboxPortal, AComboboxRoot, useForwardPropsEmits } from 'akar';

const props = defineProps<AComboboxRootProps>();
const emits = defineEmits<AComboboxRootEmits>();

const forward = useForwardPropsEmits(props, emits);
</script>

<template>
  <AComboboxRoot
    v-bind="forward"
    :open="true"
    model-value=""
  >
    <AComboboxInput placeholder="Type a command or search…" />

    <AComboboxPortal>
      <AComboboxContent
        @escape-key-down.prevent
        @focus-outside.prevent
        @interact-outside.prevent
        @pointer-down-outside.prevent
      >
        <AComboboxViewport>
          <slot />
        </AComboboxViewport>
      </AComboboxContent>
    </AComboboxPortal>
  </AComboboxRoot>
</template>
<!-- AComboboxItem.vue -->
<script setup lang="ts">
import type { AComboboxItemProps } from 'akar';
import { AComboboxItem } from 'akar';

const props = defineProps<AComboboxItemProps>();
</script>

<template>
  <AComboboxItem
    v-bind="props"
    @select.prevent
  >
    <slot />
  </AComboboxItem>
</template>