Vitral 0.2.18
Sobreposição

Tour

Um tour guiado sobre a página, conduzido por props ou pelo código, com passos que esperam, pulam, avançam sozinhos e lembram onde o leitor parou.

Importação

main.ts
import { Tour } from '@vitral/vue';

Básico

`v-model:open` o inicia e o encerra. As setas movem entre os passos, Escape o encerra, e cada popover é um diálogo que um leitor de tela lê quando chega.

<script setup lang="ts">import { share as shareIcon } from '@vitral/icons';import { Button, InputText, Tour, type TourStep } from '@vitral/vue';import { ref } from 'vue'; const open = ref(false);const create = ref<{ $el: HTMLElement } | null>(null);const search = ref<{ $el: HTMLElement } | null>(null);const share = ref<{ $el: HTMLElement } | null>(null); const steps: TourStep[] = [    { popover: { title: 'Welcome', description: 'A quick look at the three things you will use most. It takes a minute.' } },    { element: () => create.value, popover: { title: 'Create', description: 'Starts a new document from scratch or from a template.' } },    { element: () => search.value, popover: { title: 'Search', description: 'Finds documents, people and settings.', side: 'bottom', align: 'center' } },    { element: () => share.value, popover: { title: 'Share', description: 'Invites people, with the access you choose.', side: 'left', align: 'center' } }];</script> <template>    <div style="display: flex; flex-direction: column; gap: 1rem; width: 100%">        <div style="display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center">            <Button ref="create" label="New" icon="plus" />            <InputText ref="search" placeholder="Search" aria-label="Search" style="flex: 1 1 12rem" />            <Button ref="share" label="Share" :icon="shareIcon" variant="outlined" severity="secondary" />        </div>        <Button label="Start the tour" icon="play" variant="text" style="align-self: flex-start" @click="open = true" />        <Tour v-model:open="open" :steps="steps" show-progress />    </div></template>

Progresso, lados e a sobreposição

`progressStyle` desenha o progresso como texto, pontos ou uma barra; `side` e `align` posicionam o popover; `overlayClickBehavior="nextStep"` faz um toque na página escurecida avançar.

top
right
bottom
left
<script setup lang="ts">import { Button, SelectButton, Tour, type TourStep } from '@vitral/vue';import { ref } from 'vue'; const open = ref(false);const style = ref<'text' | 'dots' | 'bar'>('dots');const boxes = ref<HTMLElement[]>([]); const sides = ['top', 'right', 'bottom', 'left'] as const;const steps: TourStep[] = sides.map((side, i) => ({    element: () => boxes.value[i],    popover: { title: `side: '${side}'`, description: `align: 'center'. A press on the dimmed page moves on.`, side, align: 'center' }}));</script> <template>    <div style="display: flex; flex-direction: column; gap: 1rem; width: 100%">        <div style="display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center">            <SelectButton v-model="style" :options="['text', 'dots', 'bar']" aria-label="Progress style" :allow-empty="false" />            <Button label="Start" icon="play" @click="open = true" />        </div>        <div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; padding: 3rem 0">            <div v-for="side in sides" :key="side" ref="boxes" style="padding: 1rem; text-align: center; border: 1px dashed var(--vt-content-border-color); border-radius: 0.5rem">{{ side }}</div>        </div>        <Tour v-model:open="open" :steps="steps" show-progress :progress-style="style" overlay-click-behavior="nextStep" />    </div></template>

Passos que esperam e avançam sozinhos

`advanceOn` avança quando o leitor faz o que o passo pede, `beforeShow` é aguardado antes de um passo aparecer, `waitFor` espera por um elemento que ainda não está lá, e `when` deixa de fora um passo que não se aplica.

