Skip to content

Autocomplete ​

An input that suggests options for the text that the user types.

vue
<script setup lang="ts">
import { Autocomplete } from '@vase-ui/vue/autocomplete';
import styles from './index.module.css';

interface Tag {
  id: string;
  value: string;
}

const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];
</script>

<template>
  <Autocomplete.Root :items="tags">
    <label :class="styles.Label">
      Search tags
      <Autocomplete.Input placeholder="e.g. feature" :class="styles.Input" />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner :class="styles.Positioner" :side-offset="4">
        <Autocomplete.Popup :class="styles.Popup">
          <Autocomplete.Empty>
            <div :class="styles.Empty">No tags found.</div>
          </Autocomplete.Empty>
          <Autocomplete.List :class="styles.List">
            <template #item="{ item }">
              <Autocomplete.Item :class="styles.Item" :value="item">{{
                item.value
              }}</Autocomplete.Item>
            </template>
          </Autocomplete.List>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>
css
.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Positioner {
  outline: 0;
}

.Popup {
  box-sizing: border-box;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-width: var(--available-width);
  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.List {
  box-sizing: border-box;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-block: 0.25rem;
  scroll-padding-block: 0.25rem;
  outline: 0;
  max-height: min(22.5rem, var(--available-height));

  &[data-empty] {
    padding: 0;
  }
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding-block: 0.5rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  display: flex;
  font-size: 0.875rem;
  line-height: 1rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
    color: white;

    @media (prefers-color-scheme: dark) {
      color: oklch(14.5% 0 0deg);
    }
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(14.5% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: white;
    }
  }
}

.Separator {
  margin: 0.375rem 1rem;
  height: 1px;
  background-color: oklch(97% 0 0deg);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(26.9% 0 0deg);
  }
}

.Empty {
  box-sizing: border-box;
  padding: 1rem 1rem 1rem 0.5rem;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}
vue
<script setup lang="ts">
import { Autocomplete } from '@vase-ui/vue/autocomplete';

interface Tag {
  id: string;
  value: string;
}

const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];
</script>

<template>
  <Autocomplete.Root :items="tags">
    <label class="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
      Search tags
      <Autocomplete.Input
        placeholder="e.g. feature"
        class="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:opacity-100 placeholder:text-neutral-500 placeholder:[-webkit-text-fill-color:var(--color-neutral-500)] dark:placeholder:text-neutral-400 dark:placeholder:[-webkit-text-fill-color:var(--color-neutral-400)] focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
      />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner class="outline-hidden" :side-offset="4">
        <Autocomplete.Popup
          class="w-[var(--anchor-width)] max-w-[var(--available-width)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
        >
          <Autocomplete.Empty>
            <div class="py-4 pr-4 pl-2 text-sm leading-4 text-neutral-500 dark:text-neutral-400">
              No tags found.
            </div>
          </Autocomplete.Empty>
          <Autocomplete.List
            class="outline-0 overflow-y-auto scroll-py-[0.25rem] py-1 overscroll-contain max-h-[min(22.5rem,var(--available-height))] data-empty:p-0"
          >
            <template #item="{ item }">
              <Autocomplete.Item
                class="flex cursor-default items-center gap-2 py-2 pr-2 pl-2 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white"
                :value="item"
                >{{ item.value }}</Autocomplete.Item
              >
            </template>
          </Autocomplete.List>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>

Usage guidelines ​

  • Avoid when a selection state is necessary: Use Combobox when the selection must stay and the text must be one of the items. The input of Autocomplete can hold free-form text, and its suggestions only complete the text when the user wants them.
  • Form controls must have an accessible name: Give the input a <label> element, or use Field. See the forms guide.
  • Closing animations: Put a <Transition appear> around <Autocomplete.Popup>. Without keepMounted, the popup mounts at each open, so the <Transition> needs appear. The popup stays in the DOM until its leave ends. See the animation guide.

Anatomy ​

Import the component and assemble its parts:

vue
<script setup lang="ts">
import { Autocomplete } from '@vase-ui/vue/autocomplete';
</script>

<template>
  <Autocomplete.Root>
    <Autocomplete.InputGroup>
      <Autocomplete.Input />
      <Autocomplete.Trigger />
      <Autocomplete.Icon />
      <Autocomplete.Clear />
      <Autocomplete.Value />
    </Autocomplete.InputGroup>

    <Autocomplete.Portal>
      <Autocomplete.Backdrop />
      <Autocomplete.Positioner>
        <Autocomplete.Popup>
          <Autocomplete.Arrow />

          <Autocomplete.Status />
          <Autocomplete.Empty />

          <Autocomplete.List>
            <Autocomplete.Row>
              <Autocomplete.Item />
            </Autocomplete.Row>

            <Autocomplete.Separator />

            <Autocomplete.Group>
              <Autocomplete.GroupLabel />
            </Autocomplete.Group>
          </Autocomplete.List>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>

Item values ​

Each <Autocomplete.Item> takes a value prop that identifies it. Pass the item that the item slot of <Autocomplete.List> renders, so that props such as itemToStringValue get it. The slot gets { item, index }.

Examples ​

Load the items from a remote source when the text changes, and show the status of the load in <Autocomplete.Status>.

vue
<script setup lang="ts">
import { computed, shallowRef } from 'vue';
import { Autocomplete } from '@vase-ui/vue/autocomplete';
import styles from './index.module.css';

const searchValue = shallowRef('');
const searchResults = shallowRef<Movie[]>([]);
const error = shallowRef<string | null>(null);
const isPending = shallowRef(false);

const { contains } = Autocomplete.useFilter();

let abortController: AbortController | null = null;

const statusText = computed(() => {
  if (error.value) {
    return error.value;
  }

  if (searchValue.value === '') {
    return null;
  }

  if (searchResults.value.length === 0) {
    return `Movie or year "${searchValue.value}" does not exist in the Top 100 IMDb movies`;
  }

  return `${searchResults.value.length} result${searchResults.value.length === 1 ? '' : 's'} found`;
});

const hasStatus = computed(() => isPending.value || statusText.value !== null);

async function search(nextSearchValue: string) {
  searchValue.value = nextSearchValue;

  const controller = new AbortController();
  abortController?.abort();
  abortController = controller;

  if (nextSearchValue === '') {
    searchResults.value = [];
    error.value = null;
    isPending.value = false;
    return;
  }

  isPending.value = true;
  error.value = null;

  const result = await searchMovies(nextSearchValue, contains);
  if (controller.signal.aborted) {
    return;
  }

  searchResults.value = result.movies;
  error.value = result.error;
  isPending.value = false;
}

async function searchMovies(
  query: string,
  filter: (item: string, query: string) => boolean,
): Promise<{ movies: Movie[]; error: string | null }> {
  // Simulate network delay
  await new Promise((resolve) => {
    setTimeout(resolve, Math.random() * 500 + 100);
  });

  // Simulate occasional network errors (1% chance)
  if (Math.random() < 0.01 || query === 'will_error') {
    return {
      movies: [],
      error: 'Failed to fetch movies. Please try again.',
    };
  }

  const movies = top100Movies.filter(
    (movie) => filter(movie.title, query) || filter(movie.year.toString(), query),
  );

  return {
    movies,
    error: null,
  };
}

interface Movie {
  id: string;
  title: string;
  year: number;
}

const top100Movies: Movie[] = [
  { id: '1', title: 'The Shawshank Redemption', year: 1994 },
  { id: '2', title: 'The Godfather', year: 1972 },
  { id: '3', title: 'The Dark Knight', year: 2008 },
  { id: '4', title: 'The Godfather Part II', year: 1974 },
  { id: '5', title: '12 Angry Men', year: 1957 },
  { id: '6', title: 'The Lord of the Rings: The Return of the King', year: 2003 },
  { id: '7', title: "Schindler's List", year: 1993 },
  { id: '8', title: 'Pulp Fiction', year: 1994 },
  { id: '9', title: 'The Lord of the Rings: The Fellowship of the Ring', year: 2001 },
  { id: '10', title: 'The Good, the Bad and the Ugly', year: 1966 },
  { id: '11', title: 'Forrest Gump', year: 1994 },
  { id: '12', title: 'The Lord of the Rings: The Two Towers', year: 2002 },
  { id: '13', title: 'Fight Club', year: 1999 },
  { id: '14', title: 'Inception', year: 2010 },
  { id: '15', title: 'Star Wars: Episode V – The Empire Strikes Back', year: 1980 },
  { id: '16', title: 'The Matrix', year: 1999 },
  { id: '17', title: 'Goodfellas', year: 1990 },
  { id: '18', title: 'Interstellar', year: 2014 },
  { id: '19', title: "One Flew Over the Cuckoo's Nest", year: 1975 },
  { id: '20', title: 'Se7en', year: 1995 },
  { id: '21', title: "It's a Wonderful Life", year: 1946 },
  { id: '22', title: 'The Silence of the Lambs', year: 1991 },
  { id: '23', title: 'Seven Samurai', year: 1954 },
  { id: '24', title: 'Saving Private Ryan', year: 1998 },
  { id: '25', title: 'City of God', year: 2002 },
  { id: '26', title: 'Life Is Beautiful', year: 1997 },
  { id: '27', title: 'The Green Mile', year: 1999 },
  { id: '28', title: 'Star Wars: Episode IV – A New Hope', year: 1977 },
  { id: '29', title: 'Terminator 2: Judgment Day', year: 1991 },
  { id: '30', title: 'Back to the Future', year: 1985 },
  { id: '31', title: 'Spirited Away', year: 2001 },
  { id: '32', title: 'The Pianist', year: 2002 },
  { id: '33', title: 'Psycho', year: 1960 },
  { id: '34', title: 'Parasite', year: 2019 },
  { id: '35', title: 'Gladiator', year: 2000 },
  { id: '36', title: 'Léon: The Professional', year: 1994 },
  { id: '37', title: 'American History X', year: 1998 },
  { id: '38', title: 'The Departed', year: 2006 },
  { id: '39', title: 'Whiplash', year: 2014 },
  { id: '40', title: 'The Prestige', year: 2006 },
  { id: '41', title: 'Grave of the Fireflies', year: 1988 },
  { id: '42', title: 'The Usual Suspects', year: 1995 },
  { id: '43', title: 'Casablanca', year: 1942 },
  { id: '44', title: 'Harakiri', year: 1962 },
  { id: '45', title: 'The Lion King', year: 1994 },
  { id: '46', title: 'The Intouchables', year: 2011 },
  { id: '47', title: 'Modern Times', year: 1936 },
  { id: '48', title: 'The Lives of Others', year: 2006 },
  { id: '49', title: 'Once Upon a Time in the West', year: 1968 },
  { id: '50', title: 'Rear Window', year: 1954 },
  { id: '51', title: 'Alien', year: 1979 },
  { id: '52', title: 'City Lights', year: 1931 },
  { id: '53', title: 'The Shining', year: 1980 },
  { id: '54', title: 'Cinema Paradiso', year: 1988 },
  { id: '55', title: 'Avengers: Infinity War', year: 2018 },
  { id: '56', title: 'Paths of Glory', year: 1957 },
  { id: '57', title: 'Django Unchained', year: 2012 },
  { id: '58', title: 'WALL·E', year: 2008 },
  { id: '59', title: 'Sunset Boulevard', year: 1950 },
  { id: '60', title: 'The Great Dictator', year: 1940 },
  { id: '61', title: 'The Dark Knight Rises', year: 2012 },
  { id: '62', title: 'Princess Mononoke', year: 1997 },
  { id: '63', title: 'Witness for the Prosecution', year: 1957 },
  { id: '64', title: 'Oldboy', year: 2003 },
  { id: '65', title: 'Aliens', year: 1986 },
  { id: '66', title: 'Once Upon a Time in America', year: 1984 },
  { id: '67', title: 'Coco', year: 2017 },
  { id: '68', title: 'Your Name.', year: 2016 },
  { id: '69', title: 'American Beauty', year: 1999 },
  { id: '70', title: 'Braveheart', year: 1995 },
  { id: '71', title: 'Das Boot', year: 1981 },
  { id: '72', title: '3 Idiots', year: 2009 },
  { id: '73', title: 'Toy Story', year: 1995 },
  { id: '74', title: 'Inglourious Basterds', year: 2009 },
  { id: '75', title: 'High and Low', year: 1963 },
  { id: '76', title: 'Amadeus', year: 1984 },
  { id: '77', title: 'Good Will Hunting', year: 1997 },
  { id: '78', title: 'Star Wars: Episode VI – Return of the Jedi', year: 1983 },
  { id: '79', title: 'The Hunt', year: 2012 },
  { id: '80', title: 'Capharnaüm', year: 2018 },
  { id: '81', title: 'Reservoir Dogs', year: 1992 },
  { id: '82', title: 'Eternal Sunshine of the Spotless Mind', year: 2004 },
  { id: '83', title: 'Requiem for a Dream', year: 2000 },
  { id: '84', title: 'Come and See', year: 1985 },
  { id: '85', title: 'Ikiru', year: 1952 },
  { id: '86', title: 'Vertigo', year: 1958 },
  { id: '87', title: 'Lawrence of Arabia', year: 1962 },
  { id: '88', title: 'Citizen Kane', year: 1941 },
  { id: '89', title: 'Memento', year: 2000 },
  { id: '90', title: 'North by Northwest', year: 1959 },
  { id: '91', title: 'Star Wars: Episode III – Revenge of the Sith', year: 2005 },
  { id: '92', title: '2001: A Space Odyssey', year: 1968 },
  { id: '93', title: 'Amélie', year: 2001 },
  { id: '94', title: "Singin' in the Rain", year: 1952 },
  { id: '95', title: 'Apocalypse Now', year: 1979 },
  { id: '96', title: 'Taxi Driver', year: 1976 },
  { id: '97', title: 'Downfall', year: 2004 },
  { id: '98', title: 'The Wolf of Wall Street', year: 2013 },
  { id: '99', title: 'A Clockwork Orange', year: 1971 },
  { id: '100', title: 'Double Indemnity', year: 1944 },
];
</script>

