导航

v2.1.0
浮层

引导提示

功能导览中的一个步骤,指向它所说明的控件。

本页内容

引导提示是功能导览中的一个步骤,用一张采用墨色色调的小卡片指向控件并说明其用途。它采用弹出层,浏览器将其置于顶层并把焦点移入其中。CSS 锚点定位将卡片放在打开它的元素旁,并让箭头指向该元素。信标是一个不时向外扩散圆环的墨色圆点,既标出页面上的位置,也是打开引导提示的按钮。功能导览通过 tour 行为依次打开多个引导提示。

引导提示

<div class="toolbar" role="toolbar" aria-label="Deployments">  <button type="button" class="btn btn-quiet btn-sm btn-square" 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>  <span class="relative inline-flex">    <button type="button" class="btn btn-quiet btn-sm btn-square" 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>    <button type="button" class="coachmark-beacon absolute top(-3) right(-3)" popovertarget="tip-filter" aria-label="What is new in Filter"></button>    <div id="tip-filter" popover class="coachmark coachmark-top" role="dialog" aria-labelledby="tip-filter-title">      <p class="coachmark-count">New</p>      <h2 class="coachmark-title" id="tip-filter-title">Filter by author</h2>      <p class="coachmark-text">        Narrow the deployments to the ones you shipped, or to anyone on the team.      </p>      <div class="coachmark-actions">        <button type="button" class="btn btn-sm" popovertarget="tip-filter" popovertargetaction="hide" autofocus>          Got it        </button>      </div>    </div>  </span>  <button type="button" class="btn btn-quiet btn-sm btn-square" aria-label="Share">    <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="3.6667" cy="8" r="1.6667" /><circle cx="12" cy="3.3333" r="1.6667" /><circle cx="12" cy="12.6667" r="1.6667" /><path d="M5.12 7.1867L10.5467 4.1467" /><path d="M5.12 8.8133L10.5467 11.8533" />    </svg>  </button></div>
import SearchIcon from '@master/icons-react/native/16/soft/search'import FilterIcon from '@master/icons-react/native/16/soft/filter'import ShareIcon from '@master/icons-react/native/16/soft/share'<div className="toolbar" role="toolbar" aria-label="Deployments">  <button type="button" className="btn btn-quiet btn-sm btn-square" aria-label="Search">    <SearchIcon className="icon" />  </button>  <span className="relative inline-flex">    <button type="button" className="btn btn-quiet btn-sm btn-square" aria-label="Filter">      <FilterIcon className="icon" />    </button>    <button type="button" className="coachmark-beacon absolute top(-3) right(-3)" popoverTarget="tip-filter" aria-label="What is new in Filter"></button>    <div id="tip-filter" popover="" className="coachmark coachmark-top" role="dialog" aria-labelledby="tip-filter-title">      <p className="coachmark-count">New</p>      <h2 className="coachmark-title" id="tip-filter-title">Filter by author</h2>      <p className="coachmark-text">        Narrow the deployments to the ones you shipped, or to anyone on the team.      </p>      <div className="coachmark-actions">        <button type="button" className="btn btn-sm" popoverTarget="tip-filter" popoverTargetAction="hide" autoFocus>          Got it        </button>      </div>    </div>  </span>  <button type="button" className="btn btn-quiet btn-sm btn-square" aria-label="Share">    <ShareIcon className="icon" />  </button></div>
<script>  import SearchIcon from '@master/icons-svelte/native/16/soft/search'  import FilterIcon from '@master/icons-svelte/native/16/soft/filter'  import ShareIcon from '@master/icons-svelte/native/16/soft/share'</script><div class="toolbar" role="toolbar" aria-label="Deployments">  <button type="button" class="btn btn-quiet btn-sm btn-square" aria-label="Search">    <SearchIcon class="icon" />  </button>  <span class="relative inline-flex">    <button type="button" class="btn btn-quiet btn-sm btn-square" aria-label="Filter">      <FilterIcon class="icon" />    </button>    <button type="button" class="coachmark-beacon absolute top(-3) right(-3)" popovertarget="tip-filter" aria-label="What is new in Filter"></button>    <div id="tip-filter" popover class="coachmark coachmark-top" role="dialog" aria-labelledby="tip-filter-title">      <p class="coachmark-count">New</p>      <h2 class="coachmark-title" id="tip-filter-title">Filter by author</h2>      <p class="coachmark-text">        Narrow the deployments to the ones you shipped, or to anyone on the team.      </p>      <div class="coachmark-actions">        <button type="button" class="btn btn-sm" popovertarget="tip-filter" popovertargetaction="hide" autofocus>          Got it        </button>      </div>    </div>  </span>  <button type="button" class="btn btn-quiet btn-sm btn-square" aria-label="Share">    <ShareIcon class="icon" />  </button></div>
<script setup>import SearchIcon from '@master/icons-vue/native/16/soft/search'import FilterIcon from '@master/icons-vue/native/16/soft/filter'import ShareIcon from '@master/icons-vue/native/16/soft/share'</script><template>  <div class="toolbar" role="toolbar" aria-label="Deployments">    <button type="button" class="btn btn-quiet btn-sm btn-square" aria-label="Search">      <SearchIcon class="icon" />    </button>    <span class="relative inline-flex">      <button type="button" class="btn btn-quiet btn-sm btn-square" aria-label="Filter">        <FilterIcon class="icon" />      </button>      <button type="button" class="coachmark-beacon absolute top(-3) right(-3)" popovertarget="tip-filter" aria-label="What is new in Filter"></button>      <div id="tip-filter" popover class="coachmark coachmark-top" role="dialog" aria-labelledby="tip-filter-title">        <p class="coachmark-count">New</p>        <h2 class="coachmark-title" id="tip-filter-title">Filter by author</h2>        <p class="coachmark-text">          Narrow the deployments to the ones you shipped, or to anyone on the team.        </p>        <div class="coachmark-actions">          <button type="button" class="btn btn-sm" popovertarget="tip-filter" popovertargetaction="hide" autofocus>            Got it          </button>        </div>      </div>    </span>    <button type="button" class="btn btn-quiet btn-sm btn-square" aria-label="Share">      <ShareIcon class="icon" />    </button>  </div></template>
按下筛选按钮上的圆点。

卡片在 moonstone 中使用墨色前景,在 obsidian 中使用珍珠色前景。它采用 sheet-inverse 的平坦反色表面,与普通弹出层的面板及下方页面区分开来,箭头与卡片无缝衔接。卡片上的默认 按钮 突出操作,低强调按钮沿用卡片的前景色,指针悬停时以同一颜色淡淡着色。

#组成

标题必填,其余部分可选。

类名内容
coachmark-count用较低对比度的前景色显示当前导览进度,例如“2 of 4”,也可显示一个词,例如“New”
coachmark-title步骤标题。为标题设置 id,并用它为引导提示提供无障碍名称
coachmark-text用一两句话说明控件的用途
coachmark-actions沿卡片底部排列的小按钮。有多个按钮时,第一个放在起始侧,例如“Skip”,其余按钮排列在末尾侧,例如“Back”和“Next”

操作按钮距卡片边缘 12px。卡片的圆角半径等于小按钮的圆角半径加上这段内距,让两者的圆角相互贴合。

#位置

引导提示默认在触发元素下方居中打开,箭头尖端距该元素 6px。下方空间不足时,卡片翻到上方。coachmark-top 优先放在上方,适用于靠近屏幕底部的控件,或信标位于控件上方一角的情况,如第一个示例所示。

箭头指向标记中紧挨在引导提示之前的元素,因此应把引导提示紧接在信标或它所说明的控件之后;在功能导览中,箭头指向导览的聚光区域,它位于该步骤的目标之上。引导提示打开时,该元素会被设为具名锚点,箭头对准它的中心。视口边缘把卡片推向一侧时,箭头仍指向锚点中心,同时避开卡片圆角。卡片翻转时,箭头移到另一侧边缘。和导览中一样,每次只打开一个引导提示。

#信标

coachmark-beacon 是按钮上的一个 10px 圆点,外圈与所在表面同色。其点击目标为 24px,在触摸屏上为 44px。它通过 popovertarget 打开对应的引导提示,并需要用 aria-label 说明会打开什么。信标由页面定位。可在 relative 包裹元素内使用 absolute 工具类,把信标放在所标记控件的上方一角,也可将它与控件并排放在同一行。

