Skip to content
Layout Sidebar

Sidebar

An app sidebar that collapses to icons, can be resized, and becomes an off-canvas panel on small screens.

On this page

Examples

Collapsible, remembered

The collapsed state lives in the browser and is stored in a cookie named after the Id. Below the lg breakpoint the same aside is an off-canvas panel (a popover made modal by the browser), so the content exists once.

Sidebar

Acme
Ada Lovelace
Collapse the sidebar with its button: the state is remembered in the browser, and survives a reload.
Razor
@* As="div": the docs page already sits inside a main landmark, and an aside must not. In a layout, keep the default.
   The group-data-[state=collapsed]/sidebar variant hides a label visually while it stays available to screen readers. *@
<div class="flex h-96 w-full flex-col overflow-hidden rounded-xl border border-(--klyroo-border) lg:flex-row">
    <KlySidebar Id="playground-sidebar" As="KlyElement.Div" data-testid="sidebar">
        <Header>
            <span class="font-semibold text-(--klyroo-text-highlighted)">Acme</span>
        </Header>
        <ChildContent>
            <KlyNavigationMenu Items="_items" Orientation="KlyOrientation.Vertical" AriaLabel="Workspace" Ui="_navUi" />
        </ChildContent>
        <Footer>
            <span class="inline-flex size-8 shrink-0 items-center justify-center rounded-full bg-(--klyroo-bg-elevated) text-xs font-medium" aria-hidden="true">AL</span>
            <span class="min-w-0 truncate text-sm group-data-[state=collapsed]/sidebar:sr-only">Ada Lovelace</span>
        </Footer>
    </KlySidebar>
    <div class="min-w-0 flex-1 p-6 text-sm text-(--klyroo-text-muted)">
        Collapse the sidebar with its button: the state is remembered in the browser, and survives a reload.
    </div>
</div>

@code {
    private static readonly NavigationMenuUi _navUi = new() { Label = "group-data-[state=collapsed]/sidebar:sr-only" };

    private static readonly KlyNavigationItem[] _items =
    [
        new("Home") { Href = "/", Exact = true, Icon = KlyIcons.Named("home", "size-5") },
        new("Inbox") { Href = "/components/sidebar", Icon = KlyIcons.Named("inbox", "size-5") },
        new("Calendar") { Href = "/components/header", Icon = KlyIcons.Named("calendar", "size-5") },
        new("Settings") { Href = "/docs/theming", Icon = KlyIcons.Named("user", "size-5") },
    ];
}

Bound state

@bind-Collapsed reports the state to .NET; your state wins over the stored one.

Sidebar

Acme

Collapsed: false

Razor
<div class="flex h-72 w-full flex-col overflow-hidden rounded-xl border border-(--klyroo-border) lg:flex-row">
    <KlySidebar @bind-Collapsed="_collapsed" As="KlyElement.Div" data-testid="sidebar-bound">
        <Header>
            <span class="font-semibold text-(--klyroo-text-highlighted)">Acme</span>
        </Header>
        <ChildContent>
            <KlyNavigationMenu Items="_items" Orientation="KlyOrientation.Vertical" AriaLabel="Workspace, bound" Ui="_navUi" />
        </ChildContent>
    </KlySidebar>
    <div class="flex min-w-0 flex-1 flex-col items-start gap-3 p-6 text-sm text-(--klyroo-text-muted)">
        <p data-testid="sidebar-state">Collapsed: @(_collapsed ? "true" : "false")</p>
        <KlyButton Label="Toggle from .NET" Size="KlySize.Sm" Variant="ButtonVariant.Outline" Color="KlyColor.Neutral" OnClick="() => _collapsed = !_collapsed" data-testid="sidebar-bound-toggle" />
    </div>
</div>

@code {
    private bool _collapsed;

    private static readonly NavigationMenuUi _navUi = new() { Label = "group-data-[state=collapsed]/sidebar:sr-only" };

    private static readonly KlyNavigationItem[] _items =
    [
        new("Home") { Href = "/", Exact = true, Icon = KlyIcons.Named("home", "size-5") },
        new("Inbox") { Href = "/components/sidebar", Icon = KlyIcons.Named("inbox", "size-5") },
    ];
}

Resizable

Drag the edge of the sidebar, or focus it and use the arrow keys, Home and End.
Razor
<div class="flex h-72 w-full flex-col overflow-hidden rounded-xl border border-(--klyroo-border) lg:flex-row">
    <KlySidebar Id="playground-sidebar-resizable" Resizable Collapsible="false" Width="224" As="KlyElement.Div" data-testid="sidebar-resizable">
        <Header>
            <span class="font-semibold text-(--klyroo-text-highlighted)">Acme</span>
        </Header>
        <ChildContent>
            <KlyNavigationMenu Items="_items" Orientation="KlyOrientation.Vertical" AriaLabel="Workspace, resizable" />
        </ChildContent>
    </KlySidebar>
    <div class="min-w-0 flex-1 p-6 text-sm text-(--klyroo-text-muted)">
        Drag the edge of the sidebar, or focus it and use the arrow keys, Home and End.
    </div>
