Select
A form control that selects a predefined value from a dropdown menu.
<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Select } from '@vase-ui/vue/select';
import styles from './index.module.css';
const apples = [
{ label: 'Gala', value: 'gala' },
{ label: 'Fuji', value: 'fuji' },
{ label: 'Honeycrisp', value: 'honeycrisp' },
{ label: 'Granny Smith', value: 'granny-smith' },
{ label: 'Pink Lady', value: 'pink-lady' },
];
const ICON_PROPS = {
width: '16',
height: '16',
viewBox: '0 0 16 16',
style: { display: 'block' },
};
const CaretUpDownIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [
h('path', { d: 'M11 10H5l3 3.5zm0-4H5l3-3.5z' }),
]);
const CheckIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'none', stroke: 'currentColor', ...attrs }, [
h('path', { d: 'm2.5 8.5 4 4 7-9' }),
]);
const CaretUpIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [
h('path', { d: 'M12 10H4l4-4.5z' }),
]);
const CaretDownIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [h('path', { d: 'M12 6H4l4 4.5z' })]);
const popupHiddenClass = styles.PopupHidden;
</script>
<template>
<div :class="styles.Field">
<Select.Root :items="apples">
<Select.Label :class="styles.Label">Apple</Select.Label>
<Select.Trigger :class="styles.Select">
<Select.Value :class="styles.Value" placeholder="Select apple" />
<Select.Icon>
<CaretUpDownIcon />
</Select.Icon>
</Select.Trigger>
<Select.Portal>
<Select.Positioner :class="styles.Positioner" :side-offset="4">
<Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
<Select.Popup :class="styles.Popup">
<Select.ScrollUpArrow :class="styles.ScrollUpArrow">
<CaretUpIcon />
</Select.ScrollUpArrow>
<Select.List :class="styles.List">
<Select.Item
v-for="{ label, value } in apples"
:key="label"
:value="value"
:class="styles.Item"
v-slot="{ state }"
>
<span v-if="state.selected" :class="styles.ItemIndicator">
<CheckIcon />
</span>
<Select.ItemText :class="styles.ItemText">{{ label }}</Select.ItemText>
</Select.Item>
</Select.List>
<Select.ScrollDownArrow :class="styles.ScrollDownArrow">
<CaretDownIcon />
</Select.ScrollDownArrow>
</Select.Popup>
</Transition>
</Select.Positioner>
</Select.Portal>
</Select.Root>
</div>
</template>.Field {
display: flex;
flex-direction: column;
align-items: start;
gap: 0.25rem;
}
.Label {
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 700;
color: oklch(14.5% 0 0deg);
cursor: default;
@media (prefers-color-scheme: dark) {
color: white;
}
}
.Value[data-placeholder] {
color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
.Select {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.75rem;
height: 2rem;
padding-left: 0.5rem;
padding-right: 0.25rem;
margin: 0;
outline: 0;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
font-family: inherit;
font-size: 0.875rem;
line-height: 1;
white-space: nowrap;
font-weight: 400;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
min-width: 10rem;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover:not(:disabled) {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&[data-pressed] {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
&:active:not(:disabled) {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:disabled {
color: oklch(55.6% 0 0deg);
border-color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
border-color: oklch(70.8% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
.Positioner {
outline: none;
z-index: 10;
-webkit-user-select: none;
user-select: none;
}
.Popup {
box-sizing: border-box;
outline: 0;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
background-clip: padding-box;
color: oklch(14.5% 0 0deg);
min-width: var(--anchor-width);
transform-origin: var(--transform-origin);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
transition:
transform 100ms ease-out,
opacity 100ms ease-out;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
&[data-side='none'] {
transition: none;
transform: translateY(1px);
opacity: 1;
min-width: calc(var(--anchor-width) + 1.75rem);
}
}
/* The enter-from and leave-to class of the `<Transition>` around the popup. */
.PopupHidden {
opacity: 0;
transform: scale(0.98);
}
.List {
box-sizing: border-box;
position: relative;
padding-block: 0.25rem;
overflow-y: auto;
max-height: var(--available-height);
scroll-padding-block: 1.5rem;
}
.Item {
box-sizing: border-box;
outline: 0;
font-size: 0.875rem;
line-height: 1.25rem;
padding-block: 0.375rem;
padding-left: 0.625rem;
padding-right: 1rem;
display: grid;
gap: 0.5rem;
align-items: center;
grid-template-columns: 1rem 1fr;
cursor: default;
-webkit-user-select: none;
user-select: none;
&[data-highlighted] {
background-color: oklch(14.5% 0 0deg);
color: white;
@media (prefers-color-scheme: dark) {
background-color: white;
color: oklch(14.5% 0 0deg);
}
}
}
.ItemIndicator {
grid-column-start: 1;
}
.ItemText {
grid-column-start: 2;
}
.ScrollUpArrow,
.ScrollDownArrow {
width: 100%;
background-color: white;
z-index: 1;
text-align: center;
cursor: default;
height: 1rem;
font-size: 0.75rem;
display: flex;
align-items: center;
justify-content: center;
@media (prefers-color-scheme: dark) {
background-color: oklch(14.5% 0 0deg);
}
&::before {
content: '';
position: absolute;
width: 100%;
height: 100%;
left: 0;
}
}
.ScrollUpArrow {
top: 0;
&[data-side='none'] {
&::before {
top: -100%;
}
}
}
.ScrollDownArrow {
bottom: 0;
&[data-side='none'] {
&::before {
bottom: -100%;
}
}
}<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Select } from '@vase-ui/vue/select';
const apples = [
{ label: 'Gala', value: 'gala' },
{ label: 'Fuji', value: 'fuji' },
{ label: 'Honeycrisp', value: 'honeycrisp' },
{ label: 'Granny Smith', value: 'granny-smith' },
{ label: 'Pink Lady', value: 'pink-lady' },
];
const ICON_PROPS = {
width: '16',
height: '16',
viewBox: '0 0 16 16',
style: { display: 'block' },
};
const CaretUpDownIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [
h('path', { d: 'M11 10H5l3 3.5zm0-4H5l3-3.5z' }),
]);
const CheckIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'none', stroke: 'currentColor', ...attrs }, [
h('path', { d: 'm2.5 8.5 4 4 7-9' }),
]);
const CaretUpIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [
h('path', { d: 'M12 10H4l4-4.5z' }),
]);
const CaretDownIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [h('path', { d: 'M12 6H4l4 4.5z' })]);
const popupHiddenClass = 'scale-[0.98] opacity-0';
</script>
<template>
<div class="flex flex-col items-start gap-1">
<Select.Root :items="apples">
<Select.Label class="cursor-default text-sm font-bold text-neutral-950 dark:text-white"
>Apple</Select.Label
>
<Select.Trigger
class="flex h-8 min-w-40 items-center justify-between gap-3 pl-2 pr-1 text-sm leading-none whitespace-nowrap border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 text-neutral-950 dark:text-white select-none hover:enabled:bg-neutral-100 dark:hover:enabled:bg-neutral-800 active:enabled:bg-neutral-200 dark:active:enabled:bg-neutral-700 disabled:border-neutral-500 disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:disabled:border-neutral-400 dark:disabled:text-neutral-400 data-pressed:bg-neutral-100 dark:data-pressed:bg-neutral-800 font-normal focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
>
<Select.Value
class="data-placeholder:text-neutral-500 dark:data-placeholder:text-neutral-400"
placeholder="Select apple"
/>
<Select.Icon>
<CaretUpDownIcon />
</Select.Icon>
</Select.Trigger>
<Select.Portal>
<Select.Positioner class="outline-hidden select-none z-10" :side-offset="4">
<Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
<Select.Popup
class="group min-w-[var(--anchor-width)] origin-[var(--transform-origin)] bg-clip-padding border border-neutral-950 bg-white text-neutral-950 outline-hidden shadow-[0.25rem_0.25rem_0] shadow-black/12 transition-[scale,opacity] duration-100 ease-out data-[side=none]:translate-y-px data-[side=none]:min-w-[calc(var(--anchor-width)+1.75rem)] data-[side=none]:scale-100 data-[side=none]:opacity-100 data-[side=none]:transition-none dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
>
<Select.ScrollUpArrow
class="top-0 z-[1] flex h-4 w-full cursor-default items-center justify-center bg-white text-center text-xs before:absolute data-[side=none]:before:top-[-100%] before:left-0 before:h-full before:w-full before:content-[''] dark:bg-neutral-950"
>
<CaretUpIcon />
</Select.ScrollUpArrow>
<Select.List
class="relative py-1 scroll-py-6 overflow-y-auto max-h-[var(--available-height)]"
>
<Select.Item
v-for="{ label, value } in apples"
:key="label"
:value="value"
class="grid cursor-default grid-cols-[1rem_1fr] items-center gap-2 py-1.5 pr-4 pl-2.5 text-sm outline-hidden select-none data-highlighted:bg-neutral-950 data-highlighted:text-white dark:data-highlighted:bg-white dark:data-highlighted:text-neutral-950"
v-slot="{ state }"
>
<span v-if="state.selected" class="col-start-1">
<CheckIcon />
</span>
<Select.ItemText class="col-start-2">{{ label }}</Select.ItemText>
</Select.Item>
</Select.List>
<Select.ScrollDownArrow
class="bottom-0 z-[1] flex h-4 w-full cursor-default items-center justify-center bg-white text-center text-xs before:absolute before:left-0 before:h-full before:w-full before:content-[''] data-[side=none]:before:bottom-[-100%] dark:bg-neutral-950"
>
<CaretDownIcon />
</Select.ScrollDownArrow>
</Select.Popup>
</Transition>
</Select.Positioner>
</Select.Portal>
</Select.Root>
</div>
</template>Usage guidelines
- Use Combobox for a long list: Select has no filter, only the keyboard typeahead. When the list needs a filter, use Combobox.
- Special positioning: By default, the popup covers its trigger, with the text of the selected item over the text of the value. See Positioning to turn this off or to change it.
- Form controls must have an accessible name: Use
<Select.Label>, or give<Select.Trigger>anaria-labelwhen no label is visible. See Labeling a select and the forms guide. - Closing animations: Put a
<Transition>around<Select.Popup>. The popup stays visible until its leave ends. See the animation guide.
Anatomy
Import the component and assemble its parts:
<script setup lang="ts">
import { Select } from '@vase-ui/vue/select';
</script>
<template>
<Select.Root>
<Select.Label />
<Select.Trigger>
<Select.Value />
<Select.Icon />
</Select.Trigger>
<Select.Portal>
<Select.Backdrop />
<Select.Positioner>
<Select.Popup>
<Select.ScrollUpArrow />
<Select.Arrow />
<Select.List>
<Select.Item>
<Select.ItemText />
</Select.Item>
<Select.Separator />
<Select.Group>
<Select.GroupLabel />
</Select.Group>
</Select.List>
<Select.ScrollDownArrow />
</Select.Popup>
</Select.Positioner>
</Select.Portal>
</Select.Root>
</template>Positioning
<Select.Positioner> has the prop alignItemWithTrigger. By default, it makes the popup cover the trigger, with the text of the selected item over the text of the value.
While this mode is active, data-side is "none" on Select.Popup and Select.Positioner.
To place the popup at a side of the trigger, set alignItemWithTrigger to false. While it is true (the default), these rules apply:
- Interaction type: The mode is off when the popup opens with a touch pointer.
- Viewport space: The viewport must have enough room to put the selected item over the text of the value with a popup that is not too short. If it does not, the popup uses the default positioning. Set
min-heighton<Select.Positioner>to change the limit. A smaller value makes this occur less often. The trigger must also be at least 20px from the top and from the bottom of the viewport. If it is not, the popup uses the default positioning. - Other positioning props: Props such as
sideandalignhave no effect while the mode is active. They apply when the prop isfalse, and when the popup uses the default positioning.
While the popup covers the trigger and has a <Select.List>, <Select.Popup> is a flex column. The list takes the height that the other children of the popup leave, so a heading or a footer keeps its room. As in each flex container, the margins of two children next to each other add up, and a child keeps the margins of its own children in its box.
Examples
Typed component
Select.Root is a generic component. Its Value type follows the modelValue and defaultValue props. Its Multiple type follows multiple.
To fix both types, give the part its type arguments in an instantiation expression. Use the result as a component:
<script setup lang="ts">
import { Select } from '@vase-ui/vue/select';
type Font = 'serif' | 'sans' | 'mono';
// The type arguments are `Value` and `Multiple`.
const FontSelect = Select.Root<Font, false>;
function log(value: Font | null) {
console.log(value);
}
</script>
<template>
<FontSelect default-value="serif" @value-change="log">
<!-- ... -->
</FontSelect>
</template>A wrapper that declares defineProps<SelectRootProps<Value, Multiple>>() and forwards the props with v-bind does not work. Vue gives an absent Boolean prop the value false. The wrapper then passes open as false, and the select cannot open. Wrap a part shows a generic wrapper that keeps the types.
Formatting the value
By default, <Select.Value> renders the raw value.
Give the items prop to <Select.Root> to render the label of the selected item:
<script setup lang="ts">
import { Select } from '@vase-ui/vue/select';
const items = [
{ value: null, label: 'Select theme' },
{ value: 'system', label: 'System default' },
{ value: 'light', label: 'Light' },
{ value: 'dark', label: 'Dark' },
];
</script>
<template>
<Select.Root :items="items">
<Select.Value />
</Select.Root>
</template>The default slot of <Select.Value> renders a formatted value. Its slot prop value is the selected value:
<script setup lang="ts">
import { Select } from '@vase-ui/vue/select';
const items: Record<string, string> = {
monospace: 'Monospace',
serif: 'Serif',
'sans-serif': 'Sans-serif',
};
</script>
<template>
<Select.Root :items="items">
<Select.Value v-slot="{ value }">
<span :style="{ fontFamily: value }">{{ items[value] }}</span>
</Select.Value>
</Select.Root>
</template>To avoid the lookup, use object values for the items.
Labeling a select
Use <Select.Label> to give the trigger a visible label:
<script setup lang="ts">
import { Select } from '@vase-ui/vue/select';
</script>
<template>
<Select.Root>
<Select.Label>Theme</Select.Label>
<!-- ... -->
</Select.Root>
</template><Select.Label> renders a <div>. A click on it moves the focus to the trigger and does not open the popup.
In a Field.Root, <Field.Label> is a native <label> of the trigger. A click on it opens the popup, as a click on the trigger does.
Placeholder values
To show a placeholder, set the placeholder prop of <Select.Value>:
<script setup lang="ts">
import { Select } from '@vase-ui/vue/select';
const items = [
{ value: 'system', label: 'System default' },
{ value: 'light', label: 'Light' },
{ value: 'dark', label: 'Dark' },
];
</script>
<template>
<Select.Root :items="items">
<Select.Value placeholder="Select theme" />
</Select.Root>
</template>With a placeholder, the user cannot clear the value with the select itself. To make the value clearable from the popup, add an item with a null value to the list. While nothing is selected, <Select.Value> shows the label of that item:
<script setup lang="ts">
import { Select } from '@vase-ui/vue/select';
const items = [
{ value: null, label: 'Select theme' },
{ value: 'system', label: 'System default' },
{ value: 'light', label: 'Light' },
{ value: 'dark', label: 'Dark' },
];
</script>
<template>
<Select.Root :items="items">
<Select.Value />
</Select.Root>
</template>Multiple selection
Add the multiple prop to <Select.Root> to allow more than one selected item. The value is then an array.
<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Select } from '@vase-ui/vue/select';
import styles from './index.module.css';
const languages = {
javascript: 'JavaScript',
typescript: 'TypeScript',
python: 'Python',
java: 'Java',
csharp: 'C#',
php: 'PHP',
cpp: 'C++',
rust: 'Rust',
go: 'Go',
swift: 'Swift',
};
type Language = keyof typeof languages;
const values = Object.keys(languages) as Language[];
function renderValue(value: Language[]) {
if (value.length === 0) {
return 'Select languages';
}
const firstLanguage = languages[value[0]];
const additionalLanguages = value.length > 1 ? ` (+${value.length - 1} more)` : '';
return firstLanguage + additionalLanguages;
}
const ICON_PROPS = {
width: '16',
height: '16',
viewBox: '0 0 16 16',
style: { display: 'block' },
};
const CaretUpDownIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [
h('path', { d: 'M11 10H5l3 3.5zm0-4H5l3-3.5z' }),
]);
const CheckIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'none', stroke: 'currentColor', ...attrs }, [
h('path', { d: 'm2.5 8.5 4 4 7-9' }),
]);
const popupHiddenClass = styles.PopupHidden;
</script>
<template>
<div :class="styles.Field">
<Select.Root multiple :default-value="['javascript', 'typescript']">
<Select.Label :class="styles.Label">Languages</Select.Label>
<Select.Trigger :class="styles.Select">
<Select.Value v-slot="{ value }" :class="styles.Value">
{{ renderValue(value) }}
</Select.Value>
<Select.Icon>
<CaretUpDownIcon />
</Select.Icon>
</Select.Trigger>
<Select.Portal>
<Select.Positioner
:class="styles.Positioner"
:side-offset="4"
:align-item-with-trigger="false"
>
<Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
<Select.Popup :class="styles.Popup">
<Select.Item
v-for="value in values"
:key="value"
:value="value"
:class="styles.Item"
v-slot="{ state }"
>
<span v-if="state.selected" :class="styles.ItemIndicator">
<CheckIcon />
</span>
<Select.ItemText :class="styles.ItemText">{{ languages[value] }}</Select.ItemText>
</Select.Item>
</Select.Popup>
</Transition>
</Select.Positioner>
</Select.Portal>
</Select.Root>
</div>
</template>.Field {
display: flex;
flex-direction: column;
align-items: start;
gap: 0.25rem;
}
.Label {
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 700;
color: oklch(14.5% 0 0deg);
cursor: default;
@media (prefers-color-scheme: dark) {
color: white;
}
}
.Value[data-placeholder] {
color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
.Select {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.75rem;
height: 2rem;
padding-left: 0.5rem;
padding-right: 0.25rem;
margin: 0;
outline: 0;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
font-family: inherit;
font-size: 0.875rem;
line-height: 1;
white-space: nowrap;
font-weight: 400;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
min-width: 14rem;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover:not(:disabled) {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&[data-pressed] {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
&:active:not(:disabled) {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:disabled {
color: oklch(55.6% 0 0deg);
border-color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
border-color: oklch(70.8% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
.Positioner {
outline: none;
z-index: 10;
}
.Popup {
box-sizing: border-box;
outline: 0;
padding-block: 0.25rem;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
background-clip: padding-box;
color: oklch(14.5% 0 0deg);
min-width: var(--anchor-width);
transform-origin: var(--transform-origin);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
transition:
transform 100ms ease-out,
opacity 100ms ease-out;
overflow-y: auto;
max-height: var(--available-height);
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
&[data-side='none'] {
transition: none;
transform: none;
opacity: 1;
min-width: calc(var(--anchor-width) + 1.75rem);
}
}
/* The enter-from and leave-to class of the `<Transition>` around the popup. */
.PopupHidden {
opacity: 0;
transform: scale(0.98);
}
.Item {
box-sizing: border-box;
outline: 0;
font-size: 0.875rem;
line-height: 1.25rem;
padding-block: 0.375rem;
padding-left: 0.625rem;
padding-right: 0.625rem;
display: grid;
gap: 0.5rem;
align-items: center;
grid-template-columns: 1rem 1fr;
cursor: default;
-webkit-user-select: none;
user-select: none;
scroll-margin-block: 0.25rem;
@media (hover: hover) {
&[data-highlighted] {
background-color: oklch(14.5% 0 0deg);
color: white;
@media (prefers-color-scheme: dark) {
background-color: white;
color: oklch(14.5% 0 0deg);
}
}
}
}
.ItemIndicator {
grid-column-start: 1;
}
.ItemText {
grid-column-start: 2;
}<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Select } from '@vase-ui/vue/select';
const languages = {
javascript: 'JavaScript',
typescript: 'TypeScript',
python: 'Python',
java: 'Java',
csharp: 'C#',
php: 'PHP',
cpp: 'C++',
rust: 'Rust',
go: 'Go',
swift: 'Swift',
};
type Language = keyof typeof languages;
const values = Object.keys(languages) as Language[];
function renderValue(value: Language[]) {
if (value.length === 0) {
return 'Select languages';
}
const firstLanguage = languages[value[0]];
const additionalLanguages = value.length > 1 ? ` (+${value.length - 1} more)` : '';
return firstLanguage + additionalLanguages;
}
const ICON_PROPS = {
width: '16',
height: '16',
viewBox: '0 0 16 16',
style: { display: 'block' },
};
const CaretUpDownIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [
h('path', { d: 'M11 10H5l3 3.5zm0-4H5l3-3.5z' }),
]);
const CheckIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'none', stroke: 'currentColor', ...attrs }, [
h('path', { d: 'm2.5 8.5 4 4 7-9' }),
]);
const popupHiddenClass = 'scale-[0.98] opacity-0';
</script>
<template>
<div class="flex flex-col items-start gap-1">
<Select.Root multiple :default-value="['javascript', 'typescript']">
<Select.Label class="cursor-default text-sm font-bold text-neutral-950 dark:text-white"
>Languages</Select.Label
>
<Select.Trigger
class="flex h-8 min-w-[14rem] items-center justify-between gap-3 pl-2 pr-1 text-sm leading-none whitespace-nowrap border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 text-neutral-950 dark:text-white select-none hover:enabled:bg-neutral-100 dark:hover:enabled:bg-neutral-800 active:enabled:bg-neutral-200 dark:active:enabled:bg-neutral-700 disabled:border-neutral-500 disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:disabled:border-neutral-400 dark:disabled:text-neutral-400 data-pressed:bg-neutral-100 dark:data-pressed:bg-neutral-800 font-normal focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
>
<Select.Value
v-slot="{ value }"
class="data-placeholder:text-neutral-500 dark:data-placeholder:text-neutral-400"
>
{{ renderValue(value) }}
</Select.Value>
<Select.Icon>
<CaretUpDownIcon />
</Select.Icon>
</Select.Trigger>
<Select.Portal>
<Select.Positioner
class="outline-hidden z-10"
:side-offset="4"
:align-item-with-trigger="false"
>
<Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
<Select.Popup
class="group max-h-[var(--available-height)] min-w-[var(--anchor-width)] origin-[var(--transform-origin)] bg-clip-padding overflow-y-auto border border-neutral-950 bg-white py-1 text-neutral-950 outline-hidden shadow-[0.25rem_0.25rem_0] shadow-black/12 transition-[scale,opacity] duration-100 ease-out data-[side=none]:min-w-[calc(var(--anchor-width)+1.75rem)] data-[side=none]:scale-100 data-[side=none]:opacity-100 data-[side=none]:transition-none dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
>
<Select.Item
v-for="value in values"
:key="value"
:value="value"
class="grid cursor-default grid-cols-[1rem_1fr] items-center gap-2 py-1.5 pr-2.5 pl-2.5 text-sm outline-hidden select-none scroll-my-1 [@media(hover:hover)]:data-highlighted:bg-neutral-950 [@media(hover:hover)]:data-highlighted:text-white dark:[@media(hover:hover)]:data-highlighted:bg-white dark:[@media(hover:hover)]:data-highlighted:text-neutral-950"
v-slot="{ state }"
>
<span v-if="state.selected" class="col-start-1">
<CheckIcon />
</span>
<Select.ItemText class="col-start-2">{{ languages[value] }}</Select.ItemText>
</Select.Item>
</Select.Popup>
</Transition>
</Select.Positioner>
</Select.Portal>
</Select.Root>
</div>
</template>Object values
An item can have an object as its value. The default slot of <Select.Value> then gets the whole object, and the select needs no items for the lookup.
<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Select } from '@vase-ui/vue/select';
import styles from './index.module.css';
interface ShippingMethod {
id: string;
name: string;
duration: string;
price: string;
}
const shippingMethods: ShippingMethod[] = [
{
id: 'standard',
name: 'Standard',
duration: 'Delivers in 4-6 business days',
price: '$4.99',
},
{
id: 'express',
name: 'Express',
duration: 'Delivers in 2-3 business days',
price: '$9.99',
},
{
id: 'overnight',
name: 'Overnight',
duration: 'Delivers next business day',
price: '$19.99',
},
];
const ICON_PROPS = {
width: '16',
height: '16',
viewBox: '0 0 16 16',
style: { display: 'block' },
};
const CaretUpDownIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [
h('path', { d: 'M11 10H5l3 3.5zm0-4H5l3-3.5z' }),
]);
const CheckIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'none', stroke: 'currentColor', ...attrs }, [
h('path', { d: 'm2.5 8.5 4 4 7-9' }),
]);
const CaretUpIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [
h('path', { d: 'M12 10H4l4-4.5z' }),
]);
const CaretDownIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [h('path', { d: 'M12 6H4l4 4.5z' })]);
const popupHiddenClass = styles.PopupHidden;
</script>
<template>
<div :class="styles.Field">
<Select.Root :default-value="shippingMethods[0]" :item-to-string-value="(item) => item.id">
<Select.Label :class="styles.Label">Shipping method</Select.Label>
<Select.Trigger :class="styles.Select">
<Select.Value v-slot="{ value }">
<span :class="styles.ValueText">
<span :class="styles.ValuePrimary">{{ (value as ShippingMethod).name }}</span>
<span :class="styles.ValueSecondary">
{{ (value as ShippingMethod).duration }} ({{ (value as ShippingMethod).price }})
</span>
</span>
</Select.Value>
<Select.Icon>
<CaretUpDownIcon />
</Select.Icon>
</Select.Trigger>
<Select.Portal>
<Select.Positioner :class="styles.Positioner" :side-offset="4">
<Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
<Select.Popup :class="styles.Popup">
<Select.ScrollUpArrow :class="styles.ScrollUpArrow">
<CaretUpIcon />
</Select.ScrollUpArrow>
<Select.List :class="styles.List">
<Select.Item
v-for="method in shippingMethods"
:key="method.id"
:value="method"
:class="styles.Item"
v-slot="{ state }"
>
<span v-if="state.selected" :class="styles.ItemIndicator">
<CheckIcon />
</span>
<Select.ItemText :class="styles.ItemText">
<span :class="styles.ItemLabel">{{ method.name }}</span>
<span :class="styles.ItemDescription">
{{ method.duration }} ({{ method.price }})
</span>
</Select.ItemText>
</Select.Item>
</Select.List>
<Select.ScrollDownArrow :class="styles.ScrollDownArrow">
<CaretDownIcon />
</Select.ScrollDownArrow>
</Select.Popup>
</Transition>
</Select.Positioner>
</Select.Portal>
</Select.Root>
</div>
</template>.Field {
display: flex;
flex-direction: column;
align-items: start;
gap: 0.25rem;
}
.Label {
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 700;
color: oklch(14.5% 0 0deg);
cursor: default;
@media (prefers-color-scheme: dark) {
color: white;
}
}
.Select {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.75rem;
min-height: 2rem;
padding-block: 0.375rem;
padding-left: 0.5rem;
padding-right: 0.25rem;
margin: 0;
outline: 0;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
font-family: inherit;
font-size: 0.875rem;
line-height: 1;
white-space: nowrap;
font-weight: 400;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
min-width: 16rem;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover:not(:disabled) {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&[data-pressed] {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
&:active:not(:disabled) {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:disabled {
color: oklch(55.6% 0 0deg);
border-color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
border-color: oklch(70.8% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
.ValueText {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 0.125rem;
}
.ValuePrimary {
font-size: 0.875rem;
line-height: 1.25rem;
}
.ValueSecondary {
font-size: 0.75rem;
line-height: 1rem;
color: oklch(43.9% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
.Positioner {
outline: none;
z-index: 10;
-webkit-user-select: none;
user-select: none;
}
.Popup {
box-sizing: border-box;
outline: 0;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
background-clip: padding-box;
color: oklch(14.5% 0 0deg);
min-width: var(--anchor-width);
transform-origin: var(--transform-origin);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
transition:
transform 100ms ease-out,
opacity 100ms ease-out;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
&[data-side='none'] {
transition: none;
transform: translateY(1px);
opacity: 1;
min-width: calc(var(--anchor-width) + 1.75rem);
}
}
/* The enter-from and leave-to class of the `<Transition>` around the popup. */
.PopupHidden {
opacity: 0;
transform: scale(0.98);
}
.List {
box-sizing: border-box;
position: relative;
padding-block: 0.25rem;
overflow-y: auto;
max-height: var(--available-height);
scroll-padding-block: 1.5rem;
}
.Item {
box-sizing: border-box;
outline: 0;
font-size: 0.875rem;
line-height: 1.25rem;
padding-block: 0.375rem;
padding-left: 0.625rem;
padding-right: 1rem;
display: grid;
gap: 0.5rem;
align-items: flex-start;
grid-template-columns: 1rem 1fr;
cursor: default;
-webkit-user-select: none;
user-select: none;
&[data-highlighted] {
background-color: oklch(14.5% 0 0deg);
color: white;
@media (prefers-color-scheme: dark) {
background-color: white;
color: oklch(14.5% 0 0deg);
}
.ItemDescription {
color: oklch(70.8% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(43.9% 0 0deg);
}
}
}
}
.ItemIndicator {
grid-column-start: 1;
display: flex;
align-items: center;
justify-content: center;
align-self: start;
position: relative;
top: 0.4em;
}
.ItemText {
grid-column-start: 2;
display: flex;
flex-direction: column;
gap: 0.125rem;
}
.ItemLabel {
font-size: 0.875rem;
line-height: 1.25rem;
}
.ItemDescription {
font-size: 0.75rem;
line-height: 1rem;
color: oklch(43.9% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
.ScrollUpArrow,
.ScrollDownArrow {
width: 100%;
background-color: white;
z-index: 1;
text-align: center;
cursor: default;
height: 1rem;
font-size: 0.75rem;
display: flex;
align-items: center;
justify-content: center;
@media (prefers-color-scheme: dark) {
background-color: oklch(14.5% 0 0deg);
}
&::before {
content: '';
position: absolute;
width: 100%;
height: 100%;
left: 0;
}
}
.ScrollUpArrow {
top: 0;
&[data-side='none'] {
&::before {
top: -100%;
}
}
}
.ScrollDownArrow {
bottom: 0;
&[data-side='none'] {
&::before {
bottom: -100%;
}
}
}<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Select } from '@vase-ui/vue/select';
interface ShippingMethod {
id: string;
name: string;
duration: string;
price: string;
}
const shippingMethods: ShippingMethod[] = [
{
id: 'standard',
name: 'Standard',
duration: 'Delivers in 4-6 business days',
price: '$4.99',
},
{
id: 'express',
name: 'Express',
duration: 'Delivers in 2-3 business days',
price: '$9.99',
},
{
id: 'overnight',
name: 'Overnight',
duration: 'Delivers next business day',
price: '$19.99',
},
];
const ICON_PROPS = {
width: '16',
height: '16',
viewBox: '0 0 16 16',
style: { display: 'block' },
};
const CaretUpDownIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [
h('path', { d: 'M11 10H5l3 3.5zm0-4H5l3-3.5z' }),
]);
const CheckIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'none', stroke: 'currentColor', ...attrs }, [
h('path', { d: 'm2.5 8.5 4 4 7-9' }),
]);
const CaretUpIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [
h('path', { d: 'M12 10H4l4-4.5z' }),
]);
const CaretDownIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [h('path', { d: 'M12 6H4l4 4.5z' })]);
const popupHiddenClass = 'scale-[0.98] opacity-0';
</script>
<template>
<div class="flex flex-col items-start gap-1">
<Select.Root :default-value="shippingMethods[0]" :item-to-string-value="(item) => item.id">
<Select.Label class="cursor-default text-sm font-bold text-neutral-950 dark:text-white"
>Shipping method</Select.Label
>
<Select.Trigger
class="flex min-h-8 min-w-[16rem] items-center justify-between gap-3 pl-2 pr-1 py-1.5 text-sm leading-none whitespace-nowrap border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 text-neutral-950 dark:text-white select-none hover:enabled:bg-neutral-100 dark:hover:enabled:bg-neutral-800 active:enabled:bg-neutral-200 dark:active:enabled:bg-neutral-700 disabled:border-neutral-500 disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:disabled:border-neutral-400 dark:disabled:text-neutral-400 data-pressed:bg-neutral-100 dark:data-pressed:bg-neutral-800 font-normal focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
>
<Select.Value v-slot="{ value }">
<span class="flex flex-col items-start gap-0.5">
<span class="text-sm">{{ (value as ShippingMethod).name }}</span>
<span class="text-xs text-neutral-600 dark:text-neutral-400">
{{ (value as ShippingMethod).duration }} ({{ (value as ShippingMethod).price }})
</span>
</span>
</Select.Value>
<Select.Icon>
<CaretUpDownIcon />
</Select.Icon>
</Select.Trigger>
<Select.Portal>
<Select.Positioner class="outline-hidden select-none z-10" :side-offset="4">
<Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
<Select.Popup
class="group min-w-[var(--anchor-width)] origin-[var(--transform-origin)] bg-clip-padding border border-neutral-950 bg-white text-neutral-950 outline-hidden shadow-[0.25rem_0.25rem_0] shadow-black/12 transition-[scale,opacity] duration-100 ease-out data-[side=none]:translate-y-px data-[side=none]:min-w-[calc(var(--anchor-width)+1.75rem)] data-[side=none]:scale-100 data-[side=none]:opacity-100 data-[side=none]:transition-none dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
>
<Select.ScrollUpArrow
class="top-0 z-[1] flex h-4 w-full cursor-default items-center justify-center bg-white text-center text-xs before:absolute data-[side=none]:before:top-[-100%] before:left-0 before:h-full before:w-full before:content-[''] dark:bg-neutral-950"
>
<CaretUpIcon />
</Select.ScrollUpArrow>
<Select.List
class="relative py-1 scroll-py-6 overflow-y-auto max-h-[var(--available-height)]"
>
<Select.Item
v-for="method in shippingMethods"
:key="method.id"
:value="method"
class="group/item grid cursor-default grid-cols-[1rem_1fr] items-start gap-2 py-1.5 pr-4 pl-2.5 text-sm outline-hidden select-none data-highlighted:bg-neutral-950 data-highlighted:text-white dark:data-highlighted:bg-white dark:data-highlighted:text-neutral-950"
v-slot="{ state }"
>
<span
v-if="state.selected"
class="col-start-1 flex items-center justify-center self-start relative top-[0.4em]"
>
<CheckIcon />
</span>
<Select.ItemText class="col-start-2 flex flex-col gap-0.5">
<span class="text-sm">{{ method.name }}</span>
<span
class="text-xs text-neutral-600 group-data-highlighted/item:text-neutral-400 dark:text-neutral-400 dark:group-data-highlighted/item:text-neutral-600"
>
{{ method.duration }} ({{ method.price }})
</span>
</Select.ItemText>
</Select.Item>
</Select.List>
<Select.ScrollDownArrow
class="bottom-0 z-[1] flex h-4 w-full cursor-default items-center justify-center bg-white text-center text-xs before:absolute before:left-0 before:h-full before:w-full before:content-[''] data-[side=none]:before:bottom-[-100%] dark:bg-neutral-950"
>
<CaretDownIcon />
</Select.ScrollDownArrow>
</Select.Popup>
</Transition>
</Select.Positioner>
</Select.Portal>
</Select.Root>
</div>
</template>Grouped
Use <Select.Group> and <Select.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 { Select } from '@vase-ui/vue/select';
const groups = [
{
value: 'Fruits',
items: [
{ value: 'apple', label: 'Apple' },
{ value: 'banana', label: 'Banana' },
],
},
{
value: 'Vegetables',
items: [
{ value: 'carrot', label: 'Carrot' },
{ value: 'spinach', label: 'Spinach' },
],
},
];
</script>
<template>
<Select.Root :items="groups" />
</template><script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Select } from '@vase-ui/vue/select';
import styles from './index.module.css';
const groupedProduce = [
{
value: 'Fruits',
items: [
{ value: 'apple', label: 'Apple' },
{ value: 'banana', label: 'Banana' },
{ value: 'mango', label: 'Mango' },
{ value: 'kiwi', label: 'Kiwi' },
{ value: 'grape', label: 'Grape' },
{ value: 'orange', label: 'Orange' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'watermelon', label: 'Watermelon' },
],
},
{
value: 'Vegetables',
items: [
{ value: 'broccoli', label: 'Broccoli' },
{ value: 'carrot', label: 'Carrot' },
{ value: 'cauliflower', label: 'Cauliflower' },
{ value: 'cucumber', label: 'Cucumber' },
{ value: 'kale', label: 'Kale' },
{ value: 'pepper', label: 'Bell pepper' },
{ value: 'spinach', label: 'Spinach' },
{ value: 'zucchini', label: 'Zucchini' },
],
},
];
const ICON_PROPS = {
width: '16',
height: '16',
viewBox: '0 0 16 16',
style: { display: 'block' },
};
const CaretUpDownIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [
h('path', { d: 'M11 10H5l3 3.5zm0-4H5l3-3.5z' }),
]);
const CheckIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'none', stroke: 'currentColor', ...attrs }, [
h('path', { d: 'm2.5 8.5 4 4 7-9' }),
]);
const CaretUpIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [
h('path', { d: 'M12 10H4l4-4.5z' }),
]);
const CaretDownIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [h('path', { d: 'M12 6H4l4 4.5z' })]);
const popupHiddenClass = styles.PopupHidden;
</script>
<template>
<div :class="styles.Field">
<Select.Root :items="groupedProduce">
<Select.Label :class="styles.Label">Produce</Select.Label>
<Select.Trigger :class="styles.Select">
<Select.Value :class="styles.Value" placeholder="Select produce" />
<Select.Icon>
<CaretUpDownIcon />
</Select.Icon>
</Select.Trigger>
<Select.Portal>
<Select.Positioner :class="styles.Positioner" :side-offset="4">
<Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
<Select.Popup :class="styles.Popup">
<Select.ScrollUpArrow :class="styles.ScrollUpArrow">
<CaretUpIcon />
</Select.ScrollUpArrow>
<Select.List :class="styles.List">
<template v-for="(group, index) in groupedProduce" :key="group.value">
<Select.Group :class="styles.Group">
<Select.GroupLabel :class="styles.GroupLabel">{{
group.value
}}</Select.GroupLabel>
<Select.Item
v-for="item in group.items"
:key="item.value"
:value="item.value"
:class="styles.Item"
v-slot="{ state }"
>
<span v-if="state.selected" :class="styles.ItemIndicator">
<CheckIcon />
</span>
<Select.ItemText :class="styles.ItemText">{{ item.label }}</Select.ItemText>
</Select.Item>
</Select.Group>
<Select.Separator
v-if="index < groupedProduce.length - 1"
:class="styles.Separator"
/>
</template>
</Select.List>
<Select.ScrollDownArrow :class="styles.ScrollDownArrow">
<CaretDownIcon />
</Select.ScrollDownArrow>
</Select.Popup>
</Transition>
</Select.Positioner>
</Select.Portal>
</Select.Root>
</div>
</template>.Field {
display: flex;
flex-direction: column;
align-items: start;
gap: 0.25rem;
}
.Label {
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 700;
color: oklch(14.5% 0 0deg);
cursor: default;
@media (prefers-color-scheme: dark) {
color: white;
}
}
.Value[data-placeholder] {
color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
.Select {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.75rem;
height: 2rem;
padding-left: 0.5rem;
padding-right: 0.25rem;
margin: 0;
outline: 0;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
font-family: inherit;
font-size: 0.875rem;
line-height: 1;
white-space: nowrap;
font-weight: 400;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
min-width: 11rem;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover:not(:disabled) {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&[data-pressed] {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
&:active:not(:disabled) {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:disabled {
color: oklch(55.6% 0 0deg);
border-color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
border-color: oklch(70.8% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
.Positioner {
outline: none;
z-index: 10;
-webkit-user-select: none;
user-select: none;
}
.Popup {
box-sizing: border-box;
outline: 0;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
background-clip: padding-box;
color: oklch(14.5% 0 0deg);
min-width: var(--anchor-width);
transform-origin: var(--transform-origin);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
transition:
transform 100ms ease-out,
opacity 100ms ease-out;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
&[data-side='none'] {
transition: none;
transform: translateY(1px);
opacity: 1;
min-width: calc(var(--anchor-width) + 1.75rem);
}
}
/* The enter-from and leave-to class of the `<Transition>` around the popup. */
.PopupHidden {
opacity: 0;
transform: scale(0.98);
}
.List {
box-sizing: border-box;
position: relative;
padding-block: 0.25rem;
overflow-y: auto;
max-height: var(--available-height);
scroll-padding-block-start: 1.5rem;
scroll-padding-block-end: 1.5rem;
}
.Group {
display: block;
padding-bottom: 0.125rem;
&:last-child {
padding-bottom: 0;
}
}
.GroupLabel {
box-sizing: border-box;
padding-block: 0.375rem;
padding-left: calc(0.625rem + 1rem + 0.5rem);
padding-right: 1rem;
font-size: 0.875rem;
line-height: 1.25rem;
-webkit-user-select: none;
user-select: none;
color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
.Item {
box-sizing: border-box;
outline: 0;
font-size: 0.875rem;
line-height: 1.25rem;
padding-block: 0.375rem;
padding-left: 0.625rem;
padding-right: 1rem;
display: grid;
gap: 0.5rem;
align-items: center;
grid-template-columns: 1rem 1fr;
cursor: default;
-webkit-user-select: none;
user-select: none;
&[data-highlighted] {
background-color: oklch(14.5% 0 0deg);
color: white;
@media (prefers-color-scheme: dark) {
background-color: white;
color: oklch(14.5% 0 0deg);
}
}
}
.ItemIndicator {
grid-column-start: 1;
}
.ItemText {
grid-column-start: 2;
}
.Separator {
height: 1px;
margin-block: 0.25rem;
margin-inline: 1rem;
background-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: white;
}
}
.ScrollUpArrow,
.ScrollDownArrow {
width: 100%;
background-color: white;
z-index: 2;
text-align: center;
cursor: default;
height: 1rem;
font-size: 0.75rem;
display: flex;
align-items: center;
justify-content: center;
@media (prefers-color-scheme: dark) {
background-color: oklch(14.5% 0 0deg);
}
&::before {
content: '';
position: absolute;
width: 100%;
height: 100%;
left: 0;
}
}
.ScrollUpArrow {
top: 0;
&[data-side='none'] {
&::before {
top: -100%;
}
}
}
.ScrollDownArrow {
bottom: 0;
&[data-side='none'] {
&::before {
bottom: -100%;
}
}
}<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Select } from '@vase-ui/vue/select';
const groupedProduce = [
{
value: 'Fruits',
items: [
{ value: 'apple', label: 'Apple' },
{ value: 'banana', label: 'Banana' },
{ value: 'mango', label: 'Mango' },
{ value: 'kiwi', label: 'Kiwi' },
{ value: 'grape', label: 'Grape' },
{ value: 'orange', label: 'Orange' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'watermelon', label: 'Watermelon' },
],
},
{
value: 'Vegetables',
items: [
{ value: 'broccoli', label: 'Broccoli' },
{ value: 'carrot', label: 'Carrot' },
{ value: 'cauliflower', label: 'Cauliflower' },
{ value: 'cucumber', label: 'Cucumber' },
{ value: 'kale', label: 'Kale' },
{ value: 'pepper', label: 'Bell pepper' },
{ value: 'spinach', label: 'Spinach' },
{ value: 'zucchini', label: 'Zucchini' },
],
},
];
const ICON_PROPS = {
width: '16',
height: '16',
viewBox: '0 0 16 16',
style: { display: 'block' },
};
const CaretUpDownIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [
h('path', { d: 'M11 10H5l3 3.5zm0-4H5l3-3.5z' }),
]);
const CheckIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'none', stroke: 'currentColor', ...attrs }, [
h('path', { d: 'm2.5 8.5 4 4 7-9' }),
]);
const CaretUpIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [
h('path', { d: 'M12 10H4l4-4.5z' }),
]);
const CaretDownIcon: FunctionalComponent = (_, { attrs }) =>
h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [h('path', { d: 'M12 6H4l4 4.5z' })]);
const popupHiddenClass = 'scale-[0.98] opacity-0';
</script>
<template>
<div class="flex flex-col items-start gap-1">
<Select.Root :items="groupedProduce">
<Select.Label class="cursor-default text-sm font-bold text-neutral-950 dark:text-white"
>Produce</Select.Label
>
<Select.Trigger
class="flex h-8 min-w-44 items-center justify-between gap-3 pl-2 pr-1 text-sm leading-none whitespace-nowrap border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 text-neutral-950 dark:text-white select-none hover:enabled:bg-neutral-100 dark:hover:enabled:bg-neutral-800 active:enabled:bg-neutral-200 dark:active:enabled:bg-neutral-700 disabled:border-neutral-500 disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:disabled:border-neutral-400 dark:disabled:text-neutral-400 data-pressed:bg-neutral-100 dark:data-pressed:bg-neutral-800 font-normal focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
>
<Select.Value
class="data-placeholder:text-neutral-500 dark:data-placeholder:text-neutral-400"
placeholder="Select produce"
/>
<Select.Icon>
<CaretUpDownIcon />
</Select.Icon>
</Select.Trigger>
<Select.Portal>
<Select.Positioner class="outline-hidden select-none z-10" :side-offset="4">
<Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
<Select.Popup
class="group min-w-[var(--anchor-width)] origin-[var(--transform-origin)] bg-clip-padding border border-neutral-950 bg-white text-neutral-950 outline-hidden shadow-[0.25rem_0.25rem_0] shadow-black/12 transition-[scale,opacity] duration-100 ease-out data-[side=none]:translate-y-px data-[side=none]:min-w-[calc(var(--anchor-width)+1.75rem)] data-[side=none]:scale-100 data-[side=none]:opacity-100 data-[side=none]:transition-none dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
>
<Select.ScrollUpArrow
class="top-0 z-[2] flex h-4 w-full cursor-default items-center justify-center bg-white text-center text-xs before:absolute data-[side=none]:before:top-[-100%] before:left-0 before:h-full before:w-full before:content-[''] dark:bg-neutral-950"
>
<CaretUpIcon />
</Select.ScrollUpArrow>
<Select.List
class="relative py-1 scroll-pt-6 scroll-pb-6 overflow-y-auto max-h-[var(--available-height)]"
>
<template v-for="(group, index) in groupedProduce" :key="group.value">
<Select.Group class="block pb-0.5 last:pb-0">
<Select.GroupLabel
class="py-1.5 pr-4 pl-[2.125rem] text-sm leading-5 text-neutral-500 select-none dark:text-neutral-400"
>{{ group.value }}</Select.GroupLabel
>
<Select.Item
v-for="item in group.items"
:key="item.value"
:value="item.value"
class="grid cursor-default grid-cols-[1rem_1fr] items-center gap-2 py-1.5 pr-4 pl-2.5 text-sm outline-hidden select-none group-data-[side=none]:pr-12 data-highlighted:bg-neutral-950 data-highlighted:text-white dark:data-highlighted:bg-white dark:data-highlighted:text-neutral-950"
v-slot="{ state }"
>
<span v-if="state.selected" class="col-start-1">
<CheckIcon />
</span>
<Select.ItemText class="col-start-2">{{ item.label }}</Select.ItemText>
</Select.Item>
</Select.Group>
<Select.Separator
v-if="index < groupedProduce.length - 1"
class="mx-4 my-1 h-px bg-neutral-950 dark:bg-white"
/>
</template>
</Select.List>
<Select.ScrollDownArrow
class="bottom-0 z-[2] flex h-4 w-full cursor-default items-center justify-center bg-white text-center text-xs before:absolute before:left-0 before:h-full before:w-full before:content-[''] data-[side=none]:before:bottom-[-100%] dark:bg-neutral-950"
>
<CaretDownIcon />
</Select.ScrollDownArrow>
</Select.Popup>
</Transition>
</Select.Positioner>
</Select.Portal>
</Select.Root>
</div>
</template>API reference
Select.Root
Groups the parts of the select, and holds its value and its form inputs.
Renders a <div> element.
A <p> cannot hold the <div> of the root: the HTML parser closes the paragraph at the <div>,
and the hydration fails.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
autocomplete | string | undefined | The autocomplete of the visually hidden text input. The browser fills that input. Without
multiple, the select then takes the item whose value, label or text is the filled text. | |
defaultOpen | boolean | undefined | false | The popup is open at the mount of an uncontrolled root. |
defaultValue | SelectValue<Value, Multiple> | null | undefined | null | The value of an uncontrolled root, at the mount and at a native reset of the form. |
disabled | boolean | undefined | false | The user cannot open the select or change the value. |
form | string | undefined | The id of the form that the inputs belong to. | |
highlightItemOnHover | boolean | undefined | true | A pointer move highlights the item under the pointer. With false, :hover and the highlight
of the keys are separate. |
id | string | undefined | The id of the trigger, when the trigger has no id. | |
isItemEqualToValue | ((itemValue: Value, value: Value) => boolean) | undefined | An item is selected when this function gives true for its value and the value of the root.
The default is Object.is. | |
items | SelectItems | The items, so that Select.Value shows the label of a value. The closed select needs no
mounted popup for it. | |
itemToStringLabel | ((itemValue: Value) => string) | undefined | The text of an object value, for Select.Value. The default is its label. | |
itemToStringValue | ((itemValue: Value) => string) | undefined | The text of an object value in the form data. The default is its value. | |
modal | boolean | undefined | true | The open popup locks the scroll of the page, and the page gets no pointer. |
modelValue | SelectValue<Value, Multiple> | null | undefined | The value. v-model binds it. With multiple, an array, and null is the empty array. A
native reset of the form gives back the value of the mount. | |
multiple | (Multiple & boolean) | undefined | false | The user can select more than one item. |
name | string | undefined | The name of the value in the form data. With multiple, the form data has one entry for each
selected value. A Field.Root gives its own name. | |
open | boolean | undefined | The popup is open. v-model:open binds it. | |
readonly | boolean | undefined | false | The user can open the popup, and cannot change the value. |
required | boolean | undefined | false | The form refuses a submit while the select has no value. A visually hidden text input has the constraint validation. |
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: SelectOutputValue<Value, Multiple>, details: SelectRootChangeEventDetails] | The user changes the value. details.cancel() keeps the value as it was. Without multiple, a
press of the selected item sends no event. |
update:modelValue | [value: SelectOutputValue<Value, Multiple>] | Comes after valueChange, unless a listener cancels the change. v-model uses it. |
openChange | [open: boolean, details: SelectRootOpenChangeEventDetails] | 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. |
Exposed
| Member | Type | Description |
|---|---|---|
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. |
close | () => void | Closes the popup with the reason 'imperative-action'. |
Select.Label
A label that names the select. A click gives the focus to the trigger.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | undefined | The id of the label. 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: SelectLabelState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectLabelState | 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-disabled] | The select 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 select has a value. |
Select.Trigger
A button that opens the popup.
Renders a <button> element.
Without multiple and readonly, a key on the closed trigger selects the next item whose text
starts with the typed text, as on a native <select>.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | undefined | false | The user cannot press the trigger. |
id | string | undefined | The id of the trigger. The default is the id of Select.Root. | |
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: SelectTriggerState<any> | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectTriggerState<any> | 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-pressed] | The popup is open. |
[data-popup-side] | The side of the trigger that the open popup is on, or none while the popup covers the trigger. |
[data-placeholder] | The select has no value. |
:disabled | The trigger is a native button, and the trigger, the select or a fieldset around it is disabled. |
[aria-disabled="true"] | The trigger is another element, and the trigger or the select is disabled. |
[aria-readonly="true"] | The select is read-only. |
[aria-required="true"] | The select is required. |
[aria-invalid="true"] | In a Field.Root, 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 select has a value. |
Select.Value
The text of the value, or the placeholder.
Renders a <span> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
placeholder | ItemLabel | The content while the select has no value: a text, a number, or a function that gives the
content. An item of items for null wins over 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 | value: anystate: SelectValueState<any> | The content in place of the label of the value. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectValueState<any> | 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-placeholder] | The select has no value. |
Select.Icon
An icon that shows the select opens a list.
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: SelectIconState | The content. The default is ▼. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectIconState | 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. |
Select.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: SelectBackdropState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectBackdropState | 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. |
Select.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 its trigger.
From the first focus of the trigger, the first open or the first fill of the browser, it keeps
its content in the DOM, so the closed select has its items.
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. |
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. |
Select.Positioner
Places the popup at the trigger, or over it.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
align | Align | undefined | 'center' | The alignment of the popup on the side. |
alignItemWithTrigger | boolean | undefined | true | The popup covers the trigger, with the text of the selected item over the text of the value. A touch open, and a view with no room for it, place the popup at its 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: SelectPositionerState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectPositionerState | 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 trigger that the popup is on: top, bottom, left, right, inline-start or inline-end. It is none while the popup covers the trigger. |
[data-align] | The alignment of the popup on its side: start, center or end. |
[data-anchor-hidden] | The anchor is out of view. |
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. A popup over the trigger grows from the middle of the value. |
Select.Popup
The container of the items.
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, else the element that had the focus before the open. After an open by code, that element comes before the trigger. | |
id | string | undefined | The id of the popup. 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: SelectPopupState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectPopupState | 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 trigger that the popup is on, or none while the popup covers the trigger. |
[data-align] | The alignment of the popup on its side: start, center or end. |
Select.List
The element that holds the items and scrolls.
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: SelectListState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectListState | 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. |
Select.Arrow
An element that points to the center of the trigger.
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.
It renders nothing while the popup covers the trigger.
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: SelectArrowState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectArrowState | 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 trigger 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. |
Select.Item
An option that the user can select.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | undefined | false | The user cannot select the item. It keeps the highlight. |
label | string | undefined | The text of the typeahead and of a fill of the browser. The default is the text of the item. | |
value | any | null | The value that the item selects. |
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: SelectItemState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectItemState | 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-selected="true"] | The item is selected. |
[aria-disabled="true"] | The item or the select is disabled. |
Select.ItemText
The text of an item, which the popup puts over the value.
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: SelectItemTextState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectItemTextState | 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. |
Select.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: SelectGroupState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectGroupState | 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. |
Select.GroupLabel
A label that 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: SelectGroupLabelState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectGroupLabelState | 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. |
Select.ScrollUpArrow
Scrolls the list up while the pointer moves on it.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
keepMounted | boolean | undefined | false | The arrow stays in the DOM, with hidden, while the list cannot scroll in its direction. |
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: SelectScrollArrowState | The content. The default is ▲. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectScrollArrowState | 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 list cannot scroll up, and keepMounted keeps the arrow in the DOM. |
[data-side] | The side of the trigger that the popup is on, or none while the popup covers the trigger. |
Select.ScrollDownArrow
Scrolls the list down while the pointer moves on it.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
keepMounted | boolean | undefined | false | The arrow stays in the DOM, with hidden, while the list cannot scroll in its direction. |
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: SelectScrollArrowState | The content. The default is ▼. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectScrollArrowState | 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 list cannot scroll down, and keepMounted keeps the arrow in the DOM. |
[data-side] | The side of the trigger that the popup is on, or none while the popup covers the trigger. |
Select.Separator
Separates groups of items.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
orientation | "horizontal" | "vertical" | 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: SelectSeparatorState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SelectSeparatorState | 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. |
Differences from Base UI
Vase UI ports Select from Base UI 1.9.0.
API
| Base UI | Vase UI |
|---|---|
Select.Root | @after-enter and @after-leave of a <Transition> around the popup run after its animations, in place of openChangeComplete. With :css="false", the leave hook keeps the popup until it calls done, in place of the unmount action. A template ref gives inputElement, close() and element, in place of inputRef and actionsRef. readonly and autocomplete have the names of the native attributes. multiple has no declared default, because withDefaults refuses a default for the generic type Multiple & boolean, and Vue gives an absent Boolean prop false. SelectRootChangeEventReason has the two reasons that valueChange sends: item-press and none. |
Select.Label | :focus-visible on the trigger and :has() on the root select the focus, so the state has no focused. |
Select.Trigger | The trigger reads its element, so it needs no nativeButton. :focus-visible selects the focus, so the state has no focused. The state key readonly has the name of the native attribute. |
Select.Backdrop | A <Transition> around the backdrop animates it, so the state has no transitionStatus, and the backdrop has no data-starting-style and data-ending-style. |
Select.Portal | A <Teleport> moves the content, and to names its target. The only element of the portal in place is the visually hidden element that owns the popup, so the portal takes no as and has no element slot. |
Select.Popup | A <Transition> around the popup animates it, so the state has no transitionStatus, and the popup has no data-starting-style and data-ending-style. |
Select.Item | The item reads its element, so it needs no nativeButton. |
Select.ItemIndicator | The default slot of Select.Item gives state.selected. A v-if or a v-show on an element of the developer shows the mark, and <Transition> animates it. The part is cut. |
Added in Vase UI
| Part | Member | Why |
|---|---|---|
Select.Root | as prop | The root renders a <div> that holds the inputs of its form. as gives it another tag or a component, as on each part with an element. |
Select.Root | autocomplete prop | The name of the native attribute. Base UI has autoComplete. |
Select.Root | readonly prop | The name of the native attribute. Base UI has readOnly. |
Select.Root | element exposed member | A template ref of a part gives its element. The root renders a <div>. |
Select.Trigger | readonly state field | The name of the native attribute. Base UI has readOnly. |
Select.Trigger | required state field | The state of the trigger says that the select is required, as aria-required on its element does. |
Select.Portal | disabled prop | A disabled portal renders its content in place, also on the server (VUE-128). |
Select.Portal | defer prop | The portal is a <Teleport>, and it takes the props of <Teleport>. |
Select.ScrollUpArrow | direction state field | The state says the direction of the arrow, for a slot of the developer. |
Select.ScrollUpArrow | visible state field | The state says that the list can scroll in the direction of the arrow, for a kept arrow. |
Select.ScrollUpArrow | side state field | The state says the side of the popup: none while the popup covers the trigger. |
Select.ScrollDownArrow | direction state field | The state says the direction of the arrow, for a slot of the developer. |
Select.ScrollDownArrow | visible state field | The state says that the list can scroll in the direction of the arrow, for a kept arrow. |
Select.ScrollDownArrow | side state field | The state says the side of the popup: none while the popup covers the trigger. |
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. |
|
The server HTML of Select.Trigger and Combobox.Trigger has the id of the label part of the family in aria-labelledby, in each order of the two parts. Base UI adds the attribute after the page hydrates. |
|
Select.Value reads the reactive items prop of Select.Root in a computed. It thus follows a change that the consumer makes in place, such as a new key. Base UI sees only a new object. | |
Popover.Positioner, Select.Positioner and Combobox.Positioner have one root node, also while their popup is modal: Popover.Portal, Select.Portal and Combobox.Portal render the internal backdrop of a modal popup, and only while its positioner is mounted. A rule of <style scoped>, v-show and a custom directive of the consumer thus apply to the element of the positioner. While a positioner loads in an async component, nothing covers the page, and a press on the page is an outside press. A Combobox.List with no positioner has no backdrop. Autocomplete.Portal and Autocomplete.Positioner are the parts of Combobox under other names. In Base UI, the positioner renders the backdrop beside its element, so the backdrop lasts as long as the positioner. |
|
Shift+Tab in the popup of a menu or a select asks for the close with the reason focus-out. When a handler cancels the close, the popup stays open, and the focus still goes to the trigger. In a submenu, the focus goes to the submenu trigger, or to the input of a filterable parent menu. From the input of a filterable menu, the focus goes to the trigger. A root context menu has no trigger element, so a canceled close for Tab or Shift+Tab keeps the focus in its modal popup. In Base UI, Shift+Tab does nothing when the close is canceled: the list stops the key and moves no focus. Tab still leaves the popup, so the focus can leave in one direction only. |
|
Select.Root, Combobox.Root, Autocomplete.Root and Menu.RadioGroup emit no change event when the user picks the value that they have. Without multiple, a press of the selected item of Select or Combobox emits no valueChange and no update:modelValue, and the popup still closes. A drag that ends on the selected item of Select, and the typeahead of a closed select that matches it, emit neither event too. Select and Combobox compare the value with isItemEqualToValue. A native form reset of Select emits no update:modelValue when isItemEqualToValue matches the value with the first value. Combobox emits no inputValueChange for the text that the input has. A pick of the selected item still fills the input when the input has other text. Autocomplete emits no valueChange for an action that gives the text that it has, such as a press of the item with that text. It compares with the typed text, because the input can show an inline completion. A press of Combobox.Clear, and Escape in the input of a closed combobox, emit no valueChange when the combobox has no value. A click of the checked item of Menu.RadioGroup emits no valueChange, as a click of the checked radio of RadioGroup does not. Base UI calls onValueChange with the value that the part has. HTML fires change when the user commits a change of the value. |
|
Select keeps its popup element and its items in the DOM, hidden and inert, from the first focus of its trigger, its first open or the first fill of its input. A reference to the popup or to an item stays valid after each close. Base UI keeps them only when the trigger got the focus, or the browser autofilled the hidden input, before the end of the close. A select that opens with defaultOpen, with the open prop, or with a press that does not focus the trigger removes its popup at the end of the close. When the focus then returns to the trigger, Base UI mounts a new popup one task later. When the focus goes to another element, the closed select has no items until its trigger gets the focus. |
|
Select.Root and Combobox.Root give a form their value with its line breaks, as a <select> gives the value of its option with no change. They give the value in type="hidden" inputs, which keep each line break. new FormData(form) and a submission thus have the value of the part. Base UI submits the value without its line breaks. |
|
With multiple, Select.Root and Combobox.Root read null in modelValue as an empty array, and the part stays controlled. The default slot of Select.Value and Combobox.Value gets an empty array. The validate function of Field.Root gets an empty array, and a field that has no selected item again is not dirty. ArrowLeft at the start of Combobox.Input in Combobox.Chips gives no error. Without multiple, null stays a value, because an item can have the value null. Vue accepts null for an optional prop with no warning, so a v-model whose ref becomes null gives null to the part. Base UI gives the null to the function child of Select.Value and Combobox.Value and to the validation of the field. A field of Base UI that has no selected item again stays dirty, and Combobox.Input throws a TypeError for ArrowLeft in Combobox.Chips. |
|
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. | |
In a Select popup over the trigger, Select.List takes the height that the other children of Select.Popup leave. A heading before the list or a footer after it keeps its room, and the text of the selected item covers the text of the value also where the list ends in the view. Base UI gives the list max-height: 100% of the popup, so the list and an element before it are taller than the popup. |
|
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. |
|
While the popup of a select aligns its selected item with the trigger, the popup covers the trigger and has no side. data-popup-side of the trigger is then none, the same value as data-side of the positioner, so one selector styles the two parts. Base UI gives the trigger the side of the fallback placement, such as bottom, while its positioner has none. | |
| A key in the list of a popup, such as an arrow key, reaches the listeners of the elements around the popup, as each DOM event does. Only an Escape that the popup handles stops at the popup. A toolbar around the trigger moves no focus for a key that comes from the popup. Base UI stops the propagation of the arrow keys in the popup. | |
| A close of the popup of a select ends the typed string, so a Space on the trigger right after the close opens the popup, as Space opens a closed combobox in the WAI-ARIA Authoring Practices. Base UI ends the string at the close only when the select has no value. With a value, a Space in the 750 ms after the close is a character of the string, and the popup stays closed. | |
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.