<template>
  <Autocomplete.Root
    :items="searchResults"
    :model-value="searchValue"
    :item-to-string-value="(item: Movie) => item.title"
    :filter="null"
    @value-change="search"
  >
    <label :class="styles.Label">
      Search movies by name or year
      <Autocomplete.Input placeholder="e.g. Pulp Fiction or 1994" :class="styles.Input" />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner
        :hidden="!hasStatus"
        :class="styles.Positioner"
        :side-offset="4"
        align="start"
      >
        <Autocomplete.Popup :class="styles.Popup" :aria-busy="isPending || undefined">
          <div :class="styles.Viewport">
            <Autocomplete.Status>
              <div v-if="hasStatus" :class="styles.Status">
                <template v-if="isPending">
                  <span :class="styles.Spinner" aria-hidden="true" />
                  Searching…
                </template>
                <template v-else>{{ statusText }}</template>
              </div>
            </Autocomplete.Status>
            <Autocomplete.List>
              <template #item="{ item }">
                <Autocomplete.Item :class="styles.Item" :value="item">
                  <span :class="styles.MovieItem">
                    <span :class="styles.MovieName">{{ item.title }}</span>
                    <span :class="styles.MovieYear">{{ item.year }}</span>
                  </span>
                </Autocomplete.Item>
              </template>
            </Autocomplete.List>
          </div>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>
css
.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Positioner {
  outline: 0;
}

.Popup {
  box-sizing: border-box;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-width: var(--available-width);
  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.Viewport {
  box-sizing: border-box;
  max-height: min(var(--available-height), 22.5rem);
  padding-block: 0.25rem;
  overflow-y: auto;
  overscroll-behavior: contain;
  scroll-padding-block: 0.25rem;
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding-block: 0.5rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  display: flex;
  font-size: 0.875rem;
  line-height: 1rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
    color: white;

    @media (prefers-color-scheme: dark) {
      color: oklch(14.5% 0 0deg);
    }
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(14.5% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: white;
    }
  }
}

.MovieItem {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  width: 100%;
}

.MovieName {
  font-weight: 700;
  line-height: 1.25rem;
}

.MovieYear {
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }

  .Item[data-highlighted] & {
    color: oklch(87% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(55.6% 0 0deg);
    }
  }
}

.Status {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding-block: 0.25rem;
  padding-left: 0.5rem;
  padding-right: 2rem;
  font-size: 0.875rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}

.Spinner {
  box-sizing: border-box;
  width: 0.75rem;
  height: 0.75rem;
  border-radius: 50%;
  border: 1px solid currentColor;
  border-right-color: transparent;
  animation: autocompleteSpinner 0.75s linear infinite;
}

@keyframes autocompleteSpinner {
  100% {
    transform: rotate(360deg);
  }
}
vue
<script setup lang="ts">
import { computed, shallowRef } from 'vue';
import { Autocomplete } from '@vase-ui/vue/autocomplete';

const searchValue = shallowRef('');
const searchResults = shallowRef<Movie[]>([]);
const error = shallowRef<string | null>(null);
const isPending = shallowRef(false);

const { contains } = Autocomplete.useFilter();

let abortController: AbortController | null = null;

const statusText = computed(() => {
  if (error.value) {
    return error.value;
  }

  if (searchValue.value === '') {
    return null;
  }

  if (searchResults.value.length === 0) {
    return `Movie or year "${searchValue.value}" does not exist in the Top 100 IMDb movies`;
  }

  return `${searchResults.value.length} result${searchResults.value.length === 1 ? '' : 's'} found`;
});

const hasStatus = computed(() => isPending.value || statusText.value !== null);

async function search(nextSearchValue: string) {
  searchValue.value = nextSearchValue;

  const controller = new AbortController();
  abortController?.abort();
  abortController = controller;

  if (nextSearchValue === '') {
    searchResults.value = [];
    error.value = null;
    isPending.value = false;
    return;
  }

  isPending.value = true;
  error.value = null;

  const result = await searchMovies(nextSearchValue, contains);
  if (controller.signal.aborted) {
    return;
  }

  searchResults.value = result.movies;
  error.value = result.error;
  isPending.value = false;
}

async function searchMovies(
  query: string,
  filter: (item: string, query: string) => boolean,
): Promise<{ movies: Movie[]; error: string | null }> {
  // Simulate network delay
  await new Promise((resolve) => {
    setTimeout(resolve, Math.random() * 500 + 100);
  });

  // Simulate occasional network errors (1% chance)
  if (Math.random() < 0.01 || query === 'will_error') {
    return {
      movies: [],
      error: 'Failed to fetch movies. Please try again.',
    };
  }

  const movies = top100Movies.filter(
    (movie) => filter(movie.title, query) || filter(movie.year.toString(), query),
  );

  return {
    movies,
    error: null,
  };
}

interface Movie {
  id: string;
  title: string;
  year: number;
}

const top100Movies: Movie[] = [
  { id: '1', title: 'The Shawshank Redemption', year: 1994 },
  { id: '2', title: 'The Godfather', year: 1972 },
  { id: '3', title: 'The Dark Knight', year: 2008 },
  { id: '4', title: 'The Godfather Part II', year: 1974 },
  { id: '5', title: '12 Angry Men', year: 1957 },
  { id: '6', title: 'The Lord of the Rings: The Return of the King', year: 2003 },
  { id: '7', title: "Schindler's List", year: 1993 },
  { id: '8', title: 'Pulp Fiction', year: 1994 },
  { id: '9', title: 'The Lord of the Rings: The Fellowship of the Ring', year: 2001 },
  { id: '10', title: 'The Good, the Bad and the Ugly', year: 1966 },
  { id: '11', title: 'Forrest Gump', year: 1994 },
  { id: '12', title: 'The Lord of the Rings: The Two Towers', year: 2002 },
  { id: '13', title: 'Fight Club', year: 1999 },
  { id: '14', title: 'Inception', year: 2010 },
  { id: '15', title: 'Star Wars: Episode V – The Empire Strikes Back', year: 1980 },
  { id: '16', title: 'The Matrix', year: 1999 },
  { id: '17', title: 'Goodfellas', year: 1990 },
  { id: '18', title: 'Interstellar', year: 2014 },
  { id: '19', title: "One Flew Over the Cuckoo's Nest", year: 1975 },
  { id: '20', title: 'Se7en', year: 1995 },
  { id: '21', title: "It's a Wonderful Life", year: 1946 },
  { id: '22', title: 'The Silence of the Lambs', year: 1991 },
  { id: '23', title: 'Seven Samurai', year: 1954 },
  { id: '24', title: 'Saving Private Ryan', year: 1998 },
  { id: '25', title: 'City of God', year: 2002 },
  { id: '26', title: 'Life Is Beautiful', year: 1997 },
  { id: '27', title: 'The Green Mile', year: 1999 },
  { id: '28', title: 'Star Wars: Episode IV – A New Hope', year: 1977 },
  { id: '29', title: 'Terminator 2: Judgment Day', year: 1991 },
  { id: '30', title: 'Back to the Future', year: 1985 },
  { id: '31', title: 'Spirited Away', year: 2001 },
  { id: '32', title: 'The Pianist', year: 2002 },
  { id: '33', title: 'Psycho', year: 1960 },
  { id: '34', title: 'Parasite', year: 2019 },
  { id: '35', title: 'Gladiator', year: 2000 },
  { id: '36', title: 'Léon: The Professional', year: 1994 },
  { id: '37', title: 'American History X', year: 1998 },
  { id: '38', title: 'The Departed', year: 2006 },
  { id: '39', title: 'Whiplash', year: 2014 },
  { id: '40', title: 'The Prestige', year: 2006 },
  { id: '41', title: 'Grave of the Fireflies', year: 1988 },
  { id: '42', title: 'The Usual Suspects', year: 1995 },
  { id: '43', title: 'Casablanca', year: 1942 },
  { id: '44', title: 'Harakiri', year: 1962 },
  { id: '45', title: 'The Lion King', year: 1994 },
  { id: '46', title: 'The Intouchables', year: 2011 },
  { id: '47', title: 'Modern Times', year: 1936 },
  { id: '48', title: 'The Lives of Others', year: 2006 },
  { id: '49', title: 'Once Upon a Time in the West', year: 1968 },
  { id: '50', title: 'Rear Window', year: 1954 },
  { id: '51', title: 'Alien', year: 1979 },
  { id: '52', title: 'City Lights', year: 1931 },
  { id: '53', title: 'The Shining', year: 1980 },
  { id: '54', title: 'Cinema Paradiso', year: 1988 },
  { id: '55', title: 'Avengers: Infinity War', year: 2018 },
  { id: '56', title: 'Paths of Glory', year: 1957 },
  { id: '57', title: 'Django Unchained', year: 2012 },
  { id: '58', title: 'WALL·E', year: 2008 },
  { id: '59', title: 'Sunset Boulevard', year: 1950 },
  { id: '60', title: 'The Great Dictator', year: 1940 },
  { id: '61', title: 'The Dark Knight Rises', year: 2012 },
  { id: '62', title: 'Princess Mononoke', year: 1997 },
  { id: '63', title: 'Witness for the Prosecution', year: 1957 },
  { id: '64', title: 'Oldboy', year: 2003 },
  { id: '65', title: 'Aliens', year: 1986 },
  { id: '66', title: 'Once Upon a Time in America', year: 1984 },
  { id: '67', title: 'Coco', year: 2017 },
  { id: '68', title: 'Your Name.', year: 2016 },
  { id: '69', title: 'American Beauty', year: 1999 },
  { id: '70', title: 'Braveheart', year: 1995 },
  { id: '71', title: 'Das Boot', year: 1981 },
  { id: '72', title: '3 Idiots', year: 2009 },
  { id: '73', title: 'Toy Story', year: 1995 },
  { id: '74', title: 'Inglourious Basterds', year: 2009 },
  { id: '75', title: 'High and Low', year: 1963 },
  { id: '76', title: 'Amadeus', year: 1984 },
  { id: '77', title: 'Good Will Hunting', year: 1997 },
  { id: '78', title: 'Star Wars: Episode VI – Return of the Jedi', year: 1983 },
  { id: '79', title: 'The Hunt', year: 2012 },
  { id: '80', title: 'Capharnaüm', year: 2018 },
  { id: '81', title: 'Reservoir Dogs', year: 1992 },
  { id: '82', title: 'Eternal Sunshine of the Spotless Mind', year: 2004 },
  { id: '83', title: 'Requiem for a Dream', year: 2000 },
  { id: '84', title: 'Come and See', year: 1985 },
  { id: '85', title: 'Ikiru', year: 1952 },
  { id: '86', title: 'Vertigo', year: 1958 },
  { id: '87', title: 'Lawrence of Arabia', year: 1962 },
  { id: '88', title: 'Citizen Kane', year: 1941 },
  { id: '89', title: 'Memento', year: 2000 },
  { id: '90', title: 'North by Northwest', year: 1959 },
  { id: '91', title: 'Star Wars: Episode III – Revenge of the Sith', year: 2005 },
  { id: '92', title: '2001: A Space Odyssey', year: 1968 },
  { id: '93', title: 'Amélie', year: 2001 },
  { id: '94', title: "Singin' in the Rain", year: 1952 },
  { id: '95', title: 'Apocalypse Now', year: 1979 },
  { id: '96', title: 'Taxi Driver', year: 1976 },
  { id: '97', title: 'Downfall', year: 2004 },
  { id: '98', title: 'The Wolf of Wall Street', year: 2013 },
  { id: '99', title: 'A Clockwork Orange', year: 1971 },
  { id: '100', title: 'Double Indemnity', year: 1944 },
];
</script>

