Skip to content
GitHub11 stars

A composable, themeable and customizable sidebar component.

<Blazix.Primitives.MediaQuery Query="(max-width: 767px)" MatchesChanged="@(matches => _isMobile = matches)" />
<SidebarProvider>
    <Sidebar Collapsible="SidebarCollapsible.Icon">
A sidebar that collapses to icons.

Sidebars are one of the most complex components to build. They are central to any application and often contain a lot of moving parts.

We now have a solid foundation to build on top of. Composable. Themeable. Customizable.

Installation

dnx Blazix.Cli add sidebar

Usage

Components/Layout/MainLayout.razor
@inherits LayoutComponentBase

<SidebarProvider>
    <AppSidebar />
    <main>
        <SidebarTrigger />
        @Body
    </main>
</SidebarProvider>
Components/Layout/AppSidebar.razor
<Sidebar>
    <SidebarHeader />
    <SidebarContent>
        <SidebarGroup />
        <SidebarGroup />
    </SidebarContent>
    <SidebarFooter />
</Sidebar>

Composition

Use the following composition to build a Sidebar layout:

SidebarProvider
├── Sidebar
│   ├── SidebarHeader
│   ├── SidebarContent
│   │   ├── SidebarGroup
│   │   │   ├── SidebarGroupLabel
│   │   │   ├── SidebarGroupAction
│   │   │   ├── SidebarGroupContent
│   │   │   └── SidebarMenu
│   │   │       ├── SidebarMenuItem
│   │   │       │   ├── SidebarMenuButton
│   │   │       │   ├── SidebarMenuAction
│   │   │       │   └── SidebarMenuBadge
│   │   │       └── SidebarMenuItem
│   │   │           ├── SidebarMenuButton
│   │   │           └── SidebarMenuSub
│   │   │               ├── SidebarMenuSubItem
│   │   │               └── SidebarMenuSubItem
│   │   └── SidebarGroup
│   │       └── SidebarMenu
│   │           ├── SidebarMenuItem
│   │           └── SidebarMenuItem
│   ├── SidebarFooter
│   └── SidebarRail
├── SidebarInset
└── SidebarTrigger

Structure

  • SidebarProvider — Handles collapsible state and provides sidebar context to child components.
  • Sidebar — The main collapsible sidebar panel.
  • SidebarHeader — Sticky at the top; use for branding, titles, or workspace switchers.
  • SidebarFooter — Sticky at the bottom; use for user menus, settings, or actions.
  • SidebarContent — Scrollable region between the header and footer.
  • SidebarGroup — Groups related navigation with optional label, action, and content areas.
  • SidebarMenu / SidebarMenuItem — Menu structure for links, badges, actions, and nested submenus.
  • SidebarRail — A strip along the sidebar's edge that toggles it.
  • SidebarInset — Wraps main content when using the Inset variant.
  • SidebarTrigger — Control that toggles the sidebar open or collapsed.

SidebarProvider

The SidebarProvider component is used to provide the sidebar context to the Sidebar component. You should always wrap your application in a SidebarProvider component.

Parameters

NameTypeDescription
DefaultOpenboolDefault open state of the sidebar.
Openbool?Open state of the sidebar (controlled).
OpenChangedEventCallback<bool>Sets open state of the sidebar (controlled).

Width

If you have a single sidebar in your application, you can use the Width and WidthMobile constants in SidebarProvider.razor to set the width of the sidebar.

Components/UI/Sidebar/SidebarProvider.razor
private const string Width = "16rem";
public const string WidthMobile = "18rem";

For multiple sidebars in your application, you can set the --sidebar-width CSS variable with the Style parameter.

<SidebarProvider Style="--sidebar-width: 20rem">
    <Sidebar />
</SidebarProvider>

Keyboard Shortcut

To trigger the sidebar, you use the cmd+b keyboard shortcut on Mac and ctrl+b on Windows.

Components/UI/Sidebar/SidebarProvider.razor
private const string KeyboardShortcut = "b";

Persisted State

The provider keeps the sidebar's state in the sidebar_state cookie. Read it when the page renders and pass it as DefaultOpen to start the way the user left it.

@inject IHttpContextAccessor HttpContextAccessor

<SidebarProvider DefaultOpen="@DefaultOpen">
    <AppSidebar />
    <main>@Body</main>
