On this page

Combobox

Live examples of Combobox, with the exact Vue file that renders each one.

Every example is a complete Vue file. Copy it into an app that has the registry installed, or open it in StackBlitz. Type to filter, then use the arrow keys and Enter.

Search field

The field is the search box. Type part of a warehouse name; the list keeps only the matches.

ComboboxBasic.vue
<script setup lang="ts">
import { Check } from '@lucide/vue'
import { ref } from 'vue'
import {
  Combobox,
  ComboboxAnchor,
  ComboboxEmpty,
  ComboboxInput,
  ComboboxItem,
  ComboboxItemIndicator,
  ComboboxList,
  ComboboxViewport,
} from '@/components/ui/combobox'

interface Warehouse {
  id: string
  name: string
}

const warehouses: Warehouse[] = [
  { id: 'hcm-d7', name: 'Ho Chi Minh City, District 7' },
  { id: 'hcm-td', name: 'Ho Chi Minh City, Thu Duc' },
  { id: 'hn-lb', name: 'Hanoi, Long Bien' },
  { id: 'dn-hc', name: 'Da Nang, Hai Chau' },
  { id: 'ct-nk', name: 'Can Tho, Ninh Kieu' },
]
const warehouse = ref<Warehouse>()
</script>

<template>
  <div class="grid w-full max-w-72 gap-(--bh-space-100)">
    <label for="ship-from" class="text-(--bh-color-text) [font:var(--bh-font-label-default)]">Ship from</label>
    <Combobox v-model="warehouse" by="id" open-on-click>
      <ComboboxAnchor>
        <ComboboxInput id="ship-from" :display-value="(item) => item?.name ?? ''" placeholder="Search warehouses…" />
      </ComboboxAnchor>
      <ComboboxList>
        <ComboboxEmpty>No warehouse found.</ComboboxEmpty>
        <ComboboxViewport>
          <ComboboxItem v-for="item in warehouses" :key="item.id" :value="item">
            {{ item.name }}
            <ComboboxItemIndicator>
              <Check />
            </ComboboxItemIndicator>
          </ComboboxItem>
        </ComboboxViewport>
      </ComboboxList>
    </Combobox>
  </div>
</template>

Button with a search box in the list

A Default button opens the list, which starts with its own search field. Use it where a field would take too much room, such as above a table. The button’s name joins its label and its value with aria-labelledby.

Store
ComboboxButton.vue
<script setup lang="ts">
import { Check, ChevronsUpDown } from '@lucide/vue'
import { ref } from 'vue'
import { Button } from '@/components/ui/button'
import {
  Combobox,
  ComboboxAnchor,
  ComboboxEmpty,
  ComboboxGroup,
  ComboboxInput,
  ComboboxItem,
  ComboboxItemIndicator,
  ComboboxList,
  ComboboxSeparator,
  ComboboxTrigger,
  ComboboxViewport,
} from '@/components/ui/combobox'

interface Store {
  id: string
  name: string
}

const groups: { heading: string, stores: Store[] }[] = [
  { heading: 'Online stores', stores: [{ id: 'web', name: 'Main online store' }, { id: 'b2b', name: 'Wholesale store' }] },
  { heading: 'Marketplaces', stores: [{ id: 'mp-1', name: 'Marketplace A' }, { id: 'mp-2', name: 'Marketplace B' }] },
]
const store = ref<Store>()
</script>

<template>
  <div class="grid w-full max-w-64 gap-(--bh-space-100)">
    <span id="store-label" class="text-(--bh-color-text) [font:var(--bh-font-label-default)]">Store</span>
    <Combobox v-model="store" by="id">
      <ComboboxAnchor as-child>
        <ComboboxTrigger as-child>
          <Button id="store-picker" variant="outline" class="w-full justify-between" aria-labelledby="store-label store-picker">
            {{ store?.name ?? 'Choose a store' }}
            <ChevronsUpDown />
          </Button>
        </ComboboxTrigger>
      </ComboboxAnchor>
      <ComboboxList>
        <ComboboxInput placeholder="Search stores…" aria-label="Search stores" />
        <ComboboxEmpty>No store found.</ComboboxEmpty>
        <ComboboxViewport>
          <template v-for="(group, index) in groups" :key="group.heading">
            <ComboboxSeparator v-if="index > 0" />
            <ComboboxGroup :heading="group.heading">
              <ComboboxItem v-for="item in group.stores" :key="item.id" :value="item">
                {{ item.name }}
                <ComboboxItemIndicator>
                  <Check />
                </ComboboxItemIndicator>
              </ComboboxItem>
            </ComboboxGroup>
          </template>
        </ComboboxViewport>
      </ComboboxList>
    </Combobox>
  </div>
</template>