# Tour (/docs/tour)





A **tour** walks the user through a feature, one highlighted element at a time. It's built on [Ark UI's Tour](https://ark-ui.com/), 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.

<Preview align="center" code="`const result = await tours.start(&#x22;projects&#x22;)`">
  <TourBasicDemo />
</Preview>

## Playground [#playground]

<story.Playground />

## Setup [#setup]

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

```tsx title="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:

```tsx
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:

```tsx title="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.

| Call                                                  | What 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 [#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.

<Preview align="center" code="`{ id: &#x22;late&#x22;, target: &#x22;[data-tour=late]&#x22;, wait: { timeout: 3000 } }`">
  <TourMissingTargetDemo />
</Preview>

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

<Preview align="center" code="&#x22;choices: [\n  { label: \&#x22;Cadet\&#x22;, value: \&#x22;cadet\&#x22; },\n  { label: \&#x22;Staff\&#x22;, value: \&#x22;staff\&#x22;, goto: \&#x22;staff\&#x22; },\n]&#x22;">
  <TourChoicesDemo />
</Preview>

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

<Preview align="center" code="&#x22;{ id: \&#x22;saving\&#x22;, type: \&#x22;wait\&#x22;, until: signal(\&#x22;saved\&#x22;),\nhelp: { after: 1500, hint: \&#x22;Still saving, hang on\&#x22; } }&#x22;">
  <TourWaitDemo />
</Preview>

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.

<Preview align="center" code="`{ id: &#x22;here&#x22;, type: &#x22;floating&#x22;, position: &#x22;top-start&#x22;, title: &#x22;Up here&#x22; }`">
  <TourFloatingDemo />
</Preview>

### The page's edges [#the-pages-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 [#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()` [#click]

<Preview align="center" code="`{ id: &#x22;create&#x22;, target: &#x22;[data-tour=create]&#x22;, until: click() }`">
  <TourClickDemo />
</Preview>

### `value()` and `value.valid()` [#value-and-valuevalid]

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.

<Preview align="center" code="&#x22;until: value((v) => v.trim().length >= 3, { hint: \&#x22;At least 3 characters\&#x22; })\nuntil: value.valid()&#x22;">
  <TourValueDemo />
</Preview>

### `submit()` [#submit]

<Preview align="center" code="`until: submit()`">
  <TourSubmitDemo />
</Preview>

### `route()` [#route]

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

<Preview align="center" code="`until: route(&#x22;/projects/:id&#x22;)`">
  <TourRouteDemo />
</Preview>

### `element()` [#element]

<Preview align="center" code="`until: element(&#x22;[data-tour=panel]&#x22;) // or { state: &#x22;hidden&#x22; }`">
  <TourElementDemo />
</Preview>

### `signal()` [#signal]

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

<Preview align="center" code="&#x22;until: signal(\&#x22;project:saved\&#x22;)\n// later, in the app\ntours.signal(\&#x22;project:saved\&#x22;)&#x22;">
  <TourSignalDemo />
</Preview>

### `check()` [#check]

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

<Preview align="center" code="`until: check(async (ctx) => api.isCloned(ctx.params.repo, { signal: ctx.signal }), { interval: 1000 })`">
  <TourCheckDemo />
</Preview>

### `all()` and `any()` [#all-and-any]

`all()` shows a small checklist in the card.

<Preview align="center" code="`until: all({ title: value(…, { label: &#x22;Set a title&#x22; }), date: value(…, { label: &#x22;Pick a date&#x22; }) })`">
  <TourAllDemo />
</Preview>

<Preview align="center" code="`until: any(click({ target: &#x22;[data-tour=create]&#x22; }), submit())`">
  <TourAnyDemo />
</Preview>

### `gate()` and `useTourGate()` [#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.

<Preview align="center" code="&#x22;until: gate(\&#x22;avatar\&#x22;), help: { after: 5000, hint: \&#x22;Flip the switch\&#x22;, pulse: true }\n\n// in the component that owns the state\nuseTourGate(\&#x22;avatar\&#x22;, uploaded ? \&#x22;valid\&#x22; : \&#x22;pending\&#x22;)&#x22;">
  <TourGateDemo />
</Preview>

### Custom conditions [#custom-conditions]

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

```ts
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 [#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](/docs/stacking).

<Preview align="center" code="`{ id: &#x22;name&#x22;, target: &#x22;[data-tour=modal-name]&#x22;, title: &#x22;Inside the dialog&#x22; }`">
  <TourInModalDemo />
</Preview>

<Preview align="center" code="`{ id: &#x22;campus&#x22;, target: &#x22;[data-tour=campus]&#x22;, until: gate(&#x22;campus&#x22;) }`">
  <TourSelectDemo />
</Preview>

## Accessibility [#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 [#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`*&#x2A;, &#x2A;*`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`*&#x2A;, &#x2A;*`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` [#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](/docs/testing).

## API [#api]

`createTours(config)`:

<TypeTable
  type="{
  registry: { type: &#x22;Record<string, TourDefinition>&#x22;, required: true, description: &#x22;The tours, by key, each built with defineTour.&#x22; },
  app: { type: &#x22;App&#x22;, description: &#x22;Handed to every condition as ctx.app. Required when a registered tour's conditions need it.&#x22; },
  location: { type: &#x22;{ get(): string; subscribe(listener): () => void }&#x22;, default: &#x22;window.location&#x22;, description: &#x22;Where route() reads the pathname.&#x22; },
  storage: { type: &#x22;TourStorage&#x22;, description: &#x22;Enables startOnce, resume and reset. localStorageTours(namespace) ships with the kit.&#x22; },
  onEvent: { type: &#x22;(event: TourEvent) => void&#x22;, description: &#x22;tour:start, step:show, step:invalid, step:hint, step:complete, step:skip, step:waiting, step:timeout, tour:end.&#x22; },
  translations: { type: &#x22;TourTranslations&#x22;, description: &#x22;progressText, next, back, done, skip, skipStep, close, loading, valid, blocked.&#x22; },
  render: { type: &#x22;(props: TourRenderProps) => ReactNode&#x22;, description: &#x22;Replaces the default card.&#x22; },
}"
/>

A step:

<TypeTable
  type="{
  id: { type: &#x22;string&#x22;, required: true, description: &#x22;Unique within the tour. goto, from and the events use it.&#x22; },
  type: { type: '&#x22;tooltip&#x22; | &#x22;floating&#x22; | &#x22;dialog&#x22; | &#x22;wait&#x22;', default: '&#x22;tooltip&#x22;', description: &#x22;The step's shape.&#x22; },
  target: { type: &#x22;string | RefObject | () => HTMLElement | null&#x22;, description: &#x22;The element to highlight. Required on tooltip steps.&#x22; },
  title: { type: &#x22;ReactNode&#x22;, description: &#x22;The card's title.&#x22; },
  description: { type: &#x22;ReactNode&#x22;, description: &#x22;The card's body.&#x22; },
  position: { type: &#x22;Placement&#x22;, default: '&#x22;bottom&#x22;', description: &#x22;Where the card sits. Logical start/end are RTL-aware. On a floating step with no target, the spot on the viewport; defaults to \&#x22;bottom-end\&#x22; there.&#x22; },
  until: { type: &#x22;Condition&#x22;, description: &#x22;The gate the user must meet to move on.&#x22; },
  advance: { type: '&#x22;auto&#x22; | &#x22;manual&#x22;', description: &#x22;Overrides the condition's default.&#x22; },
  help: { type: &#x22;{ after: number; hint?: string; pulse?: boolean }&#x22;, description: &#x22;Help for a user who is stuck.&#x22; },
  required: { type: &#x22;boolean&#x22;, default: &#x22;false&#x22;, description: &#x22;Hides Skip. Close and Escape still end the tour.&#x22; },
  when: { type: &#x22;(ctx) => boolean&#x22;, description: &#x22;Shows the step only when true.&#x22; },
  choices: { type: &#x22;{ label; value; goto? }[]&#x22;, description: &#x22;Branches on an answer.&#x22; },
  wait: { type: &#x22;{ timeout?: number }&#x22;, default: &#x22;{ timeout: 5000 }&#x22;, description: &#x22;When Skip step and Close appear while the target is missing.&#x22; },
  interactive: { type: &#x22;boolean&#x22;, default: &#x22;true&#x22;, description: &#x22;Whether the highlighted element stays clickable. Always true on a gated step.&#x22; },
  withArrow: { type: &#x22;boolean&#x22;, default: &#x22;true&#x22;, description: &#x22;Draws the arrow.&#x22; },
  withBackdrop: { type: &#x22;boolean&#x22;, default: &#x22;true&#x22;, description: &#x22;Dims the page.&#x22; },
}"
/>

`<Tours />`:

<TypeTable
  type="{
  color: { type: &#x22;Color&#x22;, description: &#x22;The card's accent.&#x22; },
  classNames: { type: &#x22;Partial<Record<TourSlot, string>>&#x22;, description: &#x22;A class per part.&#x22; },
  icons: { type: &#x22;{ close?, check? }&#x22;, description: &#x22;Replaces the card's icons.&#x22; },
  withArrow: { type: &#x22;boolean&#x22;, default: &#x22;true&#x22;, description: &#x22;Draws the arrow. A step's own wins.&#x22; },
  withBackdrop: { type: &#x22;boolean&#x22;, default: &#x22;true&#x22;, description: &#x22;Dims the page. A step's own wins.&#x22; },
  withCloseButton: { type: &#x22;boolean&#x22;, default: &#x22;true&#x22;, description: &#x22;Shows the close button.&#x22; },
  testId: { type: &#x22;string&#x22;, description: &#x22;data-testid on the card, and a prefix for each part.&#x22; },
}"
/>
