Dynamic Content
Many modern web applications feature content that loads asynchronously, appears based on user interaction, or is conditionally rendered. This "dynamic content" can pose a challenge for guided tours, as the elements you want to highlight might not be present in the DOM when the tour is initialized or when a specific step is activated.
Cue.js provides powerful step-specific options to gracefully handle dynamic content and ensure a smooth tour experience.
The Challenge
By default, Cue.js will try to find the target
element for each step just before that step is displayed. If the element isn't found, the tour might stop or behave unexpectedly. Dynamic content scenarios (e.g., loading data from an API, lazy-loaded components, tabbed interfaces) require a way to:
Ensure the target element is present and ready before Cue.js tries to highlight it.
Handle situations where the target element genuinely cannot be found, even after preparation.
Solutions in Cue.js
Cue.js addresses these challenges with two key step properties: preStep
and onTargetNotFound
.
1. preStep
for Preparation
preStep
for PreparationThe preStep
callback allows you to execute custom code before a tour step's highlight and tooltip are displayed. This is the perfect place to manipulate the DOM, trigger data loads, or open UI elements that contain your target.
Type:
Function
(supportsasync
functions)When it fires: Just before the current step's visual elements appear on the page.
Arguments:
(stepData)
- The full object of the step about to be displayed.Key Feature: If
preStep
is anasync
function, Cue.js will wait for the returned Promise to resolve before rendering the step. This is crucial for asynchronous operations.
Use Case 1: Revealing a Hidden Element
Imagine you have a section of your page that is initially hidden, and a tour step needs to highlight an element within it.
Use Case 2: Opening a Tab or Accordion Section
If your target is inside a tab that's not currently active or an accordion section that's closed:
Use Case 3: Waiting for Asynchronous Data (Async preStep
)
If your target element is rendered only after an API call completes:
Tip: You can combine
preStep
withscrollToElement: true
to ensure that after your content is revealed/loaded, the tour automatically scrolls to it for the user.
2. onTargetNotFound
for Graceful Handling
onTargetNotFound
for Graceful HandlingSometimes, even with preStep
, a target element might genuinely not appear (e.g., a server error, user permissions prevent rendering, a network issue). The onTargetNotFound
callback allows you to control the tour's behavior in such scenarios.
Type:
Function
When it fires: When Cue.js attempts to find the
target
element for a step, anddocument.querySelector()
(or the directHTMLElement
reference) returnsnull
.Arguments:
(stepData)
- The full object of the step whose target was not found.Return Value:
Return
'skip'
to automatically skip the current step and proceed to the next one in the tour.Return
'stop'
to immediately stop the entire tour at this point.If nothing is returned, the tour will stop by default (equivalent to
'stop'
).
Use Case: Skipping a Step for Unavailable Features
Consider a tour that includes a step for a premium feature, which might not be visible to free users.
Combining preStep
and onTargetNotFound
preStep
and onTargetNotFound
For robust handling, it's often best to use both preStep
(to try to make the element available) and onTargetNotFound
(to handle cases where it still isn't available).
Next Steps
By effectively using preStep
and onTargetNotFound
, you can build tours that are resilient to the dynamic nature of modern web applications.
Last updated