網站導覽

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 寬時會換行,並平衡各行的長度。工具提示會調整按鍵標示使用的 token,因此內部的 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;    }  }}