<template>
  <Autocomplete.Root
    :items="searchResults"
    :model-value="searchValue"
    :item-to-string-value="(item: Movie) => item.title"
    :filter="null"
    @value-change="search"
  >
    <label class="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
      Search movies by name or year
      <Autocomplete.Input
        placeholder="e.g. Pulp Fiction or 1994"
        class="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
      />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner
        :hidden="!hasStatus"
        class="outline-hidden"
        :side-offset="4"
        align="start"
      >
        <Autocomplete.Popup
          class="w-[var(--anchor-width)] max-w-[var(--available-width)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
          :aria-busy="isPending || undefined"
        >
          <div
            class="max-h-[min(var(--available-height),22.5rem)] overflow-y-auto overscroll-contain py-1 scroll-pt-1 scroll-pb-1"
          >
            <Autocomplete.Status>
              <div
                v-if="hasStatus"
                class="flex items-center gap-2 py-1 pr-8 pl-2 text-sm text-neutral-500 dark:text-neutral-400"
              >
                <template v-if="isPending">
                  <span
                    class="inline-block size-3 animate-spin rounded-full border border-current border-r-transparent rtl:border-r-current rtl:border-l-transparent"
                    aria-hidden="true"
                  />
                  Searching…
                </template>
                <template v-else>{{ statusText }}</template>
              </div>
            </Autocomplete.Status>
            <Autocomplete.List>
              <template #item="{ item }">
                <Autocomplete.Item
                  class="group flex cursor-default py-2 pr-2 pl-2 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white"
                  :value="item"
                >
                  <span class="flex w-full flex-col gap-1">
                    <span class="font-bold leading-5">{{ item.title }}</span>
                    <span
                      class="text-sm leading-4 text-neutral-500 dark:text-neutral-400 group-data-highlighted:text-neutral-300 dark:group-data-highlighted:text-neutral-500"
                      >{{ item.year }}</span
                    >
                  </span>
                </Autocomplete.Item>
              </template>
            </Autocomplete.List>
          </div>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>

Inline autocomplete ​

Set mode to both or inline to show the text of the item that the arrow keys highlight in the input. mode takes the values of aria-autocomplete: list, both, inline or none.

vue
<script setup lang="ts">
import { Autocomplete } from '@vase-ui/vue/autocomplete';
import styles from './index.module.css';

interface Tag {
  id: string;
  value: string;
}

const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];
</script>

<template>
  <Autocomplete.Root :items="tags" mode="both">
    <label :class="styles.Label">
      Search tags
      <Autocomplete.Input placeholder="e.g. feature" :class="styles.Input" />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner :class="styles.Positioner" :side-offset="4">
        <Autocomplete.Popup :class="styles.Popup">
          <Autocomplete.List :class="styles.List">
            <template #item="{ item }">
              <Autocomplete.Item :class="styles.Item" :value="item">{{
                item.value
              }}</Autocomplete.Item>
            </template>
          </Autocomplete.List>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>
css
.Container {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Positioner {
  outline: 0;

  &[data-empty] {
    display: none;
  }
}

.Popup {
  box-sizing: border-box;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-width: var(--available-width);
  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.List {
  box-sizing: border-box;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-block: 0.25rem;
  scroll-padding-block: 0.25rem;
  outline: 0;
  max-height: min(22.5rem, var(--available-height));

  &[data-empty] {
    padding: 0;
  }
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding-block: 0.5rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  display: flex;
  align-items: center;
  font-size: 0.875rem;
  line-height: 1rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
    color: white;

    @media (prefers-color-scheme: dark) {
      color: oklch(14.5% 0 0deg);
    }
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(14.5% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: white;
    }
  }
}
vue
<script setup lang="ts">
import { Autocomplete } from '@vase-ui/vue/autocomplete';

interface Tag {
  id: string;
  value: string;
}

const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];
</script>

<template>
  <Autocomplete.Root :items="tags" mode="both">
    <label class="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
      Search tags
      <Autocomplete.Input
        placeholder="e.g. feature"
        class="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
      />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner class="outline-hidden data-empty:hidden" :side-offset="4">
        <Autocomplete.Popup
          class="w-[var(--anchor-width)] max-w-[var(--available-width)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
        >
          <Autocomplete.List
            class="outline-0 overflow-y-auto scroll-py-[0.25rem] py-1 overscroll-contain max-h-[min(22.5rem,var(--available-height))] data-empty:p-0"
          >
            <template #item="{ item }">
              <Autocomplete.Item
                class="flex cursor-default items-center gap-2 py-2 pr-2 pl-2 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white"
                :value="item"
                >{{ item.value }}</Autocomplete.Item
              >
            </template>
          </Autocomplete.List>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>

Grouped ​

Use <Autocomplete.Group> and <Autocomplete.GroupLabel> to add headings that group related items in the popup.

The items prop describes the groups as an array of objects. Each object has an items array with the items of its group. Another property, such as value, can give the text of the heading:

vue
<script setup lang="ts">
import { Autocomplete } from '@vase-ui/vue/autocomplete';

interface ProduceGroupItem {
  value: string;
  items: string[];
}

const groups: ProduceGroupItem[] = [
  {
    value: 'Fruits',
    items: ['Apple', 'Banana', 'Orange'],
  },
  {
    value: 'Vegetables',
    items: ['Carrot', 'Lettuce', 'Spinach'],
  },
];
</script>

<template>
  <Autocomplete.Root :items="groups">
    <Autocomplete.Input />
    <Autocomplete.Portal>
      <Autocomplete.Positioner>
        <Autocomplete.Popup>
          <Autocomplete.List>
            <template #item="{ item: group }">
              <Autocomplete.Group>
                <Autocomplete.GroupLabel>{{ group.value }}</Autocomplete.GroupLabel>
                <template v-for="item in group.items" :key="item.value">
                  <Autocomplete.Item :value="item">{{ item }}</Autocomplete.Item>
                </template>
              </Autocomplete.Group>
            </template>
          </Autocomplete.List>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>
vue
<script setup lang="ts">
import { Autocomplete } from '@vase-ui/vue/autocomplete';
import styles from './index.module.css';

interface Tag {
  id: string;
  label: string;
  group: 'Type' | 'Component';
}

interface TagGroup {
  value: string;
  items: Tag[];
}

const tagsData: Tag[] = [
  { id: 't1', label: 'feature', group: 'Type' },
  { id: 't2', label: 'fix', group: 'Type' },
  { id: 't3', label: 'bug', group: 'Type' },
  { id: 't4', label: 'docs', group: 'Type' },
  { id: 't5', label: 'internal', group: 'Type' },
  { id: 't6', label: 'mobile', group: 'Type' },
  { id: 'c-accordion', label: 'component: accordion', group: 'Component' },
  { id: 'c-alert-dialog', label: 'component: alert dialog', group: 'Component' },
  { id: 'c-autocomplete', label: 'component: autocomplete', group: 'Component' },
  { id: 'c-avatar', label: 'component: avatar', group: 'Component' },
  { id: 'c-checkbox', label: 'component: checkbox', group: 'Component' },
  { id: 'c-checkbox-group', label: 'component: checkbox group', group: 'Component' },
  { id: 'c-collapsible', label: 'component: collapsible', group: 'Component' },
  { id: 'c-combobox', label: 'component: combobox', group: 'Component' },
  { id: 'c-context-menu', label: 'component: context menu', group: 'Component' },
  { id: 'c-dialog', label: 'component: dialog', group: 'Component' },
  { id: 'c-field', label: 'component: field', group: 'Component' },
  { id: 'c-fieldset', label: 'component: fieldset', group: 'Component' },
  { id: 'c-filterable-menu', label: 'component: filterable menu', group: 'Component' },
  { id: 'c-form', label: 'component: form', group: 'Component' },
  { id: 'c-input', label: 'component: input', group: 'Component' },
  { id: 'c-menu', label: 'component: menu', group: 'Component' },
  { id: 'c-menubar', label: 'component: menubar', group: 'Component' },
  { id: 'c-meter', label: 'component: meter', group: 'Component' },
  { id: 'c-navigation-menu', label: 'component: navigation menu', group: 'Component' },
  { id: 'c-number-field', label: 'component: number field', group: 'Component' },
  { id: 'c-popover', label: 'component: popover', group: 'Component' },
  { id: 'c-preview-card', label: 'component: preview card', group: 'Component' },
  { id: 'c-progress', label: 'component: progress', group: 'Component' },
  { id: 'c-radio', label: 'component: radio', group: 'Component' },
  { id: 'c-scroll-area', label: 'component: scroll area', group: 'Component' },
  { id: 'c-select', label: 'component: select', group: 'Component' },
  { id: 'c-separator', label: 'component: separator', group: 'Component' },
  { id: 'c-slider', label: 'component: slider', group: 'Component' },
  { id: 'c-switch', label: 'component: switch', group: 'Component' },
  { id: 'c-tabs', label: 'component: tabs', group: 'Component' },
  { id: 'c-toast', label: 'component: toast', group: 'Component' },
  { id: 'c-toggle', label: 'component: toggle', group: 'Component' },
  { id: 'c-toggle-group', label: 'component: toggle group', group: 'Component' },
  { id: 'c-toolbar', label: 'component: toolbar', group: 'Component' },
  { id: 'c-tooltip', label: 'component: tooltip', group: 'Component' },
];

function groupTags(tags: Tag[]): TagGroup[] {
  const groups: Record<string, Tag[]> = {};
  tags.forEach((t) => {
    (groups[t.group] ??= []).push(t);
  });
  const order = ['Type', 'Component'];
  return order.map((value) => ({ value, items: groups[value] ?? [] }));
}

const groupedTags: TagGroup[] = groupTags(tagsData);
</script>

<template>
  <Autocomplete.Root :items="groupedTags">
    <label :class="styles.Label">
      Select a tag
      <Autocomplete.Input placeholder="e.g. feature" :class="styles.Input" />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner :class="styles.Positioner" :side-offset="4">
        <Autocomplete.Popup :class="styles.Popup">
          <Autocomplete.Empty>
            <div :class="styles.Empty">No tags found.</div>
          </Autocomplete.Empty>
          <Autocomplete.List :class="styles.List">
            <template #item="{ item: group }">
              <Autocomplete.Group :class="styles.Group">
                <Autocomplete.GroupLabel :class="styles.GroupLabel">
                  {{ group.value }}
                </Autocomplete.GroupLabel>
                <template v-for="tag in group.items" :key="tag.value">
                  <Autocomplete.Item :class="styles.Item" :value="tag">
                    {{ tag.label }}
                  </Autocomplete.Item>
                </template>
              </Autocomplete.Group>
            </template>
          </Autocomplete.List>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>