扩散的圆环用来吸引注意。指针悬停在信标上、信标获得焦点、对应引导提示打开,或启用减少动态效果时,圆环会停留为静态光环。读者看过该步骤后,移除信标。

#功能导览

功能导览依次打开几个引导提示,每个都显示在它所说明的控件旁。将各步骤按顺序放进 coachmark-tour,并为每一步设置 popover="manual",这样,点击页面(例如试用当前步骤指向的控件)也不会结束导览。步骤通过 data-target 中的选择器指定目标;未指定时,它指向标记中紧挨在它之前的元素。步骤中的按钮用 value 说明其作用:next、back、skip,最后一步则用 done。

其余工作由 tour 行为完成。带有 command="--start" 且 commandfor 指向该导览的按钮会启动导览;该行为通过 showPopover({ source }) 将每一步打开在其目标旁,由浏览器将卡片锚定在那里、由 CSS 定位,并将焦点移入卡片中的“Next”或“Done”。导览中的 coachmark-spotlight 会先打开并覆盖目标:无论步骤在标记中的哪个位置,其箭头都指向它。

功能导览

<div class="coachmark-tour flex items-center gap-sm" id="deploys-tour">  <button type="button" class="btn btn-sm" commandfor="deploys-tour" command="--start">    Take the tour  </button>  <div class="toolbar" role="toolbar" aria-label="Deployments">    <button type="button" class="btn btn-quiet btn-sm btn-square" id="deploys-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-quiet btn-sm btn-square" id="deploys-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>    <button type="button" class="btn btn-quiet btn-sm btn-square" id="deploys-share" aria-label="Share">      <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="3.6667" cy="8" r="1.6667" /><circle cx="12" cy="3.3333" r="1.6667" /><circle cx="12" cy="12.6667" r="1.6667" /><path d="M5.12 7.1867L10.5467 4.1467" /><path d="M5.12 8.8133L10.5467 11.8533" />      </svg>    </button>  </div>  <div class="coachmark-spotlight" popover="manual"></div>  <div class="coachmark" popover="manual" role="dialog" aria-labelledby="deploys-search-title" data-target="#deploys-search">    <p class="coachmark-count">1 of 3</p>    <h2 class="coachmark-title" id="deploys-search-title">Search every deployment</h2>    <p class="coachmark-text">Find one by its branch, its commit or the person who shipped it.</p>    <div class="coachmark-actions">      <button type="button" class="btn btn-sm btn-quiet" value="skip">Skip</button>      <button type="button" class="btn btn-sm" value="next">Next</button>    </div>  </div>  <div class="coachmark" popover="manual" role="dialog" aria-labelledby="deploys-filter-title" data-target="#deploys-filter">    <p class="coachmark-count">2 of 3</p>    <h2 class="coachmark-title" id="deploys-filter-title">Filter the list</h2>    <p class="coachmark-text">Keep only the deployments of one branch, one author or one status.</p>    <div class="coachmark-actions">      <button type="button" class="btn btn-sm btn-quiet" value="skip">Skip</button>      <button type="button" class="btn btn-sm btn-quiet" value="back">Back</button>      <button type="button" class="btn btn-sm" value="next">Next</button>    </div>  </div>  <div class="coachmark" popover="manual" role="dialog" aria-labelledby="deploys-share-title" data-target="#deploys-share">    <p class="coachmark-count">3 of 3</p>    <h2 class="coachmark-title" id="deploys-share-title">Share a deployment</h2>    <p class="coachmark-text">Copy a link to it, open for anyone on the team.</p>    <div class="coachmark-actions">      <button type="button" class="btn btn-sm btn-quiet" value="back">Back</button>      <button type="button" class="btn btn-sm" value="done">Done</button>    </div>  </div></div><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/16/soft/search'import FilterIcon from '@master/icons-react/native/16/soft/filter'import ShareIcon from '@master/icons-react/native/16/soft/share'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => attach(root.current!), [])  return (    <div ref={root} className="coachmark-tour flex items-center gap-sm" id="deploys-tour">      <button type="button" className="btn btn-sm" {...{ commandfor: 'deploys-tour', command: '--start' }}>        Take the tour      </button>      <div className="toolbar" role="toolbar" aria-label="Deployments">        <button type="button" className="btn btn-quiet btn-sm btn-square" id="deploys-search" aria-label="Search">          <SearchIcon className="icon" />        </button>        <button type="button" className="btn btn-quiet btn-sm btn-square" id="deploys-filter" aria-label="Filter">          <FilterIcon className="icon" />        </button>        <button type="button" className="btn btn-quiet btn-sm btn-square" id="deploys-share" aria-label="Share">          <ShareIcon className="icon" />        </button>      </div>      <div className="coachmark-spotlight" popover="manual"></div>      <div className="coachmark" popover="manual" role="dialog" aria-labelledby="deploys-search-title" data-target="#deploys-search">        <p className="coachmark-count">1 of 3</p>        <h2 className="coachmark-title" id="deploys-search-title">Search every deployment</h2>        <p className="coachmark-text">          Find one by its branch, its commit or the person who shipped it.        </p>        <div className="coachmark-actions">          <button type="button" className="btn btn-sm btn-quiet" value="skip">Skip</button>          <button type="button" className="btn btn-sm" value="next">Next</button>        </div>      </div>      <div className="coachmark" popover="manual" role="dialog" aria-labelledby="deploys-filter-title" data-target="#deploys-filter">        <p className="coachmark-count">2 of 3</p>        <h2 className="coachmark-title" id="deploys-filter-title">Filter the list</h2>        <p className="coachmark-text">          Keep only the deployments of one branch, one author or one status.        </p>        <div className="coachmark-actions">          <button type="button" className="btn btn-sm btn-quiet" value="skip">Skip</button>          <button type="button" className="btn btn-sm btn-quiet" value="back">Back</button>          <button type="button" className="btn btn-sm" value="next">Next</button>        </div>      </div>      <div className="coachmark" popover="manual" role="dialog" aria-labelledby="deploys-share-title" data-target="#deploys-share">        <p className="coachmark-count">3 of 3</p>        <h2 className="coachmark-title" id="deploys-share-title">Share a deployment</h2>        <p className="coachmark-text">Copy a link to it, open for anyone on the team.</p>        <div className="coachmark-actions">          <button type="button" className="btn btn-sm btn-quiet" value="back">Back</button>          <button type="button" className="btn btn-sm" value="done">Done</button>        </div>      </div>    </div>  )}
<script>  import { attach } from '@master/ui/behaviors'  import SearchIcon from '@master/icons-svelte/native/16/soft/search'  import FilterIcon from '@master/icons-svelte/native/16/soft/filter'  import ShareIcon from '@master/icons-svelte/native/16/soft/share'</script><div class="coachmark-tour flex items-center gap-sm" id="deploys-tour" {@attach attach}>  <button type="button" class="btn btn-sm" commandfor="deploys-tour" command="--start">    Take the tour  </button>  <div class="toolbar" role="toolbar" aria-label="Deployments">    <button type="button" class="btn btn-quiet btn-sm btn-square" id="deploys-search" aria-label="Search">      <SearchIcon class="icon" />    </button>    <button type="button" class="btn btn-quiet btn-sm btn-square" id="deploys-filter" aria-label="Filter">      <FilterIcon class="icon" />    </button>    <button type="button" class="btn btn-quiet btn-sm btn-square" id="deploys-share" aria-label="Share">      <ShareIcon class="icon" />    </button>  </div>  <div class="coachmark-spotlight" popover="manual"></div>  <div class="coachmark" popover="manual" role="dialog" aria-labelledby="deploys-search-title" data-target="#deploys-search">    <p class="coachmark-count">1 of 3</p>    <h2 class="coachmark-title" id="deploys-search-title">Search every deployment</h2>    <p class="coachmark-text">Find one by its branch, its commit or the person who shipped it.</p>    <div class="coachmark-actions">      <button type="button" class="btn btn-sm btn-quiet" value="skip">Skip</button>      <button type="button" class="btn btn-sm" value="next">Next</button>    </div>  </div>  <div class="coachmark" popover="manual" role="dialog" aria-labelledby="deploys-filter-title" data-target="#deploys-filter">    <p class="coachmark-count">2 of 3</p>    <h2 class="coachmark-title" id="deploys-filter-title">Filter the list</h2>    <p class="coachmark-text">Keep only the deployments of one branch, one author or one status.</p>    <div class="coachmark-actions">      <button type="button" class="btn btn-sm btn-quiet" value="skip">Skip</button>      <button type="button" class="btn btn-sm btn-quiet" value="back">Back</button>      <button type="button" class="btn btn-sm" value="next">Next</button>    </div>  </div>  <div class="coachmark" popover="manual" role="dialog" aria-labelledby="deploys-share-title" data-target="#deploys-share">    <p class="coachmark-count">3 of 3</p>    <h2 class="coachmark-title" id="deploys-share-title">Share a deployment</h2>    <p class="coachmark-text">Copy a link to it, open for anyone on the team.</p>    <div class="coachmark-actions">      <button type="button" class="btn btn-sm btn-quiet" value="back">Back</button>      <button type="button" class="btn btn-sm" value="done">Done</button>    </div>  </div></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import SearchIcon from '@master/icons-vue/native/16/soft/search'import FilterIcon from '@master/icons-vue/native/16/soft/filter'import ShareIcon from '@master/icons-vue/native/16/soft/share'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <div ref="root" class="coachmark-tour flex items-center gap-sm" id="deploys-tour">    <button type="button" class="btn btn-sm" commandfor="deploys-tour" command="--start">      Take the tour    </button>    <div class="toolbar" role="toolbar" aria-label="Deployments">      <button type="button" class="btn btn-quiet btn-sm btn-square" id="deploys-search" aria-label="Search">        <SearchIcon class="icon" />      </button>      <button type="button" class="btn btn-quiet btn-sm btn-square" id="deploys-filter" aria-label="Filter">        <FilterIcon class="icon" />      </button>      <button type="button" class="btn btn-quiet btn-sm btn-square" id="deploys-share" aria-label="Share">        <ShareIcon class="icon" />      </button>    </div>    <div class="coachmark-spotlight" popover="manual"></div>    <div class="coachmark" popover="manual" role="dialog" aria-labelledby="deploys-search-title" data-target="#deploys-search">      <p class="coachmark-count">1 of 3</p>      <h2 class="coachmark-title" id="deploys-search-title">Search every deployment</h2>      <p class="coachmark-text">Find one by its branch, its commit or the person who shipped it.</p>      <div class="coachmark-actions">        <button type="button" class="btn btn-sm btn-quiet" value="skip">Skip</button>        <button type="button" class="btn btn-sm" value="next">Next</button>      </div>    </div>    <div class="coachmark" popover="manual" role="dialog" aria-labelledby="deploys-filter-title" data-target="#deploys-filter">      <p class="coachmark-count">2 of 3</p>      <h2 class="coachmark-title" id="deploys-filter-title">Filter the list</h2>      <p class="coachmark-text">        Keep only the deployments of one branch, one author or one status.      </p>      <div class="coachmark-actions">        <button type="button" class="btn btn-sm btn-quiet" value="skip">Skip</button>        <button type="button" class="btn btn-sm btn-quiet" value="back">Back</button>        <button type="button" class="btn btn-sm" value="next">Next</button>      </div>    </div>    <div class="coachmark" popover="manual" role="dialog" aria-labelledby="deploys-share-title" data-target="#deploys-share">      <p class="coachmark-count">3 of 3</p>      <h2 class="coachmark-title" id="deploys-share-title">Share a deployment</h2>      <p class="coachmark-text">Copy a link to it, open for anyone on the team.</p>      <div class="coachmark-actions">        <button type="button" class="btn btn-sm btn-quiet" value="back">Back</button>        <button type="button" class="btn btn-sm" value="done">Done</button>      </div>    </div>  </div></template>
开始导览,然后试试“Next”、“Back”和 Esc。

