Dynamic targets
Real applications render late, re-render often, and sometimes never show the element a tour expects. Each step declares what should happen in that case, and waiting is driven by a mutation observer rather than a polling timer.
Waiting for an element that does not exist yet
Start the tour and press Next. The second step is waiting for data-bonboarding-anchor="late-panel",
which is not on the page yet, so a small card with a spinner and a close button says so - and the page stays
usable, because the user is often the one who has to make the element appear. Reveal the panel (now, or after
two seconds) and the step picks it up the moment it renders.
While it waits, the session's Status is Waiting and its WaitReason is
Target; once the target is on screen, the step's own interaction mode and focus trap apply. An element
that exists but is not rendered - display: none, a closed <details>, no size - counts
as missing too, and a target that disappears while its step is on screen is waited for again (the
re-render sample below shows it). Waiting uses a mutation observer, never a
polling timer, so a step that waits for ten seconds costs nothing while the page is idle.
new BonboardingStepDefinition
{
Id = "late",
Target = BonboardingStepTarget.Anchor("late-panel"),
Title = "There it is",
MissingTarget = BonboardingMissingTargetBehavior.Wait, // the default
WaitTimeout = TimeSpan.FromSeconds(30), // default ten seconds; TimeSpan.Zero waits for as long as it takes
}
@* Replace the default waiting card with your own: *@
<BonboardingRoot>
<WaitingTemplate>Loading the next part of the page…</WaitingTemplate>
</BonboardingRoot>Missing-target strategies
Each button runs a two-step tour whose first step points at an element that is not on this page. Only the strategy differs, and the second step shows that the tour carried on.
| MissingTarget | What happens | Choose it when |
|---|---|---|
Wait (default) |
Watch the DOM until the element appears, then show the step. After WaitTimeout (default ten
seconds; three here) apply WaitTimeoutBehavior (default Skip).
|
The element is on its way: a table behind a query, a panel behind an animation. |
Skip |
Move on silently, in the direction the user was going. | The step is nice to have - though a When condition says "not for this user" more clearly. |
Center |
Show the step as a centred card, with no spotlight. | The text stands on its own and pointing at the element is a bonus. |
Fail |
End the tour with BonboardingTourEndReason.Failed and a BonboardingException. |
A missing element means something is genuinely broken, and you want to know. |
Every miss is also published as a TargetMissing event (and a skip as StepSkipped), so a
step that is quietly skipped in production shows up in your analytics.
new BonboardingStepDefinition
{
Target = BonboardingStepTarget.Css("#definitely-not-here"),
MissingTarget = BonboardingMissingTargetBehavior.Wait, // or Skip, Center, Fail
WaitTimeout = TimeSpan.FromSeconds(3),
WaitTimeoutBehavior = BonboardingMissingTargetBehavior.Skip, // what a wait that ran out does; Wait counts as Skip
}
// Fail ends the tour; OnEnded sees why.
OnEnded = args => args.Reason == BonboardingTourEndReason.Failed ? Report(args.Error) : ValueTask.CompletedTask,Let the user do it
A step with an AdvanceOn trigger waits for a real interaction rather than a Next button.
BonboardingAdvanceTrigger.TargetClick listens for a click on the step's own target, and declaring a
trigger switches the step to Interaction = TargetOnly: the spotlight hole becomes click-through, so
the user can actually press the button they are being told about, and nothing else.
new BonboardingStepDefinition
{
Target = BonboardingStepTarget.Anchor("do-it"),
Title = "Now click this button",
AdvanceOn = BonboardingAdvanceTrigger.TargetClick, // a click on the step's own target
ShowNext = false, // the click is the only way forward
}Any event, on any element
A trigger can listen for any DOM event (EventName), on any element (Selector; the step's
target when unset), and can filter delegated events by where they came from (MatchSelector).
DelayMs lets the interface settle before the tour moves on. The first step below advances on the
select's change event; the second listens on the whole group of buttons but only moves on for
Large - try another size first.
new BonboardingStepDefinition
{
Target = BonboardingStepTarget.Anchor("plan-picker"),
AdvanceOn = new BonboardingAdvanceTrigger { EventName = "change", DelayMs = 400 },
ShowNext = false,
},
new BonboardingStepDefinition
{
Target = BonboardingStepTarget.Anchor("size-picker"),
AdvanceOn = new BonboardingAdvanceTrigger
{
EventName = "click",
Selector = null, // listen on the step's target (the default)
MatchSelector = "[data-size='large']", // only events that came from a matching element
},
ShowNext = false,
}Advancing on something the application does
Some tasks are not one DOM event: a project counts as created when the server says so, not when a button is
pressed. The code that knows calls SignalAsync, and the step waiting for that signal
(AdvanceOnSignal) moves on - with no reference to the tour or the session. The button below takes
a second and a half to "save"; the step moves on when the save completes, not when you click.
A signal is not remembered: one raised while no step waits for it does nothing, so SignalAsync returns
whether a tour moved on. Use When to skip a step whose task the user had already done before reaching it.
CanAdvance still decides, for signals as for every other way forward.
new BonboardingStepDefinition
{
Target = BonboardingStepTarget.Anchor("create-project"),
Title = "Create a project",
AdvanceOnSignal = "project-created",
ShowNext = false,
Interaction = BonboardingInteractionMode.TargetOnly,
}
// Wherever projects are created - no reference to the tour needed:
await _projects.CreateAsync(name);
var movedOn = await BonboardingService.SignalAsync("project-created");Targets that are replaced, move or go away
Blazor often throws a DOM node away and renders an identical one, and layouts move things with nothing resized.
Once a step is showing, observers keep the popover glued to its target; when the target leaves the DOM, the step's
anchor or selector is looked up again and the step silently re-binds to the new element. Only when nothing
matches is the target reported lost, and then the step's MissingTarget strategy applies, exactly as
it did when the step started.
Run the tour and use the buttons in the spotlight. Replace this element renders a brand-new element on the other side, and the step follows it. Remove it for two seconds leaves nothing to re-bind to, so the step waits - with the page usable - and comes back when the element does.
Element #1
A new DOM node every time it is replaced.
This is why anchors and selectors are preferred over a captured ElementReference, which names one
node and cannot be found again - and why a row of a virtualized list, recycled as the user scrolls, still works.
If your own code moves a target in a way the browser does not report - animating a CSS transform, say - call
session.RefreshAsync() for a fresh measurement.
Targets chosen at run time
BonboardingStepTarget.Dynamic defers the decision until the step activates, so a tour can point
at whichever row, tab or card is relevant right now. The resolver gets the step's context - the session's
State, the tour and the application's services - and can be async. Here the selected row is handed
to the tour in BonboardingStartOptions.State, and the resolver reads it back.
await BonboardingService.StartAsync(new BonboardingTourDefinition
{
Id = "dynamic-target",
Steps =
{
new BonboardingStepDefinition
{
// Resolved when the step activates; returning None (or null) makes it a centred card.
Target = BonboardingStepTarget.Dynamic(context =>
BonboardingStepTarget.Anchor($"row-{context.State["selectedRow"]}")),
Title = "The row you picked",
},
},
}, new BonboardingStartOptions { State = new Dictionary<string, object?> { ["selectedRow"] = selectedRow } });
// A resolver can be async, and reach anything in DI:
Target = BonboardingStepTarget.Dynamic(async context =>
{
var id = await context.Services.GetRequiredService<IProjects>().GetFirstIdAsync(context.CancellationToken);
return id is null ? BonboardingStepTarget.None : BonboardingStepTarget.Css($"[data-project='{id}']");
})