useTrail

Package: @trailguide/runtime

A low-level React hook that wraps the Trailguide core class. Gives you imperative start, stop, next, prev, skip, and goToStep controls along with reactive state.

For most use cases, prefer useTrailManager, which adds first-visit logic and progress tracking.

Usage

import { useTrail, Trailguide } from '@trailguide/runtime'
import { myTour } from './tours'
 
export function MyComponent() {
  const { isActive, start, stop, next, prev } = useTrail({
    trail: myTour,
    onComplete: () => console.log('done'),
  })
 
  return (
    <>
      <button onClick={start}>Start tour</button>
      {isActive && (
        <>
          <button onClick={prev}>Back</button>
          <button onClick={next}>Next</button>
          <button onClick={stop}>Stop</button>
          <Trailguide trail={myTour} />
        </>
      )}
    </>
  )
}

Or use autoStart to start immediately on mount:

const { isActive, stop } = useTrail({
  trail: myTour,
  autoStart: true,
})

Options

OptionTypeDefaultDescription
trailTrailThe tour definition. Required.
autoStartbooleanfalseStart the tour immediately on mount.
onComplete() => voidCalled when user finishes the last step.
onSkip() => voidCalled when user clicks the × button.
onStepChange(step, index) => voidCalled on every step transition.
analyticsAnalyticsConfigEnable Pro analytics.

Return value

PropertyTypeDescription
isActivebooleanWhether the tour is currently running.
currentStepStep | nullThe step currently being shown, or null if inactive.
currentStepIndexnumber0-based index of the current step.
totalStepsnumberTotal number of steps in the tour.
start() => voidStart the tour from step 0.
stop() => voidStop the tour (calls onAbandoned internally).
next() => voidAdvance to the next step.
prev() => voidGo back to the previous step.
skip() => voidSkip the tour (calls onSkip).
goToStep(index: number) => voidJump to a specific step by index.