</SidebarProvider>

@code {
    private bool DefaultOpen =>
        HttpContextAccessor.HttpContext?.Request.Cookies[SidebarProvider.CookieName] != "false";
}

The main Sidebar component used to render a collapsible sidebar.

ParameterTypeDescription
SideSidebarSide.Left or RightThe side of the sidebar.
VariantSidebarVariant.Sidebar, Floating, or InsetThe variant of the sidebar.
CollapsibleSidebarCollapsible.Offcanvas, Icon, or NoneCollapsible state of the sidebar.
CollapsibleDescription
OffcanvasA collapsible sidebar that slides in from the left or right.
IconA sidebar that collapses to icons.
NoneA non-collapsible sidebar.
<SidebarProvider>
    <Sidebar Variant="SidebarVariant.Inset" />
    <SidebarInset>
        <main>@Body</main>
    </SidebarInset>
</SidebarProvider>

SidebarContext

The SidebarProvider cascades a SidebarContext. Take it as a cascading parameter to read or control the sidebar from any component inside the provider.

<p>The sidebar is @Sidebar.State.ToString().ToLowerInvariant().</p>

@code {
    [CascadingParameter] private SidebarContext Sidebar { get; set; } = null!;
}
MemberTypeDescription
StateSidebarState.Expanded or SidebarState.CollapsedThe current state of the sidebar.
OpenboolWhether the sidebar is open.
SetOpenAsyncFunc<bool, Task>Sets the open state of the sidebar.
OpenMobileboolWhether the sidebar is open on mobile.
SetOpenMobileAsyncFunc<bool, Task>Sets the open state of the sidebar on mobile.
IsMobileboolWhether the sidebar is on mobile.
ToggleAsyncFunc<Task>Toggles the sidebar. Desktop and mobile.

SidebarHeader

Use the SidebarHeader component to add a sticky header to the sidebar.

Components/Layout/AppSidebar.razor
<Sidebar>
    <SidebarHeader>
        <SidebarMenu>
            <SidebarMenuItem>
                <DropdownMenu>
                    <DropdownMenuTrigger>
                        <Render Context="trigger">
                            <SidebarMenuButton @attributes="trigger.Attributes">
                                Select Workspace
                                <Icon Data="LucideIcons.ChevronDown" class="ml-auto" />
                            </SidebarMenuButton>
                        </Render>
                    </DropdownMenuTrigger>
                    <DropdownMenuContent>
                        <DropdownMenuItem>
                            <span>Blazix Inc</span>
                        </DropdownMenuItem>
                    </DropdownMenuContent>
                </DropdownMenu>
            </SidebarMenuItem>
        </SidebarMenu>
    </SidebarHeader>
</Sidebar>

SidebarFooter

Use the SidebarFooter component to add a sticky footer to the sidebar.

<Sidebar>
    <SidebarFooter>
        <SidebarMenu>
            <SidebarMenuItem>
                <SidebarMenuButton>
                    <Icon Data="LucideIcons.UserRound" /> Username
                </SidebarMenuButton>
            </SidebarMenuItem>
        </SidebarMenu>
    </SidebarFooter>
</Sidebar>

SidebarContent

The SidebarContent component is used to wrap the content of the sidebar. This is where you add your SidebarGroup components. It is scrollable.

<Sidebar>
    <SidebarContent>
        <SidebarGroup />
        <SidebarGroup />
    </SidebarContent>
</Sidebar>

SidebarGroup

Use the SidebarGroup component to create a section within the sidebar.

A SidebarGroup has a SidebarGroupLabel, a SidebarGroupContent and an optional SidebarGroupAction.

<SidebarGroup>
    <SidebarGroupLabel>Application</SidebarGroupLabel>
    <SidebarGroupAction>
        <Icon Data="LucideIcons.Plus" /> <span class="sr-only">Add Project</span>
    </SidebarGroupAction>
    <SidebarGroupContent></SidebarGroupContent>
</SidebarGroup>

To make a SidebarGroup collapsible, wrap it in a Collapsible.

