vael-ui v0.5.7

Selection

Layout

Combobox

A searchable dropdown that also accepts free text. Use it for autocomplete, or for tags people create as they type.

Validating a form? vael-form wires this up with a schema

Install

import { Combobox } from 'vael-ui'

Playground

Placement, behaviour and animation

Usage
<script setup lang="ts">
import { ref } from 'vue'
import { Combobox } from 'vael-ui'

const items = [
  { label: 'Apple', value: 'apple' },
  { label: 'Banana', value: 'banana' },
  { label: 'Cherry', value: 'cherry' },
]
const value = ref<string | number | (string | number)[] | null>(null)
const open = ref(false)
</script>

<template>
  <Combobox
    placeholder="Choose an option"
    aria-label="Fruits"
    :items="items"
    v-model="value"
    v-model:open="open"
  />
</template>

Props

NameTypeDefaultDescription
itemsRequiredreadonly (T | SelectGroupData<T>)[]Options to choose from. Put { label, items } entries among them to show options under a group label, the same as Select.
placeholderstringText shown in the input while it's empty.
multiplebooleanfalseLets you select several items. The model becomes an array, the panel stays open on pick, and selections show as removable chips.
maxLabelsnumbermultiple only: how many chips show before the rest collapse into a "+N" chip. Unset, all show.
disabledbooleanfalseDisables the input and blocks interaction.
invalidbooleanfalseMarks the field invalid. ORed with the nearest Field's error state.
size'sm' | 'md' | 'lg''md'Input size.
loadingbooleanfalseShows a spinner in place of an empty list, or below the rows while more load.
clearablebooleanfalseShows a clear button once there's a selection or typed text; clearing empties both.
virtualizeboolean | SelectVirtualizeConfigtrue/false forces virtualization on/off; an object also tunes itemSize/overscan. Unset, it auto-virtualizes past 100 items.
namestringRenders hidden <input>(s) so a plain <form> post carries the selection, repeating name when multiple.
filterComboboxFilter<T>trueHow typed text filters items. true matches labels ignoring case and accents; a function matches your way. false shows items as-is so you can filter them yourself.
allowCustombooleanfalseLets typed text that isn't an item become the value, after a cancelable @create. Add it to items yourself if it should become a real option.
createOptionbooleantrueallowCustom only: appends a Create "…" row whenever the typed text isn't already an item's label. Customize it with #create.
commitOnBlurbooleanfalseallowCustom only: leaving the field with uncommitted text commits it (reason 'blur'). When false, the text reverts instead.
revertOnBlurbooleantrueLeaving the field puts back the selected item's label, dropping half-typed text. Set false for a search box, so what you typed stays when you click away. Escape still clears it.
tabBehavior'select' | 'create'What Tab commits before focus moves on. 'select': the highlighted row, once you type or move the highlight with the keyboard. 'create' (allowCustom only): the typed text, or the item it names. Tab always moves focus on; with multiple, Enter commits and keeps it, for the next tag.
openOnFocusbooleanOpens the panel on focus, before you type. Unset means on; set false to require typing first.
side'top' | 'right' | 'bottom' | 'left''bottom'Which side of the input the panel opens on.
align'start' | 'end' | 'center''start'How the panel aligns against the input along that side.
sideOffsetnumber8Gap between the input and the panel, in pixels.
alignOffsetnumber0Shifts the panel along the alignment axis, in pixels.
closeOnEscbooleantrueEscape key closes the panel (and reverts uncommitted typing).
closeOnOutsidebooleantrueClicking outside the panel, or tabbing away, closes it.
beforeCloseBeforeClose<PopoverCloseReason>Runs before the panel closes. Return true to close it, false to keep it open, or a promise of either, like () => confirmAction(…).result. The panel stays open while the promise is pending.
forceMountbooleanfalseKeeps it mounted while closed, toggled with v-show, so its content keeps its state. vael-ui then leaves its motion to you.
teleportTostring | HTMLElement'body'Teleport target: a CSS selector or element.
scrollFadebooleantrueMasks the panel's top/bottom edge as its content scrolls under it, signaling there's more.
maxPanelHeightnumber320Caps the panel height in pixels; the list scrolls past it. The viewport limits it too, so pass Infinity to fill the available space.
panelWidthPanelWidth'trigger'The panel's width. 'trigger' matches the field (at least 12rem); a width in px or any CSS length ('20rem') sets it.
motionComboboxMotiontrueHow it moves. false skips all of it: the panel opening, the list easing to its new height as you search, the clear button and the chips. An object moves the panel your way, like Popover's: values for the panel part, { spring }, or enter / leave hooks. chips does the same for the chips ({ chips: { initial: { chip: { opacity: 0, y: 4 } } } }).
uiPartial<Record<'root' | 'input' | 'positioner' | 'panel' | 'header' | 'list' | 'option' | 'group' | 'groupLabel' | 'empty' | 'footer' | 'clear' | 'chevron' | 'createIcon', UiPartValue>>Class and style overrides for each part.
modelValuestring | number | (string | number)[] | nullnullSelected value, or an array of values when multiple.
querystring''Input text. Holds the selected label in single mode; clears after each pick when multiple.
openbooleanfalseWhether the panel is open.

