|Kit

Tour

Guided onboarding steps that highlight parts of the page. An imperative manager like the notifier and the overlays manager, typed from a registry, with steps a product manager can gate on what the user actually does.

A tour walks the user through a feature, one highlighted element at a time. It's built on Ark UI's Tour, but the kit owns the flow: tours are data you register once, tours.start() runs one from anywhere, and each step can wait for the user to actually do the thing (click, type, submit, land on a page) before it moves on.

Code

const result = await tours.start("projects")

Playground

Tour

Loading preview

Props

Code

<Tour
  position="bottom"
  withArrow
  withBackdrop
  withCloseButton
/>

Setup

A tour is data, defined next to the feature it explains:

features/projects/tour.ts
import { click, defineTour, value } from "@42-central/ui-react/tour";

export const projectSetup = defineTour({
  version: 1, // bump it to show the tour again after a redesign
  steps: [
    { id: "welcome", type: "dialog", title: "Create your first project" },
    { id: "name", target: "#project-name", title: "Give it a name",
      until: value((v) => v.trim().length >= 3, { hint: "At least 3 characters" }) },
    { id: "create", target: "[data-tour=create]", title: "Create it", until: click() },
    { id: "done", type: "dialog", title: "Your project is ready" },
  ],
});

A tour that reads params names them first. The call is curried so step ids are still inferred:

export const projectSetup = defineTour<{ campus?: string }>()({
  steps: [
    { id: "campus", target: "[data-tour=campus]", title: "Pick a campus",
      when: (ctx) => ctx.params.campus === undefined },
    // …
  ],
});

createTours takes the registry and returns the manager. Call it once in a "use client" module, and mount <Tours /> once near the root:

app/tours.ts
"use client";
import { createTours, localStorageTours } from "@42-central/ui-react/tour";

export const { tours, Tours, useTours, useTourGate } = createTours({
  registry: { projectSetup },
  location: { get: () => router.state.location.pathname, subscribe: router.history.subscribe },
  storage: localStorageTours("42:tours"),
  onEvent: (event) => analytics.track(event.type, event),
});

Step ids, params, gate keys and signal names are all checked against the registry, with no module augmentation: tours.start("projectSetup", { from: "nope" }) is a type error, and so is a missing required param.

CallWhat it does
tours.start(key, { params, from })Starts a tour and resolves with { status, stepId, answers } when it ends.
tours.startOnce(key, options)Starts only if this version was never completed, skipped or dismissed; resolves null otherwise. Needs storage.
tours.resume(key, options)Continues from the saved step, or starts from the first.
tours.reset(key)Forgets stored progress, for a "Replay the tour" item.
tours.next(), back(), goto(id), choose(value)Moves the running tour.
tours.skip(), tours.dismiss()Ends it as skipped or dismissed.
tours.signal(name, payload)Tells the running tour a business event happened.
tours.update(patch)Patches the current step's title, description or actions.

One tour runs at a time: a second start waits for the first to end, and a tour never starts over an open modal; it starts when the modal closes.

Steps

A step's type picks its shape. tooltip (the default) anchors a card to target; dialog centres one; wait shows no card and waits on its until. A step whose target isn't on the page yet shows a loading card instead; after wait.timeout (5 seconds) Skip step and Close appear, and the step carries on whenever the target shows up.

Code

{ id: "late", target: "[data-tour=late]", wait: { timeout: 3000 } }

choices branch on an answer, which comes back in the result:

Code

choices: [
  { label: "Cadet", value: "cadet" },
  { label: "Staff", value: "staff", goto: "staff" },
]

A wait step has no card. Its help shows a small one when the wait drags on:

Code

{ id: "saving", type: "wait", until: signal("saved"),
help: { after: 1500, hint: "Still saving, hang on" } }

A floating step with no target puts its card on the viewport, 1rem in from the edges. Its position picks the spot: the side is the edge the card sits on, and -start or -end slides it along that edge, mirrored under right-to-left on the top and bottom edges. With no alignment, the card is centred on that edge. So "bottom" is bottom centre, "top-start" is the top left corner (top right under RTL), and "start" is the middle of the start edge. Without a position, the card sits in the bottom end corner.

Code

{ id: "here", type: "floating", position: "top-start", title: "Up here" }

The page's edges

The tour never makes the page scroll. The backdrop covers the whole document, even on a page that scrolls down or sideways, but nothing the tour draws reaches past it. A spotlight around a target that fills the window or touches its edge, like a full-window canvas or a full-height side panel, is cut off at the document's edge. A card that has no room on either side of a target as tall as the page slides back onto the page, over the target.

Step validation

Every gate goes through one prop, until. The step's Next stays disabled (its tooltip says why) until the condition is met. Actions advance by themselves after a short tick; typed values and all() show the tick and wait for Next.

click()

Code

