<script setup lang="ts">
import {
ASelectContent,
ASelectGroup,
ASelectItem,
ASelectItemIndicator,
ASelectItemText,
ASelectLabel,
ASelectPortal,
ASelectRoot,
ASelectSeparator,
ASelectTrigger,
ASelectValue,
ASelectViewport,
} from 'akar';
import { ref } from 'vue';
const fruit = ref();
const fruits = ['Apple', 'Banana', 'Blueberry', 'Grapes', 'Pineapple'];
const vegetables = ['Aubergine', 'Broccoli', 'Carrot', 'Courgette', 'Leek'];
</script>
<template>
<ASelectRoot v-model="fruit">
<ASelectTrigger
class="group text-sm color-text-highlighted px-2.5 py-1.5 pe-9 rounded-md bg-background inline-flex gap-1.5 ring ring-ring-accented ring-inset transition-colors-280 items-center relative focus:(outline-none ring-2 ring-primary ring-inset)"
aria-label="Customise options"
>
<ASelectValue
class="pointer-events-none truncate"
placeholder="ASelect a fruit..."
/>
<span class="pe-2.5 flex items-center end-0 inset-y-0 absolute">
<span
class="i-lucide:chevron-down color-text-dimmed shrink-0 size-5"
/>
</span>
</ASelectTrigger>
<ASelectPortal>
<ASelectContent
class="rounded-md bg-white flex flex-col max-h-60 w-$akar-select-trigger-width pointer-events-auto ring ring-ring shadow-lg origin-$akar-select-content-transform-origin overflow-hidden data-[state=closed]:animate-out data-[state=open]:animate-in data-[state=closed]:animate-duration-280 data-[state=open]:animate-duration-280 data-[state=open]:fade-in-0 data-[state=closed]:fade-out-0 data-[state=open]:data-[side=bottom]:slide-in-top-5% data-[state=closed]:data-[side=bottom]:slide-out-top-5%"
:side-offset="5"
>
<ASelectViewport class="p-[5px]">
<ASelectGroup class="p-1 isolate">
<ASelectLabel class="text-xs color-text-highlighted font-semibold p-1.5 gap-1.5">
Fruits
</ASelectLabel>
<ASelectItem
v-for="(option, index) in fruits"
:key="index"
class="group text-sm p-1.5 outline-none flex gap-1.5 w-full cursor-pointer select-none transition-colors-280 items-center relative data-[state=checked]:color-primary before:(rounded-md content-empty transition-colors-280 inset-px absolute -z-1) data-[disabled]:(opacity-75 cursor-not-allowed) data-[highlighted]:not-[[data-state=checked]]:color-black data-[state=checked]:before:bg-background-elevated data-[highlighted]:not-[[data-state=checked]]:before:bg-primary/30"
:value="option"
>
<ASelectItemText class="flex flex-1 flex-col min-w-0">
{{ option }}
</ASelectItemText>
<span class="ms-auto inline-flex gap-1.5 items-center">
<ASelectItemIndicator class="shrink-0 size-5">
<i class="i-lucide:check" />
</ASelectItemIndicator>
</span>
</ASelectItem>
</ASelectGroup>
<ASelectSeparator class="my-1 bg-border h-px -mx-1" />
<ASelectGroup class="p-1 isolate">
<ASelectLabel class="text-xs color-text-highlighted font-semibold p-1.5 gap-1.5">
Vegetables
</ASelectLabel>
<ASelectItem
v-for="(option, index) in vegetables"
:key="index"
class="group text-sm p-1.5 outline-none flex gap-1.5 w-full cursor-pointer select-none transition-colors-280 items-center relative data-[state=checked]:color-primary before:(rounded-md content-empty transition-colors-280 inset-px absolute -z-1) data-[disabled]:(opacity-75 cursor-not-allowed) data-[highlighted]:not-[[data-state=checked]]:color-black data-[state=checked]:before:bg-background-elevated data-[highlighted]:not-[[data-state=checked]]:before:bg-primary/30"
:value="option"
:disabled="option === 'Courgette'"
>
<ASelectItemText class="flex flex-1 flex-col min-w-0">
{{ option }}
</ASelectItemText>
<span class="ms-auto inline-flex gap-1.5 items-center">
<ASelectItemIndicator class="shrink-0 size-5">
<i class="i-lucide:check" />
</ASelectItemIndicator>
</span>
</ASelectItem>
</ASelectGroup>
</ASelectViewport>
</ASelectContent>
</ASelectPortal>
</ASelectRoot>
</template>
Import all parts and piece them together.
<script setup lang="ts">
import {
ASelectContent,
ASelectGroup,
ASelectIcon,
ASelectItem,
ASelectItemIndicator,
ASelectLabel,
ASelectPortal,
ASelectRoot,
ASelectScrollDownButton,
ASelectScrollUpButton,
ASelectSeparator,
ASelectTrigger,
ASelectValue,
ASelectViewport,
} from 'akar';
</script>
<template>
<ASelectRoot>
<ASelectTrigger>
<ASelectValue />
<ASelectIcon />
</ASelectTrigger>
<ASelectPortal>
<ASelectContent>
<ASelectScrollUpButton />
<ASelectViewport>
<ASelectItem>
<ASelectItemText />
<ASelectItemIndicator />
</ASelectItem>
<ASelectGroup>
<ASelectLabel />
<ASelectItem>
<ASelectItemText />
<ASelectItemIndicator />
</ASelectItem>
</ASelectGroup>
<ASelectSeparator />
</ASelectViewport>
<ASelectScrollDownButton />
<ASelectArrow />
</ASelectContent>
</ASelectPortal>
</ASelectRoot>
</template>
One benefit of using Akar is its flexibility and low-level control over the components. However, this also means that you may need to manually construct more complex UI elements by combining multiple Akar components together.
If you feel there's a lot of elements that needs to be constructed manually using Akar, consider using Pohon UI instead. It provides a higher-level abstraction over Akar components with pre-defined styles and behaviors that can help you build UIs faster.
Contains all the parts of a ASelect
| Prop | Default | Type |
|---|---|---|
autocomplete | stringNative html input | |
by | string | ((a: AcceptableValue, b: AcceptableValue) => boolean)Use this to compare objects by a particular field, or pass your own comparison function for complete control over how objects are compared. | |
defaultOpen | booleanThe open state of the select when it is initially rendered. Use when you do not need to control its open state. | |
defaultValue | AcceptableValue | AcceptableValue[]The value of the select when initially rendered. Use when you do not need to control the state of the Select | |
dir | 'ltr' | 'rtl'The reading direction of the combobox when applicable. | |
disabled | booleanWhen | |
modelValue | AcceptableValue | AcceptableValue[] | |
multiple | booleanWhether multiple options can be selected or not. | |
name | stringThe name of the field. Submitted with its owning form as part of a name/value pair. | |
open | boolean | |
required | booleanWhen |
| Event | Type |
|---|---|
update:modelValue | [value: AcceptableValue]Event handler called when the value changes. |
update:open | [value: boolean]Event handler called when the open state of the context menu changes. |
| Slot | Type |
|---|---|
modelValue | AcceptableValue | AcceptableValue[] The controlled value of the Select. Can be bind as |
open | booleanThe controlled open state of the Select. Can be bind as |
The button that toggles the ASelect The ASelectContent will position itself by aligning over the trigger.
| Prop | Default | Type |
|---|---|---|
as | 'button' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
disabled | boolean | |
reference | ReferenceElementThe reference (or anchor) element that is being referred to for positioning. If not provided will use the current component as anchor. |
| Attribute | Value |
|---|---|
[data-state] | 'open' | 'closed' |
[data-placeholder] | Present when has placeholder |
[data-disabled] | Present when disabled |
The part that reflects the selected value. By default the selected item's text will be rendered. if you require more control, you can instead control the select and pass your own children. It should not be styled to ensure correct positioning. An optional placeholder prop is also available for when the select has no value.
| Prop | Default | Type |
|---|---|---|
as | 'span' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
placeholder | '' | stringThe content that will be rendered inside the |
| Slot | Type |
|---|---|
selectedLabel | string[] |
modelValue | AcceptableValue | AcceptableValue[] |
A small icon often displayed next to the value as a visual affordance for the fact it can be open. By default renders â–¼ but you can use your own icon via asChild or use children.
When used, portals the content part into the body.
| Prop | Default | Type |
|---|---|---|
defer | booleanDefer the resolving of a Teleport target until other parts of the application have mounted (requires Vue 3.5.0+) {@link https://vuejs.org/guide/built-ins/teleport.html#deferred-teleport} | |
disabled | booleanWhen | |
forceMount | booleanUsed to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries. | |
to | string | HTMLElementVue native teleport component prop {@link https://vuejs.org/guide/built-ins/teleport.html#basic-usage} |
The component that pops out when the select is open.
| Prop | Default | Type |
|---|---|---|
as | 'div' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
align | 'start' | 'center' | 'end'The preferred alignment against the trigger. May change when collisions occur. | |
alignFlip | booleanFlip alignment when colliding with boundary.
May only occur when | |
alignOffset | numberAn offset in pixels from the | |
arrowPadding | numberThe padding between the arrow and the edges of the content. If your content has border-radius, this will prevent it from overflowing the corners. | |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
avoidCollisions | booleanWhen | |
bodyLock | booleanThe document.body will be lock, and scrolling will be disabled. | |
collisionBoundary | Element | (Element | null)[] | nullThe element used as the collision boundary. By default this is the viewport, though you can provide additional element(s) to be included in this check. | |
collisionPadding | number | Partial<Record<Side, number>>The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { top: 20, left: 20 }. | |
disableUpdateOnLayoutShift | booleanWhether to disable the update position for the content when the layout shifted. | |
forceMount | booleanUsed to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries. | |
hideWhenDetached | booleanWhether to hide the content when the trigger becomes fully occluded. | |
position | 'popper' | 'item-aligned'The positioning mode to use
| |
positionStrategy | 'fixed' | 'absolute'The type of CSS position property to use. | |
prioritizePosition | booleanForce content to be position within the viewport. Might overlap the reference element, which may not be desired. | |
reference | ReferenceElementThe reference (or anchor) element that is being referred to for positioning. If not provided will use the current component as anchor. | |
side | 'top' | 'right' | 'bottom' | 'left'The preferred side of the trigger to render against when open. Will be reversed when collisions occur and avoidCollisions is enabled. | |
sideFlip | booleanFlip to the opposite side when colliding with boundary. | |
sideOffset | numberThe distance in pixels from the trigger. | |
sticky | 'always' | 'partial'The sticky behavior on the align axis. | |
updatePositionStrategy | 'always' | 'optimized'Strategy to update the position of the floating element on every animation frame. |
| Event | Type |
|---|---|
closeAutoFocus | [event: Event]Event handler called when auto-focusing on close. Can be prevented. |
escapeKeyDown | [event: KeyboardEvent]Event handler called when the escape key is down. Can be prevented. |
pointerDownOutside | [event: PointerDownOutsideEvent]Event handler called when a |
| Attribute | Value |
|---|---|
[data-state] | 'open' | 'closed' |
[data-side] | 'left' | 'right' | 'bottom' | 'top' |
[data-align] | 'start' | 'end' | 'center' |
| Variable | Description |
|---|---|
--akar-select-content-transform-origin | The |
--akar-select-content-available-width | The remaining width between the trigger and the boundary edge. Only present when |
--akar-select-content-available-height | The remaining height between the trigger and the boundary edge. Only present when |
--akar-select-trigger-width | The width of the trigger. Only present when |
--akar-select-trigger-height | The height of the trigger. Only present when |
The scrolling viewport that contains all of the items.
| Prop | Default | Type |
|---|---|---|
as | 'div' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
nonce | stringWill add |
The component that contains the select items.
| Prop | Default | Type |
|---|---|---|
as | 'div' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
disabled | booleanWhen | |
textValue | stringOptional text used for typeahead purposes. By default the typeahead behavior will use the Use this when the content is complex, or you have non-textual content inside. | |
value* | AcceptableValueThe value given as data when submitted with a |
| Event | Type |
|---|---|
select | [event: SelectEvent<AcceptableValue>]Event handler called when the selecting item. |
| Attribute | Value |
|---|---|
[data-state] | 'checked' | 'unchecked' |
[data-highlighted] | Present when highlighted |
[data-disabled] | Present when disabled |
The textual part of the item. It should only contain the text you want to see in the trigger when that item is selected. It should not be styled to ensure correct positioning.
| Prop | Default | Type |
|---|---|---|
as | 'span' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
Renders when the item is selected. You can style this element directly, or you can use it as a wrapper to put an icon into, or both.
| Prop | Default | Type |
|---|---|---|
as | 'span' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
An optional button used as an affordance to show the viewport overflow as well as functionally enable scrolling upwards.
| Prop | Default | Type |
|---|---|---|
as | 'div' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
An optional button used as an affordance to show the viewport overflow as well as functionally enable scrolling downwards.
| Prop | Default | Type |
|---|---|---|
as | 'div' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
Used to group multiple items. use in conjunction with ASelectLabel to ensure good accessibility via automatic labelling.
| Prop | Default | Type |
|---|---|---|
as | 'div' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
Used to render the label of a group. It won't be focusable using arrow keys.
| Prop | Default | Type |
|---|---|---|
as | 'div' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
for | string |
Used to visually separate items in the ASelect
| Prop | Default | Type |
|---|---|---|
as | 'div' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
An optional arrow element to render alongside the content. This can be used to help visually link the trigger with the ASelectContent. Must be rendered inside ASelectContent. Only available when position is set to popper.
| Prop | Default | Type |
|---|---|---|
as | 'svg' | APrimitiveAsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
height | 5 | numberThe height of the arrow in pixels. |
rounded | booleanWhen | |
width | 10 | numberThe width of the arrow in pixels. |
By default, ASelect will behave similarly to a native MacOS menu by positioning ASelectContent relative to the active item. If you would prefer an alternative positioning approach similar to Popover or DropdownMenu then you can set position to popper and make use of additional alignment options such as side, sideOffset and more.
// index.vue
<script setup lang="ts">
import {
ASelectContent,
ASelectPortal,
ASelectRoot,
ASelectTrigger,
} from 'akar';
</script>
<template>
<ASelectRoot>
<ASelectTrigger>…</ASelectTrigger>
<ASelectPortal>
<ASelectContent
position="popper"
:side-offset="5"
>
…
</ASelectContent>
</ASelectPortal>
</ASelectRoot>
</template>
When using position="popper" on ASelectContent, you may want to constrain the width of the content so that it matches the trigger width. You may also want to constrain its height to not exceed the viewport.
We expose several CSS custom properties such as --akar-select-trigger-width and --akar-select-content-available-height to support this. Use them to constrain the content dimensions.
// index.vue
<script setup lang="ts">
import {
ASelectContent,
ASelectGroup,
ASelectItem,
ASelectItemIndicator,
ASelectLabel,
ASelectPortal,
ASelectRoot,
ASelectSeparator,
ASelectTrigger,
} from 'akar';
</script>
<template>
<ASelectRoot>
<ASelectTrigger>…</ASelectTrigger>
<ASelectPortal>
<ASelectContent
class="ASelectContent"
position="popper"
:side-offset="5"
>
…
</ASelectContent>
</ASelectPortal>
</ASelectRoot>
</template>
/* styles.css */
.ASelectContent {
width: var(--akar-select-trigger-width);
max-height: var(--akar-select-content-available-height);
}
You can add special styles to disabled items via the data-disabled attribute.
// index.vue
<script setup lang="ts">
import {
ASelectContent,
ASelectGroup,
ASelectItem,
ASelectItemIndicator,
ASelectLabel,
ASelectPortal,
ASelectRoot,
ASelectSeparator,
ASelectTrigger,
} from 'akar';
</script>
<template>
<ASelectRoot>
<ASelectTrigger>…</ASelectTrigger>
<ASelectPortal>
<ASelectContent>
<ASelectViewport>
<ASelectItem
class="ASelectItem"
disabled
>
…
</ASelectItem>
<ASelectItem>…</ASelectItem>
<ASelectItem>…</ASelectItem>
</ASelectViewport>
</ASelectContent>
</ASelectPortal>
</ASelectRoot>
</template>
/* styles.css */
.ASelectItem[data-disabled] {
color: 'gainsboro';
}
You can use the placeholder prop on Value for when the select has no value. There's also a data-placeholder attribute on Trigger to help with styling.
// index.vue
<script setup lang="ts">
import {
ASelectContent,
ASelectGroup,
ASelectItem,
ASelectItemIndicator,
ASelectLabel,
ASelectPortal,
ASelectRoot,
ASelectSeparator,
ASelectTrigger,
} from 'akar';
import './styles.css';
</script>
<template>
<ASelectRoot>
<ASelectTrigger class="ASelectTrigger">
<ASelectValue placeholder="Pick an option" />
<ASelectIcon />
</ASelectTrigger>
<ASelectPortal>
<ASelectContent>…</ASelectContent>
</ASelectPortal>
</ASelectRoot>
</template>
/* styles.css */
.ASelectTrigger[data-placeholder] {
color: 'gainsboro';
}
Use the Separator part to add a separator between items.
<template>
<ASelectRoot>
<ASelectTrigger>…</ASelectTrigger>
<ASelectPortal>
<ASelectContent>
<ASelectViewport>
<ASelectItem>…</ASelectItem>
<ASelectItem>…</ASelectItem>
<ASelectItem>…</ASelectItem>
<ASelectSeparator />
<ASelectItem>…</ASelectItem>
<ASelectItem>…</ASelectItem>
</ASelectViewport>
</ASelectContent>
</ASelectPortal>
</ASelectRoot>
</template>
Use the Group and Label parts to group items in a section.
<template>
<ASelectRoot>
<ASelectTrigger>…</ASelectTrigger>
<ASelectPortal>
<ASelectContent>
<ASelectViewport>
<ASelectGroup>
<ASelectLabel>Label</ASelectLabel>
<ASelectItem>…</ASelectItem>
<ASelectItem>…</ASelectItem>
<ASelectItem>…</ASelectItem>
</ASelectGroup>
</ASelectViewport>
</ASelectContent>
</ASelectPortal>
</ASelectRoot>
</template>
You can use custom content in your items.
<script setup lang="ts">
import {
ASelectContent,
ASelectGroup,
ASelectItem,
ASelectItemIndicator,
ASelectLabel,
ASelectPortal,
ASelectRoot,
ASelectSeparator,
ASelectTrigger,
} from 'akar';
</script>
<template>
<ASelectRoot>
<ASelectTrigger>…</ASelectTrigger>
<ASelectPortal>
<ASelectContent>
<ASelectViewport>
<ASelectItem>
<ASelectItemText>
<img src="…">
Adolfo Hess
</ASelectItemText>
<ASelectItemIndicator>…</ASelectItemIndicator>
</ASelectItem>
<ASelectItem>…</ASelectItem> <ASelectItem>…</ASelectItem>
</ASelectViewport>
</ASelectContent>
</ASelectPortal>
</ASelectRoot>
</template>
By default the trigger display the selected item's text (no longer automatically render ItemText's content like in v1).
If you need to render other than plain text, you can control the component using v-model props (or accessing ASelectValue's slotProps) and passing slot to ASelectValue. Remember to make sure what you put in there is accessible.
<script setup>
const countries = { 'france': '🇫🇷', 'united-kingdom': '🇬🇧', 'spain': '🇪🇸' };
const value = ref('france');
</script>
<template>
<ASelectRoot v-model="value">
<ASelectTrigger>
<ASelectValue :aria-label="value">
{{ countries[value] }}
</ASelectValue>
<ASelectIcon />
</ASelectTrigger>
<ASelectPortal>
<ASelectContent>
<ASelectViewport>
<ASelectItem value="france">
<ASelectItemText>France</ASelectItemText>
<ASelectItemIndicator>…</ASelectItemIndicator>
</ASelectItem>
<ASelectItem value="united-kingdom">
<ASelectItemText>United Kingdom</ASelectItemText>
<ASelectItemIndicator>…</ASelectItemIndicator>
</ASelectItem>
<ASelectItem value="spain">
<ASelectItemText>Spain</ASelectItemText>
<ASelectItemIndicator>…</ASelectItemIndicator>
</ASelectItem>
</ASelectViewport>
</ASelectContent>
</ASelectPortal>
</ASelectRoot>
</template>
The native scrollbar is hidden by default as we recommend using the ScrollUpButton and ScrollDownButton parts for the best UX. If you do not want to use these parts, compose your select with our Scroll Area primitive.
// index.vue
<script setup lang="ts">
import {
ASelectContent,
ASelectGroup,
ASelectItem,
ASelectItemIndicator,
ASelectLabel,
ASelectPortal,
ASelectRoot,
ASelectSeparator,
ASelectTrigger,
ScrollAreaRoot,
ScrollAreaScrollbar,
ScrollAreaThumb,
ScrollAreaViewport,
} from 'akar';
</script>
<template>
<ASelectRoot>
<ASelectTrigger>…</ASelectTrigger>
<ASelectPortal>
<ASelectContent>
<ScrollAreaRoot
class="ScrollAreaRoot"
type="auto"
>
<ASelectViewport as-child>
<ScrollAreaViewport class="ScrollAreaViewport">
<StyledItem>…</StyledItem> <StyledItem>…</StyledItem>
<StyledItem>…</StyledItem>
</ScrollAreaViewport>
</ASelectViewport>
<ScrollAreaScrollbar
class="ScrollAreaScrollbar"
orientation="vertical"
>
<ScrollAreaThumb class="ScrollAreaThumb" />
</ScrollAreaScrollbar>
</ScrollAreaRoot>
</ASelectContent>
</ASelectPortal>
</ASelectRoot>
</template>
/* styles.css */
.ScrollAreaRoot {
width: 100%;
height: 100%;
}
.ScrollAreaViewport {
width: 100%;
height: 100%;
}
.ScrollAreaScrollbar {
width: 4px;
padding: 5px 2px;
}
.ScrollAreaThumb {
background: rgba(0, 0, 0, 0.3);
borderradius: 3px;
}
Adheres to the ListBox WAI-ARIA design pattern.
See the W3C ASelect-Only Combobox example for more information.
| Key | Description |
|---|---|
Space | When focus is on |
Enter | When focus is on |
ArrowDown | When focus is on |
ArrowUp | When focus is on |
Esc | Closes the select and moves focus to |
Use our Label component in order to offer a visual and accessible label for the ASelect
<script setup lang="ts">
import { Icon } from '@iconify/vue';
import {
ASelectContent,
ASelectGroup,
ASelectItem,
ASelectItemIndicator,
ASelectLabel,
ASelectPortal,
ASelectRoot,
ASelectSeparator,
ASelectTrigger,
Label,
} from 'akar';
import { ref } from 'vue';
</script>
<template>
<Label>
Country
<ASelectRoot>…</ASelectRoot>
</Label>
<!-- or -->
<Label for="country">Country</Label>
<ASelectRoot>
<ASelectTrigger id="country">
…
</ASelectTrigger>
<ASelectPortal>
<ASelectContent>…</ASelectContent>
</ASelectPortal>
</ASelectRoot>
</template>
Create your own API by abstracting the primitive parts into your own component.
ASelect and ASelectItemThis example abstracts most of the parts.
<script setup lang="ts">
import { ASelect, ASelectItem } from './your-select';
</script>
<template>
<ASelect default-value="2">
<ASelectItem value="1">
Item 1
</ASelectItem>
<ASelectItem value="2">
Item 2
</ASelectItem>
<ASelectItem value="3">
Item 3
</ASelectItem>
</ASelect>
</template>
// your-select.ts
export { default as ASelect } from 'ASelect.vue';
export { default as ASelectItem } from 'ASelectItem.vue';
<!-- ASelect.vue -->
<script setup lang="ts">
import type { ASelectRootEmits, ASelectRootProps } from 'akar';
import { ChevronDownIcon, ChevronUpIcon, } from '@radix-icons/vue';
import { ASelectContent, ASelectIcon, ASelectPortal, ASelectRoot, ASelectScrollDownButton, ASelectScrollUpButton, ASelectTrigger, ASelectValue, ASelectViewport, useForwardPropsEmits } from 'akar';
const props = defineProps<ASelectRootProps>();
const emits = defineEmits<ASelectRootEmits>();
const forward = useForwardPropsEmits(props, emits);
</script>
<template>
<ASelectRoot v-bind="forward">
<ASelectTrigger>
<ASelectValue />
<ASelectIcon>
<ChevronDownIcon />
</ASelectIcon>
</ASelectTrigger>
<ASelectPortal>
<ASelectContent>
<ASelectScrollUpButton>
<ChevronUpIcon />
</ASelectScrollUpButton>
<ASelectViewport>
<slot />
</ASelectViewport>
<ASelectScrollDownButton>
<ChevronDownIcon />
</ASelectScrollDownButton>
</ASelectContent>
</ASelectPortal>
</ASelectRoot>
</template>
<!-- ASelectItem.vue -->
<script setup lang="ts">
import type { ASelectItemProps } from 'akar';
import { CheckIcon } from '@radix-icons/vue';
import { ASelectItem, ASelectItemIndicator, ASelectItemText } from 'akar';
const props = defineProps<ASelectItemProps>();
</script>
<template>
<ASelectItem v-bind="props">
<ASelectItemText>
<slot />
</ASelectItemText>
<ASelectItemIndicator>
<CheckIcon />
</ASelectItemIndicator>
</ASelectItem>
</template>