Sidebar

An app shell sidebar with a top bar, app-provided nav content, and a footer — collapses to a 64px icon rail on desktop and a drawer with an overlay on mobile

Installation

pnpm add @wandercom/design-system-web

Usage

Sidebar renders no nav logic of its own — pass NavItems (and NavSectionHeaders) as children, and thread the effective collapsed state to each NavItem's rail prop so they collapse together. Derive that value with the exported SIDEBAR_DESKTOP_QUERY rather than using your own collapsed state directly — Sidebar is never collapsed below 1024px (it's a drawer there), even while collapsed stays true, and your NavItems need to follow:

19 lines
import { useState } from 'react';
import { IconHome } from '@wandercom/design-system-icons/home';
import { useMediaQuery } from '@wandercom/design-system-shared/hooks/use-media-query';
import { NavItem } from '@wandercom/design-system-web/ui/nav-item';
import { Sidebar, SIDEBAR_DESKTOP_QUERY } from '@wandercom/design-system-web/blocks/sidebar';

export function Example() {
  const [collapsed, setCollapsed] = useState(false);
  const isDesktop = useMediaQuery(SIDEBAR_DESKTOP_QUERY);
  const rail = isDesktop !== false && collapsed;

  return (
    <Sidebar collapsed={collapsed}>
      <NavItem active href="/" icon={<IconHome />} rail={rail}>
        Home
      </NavItem>
    </Sidebar>
  );
}

Sidebar needs real viewport height and width to show its breakpoints accurately, so this one is best viewed full screen — use the sidebar button in the page header to see the rail, and shrink the window to see the mobile drawer:

Loading example...

Layout

Sidebar sits on the app canvas (bg-surface-canvas). It is 256px wide (64px on the rail) and fills its parent's height, so give it a viewport-height parent (such as AppShell.Sidebar): the 64px top bar and the footer stay put, and only the nav area scrolls. The nav has a 16px start and 8px end inset, and its rows sit with no gap between them. Every icon stays in the same place whether the sidebar is expanded or on the rail. Place NavSectionHeaders directly among the NavItems: each header after the first starts a new section 20px below the previous row.

Collapsing and expanding

Sidebar renders no collapse or expand toggle of its own. Inside AppShell, clicking the canvas's left edge collapses and expands it (see App Shell). While it is collapsed, render a SidebarTrigger in your page header that sets your collapsed state back to false, labelled with DEFAULT_SIDEBAR_LABELS.expandToggle:

15 lines
import {
  DEFAULT_SIDEBAR_LABELS,
  SidebarTrigger,
} from '@wandercom/design-system-web/blocks/sidebar';

<PageShellHeader>
  {collapsed && (
    <SidebarTrigger
      aria-label={DEFAULT_SIDEBAR_LABELS.expandToggle}
      className="max-[1024px]:hidden"
      onClick={() => setCollapsed(false)}
    />
  )}
  {/* title, actions, etc. */}
</PageShellHeader>

Top bar

The top bar starts with logo, which defaults to <SidebarHomeButton href="/" />: a 32×32 link home with an 18px Wander logomark that turns like a globe while hovered or focused. It stays in place, and keeps linking home, on the rail. Pass asChild to render it around your router's link:

11 lines
import { SidebarHomeButton } from '@wandercom/design-system-web/blocks/sidebar';

<Sidebar
  logo={
    <SidebarHomeButton asChild>
      <Link href="/" />
    </SidebarHomeButton>
  }
>
  {/* nav content */}
</Sidebar>

Pass tool buttons to actions. They render at the right of the top bar, 8px apart, and hide on the rail. Use SidebarToolButton, a 32×32 ghost icon button:

18 lines
import { IconBellLight } from '@wandercom/design-system-icons/bell-light';
import { IconSearchLight } from '@wandercom/design-system-icons/search-light';
import { Sidebar, SidebarToolButton } from '@wandercom/design-system-web/blocks/sidebar';

<Sidebar
  actions={
    <>
      <SidebarToolButton aria-label="Activity" asChild>
        <Link href="/tasks"><IconBellLight /></Link>
      </SidebarToolButton>
      <SidebarToolButton aria-label="Search" onClick={openCommandMenu}>
        <IconSearchLight />
      </SidebarToolButton>
    </>
  }
>
  {/* nav content */}
</Sidebar>

footer is a full-override slot — pass any node, or use the built-in SidebarFooter: the account row, a 24px avatar (icon, 4px radius) and the ellipsized account name on a 34px row with an 8px inset. On the rail it shows the avatar alone, on a 34×34 button:

