Command palette

@yasinsensoy/web

Web · Components

Tour

Product tour (onboarding): highlights elements on the page in order and shows an explanation card next to them.

19 exports

Live example

Preview

Acme
Revenue₺48.200
New customers126
Open tasks18

Code

import { Button, Tour, toast } from "@yasinsensoy/web";

const [open, setOpen] = useState(false);

<Button onClick={() => setOpen(true)}>Start the tour</Button>
<Tour
  open={open}
  onOpenChange={setOpen}
  steps={[
    { title: "Welcome 👋", content: "Let us show you the most important places.", nextLabel: "Let's start" },
    { target: "#search", icon: "search", title: "Search everything", content: "In one box." },
    { target: "#new", title: "New record", content: "Press it and try." },
    { title: "You're ready 🎉" },
  ]}
  onComplete={() => toast.success("Tour completed")}
/>

Events

  1. Events appear here when you interact with the preview.

Import

import {
  Tour,
  resolveTarget,
  TOUR_AUTO,
  placeCard,
  getTourState,
  resetTour,
} from "@yasinsensoy/web";

Tour

componentClient component

Product tour (onboarding): highlights elements on the page in order and shows an explanation card next to them. Steps are given with the steps array; each step has a target (target), title and description.

  • Highlight: The target's surroundings are dimmed; when the step changes the highlight slides to the new target. If the target isn't on screen it is scrolled into view; when the page scrolls or the element resizes, the highlight and card follow together.
  • Placement: The card goes to the side where it fits (side, default "auto") and draws an arrow pointing to the target. On mobile the card docks to the bottom of the screen.
  • Persistence: With storageKey a finished tour doesn't open again, an interrupted one continues. The state is read and reset with getTourState and resetTour.
  • Flexible steps: With onEnter a menu can be opened or data awaited before the step; a target rendered later is waited for. With advanceOnClick "press this" steps are written.
  • Accessibility: The card is a dialog; focus stays inside the card and returns to its old place when the tour ends. ← → and Esc work. The mobile back button closes the tour.
  • The tour opens in the top layer; no z-index needed. Renders nothing on the server; no hydration mismatch.
Tour({ steps, open, defaultOpen, onOpenChange, step, defaultStep, onStepChange, onComplete, onDismiss, storageKey, resume, ...options }: TourProps): ReactPortal | null

Examples

<Tour
  storageKey="welcome"
  steps={[
    { title: "Welcome!", content: "Let us show you a few things.", nextLabel: "Let's start" },
    { target: "#search", title: "Search", content: "Find everything here." },
    { target: "[data-tour='new']", title: "New record", content: "Press it and try.", advanceOnClick: true },
    { title: "You're ready", content: "You can reopen the tour from the Help menu." },
  ]}
/>

// A tour opened by a button:
const [open, setOpen] = useState(false);
<Button onClick={() => setOpen(true)}>Start tour</Button>
<Tour open={open} onOpenChange={setOpen} steps={steps} />

Looks for the target in the DOM; returns null if not found.

resolveTarget(target: TourTarget | undefined): Element | null

TOUR_AUTO

constant

"auto" order of the Tour card: below first, then above, then the sides.

TOUR_AUTO: readonly FixedPlacement[]

placeCard

function

Computes the card position. Picks the first of the tried positions that fits on screen; if none fits, picks the one that overflows least and clamps it into the screen. Without a target the card is centered.

placeCard(hole: TourRect | null, card: { width: number; height: number; }, viewport: TourViewport, placement?: TourPlacement, gap?: number, margin?: number): CardPosition

getTourState

function

Reads the state of a tour saved with storageKey. Use it to show a "Restart tour" option in a "Help" menu or to start the tour only for new users.

getTourState(storageKey: string): TourState

Examples

if (getTourState("welcome").status === "completed") hideBadge();

resetTour

function

Deletes the saved tour; the tour starts over on the next load (or when a Tour rendered with storageKey mounts again). Changing the key is also enough to version the tour ("welcome-v2").

