ナビゲーション

v2.1.0
フォーム

リストボックス

選択肢の一覧です。コンボボックスやコマンドパレットがこの一覧を開きます。

このページの内容

リストボックスは、項目を選ぶための一覧です。コンボボックスやコマンドパレットが選択肢を表示するために開くほか、ページ内に単独で配置することもできます。スタイルシートはARIA属性に応じて各状態を描画します。スクリプトはキー操作を処理し、アクティブな選択肢や選択済みの項目に合わせて属性を更新します。単独で配置する場合はlistbox ビヘイビアがその処理を担います。コンボボックスとコマンドパレットでは、それぞれの入力フィールドからリストを操作します。

リストボックス

<div class="sheet sheet-flat max-w(320) mx-auto">  <div class="listbox" role="listbox" aria-label="Framework" tabindex="0" aria-activedescendant="fw-astro">    <div class="listbox-option" role="option" id="fw-astro" aria-selected="true" aria-checked="true">      Astro    </div>    <div class="listbox-option" role="option" id="fw-next" aria-checked="false">Next.js</div>    <div class="listbox-option" role="option" id="fw-svelte" aria-checked="false">SvelteKit</div>    <div class="listbox-option" role="option" id="fw-nuxt" aria-checked="false" aria-disabled="true">      Nuxt    </div>  </div></div><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => attach(root.current!), [])  return (    <div ref={root} className="sheet sheet-flat max-w(320) mx-auto">      <div className="listbox" role="listbox" aria-label="Framework" tabIndex={0} aria-activedescendant="fw-astro">        <div className="listbox-option" role="option" id="fw-astro" aria-selected="true" aria-checked="true">          Astro        </div>        <div className="listbox-option" role="option" id="fw-next" aria-checked="false">Next.js</div>        <div className="listbox-option" role="option" id="fw-svelte" aria-checked="false">          SvelteKit        </div>        <div className="listbox-option" role="option" id="fw-nuxt" aria-checked="false" aria-disabled="true">          Nuxt        </div>      </div>    </div>  )}
<script>  import { attach } from '@master/ui/behaviors'</script><div class="sheet sheet-flat max-w(320) mx-auto" {@attach attach}>  <div class="listbox" role="listbox" aria-label="Framework" tabindex="0" aria-activedescendant="fw-astro">    <div class="listbox-option" role="option" id="fw-astro" aria-selected="true" aria-checked="true">      Astro    </div>    <div class="listbox-option" role="option" id="fw-next" aria-checked="false">Next.js</div>    <div class="listbox-option" role="option" id="fw-svelte" aria-checked="false">SvelteKit</div>    <div class="listbox-option" role="option" id="fw-nuxt" aria-checked="false" aria-disabled="true">      Nuxt    </div>  </div></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <div ref="root" class="sheet sheet-flat max-w(320) mx-auto">    <div class="listbox" role="listbox" aria-label="Framework" tabindex="0" aria-activedescendant="fw-astro">      <div class="listbox-option" role="option" id="fw-astro" aria-selected="true" aria-checked="true">        Astro      </div>      <div class="listbox-option" role="option" id="fw-next" aria-checked="false">Next.js</div>      <div class="listbox-option" role="option" id="fw-svelte" aria-checked="false">SvelteKit</div>      <div class="listbox-option" role="option" id="fw-nuxt" aria-checked="false" aria-disabled="true">        Nuxt      </div>    </div>  </div></template>
選択肢をクリックするか、リストにフォーカスを移して矢印キーで移動し、EnterキーまたはSpaceキーで選択します。

#2つの状態と2つの属性

リストボックスでは、キー操作の対象になっている項目と、すでに選択された項目を区別します。

  • アクティブはaria-selected="true"で示します。矢印キーで移動した先の、Enterで選択できる項目です。背景はチップと同じ色調になります。ポインターを重ねた行も、フェードなしで即座に同じ色調になります。
  • 選択済みはaria-checked="true"で示します。値が確定した項目で、行末にチェックマークが付きます。aria-checkedを持つ選択肢は、どちらの状態でもチェックマークのスペースを確保するため、ラベルの位置がずれません。

通常、フォーカスはリストかテキストフィールドに留まり、aria-activedescendantがアクティブな選択肢を指します。選択肢自体がフォーカスを受け取る場合は、文字色のアウトラインも表示されます。

#情報を追加した選択肢

行頭のアイコンにはiconを指定します。listbox-bodyはタイトルの下にlistbox-descriptionを配置し、listbox-endはショートカットやヒントを行末に置きます。listbox-groupは一連の選択肢をそのlistbox-labelとともに囲み、listbox-separatorはグループ間に区切り線を引きます。

情報を追加した選択肢

