Tour 漫遊引導
新功能上線時,逐步醒目標示頁面上的元素並給出說明,帶使用者熟悉介面。遮罩蓋住頁面,目標元素處留出醒目標示區域,旁邊的卡片顯示標題、說明和上一步、下一步按鈕。
引入
import { HuiTour } from '@nscode/handy-ui'
基礎用法
steps 描述每一步:target 是要醒目標示的元素,title、description 是卡片上的標題和說明。v-model:open 控制顯示,v-model:current 是目前第幾步(從 0 開始)。
target 可以是 CSS 選擇器、元素、元件實例(取它的根元素)、ref,或者回傳它們的函式。推薦寫成函式 () => buttonRef.value:切到這一步時才去取元素,這時頁面早就呈現好了。目標不在可視區域時會先捲動過去。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTour } from '@nscode/handy-ui'
import type { TourStep } from '@nscode/handy-ui'
const open = ref(false)
const current = ref(0)
const uploadRef = ref()
const saveRef = ref()
const moreRef = ref()
const steps: TourStep[] = [
{
target: () => uploadRef.value,
title: '上傳文件',
description: '點這裡選擇本地文件上傳,一次可以選多個。'
},
{
target: () => saveRef.value,
title: '保存',
description: '改完記得保存,保存後其他成員就能看到。'
},
{
target: () => moreRef.value,
title: '更多操作',
description: '匯出、分享、刪除等不常用的操作都收在這裡。'
}
]
const start = () => {
current.value = 0
open.value = true
}
</script>
<template>
<div>
<HuiSpace direction="vertical" align="start" size="large">
<HuiSpace wrap>
<HuiButton type="primary" @click="start">開始引導</HuiButton>
<span>open:{{ open }},current:{{ current }}</span>
</HuiSpace>
<HuiSpace>
<HuiButton ref="uploadRef" icon="upload">上傳</HuiButton>
<HuiButton ref="saveRef" icon="save">保存</HuiButton>
<HuiButton ref="moreRef" icon="more-horizontal" aria-label="更多" />
</HuiSpace>
</HuiSpace>
<HuiTour v-model:open="open" v-model:current="current" :steps="steps" />
</div>
</template>
沒有目標
不寫 target 的步驟,卡片顯示在螢幕中間,遮罩蓋住整個頁面、不留醒目標示區域。適合放開場的歡迎語和最後的結束語。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTour } from '@nscode/handy-ui'
import type { TourStep } from '@nscode/handy-ui'
const open = ref(false)
const publishRef = ref()
const steps: TourStep[] = [
{
title: '歡迎使用新版編輯器',
description: '花一分鐘看看有哪些變化。這一步沒寫 target,卡片在螢幕中間。'
},
{
target: () => publishRef.value,
title: '發布',
description: '寫完點這裡發布,發布前會先自動保存。'
},
{
title: '就這些',
description: '以後可以在“幫助”選單裡重新打開這個引導。'
}
]
</script>
<template>
<div>
<HuiSpace wrap>
<HuiButton @click="open = true">開始引導</HuiButton>
<HuiButton ref="publishRef" type="primary" icon="upload">發布</HuiButton>
</HuiSpace>
<HuiTour v-model:open="open" :steps="steps" />
</div>
</template>
位置
placement 是卡片相對目標的位置,預設 bottom,共 12 個方向;放不下時自動翻到另一側。arrow 控制要不要顯示指向目標的箭頭,預設顯示,沒有目標時不顯示。
這個示例關掉了遮罩,打開後可以直接在選擇框裡換方向、勾選箭頭:
<script setup lang="ts">
import { computed, ref } from 'vue'
import { HuiButton, HuiCheckbox, HuiSelect, HuiSpace, HuiTour } from '@nscode/handy-ui'
import type { PopperPlacement, SelectOption, TourStep } from '@nscode/handy-ui'
const placements: PopperPlacement[] = [
'top',
'top-start',
'top-end',
'bottom',
'bottom-start',
'bottom-end',
'left',
'left-start',
'left-end',
'right',
'right-start',
'right-end'
]
const options: SelectOption[] = placements.map((value) => ({ label: value, value }))
const open = ref(false)
const placement = ref<PopperPlacement>('bottom')
const arrow = ref(true)
const targetRef = ref<HTMLElement>()
const steps = computed<TourStep[]>(() => [
{
target: () => targetRef.value,
title: `placement:${placement.value}`,
description: '在上面的選擇框裡換個方向看看。'
}
])
</script>
<template>
<div>
<HuiSpace direction="vertical" fill size="large">
<HuiSpace wrap>
<HuiSelect v-model="placement" :options="options" style="width: 160px" />
<HuiCheckbox v-model="arrow">顯示箭頭(arrow)</HuiCheckbox>
<HuiButton type="primary" @click="open = true">打開</HuiButton>
</HuiSpace>
<div class="stage">
<div ref="targetRef" class="target">目標元素</div>
</div>
</HuiSpace>
<HuiTour
v-model:open="open"
:steps="steps"
:placement="placement"
:arrow="arrow"
:mask="false"
/>
</div>
</template>
<style scoped>
.stage {
display: flex;
justify-content: center;
padding: 160px 0;
}
.target {
padding: var(--hui-spacing-4) var(--hui-spacing-8);
color: var(--hui-text-color-secondary);
border: 1px dashed var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
</style>
遮罩
mask 預設 true,用主題的 --hui-mask-color;false 不顯示遮罩;傳 { color } 改顏色。
有遮罩時整個頁面(包括醒目的目標)都不能點擊,焦點限制在卡片裡;沒有遮罩時頁面照常可以操作,目標也不再有醒目標示區域,只靠卡片的箭頭指著。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTour } from '@nscode/handy-ui'
import type { TourMask, TourStep } from '@nscode/handy-ui'
const open = ref(false)
const mask = ref<TourMask>(true)
const searchRef = ref()
const filterRef = ref()
const steps: TourStep[] = [
{ target: () => searchRef.value, title: '搜尋', description: '按名稱、編號搜尋。' },
{ target: () => filterRef.value, title: '篩選', description: '按狀態、時間等條件篩選。' }
]
const show = (value: TourMask) => {
mask.value = value
open.value = true
}
</script>
<template>
<div>
<HuiSpace direction="vertical" align="start" size="large">
<HuiSpace wrap>
<HuiButton @click="show(true)">預設遮罩(true)</HuiButton>
<HuiButton @click="show(false)">不顯示遮罩(false)</HuiButton>
<HuiButton @click="show({ color: 'rgba(22, 119, 255, 0.25)' })">藍色遮罩</HuiButton>
<HuiButton @click="show({ color: 'rgba(0, 0, 0, 0.75)' })">更深的遮罩</HuiButton>
</HuiSpace>
<HuiSpace>
<HuiButton ref="searchRef" icon="search">搜尋</HuiButton>
<HuiButton ref="filterRef" icon="filter">篩選</HuiButton>
</HuiSpace>
</HuiSpace>
<HuiTour v-model:open="open" :steps="steps" :mask="mask" />
</div>
</template>
醒目標示區域
gap 設定醒目標示區域比目標每邊向外擴出的距離 offset 和圓角 radius,單位 px,預設都是 6。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTour } from '@nscode/handy-ui'
import type { TourGap, TourStep } from '@nscode/handy-ui'
const open = ref(false)
const gap = ref<TourGap>({})
const cardRef = ref<HTMLElement>()
const steps: TourStep[] = [
{
target: () => cardRef.value,
title: '資料概覽',
description: '這裡匯總了今天的訪問量和訂單數。'
}
]
const show = (value: TourGap) => {
gap.value = value
open.value = true
}
</script>
<template>
<div>
<HuiSpace direction="vertical" align="start" size="large">
<HuiSpace wrap>
<HuiButton @click="show({})">預設(offset 6、radius 6)</HuiButton>
<HuiButton @click="show({ offset: 0, radius: 0 })"
>貼邊直角(offset 0、radius 0)</HuiButton
>
<HuiButton @click="show({ offset: 12, radius: 20 })"
>寬鬆圓角(offset 12、radius 20)</HuiButton
>
</HuiSpace>
<div ref="cardRef" class="overview">
<div>
<div class="overview__label">訪問量</div>
<div class="overview__value">12,846</div>
</div>
<div>
<div class="overview__label">訂單數</div>
<div class="overview__value">326</div>
</div>
</div>
</HuiSpace>
<HuiTour v-model:open="open" :steps="steps" :gap="gap" />
</div>
</template>
<style scoped>
.overview {
display: flex;
gap: var(--hui-spacing-8);
padding: var(--hui-spacing-4) var(--hui-spacing-6);
background: var(--hui-bg-color-secondary);
border-radius: var(--hui-border-radius);
}
.overview__label {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.overview__value {
font-size: var(--hui-font-size-xl);
font-weight: var(--hui-font-weight-semibold);
}
</style>
類型
type 是卡片外觀:default(預設,普通卡片)、primary(主色卡片,白字,按鈕換成白色)。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTour } from '@nscode/handy-ui'
import type { TourStep, TourType } from '@nscode/handy-ui'
const open = ref(false)
const type = ref<TourType>('default')
const shareRef = ref()
const starRef = ref()
const steps: TourStep[] = [
{ target: () => shareRef.value, title: '分享', description: '生成鏈接發給同事。' },
{ target: () => starRef.value, title: '收藏', description: '收藏後可以在首頁快速找到。' }
]
const show = (value: TourType) => {
type.value = value
open.value = true
}
</script>
<template>
<div>
<HuiSpace direction="vertical" align="start" size="large">
<HuiSpace wrap>
<HuiButton @click="show('default')">普通卡片(default)</HuiButton>
<HuiButton type="primary" @click="show('primary')">主色卡片(primary)</HuiButton>
</HuiSpace>
<HuiSpace>
<HuiButton ref="shareRef" icon="share">分享</HuiButton>
<HuiButton ref="starRef" icon="star">收藏</HuiButton>
</HuiSpace>
</HuiSpace>
<HuiTour v-model:open="open" :steps="steps" :type="type" />
</div>
</template>
步驟指示
indicator 是卡片左下角的步驟指示:text(預設,顯示“2 / 4”)、dots(圓點,目前步驟以醒目樣式顯示)、none(不顯示)。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTour } from '@nscode/handy-ui'
import type { TourIndicator, TourStep } from '@nscode/handy-ui'
const open = ref(false)
const indicator = ref<TourIndicator>('text')
const aRef = ref()
const bRef = ref()
const cRef = ref()
const dRef = ref()
const steps: TourStep[] = [
{ target: () => aRef.value, title: '新建', description: '新建一份文件。' },
{ target: () => bRef.value, title: '編輯', description: '修改文件內容。' },
{ target: () => cRef.value, title: '複製', description: '複製一份副本。' },
{ target: () => dRef.value, title: '刪除', description: '刪除後可以在回收站找回。' }
]
const show = (value: TourIndicator) => {
indicator.value = value
open.value = true
}
</script>
<template>
<div>
<HuiSpace direction="vertical" align="start" size="large">
<HuiSpace wrap>
<HuiButton @click="show('text')">文字(text)</HuiButton>
<HuiButton @click="show('dots')">圓點(dots)</HuiButton>
<HuiButton @click="show('none')">不顯示(none)</HuiButton>
</HuiSpace>
<HuiSpace>
<HuiButton ref="aRef" icon="plus">新建</HuiButton>
<HuiButton ref="bRef" icon="edit">編輯</HuiButton>
<HuiButton ref="cRef" icon="copy">複製</HuiButton>
<HuiButton ref="dRef" icon="trash" type="danger" plain>刪除</HuiButton>
</HuiSpace>
</HuiSpace>
<HuiTour v-model:open="open" :steps="steps" :indicator="indicator" />
</div>
</template>
按鈕文字
按鈕文字預設隨語言(中文:上一步、下一步、完成),整個應用設成英文時不用再寫。只想改這一個引導的按鈕文字時用 prev-button-text、next-button-text、finish-button-text:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTour } from '@nscode/handy-ui'
import type { TourStep } from '@nscode/handy-ui'
const open = ref(false)
const inboxRef = ref()
const settingRef = ref()
const steps: TourStep[] = [
{ title: '歡迎', description: '快速認識新功能。' },
{ target: () => inboxRef.value, title: '收件匣', description: '所有訊息都在這裡。' },
{ target: () => settingRef.value, title: '設定', description: '調整個人偏好設定。' }
]
</script>
<template>
<div>
<HuiSpace wrap>
<HuiButton type="primary" @click="open = true">開始導覽</HuiButton>
<HuiButton ref="inboxRef" icon="mail">收件匣</HuiButton>
<HuiButton ref="settingRef" icon="setting">設定</HuiButton>
</HuiSpace>
<HuiTour
v-model:open="open"
:steps="steps"
prev-button-text="上一步"
next-button-text="繼續"
finish-button-text="知道了"
/>
</div>
</template>
關閉方式
預設可以點右上角的關閉按鈕或按 Esc 關閉。show-close 為 false 時不顯示關閉按鈕;keyboard 為 false 時不響應鍵盤(Esc 不關閉,方向鍵不切換)。兩個都關掉時只能走完全部步驟。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTour } from '@nscode/handy-ui'
import type { TourStep } from '@nscode/handy-ui'
const open = ref(false)
const showClose = ref(true)
const keyboard = ref(true)
const helpRef = ref()
const steps: TourStep[] = [
{ target: () => helpRef.value, title: '幫助', description: '遇到問題點這裡。' },
{ title: '完成', description: '點“完成”結束引導。' }
]
const show = (close: boolean, key: boolean) => {
showClose.value = close
keyboard.value = key
open.value = true
}
</script>
<template>
<div>
<HuiSpace direction="vertical" align="start" size="large">
<HuiSpace wrap>
<HuiButton @click="show(true, true)">預設</HuiButton>
<HuiButton @click="show(false, true)">不顯示關閉按鈕(show-close=false)</HuiButton>
<HuiButton @click="show(true, false)">不響應鍵盤(keyboard=false)</HuiButton>
<HuiButton @click="show(false, false)">兩個都關掉</HuiButton>
</HuiSpace>
<HuiButton ref="helpRef" icon="info">幫助</HuiButton>
</HuiSpace>
<HuiTour v-model:open="open" :steps="steps" :show-close="showClose" :keyboard="keyboard" />
</div>
</template>
單步設定
每一步可以單獨設定 placement、mask、type,優先於元件上的同名屬性。下面第 1 步卡片在右邊,第 2 步換成主色卡片,第 3 步沒有遮罩:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTour } from '@nscode/handy-ui'
import type { TourStep } from '@nscode/handy-ui'
const open = ref(false)
const homeRef = ref()
const appsRef = ref()
const userRef = ref()
const steps: TourStep[] = [
{
target: () => homeRef.value,
title: '首頁',
description: '這一步 placement 是 right。',
placement: 'right'
},
{
target: () => appsRef.value,
title: '應用',
description: '這一步 type 是 primary。',
type: 'primary'
},
{
target: () => userRef.value,
title: '個人中心',
description: '這一步 mask 是 false,頁面可以操作。',
mask: false
}
]
</script>
<template>
<div>
<HuiSpace direction="vertical" align="start" size="large">
<HuiButton type="primary" @click="open = true">開始引導</HuiButton>
<HuiSpace>
<HuiButton ref="homeRef" icon="home">首頁</HuiButton>
<HuiButton ref="appsRef" icon="apps">應用</HuiButton>
<HuiButton ref="userRef" icon="user">個人中心</HuiButton>
</HuiSpace>
</HuiSpace>
<HuiTour v-model:open="open" :steps="steps" />
</div>
</template>
插槽
title、預設插槽:自訂標題和正文,參數是{ step, current, total }。indicator:自訂步驟指示,參數是{ current, total }。actions:自訂底部按鈕,參數在上面的基礎上多了isFirst、isLast和prev、next、close三個方法;最後一步呼叫next等同於點“完成”。
width 是卡片寬度,預設 320px,內容多時可以加寬。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTag, HuiTour } from '@nscode/handy-ui'
import type { TourStep } from '@nscode/handy-ui'
const open = ref(false)
const exportRef = ref()
const chartRef = ref()
const steps: TourStep[] = [
{ target: () => exportRef.value, title: '批量匯出', description: '勾選多行後一次匯出。' },
{ target: () => chartRef.value, title: '圖表視圖', description: '把表格資料切換成圖表。' }
]
</script>
<template>
<div>
<HuiSpace direction="vertical" align="start" size="large">
<HuiButton type="primary" @click="open = true">看看新功能</HuiButton>
<HuiSpace>
<HuiButton ref="exportRef" icon="download">匯出</HuiButton>
<HuiButton ref="chartRef" icon="eye">圖表</HuiButton>
</HuiSpace>
</HuiSpace>
<HuiTour v-model:open="open" :steps="steps" :width="360">
<template #title="{ step }">
<HuiSpace size="small">
<span>{{ step.title }}</span>
<HuiTag size="small" type="success">新功能</HuiTag>
</HuiSpace>
</template>
<template #default="{ step, current }">
<p class="tour-text">{{ step.description }}</p>
<p class="tour-tip">小提示:按 ← → 也能切換,這是第 {{ current + 1 }} 個新功能。</p>
</template>
<template #indicator="{ current, total }">
<span class="tour-tip">還剩 {{ total - current - 1 }} 個</span>
</template>
<template #actions="{ isLast, next, close }">
<HuiButton size="small" variant="text" @click="close">跳過</HuiButton>
<HuiButton size="small" type="primary" @click="next">
{{ isLast ? '開始使用' : '繼續' }}
</HuiButton>
</template>
</HuiTour>
</div>
</template>
<style scoped>
.tour-text {
margin: 0;
}
.tour-tip {
margin: var(--hui-spacing-2) 0 0;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
span.tour-tip {
margin: 0;
}
</style>
事件
change:點上一步、下一步或按方向鍵切換了步驟,參數是新的步驟序號。外部改current不觸發。finish:在最後一步點了“完成”。close:點了關閉按鈕或按了 Esc,參數是關閉時所在的步驟。點“完成”不觸發close。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTour } from '@nscode/handy-ui'
import type { TourStep } from '@nscode/handy-ui'
const open = ref(false)
const logs = ref<string[]>([])
const undoRef = ref()
const redoRef = ref()
const refreshRef = ref()
const steps: TourStep[] = [
{ target: () => undoRef.value, title: '撤銷', description: '撤銷上一次修改。' },
{ target: () => redoRef.value, title: '重做', description: '恢復剛才撤銷的修改。' },
{ target: () => refreshRef.value, title: '刷新', description: '重新載入資料。' }
]
const log = (text: string) => {
logs.value = [text, ...logs.value].slice(0, 5)
}
</script>
<template>
<div>
<HuiSpace direction="vertical" align="start" size="large">
<HuiButton type="primary" @click="open = true">開始引導</HuiButton>
<HuiSpace>
<HuiButton ref="undoRef" icon="rotate-left">撤銷</HuiButton>
<HuiButton ref="redoRef" icon="rotate-right">重做</HuiButton>
<HuiButton ref="refreshRef" icon="refresh">刷新</HuiButton>
</HuiSpace>
<div>
<div v-for="(item, index) in logs" :key="index">{{ item }}</div>
<div v-if="!logs.length">打開引導試試,事件會顯示在這裡</div>
</div>
</HuiSpace>
<HuiTour
v-model:open="open"
:steps="steps"
@change="(current) => log(`change:切到第 ${current + 1} 步`)"
@finish="log('finish:完成了引導')"
@close="(current) => log(`close:在第 ${current + 1} 步關閉`)"
/>
</div>
</template>
重新定位
頁面捲動、視窗縮放,以及卡片、目標或頁面的尺寸變化時會自動重新定位。目標被移動、但尺寸沒變(例如改了外距、做了位移動畫)時,呼叫 updatePosition() 手動重新定位。
這個示例關掉了遮罩,打開後點“移動目標”:
<script setup lang="ts">
import { nextTick, ref } from 'vue'
import { HuiButton, HuiSpace, HuiTour } from '@nscode/handy-ui'
import type { TourExpose, TourStep } from '@nscode/handy-ui'
const open = ref(false)
const moved = ref(false)
const tourRef = ref<TourExpose>()
const boxRef = ref<HTMLElement>()
const steps: TourStep[] = [
{
target: () => boxRef.value,
title: '會移動的目標',
description: '點“移動目標”,醒目區域和卡片會跟過去。'
}
]
const move = async () => {
moved.value = !moved.value
await nextTick()
tourRef.value?.updatePosition()
}
</script>
<template>
<div>
<HuiSpace direction="vertical" fill size="large">
<HuiSpace wrap>
<HuiButton type="primary" @click="open = true">打開</HuiButton>
<HuiButton @click="move">移動目標</HuiButton>
</HuiSpace>
<div class="track">
<div ref="boxRef" class="box" :class="{ 'box--moved': moved }">目標</div>
</div>
</HuiSpace>
<HuiTour ref="tourRef" v-model:open="open" :steps="steps" :mask="false" />
</div>
</template>
<style scoped>
.track {
padding-bottom: 160px;
}
.box {
width: 80px;
padding: var(--hui-spacing-3) 0;
text-align: center;
color: var(--hui-color-white);
background: var(--hui-color-primary);
border-radius: var(--hui-border-radius);
}
.box--moved {
margin-left: 240px;
}
</style>
鍵盤操作
| 按鍵 | 作用 |
|---|---|
| Esc | 關閉,觸發 close |
| → | 下一步。最後一步時不動,不會直接完成 |
| ← | 上一步。第一步時不動 |
| Tab | 有遮罩時在卡片裡循環;沒有遮罩時可以移到頁面上 |
打開後焦點移到卡片上,關閉後還給打開前聚焦的元素。焦點在輸入框裡,或者沒有遮罩、焦點在頁面別處時,方向鍵不切換步驟。keyboard 為 false 時 Esc 和方向鍵都不響應。
步驟自持與受控
未傳current或僅監聽更新時保存內部步驟;傳入即受控,顯式undefined按第0步顯示。點擊上一步、下一步及方向鍵都從實際步驟計算請求,父拒絕時保持原標題、指示和插槽狀態。每次重開,非受控步驟仍從第0步開始,重開重置不額外發步驟更新事件。
<script setup lang="ts">
import { computed, ref } from 'vue'
import { HuiButton, HuiTour, HuiSpace } from '@nscode/handy-ui'
import type { TourStep } from '@nscode/handy-ui'
type CurrentMode = 'local' | 'listener' | 'empty' | 'fixed' | 'bound'
const currentMode = ref<CurrentMode>('local')
const currentOpen = ref(false)
const currentBound = ref(1)
const currentRedraw = ref(0)
const currentLast = ref<number | string>('暫無')
const currentSteps: TourStep[] = [
{ title: '甲', description: '第一步' },
{ title: '乙', description: '第二步' },
{ title: '丙', description: '第三步' }
]
const recordCurrent = (value: number) => {
currentLast.value = value
}
const currentBinding = computed(() =>
currentMode.value === 'empty'
? { current: undefined }
: currentMode.value === 'fixed'
? { current: 1 }
: currentMode.value === 'bound'
? {
current: currentBound.value,
'onUpdate:current': (value: number) => {
currentBound.value = value
}
}
: currentMode.value === 'listener'
? { 'onUpdate:current': recordCurrent }
: {}
)
const launchCurrent = (mode: CurrentMode) => {
currentMode.value = mode
currentBound.value = 1
currentLast.value = '暫無'
currentOpen.value = true
}
</script>
<template>
<HuiSpace class="tour-current-q1-demo" direction="vertical" fill>
<HuiSpace wrap>
<HuiButton class="current-local" @click="launchCurrent('local')">未傳步驟</HuiButton>
<HuiButton class="current-listener" @click="launchCurrent('listener')">僅監聽</HuiButton>
<HuiButton class="current-empty" @click="launchCurrent('empty')">顯式 undefined</HuiButton>
<HuiButton class="current-fixed" @click="launchCurrent('fixed')"
>固定第二步,父拒絕</HuiButton
>
<HuiButton class="current-bound" @click="launchCurrent('bound')">雙向綁定</HuiButton>
</HuiSpace>
<span class="current-status"
>模式:{{ currentMode }};監聽請求:{{ currentLast }};綁定步驟:{{
currentBound
}};父重新呈現:{{ currentRedraw }}</span
>
<HuiTour
v-if="currentOpen"
v-model:open="currentOpen"
v-bind="currentBinding"
:steps="currentSteps"
:data-parent="currentRedraw"
>
<template #default="{ step, current }">
<HuiSpace direction="vertical">
<span>{{ step.description }};實際步驟:{{ current }}</span>
<HuiButton class="current-redraw" @click="currentRedraw++"
>父級重新呈現 {{ currentRedraw }}</HuiButton
>
</HuiSpace>
</template>
</HuiTour>
</HuiSpace>
</template>
原生屬性與卡片語義
class、style 落在引導外層;id、data-*、tabindex、aria-* 與未宣告的 DOM 監聽落在實際 role="dialog" 卡片。明確 aria-labelledby 優先;否則存在 aria-label(包括空字串)時使用該名稱,移除屬性後恢復步驟標題關聯。使用方的 aria-describedby 與步驟描述 ID 合併,不覆蓋既有說明。
<script setup lang="ts">
import { ref, useId } from 'vue'
import { HuiButton, HuiSpace, HuiTour } from '@nscode/handy-ui'
const open = ref(false)
const customName = ref<string | undefined>('帳號設定引導')
const helpId = useId()
const dialogId = useId()
const clicks = ref(0)
const target = ref('尚未點擊')
const steps = [
{ title: '設定入口', description: '檢視帳號設定。' },
{ title: '完成設定', description: '檢查並確認設定。' }
]
const record = (event: MouseEvent) => {
clicks.value++
target.value = (event.currentTarget as HTMLElement).getAttribute('role') ?? ''
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace
><HuiButton @click="open = true">開啟引導</HuiButton
><HuiButton @click="customName = customName === undefined ? '帳號設定引導' : undefined"
>切換自訂名稱</HuiButton
></HuiSpace
>
<p :id="helpId">外部說明與目前步驟描述共同關聯至引導卡片。</p>
<span>原生點擊:{{ clicks }};監聽目標:{{ target }}</span>
<HuiTour
:id="dialogId"
v-model:open="open"
:steps="steps"
:mask="false"
:aria-label="customName"
:aria-describedby="helpId"
tabindex="0"
data-guide="account"
class="tour-semantics"
@click="record"
/>
</HuiSpace>
</template>
卡片按鈕保留原行為,原生監聽每次只收到一次通知;event.currentTarget 是卡片,event.target 仍為實際點擊的子節點。屬性可動態更新或移除。
Props
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| open | boolean | false | 是否顯示,支援 v-model:open |
| current | number | — | 從0開始;傳入即受控(含undefined按0顯示),未傳或僅監聽時自持,每次打開從第一步開始 |
| steps | TourStep[] | [] | 步驟,見下面的 TourStep |
| type | 'default' | 'primary' | 'default' | 卡片外觀 |
| mask | boolean | { color?: string } | true | 遮罩。false 不顯示,傳 color 改顏色 |
| gap | { offset?: number; radius?: number } | { offset: 6, radius: 6 } | 醒目標示區域每邊外擴的距離和圓角(px) |
| placement | PopperPlacement | 'bottom' | 卡片相對目標的位置,12 個方向,放不下時自動翻轉 |
| arrow | boolean | true | 是否顯示指向目標的箭頭,沒有目標時不顯示 |
| width | number | string | 320 | 卡片寬度,數字按 px 處理;最寬不超過視口寬度減 16px |
| indicator | 'text' | 'dots' | 'none' | 'text' | 步驟指示:文字“2 / 4”、圓點、不顯示 |
| showClose | boolean | true | 是否顯示右上角的關閉按鈕 |
| keyboard | boolean | true | 是否響應鍵盤:Esc 關閉,左右方向鍵切換 |
| prevButtonText | string | 隨語言(中文:上一步) | “上一步”按鈕的文字 |
| nextButtonText | string | 隨語言(中文:下一步) | “下一步”按鈕的文字 |
| finishButtonText | string | 隨語言(中文:完成) | 最後一步“完成”按鈕的文字 |
TourStep
| 屬性 | 類型 | 說明 |
|---|---|---|
| target | TourTarget | 要醒目標示的目標:選擇器、元素、元件實例、ref,或回傳它們的函式。不寫時卡片在螢幕中間 |
| title | string | 標題 |
| description | string | 說明 |
| placement | PopperPlacement | 這一步卡片的位置,優先於元件的 placement |
| mask | boolean | { color?: string } | 這一步的遮罩,優先於元件的 mask |
| type | 'default' | 'primary' | 這一步卡片的外觀,優先於元件的 type |
Events
| 事件 | 回呼參數 | 說明 |
|---|---|---|
| update:open | (value: boolean) | 關閉(點關閉按鈕、按 Esc、點“完成”)時觸發,值是 false |
| update:current | (value: number) | 切換步驟時觸發 |
| change | (current: number) | 點上一步、下一步或按方向鍵切換了步驟。外部改 current 不觸發 |
| finish | — | 在最後一步點了“完成” |
| close | (current: number) | 點了關閉按鈕或按了 Esc,參數是關閉時所在的步驟。點“完成”、外部改 open 不觸發 |
Slots
| 插槽 | 參數 | 說明 |
|---|---|---|
| title | { step, current, total } | 標題 |
| default | { step, current, total } | 正文,替換步驟的 description |
| indicator | { current, total } | 步驟指示 |
| actions | { step, current, total, isFirst, isLast, prev, next, close } | 底部按鈕,替換“上一步”“下一步”按鈕 |
方法
| 方法 | 說明 |
|---|---|
| updatePosition | 重新計算醒目標示區域和卡片位置。目標被移動(不是捲動、縮放引起的)後呼叫 |
類型
import type {
TourProps,
TourEmits,
TourStep,
TourTarget,
TourTargetElement,
TourType,
TourMask,
TourGap,
TourIndicator,
TourStepSlotProps,
TourIndicatorSlotProps,
TourActionsSlotProps,
TourSlots,
TourExpose
} from '@nscode/handy-ui'