Components
List Box
A list on the page to pick from: one item, several, or none, chosen with the mouse, a finger or the keyboard.
Introduction
A list box is a list of options that is always on the page, so that they can all be seen at once. Use it for the languages of an app, the members of a team, the files of a folder, or a menu of pages. For a list that opens from a button use a Select, and for one to search in, an Autocomplete.
- Select one item, several, or none, as a list of actions or links. Selection can be controlled, or left to the list.
- Items with an icon or an avatar, a description, something at the end, sections, and disabled and danger items.
- A check mark, a checkbox, or only a background for the selected ones.
- A stack, a row, or a grid, with the arrow keys that go with it, and a short message when it is empty.
- Built on react-aria's ListBox: keyboard, typeahead, and the screen reader announcements come from there. Give each item an
id, which is what the selection holds.
Installation
centerUI has no CLI or registry yet: components are copied into your project.
# bun
bun add react-aria-components tailwind-variants tailwind-merge clsx
# pnpm
pnpm add react-aria-components tailwind-variants tailwind-merge clsx
# npm
npm install react-aria-components tailwind-variants tailwind-merge clsxManual installation
List Box uses the Avatar for the avatars of its items. Copy these files into your project and keep the paths, or adjust the imports.
1. Add the styles
src/types/list-box-styles.tsShow code
import { tv } from "tailwind-variants";
// Styles for ListBox. The item reads its state (focused, selected, disabled...)
// from data attributes that react-aria sets on it.
export const listBoxStyles = tv({
slots: {
root: "group/list-box flex w-full flex-col gap-1.5",
label: "text-sm font-medium",
description: "m-0 text-sm text-muted-fg",
list: "flex outline-none data-focus-visible:ring-2 data-focus-visible:ring-ring data-focus-visible:ring-offset-2 data-focus-visible:ring-offset-bg",
empty: "px-3 py-8 text-center text-sm text-muted-fg",
section: "not-first:mt-1 not-first:border-t not-first:pt-1",
heading: "px-2.5 py-1.5 text-xs font-medium text-muted-fg",
item: [
"relative flex cursor-default items-center gap-2.5 rounded-lg text-sm outline-none select-none",
"data-hovered:bg-muted/60 data-focused:bg-muted data-pressed:bg-muted/80",
"data-selected:bg-muted data-selected:font-medium",
"data-focus-visible:ring-2 data-focus-visible:ring-ring data-focus-visible:ring-inset",
"data-disabled:cursor-not-allowed data-disabled:opacity-50",
"data-[variant=danger]:text-danger data-[variant=danger]:data-focused:bg-danger/10",
"[&[href]]:cursor-pointer",
],
check: "size-4 shrink-0",
// The box before an item that can be checked, when `indicator` is `checkbox`.
checkbox:
"flex size-4 shrink-0 items-center justify-center rounded border bg-bg text-primary-fg transition-colors group-data-selected/item:border-primary group-data-selected/item:bg-primary [&_svg]:size-3",
icon: "shrink-0 text-muted-fg [&_svg]:size-4",
text: "flex min-w-0 flex-1 flex-col",
itemLabel: "truncate",
itemDescription: "truncate text-xs font-normal text-muted-fg",
suffix: "ml-auto shrink-0 text-xs font-normal text-muted-fg",
},
variants: {
variant: {
outline: { list: "rounded-xl border bg-card p-1 text-card-fg" },
plain: { list: "" },
},
orientation: {
vertical: { list: "flex-col gap-0.5" },
horizontal: { list: "flex-row gap-1 overflow-x-auto" },
},
size: {
sm: { item: "px-2 py-1" },
md: { item: "px-2.5 py-1.5" },
lg: { item: "px-3 py-2.5" },
},
},
defaultVariants: { variant: "outline", orientation: "vertical", size: "md" },
});2. Add the List Box file
src/components/ui/list-box/list-box.tsxShow code
"use client";
import { createContext, type ReactNode, useContext, useId } from "react";
import {
ListBox as AriaListBox,
ListBoxItem as AriaListBoxItem,
type ListBoxItemProps as AriaListBoxItemProps,
type ListBoxProps as AriaListBoxProps,
ListBoxSection as AriaListBoxSection,
Header,
Text,
} from "react-aria-components";
import { CheckIcon } from "@/components/icons";
import { Avatar, type AvatarProps } from "@/components/ui/avatar/avatar";
import { cn } from "@/lib/cn";
import { cx } from "@/lib/primitive";
import { listBoxStyles } from "@/types/list-box-styles";
type Indicator = "check" | "checkbox" | "none";
const IndicatorContext = createContext<Indicator>("check");
const styles = listBoxStyles();
export interface ListBoxProps<T extends object>
extends Omit<
AriaListBoxProps<T>,
"className" | "style" | "aria-label" | "aria-labelledby" | "orientation"
> {
/** A label above the list, which also names it for screen readers. Without one, name the list with `aria-label`. */
label?: ReactNode;
/** A line that says more, under the label. */
description?: ReactNode;
/** Names the list for screen readers when there is no `label`. */
"aria-label"?: string;
/** A box with a border around the items, or the items alone. Defaults to `outline`. */
variant?: "outline" | "plain";
/** How much room each item has. Defaults to `md`. */
size?: "sm" | "md" | "lg";
/** Stack the items, or lay them out in a row. Defaults to `vertical`. */
orientation?: "vertical" | "horizontal";
/**
* How a selected item is shown: a check mark after it, a checkbox before it, or
* only its background. Defaults to `check`.
*/
indicator?: Indicator;
/**
* The number of columns, for a list laid out as a grid. Set it together with
* `layout="grid"`, so that the arrow keys move up, down, left and right.
*/
columns?: number;
/** What it says when there are no items. Defaults to "Nothing here." */
emptyMessage?: ReactNode;
className?: string;
}
/**
* A list of items that is always on the page, and that the user picks from: one
* item, several, or none (a list of links or actions). Use it for a list of
* options that should be seen at once, such as the languages of an app, the
* members of a team or the files of a folder. For a list that opens from a
* button, use a Select, and for one to search in, an Autocomplete.
*
* Built on react-aria's ListBox: the arrow keys move, typing a letter jumps to an
* item, Space chooses one, and with several chosen Shift and Ctrl or Command
* extend the choice, as in a list of files. A screen reader hears the label, and
* that each item is selected or not.
*
* Give each `ListBoxItem` an `id`: it is what `selectedKeys` holds. Items can be
* given as `items` with a function, for a long list.
*
* @example
* <ListBox label="Fruit" selectionMode="single" defaultSelectedKeys={["apple"]}>
* <ListBoxItem id="apple">Apple</ListBoxItem>
* <ListBoxItem id="pear">Pear</ListBoxItem>
* </ListBox>
*/
export function ListBox<T extends object>({
label,
description,
"aria-label": ariaLabel,
variant,
size,
orientation = "vertical",
indicator = "check",
columns,
emptyMessage = "Nothing here.",
className,
children,
layout,
...props
}: ListBoxProps<T>) {
const labelId = useId();
const descriptionId = useId();
const classes = listBoxStyles({ variant, orientation, size });
const isGrid = layout === "grid";
return (
<div className={cn(classes.root(), className)}>
{label ? (
<span id={labelId} className={classes.label()}>
{label}
</span>
) : null}
{description ? (
<p id={descriptionId} className={classes.description()}>
{description}
</p>
) : null}
<IndicatorContext.Provider value={indicator}>
<AriaListBox<T>
{...props}
layout={layout}
orientation={orientation}
aria-labelledby={label ? labelId : undefined}
aria-label={label ? undefined : (ariaLabel ?? "List")}
aria-describedby={description ? descriptionId : undefined}
renderEmptyState={() => (
<p className={classes.empty()}>{emptyMessage}</p>
)}
className={cn(classes.list(), isGrid && "grid gap-1")}
style={
isGrid && columns
? { gridTemplateColumns: `repeat(${columns}, minmax(0, 1fr))` }
: undefined
}
>
{children}
</AriaListBox>
</IndicatorContext.Provider>
</div>
);
}
export interface ListBoxItemProps extends AriaListBoxItemProps {
/** A second, smaller line under the label. */
description?: ReactNode;
/** An icon before the label. */
icon?: ReactNode;
/** An avatar before the label. */
avatar?: AvatarProps;
/** Something at the end of the row: a badge, a count, a shortcut. */
suffix?: ReactNode;
/** Red, for an item that destroys something. */
variant?: "default" | "danger";
/** How much room the item has. Defaults to the size of the list. */
size?: "sm" | "md" | "lg";
}
/**
* One item. `id` is the key that `selectedKeys` and `onSelectionChange` use. Give
* it a `textValue` when its children are not plain text, so that typing a letter
* has something to match. With `href` it is a link, and with `onAction` it runs a
* function when it is chosen, whether or not the list can select.
*/
export function ListBoxItem({
description,
icon,
avatar,
suffix,
variant = "default",
size = "md",
className,
children,
textValue,
...props
}: ListBoxItemProps) {
const indicator = useContext(IndicatorContext);
const classes = listBoxStyles({ size });
return (
<AriaListBoxItem
{...props}
textValue={
textValue ?? (typeof children === "string" ? children : undefined)
}
data-variant={variant}
className={cx(classes.item(), "group/item", className)}
>
{(renderProps) => (
<>
{indicator === "checkbox" && renderProps.selectionMode !== "none" ? (
<span aria-hidden="true" className={classes.checkbox()}>
{renderProps.isSelected ? <CheckIcon /> : null}
</span>
) : null}
{avatar ? (
<Avatar {...avatar} />
) : icon ? (
<span aria-hidden="true" className={classes.icon()}>
{icon}
</span>
) : null}
<span className={classes.text()}>
<Text slot="label" className={classes.itemLabel()}>
{typeof children === "function"
? children(renderProps)
: children}
</Text>
{description ? (
<Text slot="description" className={classes.itemDescription()}>
{description}
</Text>
) : null}
</span>
{suffix ? <span className={classes.suffix()}>{suffix}</span> : null}
{indicator === "check" && renderProps.isSelected ? (
<CheckIcon aria-hidden="true" className={classes.check()} />
) : null}
</>
)}
</AriaListBoxItem>
);
}
export interface ListBoxSectionProps {
/** The title of the group. */
title?: ReactNode;
children: ReactNode;
/** Names the group for screen readers when it has no `title`. */
"aria-label"?: string;
}
/** A titled group of items, separated from the group above it. */
export function ListBoxSection({
title,
children,
"aria-label": ariaLabel,
}: ListBoxSectionProps) {
return (
<AriaListBoxSection
aria-label={title ? undefined : ariaLabel}
className={styles.section()}
>
{title ? <Header className={styles.heading()}>{title}</Header> : null}
{children}
</AriaListBoxSection>
);
}3. Use it
import {
ListBox,
ListBoxItem,
ListBoxSection,
} from "@/components/ui/list-box/list-box";Anatomy
A list, its items, and if you like, groups.
<ListBox label="Language" description="Used for the whole app." selectionMode="single">
<ListBoxSection title="Popular">
<ListBoxItem id="en" icon={<StarIcon />} description="United States" suffix="en">
English
</ListBoxItem>
</ListBoxSection>
</ListBox>| Part | Rendered as | Notes |
|---|---|---|
| Label and description | span, p | Above the list. The label also names the list for a screen reader. |
| ListBox | listbox | The box with the items. It has the focus, and the arrow keys move through it. |
| ListBoxSection | group | A titled group of items, with a line between it and the one above. |
| ListBoxItem | option | One item: an icon or avatar, a label, a description, something at the end, and a mark if it is selected. |
Keyboard
| Key | What it does |
|---|---|
| Tab | Moves into the list, to the selected item or the first, and out of it. |
| ↓ ↑ | Moves to the next or the previous item. With a row or a grid: ← → too. |
| A letter | Jumps to the item that starts with what is typed. |
| Space, Enter | Selects the item, or runs its action. |
| Shift + ↓ ↑ | In a list of several, extends the selection. |
| Ctrl or ⌘ + A | In a list of several, selects all. |
| Escape | Clears the selection, in a list of several. |
| Home, End | Goes to the first or the last item. |
Examples
Usage
Each item needs an id.
One item
selectionMode single. disallowEmptySelection keeps one item chosen: a press on it again does nothing.
Selected: en
Several items
selectionMode multiple. Each press adds an item or takes it away.
Selected: id, en
Indicators
How a selected item shows it.
checkcheckboxnoneRich items
An avatar or an icon, a second line, and something at the end of the row.
Sections
Disabled and danger
Actions and links
With no selectionMode a list is a list of actions: a press runs onAction. An item with href is a link.
Last action: nothing yet
Grid
layout grid with the number of columns. The arrow keys move up, down, left and right.
Horizontal
A row of items, with ← and → to move.
Selection behavior
toggle (the default) is for a list of options, where each press adds or takes away. replace is for a list of things, like files: a press selects only that item, and Ctrl or Command and Shift add and extend.
Dynamic items
Give items and a function for a list that changes. It shows emptyMessage when there is nothing in it.
Sizes
smmdlgVariants
outlineplainComponent API
ListBox takes these props, and each ListBoxItem the second table. ListBoxSection takes a title.
ListBox
| Prop | Type | Default |
|---|---|---|
| label | ReactNode | - |
| description | ReactNode | - |
| aria-label | string | "List" |
| selectionMode | "none" | "single" | "multiple" | "none" |
| selectedKeys | "all" | Iterable<Key> | - |
| defaultSelectedKeys | "all" | Iterable<Key> | - |
| onSelectionChange | (keys: Selection) => void | - |
| disallowEmptySelection | boolean | false |
| selectionBehavior | "toggle" | "replace" | "toggle" |
| disabledKeys | Iterable<Key> | - |
| items | Iterable<T> | - |
| variant | "outline" | "plain" | "outline" |
| size | "sm" | "md" | "lg" | "md" |
| orientation | "vertical" | "horizontal" | "vertical" |
| layout | "stack" | "grid" | "stack" |
| columns | number | - |
| indicator | "check" | "checkbox" | "none" | "check" |
| emptyMessage | ReactNode | "Nothing here." |
| onAction | (key: Key) => void | - |
| autoFocus | boolean | "first" | "last" | false |
| shouldFocusWrap | boolean | false |
| className | string | - |
ListBoxItem
| Prop | Type | Default |
|---|---|---|
| id | Key | - |
| children | ReactNode | - |
| textValue | string | the label |
| description | ReactNode | - |
| icon | ReactNode | - |
| avatar | AvatarProps | - |
| suffix | ReactNode | - |
| variant | "default" | "danger" | "default" |
| size | "sm" | "md" | "lg" | md |
| href | string | - |
| onAction | () => void | - |
| isDisabled | boolean | false |