Sidebar
An app's main navigation: sections of items with icons and counts, open, icons only or hidden, and a drawer on small screens.
Demo
Usage
import { Avatar, Button, FileIcon, GridIcon, MenuIcon, Sidebar, SidebarIcon, UsersIcon, type SidebarMode } from '@tuttn/slant'
// Uncontrolled: open at wide widths, a Menu button and a drawer at 900px and below.
<Sidebar label="Workspace">
<Sidebar.Header><Brand /></Sidebar.Header>
<Sidebar.Section label="Workspace">
<Sidebar.Item href="/overview" icon={<GridIcon />} current={route === 'overview'}>Overview</Sidebar.Item>
<Sidebar.Item href="/content" icon={<FileIcon />} count={128}>Content</Sidebar.Item>
<Sidebar.Item icon={<UsersIcon />} onClick={openTeam}>Team</Sidebar.Item>
</Sidebar.Section>
<Sidebar.Footer><Avatar name={me.name} /> {me.name}</Sidebar.Footer>
</Sidebar>
// Controlled: the app's own button (in its header or tab strip) cycles the mode.
const [mode, setMode] = useState<SidebarMode>('open')
<Button icon aria-label="Toggle sidebar" onClick={() => setMode(next[mode])}><SidebarIcon /></Button>
<Sidebar label="Workspace" mode={mode} onModeChange={setMode} collapse="sm">…</Sidebar>
// A router's Link as the item, and the drawer held by the app.
<Sidebar label="Workspace" drawerOpen={open} onDrawerOpenChange={setOpen}
trigger={<Button variant="ghost"><MenuIcon /> Browse</Button>} drawerTitle="Go to">
<Sidebar.Section>
<Sidebar.Item asChild icon={<FileIcon />} current={path === '/content'}><Link to="/content">Content</Link></Sidebar.Item>
</Sidebar.Section>
</Sidebar>
/* Your stylesheet: the header and footer follow the mode in effect. */
.app-sidebar[data-mode="icons"] .brand-name { display: none; }
.app-sidebar { --sl-sidebar-width: 260px; height: 100dvh; }Props
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | messages.navigation ("Navigation") | The navigation's accessible name, on its `<nav>`, and the drawer's title unless `drawerTitle` is set. |
mode | SidebarMode | — | How the column shows at wide widths: `open` (icons, labels and counts), `icons` (a rail of icons, each label in a tooltip) or `hidden`. Pass it with onModeChange to control it; leave it out and the column stays at `defaultMode`. Ignored while collapsed. |
defaultMode | SidebarMode | 'open' | The mode it starts in, when uncontrolled. |
onModeChange | (mode: SidebarMode) => void | — | Called with the new mode when it changes. Sidebar has no control of its own that changes it: the app's button does, by passing a new `mode`. |
collapse | 'sm' | 'md' | 'lg' | 'always' | 'never' | 'md' | The width at and below which the column gives way to a button that opens the same content in a Drawer: `sm` 640px, `md` 900px, `lg` 1200px. `always` keeps it collapsed, `never` never collapses. |
drawerOpen | boolean | — | Whether the drawer is open, while collapsed. Pass it with onDrawerOpenChange to control it. |
defaultDrawerOpen | boolean | false | Whether the drawer starts open, when uncontrolled. |
onDrawerOpenChange | (open: boolean) => void | — | Called with the drawer's new state: the trigger, Escape, the backdrop, its close button, a choice of item, or the window widening past the breakpoint. |
trigger | ReactElement | a ghost Button with MenuIcon and messages.openNavigation ("Menu") | The button that opens the drawer while collapsed, cloned with `aria-expanded`, `aria-haspopup` and the open handler; its own onClick runs first. |
drawerTitle | string | label | The drawer's visible title, which names it. |
children | ReactNode | — | Sidebar.Header, Sidebar.Section and Sidebar.Footer parts, in any order. |
Sidebar.Section label | ReactNode | — | The section's visible heading (a div with an id), which names its list through aria-labelledby. Visually hidden on the rail, where a hairline separates the sections. |
Sidebar.Section …rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on the section's div. Its Items go in a ul inside. |
Sidebar.Item href | string | — | Makes the item a link. Without it (and without asChild), the item is a button with type="button". |
Sidebar.Item asChild | boolean | — | Renders the single child, such as a router's Link, as the item: the class, icon, label, count and handlers go onto it. |
Sidebar.Item icon | ReactNode | — | A leading glyph, aria-hidden. On the rail it is all that shows, so give every item one there. |
Sidebar.Item count | ReactNode | — | A number or short text at the inline end, monospace and muted, read as part of the item's name. On the rail it becomes a pixel on the icon's corner, and the number stays in the name. |
Sidebar.Item current | boolean | — | The current page: aria-current="page" and the accent marker. The column scrolls to keep it in view. |
Sidebar.Item disabled | boolean | — | A button gets disabled; a link gets aria-disabled and no href (and keeps role="link"). Either way it leaves the tab order. |
Sidebar.Item textValue | string | — | The label as plain text, for the rail's tooltip when the label isn't a string. |
Sidebar.Item …rest | HTMLAttributes<HTMLElement> | — | className, style, other attributes and the ref go on the link or button (or your asChild element), inside an li. |
Sidebar.Header / Sidebar.Footer | HTMLAttributes<HTMLDivElement> | — | The brand or workspace switch above the nav, and the account below it, as divs (className, style, the ref and other attributes go there). Sidebar places them outside the <nav>, wherever they're written, in the column and in the drawer alike. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on the root div, which carries data-mode (the mode in effect, open while collapsed), data-collapse and, once collapsed, data-collapsed. |
Keyboard
| Tab | moves through the items in order, like any links and buttons; disabled items are skipped |
| Enter | follows a link item, or presses a button item (Space too) |
| Escape | closes the drawer and puts focus back on its trigger |
Accessibility
- The items sit in a
<nav>named bylabel("Navigation" by default); give each Sidebar on a page its own name. The root is a plain div, never an<aside>, so it adds no complementary landmark, and the Header and Footer sit outside the nav. - Each section is a list named by its label through
aria-labelledby. The current item hasaria-current="page", and the marker is drawn, never the only cue. - A count is part of the item's name ("Content 128"); icons are
aria-hidden. On the rail the label and count are only visually hidden, so the name stays, and a Tooltip shows the label on hover and on keyboard focus. - There are no arrow keys: a navigation list is a list of tab stops. In hidden mode the column is
hidden, so nothing in it takes focus. - Collapsed, the trigger has
aria-haspopup="dialog"andaria-expanded. The drawer is a modal Drawer titled bydrawerTitle(elselabel), focused on the current item, and the rest of the page is inert while it's open.
Modes
mode is open (240px, --sl-sidebar-width), icons (a rail about one control wide, --sl-sidebar-rail) or hidden. Both widths are custom properties you can set on the root or above it. Sidebar has no toggle of its own: the app's button, in its header or tab strip, passes a new mode. Uncontrolled, it stays at defaultMode. The root's data-mode is the mode in effect (open while collapsed, since the drawer shows everything), so your CSS can shrink the Header's and Footer's content on the rail.
Collapse
At and below collapse's width (sm 640px, md 900px, the default, lg 1200px; always or never), the CSS hides the column and shows the trigger, so a server-rendered page is right before it hydrates, and mode is ignored. The trigger is your trigger element or a ghost Menu button, with the class sl-sidebar-trigger. It opens a floating Drawer from the start edge (the right, right to left) holding the Header, the nav and the Footer. Once hydrated, a media query decides where the content renders: in the column at wide widths, in the drawer while collapsed, never both; the drawer's content mounts the first time it opens, so the page's HTML carries every link once. A click on an item in the drawer closes it, and so does widening the window past the breakpoint.
The current item
When an item mounts as the current one, when it becomes current, and when the column comes back from hidden or collapsed, the nav's body scrolls (never the page) to bring it into view, centred in the part of the body the window shows, which matters when the column is sticky and taller than the window. An item already in view stays put, and a render that leaves the current item alone leaves your scroll alone. The body's scroll-padding-block is the margin it keeps from the edges.
Layout
The root is a flex column: give it a height (or put it in a grid row) and the column fills it, with the nav's body scrolling between the Header and the Footer. Items go inside a Sidebar.Section, which holds their list; use one without a label for an unnamed group. Header, Footer and Sections are found among Sidebar's direct children, so don't wrap them in a fragment or a component of your own. The docs' own contents column on the left of this page is a Sidebar.