<div class="sheet sheet-flat max-w(360) mx-auto">  <div class="listbox" role="listbox" aria-label="Commands">    <div class="listbox-group" role="group" aria-labelledby="rich-suggestions">      <div class="listbox-label" id="rich-suggestions">Suggestions</div>      <div class="listbox-option" role="option" aria-selected="true">        <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M3 8L13 8" /><path d="M8 3L8 13" />        </svg> New project <span class="listbox-end"><kbd class="kbd">⌘</kbd><kbd class="kbd">N</kbd></span>      </div>      <div class="listbox-option" role="option">        <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <circle cx="7" cy="7" r="4.5" /><path d="M10.182 10.182L14 14" />        </svg>        <span class="listbox-body">          <span>Search deployments</span>          <span class="listbox-description">By branch, commit or author</span>        </span>      </div>    </div>    <hr class="listbox-separator">    <div class="listbox-group" role="group" aria-labelledby="rich-team">      <div class="listbox-label" id="rich-team">Team</div>      <div class="listbox-option" role="option">        <span class="avatar avatar-xs">          <img class="avatar-image" src="/demo/portrait-1.svg" alt>        </span> Ada Lovelace      </div>      <div class="listbox-option" role="option">        <span class="avatar avatar-xs">          <img class="avatar-image" src="/demo/portrait-2.svg" alt>        </span> Alan Turing      </div>    </div>  </div></div>
import PlusIcon from '@master/icons-react/native/16/soft/plus'import SearchIcon from '@master/icons-react/native/16/soft/search'<div className="sheet sheet-flat max-w(360) mx-auto">  <div className="listbox" role="listbox" aria-label="Commands">    <div className="listbox-group" role="group" aria-labelledby="rich-suggestions">      <div className="listbox-label" id="rich-suggestions">Suggestions</div>      <div className="listbox-option" role="option" aria-selected="true">        <PlusIcon className="icon" /> New project <span className="listbox-end">          <kbd className="kbd">⌘</kbd><kbd className="kbd">N</kbd>        </span>      </div>      <div className="listbox-option" role="option">        <SearchIcon className="icon" />        <span className="listbox-body">          <span>Search deployments</span>          <span className="listbox-description">By branch, commit or author</span>        </span>      </div>    </div>    <hr className="listbox-separator" />    <div className="listbox-group" role="group" aria-labelledby="rich-team">      <div className="listbox-label" id="rich-team">Team</div>      <div className="listbox-option" role="option">        <span className="avatar avatar-xs">          <img className="avatar-image" src="/demo/portrait-1.svg" alt="" />        </span> Ada Lovelace      </div>      <div className="listbox-option" role="option">        <span className="avatar avatar-xs">          <img className="avatar-image" src="/demo/portrait-2.svg" alt="" />        </span> Alan Turing      </div>    </div>  </div></div>
<script>  import PlusIcon from '@master/icons-svelte/native/16/soft/plus'  import SearchIcon from '@master/icons-svelte/native/16/soft/search'</script><div class="sheet sheet-flat max-w(360) mx-auto">  <div class="listbox" role="listbox" aria-label="Commands">    <div class="listbox-group" role="group" aria-labelledby="rich-suggestions">      <div class="listbox-label" id="rich-suggestions">Suggestions</div>      <div class="listbox-option" role="option" aria-selected="true">        <PlusIcon class="icon" /> New project <span class="listbox-end"><kbd class="kbd">⌘</kbd><kbd class="kbd">N</kbd></span>      </div>      <div class="listbox-option" role="option">        <SearchIcon class="icon" />        <span class="listbox-body">          <span>Search deployments</span>          <span class="listbox-description">By branch, commit or author</span>        </span>      </div>    </div>    <hr class="listbox-separator">    <div class="listbox-group" role="group" aria-labelledby="rich-team">      <div class="listbox-label" id="rich-team">Team</div>      <div class="listbox-option" role="option">        <span class="avatar avatar-xs">          <img class="avatar-image" src="/demo/portrait-1.svg" alt>        </span> Ada Lovelace      </div>      <div class="listbox-option" role="option">        <span class="avatar avatar-xs">          <img class="avatar-image" src="/demo/portrait-2.svg" alt>        </span> Alan Turing      </div>    </div>  </div></div>
<script setup>import PlusIcon from '@master/icons-vue/native/16/soft/plus'import SearchIcon from '@master/icons-vue/native/16/soft/search'</script><template>  <div class="sheet sheet-flat max-w(360) mx-auto">    <div class="listbox" role="listbox" aria-label="Commands">      <div class="listbox-group" role="group" aria-labelledby="rich-suggestions">        <div class="listbox-label" id="rich-suggestions">Suggestions</div>        <div class="listbox-option" role="option" aria-selected="true">          <PlusIcon class="icon" /> New project <span class="listbox-end"><kbd class="kbd">⌘</kbd><kbd class="kbd">N</kbd></span>        </div>        <div class="listbox-option" role="option">          <SearchIcon class="icon" />          <span class="listbox-body">            <span>Search deployments</span>            <span class="listbox-description">By branch, commit or author</span>          </span>        </div>      </div>      <hr class="listbox-separator">      <div class="listbox-group" role="group" aria-labelledby="rich-team">        <div class="listbox-label" id="rich-team">Team</div>        <div class="listbox-option" role="option">          <span class="avatar avatar-xs">            <img class="avatar-image" src="/demo/portrait-1.svg" alt>          </span> Ada Lovelace        </div>        <div class="listbox-option" role="option">          <span class="avatar avatar-xs">            <img class="avatar-image" src="/demo/portrait-2.svg" alt>          </span> Alan Turing        </div>      </div>    </div>  </div></template>

#検索結果

検索結果は、その場所と一致した内容を示します。listbox-pathはタイトルの下の小さな1行で、階層ごとにlistbox-crumbを置きます。listbox-excerptは一致した箇所の抜粋で、最大2行です。入力された語句はmarkで囲みます。選択肢の中ではタイトルの文字色と太さになるため、淡い色の行の中でも見つけられます。

検索結果

