导航

v2.1.0
浮层

工具提示

在墨色表面上显示的简短标签,由悬停、聚焦或长按触发。

本页内容

工具提示是在墨色表面上显示的简短标签,用于标明控件的名称或快捷键。它属于提示型弹出层。在标签上设置 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。

ui/tooltip.tsx
import { useId, type ComponentProps, type ReactNode } from 'react'export interface TooltipProps extends Omit<ComponentProps<'div'>, 'popover'> {  /** The control the tooltip names: spread what it is given on a button or a link of your own. */  trigger: (attributes: { interestfor: string }) => ReactNode}/** * A tooltip. The browser shows it on hover and on focus and places it; this component only joins the * control to its label. The children are the label. The control keeps its own accessible name. */export function Tooltip({ trigger, className, id, children, ...root }: TooltipProps) {  const generated = useId()  const name = id ?? generated  return (    <>      {trigger({ interestfor: name })}      <div id={name} popover="hint" className={className ? `tooltip ${className}` : 'tooltip'} {...root}>{children}</div>    </>  )}
ui/Tooltip.svelte
<script lang="ts" module>  import type { Snippet } from 'svelte'  import type { HTMLAttributes } from 'svelte/elements'  export interface TooltipProps extends Omit<HTMLAttributes<HTMLDivElement>, 'popover'> {    /** The control the tooltip names: spread what it is given on a button or a link of your own. */    trigger: Snippet<[{ interestfor: string }]>  }</script><script lang="ts">  let { trigger, class: className, id, children, ...root }: TooltipProps = $props()  const generated = $props.id()  const name = $derived(id ?? generated)</script><!--  @component  A tooltip. The browser shows it on hover and on focus and places it; this component only joins the  control to its label. The children are the label. The control keeps its own accessible name.-->{@render trigger({ interestfor: name })}<div id={name} popover="hint" class={['tooltip', className]} {...root}>{@render children?.()}</div>
ui/Tooltip.vue
<script setup lang="ts">import { computed, useId } from 'vue'defineOptions({ inheritAttrs: false })const { id } = defineProps<{  id?: string}>()defineSlots<{  /** The control the tooltip names: bind what it is given on a button or a link of your own. */  trigger: (attributes: { interestfor: string }) => unknown  default?: () => unknown}>()/* * A tooltip. The browser shows it on hover and on focus and places it; this component only joins the * control to its label. The default slot is the label. The control keeps its own accessible name. */const generated = useId()const name = computed(() => id ?? generated)</script><template>  <slot name="trigger" v-bind="{ interestfor: name }" />  <div :id="name" popover="hint" class="tooltip" v-bind="$attrs"><slot /></div></template>

#类名

类名说明
tooltip标签放在带 popover="hint" 的元素上。缺少 popover 属性时,标签始终不显示。标签在控件上方居中,间距为 --anchored-gap(6px),上方空间不足时翻转到下方,达到 16rem 时换行。内部的 kbd 无需修饰类即可在墨色表面上清晰显示。
tooltip-bottom优先放在控件下方,适用于顶栏中的控件与 tooltip 一同使用

#自定义属性

属性默认值说明
--anchored-gap浮层与触发控件之间的间距,未设置时为 6px。在 tooltip 上设置

#源码

@master/ui/components/tooltip.css
/* * Tooltip: a short label in ink, shown when a control is hovered, focused or long-pressed. * It is a hint popover: popover="hint" on the tooltip and interestfor on the control, so the browser handles * the delay, the keys and touch, and it never closes a menu that is already open. Anchor positioning centers * it above the control and flips it below when there is no room. */@layer components.ui {  .tooltip {    @apply --on-ink;    /* What it holds and cuts a ring out of it (a citation's fill) reads the tip's own face, not the page's. */    --surface-color: var(--color-surface-tip);    --anchored-block: block-start;    --anchored-inline: span-all;    /*     * It appears often and under the pointer, so it arrives quicker than a popover and from nearer its size, and     * leaves quicker still.     */    --pop-scale: .985;    --pop-enter: calc(var(--duration-quick) * .75);    --pop-leave: calc(var(--duration-quick) / 2);    overflow-wrap: anywhere;    width: max-content;    max-width: min(16rem, calc(100vw - 1rem));    padding: .3125rem .5rem;    border: 0;    border-radius: calc((var(--radius-chip) + 1px) * var(--corner-scale));    corner-shape: var(--corner-shape);    overflow: visible;    background: var(--color-surface-tip);    color: light-dark(var(--color-text-inverse), var(--color-text-strong));    box-shadow: var(--shadow-tip);    font: 400 .75rem/1.35 var(--font-family-sans);    font-feature-settings: inherit;    text-align: center;    text-wrap: balance;    transform-origin: bottom;    @apply --anchored;    @apply --pop;    /* Prefer the space below the control. */    &.tooltip-bottom {      --anchored-block: block-end;      transform-origin: top;    }    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;    }  }}