列表框
供用户选择的选项列表,也是组合框和命令面板打开时显示的列表。
本页内容
列表框提供一组选项供用户选择。组合框和命令面板打开时都会显示列表框,列表框也可以独立放在页面中。样式表根据 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>#两种状态与对应的属性
列表框分别标记键盘当前指向的选项和已经选中的选项。
- 活动项由
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 是标题下方的一行小字,每一级各用一个 listbox-crumb;listbox-excerpt 是匹配的段落,最多两行。把输入的文字包在 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与这两行文字的中央对齐;摘录会让文字超出选项行的高度,此时listbox-end也改为与标题对齐。 - 路径的各级是文字。一个选项只有一个目标,所以它们不是面包屑导航的链接。
- 只要有一个结果带图标,就让每个结果都带图标:页面用文档图标,页面内的标题用井号,即可覆盖没有专属图标的结果。
#筛选
脚本为不匹配的选项设置 hidden 属性来筛选。随后,:has() 会自动处理以下两种情况,无需额外脚本:
- 所有选项都隐藏时,
listbox-empty会显示。只要有一个选项重新显示,它就会再次隐藏。 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" 的元素,没有配置项,并返回用于解除绑定的函数。
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、空格键或点击选项时,脚本会为该选项设置
aria-checked="true",并为其余选项设置false。 - 事件。 每当选中项发生变化,列表框都会先派发
input,再派发change。通过aria-checked="true"读取选项。 - 跳过的选项。 隐藏的选项和带有
aria-disabled="true"的选项都不能成为活动项,也不能被选中。
列表框带有 aria-multiselectable="true" 时,可以独立选中或取消选中某个选项,其他选项保留各自的状态。与文件列表一样,也可以用按键选中一段连续的选项:最后选中的选项作为锚点,按住 Shift 再按方向键、空格键或点击,会选中从锚点到键盘或指针当前指向的选项为止的所有选项,包含两端。按 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>文本字段控制的列表框由该字段的行为脚本驱动。listbox 会跳过由 role="combobox" 字段的 aria-controls 指定的列表框,attach() 则会跳过组合框或命令面板内的列表框。没有脚本时,列表框按现有属性呈现,状态保持不变。
#浮动列表框
带有 popover 属性时,列表框呈现为浮动面板,外层圆角为 12px,选项行圆角为 8px。面板贴着锚点放置,遇到视口边缘时翻转,进入和退出的动效与菜单相同。如果列表框由文本字段控制,请使用 popover="manual",让输入时列表框保持打开。组合框展示了这种用法。
#列表框、菜单或下拉选择框
#键盘操作
以下按键适用于列表框自身持有焦点时的 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-group,以其中的 listbox-label 作为标签,并把同组的选项放在一起,分组按首次出现的顺序排列。使用 npx master-ui add listbox 将组件复制到项目中。React、Svelte 和 Vue 介绍这些组件的实现和用法。
#类名
| 类名 | 说明 |
|---|---|
listbox | 列表本体。带有 popover 属性时显示为浮动面板。 |
listbox-option | 在带 role="option" 的元素上使用。选项代表其 data-value,未设置时使用自身文本。 |
listbox-body | 用于选项内部,将标题置于说明上方。 |
listbox-description | listbox-body 内选项标题下方的弱化文字。 |
listbox-path | 选项所在的位置:标题下方的一行小字,位于 listbox-body 内,每一级各用一个 listbox-crumb。它不换行,上方的标题采用控件的字重 |
listbox-crumb | listbox-path 的一级,除第一级外,每一级前面都有箭头。它是文字,不是链接 |
listbox-excerpt | 选项文字下方的段落,位于 listbox-body 内,用于显示搜索匹配的内容,最多两行。有了它,图标和行尾内容会与标题对齐 |
listbox-end | 选项末尾的快捷键或提示 |
listbox-group | 将选项和组标题放在 role="group" 的元素中,并以组标题作为该元素的可访问名称。所有选项都隐藏时,整组随之隐藏。 |
listbox-label | 选项组上方的组标题,采用弱化样式。 |
listbox-separator | 在列表组之间绘制一条延伸到两侧边缘的细线 |
listbox-empty | 无匹配项时的列表提示,在所有选项都隐藏后显示 |
#自定义属性
| 属性 | 默认值 | 说明 |
|---|---|---|
--listbox-inset | 4px | 列表内围绕选项的内边距。 |
--listbox-option-radius | var(--radius-control-sm) | 行圆角半径。浮动列表的圆角半径为此值加内边距 |