Skip to content

Conditional & branching

Not every user should see every step. Steps can be gated by a condition, tours can fork on an answer, and Back walks the path the user actually took rather than the order the steps were declared in.

Conditional steps

When gates a step: one whose condition is false is skipped in whichever direction the user is going, and left out of the progress count. Here the billing step appears only for an administrator, and the beta step only with the feature flag on - change either and run the tour again, and watch the "1 of n" change.

Conditions are asked again on every transition, so a step can be unlocked by something an earlier step did (the lifecycle page shows one). Keep them cheap and free of side effects: load anything they need in a hook and keep it in the session's State. Prefer When when the reason is about the user or the data, and MissingTarget = Skip when it is about the page.

Live
Active projects
12
Usage this month
68%
Team members
7
new BonboardingStepDefinition
{
    Id = "billing",
    Target = BonboardingStepTarget.Anchor("billing"),
    Title = "Billing",
    // Asked on every transition; a false step is skipped and left out of the count.
    When = _ => ValueTask.FromResult(user.IsInRole("Administrator")),
}

@* In markup, a step inside an @if that starts out false would join the end of the tour.
   Order pins it: placed as the n-th step, ahead of the step that rendered n-th. *@
@if (showBeta)
{
    <BonboardingStep Id="beta" Order="2" Anchor="beta-panel" Title="Beta insights" />
}

Whether the tour runs at all

When decides about one step; CanStart decides about the whole tour. This one is for members only: with Signed in as above set to Viewer, StartAsync refuses it with a BonboardingException (you asked for it explicitly) and StartOnceAsync quietly returns null. BonboardingStartOptions.Force bypasses the check - and, given to StartOnceAsync, a completion, a dismissal or a snooze as well.

Live
new BonboardingTourDefinition
{
    Id = "members",
    CanStart = ctx => ValueTask.FromResult(user.IsInRole("Member")),
}

await BonboardingService.StartAsync(tour);       // throws BonboardingException when CanStart says no
await BonboardingService.StartOnceAsync(tour);   // returns null instead
await BonboardingService.StartAsync(tour, new BonboardingStartOptions { Force = true });   // not asked at all

Branching on an answer

Footer buttons carry an effect. Combined with BonboardingStepActionEffect.GoTo this is the whole of a branching tour: no state machine to write. ResolveNext decides in code where Next goes - here the designers' step jumps over the developers' one to where the paths rejoin; return null to fall through to the next step, or BonboardingTourDefinition.EndStepId to finish the tour.

Back follows the path the user actually took, not the order the steps were declared in: on the last step it returns to whichever branch you chose. The visit history is session.History, shown here as you go, and ResolvePrevious overrides where Back goes when you need something else.

Live

The branches point at the product on the conditional tour's sample above.

new BonboardingStepDefinition
{
    Id = "ask",
    Title = "What brings you here?",
    ShowNext = false,
    Actions =
    [
        new BonboardingStepAction
        {
            Label = "I design",
            Style = BonboardingStepActionStyle.Primary,
            Effect = BonboardingStepActionEffect.GoTo,   // also Next, Previous, Complete, Skip, Dismiss, Snooze
            TargetStepId = "design",
        },
        new BonboardingStepAction
        {
            Label = "I build",
            Style = BonboardingStepActionStyle.Primary,
            Effect = BonboardingStepActionEffect.GoTo,
            TargetStepId = "build",
        },
    ],
},
new BonboardingStepDefinition
{
    Id = "design",
    // Jump over the developers' branch to where the paths rejoin.
    // null falls through to the next step; BonboardingTourDefinition.EndStepId ends the tour.
    ResolveNext = _ => ValueTask.FromResult<string?>("shared"),
},
new BonboardingStepDefinition { Id = "build", ... },
new BonboardingStepDefinition
{
    Id = "shared",
    // Back follows session.History; override it only when you need something else:
    // ResolvePrevious = _ => ValueTask.FromResult<string?>("ask"),
}

Validation before advancing

CanAdvance holds the user on the step until it is satisfied, and ValidationMessage says why - shown in the card with role="alert", so screen readers read it out the moment the user is told no. The guard can set a more precise message of its own through its context. Here the tour will not move on until the field has a name of at least three characters; the step is TargetOnly, so the field can be typed into while the card is up.

Live

The guard decides for every way forward - the Next button, the arrow keys, an AdvanceOn trigger and an application signal - and every held advance publishes an AdvanceBlocked event, which, counted per step, points at instructions people find hard to follow. To stop the tour with an error instead, throw from OnBeforeLeave.

new BonboardingStepDefinition
{
    Target = BonboardingStepTarget.Anchor("project-name"),
    Title = "Name your project",
    Interaction = BonboardingInteractionMode.TargetOnly,   // the field stays usable
    ValidationMessage = "Give the project a name first.",
    CanAdvance = ctx =>
    {
        // The guard's own message wins over the step's.
        if (name.Length is > 0 and < 3) ctx.ValidationMessage = "Use at least three characters.";
        return ValueTask.FromResult(name.Trim().Length >= 3);
    },
}

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.

An unhandled error has occurred. Reload 🗙