Listbox
キーボードナビゲーションとタイプアヘッドによる単一選択および複数選択に対応したオプションのリストです。
Listboxは、ルートコンテナ上のフォーカスを管理するためにaria-activedescendantを使用し、Listbox WAI-ARIAデザインパターンに準拠しています。
Selected: none
import { component$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Listbox } from '@leve-ui/listbox';
import { LuCheck } from '@qwikest/icons/lucide';
import styles from './default.css?inline';
const FRUITS = [
{ value: 'apple', label: 'Apple' },
{ value: 'apricot', label: 'Apricot' },
{ value: 'banana', label: 'Banana' },
{ value: 'blueberry', label: 'Blueberry', disabled: true },
{ value: 'blackberry', label: 'Blackberry' },
{ value: 'cherry', label: 'Cherry' },
{ value: 'grape', label: 'Grape' },
{ value: 'peach', label: 'Peach' },
];
export default component$(() => {
useStyles$(styles);
const value = useSignal<string[]>([]);
return (
<div class="listbox-default">
<span id="listbox-default-label" class="listbox-default-label">
Fruit
</span>
<Listbox.Root
aria-labelledby="listbox-default-label"
bind:value={value}
class="listbox-default-list"
>
{FRUITS.map((fruit) => (
<Listbox.Item
key={fruit.value}
value={fruit.value}
disabled={fruit.disabled}
class="listbox-default-item"
>
<Listbox.ItemText>{fruit.label}</Listbox.ItemText>
<Listbox.ItemIndicator class="listbox-default-indicator">
<LuCheck aria-hidden="true" class="listbox-default-icon" />
</Listbox.ItemIndicator>
</Listbox.Item>
))}
</Listbox.Root>
<p class="listbox-default-status">Selected: {value.value[0] ?? 'none'}</p>
</div>
);
});.listbox-default {
display: flex;
flex-direction: column;
gap: 8px;
width: 240px;
font-family: inherit;
}
.listbox-default-label {
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
}
.listbox-default-list {
display: flex;
flex-direction: column;
gap: 2px;
max-height: 200px;
overflow-y: auto;
padding: 4px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
}
.listbox-default-list:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.listbox-default-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 6px 8px;
border-radius: 4px;
font-size: 14px;
cursor: default;
}
.listbox-default-item[data-state="checked"] {
color: light-dark(#006ce9, #4cb8ff);
font-weight: 500;
}
.listbox-default-item[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
@media (hover: hover) {
.listbox-default-item:hover:not([data-disabled]) {
background: light-dark(#f1f5f9, #ffffff14);
}
}
/* Active descendant highlight while navigating with the keyboard */
.listbox-default-list:focus-visible .listbox-default-item[data-highlighted] {
background: light-dark(#f1f5f9, #ffffff14);
}
.listbox-default-indicator {
display: inline-flex;
align-items: center;
}
.listbox-default-indicator[hidden] {
display: none;
}
.listbox-default-icon {
width: 16px;
height: 16px;
}
.listbox-default-status {
margin: 0;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}インストール#
個別のパッケージをインストールするか、@leve-ui/coreを使用してすべてのコンポーネントをまとめて取得します。
pnpm add @leve-ui/listboxnpm install @leve-ui/listboxyarn add @leve-ui/listboxbun add @leve-ui/listboxpnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/core構造#
import { Listbox } from '@leve-ui/listbox'; // or '@leve-ui/core'
<Listbox.Root aria-label="Options">
<Listbox.Item value="one">
<Listbox.ItemText>Option 1</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
<Listbox.Group>
<Listbox.GroupLabel>Group</Listbox.GroupLabel>
<Listbox.Item value="two">
<Listbox.ItemText>Option 2</Listbox.ItemText>
<Listbox.ItemIndicator />
</Listbox.Item>
</Listbox.Group>
</Listbox.Root>
| パーツ | 要素 | 説明 |
|---|---|---|
Listbox.Root | <div> | リストボックス(role="listbox")です。選択された値とアクティブな子孫のハイライトを保持します。 |
Listbox.Item | <div> | オプション(role="option")です。 |
Listbox.ItemText | <span> | オプションのテキストラベルです。textValueが設定されていない場合、タイプアヘッドに使用されます。 |
Listbox.ItemIndicator | <span> | オプションが選択されているときに表示され、それ以外のときは非表示になる視覚的なチェックマークです。 |
Listbox.Group | <div> | GroupLabelによって名前が付けられるオプションのグループ(role="group")です。 |
Listbox.GroupLabel | <div> | グループの見出しです。キーボードナビゲーションではスキップされます。 |
選択#
値は単一選択モードでも常に選択されたオプションの値の配列(空または1つの値)になります。非制御で使用する場合はdefaultValueを、制御で使用する場合はbind:valueを使用し、変更の監視にはonValueChange$を使用します。
Listbox.Rootにmultipleを設定すると、複数のオプションを選択できるようになり、Space / Enterまたはクリックで選択を切り替え、Shift + 矢印キー / Home / EndまたはShift + クリックで範囲を拡張し、Ctrl+A / Cmd+Aで有効なすべてのオプションを選択できます。
使用例#
複数選択#
multipleを設定すると、aria-multiselectable="true"、範囲選択、およびCtrl+A / Cmd+Aによる複数選択が有効になります。
Selected: none
import { component$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Listbox } from '@leve-ui/listbox';
import { LuCheck } from '@qwikest/icons/lucide';
import styles from './multiple.css?inline';
const TOPPINGS = [
{ value: 'avocado', label: 'Avocado' },
{ value: 'bacon', label: 'Bacon' },
{ value: 'cheese', label: 'Cheese' },
{ value: 'lettuce', label: 'Lettuce' },
{ value: 'onion', label: 'Onion' },
{ value: 'truffle', label: 'Truffle', disabled: true },
{ value: 'tomato', label: 'Tomato' },
];
export default component$(() => {
useStyles$(styles);
const value = useSignal<string[]>([]);
return (
<div class="listbox-multiple">
<span id="listbox-multiple-label" class="listbox-multiple-label">
Toppings
</span>
<Listbox.Root
multiple
aria-labelledby="listbox-multiple-label"
bind:value={value}
class="listbox-multiple-list"
>
{TOPPINGS.map((topping) => (
<Listbox.Item
key={topping.value}
value={topping.value}
disabled={topping.disabled}
class="listbox-multiple-item"
>
<Listbox.ItemText>{topping.label}</Listbox.ItemText>
<Listbox.ItemIndicator class="listbox-multiple-indicator">
<LuCheck aria-hidden="true" class="listbox-multiple-icon" />
</Listbox.ItemIndicator>
</Listbox.Item>
))}
</Listbox.Root>
<p class="listbox-multiple-status">
Selected: {value.value.length > 0 ? value.value.join(', ') : 'none'}
</p>
</div>
);
});.listbox-multiple {
display: flex;
flex-direction: column;
gap: 8px;
width: 240px;
font-family: inherit;
}
.listbox-multiple-label {
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
}
.listbox-multiple-list {
display: flex;
flex-direction: column;
gap: 2px;
padding: 4px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
}
.listbox-multiple-list:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.listbox-multiple-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 6px 8px;
border-radius: 4px;
font-size: 14px;
cursor: default;
}
.listbox-multiple-item[data-state="checked"] {
color: light-dark(#006ce9, #4cb8ff);
font-weight: 500;
}
.listbox-multiple-item[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
@media (hover: hover) {
.listbox-multiple-item:hover:not([data-disabled]) {
background: light-dark(#f1f5f9, #ffffff14);
}
}
/* Active descendant highlight while navigating with the keyboard */
.listbox-multiple-list:focus-visible .listbox-multiple-item[data-highlighted] {
background: light-dark(#f1f5f9, #ffffff14);
}
.listbox-multiple-indicator {
display: inline-flex;
align-items: center;
}
.listbox-multiple-indicator[hidden] {
display: none;
}
.listbox-multiple-icon {
width: 16px;
height: 16px;
}
.listbox-multiple-status {
margin: 0;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}グループとループ#
Listbox.GroupとListbox.GroupLabelは、オプションをラベル付きのグループに整理します。loopを指定すると、矢印キーによる移動が最初と最後の有効なオプション間でループします。
Selected: none
import { component$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Listbox } from '@leve-ui/listbox';
import { LuCheck } from '@qwikest/icons/lucide';
import styles from './grouped.css?inline';
const TIMEZONES = [
{ region: 'Americas', zones: ['New York', 'Chicago', 'Los Angeles'] },
{ region: 'Europe', zones: ['London', 'Paris', 'Berlin'] },
{ region: 'Asia', zones: ['Tokyo', 'Seoul', 'Singapore'] },
];
export default component$(() => {
useStyles$(styles);
const value = useSignal<string[]>([]);
return (
<div class="listbox-grouped">
<span id="listbox-grouped-label" class="listbox-grouped-label">
Time zone
</span>
{/* loop wraps arrow navigation from the last option back to the first */}
<Listbox.Root
loop
aria-labelledby="listbox-grouped-label"
bind:value={value}
class="listbox-grouped-list"
>
{TIMEZONES.map((group) => (
<Listbox.Group key={group.region} class="listbox-grouped-group">
<Listbox.GroupLabel class="listbox-grouped-group-label">
{group.region}
</Listbox.GroupLabel>
{group.zones.map((zone) => (
<Listbox.Item key={zone} value={zone} class="listbox-grouped-item">
<Listbox.ItemText>{zone}</Listbox.ItemText>
<Listbox.ItemIndicator class="listbox-grouped-indicator">
<LuCheck aria-hidden="true" class="listbox-grouped-icon" />
</Listbox.ItemIndicator>
</Listbox.Item>
))}
</Listbox.Group>
))}
</Listbox.Root>
<p class="listbox-grouped-status">Selected: {value.value[0] ?? 'none'}</p>
</div>
);
});.listbox-grouped {
display: flex;
flex-direction: column;
gap: 8px;
width: 240px;
font-family: inherit;
}
.listbox-grouped-label {
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
}
.listbox-grouped-list {
display: flex;
flex-direction: column;
gap: 4px;
max-height: 220px;
overflow-y: auto;
padding: 4px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
}
.listbox-grouped-list:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.listbox-grouped-group {
display: flex;
flex-direction: column;
gap: 2px;
}
.listbox-grouped-group-label {
padding: 6px 8px 2px;
font-size: 12px;
font-weight: 600;
color: light-dark(#475569, #c9ddf0);
}
.listbox-grouped-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 6px 8px;
border-radius: 4px;
font-size: 14px;
cursor: default;
}
.listbox-grouped-item[data-state="checked"] {
color: light-dark(#006ce9, #4cb8ff);
font-weight: 500;
}
@media (hover: hover) {
.listbox-grouped-item:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
}
/* Active descendant highlight while navigating with the keyboard */
.listbox-grouped-list:focus-visible .listbox-grouped-item[data-highlighted] {
background: light-dark(#f1f5f9, #ffffff14);
}
.listbox-grouped-indicator {
display: inline-flex;
align-items: center;
}
.listbox-grouped-indicator[hidden] {
display: none;
}
.listbox-grouped-icon {
width: 16px;
height: 16px;
}
.listbox-grouped-status {
margin: 0;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}無効化#
Listbox.Rootにdisabledを指定すると、タブ順序から除外され(tabIndex={-1})、すべてのオプションが無効になります。個別のListbox.Itemにdisabledを指定すると、選択できなくなり、キーボードナビゲーションおよびタイプアヘッドでスキップされます。
import { component$, useStyles$ } from '@builder.io/qwik';
import { Listbox } from '@leve-ui/listbox';
import { LuCheck } from '@qwikest/icons/lucide';
import styles from './disabled.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<div class="listbox-disabled">
<span id="listbox-disabled-label" class="listbox-disabled-label">
Fruit (disabled)
</span>
<Listbox.Root
disabled
defaultValue={['apple']}
aria-labelledby="listbox-disabled-label"
class="listbox-disabled-list"
>
<Listbox.Item value="apple" class="listbox-disabled-item">
<Listbox.ItemText>Apple</Listbox.ItemText>
<Listbox.ItemIndicator class="listbox-disabled-indicator">
<LuCheck aria-hidden="true" class="listbox-disabled-icon" />
</Listbox.ItemIndicator>
</Listbox.Item>
<Listbox.Item value="banana" class="listbox-disabled-item">
<Listbox.ItemText>Banana</Listbox.ItemText>
<Listbox.ItemIndicator class="listbox-disabled-indicator">
<LuCheck aria-hidden="true" class="listbox-disabled-icon" />
</Listbox.ItemIndicator>
</Listbox.Item>
<Listbox.Item value="cherry" class="listbox-disabled-item">
<Listbox.ItemText>Cherry</Listbox.ItemText>
<Listbox.ItemIndicator class="listbox-disabled-indicator">
<LuCheck aria-hidden="true" class="listbox-disabled-icon" />
</Listbox.ItemIndicator>
</Listbox.Item>
</Listbox.Root>
</div>
);
});.listbox-disabled {
display: flex;
flex-direction: column;
gap: 8px;
width: 240px;
font-family: inherit;
}
.listbox-disabled-label {
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
opacity: 0.5;
}
.listbox-disabled-list {
display: flex;
flex-direction: column;
gap: 2px;
padding: 4px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
}
.listbox-disabled-list[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
.listbox-disabled-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 6px 8px;
border-radius: 4px;
font-size: 14px;
}
.listbox-disabled-item[data-state="checked"] {
color: light-dark(#006ce9, #4cb8ff);
font-weight: 500;
}
.listbox-disabled-indicator {
display: inline-flex;
align-items: center;
}
.listbox-disabled-indicator[hidden] {
display: none;
}
.listbox-disabled-icon {
width: 16px;
height: 16px;
}APIリファレンス#
Root#
| Prop | デフォルト | 型 |
|---|---|---|
| defaultValue | [] | string[] 初期状態で選択されているアイテムの値です(非制御)。 |
| bind:value | – | Signal<string[]> 選択された項目の値を持つシグナルです(制御済み)。 |
| onValueChange$ | – | QRL<(value: string[]) => void> 選択された項目が変更されたときに呼び出されます。 |
| multiple | false | boolean 一度に複数の項目を選択できるかどうかです。 |
| disabled | false | boolean リストボックスが無効かどうかです。 |
| loop | false | boolean 矢印キーで最後の項目から最初の項目へ、またはその逆に循環するかどうかです。 |
| orientation | 'vertical' | 'vertical' | 'horizontal' ハイライトを移動する矢印キーを決定する、リストボックスの向きです。 |
<div>のすべての属性も受け付けます。
| データ属性 | 値 |
|---|---|
data-leve-listbox | 常に存在 |
data-orientation | "vertical" | "horizontal" |
data-disabled | disabled時に付与 |
Item#
| Prop | デフォルト | 型 |
|---|---|---|
| value (必須) | – | string アイテムの値です。リストボックス内で一意である必要があります。 |
| disabled | false | boolean アイテムを選択不可にするかどうかを指定します。矢印キーとタイプアヘッドはこれをスキップします。 |
| textValue | – | string アイテムのテキストコンテンツと異なる場合にタイプアヘッドで使用されるテキストです。 |
<div>のすべての属性も受け付けます。
| データ属性 | 値 |
|---|---|
data-leve-listbox-item | 常に存在 |
data-value | valueの値 |
data-state | "checked" | "unchecked" |
data-highlighted | ハイライトされたオプションに付与 |
data-disabled | アイテムまたはリストボックスが無効な場合に設定されます。 |
data-orientation | "vertical" | "horizontal" |
data-text-value | textValueの値(設定されている場合) |
フォーカスはListbox.Rootに留まるため、ハイライトされたオプションのスタイル設定には:focusではなく[data-highlighted]を使用してください。
ItemText#
独自のpropsはありません。 <span>のすべての属性を受け付けます。
| データ属性 | 値 |
|---|---|
data-leve-listbox-item-text | 常に存在 |
ItemIndicator#
独自のpropsはありません。 <span>のすべての属性を受け付けます。
| データ属性 | 値 |
|---|---|
data-leve-listbox-item-indicator | 常に存在 |
data-state | "checked" | "unchecked" |
オプションが選択されていない間、hidden属性が付与されます。displayを設定する場合は、[hidden] { display: none; }で再び非表示にしてください。
Group#
独自のpropsはありません。 <div>のすべての属性を受け付けます。
| データ属性 | 値 |
|---|---|
data-leve-listbox-group | 常に存在 |
GroupLabel#
独自のpropsはありません。 <div>のすべての属性を受け付けます。
| データ属性 | 値 |
|---|---|
data-leve-listbox-group-label | 常に存在 |
アクセシビリティ#
Listbox WAI-ARIAデザインパターンに準拠しています。
Listbox.Rootは、tabIndex={0}(無効な場合は-1)、aria-orientation、およびmultipleが有効な場合はaria-multiselectable="true"を伴うrole="listbox"をレンダリングします。フォーカスはルート要素に留まり、aria-activedescendantがハイライトされたオプションを指します。Listbox.Rootのaria-labelまたはaria-labelledbyでリストボックスに名前を付けてください。- 各
Listbox.Itemはrole="option"とaria-selectedをレンダリングします。無効なオプションはaria-disabled="true"をレンダリングし、矢印キー、Home/End、およびタイプアヘッドでスキップされます。 Listbox.GroupはListbox.GroupLabelによって名前が付けられたrole="group"をレンダリングします。aria-selectedが選択状態を伝えるため、Listbox.ItemIndicatorはaria-hidden="true"によって支援技術から隠されます。
キーボード操作#
| キー | 説明 |
|---|---|
Tab | リストボックスの内外にフォーカスを移動します。フォーカス時には、最初に選択されている有効なオプション、または最初の有効なオプションをハイライトします。 |
ArrowDown / ArrowUp | 縦方向のみ。ハイライトを次/前の有効なオプションに移動します。loopがtrueの場合はループします。 |
ArrowRight / ArrowLeft | 横方向のみ。ハイライトを次/前の有効なオプションに移動します(RTLでは逆方向になります)。loopがtrueの場合はループします。 |
Home / End | ハイライトを最初/最後の有効なオプションに移動します。 |
Space / Enter | ハイライトされたオプションを選択します(multipleがtrueの場合は選択を切り替えます)。 |
Shift + ArrowDown / Shift + ArrowUp | 複数選択のみ。ハイライトを移動し、次/前の有効なオプションまで範囲選択を拡張します。 |
Shift + ArrowRight / Shift + ArrowLeft | 複数選択かつ横方向のみ。ハイライトを移動し、範囲選択を拡張します。 |
Shift + Home / Shift + End | 複数選択のみ。ハイライトを移動し、最初/最後の有効なオプションまで範囲選択を拡張します。 |
Ctrl+A / Cmd+A | 複数選択のみ。有効なすべてのオプションを選択します。 |
| 印字可能文字 | タイプアヘッド。入力された文字で始まる、一致する有効なオプションにハイライトを移動します。 |