15 lines
import {
  Sidebar,
  SidebarFooter,
} from '@wandercom/design-system-web/blocks/sidebar';

<Sidebar
  footer={
    <SidebarFooter
      label="Acme Inc"
      onPress={() => openSettings()}
    />
  }
>
  {/* nav content */}
</Sidebar>

What's new

footerAction takes a SidebarFooterAction: a round 32px button at the right of the footer. On hover (with a mouse or trackpad), keyboard focus or while its popover is open, it widens over the account row and reveals its label, while the account name fades out. On the rail it sits above the account, icon only, with a native tooltip. It forwards its ref and props, so it works as a PopoverTrigger asChild:

17 lines
import { IconBolt } from '@wandercom/design-system-icons/bolt';
import { SidebarFooterAction } from '@wandercom/design-system-web/blocks/sidebar';
import { Popover, PopoverContent, PopoverTrigger } from '@wandercom/design-system-web/ui/popover';

<Sidebar
  footer={<SidebarFooter icon={<OrgAvatar />} label="Westlake Cabins" />}
  footerAction={
    <Popover>
      <PopoverTrigger asChild>
        <SidebarFooterAction icon={<IconBolt />} label="What's new?" />
      </PopoverTrigger>
      <PopoverContent align="start" side="top">…</PopoverContent>
    </Popover>
  }
>
  {/* nav content */}
</Sidebar>

Its animation lives in the package's styles.css (@wandercom/design-system-web/components), which apps already import.

Account menu

For a settings/account popover instead of a plain click, build it with the existing Dropdown primitives and wrap SidebarFooter as the trigger via asChild:

32 lines
import { SidebarFooter } from '@wandercom/design-system-web/blocks/sidebar';
import {
  Dropdown,
  DropdownContent,
  DropdownItem,
  DropdownLinkItem,
  DropdownSeparator,
  DropdownSub,
  DropdownSubContent,
  DropdownSubTrigger,
  DropdownTrigger,
} from '@wandercom/design-system-web/ui/dropdown';

<Dropdown>
  <DropdownTrigger asChild>
    <SidebarFooter label="Acme Inc" />
  </DropdownTrigger>
  <DropdownContent align="start" className="w-64" side="top">
    <DropdownLinkItem href="/settings">Settings</DropdownLinkItem>
    <DropdownLinkItem href="/whats-new">What's new</DropdownLinkItem>
    <DropdownLinkItem href="/help">Help center</DropdownLinkItem>
    <DropdownSeparator />
    <DropdownSub>
      <DropdownSubTrigger>Switch organization</DropdownSubTrigger>
      <DropdownSubContent>
        <DropdownItem onClick={() => switchOrg('acme')}>Acme Inc</DropdownItem>
        <DropdownItem onClick={() => switchOrg('globex')}>Globex Corp</DropdownItem>
      </DropdownSubContent>
    </DropdownSub>
    <DropdownItem onClick={() => signOut()}>Sign out</DropdownItem>
  </DropdownContent>
</Dropdown>

Since the popover shape (an org section, a user section, nested submenus, checkmarks for the current selection) varies by app, Sidebar doesn't ship an opinionated version of it — compose it from Dropdown/DropdownSub in your own code instead, same as any other menu in the app. See the Dropdown docs for the full set of primitives (links, actions, checkboxes, nested submenus, separators).

Drawer

Below 1024px, Sidebar renders itself in a 256px drawer from the left, over an overlay, instead of inline. Its top bar keeps your actions and adds a close button (the sidebar icon) after them. Open and close it from code with open/onOpenChange:

3 lines
<Sidebar open={mobileNavOpen} onOpenChange={setMobileNavOpen}>
  {/* nav content */}
</Sidebar>

Below 1024px, render a SidebarTrigger (a 32px round ghost button with the sidebar icon) in your own header (e.g. PageShellHeader), wired to the same state:

7 lines
import { SidebarTrigger } from '@wandercom/design-system-web/blocks/sidebar';
import { PageShellHeader } from '@wandercom/design-system-web/blocks/page-shell';

<PageShellHeader>
  <SidebarTrigger className="hidden max-[1024px]:inline-flex" onClick={() => setMobileNavOpen(true)} />
  {/* title, actions, etc. */}
</PageShellHeader>

Sidebar never overwrites your collapsed value on resize — it's your persisted preference, not Sidebar's to clobber. Derive the effective value (see Usage above) with the exported SIDEBAR_DESKTOP_QUERY wherever you need it, rather than reading collapsed directly:

5 lines
import { useMediaQuery } from '@wandercom/design-system-shared/hooks/use-media-query';
import { SIDEBAR_DESKTOP_QUERY } from '@wandercom/design-system-web/blocks/sidebar';