<Collapsible Open="true" Class="group/collapsible">
    <SidebarGroup>
        <SidebarGroupLabel>
            <Render Context="label">
                <CollapsibleTrigger @attributes="label.Attributes">
                    Help
                    <Icon Data="LucideIcons.ChevronDown" class="ml-auto transition-transform group-data-open/collapsible:rotate-180" />
                </CollapsibleTrigger>
            </Render>
        </SidebarGroupLabel>
        <CollapsibleContent>
            <SidebarGroupContent />
        </CollapsibleContent>
    </SidebarGroup>
</Collapsible>

SidebarMenu

The SidebarMenu component is used for building a menu within a SidebarGroup.

<SidebarMenu>
    @foreach (var project in Projects)
    {
        <SidebarMenuItem @key="project.Name">
            <SidebarMenuButton>
                <Render Context="link">
                    <a @attributes="link.Attributes" href="@project.Url">
                        <Icon Data="@project.Icon" />
                        <span>@project.Name</span>
                    </a>
                </Render>
            </SidebarMenuButton>
        </SidebarMenuItem>
    }
</SidebarMenu>

SidebarMenuButton

The SidebarMenuButton component is used to render a menu button within a SidebarMenuItem.

By default, the SidebarMenuButton renders a button but you can use the Render parameter to render a different element, such as an <a> tag or a NavLink.

Use the IsActive parameter to mark a menu item as active.

<SidebarMenuButton IsActive="true">
    <Render Context="link">
        <a @attributes="link.Attributes" href="#">Home</a>
    </Render>
</SidebarMenuButton>

SidebarMenuAction

The SidebarMenuAction component is used to render a menu action within a SidebarMenuItem.

<SidebarMenuItem>
    <SidebarMenuButton>
        <Render Context="link">
            <a @attributes="link.Attributes" href="#">
                <Icon Data="LucideIcons.House" />
                <span>Home</span>
            </a>
        </Render>
    </SidebarMenuButton>
    <SidebarMenuAction>
        <Icon Data="LucideIcons.Plus" /> <span class="sr-only">Add Project</span>
    </SidebarMenuAction>
</SidebarMenuItem>

SidebarMenuSub

The SidebarMenuSub component is used to render a submenu within a SidebarMenu.

<SidebarMenuItem>
    <SidebarMenuButton />
    <SidebarMenuSub>
        <SidebarMenuSubItem>
            <SidebarMenuSubButton />
        </SidebarMenuSubItem>
    </SidebarMenuSub>
</SidebarMenuItem>

SidebarMenuBadge

The SidebarMenuBadge component is used to render a badge within a SidebarMenuItem.

<SidebarMenuItem>
    <SidebarMenuButton />
    <SidebarMenuBadge>24</SidebarMenuBadge>
</SidebarMenuItem>

SidebarMenuSkeleton

The SidebarMenuSkeleton component is used to render a skeleton for a SidebarMenu.

<SidebarMenu>
    @for (var i = 0; i < 5; i++)
    {
        <SidebarMenuItem>
            <SidebarMenuSkeleton />
        </SidebarMenuItem>
    }
</SidebarMenu>

SidebarTrigger

Use the SidebarTrigger component to render a button that toggles the sidebar.

To build your own trigger, take the SidebarContext and call ToggleAsync.

Components/Layout/CustomTrigger.razor
<button type="button" @onclick="Sidebar.ToggleAsync">Toggle Sidebar</button>

@code {
    [CascadingParameter] private SidebarContext Sidebar { get; set; } = null!;
}

SidebarRail

The SidebarRail component is used to render a rail within a Sidebar. This rail can be used to toggle the sidebar.

<Sidebar>
    <SidebarHeader />
    <SidebarContent>
        <SidebarGroup />
    </SidebarContent>
    <SidebarFooter />
    <SidebarRail />
</Sidebar>

Controlled Sidebar

Use @bind-Open on the SidebarProvider to control the sidebar.

<SidebarProvider @bind-Open="_open">
    <Sidebar />
</SidebarProvider>

@code {
    private bool? _open = false;
}

Theming

We use the following CSS variables to theme the sidebar. They are in Styles/theme.css.

:root {
  --sidebar: oklch(0.985 0 0);
  --sidebar-foreground: oklch(0.145 0 0);
  --sidebar-primary: oklch(0.205 0 0);
  --sidebar-primary-foreground: oklch(0.985 0 0);
  --sidebar-accent: oklch(0.97 0 0);
  --sidebar-accent-foreground: oklch(0.205 0 0);
  --sidebar-border: oklch(0.922 0 0);
  --sidebar-ring: oklch(0.205 0 0);
}