resetTour(storageKey: string): void

Examples

<Button onClick={() => { resetTour("welcome"); setOpen(true); }}>Restart tour</Button>

TourRect

interface

A rectangle on screen (relative to the viewport).

NameTypeDescription
xnumberLeft edge.
ynumberTop edge.
widthnumberWidth.
heightnumberHeight.

TourViewport

interface

Size of the viewport.

NameTypeDescription
widthnumberWidth.
heightnumberHeight.

Element highlighted by the step:

  • A CSS selector: "#save", "[data-tour='search']".
  • The element itself or a ref obtained with useRef.
  • A function returning the element (called on every attempt if the element renders later).

If omitted, the step shows as a card in the center of the screen without a highlight (for welcome and closing steps).

type TourTarget = string | Element | { readonly current: Element | null; } | (() => Element | null | undefined)

CardPosition

interface

Computed position of the card.

NameTypeDescription
xnumberLeft edge of the card.
ynumberTop edge of the card.
sideTourSide | nullSide of the target the card sits on; null when centered.
arrownumber | nullDistance of the arrow along the card edge. null when no arrow is drawn (when the card had to overlap the target or there is no target).

Saved state of a tour.

  • "new": never shown.
  • "active": left halfway (the app closed or the user navigated away).
  • "completed": went through to the last step.
  • "dismissed": the user skipped or closed the tour.
type TourStatus = "new" | "active" | "completed" | "dismissed"

TourState

interface

Saved state and, if left halfway, the step reached.

NameTypeDescription
statusTourStatusStatus. See TourStatus.
stepnumberIndex of the step reached (starts at 0).

Values received when the step content is given as a function.

NameTypeDescription
indexnumberIndex of the shown step (starts at 0).
totalnumberTotal number of steps.
next() => voidMoves to the next step; finishes the tour on the last step.
back() => voidReturns to the previous step.
goTo(index: number) => voidGoes to the step at the given index.
close() => voidCloses the tour (counts as skipped).

TourStep

interface

A step of Tour.

