Select
Pick one value from a list.
Reka UI Select
<script setup lang="ts">
import * as Select from '@/components/ui/select';
import { ref } from 'vue';
const fruits = [
{ value: 'apple', label: 'Apple' },
{ value: 'banana', label: 'Banana' },
{ value: 'blueberry', label: 'Blueberry' },
{ value: 'grapes', label: 'Grapes', disabled: true },
{ value: 'pineapple', label: 'Pineapple' }
];
const fruit = ref('');
</script>
<template>
<Select.Root v-model="fruit">
<Select.Label>Favorite fruit</Select.Label>
<Select.Trigger style="--dui-select-trigger-width: 11rem">
<Select.Value placeholder="Select a fruit" />
</Select.Trigger>
<Select.Content>
<Select.Group>
<Select.GroupHeading>Fruits</Select.GroupHeading>
<Select.Item
v-for="f in fruits"
:key="f.value"
:value="f.value"
:label="f.label"
:disabled="f.disabled"
/>
</Select.Group>
</Select.Content>
</Select.Root>
</template>
Installation
npx distill-ui add selectIt installs reka-ui if you don't have it yet.
- Set up the tokens once, if you haven't: see manual installation.
- Install the packages it needs:
npm install reka-ui@2.11.0 - Copy these files into your project:
<script setup lang="ts">
import {
SelectRoot,
useForwardPropsEmits,
type SelectRootEmits,
type SelectRootProps
} from 'reka-ui';
import { ref, useId } from 'vue';
import { provideSelect } from './context.js';
// Use `v-model` for the value and `v-model:open` for the open state.
const props = defineProps<SelectRootProps<string>>();
const emits = defineEmits<SelectRootEmits<string>>();
const forwarded = useForwardPropsEmits(props, emits);
const uid = useId();
provideSelect({ labelId: `${uid}-label`, triggerId: `${uid}-trigger`, hasLabel: ref(false) });
</script>
<template>
<SelectRoot v-bind="forwarded">
<slot />
</SelectRoot>
</template>
<script setup lang="ts">
import {
SelectContent,
SelectViewport,
useForwardPropsEmits,
type SelectContentEmits,
type SelectContentProps
} from 'reka-ui';
import { injectSelect } from './context.js';
defineOptions({ inheritAttrs: false });
const props = withDefaults(defineProps<SelectContentProps>(), {
position: 'popper',
sideOffset: 4
});
const emits = defineEmits<SelectContentEmits>();
const forwarded = useForwardPropsEmits(props, emits);
const ctx = injectSelect();
</script>
<template>
<!-- No portal: Reka positions the list with position: fixed, so it stays inside your styles. -->
<SelectContent v-bind="forwarded" as-child>
<div
v-bind="$attrs"
class="content"
:aria-labelledby="ctx.hasLabel.value ? ctx.labelId : undefined"
>
<SelectViewport as-child>
<div class="viewport"><slot /></div>
</SelectViewport>
</div>
</SelectContent>
</template>
<!--
Options you can set from a parent or on the select content itself:
--dui-select-content-min-width, --dui-select-content-radius, --dui-select-content-bg, --dui-select-content-fg
-->
<style scoped>
/* Reka waits for animationend before removing the list, so it animates with
the shared keyframes on [data-state]. */
.content {
/* Reka copies this onto the positioning wrapper it puts around the list. */
z-index: var(--dui-z-popover);
min-width: max(var(--dui-select-content-min-width, 9rem), var(--reka-select-trigger-width));
max-height: var(--reka-select-content-available-height);
overflow-x: hidden;
overflow-y: auto;
outline: none;
border-radius: var(--dui-select-content-radius, var(--dui-radius-md));
background: var(--dui-select-content-bg, var(--dui-color-popover));
color: var(--dui-select-content-fg, var(--dui-color-popover-foreground));
box-shadow:
0 0 0 1px color-mix(in oklch, var(--dui-color-foreground) 10%, transparent),
var(--dui-shadow-md);
&[data-state='open'] {
animation: distill-zoom-in var(--dui-duration-fast) var(--dui-ease-out);
}
&[data-state='closed'] {
animation: distill-zoom-out var(--dui-duration-fast) var(--dui-ease-out);
}
}
.viewport {
padding: var(--dui-space-1);
}
</style>
<script setup lang="ts">
import { SelectGroup } from 'reka-ui';
</script>
<template>
<SelectGroup><slot /></SelectGroup>
</template>
<script setup lang="ts">
import { SelectLabel } from 'reka-ui';
</script>
<!-- Reka calls this SelectLabel; it names the group it sits in. -->
<template>
<SelectLabel as-child>
<div class="heading"><slot /></div>
</SelectLabel>
</template>
<style scoped>
.heading {
padding: var(--dui-space-1-5) var(--dui-space-2);
color: var(--dui-color-muted-foreground);
font-size: var(--dui-text-xs);
line-height: var(--dui-text-xs-line-height);
}
</style>
<script setup lang="ts">
import { SelectItem, SelectItemIndicator, SelectItemText } from 'reka-ui';
// Pass `label` for the text, or put your own content in the slot.
const {
value,
label,
disabled = false
} = defineProps<{ value: string; label?: string; disabled?: boolean }>();
</script>
<template>
<SelectItem :value="value" :text-value="label ?? value" :disabled="disabled" as-child>
<div class="item">
<SelectItemText as-child>
<span class="text"
><slot>{{ label ?? value }}</slot></span
>
</SelectItemText>
<SelectItemIndicator as-child>
<svg class="indicator" viewBox="0 0 24 24" aria-hidden="true">
<path d="M20 6 9 17l-5-5" />
</svg>
</SelectItemIndicator>
</div>
</SelectItem>
</template>
<!--
Options you can set from a parent or on the select item itself:
--dui-select-item-highlight-bg, --dui-select-item-highlight-fg
-->
<style scoped>
.item {
position: relative;
display: flex;
align-items: center;
gap: var(--dui-space-2);
width: 100%;
padding-block: var(--dui-space-1-5);
padding-inline: var(--dui-space-2) var(--dui-space-8);
border-radius: var(--dui-radius-sm);
font-size: var(--dui-text-sm);
line-height: var(--dui-text-sm-line-height);
cursor: default;
user-select: none;
outline: none;
&[data-highlighted] {
background: var(--dui-select-item-highlight-bg, var(--dui-color-accent));
color: var(--dui-select-item-highlight-fg, var(--dui-color-accent-foreground));
}
&[data-disabled] {
pointer-events: none;
opacity: 0.5;
}
}
.text {
display: flex;
flex: 1;
align-items: center;
gap: var(--dui-space-2);
white-space: nowrap;
}
.indicator {
position: absolute;
right: var(--dui-space-2);
width: 1rem;
height: 1rem;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
pointer-events: none;
}
</style>
<script setup lang="ts">
import { injectSelect } from './context.js';
const ctx = injectSelect();
ctx.hasLabel.value = true;
</script>
<template>
<label :id="ctx.labelId" :for="ctx.triggerId" class="label"><slot /></label>
</template>
<style scoped>
.label {
display: block;
margin-bottom: var(--dui-space-2);
font-size: var(--dui-text-sm);
line-height: var(--dui-text-sm-line-height);
font-weight: var(--dui-weight-medium);
}
</style>
<script setup lang="ts">
import { SelectSeparator } from 'reka-ui';
</script>
<template>
<SelectSeparator as-child>
<div class="separator"></div>
</SelectSeparator>
</template>
<style scoped>
.separator {
height: 1px;
margin: var(--dui-space-1) calc(var(--dui-space-1) * -1);
background: var(--dui-color-border);
}
</style>
<script setup lang="ts">
import { SelectTrigger } from 'reka-ui';
import { injectSelect } from './context.js';
const { size = 'md', disabled } = defineProps<{ size?: 'sm' | 'md'; disabled?: boolean }>();
const ctx = injectSelect();
</script>
<template>
<SelectTrigger :disabled="disabled" as-child>
<button :id="ctx.triggerId" type="button" class="trigger" :data-size="size">
<slot />
<svg class="icon" viewBox="0 0 24 24" aria-hidden="true"><path d="m6 9 6 6 6-6" /></svg>
</button>
</SelectTrigger>
</template>
<!--
Options you can set from a parent or on the select trigger itself:
--dui-select-trigger-width, --dui-select-trigger-height, --dui-select-trigger-border, --dui-select-trigger-radius, --dui-select-trigger-bg
-->
<style scoped>
.trigger {
display: flex;
align-items: center;
justify-content: space-between;
gap: var(--dui-space-1-5);
width: var(--dui-select-trigger-width, fit-content);
height: var(--dui-select-trigger-height, 2.25rem);
padding-block: var(--dui-space-2);
padding-inline: var(--dui-space-2-5) var(--dui-space-2);
border: 1px solid var(--dui-select-trigger-border, var(--dui-color-input));
border-radius: var(--dui-select-trigger-radius, var(--dui-radius-md));
background: var(--dui-select-trigger-bg, transparent);
color: var(--dui-color-foreground);
box-shadow: var(--dui-shadow-xs);
font: inherit;
font-size: var(--dui-text-sm);
line-height: var(--dui-text-sm-line-height);
white-space: nowrap;
cursor: pointer;
outline: none;
transition:
color var(--dui-duration-fast) var(--dui-ease-out),
box-shadow var(--dui-duration-fast) var(--dui-ease-out);
&[data-size='sm'] {
height: var(--dui-select-trigger-height, 2rem);
}
&:focus-visible {
border-color: var(--dui-color-ring);
box-shadow: 0 0 0 var(--dui-ring-width)
color-mix(in oklch, var(--dui-color-ring) 50%, transparent);
}
&[data-placeholder] {
color: var(--dui-color-muted-foreground);
}
&:disabled {
cursor: not-allowed;
opacity: 0.5;
}
&[aria-invalid='true'] {
border-color: var(--dui-color-destructive);
}
}
.icon {
flex-shrink: 0;
width: 1rem;
height: 1rem;
fill: none;
stroke: var(--dui-color-muted-foreground);
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
pointer-events: none;
}
</style>
<script setup lang="ts">
import { SelectValue } from 'reka-ui';
const { placeholder = '' } = defineProps<{ placeholder?: string }>();
</script>
<template>
<SelectValue v-slot="{ selectedLabel }" as-child>
<span class="value">{{ selectedLabel.length ? selectedLabel.join(', ') : placeholder }}</span>
</SelectValue>
</template>
<style scoped>
.value {
display: flex;
flex: 1;
align-items: center;
gap: var(--dui-space-1-5);
text-align: left;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
import { inject, provide, type Ref } from 'vue';
// Reka's trigger has no id of its own, so the root makes ids for the label to point at
// the trigger, and Label records that it exists for the list to point aria-labelledby back.
export type SelectContext = { labelId: string; triggerId: string; hasLabel: Ref<boolean> };
const key = Symbol('distill-select');
export const provideSelect = (ctx: SelectContext) => provide(key, ctx);
export const injectSelect = () => inject<SelectContext>(key)!;
export { default as Root } from './Select.vue';
export { default as Label } from './SelectLabel.vue';
export { default as Trigger } from './SelectTrigger.vue';
export { default as Value } from './SelectValue.vue';
export { default as Content } from './SelectContent.vue';
export { default as Item } from './SelectItem.vue';
export { default as Group } from './SelectGroup.vue';
export { default as GroupHeading } from './SelectGroupHeading.vue';
export { default as Separator } from './SelectSeparator.vue';
Customizing
Set any of these on the component or on a parent element. See Customizing for how they work.
| Custom property | Default |
|---|---|
--dui-select-content-min-width | 9rem |
--dui-select-content-radius | var(--dui-radius-md) |
--dui-select-content-bg | var(--dui-color-popover) |
--dui-select-content-fg | var(--dui-color-popover-foreground) |
--dui-select-item-highlight-bg | var(--dui-color-accent) |
--dui-select-item-highlight-fg | var(--dui-color-accent-foreground) |
--dui-select-trigger-width | fit-content |
--dui-select-trigger-height | 2.25rem |
--dui-select-trigger-border | var(--dui-color-input) |
--dui-select-trigger-radius | var(--dui-radius-md) |
--dui-select-trigger-bg | transparent |