清單方塊
供使用者選擇的清單,也是組合方塊與指令面板展開時顯示的介面。
本頁內容
清單方塊列出可供選擇的項目。它是組合方塊和指令面板展開時顯示的介面,也能獨立放在頁面上。樣式表根據 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、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>由文字欄位控制的清單方塊,會由該欄位的行為從欄位端操作。若清單是由 role="combobox" 欄位的 aria-controls 指定,listbox 會略過它。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) | 列的圓角半徑。浮動清單的圓角半徑為此值加上清單內距。 |