</div>

@code {
    private static readonly KlyNavigationItem[] _items =
    [
        new("Home") { Href = "/", Exact = true },
        new("Inbox") { Href = "/components/sidebar" },
        new("Settings") { Href = "/docs/theming" },
    ];
}

Components inside

A search, a radio group with a Name and an input: one copy each, in the column and in the panel.

Sidebar

Order

Inside the sidebar

Declared in the sidebar: it opens in place and the panel stays open.

1–10 of 120

Invoices
No.CustomerStatus
1Acme CorpDraft
2GlobexOverdue
3InitechPending
4UmbrellaPaid
5HooliDraft
6Stark IndustriesOverdue
7Wayne EnterprisesPending
8WonkaPaid
9CyberdyneDraft
10SoylentOverdue

Outside the sidebar

Declared outside the sidebar: the panel closes when it opens.

Razor
@inject NavigationManager Navigation

@* Real components in a sidebar: the content is rendered once, whether the sidebar is the column or the off-canvas panel, so the search keeps its focus (it is
   focused by id), the radio group keeps its explicit Name and nothing is posted twice. Below the lg breakpoint the Open sidebar button opens the aside as a modal panel.
   The modal declared in the sidebar stays in it and works as in a page; the one declared outside is opened from the panel, which closes first. A form submitted in the panel,
   or a navigation to another page, closes it too. *@
<div class="w-full" data-testid="sidebar-content-demo">
    <KlyDataList TItem="Invoice" Label="Invoices next to a sidebar" Schema="InvoiceData.Schema" Items="InvoiceData.All" UrlKey="sbc">
        <div class="flex h-[30rem] w-full flex-col overflow-hidden rounded-xl border border-(--klyroo-border) lg:flex-row">
            <KlySidebar Id="playground-sidebar-content" As="KlyElement.Div" Collapsible="false" Width="288" data-testid="sidebar-content">
                <ChildContent>
                    <KlyDataListSearch Placeholder="Search invoices" AriaLabel="Search invoices" data-testid="sidebar-content-search" />
                    <KlyRadioGroup TValue="string" Name="sidebar-content-sort" Legend="Order" Value="@("recent")" Items="Orders" />
                    <KlyInput Name="sidebar-content-note" Placeholder="A note" aria-label="A note" data-testid="sidebar-content-note" />
                    <form method="get" action="@Navigation.Uri.Split('?')[0]" data-enhance class="flex gap-2" data-testid="sidebar-content-form">
                        <input type="text" name="sbq" aria-label="Go to" class="min-w-0 flex-1 rounded border border-(--klyroo-border) bg-(--klyroo-bg) px-2 py-1 text-sm" data-testid="sidebar-content-form-input" />
                        <button type="submit" class="rounded border border-(--klyroo-border) px-2 py-1 text-sm" data-testid="sidebar-content-form-submit">Go</button>
                    </form>
                    <KlyModal Title="Inside the sidebar" Description="Declared in the sidebar: it opens in place and the panel stays open." data-testid="sidebar-inner-modal">
                        <ChildContent>
                            <KlyButton Label="Open inner modal" Variant="ButtonVariant.Outline" Color="KlyColor.Neutral" data-testid="sidebar-inner-open" />
                        </ChildContent>
                        <Body>
                            <KlyInput Placeholder="Inner field" aria-label="Inner field" data-testid="sidebar-inner-field" />
                        </Body>
                        <Footer>
                            <KlyButton Label="Done" data-kly-close data-testid="sidebar-inner-done" />
                        </Footer>
                    </KlyModal>
                    <KlyButton Label="Open outer modal" Variant="ButtonVariant.Outline" Color="KlyColor.Neutral" OnClick="@(() => _outer = true)" data-testid="sidebar-outer-open" />
                    <KlyButton Label="Go to the home page" Variant="ButtonVariant.Outline" Color="KlyColor.Neutral" OnClick="@(() => Navigation.NavigateTo("/"))" data-testid="sidebar-navigate" />
                </ChildContent>
            </KlySidebar>
            <div class="flex min-w-0 flex-1 flex-col gap-3 overflow-y-auto p-4">
                <KlyDataListResultCount />
                <KlyDataListTable Caption="Invoices">
                    <KlyDataListFieldColumn Field="id" Title="No." Align="DataListAlign.End" />
                    <KlyDataListFieldColumn Field="customer" />
                    <KlyDataListFieldColumn Field="status" />
                </KlyDataListTable>
            </div>
        </div>
    </KlyDataList>
    <KlyModal @bind-Open="_outer" Title="Outside the sidebar" Description="Declared outside the sidebar: the panel closes when it opens." data-testid="sidebar-outer-modal">
        <Body>
            <KlyInput Placeholder="Outer field" aria-label="Outer field" data-testid="sidebar-outer-field" />
        </Body>
        <Footer>
            <KlyButton Label="Close outer" data-kly-close data-testid="sidebar-outer-done" />
        </Footer>
    </KlyModal>
