Tour Lifecycle Events
Cue.js provides a set of powerful event hooks that allow you to execute custom JavaScript code at specific moments during your tour's lifecycle. This is essential for integrating with analytics, performing dynamic UI updates, or managing tour flow based on user interactions or application state.
How to Register Event Callbacks
You can register a callback function for each event using methods on your Cue
instance. You can register multiple callbacks for the same event.
All callback functions receive the Cue
instance as their this
context.
Available Event Hooks
Event Method
When it Fires
Arguments Passed to Callback ((arg1, arg2, ... )
)
cue.onStart()
Once, when cue.start()
is called and the first step is about to be shown.
()
(No arguments)
cue.onBeforeChange()
Just before the tour transitions to a new step. Elements are not yet visible.
(stepData, newStepIndex)
cue.onChange()
After a step's elements (highlight, tooltip) are initially displayed and positioned.
(stepData, currentStepIndex)
cue.onAfterChange()
After a step's elements have fully animated into place and are stable.
(stepData, currentStepIndex)
cue.onExit()
When the tour is stopped prematurely (e.g., user clicks 'Skip', presses Esc, or cue.exit()
is called).
(currentStepIndex)
cue.onComplete()
When the user successfully navigates to the very last step and clicks 'Done'.
()
(No arguments)
cue.onResize()
Whenever the browser window is resized while the tour is active.
()
(No arguments)
Event Details & Use Cases
cue.onStart(callback)
cue.onStart(callback)
Fires: Exactly once, as soon as
cue.start()
is invoked, before any visual elements of the first step appear.Arguments: None.
Use Cases:
Analytics: Log that a user has started a specific tour.
UI Preparation: Disable other interactive elements on the page that might interfere with the tour.
Initial Setup: Perform any global setup required for the tour.
cue.onBeforeChange(callback)
cue.onBeforeChange(callback)
Fires: Just before the tour transitions from one step to another. At this point, the new step's data is available, but its visual elements are not yet on the screen, and the old step's elements might still be visible.
Arguments:
stepData
: The full object for the new step that the tour is about to transition to.newStepIndex
: The 0-based index of the new step.
Use Cases:
Conditional Logic: Perform checks on the
newStepIndex
orstepData
to decide if specific actions are needed before the step renders.Early UI Adjustments: Prepare elements that need to be ready precisely when the new step is about to appear.
cue.onChange(callback)
cue.onChange(callback)
Fires: Immediately after a step's overlay, highlight, and tooltip elements have been added to the DOM and initially positioned. This happens as soon as the step appears on screen, even if CSS transitions are still animating.
Arguments:
stepData
: The full object for the currently displayed step.currentStepIndex
: The 0-based index of the currently displayed step.
Use Cases:
Analytics: Track views of individual tour steps.
Dynamic Content: Update text or subtle UI elements based on the current step.
Debugging: Log current step information.
cue.onAfterChange(callback)
cue.onAfterChange(callback)
Fires: A short delay (matching
transitionDuration
) afteronChange
. This means all CSS transitions for the step's elements should have completed, and the step is fully stable visually.Arguments:
stepData
: The full object for the currently displayed step.currentStepIndex
: The 0-based index of the currently displayed step.
Use Cases:
Post-Animation Actions: Perform actions that should only happen once the UI is settled (e.g., precise focus management, or enabling/disabling elements based on stable positions).
Confirm Visual Readiness: Ensures the step is fully presented before performing sensitive UI changes or tests.
cue.onExit(callback)
cue.onExit(callback)
Fires: When the tour is stopped prematurely by the user (clicking 'Skip', pressing
Escape
, clicking the overlay ifoverlayClickExits
is true) or by callingcue.exit()
programmatically.Arguments:
currentStepIndex
: The 0-based index of the step that was active when the tour was exited.
Use Cases:
Cleanup: Re-enable previously disabled UI elements, close any open temporary modals or menus.
Analytics: Record that a tour was abandoned and at which step.
State Reset: Reset any application state that was modified specifically for the tour.
cue.onComplete(callback)
cue.onComplete(callback)
Fires: Only when the user successfully navigates through all steps and clicks the "Done" button on the very last step.
Arguments: None.
Use Cases:
Success Message: Display a "Tour Complete!" message or animation.
Post-Tour Actions: Redirect the user, open a survey, or grant a badge.
Analytics: Record a successful tour completion.
cue.onResize(callback)
cue.onResize(callback)
Fires: When the browser window is resized while the tour is currently active.
Arguments: None.
Use Cases:
Debugging: Monitor layout changes.
Custom Refresh: While Cue.js automatically calls
_positionElements()
on resize, if you have very complex custom UI around the tour elements, you might need to trigger additional layout logic.
Important Considerations for Callbacks
Keep Callbacks Lean: Avoid heavy, blocking operations inside callbacks, especially those that fire frequently like
onChange
oronAfterChange
, to maintain a smooth user experience.Error Handling: If your callbacks perform complex logic, consider wrapping them in
try...catch
blocks to prevent errors in your custom code from breaking the entire tour.Asynchronous Operations: For
preStep
andpostStep
, you can useasync
functions andawait
to ensure operations complete before the tour proceeds or finishes, respectively.
Next Steps
Now that you understand the lifecycle and event hooks, let's explore how to customize the visual appearance of your tours.
Last updated