<div class="sheet sheet-flat max-w(420) mx-auto">  <div class="listbox" role="listbox" aria-label="Results">    <div class="listbox-option" role="option" aria-selected="true">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M2.5 5.75L13.5 5.75" /><path d="M2.5 10.25L13.5 10.25" /><path d="M6.5 2.5L4.5 13.5" /><path d="M11.5 2.5L9.5 13.5" />      </svg>      <span class="listbox-body">        <span>Retune <mark class="mark">token</mark>s</span>        <span class="listbox-path">          <span class="listbox-crumb">Guide</span><span class="listbox-crumb">Customizing</span><span class="listbox-crumb">Theming</span>        </span>        <span class="listbox-excerpt">          Override a <mark class="mark">token</mark> after the import. Every component that reads it follows.        </span>      </span>    </div>    <div class="listbox-option" role="option">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M4.5 2L9.0858 2A1 1 0 0 1 9.7929 2.2929L12.2071 4.7071A1 1 0 0 1 12.5 5.4142L12.5 13A1 1 0 0 1 11.5 14L4.5 14A1 1 0 0 1 3.5 13L3.5 3A1 1 0 0 1 4.5 2Z" /><path d="M10 2.5V4.4A0.1 0.1 0 0 0 10.1 4.5H12L12.2409 4.7427A0.9993 0.9993 0 0 1 12.5 5.4142V5.5H10.1Q9.6444 5.5 9.3222 5.1778Q9 4.8556 9 4.4V2A1.1002 1.1002 0 0 1 9.3761 2.0431A1.0001 1.0001 0 0 1 9.7929 2.2929L10 2.5Z" fill="currentColor" stroke="none" fill-rule="evenodd" />      </svg>      <span class="listbox-body">        <span>Default theme</span>        <span class="listbox-path">          <span class="listbox-crumb">Guide</span><span class="listbox-crumb">Customizing</span>        </span>        <span class="listbox-excerpt">          Moonstone and obsidian: the default materials, <mark class="mark">token</mark> roles and implementation values in Master UI.        </span>      </span>    </div>    <div class="listbox-option" role="option">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M4.5 2L9.0858 2A1 1 0 0 1 9.7929 2.2929L12.2071 4.7071A1 1 0 0 1 12.5 5.4142L12.5 13A1 1 0 0 1 11.5 14L4.5 14A1 1 0 0 1 3.5 13L3.5 3A1 1 0 0 1 4.5 2Z" /><path d="M10 2.5V4.4A0.1 0.1 0 0 0 10.1 4.5H12L12.2409 4.7427A0.9993 0.9993 0 0 1 12.5 5.4142V5.5H10.1Q9.6444 5.5 9.3222 5.1778Q9 4.8556 9 4.4V2A1.1002 1.1002 0 0 1 9.3761 2.0431A1.0001 1.0001 0 0 1 9.7929 2.2929L10 2.5Z" fill="currentColor" stroke="none" fill-rule="evenodd" />      </svg>      <span class="listbox-body">        <span>Customization</span>        <span class="listbox-path">          <span class="listbox-crumb">Guide</span><span class="listbox-crumb">Customizing</span>        </span>      </span>    </div>  </div></div>
import HashIcon from '@master/icons-react/native/16/soft/hash'import FileIcon from '@master/icons-react/native/16/soft/file'<div className="sheet sheet-flat max-w(420) mx-auto">  <div className="listbox" role="listbox" aria-label="Results">    <div className="listbox-option" role="option" aria-selected="true">      <HashIcon className="icon" />      <span className="listbox-body">        <span>Retune <mark className="mark">token</mark>s</span>        <span className="listbox-path">          <span className="listbox-crumb">Guide</span><span className="listbox-crumb">Customizing</span><span className="listbox-crumb">Theming</span>        </span>        <span className="listbox-excerpt">          Override a <mark className="mark">token</mark> after the import. Every component that reads it follows.        </span>      </span>    </div>    <div className="listbox-option" role="option">      <FileIcon className="icon" />      <span className="listbox-body">        <span>Default theme</span>        <span className="listbox-path">          <span className="listbox-crumb">Guide</span><span className="listbox-crumb">Customizing</span>        </span>        <span className="listbox-excerpt">          Moonstone and obsidian: the default materials, <mark className="mark">token</mark> roles and implementation values in Master UI.        </span>      </span>    </div>    <div className="listbox-option" role="option">      <FileIcon className="icon" />      <span className="listbox-body">        <span>Customization</span>        <span className="listbox-path">          <span className="listbox-crumb">Guide</span><span className="listbox-crumb">Customizing</span>        </span>      </span>    </div>  </div></div>
<script>  import HashIcon from '@master/icons-svelte/native/16/soft/hash'  import FileIcon from '@master/icons-svelte/native/16/soft/file'</script><div class="sheet sheet-flat max-w(420) mx-auto">  <div class="listbox" role="listbox" aria-label="Results">    <div class="listbox-option" role="option" aria-selected="true">      <HashIcon class="icon" />      <span class="listbox-body">        <span>Retune <mark class="mark">token</mark>s</span>        <span class="listbox-path">          <span class="listbox-crumb">Guide</span><span class="listbox-crumb">Customizing</span><span class="listbox-crumb">Theming</span>        </span>        <span class="listbox-excerpt">          Override a <mark class="mark">token</mark> after the import. Every component that reads it follows.        </span>      </span>    </div>    <div class="listbox-option" role="option">      <FileIcon class="icon" />      <span class="listbox-body">        <span>Default theme</span>        <span class="listbox-path">          <span class="listbox-crumb">Guide</span><span class="listbox-crumb">Customizing</span>        </span>        <span class="listbox-excerpt">          Moonstone and obsidian: the default materials, <mark class="mark">token</mark> roles and implementation values in Master UI.        </span>      </span>    </div>    <div class="listbox-option" role="option">      <FileIcon class="icon" />      <span class="listbox-body">        <span>Customization</span>        <span class="listbox-path">          <span class="listbox-crumb">Guide</span><span class="listbox-crumb">Customizing</span>        </span>      </span>    </div>  </div></div>
<script setup>import HashIcon from '@master/icons-vue/native/16/soft/hash'import FileIcon from '@master/icons-vue/native/16/soft/file'</script><template>  <div class="sheet sheet-flat max-w(420) mx-auto">    <div class="listbox" role="listbox" aria-label="Results">      <div class="listbox-option" role="option" aria-selected="true">        <HashIcon class="icon" />        <span class="listbox-body">          <span>Retune <mark class="mark">token</mark>s</span>          <span class="listbox-path">            <span class="listbox-crumb">Guide</span><span class="listbox-crumb">Customizing</span><span class="listbox-crumb">Theming</span>          </span>          <span class="listbox-excerpt">            Override a <mark class="mark">token</mark> after the import. Every component that reads it follows.          </span>        </span>      </div>      <div class="listbox-option" role="option">        <FileIcon class="icon" />        <span class="listbox-body">          <span>Default theme</span>          <span class="listbox-path">            <span class="listbox-crumb">Guide</span><span class="listbox-crumb">Customizing</span>          </span>          <span class="listbox-excerpt">            Moonstone and obsidian: the default materials, <mark class="mark">token</mark> roles and implementation values in Master UI.          </span>        </span>      </div>      <div class="listbox-option" role="option">        <FileIcon class="icon" />        <span class="listbox-body">          <span>Customization</span>          <span class="listbox-path">            <span class="listbox-crumb">Guide</span><span class="listbox-crumb">Customizing</span>          </span>        </span>      </div>    </div>  </div></template>
  • アイコンは、下にパス、説明、抜粋のどれがあっても、タイトルの行に揃います。タイトルにパスまたは説明を添えても一つの行として扱うため、listbox-endはその2行のテキストの中央に揃います。抜粋があるとテキストが行の高さを超えるため、listbox-endもタイトルの横に並びます。
  • パスの各階層はテキストです。選択肢のターゲットは一つなので、パンくずリストのリンクにはなりません。
  • アイコンを持つ結果が一つでもあれば、すべての結果にアイコンを付けます。ページにはドキュメントのアイコン、ページ内の見出しにはハッシュ記号を使えば、固有のアイコンがない結果もまかなえます。