导览可以像此处一样包含它所说明的区域,也可以只包含其步骤和聚光区域:步骤可以在文档中的任何位置找到其目标。关闭时,步骤和聚光区域不占空间,因此导览本身可以就是它所在的那一行。

由信标打开的单个引导提示不需要这些。它采用 popover="auto":点击外部或按下 Esc 时,浏览器会自动关闭引导提示并将焦点还给信标,全程无需脚本。

#等待的步骤

步骤可以等待读者,也可以等待其目标。

  • 等待操作。 带有 data-advance 的步骤指定目标上的一个事件(例如 click),该事件发生后导览继续。不要为这一步提供“Next”:它请读者亲自完成这项操作,页面作出响应后导览才继续。“Skip”和 Esc 仍可退出。
  • 稍后出现的目标。 步骤的目标不在页面中或未被渲染时,步骤会等待它,并在它出现后立即打开,例如由按钮显示出来的输入框,或仍在加载的行。三秒后(或等待传给该行为的 wait 所指定的时长)仍未出现的目标会被跳过;越过最后一步后,导览即告完成。导览不会停在一张没有指向任何东西的卡片上;可能无法显示的步骤不应包含读者在别处找不到的内容。

等待的步骤

<div class="coachmark-tour flex flex-col items-center gap-md" id="projects-tour">  <div class="flex items-center gap-sm">    <button type="button" class="btn btn-sm" commandfor="projects-tour" command="--start">      Take the tour    </button>    <button type="button" class="btn btn-sm btn-primary" id="projects-new">New project</button>  </div>  <input class="input w(240)" id="projects-name" placeholder="Project name" aria-label="Project name" hidden>  <div class="coachmark-spotlight" popover="manual"></div>  <div class="coachmark" popover="manual" role="dialog" aria-labelledby="projects-new-title" data-target="#projects-new" data-advance="click">    <p class="coachmark-count">1 of 2</p>    <h2 class="coachmark-title" id="projects-new-title">Start a project</h2>    <p class="coachmark-text">Press New project. The tour goes on once its form is open.</p>    <div class="coachmark-actions">      <button type="button" class="btn btn-sm btn-quiet" value="skip">Skip</button>    </div>  </div>  <div class="coachmark" popover="manual" role="dialog" aria-labelledby="projects-name-title" data-target="#projects-name">    <p class="coachmark-count">2 of 2</p>    <h2 class="coachmark-title" id="projects-name-title">Name it</h2>    <p class="coachmark-text">      Use the name your team already calls it by. You can change it later.    </p>    <div class="coachmark-actions">      <button type="button" class="btn btn-sm btn-quiet" value="back">Back</button>      <button type="button" class="btn btn-sm" value="done">Done</button>    </div>  </div></div><script>document.getElementById('projects-new').addEventListener('click', function () {  setTimeout(function () { document.getElementById('projects-name').hidden = false }, 400)})</script><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
开始导览,然后按“New project”:下一步会等待输入框出现。

#聚光

coachmark-spotlight-dim 用对话框的遮罩调暗步骤目标周围的页面,并让目标及其周围 4px 保持明亮,聚光区域的圆角半径等于控件的圆角半径加上这 4px。遮罩是一圈与屏幕同宽的阴影,由聚光区域绘制,锚点定位让聚光区域覆盖目标,因此页面滚动时它会跟随目标。导览开始时遮罩淡入,结束时淡出;从一步切换到下一步时,明亮区域会立即移动。

遮罩只是绘制效果,而不是一堵墙:其下方的页面仍响应指针和按键,因此读者可以试用步骤所指的控件,导览也不会妨碍读者做其他事。用于某个界面的首次导览,在需要引导视线时使用;只介绍一个新控件的提示则不要使用。

聚光