const isDesktop = useMediaQuery(SIDEBAR_DESKTOP_QUERY);
const effectiveCollapsed = isDesktop !== false && collapsed;

Rail state

From 1024px up, collapsed shrinks Sidebar to the 64px icon rail. The logo stays in place, actions hide, and NavItems become 34×34 icon buttons with their icons where they sit when expanded. SidebarFooter collapses to its avatar alone and keeps whatever onPress or Dropdown behavior you wired to it.

Props

logo?:

React.ReactNode
The start of the top bar, in the same place expanded or collapsed. Defaults to <SidebarHomeButton href="/" />; pass your own SidebarHomeButton (e.g. asChild around a router Link).

actions?:

React.ReactNode
Top bar tool buttons (use SidebarToolButton), rendered at the right, 8px apart. Hidden on the rail.

collapsed?:

boolean
Controlled collapsed state for the desktop rail.

defaultCollapsed?:

boolean
Uncontrolled default collapsed state. Defaults to false.

onCollapsedChange?:

(collapsed: boolean) => void
Deprecated and ignored: Sidebar has no collapse toggle, so it never changes its collapsed state.

collapsible?:

boolean
Deprecated and ignored: Sidebar renders no collapse toggle. Collapse it from AppShell's canvas edge and expand it with a SidebarTrigger in the page header.

open?:

boolean
Controlled open state for the mobile drawer.

defaultOpen?:

boolean
Uncontrolled default open state for the mobile drawer. Defaults to false.

onOpenChange?:

(open: boolean) => void
Fires when the mobile drawer's open state changes.

children:

React.ReactNode
Nav content — sections and NavItems passed in by the app. Sidebar renders no app-specific nav logic of its own. Derive the effective collapsed state with SIDEBAR_DESKTOP_QUERY (always false below 1024px) and thread it to your NavItems' rail prop.

footerAction?:

React.ReactNode
A SidebarFooterAction (the What's new trigger), at the right of the footer, or above the account on the rail.

className?:

string
Additional CSS classes.

labels?:

Partial<SidebarLabels>
Override any subset of the localizable aria-labels: openMenu (SidebarTrigger default), closeMenu (drawer close button) and navigation (drawer landmark). collapseToggle and expandToggle are suggested labels for your own SidebarTrigger; Sidebar renders no toggle.

SidebarTrigger

A thin Button wrapper (variant="ghost", size="icon-sm": 32px and round, with the sidebar icon) for your own header: below 1024px it opens the drawer, and from 1024px it expands the collapsed sidebar (pass aria-label={DEFAULT_SIDEBAR_LABELS.expandToggle}). Accepts every Button prop; aria-label defaults to "Open navigation".

SidebarHomeButton

The Wander logomark link for Sidebar's logo: 32×32, 8px radius, an 18px logo that turns like a globe while hovered or focused. Accepts every native <a> prop; aria-label defaults to "Home". Pass asChild to merge its props, styles and logo onto its only child, such as a router Link.

SidebarToolButton

A 32×32 ghost icon button (8px radius, 18px text-secondary icon, surface-quaternary hover fill) for Sidebar's actions. Requires an aria-label, supports asChild, and accepts every native <button> prop, including ref.

SidebarFooterAction

The What's new trigger for Sidebar's footerAction. Takes icon (16px) and label (revealed on hover, focus or open, and the accessible name), plus every native <button> prop, including ref.

SidebarFooter

The account row for Sidebar's footer slot: a 24px avatar and the account name on a 34px row with an 8px inset, filled with surface-quaternary on hover and while its menu is open. Collapses to the 34×34 avatar on the icon rail. Its account name carries data-account-fade, which fades out while a SidebarFooterAction is active. Wrap it in your own Dropdown (via asChild on DropdownTrigger) for an account menu — see Footer above.

icon?:

React.ReactNode
Leading 24×24 icon, clipped to a 4px radius (e.g. an org avatar). Defaults to IconBuilding.

label:

React.ReactNode
Label text, ellipsized; visually hidden (still announced) on the icon rail. Usually the current business name.

onPress?:

() => void
Click handler.

chevronDirection?:

"down" | "up" | "none"
Direction the trailing chevron points at rest, flipping 180° automatically when composed as an open Dropdown trigger. Defaults to "none" (no chevron).

className?:

string
Additional CSS classes.

Also accepts any other native <button> prop (e.g. aria-label), spread onto the rendered button — including the props a wrapping DropdownTrigger asChild injects, so it composes as a dropdown trigger.

Sidebar