css
.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Positioner {
  outline: 0;
}

.Popup {
  box-sizing: border-box;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-width: var(--available-width);
  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.List {
  box-sizing: border-box;
  overflow-y: auto;
  padding-block: 0.25rem;
  scroll-padding-top: 0.25rem;
  scroll-padding-bottom: 0.25rem;
  overscroll-behavior: contain;
  max-height: min(22.5rem, var(--available-height));
  outline: 0;

  &[data-empty] {
    padding: 0;
  }
}

.Group {
  display: block;
  padding-bottom: 0.5rem;

  &:last-child {
    padding-bottom: 0;
  }
}

.GroupLabel {
  box-sizing: border-box;
  -webkit-user-select: none;
  user-select: none;
  padding: 0.5rem;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding: 0.5rem;
  display: flex;
  font-size: 0.875rem;
  line-height: 1rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
    color: white;

    @media (prefers-color-scheme: dark) {
      color: oklch(14.5% 0 0deg);
    }
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(14.5% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: white;
    }
  }
}

.Separator {
  margin: 0.375rem 1rem;
  height: 1px;
  background-color: oklch(97% 0 0deg);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(26.9% 0 0deg);
  }
}

.Empty {
  box-sizing: border-box;
  padding: 1rem 1rem 1rem 0.5rem;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}
vue
<script setup lang="ts">
import { Autocomplete } from '@vase-ui/vue/autocomplete';

interface Tag {
  id: string;
  label: string;
  group: 'Type' | 'Component';
}

interface TagGroup {
  value: string;
  items: Tag[];
}

const tagsData: Tag[] = [
  { id: 't1', label: 'feature', group: 'Type' },
  { id: 't2', label: 'fix', group: 'Type' },
  { id: 't3', label: 'bug', group: 'Type' },
  { id: 't4', label: 'docs', group: 'Type' },
  { id: 't5', label: 'internal', group: 'Type' },
  { id: 't6', label: 'mobile', group: 'Type' },
  { id: 'c-accordion', label: 'component: accordion', group: 'Component' },
  { id: 'c-alert-dialog', label: 'component: alert dialog', group: 'Component' },
  { id: 'c-autocomplete', label: 'component: autocomplete', group: 'Component' },
  { id: 'c-avatar', label: 'component: avatar', group: 'Component' },
  { id: 'c-checkbox', label: 'component: checkbox', group: 'Component' },
  { id: 'c-checkbox-group', label: 'component: checkbox group', group: 'Component' },
  { id: 'c-collapsible', label: 'component: collapsible', group: 'Component' },
  { id: 'c-combobox', label: 'component: combobox', group: 'Component' },
  { id: 'c-context-menu', label: 'component: context menu', group: 'Component' },
  { id: 'c-dialog', label: 'component: dialog', group: 'Component' },
  { id: 'c-field', label: 'component: field', group: 'Component' },
  { id: 'c-fieldset', label: 'component: fieldset', group: 'Component' },
  { id: 'c-filterable-menu', label: 'component: filterable menu', group: 'Component' },
  { id: 'c-form', label: 'component: form', group: 'Component' },
  { id: 'c-input', label: 'component: input', group: 'Component' },
  { id: 'c-menu', label: 'component: menu', group: 'Component' },
  { id: 'c-menubar', label: 'component: menubar', group: 'Component' },
  { id: 'c-meter', label: 'component: meter', group: 'Component' },
  { id: 'c-navigation-menu', label: 'component: navigation menu', group: 'Component' },
  { id: 'c-number-field', label: 'component: number field', group: 'Component' },
  { id: 'c-popover', label: 'component: popover', group: 'Component' },
  { id: 'c-preview-card', label: 'component: preview card', group: 'Component' },
  { id: 'c-progress', label: 'component: progress', group: 'Component' },
  { id: 'c-radio', label: 'component: radio', group: 'Component' },
  { id: 'c-scroll-area', label: 'component: scroll area', group: 'Component' },
  { id: 'c-select', label: 'component: select', group: 'Component' },
  { id: 'c-separator', label: 'component: separator', group: 'Component' },
  { id: 'c-slider', label: 'component: slider', group: 'Component' },
  { id: 'c-switch', label: 'component: switch', group: 'Component' },
  { id: 'c-tabs', label: 'component: tabs', group: 'Component' },
  { id: 'c-toast', label: 'component: toast', group: 'Component' },
  { id: 'c-toggle', label: 'component: toggle', group: 'Component' },
  { id: 'c-toggle-group', label: 'component: toggle group', group: 'Component' },
  { id: 'c-toolbar', label: 'component: toolbar', group: 'Component' },
  { id: 'c-tooltip', label: 'component: tooltip', group: 'Component' },
];

function groupTags(tags: Tag[]): TagGroup[] {
  const groups: Record<string, Tag[]> = {};
  tags.forEach((t) => {
    (groups[t.group] ??= []).push(t);
  });
  const order = ['Type', 'Component'];
  return order.map((value) => ({ value, items: groups[value] ?? [] }));
}

const groupedTags: TagGroup[] = groupTags(tagsData);
</script>

<template>
  <Autocomplete.Root :items="groupedTags">
    <label class="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
      Select a tag
      <Autocomplete.Input
        placeholder="e.g. feature"
        class="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
      />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner class="outline-hidden" :side-offset="4">
        <Autocomplete.Popup
          class="w-[var(--anchor-width)] max-w-[var(--available-width)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
        >
          <Autocomplete.Empty>
            <div class="py-4 pr-4 pl-2 text-sm leading-4 text-neutral-500 dark:text-neutral-400">
              No tags found.
            </div>
          </Autocomplete.Empty>
          <Autocomplete.List
            class="outline-0 overflow-y-auto py-1 scroll-py-1 overscroll-contain max-h-[min(22.5rem,var(--available-height))] data-empty:p-0"
          >
            <template #item="{ item: group }">
              <Autocomplete.Group class="block pb-2 last:pb-0">
                <Autocomplete.GroupLabel
                  class="p-2 text-sm leading-4 text-neutral-500 select-none dark:text-neutral-400"
                >
                  {{ group.value }}
                </Autocomplete.GroupLabel>
                <template v-for="tag in group.items" :key="tag.value">
                  <Autocomplete.Item
                    class="flex cursor-default items-center gap-2 py-2 pr-2 pl-2 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white"
                    :value="tag"
                  >
                    {{ tag.label }}
                  </Autocomplete.Item>
                </template>
              </Autocomplete.Group>
            </template>
          </Autocomplete.List>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>

Fuzzy matching ​

A fuzzy match finds relevant items also when the query is not the same as the text of the item.

vue
<script setup lang="ts">
import { matchSorter } from 'match-sorter';
import { Autocomplete } from '@vase-ui/vue/autocomplete';
import styles from './index.module.css';

interface HighlightedPart {
  text: string;
  match: boolean;
}

function highlightText(text: string, query: string): HighlightedPart[] {
  const trimmed = query.trim();
  if (!trimmed) {
    return [{ text, match: false }];
  }

  const limited = trimmed.slice(0, 100);
  const escaped = limited.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  const regex = new RegExp(`(${escaped})`, 'gi');

  // A split with a capture group puts each match at an odd index.
  return text
    .split(regex)
    .map((part, index) => ({ text: part, match: index % 2 === 1 }))
    .filter((part) => part.text !== '');
}

function fuzzyFilter(item: FuzzyItem, query: string): boolean {
  if (!query) {
    return true;
  }

  const results = matchSorter([item], query, {
    keys: [
      'title',
      'description',
      'category',
      { key: 'title', threshold: matchSorter.rankings.CONTAINS },
      { key: 'description', threshold: matchSorter.rankings.WORD_STARTS_WITH },
    ],
  });

  return results.length > 0;
}

interface FuzzyItem {
  title: string;
  description: string;
  category: string;
}

const fuzzyItems: FuzzyItem[] = [
  {
    title: 'Vue Composables Guide',
    description: 'Learn how to use Vue composables like ref, computed, and custom composables',
    category: 'Vue.js',
  },
  {
    title: 'JavaScript Array Methods',
    description: 'Master array methods like map, filter, reduce, and forEach in JavaScript',
    category: 'JavaScript',
  },
  {
    title: 'CSS Flexbox Layout',
    description: 'Complete guide to CSS Flexbox for responsive web design',
    category: 'CSS',
  },
  {
    title: 'TypeScript Interfaces',
    description: 'Understanding TypeScript interfaces and type definitions',
    category: 'TypeScript',
  },
  {
    title: 'Vue Performance Optimization',
    description: 'Tips and techniques for optimizing Vue application performance',
    category: 'Vue.js',
  },
  {
    title: 'HTML Semantic Elements',
    description: 'Using semantic HTML elements for better accessibility and SEO',
    category: 'HTML',
  },
  {
    title: 'Node.js Express Server',
    description: 'Building RESTful APIs with Node.js and Express framework',
    category: 'Node.js',
  },
  {
    title: 'Vue Composition API',
    description: 'Modern Vue.js development using the Composition API',
    category: 'Vue.js',
  },
  {
    title: 'Angular Components',
    description: 'Creating reusable Angular components with TypeScript',
    category: 'Angular',
  },
  {
    title: 'Python Django Framework',
    description: 'Web development with Python Django framework',
    category: 'Python',
  },
  {
    title: 'CSS Grid Layout',
    description: 'Advanced CSS Grid techniques for complex layouts',
    category: 'CSS',
  },
  {
    title: 'Vue Testing Library',
    description: 'Testing Vue components with Vue Testing Library',
    category: 'Vue.js',
  },
  {
    title: 'MongoDB Queries',
    description: 'Advanced MongoDB queries and aggregation pipelines',
    category: 'Database',
  },
  {
    title: 'Webpack Configuration',
    description: 'Optimizing webpack configuration for production builds',
    category: 'Build Tools',
  },
  {
    title: 'SASS/SCSS Guide',
    description: 'Writing maintainable CSS with SASS and SCSS',
    category: 'CSS',
  },
];
</script>

<template>
  <Autocomplete.Root
    :items="fuzzyItems"
    :filter="fuzzyFilter"
    :item-to-string-value="(item: FuzzyItem) => item.title"
  >
    <label :class="styles.Label">
      Fuzzy search documentation
      <Autocomplete.Input placeholder="e.g. Vue" :class="styles.Input" />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner :class="styles.Positioner" :side-offset="4">
        <Autocomplete.Popup :class="styles.Popup">
          <Autocomplete.Empty>
            <div :class="styles.Empty">No results found for "<Autocomplete.Value />"</div>
          </Autocomplete.Empty>

          <Autocomplete.List :class="styles.List">
            <template #item="{ item }">
              <Autocomplete.Item :value="item" :class="styles.Item">
                <Autocomplete.Value v-slot="{ value }">
                  <span :class="styles.ItemContent">
                    <span :class="styles.ItemHeader">
                      <span :class="styles.ItemTitle">
                        <template
                          v-for="(part, index) in highlightText(item.title, value)"
                          :key="index"
                        >
                          <mark v-if="part.match">{{ part.text }}</mark>
                          <template v-else>{{ part.text }}</template>
                        </template>
                      </span>
                    </span>
                    <span :class="styles.ItemDescription">
                      <template
                        v-for="(part, index) in highlightText(item.description, value)"
                        :key="index"
                      >
                        <mark v-if="part.match">{{ part.text }}</mark>
                        <template v-else>{{ part.text }}</template>
                      </template>
                    </span>
                  </span>
                </Autocomplete.Value>
              </Autocomplete.Item>
            </template>
          </Autocomplete.List>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>
css
.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Positioner {
  outline: 0;
}

.Popup {
  box-sizing: border-box;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-width: var(--available-width);
  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.List {
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  padding-block: 0.25rem;
  max-height: min(var(--available-height), 28rem);
  overflow-y: auto;
  overscroll-behavior: contain;
  scroll-padding-block: 0.25rem;

  &:empty {
    padding: 0;
  }
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding: 0.75rem 0.5rem;
  display: flex;
  font-size: 0.875rem;
  line-height: 1.5rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(97% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(26.9% 0 0deg);
    }
  }
}