NameTypeDefaultDescription
id?stringId of the step; for onStepChange and logs.
target?TourTargetElement to highlight: a CSS selector ("#save"), an element, a ref or a function returning the element. If omitted, the card shows in the center of the screen without a highlight (welcome and closing steps). See TourTarget.
title?ReactNodeTitle; screen readers announce the card by this name.
content?ReactNode | ((context: TourStepContext) => ReactNode)Description. If a function is given it receives TourStepContext; you can use your own buttons (e.g. "Try it now") in the content.
media?ReactNodeImage or video shown edge to edge at the top of the card.
icon?IconPropIcon next to the title: name or element.
side?Placementthe Tour's `side` valuePosition of the card relative to the target. See Placement.
padding?numberthe Tour's `padding` valueSpace the highlight leaves around the element (px).
radius?numberthe Tour's `radius` valueCorner radius of the highlight (px).
interactive?booleanfalse` (`true` when `advanceOnClick` is given)Whether the highlighted element is clickable. When off, the whole screen is closed to clicks.
advanceOnClick?booleanfalseWhether clicking the highlighted element moves to the next step. For "press this button" steps. The Next button stays visible, and the keyboard can advance too.
nextLabel?ReactNodeText of the Next button in this step (e.g. "Let's start").
hideBack?booleanfalseHides the Back button in this step.
onEnter?() => void | Promise<void>Called before the step is shown: to open a menu, switch a tab or load data. If it returns a Promise, the Next button shows loading until it ends and the step is shown afterwards.
onLeave?() => voidCalled when leaving the step (moving to another step or closing the tour).
scroll?booleantrueWhether the target is scrolled into view if it isn't on screen.
className?stringClasses added to this step's card.

Reason the tour closed.

  • "close": Close (×) or the "Skip" button on the first step.
  • "escape": The Esc key.
  • "overlay": Clicking the dimmed area (closeOnOverlayClick).
  • "back": The browser's or phone's back button.
type TourDismissReason = "close" | "escape" | "overlay" | "back"

Progress indicator: "dots" dots, "count" "2 / 5", "bar" a thin bar at the top of the card, "none" none. "auto" shows dots up to 7 steps, a count beyond that.

type TourProgress = "auto" | "dots" | "count" | "bar" | "none"

TourLabels

interface

Button and screen reader texts of Tour. Defaults come from the dictionary's ui texts (tourNext, tourBack…); the labels prop overrides them.

NameTypeDefaultDescription
nextstringDefaults to the UIProvider text ("İleri" in Turkish).
backstringDefaults to the UIProvider text ("Geri" in Turkish).
donestringButton on the last step. Defaults to the UIProvider text ("Bitir" in Turkish).
skipstringButton in place of Back on the first step. Defaults to the UIProvider text ("Geç" in Turkish).
closestringName of the close (×) button. Defaults to the UIProvider text ("Turu kapat" in Turkish).
progress(step: number, total: number) => string(step, total) => "2 / 5"Progress text.

Classes that can be passed to the inner parts of Tour.

NameTypeDescription
spotlight?stringHighlight (the hole with dimmed surroundings).
media?stringMedia area of the card.
title?stringTitle.
content?stringDescription.
footer?stringBottom row holding the buttons.

TourProps

interface

Props of the Tour component.

NameTypeDefaultDescription
stepsreadonly TourStep[]Steps. See TourStep.
open?booleanWhether the tour is open in controlled use.
defaultOpen?booleantrue` if `storageKey` is set, otherwise `falseWhether the tour starts automatically when the page opens. Defaults to true when storageKey is given: the tour starts only if it wasn't finished or skipped before.
onOpenChange?(open: boolean) => voidCalled with the new state when it opens or closes.
step?numberIndex of the shown step in controlled use (starts at 0).
defaultStep?number0Initial step in uncontrolled use.
onStepChange?(index: number, step: TourStep) => voidCalled with the new index and step when the step changes.
onComplete?() => voidCalled when Finish is pressed on the last step.
onDismiss?(index: number, reason: TourDismissReason) => voidCalled with the step where it stopped and the reason when the tour closes before finishing.
storageKey?stringName under which the tour is remembered in the browser. If given, a finished or skipped tour doesn't open by itself again; an interrupted tour (on page reload) continues from where it stopped. Change the name to version it ("welcome-v2"). See getTourState, resetTour.
resume?booleantrueWhether a tour interrupted with storageKey continues from where it stopped.
side?Placement"auto"Default position of the card relative to the target. See Placement.
padding?number8Space of the highlight around the element (px).
radius?number10Corner radius of the highlight (px).
mask?booleantrueWhether the surroundings of the target are dimmed. When off, only a colored frame shows around the target.
progress?TourProgress"auto"Progress indicator. See TourProgress.
dismissible?booleantrueCan the user close the tour (×, Esc, "Skip", back button)? When off, the tour closes only by finishing it.
closeOnOverlayClick?booleanfalseWhether clicking the dimmed area closes the tour.
closeOnBackButton?booleantrueWhether the back button closes the tour.
keyboard?booleantrueKeyboard: ← → move between steps (directions flip in right-to-left writing), Esc closes.
missingTarget?"center" | "skip""center"What to do if the target isn't found? "center" shows the card in the center and moves to the target if it renders later; "skip" skips the step.
targetTimeout?number1500If the target isn't on the page yet (e.g. a menu opened in onEnter), how long to wait at most (ms)?
mobileDock?booleantrueWhether on small screens (below 640 px) the card docks to the bottom of the screen (to the top if the target is low). The card can be swiped left/right with a finger to change steps.
labels?Partial<TourLabels>Texts. See TourLabels.
className?stringClasses added to the card (e.g. w-96 for width).
classNames?TourClassNamesClasses added to the inner parts.