{ id: "create", target: "[data-tour=create]", until: click() }

value() and value.valid()

The hint waits until the field loses focus or the user pauses typing. value.valid() reads the browser's constraint validation, so a field's required or pattern is the check.

Code

until: value((v) => v.trim().length >= 3, { hint: "At least 3 characters" })
until: value.valid()

submit()

Code

until: submit()

route()

Through the location adapter, so it works with any router. Without one, it reads window.location.

/projects

Code

until: route("/projects/:id")

element()

Code

until: element("[data-tour=panel]") // or { state: "hidden" }

signal()

Fired by the app with tours.signal(). A signal fired earlier in the same run counts too.

Code

until: signal("project:saved")
// later, in the app
tours.signal("project:saved")

check()

Asks the server, with a loading state, an AbortSignal that fires when the step is left, and an optional interval.

git@vogsphere:libft.git

Code

until: check(async (ctx) => api.isCloned(ctx.params.repo, { signal: ctx.signal }), { interval: 1000 })

all() and any()

all() shows a small checklist in the card.

Code

until: all({ title: value(…, { label: "Set a title" }), date: value(…, { label: "Pick a date" }) })

Code

until: any(click({ target: "[data-tour=create]" }), submit())

gate() and useTourGate()

For state that lives in React. The component that owns it reports it; useTourGate comes from createTours(), typed against the registry's gate keys. This demo also shows help: wait five seconds.

Code

until: gate("avatar"), help: { after: 5000, hint: "Flip the switch", pulse: true }

// in the component that owns the state
useTourGate("avatar", uploaded ? "valid" : "pending")

Custom conditions

The helpers are sugar over condition(). Report the state now, subscribe to what can change it, and return the unsubscribe:

const hasAvatar = condition((ctx, report) => {
  const check = (user: User) => report(user.avatar ? "valid" : "pending")
  check(userStore.getState())
  return userStore.subscribe(check)
})

report takes "pending", "valid" or { invalid: "Upload a photo first" }. Type ctx.app with condition<{ queryClient: QueryClient }>(…); a tour using it then declares the same app, and createTours requires it.

Stacking

The tour sits above modals, so a step can point inside one. A Select, Menu or other floating panel opened while a tour runs is lifted above the tour's card. See Stacking.

Code

{ id: "name", target: "[data-tour=modal-name]", title: "Inside the dialog" }

Code

{ id: "campus", target: "[data-tour=campus]", until: gate("campus") }

Accessibility

  • The card is an alertdialog. Its title and description label it, and focus moves into it, trapped across the card and the highlighted element, so the element stays usable.
  • Hints are announced politely, and only once due: value() holds its hint back while the user types.
  • Next is never hidden without a reason. While a step is gated it's aria-disabled, stays focusable, and its tooltip says what's missing.
  • The keyboard is never trapped. Close and Escape always end the tour, even on a required step. An open picker raised above the tour takes Escape first.
  • A click outside the card doesn't end the tour. Only Close and Escape do, so a stray click on the page never throws away the user's progress. The highlighted element stays clickable.
  • Arrow keys move between steps from inside the card, mirrored under right-to-left, and never past an unmet gate.
  • Focus comes back to where it was when the tour started, unless the user moved it elsewhere.
  • Motion is optional. The spotlight's slide between steps and the help pulse turn off under prefers-reduced-motion.
  • Page shortcuts yield to a running tour, like they do to a modal.

Anatomy

  • Tours — the renderer from createTours(). Mount it once.
  • Tour.Backdrop — the dimmed page, with the target cut out.
  • Tour.Spotlight — the box over the target: its hairline ring and the help pulse. On a step with interactive: false, it takes the clicks.
  • Tour.Positioner, Tour.Content — the card, on the popover surface for anchored steps and Modal's surface for dialog steps.
  • Tour.Arrow — the shared floating arrow.
  • Tour.Title, Tour.Description — label the card.

Pass createTours({ render }) to replace the default card; it renders inside the tour's root, so these parts work there.

testId

<Tours testId="tour" /> lands on the card; each part gets ${testId}-${part}: layer (the box every part sits in, the document's size), backdrop, spotlight, title, description, hint, progress, close, skip, back, next, skip-step. See Testing.

API

createTours(config):

Prop

Type

A step:

Prop

Type

<Tours />:

Prop

Type

Changelog

v0.31.0
  • The tour never grows the page: every part sits in one clipping layer over the document

    #513
  • A floating step with no target is placed by its position instead of always the bottom end corner

    #513
  • A card with no room beside its target slides back onto the page, and only when it would actually leave it

    #513
  • A custom card wrapper's own pointer-events-none wins again

    #514
v0.30.0
  • New Tour: guided onboarding tours from a typed registry, with steps that wait for a click, a typed value, a route, a signal and more

    #489

On this page