.ItemContent {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  width: 100%;
}

.ItemHeader {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
}

.ItemTitle {
  font-weight: 700;
  line-height: 1.25rem;
  flex: 1;
}

.ItemDescription {
  font-size: 0.875rem;
  color: oklch(55.6% 0 0deg);
  line-height: 1.25rem;

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}

.Empty {
  box-sizing: border-box;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);
  padding: 0.75rem 0.5rem;

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}

.Item mark {
  background-color: transparent;
  color: oklch(42.4% 0.199 265.638deg);
  font-weight: 700;

  @media (prefers-color-scheme: dark) {
    color: oklch(62.3% 0.214 259.815deg);
  }
}
vue
<script setup lang="ts">
import { matchSorter } from 'match-sorter';
import { Autocomplete } from '@vase-ui/vue/autocomplete';

const markClass = 'bg-transparent font-bold text-blue-800 dark:text-blue-500';

interface HighlightedPart {
  text: string;
  match: boolean;
}

function highlightText(text: string, query: string): HighlightedPart[] {
  const trimmed = query.trim();
  if (!trimmed) {
    return [{ text, match: false }];
  }

  const limited = trimmed.slice(0, 100);
  const escaped = limited.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  const regex = new RegExp(`(${escaped})`, 'gi');

  // A split with a capture group puts each match at an odd index.
  return text
    .split(regex)
    .map((part, index) => ({ text: part, match: index % 2 === 1 }))
    .filter((part) => part.text !== '');
}

function fuzzyFilter(item: FuzzyItem, query: string): boolean {
  if (!query) {
    return true;
  }

  const results = matchSorter([item], query, {
    keys: [
      'title',
      'description',
      'category',
      { key: 'title', threshold: matchSorter.rankings.CONTAINS },
      { key: 'description', threshold: matchSorter.rankings.WORD_STARTS_WITH },
    ],
  });

  return results.length > 0;
}

interface FuzzyItem {
  title: string;
  description: string;
  category: string;
}

const fuzzyItems: FuzzyItem[] = [
  {
    title: 'Vue Composables Guide',
    description: 'Learn how to use Vue composables like ref, computed, and custom composables',
    category: 'Vue.js',
  },
  {
    title: 'JavaScript Array Methods',
    description: 'Master array methods like map, filter, reduce, and forEach in JavaScript',
    category: 'JavaScript',
  },
  {
    title: 'CSS Flexbox Layout',
    description: 'Complete guide to CSS Flexbox for responsive web design',
    category: 'CSS',
  },
  {
    title: 'TypeScript Interfaces',
    description: 'Understanding TypeScript interfaces and type definitions',
    category: 'TypeScript',
  },
  {
    title: 'Vue Performance Optimization',
    description: 'Tips and techniques for optimizing Vue application performance',
    category: 'Vue.js',
  },
  {
    title: 'HTML Semantic Elements',
    description: 'Using semantic HTML elements for better accessibility and SEO',
    category: 'HTML',
  },
  {
    title: 'Node.js Express Server',
    description: 'Building RESTful APIs with Node.js and Express framework',
    category: 'Node.js',
  },
  {
    title: 'Vue Composition API',
    description: 'Modern Vue.js development using the Composition API',
    category: 'Vue.js',
  },
  {
    title: 'Angular Components',
    description: 'Creating reusable Angular components with TypeScript',
    category: 'Angular',
  },
  {
    title: 'Python Django Framework',
    description: 'Web development with Python Django framework',
    category: 'Python',
  },
  {
    title: 'CSS Grid Layout',
    description: 'Advanced CSS Grid techniques for complex layouts',
    category: 'CSS',
  },
  {
    title: 'Vue Testing Library',
    description: 'Testing Vue components with Vue Testing Library',
    category: 'Vue.js',
  },
  {
    title: 'MongoDB Queries',
    description: 'Advanced MongoDB queries and aggregation pipelines',
    category: 'Database',
  },
  {
    title: 'Webpack Configuration',
    description: 'Optimizing webpack configuration for production builds',
    category: 'Build Tools',
  },
  {
    title: 'SASS/SCSS Guide',
    description: 'Writing maintainable CSS with SASS and SCSS',
    category: 'CSS',
  },
];
</script>

<template>
  <Autocomplete.Root
    :items="fuzzyItems"
    :filter="fuzzyFilter"
    :item-to-string-value="(item: FuzzyItem) => item.title"
  >
    <label class="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
      Fuzzy search documentation
      <Autocomplete.Input
        placeholder="e.g. Vue"
        class="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
      />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner class="outline-hidden" :side-offset="4">
        <Autocomplete.Popup
          class="w-(--anchor-width) max-w-(--available-width) border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
        >
          <Autocomplete.Empty>
            <div class="py-3 pr-4 pl-2 text-sm leading-4 text-neutral-500 dark:text-neutral-400">
              No results found for "<Autocomplete.Value />"
            </div>
          </Autocomplete.Empty>

          <Autocomplete.List
            class="flex max-h-[min(var(--available-height),28rem)] flex-col overflow-y-auto overscroll-contain py-1 scroll-pt-1 scroll-pb-1 empty:p-0"
          >
            <template #item="{ item }">
              <Autocomplete.Item
                :value="item"
                class="flex cursor-default py-3 pr-2 pl-2 text-sm leading-6 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-100 dark:data-highlighted:before:bg-neutral-800"
              >
                <Autocomplete.Value v-slot="{ value }">
                  <span class="flex w-full flex-col gap-1">
                    <span class="flex items-center justify-between gap-3">
                      <span class="flex-1 font-bold leading-5">
                        <template
                          v-for="(part, index) in highlightText(item.title, value)"
                          :key="index"
                        >
                          <mark v-if="part.match" :class="markClass">{{ part.text }}</mark>
                          <template v-else>{{ part.text }}</template>
                        </template>
                      </span>
                    </span>
                    <span class="text-sm text-neutral-500 dark:text-neutral-400">
                      <template
                        v-for="(part, index) in highlightText(item.description, value)"
                        :key="index"
                      >
                        <mark v-if="part.match" :class="markClass">{{ part.text }}</mark>
                        <template v-else>{{ part.text }}</template>
                      </template>
                    </span>
                  </span>
                </Autocomplete.Value>
              </Autocomplete.Item>
            </template>
          </Autocomplete.List>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>

Limit results ​

The limit prop sets the largest number of visible items. Use <Autocomplete.Status> to tell the user to make the query more specific.

vue
<script setup lang="ts">
import { computed, shallowRef } from 'vue';
import { Autocomplete } from '@vase-ui/vue/autocomplete';
import styles from './index.module.css';

const limit = 8;

const value = shallowRef('');

const { contains } = Autocomplete.useFilter({ sensitivity: 'base' });

const totalMatches = computed(() => {
  const trimmed = value.value.trim();
  if (!trimmed) {
    return tags.length;
  }
  return tags.filter((t) => contains(t.value, trimmed)).length;
});

const moreCount = computed(() => Math.max(0, totalMatches.value - limit));

interface Tag {
  id: string;
  value: string;
}

// Larger dataset to make the limit visible.
const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 't7', value: 'frontend' },
  { id: 't8', value: 'backend' },
  { id: 't9', value: 'performance' },
  { id: 't10', value: 'accessibility' },
  { id: 't11', value: 'design' },
  { id: 't12', value: 'research' },
  { id: 't13', value: 'testing' },
  { id: 't14', value: 'infrastructure' },
  { id: 't15', value: 'documentation' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];
</script>

<template>
  <Autocomplete.Root :items="tags" v-model="value" :limit="limit">
    <label :class="styles.Label">
      Limit results to 8
      <Autocomplete.Input placeholder="e.g. component" :class="styles.Input" />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner :class="styles.Positioner" :side-offset="4">
        <Autocomplete.Popup :class="styles.Popup">
          <Autocomplete.Empty>
            <div :class="styles.Empty">No results found for "{{ value }}"</div>
          </Autocomplete.Empty>

          <Autocomplete.List>
            <template #item="{ item }">
              <Autocomplete.Item :class="styles.Item" :value="item">{{
                item.value
              }}</Autocomplete.Item>
            </template>
          </Autocomplete.List>

          <Autocomplete.Status>
            <div v-if="moreCount > 0" :class="styles.Status">
              {{ `Hiding ${moreCount} results (type a more specific query to narrow results)` }}
            </div>
          </Autocomplete.Status>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>
css
.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Positioner {
  outline: 0;
}

.Popup {
  box-sizing: border-box;
  padding-block: 0.25rem;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-height: min(var(--available-height), 22.5rem);
  max-width: var(--available-width);
  overflow-y: auto;
  scroll-padding-block: 0.25rem;
  overscroll-behavior: contain;

  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding-block: 0.5rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  display: flex;
  font-size: 0.875rem;
  line-height: 1rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
    color: white;

    @media (prefers-color-scheme: dark) {
      color: oklch(14.5% 0 0deg);
    }
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(14.5% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: white;
    }
  }
}

.Empty {
  box-sizing: border-box;
  padding: 0.5rem 1rem 0.5rem 0.5rem;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}

.Status {
  box-sizing: border-box;
  padding: 0.5rem 1rem 0.5rem 0.5rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}
vue
<script setup lang="ts">
import { computed, shallowRef } from 'vue';
import { Autocomplete } from '@vase-ui/vue/autocomplete';

const limit = 8;

const value = shallowRef('');

const { contains } = Autocomplete.useFilter({ sensitivity: 'base' });

const totalMatches = computed(() => {
  const trimmed = value.value.trim();
  if (!trimmed) {
    return tags.length;
  }
  return tags.filter((t) => contains(t.value, trimmed)).length;
});

const moreCount = computed(() => Math.max(0, totalMatches.value - limit));

interface Tag {
  id: string;
  value: string;
}

// Larger dataset to make the limit visible.
const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 't7', value: 'frontend' },
  { id: 't8', value: 'backend' },
  { id: 't9', value: 'performance' },
  { id: 't10', value: 'accessibility' },
  { id: 't11', value: 'design' },
  { id: 't12', value: 'research' },
  { id: 't13', value: 'testing' },
  { id: 't14', value: 'infrastructure' },
  { id: 't15', value: 'documentation' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];
</script>

<template>
  <Autocomplete.Root :items="tags" v-model="value" :limit="limit">
    <label class="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
      Limit results to 8
      <Autocomplete.Input
        placeholder="e.g. component"
        class="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
      />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner class="outline-hidden" :side-offset="4">
        <Autocomplete.Popup
          class="w-[var(--anchor-width)] max-h-[min(var(--available-height),22.5rem)] max-w-[var(--available-width)] overflow-y-auto scroll-pt-1 scroll-pb-1 overscroll-contain border border-neutral-950 bg-white py-1 text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
        >
          <Autocomplete.Empty>
            <div class="py-2 pr-4 pl-2 text-sm leading-4 text-neutral-500 dark:text-neutral-400">
              No results found for "{{ value }}"
            </div>
          </Autocomplete.Empty>

          <Autocomplete.List>
            <template #item="{ item }">
              <Autocomplete.Item
                class="flex cursor-default py-2 pr-2 pl-2 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white"
                :value="item"
                >{{ item.value }}</Autocomplete.Item
              >
            </template>
          </Autocomplete.List>

          <Autocomplete.Status>
            <div
              v-if="moreCount > 0"
              class="py-2 pr-4 pl-2 text-sm text-neutral-500 dark:text-neutral-400"
            >
              {{ `Hiding ${moreCount} results (type a more specific query to narrow results)` }}
            </div>
          </Autocomplete.Status>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>

Auto highlight ​

The autoHighlight prop of <Autocomplete.Root> gives the highlight to the first item that matches the typed text. Set it to "always" to also highlight the first item while the text is empty and at each open, such as for an inline list in a dialog.

Use it with the keepHighlight and highlightItemOnHover props to set the highlight for the pointer.

vue
<script setup lang="ts">
import { Autocomplete } from '@vase-ui/vue/autocomplete';
import styles from './index.module.css';

interface Tag {
  id: string;
  value: string;
}

const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];
</script>

