Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Listbox for Svelte

A list that keeps every option on screen, picked one at a time, several at once or as a Shift-click range. A listbox for Svelte and Ark UI projects.

On this page9 sections
UI library: Ark UI

A listbox keeps every option on the page, so people can compare them before they pick one or several. You build the options once as a collection and render a row for each, and the selection comes back as an array of their values.

Use a Select when the options should wait behind a button, and a Combobox when typing beats scanning.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Listbox anatomy: content, item, item indicator, item group label, item text

Usage

<script lang="ts">
import { createListCollection } from "@ark-ui/react/collection";
import {
  Listbox, ListboxContent, ListboxItem, ListboxItemIndicator,
  ListboxItemText,
} from "@/components/ui/listbox";
</script>

Each ListboxItem takes its item from the collection. defaultValue lists the values that start selected, as an array even when only one row can be picked.

const Example = () => (
  <Listbox
    className="w-full max-w-64"
    collection={collection}
    defaultValue={["br"]}
  >
    <ListboxContent aria-label="Country">
      {collection.items.map((item) => (
        <ListboxItem item={item} key={item.value}>
          <ListboxItemText>{item.label}</ListboxItemText>
          <ListboxItemIndicator />
        </ListboxItem>
      ))}
    </ListboxContent>
  </Listbox>
);

const collection = createListCollection({
  items: [
    { label: "Brazil", value: "br" },
    { label: "Mexico", value: "mx" },
    { label: "Ireland", value: "ie" },
  ],
});

Accessibility

ListboxContent renders the element with role="listbox", and each ListboxItem is an option that reports aria-selected, plus aria-disabled when it can't be picked. The list is one tab stop: focus stays on it while aria-activedescendant tells a screen reader which row is highlighted, and multiple and extended modes set aria-multiselectable. Naming the list is up to you: render ListboxLabel inside Listbox and the list takes its name from it, or put aria-label on ListboxContent when there is no visible heading. A label on Listbox itself reaches nobody, because its element has no role.

Tab
Moves focus into the list as a single stop. With nothing selected, the first item is highlighted.
Arrow Down
Highlights the next item, skipping disabled ones. In a grid, moves to the same column in the next row.
Arrow Up
Highlights the previous item, skipping disabled ones. In a grid, moves to the same column in the row above.
Arrow Right
In a horizontal list or a grid, highlights the next item.
Arrow Left
In a horizontal list or a grid, highlights the previous item.
Home
Highlights the first item.
End
Highlights the last item.
Enter
Selects the highlighted item. In multiple mode it toggles that item, and in single and extended modes it replaces the selection.
Space
Does what Enter does, except during a typeahead search, where it adds a space to the search.
Shift+Arrow Down
In multiple and extended modes, highlights the next item and selects the run from where the range started.
Shift+Arrow Up
In multiple and extended modes, highlights the previous item and selects the run from where the range started.
Ctrl+A
In multiple and extended modes, selects every item, unless disallowSelectAll is set. Cmd + A on a Mac.
Escape
Clears the selection when deselectable is set, and does nothing otherwise.
A-Z
Highlights the next item whose label starts with the typed letters.

Demos

Controlled

The parent holds the selection in state, starting on Medium out of four sizes, and the line under the list names the size that is picked. Hold the value yourself when a price, a preview or the next step depends on it. When nothing outside the list needs the value, setting defaultValue is enough.

Preview width

Disabled

Brazil, Mexico and Ireland stay readable, but disabled on Listbox stops every click and key from changing the selection. Lock the whole list this way while an earlier answer is missing, or once the form has been sent. When only one option is off limits, disable that item and leave the rest working.

Preview width

Disabled item

Of four plans, Enterprise is dimmed: its entry in the collection has disabled set to true, so clicks do nothing and the arrow keys skip over it. Use this for a sold-out tier, a plan the account can't buy or a step whose prerequisite isn't met, where people should still see the option exists.

Preview width

Grid

Twenty emoji sit in five columns, built with createGridCollection and columnCount set to 5. Up and Down move a whole row in the same column, while Left and Right step through the items and carry on into the next row at the edge. Use a grid for emoji, color swatches or icons, where a single column would stretch short choices into a tall list.

Preview width

Grouping

Brazil and Colombia sit under South America, Mexico and Canada under North America. The collection's groupBy puts each country in its region, and each group's label names the group for a screen reader without being an option itself. Group a long list when people already know its categories. A list as short as this one reads fine without headings.

