Sidebar recipe
Source: 09_Sidebar/ · how to inject custom buttons into the workspace's default sidebar.
What it teaches
The actual wiring lives in src/App.cs — a single subscription to App.Sidebar.OnSidebarRebuild_BeforeFooter that runs every time the sidebar is rebuilt. For each recipe in Recipes.cs the code:
- Creates a
SidebarButtonwith an id, icon and label. - Wires
OnClicktoRouter.Navigate(...). - Uses
tracker.Add(...)so the button'sIsSelectedstate stays in sync with the URL hash.
The four OnSidebarRebuild_* hooks (BeforeHeader, AfterHeader, BeforeFooter, AfterFooter) give you four anchor points inside the sidebar. BeforeFooter is the right one for "add a navigation item to the existing list".
Code shape
App.Sidebar.OnSidebarRebuild_BeforeFooter += (sidebar, mode, tracker) =>
{
if (mode != App.Sidebar.Mode.Default) return;
foreach (var recipe in RecipeCatalog.All)
{
var captured = recipe;
var btn = new SidebarButton(captured.Id, captured.Icon, captured.Title)
.OnClick(() => Router.Navigate(captured.Route));
tracker.Add(() => btn.IsSelected = window.location.hash.Contains(captured.Route));
sidebar.AddContent(btn);
}
};
Reacting to mode
The mode argument tells you which sidebar variant is being rebuilt:
| Mode | Sidebar context |
|---|---|
Default |
Standard navigation sidebar. |
UserPreferences |
The sidebar of the user-preferences page — covered in the User Preferences recipe. |
AdminSettings* |
The sidebar of the admin section (and its per-category siblings). |
* See App.Sidebar.IsAdminMode(mode) for the full list.
See also
- User Preferences recipe — adds a button to the
UserPreferencessidebar mode and a custom page to back it. Mosaik.FrontEnd.LicenseServer/src/LicenseApp.cs— production app that uses the same pattern, plus role-gating (if (CurrentUser.IsAdmin)).Mosaik.FrontEnd/src/App.Sidebar.cs— the full sidebar implementation if you need to look behind the curtain.