<div class="coachmark-tour flex flex-col items-center gap-md" id="release-tour">  <button type="button" class="btn btn-sm" commandfor="release-tour" command="--start">    Take the tour  </button>  <div class="card flex-row items-center gap-md p-md w(300)">    <div class="flex flex-col grow(1)">      <span class="text-sm fg-text-strong">atlas.dev</span>      <span class="text-xs fg-text-muted" id="release-status">Ready, 2 minutes ago</span>    </div>    <button type="button" class="btn btn-sm btn-primary" id="release-deploy">Deploy</button>  </div>  <div class="coachmark-spotlight coachmark-spotlight-dim" popover="manual"></div>  <div class="coachmark" popover="manual" role="dialog" aria-labelledby="release-status-title" data-target="#release-status">    <p class="coachmark-count">1 of 2</p>    <h2 class="coachmark-title" id="release-status-title">See where it stands</h2>    <p class="coachmark-text">Each project says whether its last build is ready and when it ran.</p>    <div class="coachmark-actions">      <button type="button" class="btn btn-sm btn-quiet" value="skip">Skip</button>      <button type="button" class="btn btn-sm" value="next">Next</button>    </div>  </div>  <div class="coachmark" popover="manual" role="dialog" aria-labelledby="release-deploy-title" data-target="#release-deploy">    <p class="coachmark-count">2 of 2</p>    <h2 class="coachmark-title" id="release-deploy-title">Ship it</h2>    <p class="coachmark-text">Deploy sends the ready build to production.</p>    <div class="coachmark-actions">      <button type="button" class="btn btn-sm btn-quiet" value="back">Back</button>      <button type="button" class="btn btn-sm" value="done">Done</button>    </div>  </div></div><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => attach(root.current!), [])  return (    <div ref={root} className="coachmark-tour flex flex-col items-center gap-md" id="release-tour">      <button type="button" className="btn btn-sm" {...{ commandfor: 'release-tour', command: '--start' }}>        Take the tour      </button>      <div className="card flex-row items-center gap-md p-md w(300)">        <div className="flex flex-col grow(1)">          <span className="text-sm fg-text-strong">atlas.dev</span>          <span className="text-xs fg-text-muted" id="release-status">Ready, 2 minutes ago</span>        </div>        <button type="button" className="btn btn-sm btn-primary" id="release-deploy">Deploy</button>      </div>      <div className="coachmark-spotlight coachmark-spotlight-dim" popover="manual"></div>      <div className="coachmark" popover="manual" role="dialog" aria-labelledby="release-status-title" data-target="#release-status">        <p className="coachmark-count">1 of 2</p>        <h2 className="coachmark-title" id="release-status-title">See where it stands</h2>        <p className="coachmark-text">          Each project says whether its last build is ready and when it ran.        </p>        <div className="coachmark-actions">          <button type="button" className="btn btn-sm btn-quiet" value="skip">Skip</button>          <button type="button" className="btn btn-sm" value="next">Next</button>        </div>      </div>      <div className="coachmark" popover="manual" role="dialog" aria-labelledby="release-deploy-title" data-target="#release-deploy">        <p className="coachmark-count">2 of 2</p>        <h2 className="coachmark-title" id="release-deploy-title">Ship it</h2>        <p className="coachmark-text">Deploy sends the ready build to production.</p>        <div className="coachmark-actions">          <button type="button" className="btn btn-sm btn-quiet" value="back">Back</button>          <button type="button" className="btn btn-sm" value="done">Done</button>        </div>      </div>    </div>  )}
<script>  import { attach } from '@master/ui/behaviors'</script><div class="coachmark-tour flex flex-col items-center gap-md" id="release-tour" {@attach attach}>  <button type="button" class="btn btn-sm" commandfor="release-tour" command="--start">    Take the tour  </button>  <div class="card flex-row items-center gap-md p-md w(300)">    <div class="flex flex-col grow(1)">      <span class="text-sm fg-text-strong">atlas.dev</span>      <span class="text-xs fg-text-muted" id="release-status">Ready, 2 minutes ago</span>    </div>    <button type="button" class="btn btn-sm btn-primary" id="release-deploy">Deploy</button>  </div>  <div class="coachmark-spotlight coachmark-spotlight-dim" popover="manual"></div>  <div class="coachmark" popover="manual" role="dialog" aria-labelledby="release-status-title" data-target="#release-status">    <p class="coachmark-count">1 of 2</p>    <h2 class="coachmark-title" id="release-status-title">See where it stands</h2>    <p class="coachmark-text">Each project says whether its last build is ready and when it ran.</p>    <div class="coachmark-actions">      <button type="button" class="btn btn-sm btn-quiet" value="skip">Skip</button>      <button type="button" class="btn btn-sm" value="next">Next</button>    </div>  </div>  <div class="coachmark" popover="manual" role="dialog" aria-labelledby="release-deploy-title" data-target="#release-deploy">    <p class="coachmark-count">2 of 2</p>    <h2 class="coachmark-title" id="release-deploy-title">Ship it</h2>    <p class="coachmark-text">Deploy sends the ready build to production.</p>    <div class="coachmark-actions">      <button type="button" class="btn btn-sm btn-quiet" value="back">Back</button>      <button type="button" class="btn btn-sm" value="done">Done</button>    </div>  </div></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <div ref="root" class="coachmark-tour flex flex-col items-center gap-md" id="release-tour">    <button type="button" class="btn btn-sm" commandfor="release-tour" command="--start">      Take the tour    </button>    <div class="card flex-row items-center gap-md p-md w(300)">      <div class="flex flex-col grow(1)">        <span class="text-sm fg-text-strong">atlas.dev</span>        <span class="text-xs fg-text-muted" id="release-status">Ready, 2 minutes ago</span>      </div>      <button type="button" class="btn btn-sm btn-primary" id="release-deploy">Deploy</button>    </div>    <div class="coachmark-spotlight coachmark-spotlight-dim" popover="manual"></div>    <div class="coachmark" popover="manual" role="dialog" aria-labelledby="release-status-title" data-target="#release-status">      <p class="coachmark-count">1 of 2</p>      <h2 class="coachmark-title" id="release-status-title">See where it stands</h2>      <p class="coachmark-text">        Each project says whether its last build is ready and when it ran.      </p>      <div class="coachmark-actions">        <button type="button" class="btn btn-sm btn-quiet" value="skip">Skip</button>        <button type="button" class="btn btn-sm" value="next">Next</button>      </div>    </div>    <div class="coachmark" popover="manual" role="dialog" aria-labelledby="release-deploy-title" data-target="#release-deploy">      <p class="coachmark-count">2 of 2</p>      <h2 class="coachmark-title" id="release-deploy-title">Ship it</h2>      <p class="coachmark-text">Deploy sends the ready build to production.</p>      <div class="coachmark-actions">        <button type="button" class="btn btn-sm btn-quiet" value="back">Back</button>        <button type="button" class="btn btn-sm" value="done">Done</button>      </div>    </div>  </div></template>
开始导览:页面会在每一步的目标周围变暗。

在强制颜色模式下,阴影不会绘制,改用系统高亮色的轮廓标出目标。

#行为

tour 接收带有 coachmark-tour 类的根元素,并返回一个用于解除绑定的函数。@master/ui/behaviors 中的 attach() 会找到页面上的所有导览。

app.js
import { tour } from '@master/ui/behaviors/tour.js'const root = document.querySelector('#deploys-tour')const deploys = tour(root)deploys.value = 0 // starts the tour, as the button doesroot.addEventListener('end', (event) => {  if (event.detail.completed) localStorage.setItem('deploys-tour', 'seen')})
  • 值是导览当前所在步骤的索引,从 0 开始;导览未运行时为 null。设置它可打开某一步,设为 null 则结束导览;设置时不会派发事件。
  • 事件。 每当导览移到另一步,根元素都会派发可冒泡的 change 事件,索引位于 detail.value 中。读者结束导览时,先派发值为 null 的 change,再派发 end;读者走完最后一步时,其 detail.completed 为 true,在此之前通过“Skip”、Esc 或其他关闭步骤的方式离开时为 false。
  • 焦点会在每一步打开时移入其中:优先移到带有 autofocus 的操作,其次是“Next”或“Done”,否则移到步骤本身。导览结束时,焦点回到导览开始时所在的位置,除非读者此后已将焦点移到页面中的其他地方。
  • 选项。 target(step, index) 用于查找步骤的目标,适用于由页面自行管理目标的情况;wait 是步骤等待其目标的时长,以毫秒为单位,默认为 3000。
  • 触发器命令。 浏览器不支持 command 和 commandfor 时,该行为会直接监听启动按钮的点击。

未启用该行为时,导览不会启动;各步骤保持关闭,页面在没有它们的情况下也完整可用。根据 end 事件记住读者已完成导览,不要再自动启动它:改为通过读者以后能找到的按钮提供导览。