Preview width

Horizontal

Four albums sit in a row, each a square cover placeholder with the title and artist below and a check in the corner once chosen. ListboxLabel shows Favorite album above the row and names the list for a screen reader. With orientation set to horizontal, Left and Right move along the row, which scrolls when it overflows. Use it for covers, swatches or cards.

Preview width

Image explorer

A narrow list of five places, with Mountain Landscape picked, drives a panel beside it that shows the chosen name and a one-line caption. The panel reads the selection from state, and on a phone the list moves above it. Use this layout when each option stands for something larger than its label, such as a photo or a document, and put the real preview in the panel.

Preview width

Selection extended

With selectionMode set to extended, a plain click replaces the selection, Ctrl or Cmd with a click adds or removes one country, and Shift with a click selects the run between two. The note above the list tells people about the modifier. Choose it for file-manager lists where people expect a click to start over.

Preview width

Selection multiple

Every click toggles its country and leaves the others alone, with selectionMode set to multiple. There is no modifier key to learn, which suits filters, tags and any set people treat like checkboxes. When a plain click should collapse the selection back to one item, extended mode is the better fit.

Preview width

Selection none

New file, Edit file and Delete file, each with an icon, a short description and a shortcut label, under Actions and Danger zone headings. With selectionMode set to none no row stays checked, and the listbox reports no selection at all. The rows here only show the layout, so for commands people run by mouse and keyboard, use a Dropdown Menu.

Preview width

Transfer list

Two lists in multiple mode, Available with Brazil and Ireland and Selected with Mexico, sit on either side of a pair of arrow buttons. A button stays disabled until its side has a selection, then moves those countries across. Use a transfer list when the task is deciding membership, such as who belongs to a team. For a short set, one multiple-selection list is less to build.

Preview width

With description

Each country has a second line in small muted text, its region and language, inside the same row as the check mark. Add a line like this when the label alone leaves people guessing, and keep it to one short sentence so the row stays a row. A screen reader reads it as part of the option's name.

Preview width

With filter

A search field above the countries narrows the list as someone types, ignoring case and accents. When nothing matches, ListboxEmpty puts a no-results line where the rows were. The narrowing comes from useListCollection with the contains matcher of useFilter. Add a filter once scanning takes longer than typing a few letters.

Preview width

With icon

A flag sits before each country name and the check stays at the end of the row. The flag is plain content inside ListboxItem, so any icon or image goes the same way. Use one when the mark helps people find the row faster than the word does, and skip it when it only repeats the label.

Preview width

With popover

A button shows the chosen country, or a placeholder before there is one, and opens a popover with a search field and the list. Picking a new country closes it through the select callback, and clicking the current one closes it too. Use this when the list shouldn't take up room until it's needed. If typing is how people choose, a Combobox ties the field and the list together.

Preview width

FAQ

How do I control the selected Listbox items in Svelte?

Keep the selection in state, pass it as value on Listbox and update it in onValueChange. The callback receives an object, and the new array is details.value.

The value is an array of item values in every selection mode, so a single-select list holds one entry or none. The Controlled demo reads that array to decide what the line under the list says. When nothing outside the list needs the pick, set defaultValue and leave the state out.

How do I let people select more than one item in a Listbox?

Set selectionMode on Listbox to multiple or extended. In multiple every click toggles one item, as the Selection multiple demo shows. In extended a plain click replaces the selection and Ctrl or Cmd adds to it, as in Selection extended.

Both modes let Shift with a click or an arrow key select a run of items, and Ctrl+A select everything unless disallowSelectAll is set. The default is single, and none turns selection off.

How do I disable one option in a Listbox?

Set disabled to true on that item's object in the collection. ListboxItem has no prop for it, because the listbox reads the state from the collection when it decides what the arrow keys skip and what a click may select.

A disabled row stays visible with data-disabled and aria-disabled on it, and the component dims it. The Disabled item demo does this for an Enterprise plan. To lock every row at once, set disabled on Listbox instead.

How do I add a search field to filter a Listbox?

Build the collection with useListCollection, pass it the contains matcher from useFilter, and call its filter function with the field's text on every change. The collection's items shrink to the matches, and ListboxEmpty shows its text when none are left, as in the With filter demo.

