Sidebar Layout
A page with a sidebar beside it: resizable, collapsible, and a drawer on narrow screens, with a header and footer that stay as the page scrolls.
Usage
import {
Sidebar,
SidebarLayout,
SidebarLayoutHeader,
} from 'nyctoui/sidebar'
<SidebarLayout
sidebar={
<Sidebar aria-label="Main">
<SidebarHeader>…</SidebarHeader>
<SidebarContent>…</SidebarContent>
</Sidebar>
}
sx={styles.page}
>
<SidebarLayoutHeader>
<Breadcrumbs>…</Breadcrumbs>
</SidebarLayoutHeader>
<main>…</main>
</SidebarLayout>
// A whole-page layout fills the window.
const styles = stylex.create({ page: { height: '100dvh' } })The sidebar prop takes the column, usually a Sidebar, and the children are the page. The layout decides where the sidebar goes: in a Resizable panel beside the page when there is room, or in a Drawer over it when there is not. The page scrolls on its own, inside a scroll area that fills the layout’s height.
Good for
- The frame of a SaaS app: navigation on the left, the page on the right.
- An email client with folders in the sidebar.
- A docs site with the page list beside the content.
- An admin console that turns its navigation into a drawer on phones.
The toggle
The layout draws its own toggle, at the sidebar’s top corner, and it never moves. With the sidebar open it is the first thing in the SidebarHeader, which leaves room for it, its icon in line with the rows’ icons below. Collapsed, the page moves aside for a narrow rail and the toggle stays where it was; hover it and the sidebar fades in over the page, staying while the pointer is on it. Click while it is out and it stays open.
On narrow screens the toggle sits at the start of the SidebarLayoutHeader as a menu button. It opens the drawer and, staying in place above it, turns into its close button. Give the sidebar a SidebarHeader so the toggle has a row to sit in.
Anatomy
<SidebarLayout sidebar={<Sidebar />}>
<SidebarLayoutHeader />
{/* the page */}
<SidebarLayoutFooter />
</SidebarLayout>Header and footer are optional. Put the header first and the footer last: they stick to the top and bottom of the page as it scrolls, and each draws a hairline only while content is passing under it.
Examples
Narrow screens
Below breakpoint, 640px by default, the page takes the whole width and the toggle opens the sidebar as a drawer from the same side, then closes it from the same spot. Following a link in it closes it too. The breakpoint is measured on the layout itself, not the window, so this 375px frame shows it on any screen; its drawer opens from the window’s edge, as it would on a phone.
Sticky footer
SidebarLayoutFooter holds a composer or a form’s actions along the bottom. On a short page it still sits at the bottom.
Flush
variant="flush" runs the sidebar and the page edge to edge with a hairline between them, for tools where the page is a canvas or an editor.
Right side
side="right" puts the sidebar after the page, as an inspector. Its drawer opens from the right.
Sizes and saving
defaultSize, minSize and maxSize bound the sidebar, in pixels by default. defaultCollapsed starts it closed, and autoSaveId keeps its width and state between visits.
Controlled
Pass collapsed and onCollapsedChange to keep the state yourself. Dragging the edge closed or open reports the change like the toggle does.
Keyboard shortcut
useSidebarLayout() reads and changes the layout from anywhere inside it. Its toggle collapses the sidebar on wide layouts and opens the drawer on narrow ones.
Keyboard
The sidebar’s edge is a resize handle: focus it with Tab, move it with the arrow keys, and press Enter to collapse or restore the sidebar. The toggle works with Enter and Space and never peeks: from the keyboard it collapses and expands. In the drawer, focus stays inside until it closes, then returns to the toggle. Escape closes a peek or the drawer.
Best practices
- Save the sidebar’s width with
autoSaveId, so it stays as people left it. - Name the sidebar with
sidebarLabel, so screen readers list it as navigation. - Start the page’s own heading inside the content, as its first heading.
- Check the narrow layout, where the sidebar becomes a drawer, at a phone width.
- Put a sidebar layout inside another one. It’s the outer frame of the page.
- Use it for a two-column area inside a page. That’s resizable panels or plain layout.
API reference
SidebarLayout
The layout. Fills the height it is given and spreads the rest onto its root <div>, which carries data-mobile below the breakpoint and data-collapsed while the sidebar is collapsed.
| Prop | Type | Default |
|---|---|---|
sidebarThe column beside the page, usually a <Sidebar>. | ReactNode | — |
sideWhich side the sidebar sits on, and the drawer opens from. | 'left' | 'right' | 'left' |
variantinset sets the page in a raised panel; flush runs both edge to edge with a hairline between. | 'inset' | 'flush' | 'inset' |
defaultSizeThe sidebar’s starting width. | number | `${number}px` | `${number}%` | `${number}rem` | '240px' |
minSizeThe narrowest the sidebar gets while open. | number | `${number}px` | `${number}%` | `${number}rem` | '200px' |
maxSizeThe widest the sidebar gets. | number | `${number}px` | `${number}%` | `${number}rem` | '360px' |
collapsibleLets the sidebar collapse, with the toggle, by dragging, or Enter on its edge. Without it the toggle shows only on narrow layouts. | boolean | true |
collapsedWhether the sidebar is collapsed. Pass it to control the layout. | boolean | — |
defaultCollapsedWhether the sidebar starts collapsed, when uncontrolled. | boolean | false |
onCollapsedChangeRuns when the sidebar collapses or expands, by any means. | (collapsed: boolean) => void | — |
breakpointBelow this width of the layout, in pixels, the sidebar becomes a drawer. | number | 640 |
autoSaveIdSaves the sidebar’s width and collapsed state in localStorage under this key. | string | — |
sidebarLabelNames the drawer the sidebar opens in. | string | 'Sidebar' |
toggleLabelNames the toggle. aria-expanded says whether the sidebar is open. | string | 'Toggle sidebar' |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SidebarLayoutHeader
A 48px bar that sticks to the top of the page, with a hairline once content scrolls under it. On narrow layouts it leaves its first slot to the toggle.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SidebarLayoutFooter
A bar that sticks to the bottom of the page. Put it last.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
useSidebarLayout()
The layout around the caller. Throws outside a SidebarLayout.
| Prop | Type | Default |
|---|---|---|
collapsedWhether the sidebar is collapsed beside the page. | boolean | — |
setCollapsedCollapses or expands it. | (collapsed: boolean) => void | — |
mobileWhether the layout is below its breakpoint. | boolean | — |
openWhether the drawer is open, on narrow layouts. | boolean | — |
setOpenOpens or closes the drawer. | (open: boolean) => void | — |
toggleDoes whichever of the two the layout is showing. | () => void | — |