Events

NameTypeDescription
open-change(value: boolean, details: PopoverOpenChangeDetails)Fires before the panel closes; details.cancel() keeps it open.
select(item: T)Fires when you pick an item, including a pick that deselects it in multiple mode.
change(value: string | number | (string | number)[] | null)Fires when you change the value, including custom values, chip removal and clear.
reach-end()Fires when the list's last rows render, for loading more. Re-arms when the item count changes.
create(query: string, details: ComboboxCreateDetails)Fires before the typed text commits as a custom value. Call details.cancel() to veto it, for validation or an async create that sets the model itself.
update:modelValue(value: string | number | (string | number)[] | null)Fires when modelValue changes (v-model).
update:open(value: boolean)Fires when open changes (v-model:open).
update:query(value: string)Fires when query changes (v-model:query).

Slots

NameTypeDescription
startanyContent at the start of the field, such as an icon. It comes before any chips, so it stays put as they come and go.
endanyContent before the built-in clear button and chevron, inside Input's #end.
display{ value: string; placeholder: string; focused: boolean; }Draws the typed text yourself over the field's own text, as Input's #display. With multiple, it covers the query after the chips; the chips stay as they are.
header{ count: number; total: number; }Content above the listbox, inside the popover panel. count and total support a result-count readout.
item{ item: T; index: number; active: boolean; selected: boolean; query: string; group?: SelectGroupSlotData<T> | undefined; }Custom row content for each option. query is the trimmed typed text, for highlighting the match.
group-label{ group: SelectGroupSlotData<T>; items: T[]; }Custom content for a group's label. group is the object you passed, with your own fields; items are its options still showing. Screen readers read this content as the group's name.
create{ query: string; active: boolean; }Content of the allowCustom Create row.
emptyanyReplaces the text shown when no items match.

Exposed

NameTypeDescription
elHTMLElement | nullRoot element (the input's frame).
inputElHTMLInputElement | nullNative <input> element.
panelElHTMLElement | nullPanel element (null while closed).
positionerElHTMLElement | nullPositioning wrapper around the panel (null while closed).
listElHTMLElement | nullScrollable listbox element (null while closed).
highlightElHTMLElement | nullThe hover and keyboard highlight, sliding between options (null while closed). With motion: false it moves at once, for your own animation.
placementPlacementResolved placement after flipping, e.g. 'bottom-start'.
positionerStyleRecord<string, string>Inline positioning styles applied to the positioner.
isClosingbooleanTrue while a beforeClose close is pending.
open() => voidOpens the panel (no-op while disabled).
close() => voidCloses the panel, running @open-change and beforeClose first.
cancelClose() => voidCancels a close pending in beforeClose and keeps the panel open.
activeIndexnumberIndex of the highlighted row in the list (-1 for none).
scrollToIndex(index: number, align?: ScrollAlign | undefined) => voidScrolls the list to the row at index.

Motion New

Combobox's panel: values or hooks for its parts, like Popover's. chips moves the chips.

<Combobox
  :motion="{
    initial: { panel: { opacity: 0, y: 8, scale: 0.98 } },
    spring: { damping: 0.85, response: 0.35 },
  }"
/>
NameTypeDescription
initialexitpositionerpanelSets where each part comes in from and leaves to: any CSS property, plus x, y and blur. A plain number is px, or degrees for rotate.
enterleave(parts, context) => animationAnimates the parts with GSAP, motion or element.animate(). Return the animation and the component waits for it, and stops it if interrupted.
morphlinkedexitOrder'trigger' | Record<string, LinkedMotion> | 'reverse'morph: 'trigger' grows it out of the element that opened it. linked moves other elements with it, such as the page receding behind it. exitOrder: 'reverse' closes its children first.
chipsChipsMotionA select's chips (multiple): values or hooks for chip, and the list's stagger and leaving.
springdurationeasing{ damping, response } | number | stringTimes all of it: a spring, or a duration in ms with an easing. Never both.
falsebooleanTurns its motion off, for an animation of your own on its exposed elements.

Accessibility

Follows the Combobox pattern from the WAI-ARIA Authoring Practices Guide.

KeyAction
A letterTypes into the input and opens the list, filtered to the matches.
ArrowDownArrowUpOpens the list, then moves the highlight, wrapping at the ends.
HomeEndMoves the highlight to the first or last option while the list is open.
EnterSelects or toggles the highlighted option; with allowCustom and none highlighted, adds the typed text.
EscapeCloses the list and puts back the selected label if you typed.
BackspaceRemoves the last chip when the input is empty (multiple).
DeleteBackspaceRemoves the focused chip.
TabCloses the list and moves focus on; with tabBehavior, it commits the highlighted option or the typed text first.
  • The input has role="combobox" with aria-autocomplete="list", aria-expanded and aria-controls. Options have role="option" and aria-selected, plus aria-setsize and aria-posinset in a virtualized list.
  • Focus stays in the input, and aria-activedescendant points at the highlighted option. The list opens on focus unless openOnFocus is false.
  • Chips sit outside the input and are Tab stops. When a focused chip is removed, focus moves to the next chip, the previous one, or the input. Clear returns focus to the input.
  • Inside a Field, the label names the input. Without one, pass aria-label.