#絞り込み

スクリプトは、一致しない選択肢にhidden属性を設定して絞り込みます。すると、追加のスクリプトなしで、:has()によって次の2つの表示が切り替わります。

  • すべての選択肢が非表示になるとlistbox-emptyが表示され、1件でも再表示されると消えます。
  • listbox-groupは、見出しと選択肢を囲むrole="group"要素です。配下の選択肢がすべて非表示になると要素全体が隠れ、見出しだけが残ることはありません。

一致する項目がありません

<div class="sheet sheet-flat max-w(320) mx-auto">  <div class="listbox" role="listbox" aria-label="Regions">    <div class="listbox-option" role="option" hidden>Tokyo</div>    <div class="listbox-option" role="option" hidden>Frankfurt</div>    <div class="listbox-empty">No regions match "zz".</div>  </div></div>
<div className="sheet sheet-flat max-w(320) mx-auto">  <div className="listbox" role="listbox" aria-label="Regions">    <div className="listbox-option" role="option" hidden>Tokyo</div>    <div className="listbox-option" role="option" hidden>Frankfurt</div>    <div className="listbox-empty">No regions match "zz".</div>  </div></div>
<div class="sheet sheet-flat max-w(320) mx-auto">  <div class="listbox" role="listbox" aria-label="Regions">    <div class="listbox-option" role="option" hidden>Tokyo</div>    <div class="listbox-option" role="option" hidden>Frankfurt</div>    <div class="listbox-empty">No regions match "zz".</div>  </div></div>
<template>  <div class="sheet sheet-flat max-w(320) mx-auto">    <div class="listbox" role="listbox" aria-label="Regions">      <div class="listbox-option" role="option" hidden>Tokyo</div>      <div class="listbox-option" role="option" hidden>Frankfurt</div>      <div class="listbox-empty">No regions match "zz".</div>    </div>  </div></template>

#ビヘイビア

listboxは、ページ内に配置され、自身がフォーカスを保持するリストボックスを制御します。そのため、リストボックスにはtabindex="0"を指定します。role="listbox"が付いた要素を受け取り、オプション引数は取らず、ビヘイビアを解除する関数を返します。

app.js
import { listbox } from '@master/ui/behaviors/listbox.js'const list = document.querySelector('.listbox')listbox(list)list.addEventListener('change', () => {  const chosen = list.querySelectorAll('[aria-checked="true"]')})
  • アクティブな選択肢。 キー操作やクリックでaria-selected="true"を対象の選択肢に移し、その選択肢をリストボックスのaria-activedescendantで参照して、表示範囲に入るようにスクロールします。フォーカスはリストボックスに留まります。idのない選択肢には自動で付与します。フォーカスが入った時点でアクティブな選択肢がなければ、選択済みの項目を、選択済みの項目もなければ先頭の項目をアクティブにします。
  • 選択。 Enter、Space、またはクリックで、対象の選択肢にaria-checked="true"を、ほかの選択肢にfalseを設定します。
  • イベント。 選択内容が変わるたびに、リストボックスはinput、changeの順にイベントを発行します。選択済みの項目は、aria-checked="true"が付いた選択肢を取得して確認します。
  • 操作対象外の選択肢。 非表示の選択肢とaria-disabled="true"が付いた選択肢は、アクティブにならず、選択もできません。

リストボックスにaria-multiselectable="true"を指定すると、操作した選択肢だけが選択または解除され、ほかの選択肢の状態は維持されます。ファイル一覧と同様に、キー操作で連続した範囲の選択肢も選べます。最後に選んだ選択肢が起点となり、Shiftを押したまま矢印キーやSpaceを押すか、選択肢をクリックすると、起点からキー操作やポインターが指す選択肢までをすべて選択します。ControlまたはCommandを押しながらAを押すと、すべての選択肢をチェックします。すべてチェック済みの場合は、すべてのチェックを外します。

複数選択

<div class="sheet sheet-flat max-w(320) mx-auto">  <div class="listbox" role="listbox" aria-label="Notify me about" aria-multiselectable="true" tabindex="0">    <div class="listbox-option" role="option" id="notify-deploys" aria-checked="true">      Deployments    </div>    <div class="listbox-option" role="option" id="notify-comments" aria-checked="false">      Comments    </div>    <div class="listbox-option" role="option" id="notify-billing" aria-checked="true">Billing</div>    <div class="listbox-option" role="option" id="notify-mentions" aria-checked="false">      Mentions    </div>    <div class="listbox-option" role="option" id="notify-digest" aria-checked="false" aria-disabled="true">      Weekly digest    </div>  </div></div><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => attach(root.current!), [])  return (    <div ref={root} className="sheet sheet-flat max-w(320) mx-auto">      <div className="listbox" role="listbox" aria-label="Notify me about" aria-multiselectable="true" tabIndex={0}>        <div className="listbox-option" role="option" id="notify-deploys" aria-checked="true">          Deployments        </div>        <div className="listbox-option" role="option" id="notify-comments" aria-checked="false">          Comments        </div>        <div className="listbox-option" role="option" id="notify-billing" aria-checked="true">          Billing        </div>        <div className="listbox-option" role="option" id="notify-mentions" aria-checked="false">          Mentions        </div>        <div className="listbox-option" role="option" id="notify-digest" aria-checked="false" aria-disabled="true">          Weekly digest        </div>      </div>    </div>  )}
<script>  import { attach } from '@master/ui/behaviors'</script><div class="sheet sheet-flat max-w(320) mx-auto" {@attach attach}>  <div class="listbox" role="listbox" aria-label="Notify me about" aria-multiselectable="true" tabindex="0">    <div class="listbox-option" role="option" id="notify-deploys" aria-checked="true">      Deployments    </div>    <div class="listbox-option" role="option" id="notify-comments" aria-checked="false">      Comments    </div>    <div class="listbox-option" role="option" id="notify-billing" aria-checked="true">Billing</div>    <div class="listbox-option" role="option" id="notify-mentions" aria-checked="false">      Mentions    </div>    <div class="listbox-option" role="option" id="notify-digest" aria-checked="false" aria-disabled="true">      Weekly digest    </div>  </div></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <div ref="root" class="sheet sheet-flat max-w(320) mx-auto">    <div class="listbox" role="listbox" aria-label="Notify me about" aria-multiselectable="true" tabindex="0">      <div class="listbox-option" role="option" id="notify-deploys" aria-checked="true">        Deployments      </div>      <div class="listbox-option" role="option" id="notify-comments" aria-checked="false">        Comments      </div>      <div class="listbox-option" role="option" id="notify-billing" aria-checked="true">        Billing      </div>      <div class="listbox-option" role="option" id="notify-mentions" aria-checked="false">        Mentions      </div>      <div class="listbox-option" role="option" id="notify-digest" aria-checked="false" aria-disabled="true">        Weekly digest      </div>    </div>  </div></template>
リストにフォーカスしてから、Shift キーを押しながら ArrowDown キーを押すと、連続した選択肢をチェックできます。

