ナビゲーション

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 は Interest Invoker に対応しています。未対応のブラウザーではツールチップは表示されず、コントロールのアクセシブルネームは維持されます。未対応のブラウザーでも表示するには、ポインターとフォーカスのイベントハンドラーから 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>

#クラス

クラス説明
tooltippopover="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;    }  }}