<template>
  <Autocomplete.Root :items="tags" auto-highlight>
    <label :class="styles.Label">
      Auto highlight on type
      <Autocomplete.Input placeholder="e.g. feature" :class="styles.Input" />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner :class="styles.Positioner" :side-offset="4">
        <Autocomplete.Popup :class="styles.Popup">
          <Autocomplete.Empty>
            <div :class="styles.Empty">No tags found.</div>
          </Autocomplete.Empty>
          <Autocomplete.List :class="styles.List">
            <template #item="{ item }">
              <Autocomplete.Item :class="styles.Item" :value="item">{{
                item.value
              }}</Autocomplete.Item>
            </template>
          </Autocomplete.List>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>
css
.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Positioner {
  outline: 0;
}

.Popup {
  box-sizing: border-box;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-width: var(--available-width);
  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.List {
  box-sizing: border-box;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-block: 0.25rem;
  scroll-padding-block: 0.25rem;
  outline: 0;
  max-height: min(22.5rem, var(--available-height));

  &[data-empty] {
    padding: 0;
  }
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding-block: 0.5rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  display: flex;
  font-size: 0.875rem;
  line-height: 1rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
    color: white;

    @media (prefers-color-scheme: dark) {
      color: oklch(14.5% 0 0deg);
    }
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(14.5% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: white;
    }
  }
}

.Empty {
  box-sizing: border-box;
  padding: 1rem 1rem 1rem 0.5rem;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}
vue
<script setup lang="ts">
import { Autocomplete } from '@vase-ui/vue/autocomplete';

interface Tag {
  id: string;
  value: string;
}

const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];
</script>

<template>
  <Autocomplete.Root :items="tags" auto-highlight>
    <label class="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
      Auto highlight on type
      <Autocomplete.Input
        placeholder="e.g. feature"
        class="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
      />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner class="outline-hidden" :side-offset="4">
        <Autocomplete.Popup
          class="w-[var(--anchor-width)] max-w-[var(--available-width)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
        >
          <Autocomplete.Empty>
            <div class="py-4 pr-4 pl-2 text-sm leading-4 text-neutral-500 dark:text-neutral-400">
              No tags found.
            </div>
          </Autocomplete.Empty>
          <Autocomplete.List
            class="outline-0 overflow-y-auto scroll-py-[0.25rem] py-1 overscroll-contain max-h-[min(22.5rem,var(--available-height))] data-empty:p-0"
          >
            <template #item="{ item }">
              <Autocomplete.Item
                class="flex cursor-default items-center gap-2 py-2 pr-2 pl-2 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white"
                :value="item"
                >{{ item.value }}</Autocomplete.Item
              >
            </template>
          </Autocomplete.List>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>

Virtualized ​

Use a virtualization library, such as @tanstack/vue-virtual, to handle a large data set.

vue
<script setup lang="ts">
import { useTemplateRef } from 'vue';
import { Autocomplete } from '@vase-ui/vue/autocomplete';
import styles from './index.module.css';
import VirtualizedList from './VirtualizedList.vue';

const list = useTemplateRef<InstanceType<typeof VirtualizedList>>('list');

function scrollToHighlightedItem(
  item: VirtualizedItem | undefined,
  { reason, index }: Autocomplete.Root.HighlightEventDetails,
) {
  const virtualizer = list.value?.virtualizer;

  if (!item || !virtualizer) {
    return;
  }

  const isStart = index === 0;
  const isEnd = index === virtualizer.options.count - 1;
  const shouldScroll = reason === 'none' || (reason === 'keyboard' && (isStart || isEnd));

  if (shouldScroll) {
    queueMicrotask(() => {
      virtualizer.scrollToIndex(index, { align: isEnd ? 'start' : 'end' });
    });
  }
}

interface VirtualizedItem {
  id: string;
  name: string;
}

function getItemLabel(item: VirtualizedItem | null) {
  return item ? item.name : '';
}

const virtualizedItems: VirtualizedItem[] = Array.from({ length: 10000 }, (_, index) => {
  const id = String(index + 1);
  const indexLabel = id.padStart(4, '0');
  return { id, name: `Item ${indexLabel}` };
});
</script>

<template>
  <Autocomplete.Root
    virtualized
    :items="virtualizedItems"
    open-on-input-click
    :item-to-string-value="getItemLabel"
    @item-highlighted="scrollToHighlightedItem"
  >
    <label :class="styles.Label">
      Search 10,000 items
      <Autocomplete.Input :class="styles.Input" />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner :class="styles.Positioner" :side-offset="4">
        <Autocomplete.Popup :class="styles.Popup">
          <Autocomplete.Empty>
            <div :class="styles.Empty">No items found.</div>
          </Autocomplete.Empty>
          <Autocomplete.List :class="styles.List">
            <VirtualizedList ref="list" />
          </Autocomplete.List>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>
vue
<script setup lang="ts">
import { computed, shallowRef } from 'vue';
import { useVirtualizer } from '@tanstack/vue-virtual';
import { Autocomplete } from '@vase-ui/vue/autocomplete';
import styles from './index.module.css';

interface VirtualizedItem {
  id: string;
  name: string;
}

const filteredItems = Autocomplete.useFilteredItems<VirtualizedItem>();

const scrollElement = shallowRef<HTMLDivElement | null>(null);

const virtualizer = useVirtualizer(
  computed(() => ({
    count: filteredItems.value.length,
    getScrollElement: () => scrollElement.value,
    estimateSize: () => 32,
    overscan: 20,
    paddingStart: 4,
    paddingEnd: 4,
    scrollPaddingEnd: 4,
    scrollPaddingStart: 4,
  })),
);

const totalSize = computed(() => virtualizer.value.getTotalSize());
const rows = computed(() =>
  virtualizer.value.getVirtualItems().flatMap((virtualItem) => {
    const item = filteredItems.value[virtualItem.index];
    return item ? [{ virtualItem, item }] : [];
  }),
);

// Vue calls a function ref again on each patch, and `measure()` makes the next patch.
function setScrollElement(element: unknown) {
  if (element === scrollElement.value) {
    return;
  }
  scrollElement.value = element as HTMLDivElement | null;
  if (element) {
    virtualizer.value.measure();
  }
}

// A part gives its element through the exposed `element`.
function measureItem(item: unknown) {
  virtualizer.value.measureElement(
    (item as { element: HTMLElement | null } | null)?.element ?? null,
  );
}

defineExpose({ virtualizer });
</script>

<template>
  <div
    v-if="filteredItems.length > 0"
    :ref="setScrollElement"
    role="presentation"
    :class="styles.Scroller"
    :style="{ '--total-size': `${totalSize}px` }"
  >
    <div
      role="presentation"
      :class="styles.VirtualizedPlaceholder"
      :style="{ height: `${totalSize}px` }"
    >
      <Autocomplete.Item
        v-for="{ virtualItem, item } in rows"
        :key="virtualItem.index"
        :ref="measureItem"
        :index="virtualItem.index"
        :data-index="virtualItem.index"
        :value="item"
        :class="styles.Item"
        :aria-setsize="filteredItems.length"
        :aria-posinset="virtualItem.index + 1"
        :style="{
          position: 'absolute',
          top: 0,
          left: 0,
          width: '100%',
          height: `${virtualItem.size}px`,
          transform: `translateY(${virtualItem.start}px)`,
        }"
      >
        {{ item.name }}
      </Autocomplete.Item>
    </div>
  </div>
</template>
css
.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Positioner {
  outline: 0;
}

.Popup {
  box-sizing: border-box;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-width: var(--available-width);
  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.Scroller {
  box-sizing: border-box;
  height: min(22.5rem, var(--total-size));
  max-height: var(--available-height);
  overflow: auto;
  overscroll-behavior: contain;
  scroll-padding-block: 0.25rem;
}

.VirtualizedPlaceholder {
  width: 100%;
  position: relative;
}

.List {
  padding: 0;
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding-block: 0.5rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  display: flex;
  font-size: 0.875rem;
  line-height: 1rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
    color: white;

    @media (prefers-color-scheme: dark) {
      color: oklch(14.5% 0 0deg);
    }
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(14.5% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: white;
    }
  }
}

.Empty {
  box-sizing: border-box;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);
  padding: 0.75rem 0.5rem;

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}
vue
<script setup lang="ts">
import { useTemplateRef } from 'vue';
import { Autocomplete } from '@vase-ui/vue/autocomplete';
import VirtualizedList from './VirtualizedList.vue';

const list = useTemplateRef<InstanceType<typeof VirtualizedList>>('list');

function scrollToHighlightedItem(
  item: VirtualizedItem | undefined,
  { reason, index }: Autocomplete.Root.HighlightEventDetails,
) {
  const virtualizer = list.value?.virtualizer;

  if (!item || !virtualizer) {
    return;
  }

  const isStart = index === 0;
  const isEnd = index === virtualizer.options.count - 1;
  const shouldScroll = reason === 'none' || (reason === 'keyboard' && (isStart || isEnd));

  if (shouldScroll) {
    queueMicrotask(() => {
      virtualizer.scrollToIndex(index, { align: isEnd ? 'start' : 'end' });
    });
  }
}

interface VirtualizedItem {
  id: string;
  name: string;
}

function getItemLabel(item: VirtualizedItem | null) {
  return item ? item.name : '';
}

const virtualizedItems: VirtualizedItem[] = Array.from({ length: 10000 }, (_, index) => {
  const id = String(index + 1);
  const indexLabel = id.padStart(4, '0');
  return { id, name: `Item ${indexLabel}` };
});
</script>

<template>
  <Autocomplete.Root
    virtualized
    :items="virtualizedItems"
    open-on-input-click
    :item-to-string-value="getItemLabel"
    @item-highlighted="scrollToHighlightedItem"
  >
    <label class="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
      Search 10,000 items
      <Autocomplete.Input
        class="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
      />
    </label>

    <Autocomplete.Portal>
      <Autocomplete.Positioner class="outline-hidden" :side-offset="4">
        <Autocomplete.Popup
          class="w-[var(--anchor-width)] max-w-[var(--available-width)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
        >
          <Autocomplete.Empty>
            <div class="py-3 px-2 text-sm leading-4 text-neutral-500 dark:text-neutral-400">
              No items found.
            </div>
          </Autocomplete.Empty>
          <Autocomplete.List class="p-0">
            <VirtualizedList ref="list" />
          </Autocomplete.List>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>
vue
<script setup lang="ts">
import { computed, shallowRef } from 'vue';
import { useVirtualizer } from '@tanstack/vue-virtual';
import { Autocomplete } from '@vase-ui/vue/autocomplete';

interface VirtualizedItem {
  id: string;
  name: string;
}

const filteredItems = Autocomplete.useFilteredItems<VirtualizedItem>();

const scrollElement = shallowRef<HTMLDivElement | null>(null);

const virtualizer = useVirtualizer(
  computed(() => ({
    count: filteredItems.value.length,
    getScrollElement: () => scrollElement.value,
    estimateSize: () => 32,
    overscan: 20,
    paddingStart: 4,
    paddingEnd: 4,
    scrollPaddingEnd: 4,
    scrollPaddingStart: 4,
  })),
);

const totalSize = computed(() => virtualizer.value.getTotalSize());
const rows = computed(() =>
  virtualizer.value.getVirtualItems().flatMap((virtualItem) => {
    const item = filteredItems.value[virtualItem.index];
    return item ? [{ virtualItem, item }] : [];
  }),
);

// Vue calls a function ref again on each patch, and `measure()` makes the next patch.
function setScrollElement(element: unknown) {
  if (element === scrollElement.value) {
    return;
  }
  scrollElement.value = element as HTMLDivElement | null;
  if (element) {
    virtualizer.value.measure();
  }
}

// A part gives its element through the exposed `element`.
function measureItem(item: unknown) {
  virtualizer.value.measureElement(
    (item as { element: HTMLElement | null } | null)?.element ?? null,
  );
}

defineExpose({ virtualizer });
</script>