テキストフィールドが制御するリストボックスは、そのフィールドのビヘイビアに従い、フィールドから操作します。listboxは、role="combobox"フィールドのaria-controlsが参照するリストボックスを処理対象から外します。attach()も、コンボボックスやコマンドパレット内のリストボックスを処理対象から外します。スクリプトがない場合は、属性に従って表示され、状態は変化しません。

#フローティング表示

popover属性を付けると、リストボックスは角丸12px、各行の角丸8pxのフローティングシートになります。アンカーに隣接して配置され、ビューポートの端では配置が反転します。表示・非表示時のアニメーションはメニューと同じです。テキストフィールドが制御する場合は、入力中に閉じないようにpopover="manual"を使います。コンボボックスに実装例があります。

#リストボックス、メニュー、セレクトの使い分け

  • セレクトは、一覧から値を1つ選ぶためのネイティブコントロールです。選択肢がプレーンテキストならセレクトを使います。スクリプトは不要です。
  • メニューには、ネイティブのボタンやリンクでアクションを並べます。
  • リストボックスには、スクリプトで絞り込み、グループ化、読み込みを行う値や結果を表示します。コンボボックス、コマンドパレット、複数選択などで使います。

#キーボード操作

以下は、リストボックス自身がフォーカスを保持しているときのlistboxビヘイビアのキー操作です。矢印キーでの移動は、先頭と末尾の選択肢で止まります。コンボボックスとコマンドパレットでは、それぞれのパターンに従ってフィールドでキー操作を行います。

キー操作
ArrowDown, ArrowUp無効な選択肢を飛ばして別の選択肢をアクティブにし、その項目が見える位置までスクロールします。
Home, End最初または最後の選択肢に移動します。
Enter, Spaceアクティブな選択肢を選びます。aria-multiselectable="true" が指定されたリストでは、その選択肢のチェックを切り替えます。
Shift+ArrowDown, Shift+ArrowUp複数選択できるリストでは、移動したうえで、最後に選んだ選択肢から新しいアクティブな選択肢までをすべてチェックします。
Shift+Space, Shift+click複数選択できるリストでは、最後に選んだ選択肢からこの選択肢までをすべてチェックします。
Control+Shift+Home, Control+Shift+End複数選択できるリストでは、最初または最後の選択肢までをすべてチェックします。Mac では Command を使います。
Control+A複数選択できるリストでは、すべての選択肢をチェックします。すべてチェック済みの場合は、すべてのチェックを外します。Mac では Command を使います。
Typing入力した文字列で始まる次の選択肢に移動します。

#アクセシビリティ

  • コンテナーにrole="listbox"とラベルを、各選択肢にrole="option"を指定します。listbox-groupにはrole="group"を指定し、aria-labelledbyで見出しをラベルとして参照します。
  • ビヘイビアはaria-selectedとaria-activedescendantを連動させ、アクティブな選択肢が見える位置までスクロールします。各選択肢にはスクロールマージンがあり、リストの端に余白が残ります。
  • 無効な選択肢にはaria-disabled="true"を指定します。キー操作ではその選択肢を飛ばします。

#React、Svelte、Vue

listboxコンポーネントは、データから選択肢を描画し、ビヘイビアを適用して、選択値の受け渡しを行います。groupを持つ選択肢は、その見出しの下に書き出されます。コンポーネントはrole="group"を持ち、listbox-labelでラベル付けされたlistbox-groupを書き出し、同じグループの選択肢をまとめて、グループが最初に現れた順に並べます。npx master-ui add listboxでプロジェクトにコピーできます。コンポーネントの構成と使い方は、React、Svelte、Vueを参照してください。