</div>

@code {
    private bool _outer;

    private static readonly KlyRadioItem<string>[] Orders =
    [
        new("recent", "Most recent"),
        new("amount", "Largest first"),
    ];
}

API

KlySidebar

Application sidebar that collapses to icons and resizes from the lg breakpoint up, and becomes an off-canvas panel below it.

Parameters

Name Type Default Description
AriaLabel string Accessible name of the sidebar landmark. Defaults to the built-in text.
As KlyElement KlyElement.Aside The element to render, Aside by default. A complementary landmark must not sit inside another landmark: use Div when the sidebar is placed inside a main element. Values: Div, Section, Article, Aside, Header, Footer, Main, Nav.
Class string Classes added to the root element, merged last so they win over the theme.
Collapsed (@bind-Collapsed) bool false The collapsed state when nothing is remembered. Use @bind-Collapsed to follow and set it from .NET; the browser still toggles it without a round trip, and in static rendering a bound state is not reported.
Collapsible bool true Shows the button that collapses the sidebar to icons.
Id string Name under which the collapsed state and the width are remembered, in the cookies klyroo-sidebar-{id} ("1" when collapsed) and klyroo-sidebar-{id}-width , written by the browser. Without an id nothing is remembered. An app served below a path base ( UsePathBase("/x") ) shares its origin with other apps, so its cookies are scoped to the base path and named klyroo-sidebar.{slug}~~{id} (see String) ): a server keeps the last of two cookies of the same name, and another app's would otherwise win. At the root nothing changes. With a bound Collapsed the collapsed state is not restored: your state wins. A small inline script placed right after the sidebar applies the remembered state while the page is parsed, before the first paint, so a reload neither shifts nor animates (give it ScriptNonce under a strict Content-Security-Policy). Reading the cookies on the server and passing Collapsed also works: get the cookie name from String) with Request.PathBase , so the same code is right at the root and below a path base.
MaxWidth int 448 Largest width the user can resize the sidebar to, in pixels. Defaults to 448.
MinWidth int 192 Smallest width the user can resize the sidebar to, in pixels. Defaults to 192.
MobileTitle string Title of the mobile panel. Defaults to the built-in text.
MobileTrigger bool true Shows the button that opens the off-canvas panel. Turn it off when another button opens the panel instead, such as KlyDashboardSidebarToggle , which finds the sidebar by itself.
Resizable bool false Adds a handle on the edge to resize the sidebar with the mouse or the arrow keys, Home and End.
ScriptNonce string Nonce for the small inline script that applies the remembered state before the first paint, when a Content-Security-Policy forbids inline scripts without one.
Ui SidebarUi Per-instance class overrides of the slots of the sidebar, see SidebarUi . They are merged after the theme and the Class parameter wins on the root slot.
Width int 256 Width in pixels when expanded and not resized yet.

Slots

Name Type Description
Footer RenderFragment Bottom of the sidebar: the signed-in user, settings.
Header RenderFragment Top of the sidebar: a logo and a title. Hidden while the sidebar is collapsed, next to the collapse button.
ChildContent RenderFragment The scrollable middle: navigation, search. It is rendered once and serves both the column and the off-canvas panel.

Ui slots

Ui takes a SidebarUi with one class string per slot, merged after the theme.

Slot Styles
Root Classes merged into the root slot of KlyDashboardSidebar and KlySidebar , after the theme and the app configuration.
Host Classes merged into the host slot of KlyDashboardSidebar and KlySidebar , after the theme and the app configuration.
Panel The wrapper of everything inside the aside but the resize handle; the panel takes its dialog role while it is open.
Header Classes merged into the header slot of KlyDashboardSidebar and KlySidebar , after the theme and the app configuration.
HeaderContent Classes merged into the headerContent slot of KlyDashboardSidebar and KlySidebar , after the theme and the app configuration.
Toggle Classes merged into the toggle slot of KlyDashboardSidebar and KlySidebar , after the theme and the app configuration.
ToggleIcon Classes merged into the toggleIcon slot of KlyDashboardSidebar and KlySidebar , after the theme and the app configuration.
Content Classes merged into the content slot of KlyDashboardSidebar and KlySidebar , after the theme and the app configuration.
Footer Classes merged into the footer slot of KlyDashboardSidebar and KlySidebar , after the theme and the app configuration.
Handle Classes merged into the handle slot of KlyDashboardSidebar and KlySidebar , after the theme and the app configuration.
Mobile The wrapper of the button that opens the panel (below lg).
MobileHeader The title row of the panel, below lg.
MobileTitle Classes merged into the mobileTitle slot of KlyDashboardSidebar and KlySidebar , after the theme and the app configuration.
MobileClose Classes merged into the mobileClose slot of KlyDashboardSidebar and KlySidebar , after the theme and the app configuration.