<template>
  <div
    v-if="filteredItems.length > 0"
    :ref="setScrollElement"
    role="presentation"
    class="h-[min(22.5rem,var(--total-size))] max-h-[var(--available-height)] overflow-auto overscroll-contain scroll-py-1"
    :style="{ '--total-size': `${totalSize}px` }"
  >
    <div role="presentation" class="relative w-full" :style="{ height: `${totalSize}px` }">
      <Autocomplete.Item
        v-for="{ virtualItem, item } in rows"
        :key="virtualItem.index"
        :ref="measureItem"
        :index="virtualItem.index"
        :data-index="virtualItem.index"
        :value="item"
        class="flex cursor-default py-2 pr-2 pl-2 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white"
        :aria-setsize="filteredItems.length"
        :aria-posinset="virtualItem.index + 1"
        :style="{
          position: 'absolute',
          top: 0,
          left: 0,
          width: '100%',
          height: `${virtualItem.size}px`,
          transform: `translateY(${virtualItem.start}px)`,
        }"
      >
        {{ item.name }}
      </Autocomplete.Item>
    </div>
  </div>
</template>

Memoizing items ​

A child component that takes the item as a prop is a simpler alternative to virtualization for a short list. Vue renders a child component again only when its props, its slots or the reactive values of its render change. A typed character then renders only the items that change. The open is not faster, because the popup mounts each item. For a long list, use virtualization.

vue
<script lang="ts">
export interface Suggestion {
  id: string;
  label: string;
  description: string;
}
</script>

<script setup lang="ts">
import { Autocomplete } from '@vase-ui/vue/autocomplete';

defineProps<{ item: Suggestion }>();
</script>

<template>
  <Autocomplete.Item :value="item">
    <span>{{ item.label }}</span>
    <span>{{ item.description }}</span>
  </Autocomplete.Item>
</template>
vue
<script setup lang="ts">
import { Autocomplete } from '@vase-ui/vue/autocomplete';
import SuggestionItem from './SuggestionItem.vue';
import type { Suggestion } from './SuggestionItem.vue';

const suggestions: Suggestion[] = [
  { id: 'a', label: 'Apple', description: 'A round fruit' },
  { id: 'b', label: 'Banana', description: 'A long fruit' },
];
</script>

<template>
  <Autocomplete.Root :items="suggestions">
    <Autocomplete.Input />
    <Autocomplete.Portal>
      <Autocomplete.Positioner>
        <Autocomplete.Popup>
          <Autocomplete.List>
            <template #item="{ item }">
              <SuggestionItem :item="item" />
            </template>
          </Autocomplete.List>
        </Autocomplete.Popup>
      </Autocomplete.Positioner>
    </Autocomplete.Portal>
  </Autocomplete.Root>
</template>

API reference ​

Autocomplete.Root ​

Groups the parts of the autocomplete. Renders a <div> element.

Props

PropTypeDefaultDescription
autoHighlightboolean | "always" | undefinedfalseThe first item gets the highlight after each typed text that is not empty. With 'always', also while the text is empty and at each open.
defaultOpenboolean | undefinedfalseThe popup is open at the mount of an uncontrolled root.
defaultValuestring | undefinedThe text of an uncontrolled root, at the mount and at a native reset of the form.
disabledboolean | undefinedfalseThe user cannot change the value, and the form gets no value.
filter((item: Item, query: string, itemToString?: FilterItemToString | undefined) => boolean) | null | undefinedKeeps an item for the query, the text with no space at the ends. null keeps each item. The default is contains of useFilter.
filteredItemsreadonly Item[] | readonly Group<Item>[] | undefinedThe items to show, in place of the result of filter. Keep the form of items.
formstring | undefinedThe id of the form of the value.
gridboolean | undefinedfalseArrow keys move in the rows and columns of Row parts.
highlightItemOnHoverboolean | undefinedtrueA move of the pointer highlights its item.
inlineboolean | undefinedfalseThe list is in place, with no popup. The input is the control and has aria-expanded.
itemsComboboxItems<Item, any> | undefinedThe data of the items. With items, the root filters the data, and the item slot of List renders each item that the filter keeps.
itemToStringValue((item: Item) => string) | undefinedThe text of an item in the input. An object with value and label gives its value.
keepHighlightboolean | undefinedfalseA pointer that leaves the list keeps the highlight.
limitnumber | undefined-1The largest number of items to show. -1 shows each item.
localeIntl.LocalesArgumentThe locale of the default filter. Else the nearest LocaleProvider, else 'en-US'.
loopFocusboolean | undefinedtrueThe arrow keys go from the last item to the input and to the first item.
modalboolean | undefinedfalseThe open popup locks the scroll of the page and blocks the pointer outside it.
mode"list" | "both" | "inline" | "none" | undefined'list'How the input completes the text: - 'list': the list filters by the text. - 'both': the list filters, and a key highlight shows the text of its item in the input. - 'inline': the list does not filter, and a key highlight shows the text of its item. - 'none': the list does not filter, and the input shows only the typed text.
modelValuestring | undefinedThe text of the input. v-model binds it. An inline completion in the input is not in it.
namestring | undefinedThe name of the value in the form data.
openboolean | undefinedThe popup is open. v-model:open binds it.
openOnInputClickboolean | undefinedfalseA press on the input opens the popup.
readonlyboolean | undefinedfalseThe user can browse the items, and cannot change the value.
requiredboolean | undefinedfalseThe form needs a value.
submitOnItemClickboolean | undefinedfalseA press on an item submits the form of the root.
virtualizedboolean | undefinedfalseThe developer renders a window of the items. The arrow keys count on the filtered items, and the list sets no scroll position. In a grid, the mounted Row with the most items gives the number of columns.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
defaultThe content of the part.

Events

EventArgumentsDescription
valueChange[value: string, details: AutocompleteRootChangeEventDetails]The text changes. details.cancel() keeps the text as it was. An action that gives the text that the root has, such as a press of the item with that text, sends no event.
update:modelValue[value: string]Comes after valueChange, unless a listener cancels the change. v-model uses it.
openChange[open: boolean, details: AutocompleteRootOpenChangeEventDetails]The popup opens or closes. details.cancel() keeps it as it was.
update:open[open: boolean]Comes after openChange, unless a listener cancels the change. v-model:open uses it.
itemHighlighted[value: Item | undefined, details: AutocompleteRootHighlightEventDetails]An item gets the highlight, or no item has it (undefined, with the index -1).

Exposed

MemberTypeDescription
close() => voidCloses the popup with the reason 'imperative-action'.
elementHTMLElement | nullThe element of the part, or null while it is not mounted.
inputElementHTMLInputElement | nullThe visually hidden text input of the form, while the root renders one.

Autocomplete.Value ​

The text of the input. Renders no element.

Slots

SlotSlot propsDescription
default
value: string
Formats the text of the input. With no slot, the part shows the text.

Autocomplete.Input ​

The text input. Renders an <input> element. Outside the popup, the input is the form control: it has the name, required and the text of the root. In the popup, the root gives the form a hidden input after its mount.

Props

PropTypeDefaultDescription
disabledboolean | undefinedfalseThe user cannot use the input.
idstring | undefinedThe id of the input. The default is the control id of the field, else a generated id. In the popup, the default is a generated id.

Slots

SlotSlot propsDescription
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxInputState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementTextElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[aria-expanded="true"]The popup is open, the list is inline, or the input is in the popup.
[data-popup-side]The side of the anchor that the open popup is on.
[data-list-empty]The list has no item to show.
:disabledThe input or the autocomplete is disabled.
:read-onlyThe autocomplete is read-only.
:requiredThe autocomplete is required, and the input is the control.
[aria-invalid="true"]In a Field.Root, the input is the control, and the value is invalid.
[data-valid]In a Field.Root, the input is the control, and the value is valid.
[data-touched]In a Field.Root, the input is the control, and the focus left it one time or more.
[data-dirty]In a Field.Root, the input is the control, and the value differs from the first value.
[data-filled]In a Field.Root, the input is the control and has text.

Autocomplete.InputGroup ​

Groups the input and its buttons. Renders a <div> element.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxInputGroupState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxInputGroupState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-popup-open]The popup is open.
[data-popup-side]The side of the anchor that the open popup is on.
[data-list-empty]The list has no item to show.
[data-readonly]The autocomplete is read-only.
[data-disabled]The autocomplete is disabled.
[data-valid]In a Field.Root, the value is valid.
[data-invalid]In a Field.Root, the value is invalid.
[data-touched]In a Field.Root, the focus left the control one time or more.
[data-dirty]In a Field.Root, the value differs from the first value.
[data-filled]In a Field.Root, the input has text.

Autocomplete.Trigger ​

A button that opens the popup. Renders a <button> element.

Props

PropTypeDefaultDescription
disabledboolean | undefinedfalseThe user cannot press the trigger.
idstring | undefinedThe id of the trigger. When the trigger is the control, the default is the control id of the field, else a generated id. Beside an input, the default is a generated id.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxTriggerState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxTriggerState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[aria-expanded="true"]The popup is open.
[data-popup-side]The side of the anchor that the open popup is on.
[data-list-empty]The list has no item to show.
[data-readonly]The autocomplete is read-only.
[data-required]The autocomplete is required.
:disabledThe trigger is a native button, and the trigger, the autocomplete or a fieldset around the trigger is disabled.
[aria-disabled="true"]The trigger is another element, and the trigger or the autocomplete is disabled.
[aria-invalid="true"]In a Field.Root, the trigger is the control, and the value is invalid.
[data-invalid]In a Field.Root, the trigger is beside the input, and the value is invalid.
[data-valid]In a Field.Root, the value is valid.
[data-touched]In a Field.Root, the focus left the control one time or more.
[data-dirty]In a Field.Root, the value differs from the first value.
[data-filled]In a Field.Root, the input has text.

Autocomplete.Icon ​

An icon that shows that the popup opens. Renders a <span> element.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxIconState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxIconState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Autocomplete.Clear ​

A button that clears the text. Renders a <button> element. Without keepMounted, it is in the DOM only while the input has text.

Props

PropTypeDefaultDescription
disabledboolean | undefinedfalseThe user cannot press the button.
keepMountedboolean | undefinedfalseThe button stays in the DOM with hidden while there is nothing to clear.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxClearState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxClearState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-popup-open]The popup is open.
[hidden]There is nothing to clear, and keepMounted keeps the button in the DOM.
:disabledThe element is a native button, and the button, the autocomplete or a fieldset around it is disabled.
[aria-disabled="true"]The element is not a native button, and the button or the autocomplete is disabled.

Autocomplete.List ​

The list of the items. Renders a <div> element.

Props

PropTypeDefaultDescription
idstring | undefinedThe id of the list. The default is a generated id.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxListState
The content of the part.
item
item: any
index: number
Renders each item that the filter keeps.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxListState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-empty]The list has no item to show.

Autocomplete.Portal ​

Moves its content to another place of the DOM, <body> by default. Renders a visually hidden <span> in place while the moved popup is open. Its aria-owns names the popup, so a screen reader reads the popup after the input. With keepMounted, the closed popup keeps its list and the text of an input in it until the next open.

Props

PropTypeDefaultDescription
deferboolean | undefinedfalseMoves the content after the other content of the same update mounted.
disabledboolean | undefinedfalseRenders the content in place, also on the server.
keepMountedboolean | undefinedfalseThe closed popup stays in the DOM, with hidden and inert.
toPortalTarget | undefinedThe target of the content. With no value, the target of the nearest portal above, else body. While it is null, the portal renders nothing.

Slots

SlotSlot propsDescription
defaultThe content of the part.

Autocomplete.Backdrop ​

An overlay under the popup. Renders a <div> element.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxBackdropState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxBackdropState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[hidden]The autocomplete is closed, and keepMounted of the portal keeps the backdrop in the DOM.

Autocomplete.Positioner ​

Places the popup at its anchor. Renders a <div> element.

Props

