Autocomplete
An input that suggests options for the text that the user types.
<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>.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);
}
}<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>. WithoutkeepMounted, the popup mounts at each open, so the<Transition>needsappear. The popup stays in the DOM until its leave ends. See the animation guide.
Anatomy
Import the component and assemble its parts:
<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
Async search
Load the items from a remote source when the text changes, and show the status of the load in <Autocomplete.Status>.
<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>.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);
}
}<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.
<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>.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;
}
}
}<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:
<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><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>.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);
}
}<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.
<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>.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);
}
}<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.
<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>.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);
}
}<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.
<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>.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);
}
}<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.
<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><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>.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);
}
}<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><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.
<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><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
| Prop | Type | Default | Description |
|---|---|---|---|
autoHighlight | boolean | "always" | undefined | false | The 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. |
defaultOpen | boolean | undefined | false | The popup is open at the mount of an uncontrolled root. |
defaultValue | string | undefined | The text of an uncontrolled root, at the mount and at a native reset of the form. | |
disabled | boolean | undefined | false | The user cannot change the value, and the form gets no value. |
filter | ((item: Item, query: string, itemToString?: FilterItemToString | undefined) => boolean) | null | undefined | Keeps an item for the query, the text with no space at the ends. null keeps each item. The
default is contains of useFilter. | |
filteredItems | readonly Item[] | readonly Group<Item>[] | undefined | The items to show, in place of the result of filter. Keep the form of items. | |
form | string | undefined | The id of the form of the value. | |
grid | boolean | undefined | false | Arrow keys move in the rows and columns of Row parts. |
highlightItemOnHover | boolean | undefined | true | A move of the pointer highlights its item. |
inline | boolean | undefined | false | The list is in place, with no popup. The input is the control and has aria-expanded. |
items | ComboboxItems<Item, any> | undefined | The 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) | undefined | The text of an item in the input. An object with value and label gives its value. | |
keepHighlight | boolean | undefined | false | A pointer that leaves the list keeps the highlight. |
limit | number | undefined | -1 | The largest number of items to show. -1 shows each item. |
locale | Intl.LocalesArgument | The locale of the default filter. Else the nearest LocaleProvider, else 'en-US'. | |
loopFocus | boolean | undefined | true | The arrow keys go from the last item to the input and to the first item. |
modal | boolean | undefined | false | The 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. |
modelValue | string | undefined | The text of the input. v-model binds it. An inline completion in the input is not in it. | |
name | string | undefined | The name of the value in the form data. | |
open | boolean | undefined | The popup is open. v-model:open binds it. | |
openOnInputClick | boolean | undefined | false | A press on the input opens the popup. |
readonly | boolean | undefined | false | The user can browse the items, and cannot change the value. |
required | boolean | undefined | false | The form needs a value. |
submitOnItemClick | boolean | undefined | false | A press on an item submits the form of the root. |
virtualized | boolean | undefined | false | The 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. |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | The content of the part. |
Events
| Event | Arguments | Description |
|---|---|---|
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
| Member | Type | Description |
|---|---|---|
close | () => void | Closes the popup with the reason 'imperative-action'. |
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
inputElement | HTMLInputElement | null | The visually hidden text input of the form, while the root renders one. |
Autocomplete.Value
The text of the input. Renders no element.
Slots
| Slot | Slot props | Description |
|---|---|---|
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
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | undefined | false | The user cannot use the input. |
id | string | undefined | The 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
| Slot | Slot props | Description |
|---|---|---|
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxInputState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | TextElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[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. |
:disabled | The input or the autocomplete is disabled. |
:read-only | The autocomplete is read-only. |
:required | The 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
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxInputGroupState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxInputGroupState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[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
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | undefined | false | The user cannot press the trigger. |
id | string | undefined | The 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. | |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxTriggerState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxTriggerState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[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. |
:disabled | The 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
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxIconState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxIconState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The 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
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | undefined | false | The user cannot press the button. |
keepMounted | boolean | undefined | false | The button stays in the DOM with hidden while there is nothing to clear. |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxClearState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxClearState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[data-popup-open] | The popup is open. |
[hidden] | There is nothing to clear, and keepMounted keeps the button in the DOM. |
:disabled | The 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
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | undefined | The id of the list. The default is a generated id. | |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxListState | The content of the part. |
item | item: anyindex: number | Renders each item that the filter keeps. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxListState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[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
| Prop | Type | Default | Description |
|---|---|---|---|
defer | boolean | undefined | false | Moves the content after the other content of the same update mounted. |
disabled | boolean | undefined | false | Renders the content in place, also on the server. |
keepMounted | boolean | undefined | false | The closed popup stays in the DOM, with hidden and inert. |
to | PortalTarget | undefined | The 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
| Slot | Slot props | Description |
|---|---|---|
default | The content of the part. |
Autocomplete.Backdrop
An overlay under the popup.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxBackdropState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxBackdropState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[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
| Prop | Type | Default | Description |
|---|---|---|---|
align | Align | undefined | 'center' | The alignment of the popup on the side. |
alignOffset | number | OffsetFunction | undefined | 0 | The offset on the align axis in pixels, or a function that gives it. |
anchor | Anchor | (() => 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. | |
arrowPadding | number | undefined | 5 | The smallest space between the arrow and an inner edge of its nearest positioned ancestor, in pixels. |
collisionAvoidance | CollisionAvoidance | undefined | How the popup avoids the edge of the boundary. | |
collisionBoundary | Boundary | undefined | 'clipping-ancestors' | The area that holds the popup: an element, elements, or a rectangle. |
collisionPadding | Padding | undefined | 5 | The space between the popup and the edge of the boundary, in pixels. |
disableAnchorTracking | boolean | undefined | false | The popup does not follow a move of its anchor. |
positionMethod | "absolute" | "fixed" | undefined | 'absolute' | The CSS position of the positioner. |
side | Side | 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. |
sideOffset | number | OffsetFunction | undefined | 0 | The distance from the anchor in pixels, or a function that gives it. |
sticky | boolean | undefined | false | The popup stays in the view after its anchor scrolls out of it. |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxPositionerState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxPositionerState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[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
| Variable | Description |
|---|---|
--anchor-width | The width of the anchor, in pixels. |
--anchor-height | The height of the anchor, in pixels. |
--available-width | The width that the popup can fill before it overflows the boundary, in pixels. |
--available-height | The height that the popup can fill before it overflows the boundary, in pixels. |
--transform-origin | The 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
| Prop | Type | Default | Description |
|---|---|---|---|
finalFocus | FocusTarget | undefined | The 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. | |
id | string | undefined | The id of the popup. The default is a generated id. | |
initialFocus | FocusTarget | undefined | The 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. | |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxPopupState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxPopupState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[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
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxArrowState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxArrowState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[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
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxStatusState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxStatusState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Autocomplete.Empty
The text of an empty list.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxEmptyState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxEmptyState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The 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
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxRowState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxRowState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Autocomplete.Item
An item of the list.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | undefined | false | The user cannot pick the item. The arrow keys still reach it. |
id | string | undefined | The id of the item. The default is a generated id. | |
index | number | undefined | The place of the item in the filtered items, for a list that renders a window of them. | |
value | any | null | The value of a pick. |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxItemState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxItemState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[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
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxGroupState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxGroupState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Autocomplete.GroupLabel
Names a group.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | undefined | The id of the label, which names its group. The default is a generated id. | |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxGroupLabelState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxGroupLabelState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Autocomplete.Separator
Separates groups of items.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
orientation | Orientation | undefined | 'horizontal' | The orientation of the separator. |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A 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
| Slot | Slot props | Description |
|---|---|---|
default | state: ComboboxSeparatorState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ComboboxSeparatorState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[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
| Parameter | Type | Description |
|---|---|---|
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 UI | Vase UI |
|---|---|
Autocomplete.Root | The 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.Trigger | The 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.Clear | The button reads its element, so it needs no nativeButton. A <Transition> animates it, so its state has no transitionStatus. |
Autocomplete.Portal | A <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.Backdrop | A <Transition> around the backdrop animates it, so its state has no transitionStatus. |
Autocomplete.Popup | A <Transition> around the popup animates it, and a kept popup has hidden, so its state has no transitionStatus. |
Autocomplete.Collection | v-for over the items of a group, and the item slot of List, render the items. The part is cut. |
Autocomplete.Item | The item reads its element, so it needs no nativeButton. Autocomplete.Item is Combobox.Item, and it takes the props of that part. |
Autocomplete.Group | v-for over the items of the group renders them, so the group takes no items. |
Added in Vase UI
| Part | Member | Why |
|---|---|---|
Autocomplete.Root | as prop | The root renders a <div> that holds the inputs of its form. as gives it another tag or a component. |
Autocomplete.Root | readonly prop | The name of the native attribute. A read-only root opens and moves the highlight, and the user cannot change its text. |
Autocomplete.Root | element exposed member | A template ref of a part gives its element. |
Autocomplete.List | item slot | The slot renders each filtered item with { item, index }, in place of the function child of the list. |
Autocomplete.Portal | defer prop | The portal is a <Teleport>, and it takes the props of <Teleport>: defer waits for the other content of the update. |
Autocomplete.Portal | disabled prop | A disabled portal renders its content in place, also on the server (VUE-128). |
Autocomplete.Empty | empty state field | The state says that the list is empty, for an element of the developer in the element slot. |
Return types
| Function | Vase UI returns | Why |
|---|---|---|
Autocomplete.useFilter | Filter | The composable returns Filter. AutocompleteFilter, the name that Base UI documents, is the same type. |
Autocomplete.useFilteredItems | ComputedRef<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 UI | Evidence |
|---|---|
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.