The field is a separate input, so its arrow keys don't move through the options: people press Tab to reach the list. When the field and the list should work as one control, use a Combobox.

Should I use a Listbox, a Select or a Combobox?

Use a listbox when the options should stay on screen: people compare them, pick several, or come back to change the pick often, as in a filter panel or a plan chooser. It costs the space of every row.

A Select folds the same choice into a button and fits a form with many fields. A Combobox adds a text field and suits long lists where people know what they're looking for and type it.

How do I submit the selected Listbox values with a form?

Nothing submits on its own, because the listbox renders no input and takes no name. Control the value, then render a hidden input for each selected value inside the form, named the way your server expects, and the browser sends them with the rest of the fields.

With a form library, register the field yourself and pass the selection array to its setter from the change callback. Validation works on that array, so a required list checks that it isn't empty.

How do I make a long Listbox scroll?

Give ListboxContent a maximum height and an overflow class, such as max-h-64 and overflow-auto. The component ships the content with overflow-hidden, so rows past the height are cut off until you change it.

Once it scrolls, the arrow keys keep the highlighted row in view on their own. For thousands of rows, render only the visible ones with a virtualizer and pass scrollToIndexFn to Listbox, which the listbox calls to bring a highlighted row into view.

How do I style the selected and highlighted Listbox items?

Target the data attributes the items carry. A selected item has data-state="checked" and data-selected, the row the arrow keys are on has data-highlighted, and a disabled one has data-disabled. The highlight appears only while someone uses the keyboard, and mouse users get the hover style.

The default look lives in listboxItemVariants in components/ui/listbox, which is your file once installed. Set variant to destructive for a red row, and put your own icon inside ListboxItemIndicator to replace the check.

API Reference

Listbox

<div> data-slot="listbox"

PropType
value?string[]

The controlled keys of the selected items.

defaultValue?string[]

The initial default value of the listbox when rendered. Use when you don't need to control the value of the listbox.

Default
[]
onValueChange?(details: ValueChangeDetails<T>) → void

The callback fired when the selected item changes.

Type
(details: ValueChangeDetails<T>) → void
Parameters

details – ValueChangeDetails<T>

Returns
void
collectionListCollection<T>

The collection of items.

Type
ListCollection<T>
defaultHighlightedValue?string | null

The initial value of the highlighted item when opened. Use when you don't need to control the highlighted value of the listbox.

Type
string | null
deselectable?boolean

Whether to disallow empty selection.

dir?"ltr" | "rtl"

The document's text/writing direction.

Type
"ltr" | "rtl"
Default
"ltr"
disabled?boolean

Whether the listbox is disabled.

disallowSelectAll?boolean

Whether to disallow selecting all items when meta+a is pressed.

highlightedValue?string | null

The controlled key of the highlighted item.

Type
string | null
id?string

The unique identifier of the machine.

ids?object

The ids of the elements in the listbox. Useful for composition.

Type
{ root?: string; content?: string; label?: string; item?: (id: string | number) → string; itemGroup?: (id: string | number) → string; itemGroupLabel?: (id: string | number) → string }
loopFocus?boolean

Whether to loop the keyboard navigation through the options.

Default
false
onHighlightChange?(details: HighlightChangeDetails<T>) → void

The callback fired when the highlighted item changes.

Type
(details: HighlightChangeDetails<T>) → void
Parameters

details – HighlightChangeDetails<T>

Returns
void
onSelect?(details: SelectionDetails) → void

Function called when an item is selected.

Type
(details: SelectionDetails) → void
Parameters

details – { value: string }

Returns
void
orientation?"horizontal" | "vertical"

The orientation of the listbox.

Type
"horizontal" | "vertical"
Default
"vertical"
scrollToIndexFn?(details: ScrollToIndexDetails) → void

Function to scroll to a specific index.

Type
(details: ScrollToIndexDetails) → void
Parameters

details – { index: number; immediate?: boolean; getElement: () → HTMLElement | null }

Returns
void
selectionMode?"single" | "multiple" | "none" | "extended"

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.

Type
"single" | "multiple" | "none" | "extended"
Default
"single"
selectOnHighlight?boolean

Whether to select the item when it is highlighted.

typeahead?boolean

Whether to enable typeahead on the listbox.

asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-orientation"horizontal" | "vertical"

The orientation of the listbox.

Type
"horizontal" | "vertical"

ListboxLabel

