浮层
命令面板
带搜索框的对话框,下方列出可执行命令。
本页内容
命令面板由上方的搜索字段和下方的命令列表组成,可在文档中的任意位置用快捷键打开。它是一种内部结构有所不同的对话框:将 command 与 dialog 一同使用,并在其中放入一行 command-search、一个 列表框 和可选的 command-footer。面板使用原生 <dialog>,按钮通过 command="show-modal" 打开它,浏览器负责显示遮罩、将焦点限制在对话框内,以及响应 Esc 关闭面板。
命令面板
<button type="button" class="btn" commandfor="palette" command="show-modal"> Search commands <kbd class="kbd">⌘K</kbd></button><dialog id="palette" class="dialog command" closedby="any" aria-label="Command palette"> <label class="command-search"> <svg class="icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="8.75" cy="8.75" r="5.75" /><path d="M12.8159 12.8159L17.5 17.5" /> </svg> <input class="command-input" id="palette-input" type="search" role="combobox" aria-expanded="true" aria-controls="palette-list" aria-activedescendant="cmd-new" aria-autocomplete="list" aria-label="Search commands" autocomplete="off" spellcheck="false" enterkeyhint="go" placeholder="Type a command or search" autofocus> <kbd class="kbd">Esc</kbd> </label> <div class="listbox command-list" id="palette-list" role="listbox" aria-label="Commands"> <div class="listbox-group" role="group" aria-labelledby="group-suggestions"> <div class="listbox-label" id="group-suggestions">Suggestions</div> <div class="listbox-option" role="option" id="cmd-new" 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" id="cmd-deploy"> <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="M7.5892 2.215A0.4996 0.4996 0 0 0 7.5 2.5L7.5006 10.0245A0.5 0.5 0 0 0 8.4946 10.0734L8.5 10L8.4986 2.4621A0.5007 0.5007 0 0 0 8.4108 2.215A0.7044 0.7044 0 0 0 7.5892 2.215Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><path d="M5 5.5L7.8586 2.6414A0.2 0.2 0 0 1 8.1414 2.6414L11 5.5" /><path d="M2.5 10.5L2.5 12.5A1 1 0 0 0 3.5 13.5L12.5 13.5A1 1 0 0 0 13.5 12.5L13.5 10.5" /> </svg> Deploy to production </div> <div class="listbox-option" role="option" id="cmd-invite"> <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="6.25" cy="4.5" r="2" /><path d="M2.25 13.75V12.5C2.25 10.7 3.85 9.5 5.35 9.5H7.15C8.65 9.5 10.25 10.7 10.25 12.5V13.75" /><path d="M11 6.3333L14.3333 6.3333" /><path d="M12.6667 4.6667L12.6667 8" /> </svg> Invite a member </div> </div> <div class="listbox-group" role="group" aria-labelledby="group-go"> <div class="listbox-label" id="group-go">Go to</div> <div class="listbox-option" role="option" id="cmd-settings"> <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 4.5L4.5 4.5" /><circle cx="6" cy="4.5" r="1.5" /><path d="M7.5 4.5L13.5 4.5" /><path d="M2.5 11.5L8.5 11.5" /><circle cx="10" cy="11.5" r="1.5" /><path d="M11.5 11.5L13.5 11.5" /> </svg> Settings <span class="listbox-end"><kbd class="kbd">G</kbd><kbd class="kbd">S</kbd></span> </div> <div class="listbox-option" role="option" id="cmd-billing"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="1.6667" y="3.3333" width="12.6667" height="9.3333" rx="1" /><path d="M1.6667 6.6667L14.3333 6.6667" /><path d="M4 10L6.6667 10" /> </svg> Billing </div> </div> <div class="listbox-empty">No commands match.</div> </div> <footer class="command-footer"> <span class="command-hint"><kbd class="kbd">↑</kbd><kbd class="kbd">↓</kbd> to move</span> <span class="command-hint"><kbd class="kbd">↵</kbd> to run</span> <span class="command-hint"><kbd class="kbd">Esc</kbd> to close</span> </footer></dialog><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import SearchIcon from '@master/icons-react/native/20/soft/search'import PlusIcon from '@master/icons-react/native/16/soft/plus'import UploadIcon from '@master/icons-react/native/16/soft/upload'import UserPlusIcon from '@master/icons-react/native/16/soft/user-plus'import SlidersIcon from '@master/icons-react/native/16/soft/sliders'import CreditCardIcon from '@master/icons-react/native/16/soft/credit-card'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root}> <button type="button" className="btn" {...{ commandfor: 'palette', command: 'show-modal' }}> Search commands <kbd className="kbd">⌘K</kbd> </button> <dialog id="palette" className="dialog command" closedby="any" aria-label="Command palette"> <label className="command-search"> <SearchIcon className="icon" /> <input className="command-input" id="palette-input" type="search" role="combobox" aria-expanded="true" aria-controls="palette-list" aria-activedescendant="cmd-new" aria-autocomplete="list" aria-label="Search commands" autoComplete="off" spellCheck="false" enterKeyHint="go" placeholder="Type a command or search" autoFocus /> <kbd className="kbd">Esc</kbd> </label> <div className="listbox command-list" id="palette-list" role="listbox" aria-label="Commands"> <div className="listbox-group" role="group" aria-labelledby="group-suggestions"> <div className="listbox-label" id="group-suggestions">Suggestions</div> <div className="listbox-option" role="option" id="cmd-new" 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" id="cmd-deploy"> <UploadIcon className="icon" /> Deploy to production </div> <div className="listbox-option" role="option" id="cmd-invite"> <UserPlusIcon className="icon" /> Invite a member </div> </div> <div className="listbox-group" role="group" aria-labelledby="group-go"> <div className="listbox-label" id="group-go">Go to</div> <div className="listbox-option" role="option" id="cmd-settings"> <SlidersIcon className="icon" /> Settings <span className="listbox-end"> <kbd className="kbd">G</kbd><kbd className="kbd">S</kbd> </span> </div> <div className="listbox-option" role="option" id="cmd-billing"> <CreditCardIcon className="icon" /> Billing </div> </div> <div className="listbox-empty">No commands match.</div> </div> <footer className="command-footer"> <span className="command-hint"> <kbd className="kbd">↑</kbd><kbd className="kbd">↓</kbd> to move </span> <span className="command-hint"><kbd className="kbd">↵</kbd> to run</span> <span className="command-hint"><kbd className="kbd">Esc</kbd> to close</span> </footer> </dialog> </div> )}<script> import { attach } from '@master/ui/behaviors' import SearchIcon from '@master/icons-svelte/native/20/soft/search' import PlusIcon from '@master/icons-svelte/native/16/soft/plus' import UploadIcon from '@master/icons-svelte/native/16/soft/upload' import UserPlusIcon from '@master/icons-svelte/native/16/soft/user-plus' import SlidersIcon from '@master/icons-svelte/native/16/soft/sliders' import CreditCardIcon from '@master/icons-svelte/native/16/soft/credit-card'</script><div {@attach attach}> <button type="button" class="btn" commandfor="palette" command="show-modal"> Search commands <kbd class="kbd">⌘K</kbd> </button> <dialog id="palette" class="dialog command" closedby="any" aria-label="Command palette"> <label class="command-search"> <SearchIcon class="icon" /> <input class="command-input" id="palette-input" type="search" role="combobox" aria-expanded="true" aria-controls="palette-list" aria-activedescendant="cmd-new" aria-autocomplete="list" aria-label="Search commands" autocomplete="off" spellcheck="false" enterkeyhint="go" placeholder="Type a command or search" autofocus> <kbd class="kbd">Esc</kbd> </label> <div class="listbox command-list" id="palette-list" role="listbox" aria-label="Commands"> <div class="listbox-group" role="group" aria-labelledby="group-suggestions"> <div class="listbox-label" id="group-suggestions">Suggestions</div> <div class="listbox-option" role="option" id="cmd-new" 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" id="cmd-deploy"> <UploadIcon class="icon" /> Deploy to production </div> <div class="listbox-option" role="option" id="cmd-invite"> <UserPlusIcon class="icon" /> Invite a member </div> </div> <div class="listbox-group" role="group" aria-labelledby="group-go"> <div class="listbox-label" id="group-go">Go to</div> <div class="listbox-option" role="option" id="cmd-settings"> <SlidersIcon class="icon" /> Settings <span class="listbox-end"><kbd class="kbd">G</kbd><kbd class="kbd">S</kbd></span> </div> <div class="listbox-option" role="option" id="cmd-billing"> <CreditCardIcon class="icon" /> Billing </div> </div> <div class="listbox-empty">No commands match.</div> </div> <footer class="command-footer"> <span class="command-hint"><kbd class="kbd">↑</kbd><kbd class="kbd">↓</kbd> to move</span> <span class="command-hint"><kbd class="kbd">↵</kbd> to run</span> <span class="command-hint"><kbd class="kbd">Esc</kbd> to close</span> </footer> </dialog></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import SearchIcon from '@master/icons-vue/native/20/soft/search'import PlusIcon from '@master/icons-vue/native/16/soft/plus'import UploadIcon from '@master/icons-vue/native/16/soft/upload'import UserPlusIcon from '@master/icons-vue/native/16/soft/user-plus'import SlidersIcon from '@master/icons-vue/native/16/soft/sliders'import CreditCardIcon from '@master/icons-vue/native/16/soft/credit-card'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root"> <button type="button" class="btn" commandfor="palette" command="show-modal"> Search commands <kbd class="kbd">⌘K</kbd> </button> <dialog id="palette" class="dialog command" closedby="any" aria-label="Command palette"> <label class="command-search"> <SearchIcon class="icon" /> <input class="command-input" id="palette-input" type="search" role="combobox" aria-expanded="true" aria-controls="palette-list" aria-activedescendant="cmd-new" aria-autocomplete="list" aria-label="Search commands" autocomplete="off" spellcheck="false" enterkeyhint="go" placeholder="Type a command or search" autofocus> <kbd class="kbd">Esc</kbd> </label> <div class="listbox command-list" id="palette-list" role="listbox" aria-label="Commands"> <div class="listbox-group" role="group" aria-labelledby="group-suggestions"> <div class="listbox-label" id="group-suggestions">Suggestions</div> <div class="listbox-option" role="option" id="cmd-new" 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" id="cmd-deploy"> <UploadIcon class="icon" /> Deploy to production </div> <div class="listbox-option" role="option" id="cmd-invite"> <UserPlusIcon class="icon" /> Invite a member </div> </div> <div class="listbox-group" role="group" aria-labelledby="group-go"> <div class="listbox-label" id="group-go">Go to</div> <div class="listbox-option" role="option" id="cmd-settings"> <SlidersIcon class="icon" /> Settings <span class="listbox-end"><kbd class="kbd">G</kbd><kbd class="kbd">S</kbd></span> </div> <div class="listbox-option" role="option" id="cmd-billing"> <CreditCardIcon class="icon" /> Billing </div> </div> <div class="listbox-empty">No commands match.</div> </div> <footer class="command-footer"> <span class="command-hint"><kbd class="kbd">↑</kbd><kbd class="kbd">↓</kbd> to move</span> <span class="command-hint"><kbd class="kbd">↵</kbd> to run</span> <span class="command-hint"><kbd class="kbd">Esc</kbd> to close</span> </footer> </dialog> </div></template>点击按钮打开面板,或在焦点位于预览中时按 ⌘K 或 Ctrl K 打开。输入文字筛选命令,用方向键移动,按 Enter 执行命令。
#组成部分
command-search是一个<label>,点击整行的任意位置都能让输入框获得焦点。它包含一个icon、command-input和可选的提示。command-input直接以对话框为底,与搜索结果之间仅用一条细线分隔。- 将
command-list加到列表框上。列表框占据剩余高度,并在对话框内滚动。 command-footer包含多组command-hint,每组先用kbd显示按键,再说明其作用。
面板在视口顶部附近打开,并向下扩展。下方列表长度变化时,搜索字段的位置保持固定。
#行为
对话框无需脚本即可打开和关闭。command 行为脚本 为对话框添加命令面板所需的交互。它接收对话框,并返回一个用于解除绑定的函数。
import { command } from '@master/ui/behaviors/command.js'command(document.querySelector('.command'))document.getElementById('cmd-new').addEventListener('click', () => { // Create the project.})- 快捷键。 在文档中的任意位置按 ⌘K 或 Ctrl K 可打开面板,再按一次即可关闭。
- 筛选。 在
command-input中输入时,脚本会给不匹配的选项设置hidden,并将第一个匹配项设为当前选项。列表框 会自行显示空状态,并隐藏其中所有命令都被筛掉的分组。 - 当前选项带有
aria-selected="true",由搜索字段上的aria-activedescendant指定。用方向键切换当前选项。面板打开时就有一个当前选项,因此可以直接按 Enter 执行命令。 - 执行命令。 按 Enter 会触发当前选项的点击,效果与用指针点击相同。链接选项会跳转,选项上的
click监听器也会执行。通过 Enter 或指针触发点击后,面板会关闭。行为脚本仅使用该click事件,不派发自己的事件。
| 选项 | 设置内容 |
|---|---|
shortcut | 与 Command 或 Control 组合使用以打开和关闭面板的按键。默认为 'k',设为 false 可禁用快捷键 |
未绑定行为脚本时,按钮仍可打开对话框,搜索字段仍可输入文字,按 Esc 仍可关闭对话框。输入不会筛选列表,方向键也无法在列表选项间移动。
#搜索结果
搜索文档的面板要显示的不只是名称,还有结果所在的位置,以及匹配的文字。这些都是列表框的组成部分,因此组合框也以同样的方式列出结果,面板无需为它们增加任何类名。
搜索结果
<button type="button" class="btn" commandfor="search" command="show-modal"> Search documentation <kbd class="kbd">⌘K</kbd></button><dialog id="search" class="dialog command" closedby="any" aria-label="Search documentation"> <label class="command-search"> <svg class="icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="8.75" cy="8.75" r="5.75" /><path d="M12.8159 12.8159L17.5 17.5" /> </svg> <input class="command-input" type="search" role="combobox" aria-expanded="true" aria-controls="search-list" aria-activedescendant="hit-retune" aria-autocomplete="list" aria-label="Search documentation" autocomplete="off" spellcheck="false" enterkeyhint="go" placeholder="Search documentation" value="token" autofocus> <kbd class="kbd">Esc</kbd> </label> <div class="listbox command-list" id="search-list" role="listbox" aria-label="Results"> <div class="listbox-group" role="group" aria-labelledby="search-count"> <div class="listbox-label" id="search-count">4 results</div> <div class="listbox-option" role="option" id="hit-retune" 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" id="hit-theme"> <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" id="hit-theming"> <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>Theming</span> <span class="listbox-path"> <span class="listbox-crumb">Guide</span><span class="listbox-crumb">Customizing</span> </span> <span class="listbox-excerpt"> Override <mark class="mark">token</mark>s, define material recipes and build a theme of your own, with Paper as a working example. </span> </span> </div> <div class="listbox-option" role="option" id="hit-customization"> <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 class="listbox-excerpt"> Choose the right layer for a change: utilities, theme <mark class="mark">token</mark>s, component properties, a theme or owning the source. </span> </span> </div> </div> <div class="listbox-empty">No documents match.</div> </div> <footer class="command-footer"> <span class="command-hint"><kbd class="kbd">↑</kbd><kbd class="kbd">↓</kbd> to move</span> <span class="command-hint"><kbd class="kbd">↵</kbd> to open</span> <span class="command-hint"><kbd class="kbd">Esc</kbd> to close</span> </footer></dialog><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import SearchIcon from '@master/icons-react/native/20/soft/search'import HashIcon from '@master/icons-react/native/16/soft/hash'import FileIcon from '@master/icons-react/native/16/soft/file'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root}> <button type="button" className="btn" {...{ commandfor: 'search', command: 'show-modal' }}> Search documentation <kbd className="kbd">⌘K</kbd> </button> <dialog id="search" className="dialog command" closedby="any" aria-label="Search documentation"> <label className="command-search"> <SearchIcon className="icon" /> <input className="command-input" type="search" role="combobox" aria-expanded="true" aria-controls="search-list" aria-activedescendant="hit-retune" aria-autocomplete="list" aria-label="Search documentation" autoComplete="off" spellCheck="false" enterKeyHint="go" placeholder="Search documentation" defaultValue="token" autoFocus /> <kbd className="kbd">Esc</kbd> </label> <div className="listbox command-list" id="search-list" role="listbox" aria-label="Results"> <div className="listbox-group" role="group" aria-labelledby="search-count"> <div className="listbox-label" id="search-count">4 results</div> <div className="listbox-option" role="option" id="hit-retune" 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" id="hit-theme"> <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" id="hit-theming"> <FileIcon className="icon" /> <span className="listbox-body"> <span>Theming</span> <span className="listbox-path"> <span className="listbox-crumb">Guide</span><span className="listbox-crumb">Customizing</span> </span> <span className="listbox-excerpt"> Override <mark className="mark">token</mark>s, define material recipes and build a theme of your own, with Paper as a working example. </span> </span> </div> <div className="listbox-option" role="option" id="hit-customization"> <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 className="listbox-excerpt"> Choose the right layer for a change: utilities, theme <mark className="mark">token</mark>s, component properties, a theme or owning the source. </span> </span> </div> </div> <div className="listbox-empty">No documents match.</div> </div> <footer className="command-footer"> <span className="command-hint"> <kbd className="kbd">↑</kbd><kbd className="kbd">↓</kbd> to move </span> <span className="command-hint"><kbd className="kbd">↵</kbd> to open</span> <span className="command-hint"><kbd className="kbd">Esc</kbd> to close</span> </footer> </dialog> </div> )}<script> import { attach } from '@master/ui/behaviors' import SearchIcon from '@master/icons-svelte/native/20/soft/search' import HashIcon from '@master/icons-svelte/native/16/soft/hash' import FileIcon from '@master/icons-svelte/native/16/soft/file'</script><div {@attach attach}> <button type="button" class="btn" commandfor="search" command="show-modal"> Search documentation <kbd class="kbd">⌘K</kbd> </button> <dialog id="search" class="dialog command" closedby="any" aria-label="Search documentation"> <label class="command-search"> <SearchIcon class="icon" /> <input class="command-input" type="search" role="combobox" aria-expanded="true" aria-controls="search-list" aria-activedescendant="hit-retune" aria-autocomplete="list" aria-label="Search documentation" autocomplete="off" spellcheck="false" enterkeyhint="go" placeholder="Search documentation" value="token" autofocus> <kbd class="kbd">Esc</kbd> </label> <div class="listbox command-list" id="search-list" role="listbox" aria-label="Results"> <div class="listbox-group" role="group" aria-labelledby="search-count"> <div class="listbox-label" id="search-count">4 results</div> <div class="listbox-option" role="option" id="hit-retune" 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" id="hit-theme"> <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" id="hit-theming"> <FileIcon class="icon" /> <span class="listbox-body"> <span>Theming</span> <span class="listbox-path"> <span class="listbox-crumb">Guide</span><span class="listbox-crumb">Customizing</span> </span> <span class="listbox-excerpt"> Override <mark class="mark">token</mark>s, define material recipes and build a theme of your own, with Paper as a working example. </span> </span> </div> <div class="listbox-option" role="option" id="hit-customization"> <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 class="listbox-excerpt"> Choose the right layer for a change: utilities, theme <mark class="mark">token</mark>s, component properties, a theme or owning the source. </span> </span> </div> </div> <div class="listbox-empty">No documents match.</div> </div> <footer class="command-footer"> <span class="command-hint"><kbd class="kbd">↑</kbd><kbd class="kbd">↓</kbd> to move</span> <span class="command-hint"><kbd class="kbd">↵</kbd> to open</span> <span class="command-hint"><kbd class="kbd">Esc</kbd> to close</span> </footer> </dialog></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import SearchIcon from '@master/icons-vue/native/20/soft/search'import HashIcon from '@master/icons-vue/native/16/soft/hash'import FileIcon from '@master/icons-vue/native/16/soft/file'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root"> <button type="button" class="btn" commandfor="search" command="show-modal"> Search documentation <kbd class="kbd">⌘K</kbd> </button> <dialog id="search" class="dialog command" closedby="any" aria-label="Search documentation"> <label class="command-search"> <SearchIcon class="icon" /> <input class="command-input" type="search" role="combobox" aria-expanded="true" aria-controls="search-list" aria-activedescendant="hit-retune" aria-autocomplete="list" aria-label="Search documentation" autocomplete="off" spellcheck="false" enterkeyhint="go" placeholder="Search documentation" value="token" autofocus> <kbd class="kbd">Esc</kbd> </label> <div class="listbox command-list" id="search-list" role="listbox" aria-label="Results"> <div class="listbox-group" role="group" aria-labelledby="search-count"> <div class="listbox-label" id="search-count">4 results</div> <div class="listbox-option" role="option" id="hit-retune" 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" id="hit-theme"> <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" id="hit-theming"> <FileIcon class="icon" /> <span class="listbox-body"> <span>Theming</span> <span class="listbox-path"> <span class="listbox-crumb">Guide</span><span class="listbox-crumb">Customizing</span> </span> <span class="listbox-excerpt"> Override <mark class="mark">token</mark>s, define material recipes and build a theme of your own, with Paper as a working example. </span> </span> </div> <div class="listbox-option" role="option" id="hit-customization"> <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 class="listbox-excerpt"> Choose the right layer for a change: utilities, theme <mark class="mark">token</mark>s, component properties, a theme or owning the source. </span> </span> </div> </div> <div class="listbox-empty">No documents match.</div> </div> <footer class="command-footer"> <span class="command-hint"><kbd class="kbd">↑</kbd><kbd class="kbd">↓</kbd> to move</span> <span class="command-hint"><kbd class="kbd">↵</kbd> to open</span> <span class="command-hint"><kbd class="kbd">Esc</kbd> to close</span> </footer> </dialog> </div></template>搜索 token 的结果。匹配的文字与结果一样由搜索写入:行为只负责筛选列表中已有的选项。
- 图标说明结果的种类。 页面使用文档图标,页面内的标题使用井号。没有专属图标的结果从这两者中取其一,让所有标题对齐同一条边。
listbox-path说明结果的位置。 它是标题下方的一行小字,每一级各用一个listbox-crumb。这些层级不是链接,因为一个选项只有一个目标,所以它不是面包屑导航。listbox-excerpt是匹配的段落,最多两行。有了它,图标会与标题对齐,而不是位于整行的中央。- 匹配的文字使用
mark。 在选项中它会采用标题的文字颜色,因此在弱化的文字行中也能找到。只标记输入的文字,不要标记行内的其他内容。
#键盘操作
| 按键 | 操作 |
|---|---|
| Meta+K, Control+K | 使用行为脚本时,可从任意位置打开命令面板,已打开时则将其关闭 |
| Typing | 使用行为脚本时,隐藏不匹配的选项,并将首个匹配项设为活动选项 |
| ArrowDown, ArrowUp | 使用行为脚本时,切换活动选项,跳过禁用项,并在首尾之间循环。 |
| Home, End | 使用行为脚本时,移到第一个或最后一个选项 |
| Enter | 使用行为脚本时,执行活动命令并关闭命令面板 |
| Escape | 像原生 dialog 一样,关闭命令面板并恢复原来的焦点 |
#无障碍
- 用
aria-label为对话框提供无障碍名称。在输入框上设置autofocus,打开后即可直接输入。 - 输入框是一个控制列表框的组合框,此处的列表框始终展开。
- 命令面板提供快捷入口,其中每条命令都必须能通过其他方式访问。
#React、Svelte 和 Vue
command 组件根据命令列表渲染面板,绑定行为脚本,并在面板打开和关闭时通知应用。用 npx master-ui add command 将组件复制到项目中。React、Svelte 和 Vue 介绍了这些组件的实现和用法。
#类名
| 类名 | 说明 |
|---|---|
command | 在同一个 dialog 元素上与 dialog 类搭配使用。命令面板内部无内边距,宽 36rem,靠近视口顶部放置,使列表长度变化时输入框仍停在原位。 |
command-search | 将此类用于字段行的 label,使点击整行都能聚焦输入元素。行内包含图标、command-input 和可选提示 |
command-input | 搜索输入框,设置 role="combobox",并让 aria-controls 指向列表。输入框没有独立背景,直接使用对话框的背景。 |
command-list | 用于显示搜索结果的列表框,占据剩余高度并在对话框内滚动。 |
command-footer | 列表下方的一行按键提示 |
command-hint | 页脚中的一条提示,先写按键,再写其作用 |