#键盘操作

按键操作
Enter, Space在提示圆点上:打开或关闭对应的引导提示。在导览步骤中:按按钮 value 中的值执行 Next、Back、Skip 或 Done。引导提示打开时,其中带 autofocus 的操作项获得焦点;没有此类操作项的步骤由自身获得焦点
Tab在引导提示的操作项之间移动焦点,也可将焦点移回页面中提示圆点之后的位置。步骤不会锁定焦点:步骤打开时页面仍可使用
Escape关闭通过 popover="auto" 打开的引导提示,并将焦点移回提示圆点。在导览中,焦点位于步骤内时:以跳过方式结束导览,并将焦点移回导览开始时所在的位置

#无障碍

  • 为引导提示设置 role="dialog",并通过 aria-labelledby 用标题为它提供无障碍名称。引导提示以非模态方式打开,周围的页面仍可操作;在功能导览中,聚光遮罩下方的页面同样可以操作。
  • 在由信标打开的引导提示的主要操作上设置 autofocus,使卡片打开时焦点移入卡片,键盘用户可按 Enter 继续。功能导览的行为脚本会将焦点移入各步骤。
  • 每一步都可通过“Skip”或“Done”操作,或按下 Esc 退出。单个引导提示还可通过点击其他位置关闭。
  • 从读者的操作(例如点击按钮)启动导览,绝不要在页面加载时启动:导览是帮助,不应从读者前来完成的任务中夺走焦点。
  • 确保导览中的所有信息都能从其他地方找到,让跳过导览的读者,或遇到被跳过的步骤时,都不会错过任何信息。
  • 在标记中将引导提示紧接在信标之后。无论卡片显示在哪里,引导提示在 Tab 导航顺序中都紧跟在信标之后。

#React、Svelte 和 Vue

tour 组件为数据中的每一项写出一个步骤,各带其标题、文字、计数和所需的操作,并接收和传出当前打开步骤的索引,导览结束时调用 onEnd。步骤的目标可以是选择器、元素、ref 或用于查找它的函数。使用 npx master-ui add tour 将组件复制到项目中。React、Svelte 和 Vue 介绍这些组件的实现和用法。

