Listbox
A component for selecting a single or multiple items from a list.
Anatomy
To set up the Listbox correctly, you'll need to understand its anatomy and how we name its parts.
Each part includes a
data-part
attribute to help identify them in the DOM.
Examples
Basic
Here's a basic example of the Listbox component.
import { Listbox, createListCollection } from '@ark-ui/react/listbox'
export const Basic = () => {
const collection = createListCollection({ items: ['React', 'Solid', 'Vue'] })
return (
<Listbox.Root collection={collection}>
<Listbox.Label>Select your Framework</Listbox.Label>
<Listbox.Content>
{collection.items.map((item) => (
<Listbox.Item key={item} item={item}>
<Listbox.ItemText>{item}</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
))}
</Listbox.Content>
</Listbox.Root>
)
}
import { Listbox, createListCollection } from '@ark-ui/solid/listbox'
import { Index } from 'solid-js'
export const Basic = () => {
const collection = createListCollection({ items: ['React', 'Solid', 'Vue'] })
return (
<Listbox.Root collection={collection}>
<Listbox.Label>Select your Framework</Listbox.Label>
<Listbox.Content>
<Index each={collection.items}>
{(item) => (
<Listbox.Item item={item()}>
<Listbox.ItemText>{item()}</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
)}
</Index>
</Listbox.Content>
</Listbox.Root>
)
}
<script setup lang="ts">
import { Listbox, createListCollection } from '@ark-ui/vue/listbox'
const collection = createListCollection({
items: ['React', 'Solid', 'Vue'],
})
</script>
<template>
<Listbox.Root :collection="collection">
<Listbox.Label>Framework</Listbox.Label>
<Listbox.Content>
<Listbox.Item v-for="item in collection.items" :key="item" :item="item">
<Listbox.ItemText>{{ item }}</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
</Listbox.Content>
</Listbox.Root>
</template>
Controlled
The Listbox component can be controlled by using the value
and onValueChange
props. This allows you to manage the
selected value externally.
import { Listbox, createListCollection } from '@ark-ui/react/listbox'
import { useState } from 'react'
export const Controlled = () => {
const collection = createListCollection({ items: ['React', 'Solid', 'Vue'] })
const [value, setValue] = useState(['React'])
return (
<Listbox.Root value={value} onValueChange={(e) => setValue(e.value)} collection={collection}>
<Listbox.Label>Select your Framework</Listbox.Label>
<Listbox.Content>
{collection.items.map((item) => (
<Listbox.Item key={item} item={item}>
<Listbox.ItemText>{item}</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
))}
</Listbox.Content>
</Listbox.Root>
)
}
import { Listbox, createListCollection } from '@ark-ui/solid/listbox'
import { Index, createSignal } from 'solid-js'
export const Controlled = () => {
const collection = createListCollection({ items: ['React', 'Solid', 'Vue'] })
const [value, setValue] = createSignal(['React'])
return (
<Listbox.Root value={value()} onValueChange={(e) => setValue(e.value)} collection={collection}>
<Listbox.Label>Select your Framework</Listbox.Label>
<Listbox.Content>
<Index each={collection.items}>
{(item) => (
<Listbox.Item item={item()}>
<Listbox.ItemText>{item()}</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
)}
</Index>
</Listbox.Content>
</Listbox.Root>
)
}
<script setup lang="ts">
import { Listbox, createListCollection } from '@ark-ui/vue/listbox'
import { ref } from 'vue'
const collection = createListCollection({
items: ['React', 'Solid', 'Vue'],
})
const value = ref(['React'])
</script>
<template>
<Listbox.Root :collection="collection" v-model="value">
<Listbox.Label>Framework</Listbox.Label>
<Listbox.Content>
<Listbox.Item v-for="item in collection.items" :key="item" :item="item">
<Listbox.ItemText>{{ item }}</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
</Listbox.Content>
</Listbox.Root>
</template>
Disabled Item
Listbox items can be disabled using the disabled
prop on the collection item.
import { Listbox, createListCollection } from '@ark-ui/react/listbox'
export const Disabled = () => {
const collection = createListCollection({
items: [
{ label: 'React', value: 'react' },
{ label: 'Solid', value: 'solid' },
{ label: 'Svelte', value: 'svelte', disabled: true },
{ label: 'Vue', value: 'vue' },
],
})
return (
<Listbox.Root collection={collection}>
<Listbox.Label>Select your Framework</Listbox.Label>
<Listbox.Content>
{collection.items.map((item) => (
<Listbox.Item key={item.value} item={item}>
<Listbox.ItemText>{item.label}</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
))}
</Listbox.Content>
</Listbox.Root>
)
}
import { Listbox, createListCollection } from '@ark-ui/solid/listbox'
import { Index } from 'solid-js'
export const Disabled = () => {
const collection = createListCollection({
items: [
{ label: 'React', value: 'react' },
{ label: 'Solid', value: 'solid' },
{ label: 'Svelte', value: 'svelte', disabled: true },
{ label: 'Vue', value: 'vue' },
],
})
return (
<Listbox.Root collection={collection}>
<Listbox.Label>Select your Framework</Listbox.Label>
<Listbox.Content>
<Index each={collection.items}>
{(item) => (
<Listbox.Item item={item()}>
<Listbox.ItemText>{item().label}</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
)}
</Index>
</Listbox.Content>
</Listbox.Root>
)
}
<script setup lang="ts">
import { Listbox, createListCollection } from '@ark-ui/vue/listbox'
const collection = createListCollection({
items: [
{ label: 'React', value: 'react' },
{ label: 'Solid', value: 'solid' },
{ label: 'Svelte', value: 'svelte', disabled: true },
{ label: 'Vue', value: 'vue' },
],
})
</script>
<template>
<Listbox.Root :collection="collection">
<Listbox.Label>Select your Framework</Listbox.Label>
<Listbox.Content>
<Listbox.Item v-for="item in collection.items" :key="item.value" :item="item">
<Listbox.ItemText>{{ item.label }}</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
</Listbox.Content>
</Listbox.Root>
</template>
You can also use the
isItemDisabled
within thecreateListCollection
to disable items based on a condition.
Multiple Selection
You can set the selectionMode
property as multiple
to allow the user to select multiple items at a time.
import { Listbox, createListCollection } from '@ark-ui/react/listbox'
export const Multiple = () => {
const collection = createListCollection({ items: ['React', 'Solid', 'Vue'] })
return (
<Listbox.Root collection={collection} selectionMode="multiple">
<Listbox.Label>Select your Framework</Listbox.Label>
<Listbox.Content>
{collection.items.map((item) => (
<Listbox.Item key={item} item={item}>
<Listbox.ItemText>{item}</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
))}
</Listbox.Content>
</Listbox.Root>
)
}
import { Listbox, createListCollection } from '@ark-ui/solid/listbox'
import { Index } from 'solid-js'
export const Multiple = () => {
const collection = createListCollection({ items: ['React', 'Solid', 'Vue'] })
return (
<Listbox.Root collection={collection} selectionMode="multiple">
<Listbox.Label>Select your Framework</Listbox.Label>
<Listbox.Content>
<Index each={collection.items}>
{(item) => (
<Listbox.Item item={item()}>
<Listbox.ItemText>{item()}</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
)}
</Index>
</Listbox.Content>
</Listbox.Root>
)
}
<script setup lang="ts">
import { Listbox, createListCollection } from '@ark-ui/vue/listbox'
const collection = createListCollection({
items: ['React', 'Solid', 'Vue'],
})
</script>
<template>
<Listbox.Root :collection="collection" selectionMode="multiple">
<Listbox.Label>Select your Framework</Listbox.Label>
<Listbox.Content>
<Listbox.Item v-for="item in collection.items" :key="item" :item="item">
<Listbox.ItemText>{{ item }}</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
</Listbox.Content>
</Listbox.Root>
</template>
API Reference
Root
Prop | Default | Type |
---|---|---|
collection | ListCollection<T> The collection of items | |
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
defaultHighlightedValue | string The initial value of the highlighted item when opened. Use when you don't need to control the highlighted value of the listbox. | |
defaultValue | [] | string[] The initial default value of the listbox when rendered. Use when you don't need to control the value of the listbox. |
deselectable | boolean Whether to disallow empty selection | |
disabled | boolean Whether the listbox is disabled | |
disallowSelectAll | boolean Whether to disallow selecting all items when `meta+a` is pressed | |
highlightedValue | string The controlled key of the highlighted item | |
id | string The unique identifier of the machine. | |
ids | Partial<{
root: string
content: string
label: string
item(id: string | number): string
itemGroup(id: string | number): string
itemGroupLabel(id: string | number): string
}> The ids of the elements in the listbox. Useful for composition. | |
loopFocus | false | boolean Whether to loop the keyboard navigation through the options |
modelValue | string[] The model value of the listbox | |
orientation | 'horizontal' | 'horizontal' | 'vertical' The orientation of the element. |
scrollToIndexFn | (details: ScrollToIndexDetails) => void Function to scroll to a specific index | |
selectionMode | 'single' | SelectionMode How multiple selection should behave in the listbox. - `single`: The user can select a single item. - `multiple`: The user can select multiple items without using modifier keys. - `extended`: The user can select multiple items by using modifier keys. |
selectOnHighlight | boolean Whether to select the item when it is highlighted | |
typeahead | boolean Whether to enable typeahead on the listbox |
Data Attribute | Value |
---|---|
[data-scope] | listbox |
[data-part] | root |
[data-orientation] | The orientation of the listbox |
[data-disabled] | Present when disabled |
Content
Prop | Default | Type |
---|---|---|
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Data Attribute | Value |
---|---|
[data-scope] | listbox |
[data-part] | content |
[data-activedescendant] | |
[data-orientation] | The orientation of the content |
[data-layout] | |
[data-empty] |
Input
Prop | Default | Type |
---|---|---|
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Data Attribute | Value |
---|---|
[data-scope] | listbox |
[data-part] | input |
[data-disabled] | Present when disabled |
ItemGroupLabel
Prop | Default | Type |
---|---|---|
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
ItemGroup
Prop | Default | Type |
---|---|---|
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
id | string |
Data Attribute | Value |
---|---|
[data-scope] | listbox |
[data-part] | item-group |
[data-disabled] | Present when disabled |
[data-orientation] | The orientation of the item |
[data-empty] |
ItemIndicator
Prop | Default | Type |
---|---|---|
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Data Attribute | Value |
---|---|
[data-scope] | listbox |
[data-part] | item-indicator |
[data-state] | "checked" | "unchecked" |
Item
Prop | Default | Type |
---|---|---|
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
highlightOnHover | boolean Whether to highlight the item on hover | |
item | any The item to render |
Data Attribute | Value |
---|---|
[data-scope] | listbox |
[data-part] | item |
[data-value] | The value of the item |
[data-selected] | Present when selected |
[data-layout] | |
[data-state] | "checked" | "unchecked" |
[data-orientation] | The orientation of the item |
[data-highlighted] | Present when highlighted |
[data-disabled] | Present when disabled |
ItemText
Prop | Default | Type |
---|---|---|
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Data Attribute | Value |
---|---|
[data-scope] | listbox |
[data-part] | item-text |
[data-state] | "checked" | "unchecked" |
[data-disabled] | Present when disabled |
[data-highlighted] | Present when highlighted |
Label
Prop | Default | Type |
---|---|---|
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Data Attribute | Value |
---|---|
[data-scope] | listbox |
[data-part] | label |
[data-disabled] | Present when disabled |
RootProvider
Prop | Default | Type |
---|---|---|
value | ListboxApi<PropTypes, T> | |
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
ValueText
Prop | Default | Type |
---|---|---|
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
placeholder | string |
Data Attribute | Value |
---|---|
[data-scope] | listbox |
[data-part] | value-text |
[data-disabled] | Present when disabled |