ui/listbox.tsx
'use client'import { useId, useState, type ComponentProps } from 'react'import { listbox } from '@master/ui/behaviors/listbox.js'import { useBehavior } from './use-behavior'export interface ListboxOption {  /** What choosing the option gives `onValueChange`. */  value: string  label: string  description?: string  /** The caption of the group the option belongs to. Options of one group are written together, under it. */  group?: string  disabled?: boolean}export interface ListboxProps extends Omit<ComponentProps<'div'>, 'defaultValue' | 'children' | 'role' | 'ref'> {  options: ListboxOption[]  /** The value of the chosen option, null for none; a list of them when `multiple`. */  value?: string | string[] | null  defaultValue?: string | string[] | null  onValueChange?: (value: string | string[] | null) => void  /** Any number of options may be chosen. */  multiple?: boolean}/** A listbox that stands in the page. Name it with `aria-label` or `aria-labelledby`. */export function Listbox({ options, value, defaultValue, onValueChange, multiple, className, ...root }: ListboxProps) {  const id = useId()  const ref = useBehavior(listbox, undefined, { value, defaultValue, onValueChange })  const [first] = useState(() => [value ?? defaultValue ?? []].flat())  const groups = [...new Set(options.map(option => option.group))]  const row = (option: ListboxOption) => (    <div className="listbox-option" role="option" key={option.value} data-value={option.value} aria-checked={first.includes(option.value)} aria-disabled={option.disabled || undefined}>      {option.description        ? <span className="listbox-body"><span>{option.label}</span><span className="listbox-description">{option.description}</span></span>        : option.label}    </div>  )  return (    <div className={className ? `listbox ${className}` : 'listbox'} role="listbox" tabIndex={0} aria-multiselectable={multiple || undefined} ref={ref} {...root}>      {groups.map((group, index) => group === undefined        ? options.filter(option => option.group === undefined).map(row)        : (            <div className="listbox-group" role="group" aria-labelledby={`${id}group${index}`} key={group}>              <div className="listbox-label" id={`${id}group${index}`}>{group}</div>              {options.filter(option => option.group === group).map(row)}            </div>          ))}    </div>  )}
ui/Listbox.svelte
<script lang="ts" module>  import type { HTMLAttributes } from 'svelte/elements'  export interface ListboxOption {    /** What choosing the option gives `onValueChange`. */    value: string    label: string    description?: string    /** The caption of the group the option belongs to. Options of one group are written together, under it. */    group?: string    disabled?: boolean  }  export interface ListboxProps extends Omit<HTMLAttributes<HTMLDivElement>, 'role' | 'children'> {    options: ListboxOption[]    /** The value of the chosen option, null for none; a list of them when `multiple`. Bind it, or leave it out. */    value?: string | string[] | null    onValueChange?: (value: string | string[] | null) => void    /** Any number of options may be chosen. */    multiple?: boolean  }</script><script lang="ts">  import { untrack } from 'svelte'  import { listbox } from '@master/ui/behaviors/listbox.js'  import { behavior } from './behavior.svelte.js'  let { options, value = $bindable(), onValueChange, multiple, class: className, ...root }: ListboxProps = $props()  const id = $props.id()  // The first value is drawn into the first render; after that the behavior marks what is chosen. A row written  // later, from new options, is drawn with the value as it is then, which is not read as state.  const chosen = { values: untrack(() => [value ?? []].flat()) }  const groups = $derived([...new Set(options.map(option => option.group))])  const attach = behavior(listbox, undefined, {    value: () => value,    change: (next) => { chosen.values = [next ?? []].flat(); value = next; onValueChange?.(next) },    settled: (held) => { chosen.values = [held ?? []].flat() },  })</script>{#snippet row(option: ListboxOption)}  <!-- The behavior writes `aria-selected` on the option the keys are on. -->  <!-- svelte-ignore a11y_role_has_required_aria_props -->  <div class="listbox-option" role="option" data-value={option.value} aria-checked={chosen.values.includes(option.value)} aria-disabled={option.disabled || undefined}>    {#if option.description}      <span class="listbox-body"><span>{option.label}</span><span class="listbox-description">{option.description}</span></span>    {:else}      {option.label}    {/if}  </div>{/snippet}<!--  @component  A listbox that stands in the page. Name it with `aria-label` or `aria-labelledby`.--><div class={['listbox', className]} role="listbox" tabindex={0} aria-multiselectable={multiple || undefined} {@attach attach} {...root}>  {#each groups as group, index (group)}    {#if group === undefined}      {#each options.filter(option => option.group === undefined) as option (option.value)}{@render row(option)}{/each}    {:else}      <div class="listbox-group" role="group" aria-labelledby="{id}group{index}">        <div class="listbox-label" id="{id}group{index}">{group}</div>        {#each options.filter(option => option.group === group) as option (option.value)}{@render row(option)}{/each}      </div>    {/if}  {/each}</div>
ui/Listbox.vue
<script lang="ts">export interface ListboxOption {  /** What choosing the option gives the model. */  value: string  label: string  description?: string  /** The caption of the group the option belongs to. Options of one group are written together, under it. */  group?: string  disabled?: boolean}</script><script setup lang="ts">import { computed, h, useId, useTemplateRef, type FunctionalComponent } from 'vue'import { listbox } from '@master/ui/behaviors/listbox.js'import { useBehavior } from './use-behavior'defineOptions({ inheritAttrs: false })const { options } = defineProps<{  options: ListboxOption[]  /** Any number of options may be chosen. */  multiple?: boolean}>()/** The value of the chosen option, null for none; a list of them when `multiple`: `v-model`. */const model = defineModel<string | string[] | null>()/* A listbox that stands in the page. Name it with `aria-label` or `aria-labelledby`. */const id = useId()const first = [model.value ?? []].flat()const groups = computed(() => [...new Set(options.map(option => option.group))])const root = useTemplateRef<HTMLDivElement>('root')useBehavior(root, listbox, undefined, model)const Row: FunctionalComponent<{ option: ListboxOption }> = ({ option }) => h('div', {  'class': 'listbox-option',  'role': 'option',  'data-value': option.value,  'aria-checked': first.includes(option.value),  'aria-disabled': option.disabled || undefined,}, option.description  ? h('span', { class: 'listbox-body' }, [h('span', option.label), h('span', { class: 'listbox-description' }, option.description)])  : option.label)Row.props = ['option']</script><template>  <div class="listbox" role="listbox" tabindex="0" :aria-multiselectable="multiple || undefined" ref="root" v-bind="$attrs">    <template v-for="(group, index) in groups" :key="group ?? ''">      <template v-if="group === undefined">        <template v-for="option in options" :key="option.value"><Row v-if="option.group === undefined" :option="option" /></template>      </template>      <div v-else class="listbox-group" role="group" :aria-labelledby="`${id}-group-${index}`">        <div class="listbox-label" :id="`${id}-group-${index}`">{{ group }}</div>        <template v-for="option in options" :key="option.value"><Row v-if="option.group === group" :option="option" /></template>      </div>    </template>  </div></template>

#クラス

クラス説明
listbox選択肢の一覧です。popover 属性がある場合はフローティングシートとして表示されます。
listbox-optionrole="option" を持つ要素に指定する選択肢です。data-value を値として使い、data-value がない場合は要素のテキストを使います。
listbox-body選択肢内でタイトルと説明を上下に並べます。タイトルが上に表示されます。
listbox-descriptionlistbox-body 内で、選択肢のタイトルの下に控えめな色で表示する説明文です。
listbox-path選択肢の場所を示します。タイトルの下の小さな1行で、listbox-body の中に置き、階層ごとに listbox-crumb を持ちます。折り返さず、上のタイトルはコントロールの太さになります。
listbox-crumblistbox-path の1階層です。最初の階層を除き、前に山形の記号が付きます。リンクではなくテキストです。
listbox-excerpt選択肢の文字の下に置く抜粋です。listbox-body の中に置き、検索に一致した内容を最大2行で示します。抜粋があると、アイコンと末尾の要素はタイトルの横に並びます。
listbox-end選択肢の末尾に表示するショートカットまたはヒントです。
listbox-groupキャプション付きの選択肢グループです。role="group" を持つ要素をキャプションでラベル付けします。グループ内の選択肢がすべて非表示の間は、グループも非表示になります。
listbox-label選択肢のグループの上に控えめに表示するキャプションです。
listbox-separatorリストの両端まで伸びる、グループ間の極細の区切り線です。
listbox-empty一致する項目がないことを伝えるメッセージです。すべての選択肢が非表示になると表示されます。