<script setup lang="ts">import { settings as settingsIcon } from '@vitral/icons';import { Button, Checkbox, Tour, type TourStep } from '@vitral/vue';import { ref } from 'vue'; const open = ref(false);const loaded = ref(false);const admin = ref(false);const load = ref<{ $el: HTMLElement } | null>(null);const report = ref<HTMLElement | null>(null);const settings = ref<{ $el: HTMLElement } | null>(null); const steps: TourStep[] = [    {        element: () => load.value,        popover: { title: 'Load a report', description: 'Press the button — the tour moves on when you do.', showButtons: ['close'] },        advanceOn: { event: 'click' }    },    {        // Not on the page until the report has loaded: the step waits for it.        element: () => report.value,        waitFor: 3000,        popover: { title: 'Your report', description: 'It arrived a moment after the press, and this step waited for it.' }    },    {        // Only for someone who can change the settings.        when: () => admin.value,        element: () => settings.value,        popover: { title: 'Settings', description: 'Shown only because “I am an admin” is ticked.' }    },    { popover: { title: 'Done', description: 'Steps that do not apply were left out of the count too.' } }]; function start() {    loaded.value = false;    open.value = true;} function loadReport() {    setTimeout(() => (loaded.value = true), 400);}</script> <template>    <div style="display: flex; flex-direction: column; gap: 1rem; width: 100%">        <div style="display: flex; flex-wrap: wrap; gap: 1rem; align-items: center">            <Button label="Start" icon="play" @click="start" />            <label style="display: inline-flex; gap: 0.5rem; align-items: center"><Checkbox v-model="admin" binary /> I am an admin</label>        </div>        <div style="display: flex; flex-wrap: wrap; gap: 0.5rem">            <Button ref="load" label="Load report" icon="download" variant="outlined" @click="loadReport" />            <Button ref="settings" label="Settings" :icon="settingsIcon" variant="text" severity="secondary" />        </div>        <div v-if="loaded" ref="report" style="padding: 1rem; border: 1px solid var(--vt-content-border-color); border-radius: 0.5rem">Q3 report: revenue up 12%.</div>        <Tour v-model:open="open" :steps="steps" show-progress />    </div></template>

Movimento, posição e rolagem

Desligado por padrão: `animate` move o destaque de passo em passo e faz cada popover aparecer suavemente, em `animation-duration` milissegundos. `side: 'auto'` põe o popover onde houver espaço, e com `arrow` a seta acompanha. `scroll-into-view` traz cada elemento à vista — `smooth-scroll` para uma rolagem suave — e, desligado, a página fica onde está enquanto o popover continua na tela.

First
Second
<script setup lang="ts">import { Button, Checkbox, Slider, Tour, type TourStep } from '@vitral/vue';import { ref } from 'vue'; const open = ref(false);const animate = ref(true);const duration = ref(400);const arrow = ref(true);const smooth = ref(true);const scroll = ref(true); // `side: 'auto'` takes whichever side has room, and the arrow follows it.// With `scroll-into-view` off the page stays where it is; the popover stays// on the screen anyway, beside wherever its element went.const steps: TourStep[] = [    { element: '#demo-motion-a', popover: { title: 'Top of the section', description: 'The highlight moves to the next step instead of jumping.', side: 'auto' } },    { element: '#demo-motion-b', popover: { title: 'Far below', description: 'Reached by scrolling, smoothly when asked.', side: 'auto', align: 'center' } },    { element: '#demo-motion-a', popover: { title: 'And back', description: 'Placed on whichever side has room.', side: 'auto' } }];</script> <template>    <div style="display: flex; flex-direction: column; gap: 0.75rem; width: 100%">        <div style="display: flex; flex-wrap: wrap; gap: 1rem; align-items: center">            <label style="display: inline-flex; gap: 0.4rem; align-items: center"><Checkbox v-model="animate" binary /> animate</label>            <label style="display: inline-flex; gap: 0.5rem; align-items: center">duration <Slider v-model="duration" :min="0" :max="1200" :step="100" style="width: 8rem" aria-label="Duration" /> {{ duration }} ms</label>            <label style="display: inline-flex; gap: 0.4rem; align-items: center"><Checkbox v-model="arrow" binary /> arrow</label>            <label style="display: inline-flex; gap: 0.4rem; align-items: center"><Checkbox v-model="scroll" binary /> scroll into view</label>            <label style="display: inline-flex; gap: 0.4rem; align-items: center"><Checkbox v-model="smooth" binary :disabled="!scroll" /> smooth</label>            <Button label="Start" icon="play" @click="open = true" />        </div>        <div id="demo-motion-a" style="padding: 0.75rem; border: 1px dashed var(--vt-content-border-color); border-radius: 0.5rem; align-self: flex-start">First</div>        <div style="height: 70vh" aria-hidden="true" />        <div id="demo-motion-b" style="padding: 0.75rem; border: 1px dashed var(--vt-content-border-color); border-radius: 0.5rem; align-self: flex-end">Second</div>        <Tour            v-model:open="open"            :steps="steps"            :animate="animate"            :animation-duration="duration"            :arrow="arrow"            :scroll-into-view="scroll"            :smooth-scroll="smooth"            show-progress            progress-style="dots"        />    </div></template>

Entre páginas

Um passo pode indicar a sua `page`. Chegar a um passo em outra página vai até lá primeiro: pelo `navigate` quando há um router (aqui, um substituto para um), ou carregando a página, com um `<Tour>` lá retomando o tour sozinho (`resume()` sem Vue).

Inbox — 3 new
<script setup lang="ts">import { barChart, inbox, settings } from '@vitral/icons';import { Button, Icon, SelectButton, Tour, type TourStep } from '@vitral/vue';import { nextTick, ref } from 'vue'; // A stand-in for a router: three pages, one on show. With vue-router the two// options below are `navigate: (page) => router.push(page)` and// `currentPage: () => router.currentRoute.value.path`. Without a router,// leave `navigate` out: the tour loads the page, and a <Tour> there picks up.const page = ref('/inbox');const pages = ['/inbox', '/reports', '/settings'];const open = ref(false); async function navigate(to: string) {    page.value = to;    await nextTick();} const steps: TourStep[] = [    { element: '#demo-page-inbox', page: '/inbox', popover: { title: 'Inbox', description: 'Everything addressed to you. Next, the tour goes to Reports by itself.' } },    { element: '#demo-page-reports', page: '/reports', popover: { title: 'Reports', description: 'This step is on another page: the tour went there first, then found it.' } },    { element: '#demo-page-settings', page: '/settings', popover: { title: 'Settings', description: 'And one more page. Previous goes back the same way.' } }];</script> <template>    <div style="display: flex; flex-direction: column; gap: 0.75rem; width: 100%">        <div style="display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center">            <SelectButton v-model="page" :options="pages" :allow-empty="false" aria-label="Page" />            <Button label="Start the tour" icon="play" @click="open = true" />        </div>        <div style="padding: 1rem; border: 1px solid var(--vt-content-border-color); border-radius: 0.5rem; min-height: 6rem">            <div v-if="page === '/inbox'" id="demo-page-inbox" style="display: inline-flex; gap: 0.5rem; align-items: center; padding: 0.5rem 0.75rem"><Icon :icon="inbox" />Inbox — 3 new</div>            <div v-else-if="page === '/reports'" id="demo-page-reports" style="display: inline-flex; gap: 0.5rem; align-items: center; padding: 0.5rem 0.75rem"><Icon :icon="barChart" />Reports — Q3</div>            <div v-else id="demo-page-settings" style="display: inline-flex; gap: 0.5rem; align-items: center; padding: 0.5rem 0.75rem"><Icon :icon="settings" />Settings</div>        </div>        <Tour v-model:open="open" :steps="steps" :navigate="navigate" :options="{ currentPage: () => page }" show-progress />    </div></template>

Um tour para o app inteiro

`useGlobalTour()` devolve o tour da própria aplicação, o mesmo a partir de qualquer componente: ele pertence ao app e não à página que o iniciou, então um onboarding que passa por várias telas continua através das trocas de rota. A opção `tour` do plugin define de onde todo tour parte — `<Tour>`, `useTour()` e este — e as props de cada tour ficam por cima.

<script setup lang="ts">import { Button, useGlobalTour } from '@vitral/vue'; // In an application this sits where the tour is started — a help menu, a// first visit — and every page reaches the same tour with `useGlobalTour()`.// Defaults for every tour go in the plugin://   app.use(Vitral, { tour: { showProgress: true, progressStyle: 'dots', dismissableMask: false } })const tour = useGlobalTour(); function start() {    tour.setSteps([        { element: '#vt-app-tour-inbox', popover: { title: 'Inbox', description: 'Everything addressed to you. On another page, the same tour would carry on.' } },        { element: '#vt-app-tour-reports', popover: { title: 'Reports', description: 'The tour is the app’s, not this component’s: it ends when it is finished or the app goes away.' } }    ]);    tour.drive();}</script> <template>    <div style="display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center">        <Button id="vt-app-tour-inbox" label="Inbox" icon="bell" variant="outlined" severity="secondary" />        <Button id="vt-app-tour-reports" label="Reports" icon="file" variant="outlined" severity="secondary" />        <Button label="Start the app tour" icon="play" variant="text" @click="start" />    </div></template>

Pelo código

`useTour()` devolve o próprio tour — `drive()`, `moveNext()`, `highlight()`, `destroy()` — e aqui `highlight()` mostra um elemento sozinho.

<script setup lang="ts">import { Button, useTour } from '@vitral/vue';import { ref } from 'vue'; const help = ref<{ $el: HTMLElement } | null>(null);const tour = useTour(); function explain() {    tour.highlight({        element: help.value,        popover: { title: 'Help', description: 'Opens the help centre, and the chat with support.', side: 'right', align: 'center' }    });}</script> <template>    <div style="display: flex; flex-wrap: wrap; gap: 1rem; align-items: center">        <Button ref="help" icon="info" aria-label="Help" rounded variant="outlined" severity="secondary" />        <Button label="What is this button?" variant="text" @click="explain" />    </div></template>

API

Lido de packages/vue/src/components/Tour/types.ts, então diz o que o componente aceita de fato.

Props

NomeTipoDescrição
steps TourStep[]—
animate booleanMove o destaque de passo em passo e faz cada popover aparecer suavemente. Desligado por padrão.
animationDuration numberQuanto tempo dura um movimento quando `animate` está ligado, em milissegundos. O padrão é 300.
overlayColor string—
overlayOpacity number—
smoothScroll boolean—
scrollIntoView booleanTraz o elemento de cada passo à vista. O padrão é true; desligado, a página fica parada e o popover continua na tela.
scrollBlock 'start' | 'center' | 'end' | 'nearest'Onde um elemento rolado até a vista fica na janela. O padrão é `'center'`.
keepInView booleanMantém o popover na tela mesmo quando o elemento não está. O padrão é true.
navigate (page: string) => void | Promise<unknown>Vai a outra página para um passo cuja `page` não é esta — o `push` de um router: `(page) => router.push(page)`. Sem ele, a página é carregada, e um `<Tour>` lá retoma o tour sozinho.
allowClose booleanEscape, o botão de fechar e um toque na sobreposição encerram o tour. O padrão é true.
overlayClickBehavior 'close' | 'nextStep'—
dismissableMask boolean`false` impede que um toque na sobreposição encerre o tour, como na máscara de um dialog. O padrão é true.
arrow booleanUma seta do popover apontando para o elemento. Desligada por padrão.
stagePadding numberO espaço em volta do elemento, em pixels. O padrão é 2, para que o destaque o contorne de perto.
stageRadius numberO canto do recorte. O padrão é o canto do próprio elemento, somado ao espaço em volta.
disableActiveInteraction boolean—
allowKeyboardControl booleanO padrão é true.
popoverClass string—
popoverOffset number—
showButtons TourButton[]—
disableButtons TourButton[]—
showProgress boolean—
progressText string`'{current} de {total}'`; o da locale caso contrário.
progressStyle TourProgressStyle—
nextBtnText string—
prevBtnText string—
doneBtnText string—
allowHtml booleanTítulos e descrições são HTML. Só para texto que você escreveu.
elementTimeout numberQuanto tempo um passo espera o seu elemento aparecer, em milissegundos.
missingElement 'center' | 'skip'—
storageKey stringLembra onde o leitor parou, e se terminou, sob esta chave.
options TourConfigTodo o resto que `createTour` aceita: os hooks, `storage`, `dir`.
appendTo string | HTMLElement—

Mais pt, dt e unstyled de BaseProps; veja pass-through e modo sem estilo.

Slots

NomeProps do slotDescrição
content (context: TourStepContext)Desenhado no popover abaixo da descrição, em todo passo.