Selection & Dates
@hacline/ui/listboxListbox
Select one or multiple options from an always-visible list.
Usage
Click or use the arrow keys and Space to select notification channels. This example uses multiple, so the currently selected values are stored in an array.
"use client";
import { useState } from "react";
import {
Listbox,
ListboxItem,
ListboxItemIndicator,
} from "@/components/ui/hacline/components/listbox";
const channels = [
{ label: "Email digest", value: "email" },
{ label: "In-app notifications", value: "inbox" },
{ label: "Desktop push", value: "desktop" },
];
export const Example = () => {
const [value, setValue] = useState(["email"]);
return (
<div className="grid w-full max-w-xs gap-2">
<Listbox
aria-label="Notification channels"
multiple
onValueChange={setValue}
value={value}
>
{channels.map((channel) => (
<ListboxItem key={channel.value} value={channel.value}>
<span className="flex-1">{channel.label}</span>
<ListboxItemIndicator />
</ListboxItem>
))}
</Listbox>
<output className="text-hl-fg-muted text-xs">
{value.length} {value.length === 1 ? "channel" : "channels"} selected
</output>
</div>
);
};Rendering Grouped Data Options
Pass grouped objects into items. The listbox generates keyboard-navigable structure using the group labels and option labels.
"use client";
import { Listbox } from "@/components/ui/hacline/components/listbox";
import type { ListData } from "@/components/ui/hacline/components/listbox";
const categories: ListData<string> = [
{
items: [
{ label: "Code review", value: "review" },
{ label: "Build artifacts", value: "build" },
],
label: "Engineering",
type: "group",
},
{
items: [{ label: "Getting started", value: "guide" }],
label: "Documentation",
type: "group",
},
];
export const Example = () => (
<Listbox
aria-label="Project categories"
className="max-w-xs"
defaultValue="review"
items={categories}
/>
);Parts
| Part | Description |
|---|---|
ListboxGroup | A group of items, named by its ListboxGroupLabel. |
ListboxGroupLabel | The group's heading, which names the group. |
ListboxItem | An option. Pressing it selects it, or toggles it with multiple. |
ListboxItemIcon | An icon inside an item. Screen readers and typeahead skip it, and mirror flips it in right-to-left text. |
ListboxItemIndicator | The check inside a selected item. Rendered while it's selected, or always with keepMounted. |
ListboxItems | Renders data items. Used by data listboxes, and available for custom layouts. |
ListboxSeparator | A line between items or groups, hidden from screen readers. |
Props
Prop
Type