PropTypeDefaultDescription
alignAlign | undefined'center'The alignment of the popup on the side.
alignOffsetnumber | OffsetFunction | undefined0The offset on the align axis in pixels, or a function that gives it.
anchorAnchor | (() => Anchor)The element to place the popup against: an element, a virtual element, a template ref of a part, or a function that gives one. With no value, the active trigger.
arrowPaddingnumber | undefined5The smallest space between the arrow and an inner edge of its nearest positioned ancestor, in pixels.
collisionAvoidanceCollisionAvoidance | undefinedHow the popup avoids the edge of the boundary.
collisionBoundaryBoundary | undefined'clipping-ancestors'The area that holds the popup: an element, elements, or a rectangle.
collisionPaddingPadding | undefined5The space between the popup and the edge of the boundary, in pixels.
disableAnchorTrackingboolean | undefinedfalseThe popup does not follow a move of its anchor.
positionMethod"absolute" | "fixed" | undefined'absolute'The CSS position of the positioner.
sideSide | undefined'bottom'The side of the anchor for the popup. It can change to avoid a collision. inline-start and inline-end follow the CSS direction of the positioner: give the positioner dir when the target of its portal has another direction.
sideOffsetnumber | OffsetFunction | undefined0The distance from the anchor in pixels, or a function that gives it.
stickyboolean | undefinedfalseThe popup stays in the view after its anchor scrolls out of it.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxPositionerState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxPositionerState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-side]The side of the anchor that the popup is on: top, bottom, left, right, inline-start or inline-end.
[data-align]The alignment of the popup on its side: start, center or end.
[data-anchor-hidden]The anchor is out of view.
[data-empty]The list has no item to show.

CSS variables

VariableDescription
--anchor-widthThe width of the anchor, in pixels.
--anchor-heightThe height of the anchor, in pixels.
--available-widthThe width that the popup can fill before it overflows the boundary, in pixels.
--available-heightThe height that the popup can fill before it overflows the boundary, in pixels.
--transform-originThe point that the popup grows from, for the transform-origin of the popup. It is at the arrow, on the edge of the anchor. With no arrow, it is at the aligned edge of the popup or at the center of the anchor.

Autocomplete.Popup ​

The container of the list. Renders a <div> element.

Props

PropTypeDefaultDescription
finalFocusFocusTarget | undefinedThe element that gets the focus at the close. By default: the trigger that is the control, else the element that had the focus before the open. After an open by code, that element comes before the trigger. An input that is the control keeps the focus.
idstring | undefinedThe id of the popup. The default is a generated id.
initialFocusFocusTarget | undefinedThe element that gets the focus at the open, while the trigger is the control. By default: the input in the popup; after a touch, the popup.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxPopupState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxPopupState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-side]The side of the anchor that the popup is on: top, bottom, left, right, inline-start or inline-end.
[data-align]The alignment of the popup on its side: start, center or end.
[data-anchor-hidden]The anchor is out of view.
[data-empty]The list has no item to show.
[hidden]The autocomplete is closed, and keepMounted of the portal keeps the popup in the DOM.

Autocomplete.Arrow ​

An element that points to the center of the anchor. Renders a <div> element. Its place is in the padding box of its nearest positioned ancestor: the popup when the popup has a position, else the positioner.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxArrowState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxArrowState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-side]The side of the anchor that the popup is on.
[data-align]The alignment of the popup on its side: start, center or end.
[data-uncentered]The arrow cannot point to the center of the anchor.

Autocomplete.Status ​

Tells a screen reader the status of the list, such as a load. Renders a <div> element.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxStatusState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxStatusState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Autocomplete.Empty ​

The text of an empty list. Renders a <div> element.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxEmptyState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxEmptyState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Autocomplete.Row ​

A row of items in a grid. Renders a <div> element.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxRowState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxRowState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Autocomplete.Item ​

An item of the list. Renders a <div> element.

Props

PropTypeDefaultDescription
disabledboolean | undefinedfalseThe user cannot pick the item. The arrow keys still reach it.
idstring | undefinedThe id of the item. The default is a generated id.
indexnumber | undefinedThe place of the item in the filtered items, for a list that renders a window of them.
valueanynullThe value of a pick.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxItemState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxItemState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-highlighted]The item has the highlight.
[aria-disabled="true"]The item or the autocomplete is disabled.

Autocomplete.Group ​

Groups related items. Renders a <div> element.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxGroupState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxGroupState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Autocomplete.GroupLabel ​

Names a group. Renders a <div> element.

Props

PropTypeDefaultDescription
idstring | undefinedThe id of the label, which names its group. The default is a generated id.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxGroupLabelState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxGroupLabelState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Autocomplete.Separator ​

Separates groups of items. Renders a <div> element.

Props

PropTypeDefaultDescription
orientationOrientation | undefined'horizontal'The orientation of the separator.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ComboboxSeparatorState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ComboboxSeparatorState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-orientation]The orientation of the separator: horizontal or vertical.

useFilter ​

Returns a filter that matches items with a query through Intl.Collator, for a list that you filter yourself.

Parameters

ParameterTypeDescription
options?MaybeRefOrGetter<AutocompleteFilterOptions>

Return value

Filter

useFilteredItems ​

Returns the items that the filter of Autocomplete.Root keeps, for a component inside the root.

Return value

ComputedRef<T[]>

Differences from Base UI ​

Vase UI ports Autocomplete from Base UI 1.9.0.

API ​

Base UIVase UI
Autocomplete.RootThe root renders a <div> that holds the inputs of its form, so it takes as. @after-enter and @after-leave of a <Transition> run after the animations, in place of openChangeComplete and the unmount action. A template ref gives inputElement, close() and element, in place of inputRef and actionsRef. readonly has the name of the native attribute. Input and Trigger take id, because they have the role. form names the form of the inputs, as the native attribute does. No part sends close-press, because the popup has no hidden dismiss buttons, and no part of Autocomplete sends chip-remove-press.
Autocomplete.Input:focus, :focus-visible and :focus-within select the focus, so the state has no focused, and the part has no data-focused.
Autocomplete.InputGroup:focus, :focus-visible and :focus-within select the focus, so the state has no focused, and the part has no data-focused.
Autocomplete.TriggerThe trigger reads its element, so it needs no nativeButton. :focus, :focus-visible and :focus-within select the focus, so the state has no focused, and the part has no data-focused.
Autocomplete.ClearThe button reads its element, so it needs no nativeButton. A <Transition> animates it, so its state has no transitionStatus.
Autocomplete.PortalA <Teleport> moves the content, and to names its target. The portal has no element of its own, so it takes no as and has no element slot. It takes defer and disabled of <Teleport>.
Autocomplete.BackdropA <Transition> around the backdrop animates it, so its state has no transitionStatus.
Autocomplete.PopupA <Transition> around the popup animates it, and a kept popup has hidden, so its state has no transitionStatus.
Autocomplete.Collectionv-for over the items of a group, and the item slot of List, render the items. The part is cut.
Autocomplete.ItemThe item reads its element, so it needs no nativeButton. Autocomplete.Item is Combobox.Item, and it takes the props of that part.
Autocomplete.Groupv-for over the items of the group renders them, so the group takes no items.

Added in Vase UI ​

PartMemberWhy
Autocomplete.Rootas propThe root renders a <div> that holds the inputs of its form. as gives it another tag or a component.
Autocomplete.Rootreadonly propThe name of the native attribute. A read-only root opens and moves the highlight, and the user cannot change its text.
Autocomplete.Rootelement exposed memberA template ref of a part gives its element.
Autocomplete.Listitem slotThe slot renders each filtered item with { item, index }, in place of the function child of the list.
Autocomplete.Portaldefer propThe portal is a <Teleport>, and it takes the props of <Teleport>: defer waits for the other content of the update.
Autocomplete.Portaldisabled propA disabled portal renders its content in place, also on the server (VUE-128).
Autocomplete.Emptyempty state fieldThe state says that the list is empty, for an element of the developer in the element slot.

Return types ​

FunctionVase UI returnsWhy
Autocomplete.useFilterFilterThe composable returns Filter. AutocompleteFilter, the name that Base UI documents, is the same type.
Autocomplete.useFilteredItemsComputedRef<T[]>setup runs one time, so the composable returns a ComputedRef that follows each change of the filtered items. A plain array does not follow them.

Behavior ​

Vase UIEvidence
A native reset of the form gives a control the v-model value of its mount, or else the default value that it has at the reset, as a native control does. The control emits only update:modelValue, also when v-model controls it. Its field is then not dirty, and it has data-filled only when that value is not empty.
The label of an item can be a function that returns the content of the label, so one label can show in the value part and in its item at the same time. The text of such an item is the result of itemToStringLabel, or the text of its value. Base UI takes a React element as a label, and the text of that item is [object Object]. A label function that returns vnodes needs the virtual DOM runtime of Vue. An application in Vapor mode has it with the vaporInteropPlugin. With no plugin, a label is text, and the slot of the Value part and of the item gives other content.
A valueChange listener that calls details.cancel() stops the change of an uncontrolled Autocomplete.Root. The input and Autocomplete.Value keep the old text, and the root emits no update:modelValue. Base UI writes its state before it calls onValueChange, so a canceled change still changes an uncontrolled autocomplete.
A touch outside an open popup closes it only at a tap. A tap ends with pointerup in less than 1 s and moves 8 px or less. Chromium on Android sends click for a move of 8 px and not for 8.1 px, the smallest limit of the measured browsers. A touch that scrolls the page ends with pointercancel and closes nothing, as a native popover stays open in Chromium and WebKit. A drag of more than 8 px on an element that does not scroll also closes nothing. A tap on a backdrop, or on the element that a modal popup puts over the page, closes the popup and gives the page under it no click. In Base UI, a touch that scrolls the page closes a Popover, a Menu or a Select.
An arrow points at the center of its anchor. Its place is in the padding box of its nearest positioned ancestor, so the borders of a popup with a position do not move it. arrowPadding keeps the same space from each inner edge of that box. In the Popover demo of Base UI, the popup has position: relative and a 1 px border, and the arrow is 1.5 px off the center of its trigger.
--transform-origin is in the pixels of the popup, from its border box: the arrow or the aligned edge or the center of the anchor, on the edge of the anchor. A margin of the popup, a padding of the positioner, a margin that collapses through the popup and a zoom of the popup do not move it. A Select popup over the trigger grows from the middle of the value, also after it grows while the user scrolls the list. In the Popover demo of Base UI, a 16 px margin on the popup moves the origin 16 px off the edge of the trigger.
An arrow in a popup with a CSS zoom points at the center of its anchor. Its place and arrowPadding are in the pixels of its nearest positioned ancestor. A Select popup over the trigger with a zoom puts the text of the selected item over the text of the value. In the Popover demo of Base UI, a zoom: 2 on the popup moves the arrow 225 px off the center of the trigger.
A control in a field keeps the id of the developer in the server HTML when no Field.Label of the field renders before it, and a label that renders after the control has that id in for. When a label renders before the control, the control has the generated id of the field until the hydration, because that label names the generated id. Then the control takes its id, and the label follows. In each order, the label names the control in the server HTML. Base UI gives each control in a field the generated id until the hydration, also where no label needs it.
modal of Combobox.Root locks the scroll of the page and blocks the pointer outside the popup. It sets no focus trap. Tab from the last tab stop of the popup closes the popup and moves the focus on, as in Select and Menu. Each user thus has a way out of the popup with no close button. A modal Popover traps the focus only with a Popover.Close part, for the same reason. Base UI keeps the focus in a modal popup that holds the input: Tab from the last tab stop goes back to the input. Its way out for a touch screen reader is a hidden button with the fixed name "Dismiss".
While the popup of Combobox or Autocomplete is open, the rest of the page stays in the accessibility tree, also with modal. The combobox pattern of the WAI-ARIA Authoring Practices hides no content. A screen reader user can read the page and can reach the trigger and the clear button. An element with aria-owns at the place of the portal puts the popup after the input in the reading order. Base UI gives aria-hidden to each element outside the input group and the popup. Its way out for a touch screen reader is a hidden button with the fixed name "Dismiss".
A button part knows the element of a component in as or of the element slot only at the mount, and it needs no prop that says what the element is. Until the mount, the element has the attributes that suit a <button> and each other element: type="button", role="button", a tab stop, and the disabled state of both. So in the server HTML, Tab reaches a <div> before a lazy hydration, and a <button> does not submit its form. At the mount, the part keeps the attributes of its element. Base UI needs nativeButton set to false for an element that is not a <button>.

The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.