.dark {
  --sidebar: oklch(0.205 0 0);
  --sidebar-foreground: oklch(0.985 0 0);
  --sidebar-primary: oklch(0.488 0.243 264.376);
  --sidebar-primary-foreground: oklch(0.985 0 0);
  --sidebar-accent: oklch(0.269 0 0);
  --sidebar-accent-foreground: oklch(0.985 0 0);
  --sidebar-border: oklch(1 0 0 / 10%);
  --sidebar-ring: oklch(0.922 0 0);
}

Styling

Here are some tips for styling the sidebar based on different states.

<Sidebar Collapsible="SidebarCollapsible.Icon">
    <SidebarContent>
        <SidebarGroup Class="group-data-[collapsible=icon]:hidden" />
    </SidebarContent>
</Sidebar>
<SidebarMenuItem>
    <SidebarMenuButton />
    <SidebarMenuAction Class="peer-data-[active=true]/menu-button:opacity-100" />
</SidebarMenuItem>

RTL

To enable RTL support in Blazix, see the RTL configuration guide.

Translated with AI for the demo, so it may have mistakes.

<Blazix.Primitives.MediaQuery Query="(max-width: 767px)" MatchesChanged="@(matches => _isMobile = matches)" />
<SidebarProvider>
    @* Side is physical: put the sidebar at the start of the reading direction, the right in RTL. *@

API Reference

ParameterTypeDefault
Side SidebarSide SidebarSide.Left
Variant SidebarVariant SidebarVariant.Sidebar
Collapsible SidebarCollapsible SidebarCollapsible.Offcanvas
Dir TextDirection? -
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarContent

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarFooter

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarGroup

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarGroupAction

ParameterTypeDefault
Render RenderFragment<UseRenderProps>? -
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarGroupContent

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarGroupLabel

ParameterTypeDefault
Render RenderFragment<UseRenderProps>? -
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarHeader

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarInput

ParameterTypeDefault
Value string? -
ValueChanged EventCallback<string?> -
Class string? -

Other attributes are passed to the element it renders.

SidebarInset

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarMenu

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarMenuAction

ParameterTypeDefault
Render RenderFragment<UseRenderProps>? -
ShowOnHover bool false
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarMenuBadge

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarMenuButton

ParameterTypeDefault
Render RenderFragment<UseRenderProps>? -
IsActive bool false
Variant SidebarMenuButtonVariant SidebarMenuButtonVariant.Default
Size SidebarMenuButtonSize SidebarMenuButtonSize.Default
Tooltip RenderFragment? -
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarMenuItem

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarMenuSkeleton

ParameterTypeDefault
ShowIcon bool false
Class string? -

Other attributes are passed to the element it renders.

SidebarMenuSub

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarMenuSubButton

ParameterTypeDefault
Render RenderFragment<UseRenderProps>? -
IsActive bool false
Size SidebarMenuSubButtonSize SidebarMenuSubButtonSize.Md
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarMenuSubItem

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarProvider

ParameterTypeDefault
DefaultOpen bool true
Open bool? -
OpenChanged EventCallback<bool> -
Style string? -
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

SidebarRail

ParameterTypeDefault
Class string? -

Other attributes are passed to the element it renders.

SidebarSeparator

ParameterTypeDefault
Class string? -

Other attributes are passed to the element it renders.

SidebarTrigger

ParameterTypeDefault
OnClick EventCallback<MouseEventArgs> -
Type ButtonType ButtonType.Button
Class string? -

Other attributes are passed to the element it renders.

Changelog

2026-11-24 Sidebar state and direction

SidebarContext.State is now a SidebarState: SidebarState.Expanded or SidebarState.Collapsed, not the strings "expanded" or "collapsed". The data-state attribute still says expanded or collapsed, so the styles don't change.

Sidebar's Dir is now a TextDirection: Dir="TextDirection.Rtl" instead of Dir="rtl".

@if (Sidebar.State == SidebarState.Collapsed)
{
    <p>The sidebar is collapsed.</p>
}

To get the new version, run dnx Blazix.Cli diff sidebar to see your changes, then dnx Blazix.Cli add sidebar --overwrite.