#カスタムプロパティ

プロパティ既定値説明
--listbox-inset4px選択肢全体の周囲のパディング
--listbox-option-radiusvar(--radius-control-sm)行の角の丸みです。フローティングリストの角の丸みは、この値に内側の余白を加えた値です。

#ソース

@master/ui/components/listbox.css
/* * Listbox: a list of options to choose from, the surface a combobox and a command palette open. * The stylesheet draws every state from ARIA and leaves the behavior to the script that drives it: * aria-selected marks the active option, the one the arrow keys are on; aria-checked marks a chosen value * with a check. In the flow of the page it is a plain list; with the popover attribute it is a floating * sheet placed against its anchor, whose corner is a row's corner plus its 4px inset. * An option follows one alignment: its icon, its words and its end center on one another, which holds while the * words are a title and one line under it, a description or a path. With an excerpt, a passage of up to two * lines, the option turns to its first line: the icon and the end stand beside the title and the rest runs * under it. A match in an option is a mark, in the title's ink and weight so it is found in a muted line too. */@layer components.ui {  .listbox {    --listbox-inset: 4px;    --listbox-option-radius: var(--radius-control-sm);    min-width: 0;    margin: 0;    padding: var(--listbox-inset);    overflow-y: auto;    overscroll-behavior: contain;    color: var(--color-text-strong);    font: 400 .84375rem/1.2 var(--font-family-sans);    font-feature-settings: inherit;    /* A label, a description or a query echoed in the empty state wraps rather than scroll the list sideways. */    overflow-wrap: anywhere;    list-style: none;    scrollbar-width: thin;    scrollbar-color: var(--color-line-strong) transparent;    /*     * The list holds focus while aria-activedescendant moves through it: the active row shows where, with the     * ink outline the keys leave inside a row, so it never reads as a row under the pointer.     */    &:focus-visible {      outline: none;      .listbox-option[aria-selected="true"] {        outline: 2px solid var(--color-focus);        outline-offset: -2px;      }    }    /*     * Until an option is active (the script has not run yet, a press gave the list focus, or every option is     * filtered out) the list itself shows that it holds the keys.     */    &:focus-visible:not(:has(.listbox-option[aria-selected="true"]:not([hidden]))) {      @apply --focus-ring;    }    /* Floating: opened with showPopover() by the script that owns the field. */    &[popover] {      --surface-color: var(--color-surface-floating);      min-width: 11rem;      max-width: min(24rem, calc(100vw - 1rem));      max-height: min(20rem, 60dvh);      border: 0;      border-radius: calc(var(--listbox-option-radius) * var(--corner-scale) + var(--listbox-inset));      corner-shape: var(--corner-shape);      background: var(--color-surface-floating);      box-shadow: var(--shadow-floating);      transform-origin: top;      @apply --anchored;      @apply --pop;    }    @media (forced-colors: active) {      &[popover] {        outline: 1px solid CanvasText;        outline-offset: -1px;      }    }  }  /* An option: role="option" on any element. */  .listbox-option {    --icon-size: 1rem;    --icon-offset: -.0625rem;    --icon-color: var(--color-text-muted);    display: flex;    align-items: center;    gap: .625rem;    min-height: 2rem;    padding: .25rem .5rem;    border-radius: calc(var(--listbox-option-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    outline: none;    color: var(--color-text-strong);    scroll-margin: var(--listbox-inset);    user-select: none;    -webkit-user-select: none;    -webkit-tap-highlight-color: transparent;    cursor: pointer;    touch-action: manipulation;    /* A script filters by setting hidden; a display on this rule would otherwise bring the option back. */    &[hidden] {      display: none;    }    /* A row answers at once: no fade between rows as the pointer or the keys travel. */    @media (hover: hover) {      &:hover:not([aria-disabled="true"]) {        --icon-color: currentColor;        background: var(--color-surface-chip);      }    }    /* Pressed: a step deeper than the pointer's tone. */    &:active:not([aria-disabled="true"]) {      background: var(--color-surface-inset-hover);    }    &[aria-selected="true"] {      --icon-color: currentColor;      background: var(--color-surface-chip);    }    /* With real focus on the option, the ink outline says so. */    &:focus-visible {      --icon-color: currentColor;      background: var(--color-surface-chip);      outline: 2px solid var(--color-focus);      outline-offset: -2px;    }    &[aria-disabled="true"] {      --icon-color: currentColor;      color: var(--color-text-disabled);      cursor: not-allowed;    }    /* A checkable option keeps room for its mark at the end, so nothing moves when it is chosen. */    &[aria-checked]::after {      content: '';      flex: none;      width: 14px;      height: 14px;      margin-inline-start: auto;      opacity: 0;      background: currentColor;      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3 8L6.3532 11.3532A0.2 0.2 0 0 0 6.6412 11.3479L13 4.5'/%3E%3C/svg%3E") center / contain no-repeat;    }    /* Beside something at the end of the option, the mark follows it: only one of the two is pushed to the end. */    &[aria-checked]:has(.listbox-end)::after {      margin-inline-start: 0;    }    &[aria-checked="true"]::after {      opacity: 1;    }    @media (pointer: coarse) {      min-height: 2.75rem;    }    @media (forced-colors: active) {      &:is(:focus-visible, [aria-selected="true"]):not([aria-disabled="true"]) {        background: Highlight;        color: HighlightText;        forced-color-adjust: none;      }      /* Only a pointer that hovers lights a row: a tap would leave it lit on a touch screen. */      @media (hover: hover) {        &:hover:not([aria-disabled="true"]) {          background: Highlight;          color: HighlightText;          forced-color-adjust: none;        }      }      &[aria-disabled="true"] {        color: GrayText;      }      /* The check is a fill cut by a mask: kept out of forced colors, it takes the row's system ink. */      &::after {        forced-color-adjust: none;        background: currentColor;      }    }  }  /* A title with a line under it, for an option that needs a sentence. */  .listbox-body {    display: grid;    flex: 1 1 0;    gap: .125rem;    min-width: 0;    padding-block: .125rem;  }  .listbox-description {    margin: 0;    color: var(--color-text-muted);    font-size: .75rem;    line-height: 1.4;    text-wrap: pretty;  }  /*   * Where an option is: the steps of a path on one line under the title, a chevron between them. They are not   * links (an option is one target), so this is not a breadcrumb. The line never wraps: its steps give way   * together and end in an ellipsis.   */  .listbox-path {    display: flex;    min-width: 0;    margin: 0;    color: var(--color-text-muted);    font-size: .71875rem;    line-height: 1.4;    white-space: nowrap;  }  .listbox-crumb {    flex: 0 1 auto;    min-width: 0;    overflow: hidden;    text-overflow: ellipsis;    &:not(:first-child)::before {      content: '';      display: inline-block;      width: .75rem;      height: .75rem;      margin-inline: .125rem;      background: var(--color-text-disabled);      vertical-align: -.125rem;      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5'/%3E%3C/svg%3E") center / contain no-repeat;    }    &:dir(rtl)::before {      scale: -1 1;    }  }  /*   * A passage under an option's words, where a search shows what it matched: two lines at most, then an ellipsis.   * Without line-clamp the passage runs to its end.   */  .listbox-excerpt {    display: -webkit-box;    margin: .125rem 0 0;    overflow: hidden;    color: var(--color-text-muted);    font-size: .75rem;    line-height: 1.45;    -webkit-box-orient: vertical;    -webkit-line-clamp: 2;    line-clamp: 2;  }  /*   * An icon names the option, so it stands on the title's line whatever the body holds under it: a description,   * a path or an excerpt. Centred on two lines it sat between the title and the line under, beside neither, and a   * list that mixes results with and without an excerpt showed its icons at two heights. A picture, an avatar or   * a thumbnail, is as tall as the two lines and still centers on them.   */  .listbox-option:has(.listbox-body) {    /* The title's line and the body's padding, in rem: the end reads it at its own smaller size. */    --listbox-line: calc(.84375rem * 1.2 + .25rem);    .icon {      align-self: start;      margin-block: calc((var(--listbox-line) - var(--icon-size)) / 2);    }  }  /*   * Centering reads as one row for a title and a line under it. An excerpt makes the words taller than that, so   * the option turns to its first line: the icon, the end and the check stand beside the title, each in a box   * as tall as the title's line with the body's own padding, and the row keeps a little more room around it.   */  .listbox-option:has(.listbox-excerpt) {    align-items: start;    padding-block: .375rem;    .listbox-end {      min-height: var(--listbox-line);    }    &[aria-checked]::after {      margin-block: calc((var(--listbox-line) - 14px) / 2);    }  }  /*   * A result's title leads smaller lines, so it takes a control's weight and they do not. A list of results   * keeps one weight whether or not a result has a passage to show.   */  .listbox-option:has(.listbox-path, .listbox-excerpt) {    font-weight: var(--font-weight-control);    :is(.listbox-description, .listbox-path, .listbox-excerpt, .listbox-end) {      font-weight: 400;    }  }  /* A match: the title's ink and a control's weight over the wash, so it is found at a glance in a muted line. */  .listbox-option .mark {    --mark-weight: var(--font-weight-control);  }  .listbox-option:not([aria-disabled="true"]) .mark {    --mark-ink: var(--color-text-strong);  }  /* A shortcut or a hint at the end of an option. */  .listbox-end {    display: inline-flex;    flex: none;    align-items: center;    gap: .25rem;    margin-inline-start: auto;    padding-inline-start: 1rem;    color: var(--color-text-muted);    font-size: .71875rem;    font-variant-numeric: tabular-nums;  }  /* Secondary ink must follow the system-selected foreground, including on highlighted options. */  @media (forced-colors: active) {    :is(.listbox-description, .listbox-path, .listbox-excerpt, .listbox-end) {      color: inherit;    }    /* The chevron is a fill cut by a mask: it takes the row's system ink, as the check does. */    .listbox-crumb:not(:first-child)::before {      forced-color-adjust: none;      background: currentColor;    }  }  /* A group: role="group" around a caption and its options. With every option filtered out, it steps aside. */  .listbox-group:not(:has(.listbox-option:not([hidden]))) {    display: none;  }  /*   * A quiet caption over a group of options. It belongs to the options under it: the room above parts its group from   * the one before, and the room below is small, so the caption reads with the options it leads. The first group still   * shown, or one after a separator, has nothing to part from or a hairline that does it, and keeps a little room.   * Without :nth-child(of), the first group keeps the larger room.   */  .listbox-label {    margin: 0;    padding: .75rem .5rem .25rem;    color: var(--color-text-muted);    font-size: .71875rem;    user-select: none;    -webkit-user-select: none;    .listbox-separator + .listbox-group & {      padding-block-start: .375rem;    }    .listbox-group:nth-child(1 of .listbox-group:has(.listbox-option:not([hidden]))) & {      padding-block-start: .375rem;    }  }  /* A hairline between groups, running to the list's edges. */  .listbox-separator {    height: 1px;    margin: var(--listbox-inset) calc(-1 * var(--listbox-inset));    border: 0;    background: var(--color-line-hair);    /* Forced colors repaint the background as the canvas; a system color keeps the rule. */    @media (forced-colors: active) {      background: CanvasText;    }  }  /* What the list says when nothing matches. It shows by itself once every option is hidden. */  .listbox-empty {    margin: 0;    padding: 1.5rem .75rem;    color: var(--color-text-muted);    text-align: center;    text-wrap: balance;  }  .listbox:has(.listbox-option:not([hidden])) .listbox-empty {    display: none;  }  /* A caption or a rule with no visible option left under its list has nothing to introduce. */  .listbox:not(:has(.listbox-option:not([hidden]))) :is(.listbox-label, .listbox-separator) {    display: none;  }}