UI Toolbox logoUI Toolbox

Sidebar

A Sidebar is a vertical navigation for a settings screen, an inventory or the sections of an application. One item is the selected one, and your code shows the page for it.

A sidebar with two groups, a selected item, a text badge and a count badge, next to a page

How to add a sidebar:

<ui:VisualElement style="flex-direction: row; flex-grow: 1;">
    <tb:Sidebar name="nav" toggle-shortcut="Ctrl+B">
        <ui:VisualElement class="tb-sidebar__header">
            <ui:Button name="navToggle" text="≡" />
        </ui:VisualElement>
        <tb:SidebarGroup text="Game">
            <tb:SidebarItem name="general" text="General" selected="true" />
            <tb:SidebarItem name="controls" text="Controls" badge="2" />
            <tb:SidebarItem text="Video">
                <tb:SidebarItem name="display" text="Display" />
                <tb:SidebarItem name="quality" text="Quality" />
            </tb:SidebarItem>
        </tb:SidebarGroup>
        <ui:VisualElement class="tb-sidebar__footer">
            <ui:Label text="v1.0" />
        </ui:VisualElement>
    </tb:Sidebar>
    <ui:VisualElement name="pages" style="flex-grow: 1;" />
</ui:VisualElement>
var nav = root.Q<Sidebar>("nav");
nav.ItemSelected += item => ShowPage(item?.name);
root.Q<Button>("navToggle").clicked += nav.Toggle;

// Items from code
var account = new SidebarGroup("Account");
account.Add(new SidebarItem("Profile", profileIcon));
var friends = new SidebarItem("Friends");
friends.Badge.Count = 3;
friends.Badge.Tone = BadgeTone.Accent;
account.Add(friends);
nav.Add(account);
nav.Selected = friends;

Sidebar:

Member Description
Selected The selected SidebarItem, or null. Setting it raises ItemSelected. A sub-item expands its parent.
Collapsed (collapsed) Shows the icons only. The text of an item is its tooltip.
Toggle() Changes Collapsed to its other value.
Flyout The ContextMenuComponent that shows the sub-items of a parent item in the icon-only mode. Null until it first shows.
ToggleShortcut (toggle-shortcut) A shortcut that calls Toggle, such as Ctrl+B. Empty (the default) is no shortcut.
Side (side) Left (the default) or Right. It sets the side of the border and of the mark of the selected item.
Items All items in order. A sub-item comes after its parent.
ItemSelected Action<SidebarItem?>, raised when Selected changes.
CollapsedChanged Action<bool>, raised when Collapsed changes.

SidebarGroup:

Member Description
Text (text) The label above the items. Empty shows no label.

SidebarItem:

Member Description
Text (text) The text.
Icon (icon) The image left of the text (a Background).
BadgeText (badge) The text of the badge, such as "New" or "3". Empty (the default) hides the badge.
Badge The Badge right of the text. Set its Tone or its Count in code.
Selected (selected) True for the selected item.
Expanded (expanded) Shows the sub-items.
HasSubItems, ParentItem True for an item with sub-items; the parent of a sub-item, or null.

Tips:

Styling

Class Element
tb-sidebar The sidebar: 200 px wide, on --tb-color-surface-alt, with a border on the right.
tb-sidebar--collapsed The icon-only mode: 44 px wide.
tb-sidebar--right Side is Right: the border and the mark of the selected item change sides.
tb-sidebar__header, tb-sidebar__footer Your header and footer elements.
tb-sidebar__group, tb-sidebar__group-label A group and its label.
tb-sidebar__item An item: its row and its sub-items.
tb-sidebar__item--selected The selected item. Its row has the hover color, an accent mark on the side and bold text.
tb-sidebar__item--expanded An item that shows its sub-items.
tb-sidebar__item--parent An item with sub-items. Its row shows the arrow.
tb-sidebar__row The row of an item. It takes the focus, and its border is the focus ring.
tb-sidebar__icon The icon, 16 px.
tb-sidebar__icon--letter The icon of an item without an image. It shows a letter in the icon-only mode.
tb-sidebar__content The text, the badge and the arrow of a row. The icon-only mode hides it.
tb-sidebar__text, tb-sidebar__arrow The text and the arrow of a row.
tb-sidebar__sub-items The sub-items of an item, with a left margin.

To make the sidebar wider and fill the selected row with the accent color:

.tb-sidebar { width: 260px; }
.tb-sidebar__item--selected > .tb-sidebar__row { background-color: var(--tb-color-accent); }
.tb-sidebar__item--selected > .tb-sidebar__row .tb-sidebar__text { color: var(--tb-color-accent-text); }