浮层
工具提示
在墨色表面上显示的简短标签,由悬停、聚焦或长按触发。
本页内容
工具提示是在墨色表面上显示的简短标签,用于标明控件的名称或快捷键。它属于提示型弹出层。在标签上设置 popover="hint" 和 .tooltip,在控件上设置 interestfor。鼠标悬停或通过键盘聚焦时,浏览器会短暂延迟后显示工具提示。在触摸屏上,长按即可显示。按 Esc 可隐藏工具提示,已打开的菜单会保持打开。CSS 锚点定位将工具提示水平居中放在控件上方。
工具提示
<button type="button" class="btn btn-square" interestfor="tip-search" aria-label="Search"> <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></button><button type="button" class="btn btn-square" interestfor="tip-filter" aria-label="Filter"> <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.5365 3L11.4635 3A1 1 0 0 1 12.2548 4.6114L9.4587 8.2299A1 1 0 0 0 9.25 8.8413L9.25 11.323A1 1 0 0 1 8.6214 12.2514L7.875 12.55A0.8203 0.8203 0 0 1 6.75 11.7883L6.75 8.8413A1 1 0 0 0 6.5413 8.2299L3.7452 4.6114A1 1 0 0 1 4.5365 3Z" /> </svg></button><div id="tip-search" popover="hint" class="tooltip">Search <kbd class="kbd">/</kbd></div><div id="tip-filter" popover="hint" class="tooltip">Filter <kbd class="kbd">F</kbd></div>import SearchIcon from '@master/icons-react/native/16/soft/search'import FilterIcon from '@master/icons-react/native/16/soft/filter'<> <button type="button" className="btn btn-square" {...{ interestfor: 'tip-search' }} aria-label="Search"> <SearchIcon className="icon" /> </button> <button type="button" className="btn btn-square" {...{ interestfor: 'tip-filter' }} aria-label="Filter"> <FilterIcon className="icon" /> </button> <div id="tip-search" popover="hint" className="tooltip">Search <kbd className="kbd">/</kbd></div> <div id="tip-filter" popover="hint" className="tooltip">Filter <kbd className="kbd">F</kbd></div></><script> import SearchIcon from '@master/icons-svelte/native/16/soft/search' import FilterIcon from '@master/icons-svelte/native/16/soft/filter'</script><button type="button" class="btn btn-square" interestfor="tip-search" aria-label="Search"> <SearchIcon class="icon" /></button><button type="button" class="btn btn-square" interestfor="tip-filter" aria-label="Filter"> <FilterIcon class="icon" /></button><div id="tip-search" popover="hint" class="tooltip">Search <kbd class="kbd">/</kbd></div><div id="tip-filter" popover="hint" class="tooltip">Filter <kbd class="kbd">F</kbd></div><script setup>import SearchIcon from '@master/icons-vue/native/16/soft/search'import FilterIcon from '@master/icons-vue/native/16/soft/filter'</script><template> <button type="button" class="btn btn-square" interestfor="tip-search" aria-label="Search"> <SearchIcon class="icon" /> </button> <button type="button" class="btn btn-square" interestfor="tip-filter" aria-label="Filter"> <FilterIcon class="icon" /> </button> <div id="tip-search" popover="hint" class="tooltip">Search <kbd class="kbd">/</kbd></div> <div id="tip-filter" popover="hint" class="tooltip">Filter <kbd class="kbd">F</kbd></div></template>将鼠标悬停在按钮上,或按 Tab 将焦点移到按钮上。
#位置
工具提示位于控件上方 6px 处,上方空间不足时会翻转到下方。对于顶部栏中的控件,可以用 tooltip-bottom 优先将工具提示放在下方。
下方
<button type="button" class="btn" interestfor="tip-deploy">Deploy</button><div id="tip-deploy" popover="hint" class="tooltip tooltip-bottom"> Ships the main branch to production</div><> <button type="button" className="btn" {...{ interestfor: 'tip-deploy' }}>Deploy</button> <div id="tip-deploy" popover="hint" className="tooltip tooltip-bottom"> Ships the main branch to production </div></><button type="button" class="btn" interestfor="tip-deploy">Deploy</button><div id="tip-deploy" popover="hint" class="tooltip tooltip-bottom"> Ships the main branch to production</div><template> <button type="button" class="btn" interestfor="tip-deploy">Deploy</button> <div id="tip-deploy" popover="hint" class="tooltip tooltip-bottom"> Ships the main branch to production </div></template>#内容
工具提示用几个词即可。文本在 16rem 的宽度限制下自动换行,并尽量让各行长度均衡。工具提示会调整按键标记使用的令牌,因此其中的 kbd 无需指定变体,就能在墨色表面上清晰显示。链接、表单等需要读者操作的内容应放在弹出层中。
#无障碍
- 控件需要有自己的名称,工具提示用于补充说明。纯图标按钮应保留
aria-label,因为在触摸屏上只有长按才会显示工具提示。 - 浏览器会将工具提示与对应控件关联起来,辅助技术会将其作为控件的描述读出。
- Chrome 和 Edge 已支持通过 interestfor 触发提示。缺少这项支持时,浏览器会保留控件的无障碍名称,工具提示则不会显示。要在这些浏览器中也显示工具提示,请在指针和焦点事件处理函数中调用
showPopover()和hidePopover(),并沿用同一份样式表。
#React、Svelte 和 Vue
tooltip 组件通过 id 将控件与工具提示关联起来。控件由页面自身的标记定义,工具提示由浏览器显示。使用 npx master-ui add tooltip 将组件复制到项目中。组件的实现和用法见 React、Svelte 和 Vue。
#类名
| 类名 | 说明 |
|---|---|
tooltip | 标签放在带 popover="hint" 的元素上。缺少 popover 属性时,标签始终不显示。标签在控件上方居中,间距为 --anchored-gap(6px),上方空间不足时翻转到下方,达到 16rem 时换行。内部的 kbd 无需修饰类即可在墨色表面上清晰显示。 |
tooltip-bottom | 优先放在控件下方,适用于顶栏中的控件与 tooltip 一同使用 |
#自定义属性
| 属性 | 默认值 | 说明 |
|---|---|---|
--anchored-gap | 浮层与触发控件之间的间距,未设置时为 6px。在 tooltip 上设置 |