Getting started
Four pieces of wiring and a first tour: install the package, register the services, reference the
stylesheet and host one BonboardingRoot - then mark an element and point a step at it.
Install the package
Bit.Bonboarding targets .NET 10 and .NET 11, and depends on nothing but Blazor itself.
dotnet add package Bit.Bonboarding
Register the services
Everything is registered as scoped - per circuit on Blazor Server, per application on WebAssembly - which matches the lifetime of a user's onboarding state. Global defaults are set in the same call.
using Bit.Bonboarding; builder.Services.AddBitBonboardingServices(options => { options.Placement = BonboardingPlacement.Bottom; options.Progress = BonboardingProgressStyle.Bar; options.Labels.Skip = "Not now"; options.StorageKeyPrefix = "acme:onboarding:"; });
InteractiveWebAssembly component switches to the client
project's. So a registration missing in the server project fails the page while it prerenders, and one missing in
the client project only once the page turns interactive on WebAssembly - a Bonboarding component with an error
that names AddBitBonboardingServices, a component that injects IBonboardingService with
Blazor's own "no registered service" error. This site
does it through one extension method both Program.cs files call. Only a WebAssembly app that
lazy loads the library leaves it out.
Reference the stylesheet
In App.razor (Blazor Web App) or index.html (WebAssembly), before your own stylesheet
so your overrides of its --bit-bonboarding-* variables win. There is no script tag to add: the
library imports its own JavaScript module.
<link rel="stylesheet" href="@Assets["_content/Bit.Bonboarding/bit-bonboarding.css"]" />
Host one BonboardingRoot
One host renders whichever tour is running, from anywhere in the application. Put it somewhere that outlives
navigation - the main layout, or beside the router as this site does - because a host on a page would be
disposed by the navigation a multi-page tour performs. Then import the namespace in _Imports.razor.
@* MainLayout.razor *@ <div class="page"> @Body </div> <BonboardingRoot /> @* _Imports.razor *@ @using Bit.Bonboarding
Write a first tour
Mark the element a step is about with data-bonboarding-anchor, then start a tour that points at it.
This is the setup check from the guide, running on this page: if the card appears next to the button, with
the spotlight around it and the arrow keys working, the wiring is complete.
@inject IBonboardingService BonboardingService
<button data-bonboarding-anchor="probe" @onclick="RunAsync">Test</button>
@code {
private Task RunAsync() => BonboardingService.StartAsync(new BonboardingTourDefinition
{
Id = "probe",
Persist = false,
Steps =
{
new BonboardingStepDefinition
{
Target = BonboardingStepTarget.Anchor("probe"),
Title = "It works",
Description = "Positioning, spotlight and keyboard navigation are all live.",
},
},
});
}
BonboardingRoot is missing from the layout - or the component that starts the tour is not interactive.
The same tour can be declared in markup with <BonboardingTour> and
<BonboardingStep> - the landing page's welcome tour is written that way.
Pointing at an element
A step's Target says which element it is about. Each button below is found a different way; the
tour visits all three and ends on a step with no target at all, which is drawn as a centred card - the right
shape for a welcome or a summary.
| Target | Finds |
|---|---|
Anchor("name") |
The element carrying data-bonboarding-anchor="name". Preferred: it is
looked up again on every measurement, so it survives re-renders and elements that appear later.
BitBonboarding.Anchor("name") splats the attribute for you.
|
Css("#id .cls") |
The first rendered element matching a CSS selector. A plain string converts to one implicitly. |
Element(reference) |
A captured ElementReference. It names one DOM node, so it goes stale if Blazor replaces
that node - use it for elements that are rendered once and stay.
|
Dynamic(resolver) |
Decided when the step activates. See dynamic targets. |
None |
No element: a centred card with no spotlight and no arrow. The default. |
<button @attributes="BitBonboarding.Anchor("targeting-by-anchor")">By anchor</button>
<button id="targeting-by-selector">By CSS selector</button>
<button @ref="_byElement">By element reference</button>
Steps =
{
new BonboardingStepDefinition { Target = BonboardingStepTarget.Anchor("targeting-by-anchor"), Title = "Found by its anchor" },
new BonboardingStepDefinition { Target = "#targeting-by-selector", Title = "Found by a CSS selector" },
new BonboardingStepDefinition { Target = _byElement, Title = "Found by an element reference" },
new BonboardingStepDefinition { Title = "And no element at all" }, // a centred card
}
@* In markup, each kind has its own parameter: *@
<BonboardingStep Anchor="targeting-by-anchor" ... />
<BonboardingStep Selector="#targeting-by-selector" ... />
<BonboardingStep Element="_byElement" ... />Where to start a tour
The library is prerender-safe, but OnInitializedAsync runs during prerendering and again after
hydration - in two different scopes. Start tours from OnAfterRenderAsync(firstRender: true), from a
user action, or with AutoStart, which already does the right thing.
<BonboardingTour Id="welcome" AutoStart="BonboardingAutoStart.Once"> ... </BonboardingTour> await BonboardingService.StartAsync(tour); // always start, resuming saved progress await BonboardingService.StartOnceAsync(tour); // only if not already completed, dismissed or snoozed await BonboardingService.RestartAsync(tour); // forget progress and start from the top
AutoStart is None (the default), Once - per user, remembered by the store -
or Always, every time the page renders.
style attributes for positioning - a policy that allows Blazor allows
both, unless its style-src forbids unsafe-inline.
Let people replay it
A tour is known by its id, so a help menu anywhere in the application can start it again. A tour declared in markup is registered only while its page is on screen; register the ones a help menu should always offer at startup. A ready-made list of tours with Start, Resume and Replay buttons is on the guidance page.
// MainLayout.razor: registered once per user, so every page can start it by id.
protected override void OnInitialized() => BonboardingService.Register(WelcomeTour.Create());
// A help menu, anywhere in the application:
<button @onclick='() => BonboardingService.StartAsync("welcome")'>Replay the welcome tour</button>
<button @onclick='() => BonboardingService.RestartAsync(WelcomeTour.Create())'>Start it from the top</button>Lazy loading on WebAssembly
A WebAssembly app can fetch Bonboarding only when someone opens a part of the app that has tours. Such an app cannot
call AddBitBonboardingServices - Program.cs runs before the assembly is there - so it
turns on the Bit.Bonboarding.LazyLoad runtime switch instead. With the switch on and the services not
registered, BonboardingRoot, BonboardingTour, BonboardingBeacon,
BonboardingChecklist and BonboardingTourList create every service they need themselves,
once per browser connection, and IServiceProvider.GetBonboardingService() hands the app's own code the
same IBonboardingService. A tour's hooks still reach the app's own services through their context.
In the project that runs in the browser, mark the assembly as lazy loaded and turn the switch on, then load the
assembly before the first page that uses it renders:
<!-- The browser project's .csproj --> <ItemGroup> <BlazorWebAssemblyLazyLoad Include="Bit.Bonboarding.wasm" /> <RuntimeHostConfigurationOption Include="Bit.Bonboarding.LazyLoad" Value="true" /> </ItemGroup>
@* App.razor: the router waits for OnNavigateAsync before it renders the page. *@
@using Microsoft.AspNetCore.Components.WebAssembly.Services
@inject LazyAssemblyLoader LazyAssemblyLoader
<Router AppAssembly="typeof(App).Assembly" OnNavigateAsync="OnNavigateAsync">
...
</Router>
@code {
private async Task OnNavigateAsync(NavigationContext context)
{
// The pages with tours: fetch Bonboarding before they render. A second call loads nothing again. Routes
// match regardless of case, so /Admin/users is one of them too, and /administrators is not.
var path = context.Path.Split('?', '#')[0]; // the path keeps the query string and the fragment
if (path.Equals("admin", StringComparison.OrdinalIgnoreCase) || path.StartsWith("admin/", StringComparison.OrdinalIgnoreCase))
await LazyAssemblyLoader.LoadAssembliesAsync(["Bit.Bonboarding.wasm"]);
}
}
Those pages, or a layout only they use, host the tours and reach the service through
GetBonboardingService() instead of injecting IBonboardingService:
@* AdminLayout.razor *@
@inherits LayoutComponentBase
@inject IServiceProvider Services
<nav>
<a href="admin/users" data-bonboarding-anchor="users">Users</a>
<button @onclick="ReplayAsync">Replay the tour</button>
</nav>
@Body
<BonboardingTour Id="admin-welcome" AutoStart="BonboardingAutoStart.Once">
<BonboardingStep Anchor="users" Title="Users" Description="Invite people and manage their roles here." />
</BonboardingTour>
<BonboardingRoot />
@code {
private Task ReplayAsync() => Services.GetBonboardingService().StartAsync("admin-welcome");
}
- Leave
AddBitBonboardingServicesout of that project'sProgram.cs: calling it references Bonboarding's types before the assembly is there. For the same reason only what renders after the assembly has loaded may use Bonboarding's types - the pages it is loaded for and a layout only they use, notApp.razor, a layout every page shares orProgram.cs. That includes<BonboardingRoot />: it goes in that layout, not in the main one. - The switch is what tells a lazy loading app apart from one that forgot the registration: without it, a missing registration fails with an error that names
AddBitBonboardingServices, as it does everywhere else.AppContext.SetSwitch("Bit.Bonboarding.LazyLoad", true);at the top ofProgram.csturns it on too, without referencing Bonboarding's types. - No type in an assembly the router reads when the app starts - the app's own assembly first of all - may derive from or implement one of Bonboarding's: no
IBonboardingObserver,IBonboardingStoreorIBonboardingLocalizerclass. The router reads the types of those assemblies at startup, and on WebAssembly a type whose base type or interface lives in an assembly that is not loaded yet fails to load - and keeps failing after Bit.Bonboarding has loaded, breaking the app's use of it for the rest of the session. Put such a class in a library that is lazy loaded together with Bit.Bonboarding. - The stylesheet is a plain
<link>, served whether the assembly has loaded or not, and the components import their JavaScript module the first time they need it. - Without the registration the components run on the default
BonboardingOptions. A tour and a step override most of them - everyBonboardingStepOptionsmember,LabelsandPersist- on their definition or as parameters ofBonboardingTourandBonboardingStep. The rest -StorageKeyPrefix,StorageLocation,StorageScope,AutoNavigateToStepRouteandEnableJsDiagnostics- keep their defaults; an app that needs them registers the services rather than lazy loading. - The store, the localizer and the observers cannot be replaced either: progress goes to the browser's
localStorageunder the defaultbit-bonboarding:prefix, which every app on the same origin shares, for the device as a whole rather than per user. A tour that must not write to the browser setsPersist = false; analytics subscribe toGetBonboardingService().EventRaisedinstead of registering an observer. - Do not lazy load in an AOT build. AOT compiles every assembly into
dotnet.native.wasm, so there is nothing left to download later, and lazy loading an AOT-compiled assembly can fail at startup.
Where to go next
- Placement & spotlight - where the popover goes, and what the overlay does with a click.
- Dynamic targets - elements that render late, re-render or never appear, and steps the user completes.
- Conditional & branching - different tours for different users.
- Lifecycle & orchestration - pausing, queueing, snoozing and checking a tour.
- Beacons, checklists & tour lists - guidance that stays on the page.
- Multi-page tour - one tour across several routes.
- Theming & RTL - make the card look like your product.
- Headless UI - draw the card yourself.
- Events & persistence - know how it went, and remember where each user got to.