ui/tour.tsx
'use client'import { useId, type ComponentProps, type ReactNode, type RefObject } from 'react'import { tour } from '@master/ui/behaviors/tour.js'import { useBehavior } from './use-behavior'export interface TourStep {  /**   * What the step points at: a selector, an element, a ref, or a function that finds it. Until it is in the   * page the step waits for it, and passes on when it does not come.   */  target: string | Element | RefObject<Element | null> | (() => Element | null | undefined)  title: string  text?: string  /**   * An event on the target that moves the tour on, such as `click`: the step asks the reader to do it, and   * has no Next.   */  advance?: string}export interface TourProps extends Omit<ComponentProps<'div'>, 'children' | 'defaultValue' | 'ref'> {  steps: TourStep[]  /** The index of the open step, or null while the tour is not running. */  value?: number | null  defaultValue?: number | null  onValueChange?: (value: number | null) => void  /** Called when the reader ends the tour: completed, or left before the last step. */  onEnd?: (completed: boolean) => void  /** The button that starts the tour: spread what it is given on a button of your own. */  trigger?: (attributes: { commandfor: string, command: '--start' }) => ReactNode  /** Dim the page around each step's target. */  spotlight?: boolean  /** How long a step waits for its target, in milliseconds. */  wait?: number  nextLabel?: string  backLabel?: string  skipLabel?: string  doneLabel?: string  /** Where a step stands: `2 of 4` by default. */  countLabel?: (step: number, steps: number) => string}/** The element a step points at, found as the page is now. */function find(target: TourStep['target'] | undefined): Element | null {  if (!target) return null  if (typeof target === 'string') return document.querySelector(target)  if (typeof target === 'function') return target() ?? null  if ('current' in target) return target.current  return target}/** * A tour: one coachmark for each step, written from data, opened in turn against its target. The behavior * opens and closes the steps, waits for a target, moves focus and hears the keys; this component writes the * steps, finds their targets and passes the index of the open step in and out. */export function Tour({ steps, value, defaultValue, onValueChange, onEnd, trigger, spotlight = false, wait, nextLabel = 'Next', backLabel = 'Back', skipLabel = 'Skip', doneLabel = 'Done', countLabel = (step, count) => `${step} of ${count}`, className, id, ...root }: TourProps) {  const generated = useId()  const name = id ?? generated  const ref = useBehavior(tour, { wait, target: (_step: HTMLElement, index: number) => find(steps[index]?.target) }, {    value,    defaultValue,    onValueChange,    on: { end: event => onEnd?.((event as CustomEvent<{ completed: boolean }>).detail.completed) },  })  return (    <>      {trigger?.({ commandfor: name, command: '--start' })}      <div id={name} className={className ? `coachmark-tour ${className}` : 'coachmark-tour'} ref={ref} {...root}>        <div className={spotlight ? 'coachmark-spotlight coachmark-spotlight-dim' : 'coachmark-spotlight'} popover="manual" />        {steps.map((step, index) => {          const last = index === steps.length - 1          return (            <div className="coachmark" popover="manual" role="dialog" aria-labelledby={`${name}-${index}-title`} data-advance={step.advance} key={index}>              <p className="coachmark-count">{countLabel(index + 1, steps.length)}</p>              <h2 className="coachmark-title" id={`${name}-${index}-title`}>{step.title}</h2>              {step.text ? <p className="coachmark-text">{step.text}</p> : null}              <div className="coachmark-actions">                {last ? null : <button type="button" className="btn btn-sm btn-quiet" value="skip">{skipLabel}</button>}                {index > 0 ? <button type="button" className="btn btn-sm btn-quiet" value="back">{backLabel}</button> : null}                {step.advance ? null : <button type="button" className="btn btn-sm" value={last ? 'done' : 'next'}>{last ? doneLabel : nextLabel}</button>}              </div>            </div>          )        })}      </div>    </>  )}
ui/Tour.svelte
<script lang="ts" module>  import type { Snippet } from 'svelte'  import type { HTMLAttributes } from 'svelte/elements'  export interface TourStep {    /**     * What the step points at: a selector, an element, or a function that finds it. Until it is in the page     * the step waits for it, and passes on when it does not come.     */    target: string | Element | (() => Element | null | undefined)    title: string    text?: string    /**     * An event on the target that moves the tour on, such as `click`: the step asks the reader to do it, and     * has no Next.     */    advance?: string  }  export interface TourProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {    steps: TourStep[]    /** The index of the open step, or null while the tour is not running. Bind it, or leave it out. */    value?: number | null    onValueChange?: (value: number | null) => void    /** Called when the reader ends the tour: completed, or left before the last step. */    onEnd?: (completed: boolean) => void    /** The button that starts the tour: spread what it is given on a button of your own. */    trigger?: Snippet<[{ commandfor: string, command: '--start' }]>    /** Dim the page around each step's target. */    spotlight?: boolean    /** How long a step waits for its target, in milliseconds. */    wait?: number    nextLabel?: string    backLabel?: string    skipLabel?: string    doneLabel?: string    /** Where a step stands: `2 of 4` by default. */    countLabel?: (step: number, steps: number) => string  }</script><script lang="ts">  import { tour } from '@master/ui/behaviors/tour.js'  import { behavior } from './behavior.svelte.js'  let { steps, value = $bindable(), onValueChange, onEnd, trigger, spotlight = false, wait, nextLabel = 'Next', backLabel = 'Back', skipLabel = 'Skip', doneLabel = 'Done', countLabel = (step: number, count: number) => `${step} of ${count}`, class: className, id, ...root }: TourProps = $props()  const generated = $props.id()  const name = $derived(id ?? generated)  /** The element a step points at, found as the page is now. */  const find = (target: TourStep['target'] | undefined) => {    if (!target) return null    if (typeof target === 'string') return document.querySelector(target)    if (typeof target === 'function') return target() ?? null    return target  }  const attach = behavior(tour, () => ({ wait, target: (_step: HTMLElement, index: number) => find(steps[index]?.target) }), {    value: () => value,    change: (next) => {      value = next      onValueChange?.(next)    },    on: { end: event => onEnd?.((event as CustomEvent<{ completed: boolean }>).detail.completed) },  })</script><!--  @component  A tour: one coachmark for each step, written from data, opened in turn against its target. The behavior  opens and closes the steps, waits for a target, moves focus and hears the keys; this component writes the  steps, finds their targets and passes the index of the open step in and out.-->{@render trigger?.({ commandfor: name, command: '--start' })}<div id={name} class={['coachmark-tour', className]} {@attach attach} {...root}>  <div class={spotlight ? 'coachmark-spotlight coachmark-spotlight-dim' : 'coachmark-spotlight'} popover="manual"></div>  {#each steps as step, index (index)}    {@const last = index === steps.length - 1}    <div class="coachmark" popover="manual" role="dialog" aria-labelledby="{name}-{index}-title" data-advance={step.advance}>      <p class="coachmark-count">{countLabel(index + 1, steps.length)}</p>      <h2 class="coachmark-title" id="{name}-{index}-title">{step.title}</h2>      {#if step.text}<p class="coachmark-text">{step.text}</p>{/if}      <div class="coachmark-actions">        {#if !last}<button type="button" class="btn btn-sm btn-quiet" value="skip">{skipLabel}</button>{/if}        {#if index > 0}<button type="button" class="btn btn-sm btn-quiet" value="back">{backLabel}</button>{/if}        {#if !step.advance}<button type="button" class="btn btn-sm" value={last ? 'done' : 'next'}>{last ? doneLabel : nextLabel}</button>{/if}      </div>    </div>  {/each}</div>
ui/Tour.vue
<script lang="ts">export interface TourStep {  /**   * What the step points at: a selector, an element, or a function that finds it. Until it is in the page the   * step waits for it, and passes on when it does not come.   */  target: string | Element | (() => Element | null | undefined)  title: string  text?: string  /**   * An event on the target that moves the tour on, such as `click`: the step asks the reader to do it, and has   * no Next.   */  advance?: string}</script><script setup lang="ts">import { computed, useId, useTemplateRef } from 'vue'import { tour } from '@master/ui/behaviors/tour.js'import { useBehavior } from './use-behavior'defineOptions({ inheritAttrs: false })const { steps, id, spotlight = false, wait, nextLabel = 'Next', backLabel = 'Back', skipLabel = 'Skip', doneLabel = 'Done', countLabel = (step: number, count: number) => `${step} of ${count}` } = defineProps<{  steps: TourStep[]  id?: string  /** Dim the page around each step's target. */  spotlight?: boolean  /** How long a step waits for its target, in milliseconds. */  wait?: number  nextLabel?: string  backLabel?: string  skipLabel?: string  doneLabel?: string  /** Where a step stands: `2 of 4` by default. */  countLabel?: (step: number, steps: number) => string}>()/** The index of the open step, or null while the tour is not running: `v-model`. */const model = defineModel<number | null>()const emit = defineEmits<{  /** The reader ended the tour: completed, or left before the last step. */  end: [completed: boolean]}>()defineSlots<{  /** The button that starts the tour: bind what it is given on a button of your own. */  trigger?: (attributes: { commandfor: string, command: '--start' }) => unknown}>()/* * A tour: one coachmark for each step, written from data, opened in turn against its target. The behavior * opens and closes the steps, waits for a target, moves focus and hears the keys; this component writes the * steps, finds their targets and passes the index of the open step in and out. */const generated = useId()const name = computed(() => id ?? generated)const root = useTemplateRef<HTMLDivElement>('root')/** The element a step points at, found as the page is now. */const find = (target: TourStep['target'] | undefined) => {  if (!target) return null  if (typeof target === 'string') return document.querySelector(target)  if (typeof target === 'function') return target() ?? null  return target}useBehavior(root, tour, () => ({ wait, target: (_step: HTMLElement, index: number) => find(steps[index]?.target) }), model, {  end: event => emit('end', (event as CustomEvent<{ completed: boolean }>).detail.completed),})</script><template>  <slot name="trigger" v-bind="{ commandfor: name, command: '--start' as const }" />  <div :id="name" class="coachmark-tour" ref="root" v-bind="$attrs">    <div :class="spotlight ? 'coachmark-spotlight coachmark-spotlight-dim' : 'coachmark-spotlight'" popover="manual" />    <div v-for="(step, index) in steps" :key="index" class="coachmark" popover="manual" role="dialog" :aria-labelledby="`${name}-${index}-title`" :data-advance="step.advance">      <p class="coachmark-count">{{ countLabel(index + 1, steps.length) }}</p>      <h2 :id="`${name}-${index}-title`" class="coachmark-title">{{ step.title }}</h2>      <p v-if="step.text" class="coachmark-text">{{ step.text }}</p>      <div class="coachmark-actions">        <button v-if="index < steps.length - 1" type="button" class="btn btn-sm btn-quiet" value="skip">{{ skipLabel }}</button>        <button v-if="index > 0" type="button" class="btn btn-sm btn-quiet" value="back">{{ backLabel }}</button>        <button v-if="!step.advance" type="button" class="btn btn-sm" :value="index === steps.length - 1 ? 'done' : 'next'">{{ index === steps.length - 1 ? doneLabel : nextLabel }}</button>      </div>    </div>  </div></template>

#浏览器支持

引导提示使用 CSS 锚点定位。卡片通过 position-area 和 position-try-fallbacks 定位,箭头和功能导览的聚光区域都通过卡片与所指元素之间的 anchor() 定位。在不支持锚点定位的浏览器中,卡片仍可打开和关闭,会按浏览器的默认弹出层位置居中显示在视口中,且不显示箭头;聚光区域不会调暗任何内容,因为它无法让目标保持明亮。showPopover() 的 source 选项为导览步骤指定锚点。省略该选项时,步骤会在没有锚点的情况下打开。启动按钮使用触发器命令,浏览器不支持时由该行为代为处理。淡入淡出和短距离滑动使用 @starting-style。不支持该特性时,卡片和遮罩会立即出现或消失。

卡片淡入时朝锚点滑动 4px,退出时以更短的时间淡出。其他弹出层带有缩放动画,引导提示卡片保持原尺寸,因为缩放会让箭头缩进卡片内,够不到它所指的元素。

#类名

类名说明
coachmark墨色卡片。将该类用于带 popover 属性和 role="dialog" 的元素,并用卡片标题为该元素提供可访问名称。缺少 popover 属性时,卡片始终隐藏。卡片宽 18rem,打开时在触发元素下方居中,下方空间不足时翻转到上方。箭头指向标记中紧邻卡片之前的元素,因此将卡片紧接在提示圆点或它所说明的控件之后。
coachmark-top优先放在上方,适用于靠近屏幕底部的控件或位于控件上方一角的提示圆点。与 coachmark 一同使用
coachmark-count标题上方以较淡墨色显示的短文字,可写导览进度(第 2 步,共 4 步),也可写「新功能」之类的标签。
coachmark-title步骤标题,为引导提示提供可访问名称。
coachmark-text用一两句话说明控件的用途。
coachmark-actions沿卡片底部排列的一行小按钮,距卡片边缘 12px。有多个按钮时,第一个放在起始侧(跳过),其余放在末尾侧(上一步、下一步)。默认按钮在墨色背景上突出显示操作,低强调按钮沿用卡片的前景色。
coachmark-tour按导览顺序容纳各个步骤(每一步都是带 popover="manual" 的引导提示)及其聚光层。导览行为脚本挂载在此元素上;为它设置 id,供启动导览的按钮引用。它本身不绘制任何内容,并将其箭头所指目标的锚点名称限定在自身范围内
coachmark-spotlight导览在每一步打开前,于该步骤目标上方打开的空 popover:覆盖目标及其周围 4px,无论打开的步骤位于标记中何处,其箭头都指向它。它本身不绘制任何内容,并让指针穿透
coachmark-spotlight-dim用对话框的遮罩调暗目标周围的页面,目标保持明亮。遮罩下的页面仍可使用。在强制颜色模式下,改用系统高亮色的轮廓标示目标与 coachmark-spotlight 一同使用
coachmark-beacon用一个 10px 的墨色圆点标记引导步骤的位置,并通过 popovertarget 打开该步骤。圆点会不时向外扩散一圈圆环。当指针悬停、圆点获得焦点、对应的引导提示打开或启用减少动态效果时,圆环保持为静止的光晕。点击区域为 24px,在触摸屏上为 44px。位置由页面决定,通常放在所标记控件的上角

#自定义属性

属性默认值说明
--coachmark-width18rem卡片宽度,最大为视口宽度减去 1 rem。
--coachmark-inset12px操作区到卡片边缘的间距,文字到边缘的间距再多 4px。卡片圆角等于小号按钮圆角加上操作区的内边距
--coachmark-arrow7px箭头尖端伸出卡片的距离
--anchored-gapcalc(var(--coachmark-arrow) + 6px)卡片与触发元素的间距。箭头尖端距该元素 6px
--coachmark-beacon-size.625rem提示圆点的直径。在 coachmark-beacon 上设置
--coachmark-spotlight-inset4px聚光层在各边超出目标的距离在 coachmark-spotlight 上设置
--coachmark-spotlight-radiusvar(--radius-control)所照亮目标的圆角半径。聚光层的圆角为此值加外扩距离在 coachmark-spotlight 上设置

#源码

@master/ui/components/coachmark.css
/* * Coachmark: one step of a tour, pointing at the control it explains. * It is a popover of ink: a small card of the inverse surface sheet-inverse is made of, laid flat so its arrow * joins it without a seam. It stands apart from the sheets of ordinary popovers and from the page under it. * Anchor positioning centers it under the element that opened it (popovertarget, commandfor, or the source a * script passes to showPopover) and flips it above at the viewport's edge. It enters with a fade and slides 4px * toward that element. * An arrow of the same ink points at the element right before the coachmark in the markup, which is its beacon * or the control it explains: while the coachmark is open that element is named as an anchor, and the arrow * follows its middle when an edge pushes the card aside and moves to the other edge when the card flips. * Geometry: 12px between the actions and the card's edge, so the card's corner is a small button's corner plus * that inset, and a 7px arrow whose two flanks carry the card's contour into its point. * A beacon marks where a step is and is the button that opens it: a 10px ink dot that sends out a ring now and * then. Under the pointer, with focus, while its step is open and with reduced motion, the ring rests as a halo. * Without anchor positioning the card opens where the browser places a popover, centered, with no arrow. * A tour holds its steps in coachmark-tour, which keeps the name of what its arrows point at to itself. Its * spotlight is an empty popover the tour opens over each step's target with that target as its anchor: it * covers the target and 4px around it, so the card's arrow points there wherever the step stands in the * markup. It draws nothing by itself; dimmed, a ring of scrim as wide as the screen leaves the target lit, * and in forced colors, where shadows are gone, a system-color outline marks the target instead. The scrim * is paint only: the page under it still answers the pointer and the keys. */@layer components.ui {  .coachmark {    --coachmark-inset: 12px;    --coachmark-width: 18rem;    --coachmark-arrow: 7px;    --anchored-gap: calc(var(--coachmark-arrow) + 6px);    --anchored-inline: span-all;    --surface-color: var(--color-surface-inverse);    /* Focus rings stand on ink, so they are drawn in the ink of the words. */    --color-focus: var(--color-text-inverse);    @apply --on-ink;    flex-direction: column;    gap: .25rem;    width: min(var(--coachmark-width), calc(100vw - 1rem));    padding: calc(var(--coachmark-inset) + 4px) calc(var(--coachmark-inset) + 4px) var(--coachmark-inset);    border: 0;    border-radius: calc(var(--radius-control-sm) * var(--corner-scale) + var(--coachmark-inset));    corner-shape: var(--corner-shape);    overflow: visible;    background: var(--color-surface-inverse);    color: var(--color-text-inverse);    box-shadow:      inset 0 .5px 0 var(--color-ink-highlight),      0 0 0 .5px var(--color-ink-edge),      0 1px 2px -1px var(--color-shade-1), 0 3px 6px -3px var(--color-shade-2), 0 6px 12px -6px var(--color-shade-3),      0 12px 24px -12px var(--color-shade-4), 0 24px 48px -24px var(--color-shade-5);    font: 400 .8125rem/1.5 var(--font-family-sans);    font-feature-settings: inherit;    overflow-wrap: anywhere;    overscroll-behavior: contain;    @apply --anchored;    /*     * In and out of the top layer. Not --pop: a scale would make the card the containing block of its arrow,     * which could then no longer reach the element it points at. Closed is where it leaves to.     */    opacity: 0;    transition-property: opacity, margin, overlay, display;    transition-duration: var(--duration-quick);    transition-timing-function: var(--easing-glide);    transition-behavior: allow-discrete;    &:popover-open {      display: flex;      opacity: 1;      transition-duration: var(--duration-base);      @starting-style {        opacity: 0;      }    }    /* Prefer the space above. */    &.coachmark-top {      --anchored-block: block-start;    }    /* A step with no action to focus is focused itself, as a dialog is, and reads as its title. */    &:focus-visible {      outline: none;    }    @media (forced-colors: active) {      &, &:focus-visible {        outline: 1px solid CanvasText;        outline-offset: -1px;      }    }  }  /*   * The spotlight: an empty popover, nothing of the browser's own box. Where anchor positioning is missing it   * stays a point, and dims nothing it could not leave a hole in.   */  .coachmark-spotlight {    --coachmark-spotlight-inset: 4px;    --coachmark-spotlight-radius: var(--radius-control);    width: 0;    height: 0;    margin: 0;    padding: 0;    border: 0;    overflow: visible;    background: none;    pointer-events: none;  }  /*   * Placed: a half-rem from the viewport's sides when an edge pushes it aside, and only a flip to the other side   * of its anchor. It travels 4px toward the anchor as it enters and 2px away as it leaves; margins are swapped   * with the flip, so the travel follows the side it opened on.   */  @supports (position-area: block-end) {    .coachmark {      --coachmark-travel: 2px;      margin: calc(var(--anchored-gap) + var(--coachmark-travel)) .5rem;      position-try-fallbacks: flip-block;      anchor-name: --coachmark-card;      anchor-scope: --coachmark-card;      &:popover-open {        --coachmark-travel: 0px;        @starting-style {          --coachmark-travel: 4px;        }      }      @media (prefers-reduced-motion: reduce) {        &, &:popover-open {          --coachmark-travel: 0px;        }      }      /*       * The arrow: a square turned a quarter, its middle on the edge of the card that faces the element it       * points at, and a patch of the card's ink over its inner half, so only the point outside the card is       * seen. Both lie over the card's face and under its content, so the point covers the card's contour where       * they join and its own contour carries the card's into the point.       * They are fixed to the viewport and placed with anchor(), between that element and the card itself:       * across, at the element's middle, but never into the card's rounded corners; up and down, at whichever       * of the card's edges is nearer to it. With no element to point at, they meet in the card's middle, where       * the patch hides the arrow.       */      &::before, &::after {        --coachmark-arrow-side: calc(var(--coachmark-arrow) * 1.4142);        --coachmark-arrow-reach: calc(var(--radius-control-sm) * var(--corner-scale) + var(--coachmark-inset) + var(--coachmark-arrow));        --coachmark-arrow-x: clamp(          anchor(--coachmark-card left, 0px) + var(--coachmark-arrow-reach),          anchor(--coachmark-target center, anchor(--coachmark-card center, 0px)),          anchor(--coachmark-card right, 0px) - var(--coachmark-arrow-reach)        );        --coachmark-arrow-y: clamp(          anchor(--coachmark-card top, 0px),          anchor(--coachmark-target center, anchor(--coachmark-card center, 0px)),          anchor(--coachmark-card bottom, 0px)        );        content: '';        position: fixed;        z-index: -1;        background: var(--color-surface-inverse);        pointer-events: none;      }      &::before {        top: calc(var(--coachmark-arrow-y) - var(--coachmark-arrow-side) / 2);        left: calc(var(--coachmark-arrow-x) - var(--coachmark-arrow-side) / 2);        width: var(--coachmark-arrow-side);        height: var(--coachmark-arrow-side);        border-radius: 2px;        /* The light catches the two flanks that face up, which the patch hides when the arrow points down. */        box-shadow: inset .5px .5px 0 var(--color-ink-highlight), 0 0 0 .5px var(--color-ink-edge);        rotate: 45deg;      }      &::after {        top: clamp(          anchor(--coachmark-card top, 0px),          var(--coachmark-arrow-y) - var(--coachmark-arrow) - 1px,          anchor(--coachmark-card bottom, 0px) - var(--coachmark-arrow) * 2 - 2px        );        left: calc(var(--coachmark-arrow-x) - var(--coachmark-arrow) - .5px);        width: calc(var(--coachmark-arrow) * 2 + 1px);        height: calc(var(--coachmark-arrow) * 2 + 2px);      }      /*       * Forced colors repaint both as the canvas, and the card would point at nothing: the arrow is kept in the       * system's text color, and the patch in the canvas the card is repainted with.       */      @media (forced-colors: active) {        &::before, &::after {          forced-color-adjust: none;        }        &::before {          background: CanvasText;          box-shadow: none;        }        &::after {          background: Canvas;        }      }    }    /* The element right before an open coachmark is what its arrow points at. */    :where(:has(+ .coachmark:popover-open)) {      anchor-name: --coachmark-target;    }    /* A tour keeps the name to itself, so the arrows of its steps find its spotlight and no other. */    .coachmark-tour {      anchor-scope: --coachmark-target;    }    /*     * Over the target the tour opened it against, and 4px around it, with the corner of a control plus that     * inset. It is what the arrow of the open step points at.     */    .coachmark-spotlight {      /* The element it was opened against; anchor() alone does not reach for it as position-area does. */      position-anchor: auto;      inset: auto;      top: anchor(top);      right: anchor(right);      bottom: anchor(bottom);      left: anchor(left);      width: auto;      height: auto;      margin: calc(var(--coachmark-spotlight-inset) * -1);      border-radius: calc(var(--coachmark-spotlight-radius) * var(--corner-scale) + var(--coachmark-spotlight-inset));      corner-shape: var(--corner-shape);      anchor-name: --coachmark-target;      /*       * Dimmed: a scrim as wide as the screen, drawn as the spotlight's ring, so the target stays lit. It       * fades in as the tour starts and out as it ends, a fade reduced motion keeps; from one step to the       * next the hole moves at once, so there is no travel to take away.       */      &.coachmark-spotlight-dim {        box-shadow: 0 0 0 100vmax var(--color-surface-scrim);        opacity: 0;        transition-property: opacity, overlay, display;        transition-duration: var(--duration-quick);        transition-timing-function: var(--easing-glide);        transition-behavior: allow-discrete;        &:popover-open {          opacity: 1;          transition-duration: var(--duration-base);          @starting-style {            opacity: 0;          }        }        /* Shadows are gone in forced colors: the target is marked by an outline of the system's highlight. */        @media (forced-colors: active) {          outline: 2px solid Highlight;        }      }    }  }  /* Where the step stands in the tour: "2 of 4". */  .coachmark-count {    margin: 0;    color: color-mix(in oklab, currentColor 62%, transparent);    font-size: .75rem;    font-weight: var(--font-weight-control);    font-variant-numeric: tabular-nums;    line-height: 1.4;  }  .coachmark-title {    margin: 0;    color: inherit;    font: var(--font-weight-control) .875rem/1.4 var(--font-family-sans);    font-feature-settings: inherit;    text-wrap: balance;  }  .coachmark-text {    margin: 0;    color: color-mix(in oklab, currentColor 76%, transparent);    text-wrap: pretty;  }  /*   * The actions, along the foot: 12px from the card's edges, so a small button's corner nests in the card's.   * The first of several stands at the start (Skip, or Back), the others at the end.   */  .coachmark-actions {    display: flex;    flex-wrap: wrap;    align-items: center;    justify-content: flex-end;    gap: .25rem;    margin: .625rem -4px 0;    .btn:first-child:not(:only-child) {      margin-inline-end: auto;    }    /*     * The first is the first the row draws: a button left hidden on this step, such as Back on the first, is     * not counted, and a button left alone beside a hidden one stays at the end.     */    .btn:nth-child(1 of :not([hidden])) {      margin-inline-end: auto;    }    .btn:nth-child(1 of :not([hidden])):nth-last-child(1 of :not([hidden])) {      margin-inline-end: 0;    }  }  /*   * A quiet button on ink takes a quieter step of the card's ink, and a tint of it under the pointer; busy, it   * does not answer the pointer. These rules outweigh the quiet button's own hover, which would otherwise lay   * a pearl sheet in the strong ink of a page on the card.   */  .coachmark .btn.btn-quiet:not(:disabled, [aria-disabled="true"]) {    color: color-mix(in oklab, var(--color-text-inverse) 72%, transparent);    @media (hover: hover) {      &:hover:not([aria-busy="true"]) {        background: color-mix(in oklab, var(--color-text-inverse) 12%, transparent);        color: var(--color-text-inverse);        box-shadow: none;      }    }    &:active:not([aria-busy="true"]) {      background: color-mix(in oklab, var(--color-text-inverse) 18%, transparent);      color: var(--color-text-inverse);      box-shadow: none;    }  }  /*   * The beacon: a 10px dot of ink with a rim of the surface it stands on, and a ring that leaves it now and then   * to say there is something to see. The page places it, beside or on the corner of what it marks; its target   * is 24px, and 44px on a touch screen.   */  .coachmark-beacon {    --coachmark-beacon-size: .625rem;    position: relative;    display: inline-block;    flex: none;    width: var(--coachmark-beacon-size);    height: var(--coachmark-beacon-size);    margin: 0;    padding: 0;    border: 0;    border-radius: 999px;    corner-shape: round;    background: var(--color-surface-inverse);    box-shadow: 0 0 0 2px var(--surface-color, var(--color-surface-base)), 0 1px 3px 1.5px var(--color-shade-1);    vertical-align: middle;    appearance: none;    cursor: pointer;    touch-action: manipulation;    -webkit-tap-highlight-color: transparent;    transition: scale var(--duration-quick) var(--easing-glide);    /* The ring that leaves the dot. */    &::before {      content: '';      position: absolute;      inset: 0;      border-radius: inherit;      corner-shape: inherit;      box-shadow: 0 0 0 0 transparent;      pointer-events: none;      animation: coachmark-beacon 2.4s var(--easing-glide) infinite;    }    /* The target a pointer and a finger need, around the dot. */    &::after {      content: '';      position: absolute;      inset: calc((var(--coachmark-beacon-size) - 1.5rem) / 2);      @media (pointer: coarse) {        inset: calc((var(--coachmark-beacon-size) - 2.75rem) / 2);      }    }    @media (hover: hover) {      &:hover {        scale: 1.2;      }      &:hover::before {        box-shadow: 0 0 0 4px color-mix(in oklab, var(--color-surface-inverse) 14%, transparent);        animation: none;      }    }    &:active {      scale: .9;      transition-duration: var(--duration-press);    }    &:focus-visible {      @apply --focus-ring;      outline-offset: 4px;    }    /* Found: while it has focus or its step is open, the ring rests as a halo, as it does under the pointer. */    &:is(:focus-visible, :has(+ .coachmark:popover-open))::before {      box-shadow: 0 0 0 4px color-mix(in oklab, var(--color-surface-inverse) 14%, transparent);      animation: none;    }    @media (prefers-reduced-motion: reduce) {      transition: none;      &::before {        box-shadow: 0 0 0 4px color-mix(in oklab, var(--color-surface-inverse) 14%, transparent);        animation: none;      }      &:hover, &:active {        scale: none;      }    }    @media (forced-colors: active) {      outline: 2px solid ButtonText;      outline-offset: -2px;      background: ButtonText;      &::before {        animation: none;      }      /* The halo is a shadow, gone here: found, the dot takes the system's highlight instead. */      &:has(+ .coachmark:popover-open) {        outline-color: Highlight;        background: Highlight;      }      /* Keyboard focus is a ring of the highlight, as on a button. */      &:focus-visible {        outline: 2px solid Highlight;        outline-offset: 4px;      }    }  }}@keyframes coachmark-beacon {  0%, 20% {    box-shadow: 0 0 0 0 color-mix(in oklab, var(--color-surface-inverse) 42%, transparent);  }  75%, 100% {    box-shadow: 0 0 0 .625rem transparent;  }}