Components

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.

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

Do
  • 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.
Don’t
  • 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.

PropTypeDefault
sidebar
The column beside the page, usually a <Sidebar>.
ReactNode—
side
Which side the sidebar sits on, and the drawer opens from.
'left' | 'right''left'
variant
inset sets the page in a raised panel; flush runs both edge to edge with a hairline between.
'inset' | 'flush''inset'
defaultSize
The sidebar’s starting width.
number | `${number}px` | `${number}%` | `${number}rem`'240px'
minSize
The narrowest the sidebar gets while open.
number | `${number}px` | `${number}%` | `${number}rem`'200px'
maxSize
The widest the sidebar gets.
number | `${number}px` | `${number}%` | `${number}rem`'360px'
collapsible
Lets the sidebar collapse, with the toggle, by dragging, or Enter on its edge. Without it the toggle shows only on narrow layouts.
booleantrue
collapsed
Whether the sidebar is collapsed. Pass it to control the layout.
boolean—
defaultCollapsed
Whether the sidebar starts collapsed, when uncontrolled.
booleanfalse
onCollapsedChange
Runs when the sidebar collapses or expands, by any means.
(collapsed: boolean) => void—
breakpoint
Below this width of the layout, in pixels, the sidebar becomes a drawer.
number640
autoSaveId
Saves the sidebar’s width and collapsed state in localStorage under this key.
string—
sidebarLabel
Names the drawer the sidebar opens in.
string'Sidebar'
toggleLabel
Names the toggle. aria-expanded says whether the sidebar is open.
string'Toggle sidebar'
sx
StyleX 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.

PropTypeDefault
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

SidebarLayoutFooter

A bar that sticks to the bottom of the page. Put it last.

PropTypeDefault
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

useSidebarLayout()

The layout around the caller. Throws outside a SidebarLayout.

PropTypeDefault
collapsed
Whether the sidebar is collapsed beside the page.
boolean—
setCollapsed
Collapses or expands it.
(collapsed: boolean) => void—
mobile
Whether the layout is below its breakpoint.
boolean—
open
Whether the drawer is open, on narrow layouts.
boolean—
setOpen
Opens or closes the drawer.
(open: boolean) => void—
toggle
Does whichever of the two the layout is showing.
() => void—