Command palette

@yasinsensoy/native

React Native · Components

Tour

Product tour (onboarding): highlights elements on screen one by one and shows a description card next to them.

21 exports

Live example

The preview runs in the browser with the web component; the native component takes the same props and looks the same on iOS and Android.

Preview

Acme
Revenue₺48.200
New customers126
Open tasks18

Code

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

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 {
  TourTarget,
  Tour,
  TOUR_AUTO,
  placeCard,
  getTourState,
  resetTour,
} from "@yasinsensoy/native";

Tour

component

Product tour (onboarding): highlights elements on screen one by one and shows a description card next to them. Same steps and behavior as the web Tour; targets are given by TourTarget name or ref instead of a CSS selector.

  • Highlight: The target's surroundings are dimmed; on step change the highlight slides to the new target. With a scroll tracker, an off-screen target is scrolled into view and the highlight and card follow the element while scrolling.
  • Placement: The card sits on the side where it fits (side) and draws an arrow pointing at the target (calculation shared with web). On phones the card sticks to the bottom of the screen; swiping left/right changes steps.
  • Persistence: With storageKey (+ storage) a finished tour never opens again and an interrupted one resumes.
  • Flexible steps: onEnter can open a screen or wait for data before a step; a target rendered later is waited for. advanceOnClick writes "press this" steps.
  • Accessibility: The card is announced like a modal window and progress is read on step change; the Android back button closes the tour.
  • Looks the same on iOS and Android; opens in the library's own layer.
Tour({ steps, open, defaultOpen, onOpenChange, step, defaultStep, onStepChange, onComplete, onDismiss, storageKey, storage, resume, ...options }: TourProps): JSX.Element | null

Examples

const scroll = useScrollTracker();
<ScrollView ref={scroll.ref} onScroll={scroll.onScroll} scrollEventThrottle={16}>
  <TourTarget name="search"><TextField placeholder="Search" /></TourTarget>
  <TourTarget name="new" style={{ alignSelf: "flex-start" }}>
    <Button>New record</Button>
  </TourTarget>
</ScrollView>
<Tour
  storageKey="welcome"
  storage={storage}
  scroll={scroll}
  steps={[
    { title: "Welcome!", content: "Let us show you a few things.", nextLabel: "Let's start" },
    { target: "search", title: "Search", content: "Find everything here." },
    { target: "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." },
  ]}
/>

TourTarget

component

Marks content by name for Tour steps. Native counterpart of an element found by CSS selector on web: a step highlights this element with target: "search". It does not block touches on the content; in advanceOnClick steps pressing the content advances the tour.

TourTarget({ name, children, style }: TourTargetProps): JSX.Element

Examples

<TourTarget name="search">
  <TextField placeholder="Search" />
</TourTarget>
<Tour steps={[{ target: "search", title: "Search", content: "Find everything here." }]} />

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 (same record format as web). Use it to show a "Restart tour" option in a "Help" menu or to open the tour only for new users. storage must be the same store given to Tour.

getTourState(storageKey: string, storage?: PreferenceStorage): Promise<TourState>

Examples

const state = await getTourState("welcome", storage);
if (state.status === "completed") hideBadge();

resetTour

function

Deletes the saved tour; the tour starts over on the next launch (or when a Tour rendered with storageKey remounts). Changing the key is also enough to change the tour's version ("welcome-v2").

resetTour(storageKey: string, storage?: PreferenceStorage): void

Examples

