Skip to content

Sidebar

An app sidebar that folds to an icon rail and becomes a sheet on phones, built from parts or from a tree. Plus a docs variant with filters, counts and Pro marks. This site's own sidebar and the account pages run on it.

On this page
Used for
  • App navigation tree
  • Agent workspace switcher
  • Docs navigation
  • Icon rail layout
  • Mobile nav sheet

Examples

Icon rail

Open switcher with gradient avatars

expanded shows the list open in the flow, as a card, instead of in a popover: for environments, projects or teams that you switch often. Each workspace can carry a swatch, any CSS background, for a square avatar. The arrow keys move and pick, like radio buttons.

Docs sidebar

Installation

npx knowns-ui@latest add sidebar sidebar-docs
For Claude Code, Cursor or Codex: installs it and wires the props to your own data.
storageKey
string
Remembers the collapse in localStorage (<key>:collapsed). Pair it with sidebarBootScript([key]) in a <head> script so a server-rendered page paints collapsed, with no flash.
defaultCollapsed / collapsed / onCollapsedChange
boolean / boolean / (v) => void
Defaultfalse
Start as the rail, or control it.
hotkey
boolean
Defaulttrue
Cmd+B on macOS, Ctrl+B elsewhere; ignored while typing in a field. Nested in another sidebar's layout, only while focus is inside it.
mobileBelow
number
Default768
Below this width the sidebar is a modal sheet opened by SidebarTrigger.
label
string
Default"Sidebar"
Accessible name of the landmark and the sheet.
landmark
boolean
Defaulttrue
Render as an aside landmark. Set false when the sidebar is nested inside another landmark or shown as a demo.
collapsible
boolean | "offcanvas"
Defaulttrue
true collapses to the 56px icon rail (branches open flyouts on hover or click); "offcanvas" hides it fully (width 0, inert); false never collapses.
width
number
Default256
Expanded width in px.
sticky / top
boolean / number
Defaulttrue / 0
Stick to the viewport (fill its height below `top`), or fill the parent.
sheetHeader
ReactNode
The sheet's top row, beside its close button.
Motion
-
Collapsing and expanding ease the width over 180ms. Opening, the labels fade in and slide 10px into place one after another down each list (30ms apart, at most four rows apart); closing, they fade out together in 120ms. Reduced motion: instant.
toggle
boolean | { collapse, expand }
Puts a SidebarTrigger at the end of the first row (a logo or switcher). In the rail that row gives way and the toggle takes its place. Not shown in the mobile sheet, which has its own close button.
label
string
Default"Toggle sidebar"
Its name on mobile, where it opens the sheet (and on desktop without labels).
labels
{ collapse, expand }
Its name and tooltip on desktop by state, e.g. "Hide sidebar" / "Show sidebar". The tooltip adds the shortcut.
look
"ghost" | "field" | "float"
Default"ghost"
field matches a search field (36px, fill, radius); float is a 36px floating button (card, hairline border, soft shadow) for a trigger fixed over the page. Its chevron points in while the sidebar is open and turns 180 degrees to point out when it is collapsed.
items
SidebarNode[]
{ id, label, href?, icon?, badge?, count?, mark?, children?, defaultOpen?, disabled? }.
active
string
The current row: aria-current, and its branches open.
onSelect
(node) => void
Every row picked; links still navigate.
linkAs
Component
Default"a"
Your router's link, e.g. Next's Link.
storageKey
string
Remembers which branches are open.
Active pill
-
One pill behind the rows travels to the picked row (180ms, transform only) instead of the fill jumping. It follows folds and the rail without travelling. Reduced motion: it moves at once.
sections
DocsSection[]
{ id, label, icon?, href?, pages? | categories? }; pages take isNew and pro (true or "variants").
current
string
The current page, section or category id. Its folds open and the row scrolls into view.
filters / chips
DocsFilter[] / number
Default[] / 3
{ id, label, match, expand?, dot? }; the first `chips` are a segmented control, the rest a More menu. Empty sections drop out.
filter / onFilterChange
string / (id) => void
Control the filter, e.g. from a ?tier= query.
renderMark
(page) => ReactNode
DefaultNew dot, Pro star
Your own marks after a page's name.
onSearch / footer / storageKey / linkAs
-
The search button, a slot under the list, remembered folds, your link.

Keyboard: the tree is one tab stop. ↑ ↓ move between rows, Home and End jump, → opens a branch or steps into it, ← closes it or steps back to its parent. In the rail every row keeps its name: hover or focus shows it as a tooltip, and a branch opens a small flyout of its pages. On phones the sidebar is a modal dialog: focus stays inside, Escape or a tap outside closes it, focus returns to the trigger, and following a link closes it too. Motion (the rail's width, folds, the sheet's slide) turns instant with reduced motion.

What it handles

  1. 01Renders a nav tree from SidebarNode items with the tree keyboard: arrows move between visible rows, Home and End jump, Right opens or steps in, Left closes or steps out, and disabled rows are skipped.
  2. 02Collapses to a 56px icon rail with tooltips, or hides fully with collapsible="offcanvas", and toggles on Cmd or Ctrl+B unless you set hotkey to false.
  3. 03Remembers the rail and the open branches in localStorage under `storageKey`, and sidebarBootScript applies a saved collapse before the first paint so nothing jumps.
  4. 04Shows a branch in the rail as an icon that opens a small flyout of its children, so nested pages stay reachable without expanding the bar.
  5. 05Turns into a modal sheet below `mobileBelow` (768px by default), where SidebarTrigger opens it and the row of the current page is scrolled into view.
  6. 06Ships the parts around the tree: SidebarSwitcher for workspaces, SidebarSearch with a Cmd K hint, SidebarGroup with a foldable label, SidebarFooter, SidebarReveal and SidebarInset.

States

First render
Nothing. It paints in its remembered width
none
Collapse to the rail
The width switches at once and the labels fade out
120ms, ease out
Open a branch or fold a group
The rows grow or shrink and fade, and the chevron turns
180ms, ease out
Hover the trigger
Background and colour change
120ms, ease out
Mobile sheet
It slides in from the Sheet component
none
Reduced motion
Labels no longer fade and groups fold with no motion
0ms

Works with

When to use

When to use

  • The main navigation of an app: sections, nested projects, counts and a workspace switcher, folding to an icon rail.
  • A docs or component library with filters, categories and per-page marks.
  • One navigation for every screen size: a column on desktop, a modal sheet on phones.

When not to use

Using with AI

Use Copy page for AI above to give your assistant this page, Copy prompt under the install command to have it installed and wired to your data, or connect the Knowns UI MCP server and just ask:

  • Add the Knowns UI Sidebar to my app and wire it to my own data.
  • I need the main navigation of an app: sections, nested projects, counts and a workspace switcher, folding to an icon rail. Set that up with the Knowns UI Sidebar.
  • Which Sidebar props do I need for a minimal setup? Show me a short example.

With the server connected, your assistant calls get_component({ name: "sidebar" }) and search_docs({ query: "sidebar" }).

Need help?

Stuck on Sidebar, or found something off? We read every message.