<span> data-slot="listbox-label"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

ListboxItem

<div> data-slot="listbox-item"

PropType
itemT

The item to render.

highlightOnHover?boolean

Whether to highlight the item on hover.

variant?"default" | "destructive"

The visual style.

Type
"default" | "destructive"
Default
"default"
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-highlighted?string

Present when highlighted.

data-layoutstring

Mirrors layout.

data-orientation"horizontal" | "vertical"

The orientation of the item.

Type
"horizontal" | "vertical"
data-selected?string

Present when selected.

data-state"checked" | "unchecked"

"checked" or "unchecked".

Type
"checked" | "unchecked"
data-valueboolean

The value of the item.

data-variant?string

Mirrors variant.

ListboxItemText

<span> data-slot="listbox-item-text"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-highlighted?string

Present when highlighted.

data-state"checked" | "unchecked"

"checked" or "unchecked".

Type
"checked" | "unchecked"

ListboxItemIndicator

<div> data-slot="listbox-item-indicator"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-state"checked" | "unchecked"

"checked" or "unchecked".

Type
"checked" | "unchecked"

ListboxItemGroup

<div> data-slot="listbox-item-group"

PropType
heading?string

The heading of the listbox item group.

asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-empty?string

Present when the content is empty.

data-orientation"horizontal" | "vertical"

The orientation of the item.

Type
"horizontal" | "vertical"

ListboxItemGroupLabel

<div> data-slot="listbox-item-group-label"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

ListboxContent

<div>

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-activedescendantstring

Mirrors ariaActiveDescendant.

data-empty?string

Present when the content is empty.

data-layoutstring

Mirrors layout.

data-orientation"horizontal" | "vertical"

The orientation of the content.

Type
"horizontal" | "vertical"
CSS variableType
--column-countstring

ListboxValueText

<span> data-slot="listbox-value-text"

PropType
placeholder?string

Text to display when no value is listboxed.

asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

ListboxEmpty

<div> data-slot="listbox-empty"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

ListboxShortcut

data-slot="listbox-shortcut"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
AttributeType
data-disabled?string

Present when disabled is true.

data-orientation"horizontal" | "vertical"

Mirrors the orientation prop.

Type
"horizontal" | "vertical"

useListbox

PropType
value?string[]

The controlled keys of the selected items.

defaultValue?string[]

The initial default value of the listbox when rendered. Use when you don't need to control the value of the listbox.

Default
[]
onValueChange?(details: ValueChangeDetails<T>) → void

The callback fired when the selected item changes.

Type
(details: ValueChangeDetails<T>) → void
Parameters

details – ValueChangeDetails<T>

Returns
void
collectionListCollection<T>

The collection of items.

Type
ListCollection<T>
defaultHighlightedValue?string | null

The initial value of the highlighted item when opened. Use when you don't need to control the highlighted value of the listbox.

Type
string | null
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 | null

The controlled key of the highlighted item.

Type
string | null
id?string

The unique identifier of the machine.

ids?object

The ids of the elements in the listbox. Useful for composition.

Type
{ root?: string; content?: string; label?: string; item?: (id: string | number) → string; itemGroup?: (id: string | number) → string; itemGroupLabel?: (id: string | number) → string }
loopFocus?boolean

Whether to loop the keyboard navigation through the options.

Default
false
onHighlightChange?(details: HighlightChangeDetails<T>) → void

The callback fired when the highlighted item changes.

Type
(details: HighlightChangeDetails<T>) → void
Parameters

details – HighlightChangeDetails<T>

Returns
void
onSelect?(details: SelectionDetails) → void

Function called when an item is selected.

Type
(details: SelectionDetails) → void
Parameters

details – { value: string }

Returns
void
orientation?"horizontal" | "vertical"

The orientation of the listbox.

Type
"horizontal" | "vertical"
Default
"vertical"
scrollToIndexFn?(details: ScrollToIndexDetails) → void

Function to scroll to a specific index.

Type
(details: ScrollToIndexDetails) → void
Parameters

details – { index: number; immediate?: boolean; getElement: () → HTMLElement | null }

Returns
void
selectionMode?"single" | "multiple" | "none" | "extended"

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.

Type
"single" | "multiple" | "none" | "extended"
Default
"single"
selectOnHighlight?boolean

Whether to select the item when it is highlighted.

typeahead?boolean

Whether to enable typeahead on the listbox.