<Button onPress={() => { resetTour("welcome", storage); 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.

Position of the card relative to the target (same names as Placement on web and native): "auto", a side ("bottom") or a side and alignment ("bottom-left", "right-top").

type TourPlacement = "auto" | TourSide | `${"top" | "bottom"}-${"left" | "center" | "right"}` | `${"left" | "right"}-${"top" | "center" | "bottom"}`

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).

The element a step highlights:

  • A name: an element marked with TourTarget ("search"). If the element renders later it is waited for; advanceOnClick works only with this.
  • A View ref obtained with useRef.
  • A function returning the element (called on every attempt).

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

type TourTargetRef = string | { readonly current: TourMeasurable | null; } | (() => TourMeasurable | null | undefined)

Props of the TourTarget component.

NameTypeDescription
namestringName used in a step's target field. Must be unique in the app.
children?ReactNodeContent to highlight.
style?StyleProp<ViewStyle>Style of the wrapper (e.g. alignSelf: "flex-start").

Values a step's content receives when given as a function.

NameTypeDescription
indexnumberIndex of the shown step (starting 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 (same fields as web).

NameTypeDefaultDescription
id?stringStep id; for onStepChange and logs.
target?TourTargetRefElement to highlight: a TourTarget name ("search"), a View ref or a function returning the element. If omitted the card appears in the middle of the screen without a highlight (welcome and closing steps). See TourTargetRef.
title?ReactNodeTitle; screen readers announce the card with this name.
content?ReactNode | ((context: TourStepContext) => ReactNode)Description. A function 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: a name or an element.
side?TourPlacementthe Tour's `sidePosition of the card relative to the target.
padding?numberthe Tour's `paddingGap the highlight leaves around the element (dp).
radius?numberthe Tour's `radiusCorner radius of the highlight (dp).
interactive?booleanfalse` (`true` when `advanceOnClick` is given)Can the highlighted element be touched? When off, the whole screen blocks touches.
advanceOnClick?booleanfalseMove to the next step when the highlighted element is pressed? For "press this button" steps; the target must be given by its TourTarget name.
nextLabel?ReactNodeLabel 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 tabs or load data. If it returns a Promise the Next button shows loading until it settles and the step is shown afterwards.
onLeave?() => voidCalled when leaving the step (moving to another step or closing the tour).
scroll?booleantrueIf the target is off screen, should it be scrolled into view with the Tour's scroll tracker?
style?ViewStyleStyle of this step's card.

Why the tour closed (same as web).

  • "close": Close (×) or the "Skip" button on the first step.
  • "escape": Not used on native (the Esc key on web).
  • "overlay": Tapping the dimmed area (closeOnOverlayClick).
  • "back": Android 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" nothing. "auto" shows dots up to 7 steps and a count beyond.

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

TourLabels

interface

Button and screen reader texts of Tour.

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.

TourStyles

interface

Styles of the Tour inner parts.

NameTypeDescription
card?ViewStyleCard.
spotlight?ViewStyleHighlight (the hole with dimmed surroundings, or a frame).
media?ViewStyleMedia area of the card.
footer?ViewStyleBottom row holding the buttons.

TourProps

interface

Props of the Tour component (same names as web).

NameTypeDefaultDescription
stepsreadonly TourStep[]Steps. See TourStep.
open?booleanWhether the tour is open, in controlled use.
defaultOpen?booleantrue` if `storageKey` is given, otherwise `falseShould the tour start by itself when the screen opens? Defaults to true when storageKey is given: the tour starts only if it was not 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 (starting 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 reached and the reason when the tour is closed before finishing.
storageKey?stringName under which the tour is remembered. When given, a finished or skipped tour never opens by itself again; an interrupted tour resumes from its step. Pass storage to make it persistent. See getTourState, resetTour.
storage?PreferenceStorageWhere the state is stored (a @yasinsensoy/native/storage store can be passed directly). If omitted it is forgotten when the app closes.
resume?booleantrueShould a tour interrupted with storageKey resume from its step?
scroll?ScrollTrackerTracker of the scroll area the targets live in (useScrollTracker). When given, an off-screen target is scrolled into view and the highlight follows the element as the user scrolls.
side?TourPlacement"auto"Default position of the card relative to the target.
padding?number8Gap of the highlight around the element (dp).
radius?number10Corner radius of the highlight (dp).
mask?booleantrueDim the target's surroundings? When off, only a colored frame appears around the target.
progress?TourProgress"auto"Progress indicator. See TourProgress.
dismissible?booleantrueCan the user close the tour (×, "Skip", back button)? When off, the tour only closes by finishing.
closeOnOverlayClick?booleanfalseClose the tour when the dimmed area is tapped?
closeOnBackButton?booleantrueShould the Android back button close the tour?
missingTarget?"center" | "skip""center"What to do if the target is not found? "center" shows the card in the middle; "skip" skips the step.
targetTimeout?number1500If the target has not rendered yet (e.g. a screen opened in onEnter), how long to wait at most (ms)?
mobileDock?booleantrueOn phones (below 640 dp), should the card stick to the bottom of the screen (to the top if the target is at the bottom)? The card can be swiped left/right to change steps.
labels?Partial<TourLabels>Texts. See TourLabels.
styles?TourStylesStyles of the inner parts. See TourStyles.
testID?stringTest id (given to the card).