# Sidebar Layout

> SidebarLayout places a SidebarPanel beside SidebarMain - push mode with a CSS grid track, an icon rail, start or end side, a cancellable open state, and a container or media breakpoint that switches to the overlay presentation without hydration differences.

Canonical: https://salmonui.dev/docs/navigation/sidebar-layout

```tsx
import { SidebarLayout, SidebarMain, SidebarPanel, SidebarTrigger } from '@salmonui/navigation/sidebar-layout';
import '@salmonui/navigation/sidebar-layout/styles.css';

<SidebarLayout collapseBelow={768} side="start" style={{ blockSize: '100dvh' }}>
  <SidebarPanel navigation aria-label="Main navigation">…</SidebarPanel>
  <SidebarMain render={(props) => <main {...props} />}>
    <SidebarTrigger />
    …
  </SidebarMain>
</SidebarLayout>
```

A sidebar can be a layout column (push, or a docked icon rail) or a modal
overlay. Salmon UI keeps the two apart: `SidebarLayout` is the layout, and
its overlay mode is the place where the Layout category's `Drawer` plugs in.
The panel is bounded to the layout's own element
(`position: relative`, no portal), so there is no target prop: place the
layout inside the container the sidebar belongs to.
`/sidebar-layout/styles.css` is all push mode needs besides
`@salmonui/core/styles.css`; the overlay presentation also needs the Drawer
stylesheet ([Overlay mode](https://salmonui.dev/docs/navigation/sidebar-layout#overlay-mode)).

## Parts

| Component | Element | Notes |
| --- | --- | --- |
| `SidebarLayout` | `<div class="salmon-sidebar-layout">` | The grid and the state owner; `ref` is this element. `data-side`, `data-mode`, `data-presentation`, `data-state`, `data-collapsed` |
| `SidebarPanel` | `<aside>`, or `<nav>` with `navigation` | Landmark named by `aria-label` / `aria-labelledby`, `localeText.panel` (`'Sidebar'`) by default. A closed panel is `inert` and `visibility: hidden`, so it leaves the tab order and the accessibility tree. Carries `data-salmon-surface="sidebar"` ([Theme surface](https://salmonui.dev/docs/navigation/sidebar-layout#theme-surface)) |
| `SidebarMain` | `<div class="salmon-sidebar-main">` | The content; `render` returns another element (`<main>`, `<section>`) with the computed props |
| `SidebarTrigger` | `<button type="button">` | `aria-expanded` and `aria-controls` (the panel id). `action="open"` (default) toggles the panel, `action="collapse"` the icon rail. Without text content it is named by `localeText.toggle` / `localeText.collapse`; `icon` replaces the built-in icon; `render` keeps ARIA and handlers |

`className`, `style` and DOM props reach each part's element.

## Push mode, size, rail and side

`mode="push"` (default) is a CSS grid: the panel track is `size`
(`16rem` by default; a number is px) while open, `railSize` (`56px`) while
`collapsed`, and 0 while closed; the main content takes the rest, so its
inline size shrinks. The track change is one CSS
transition, off under `prefers-reduced-motion`. There is no slide mode that
translates the main content instead of resizing it: it looks
close to push and would cost a second layout path.
Use `mode="push"`; if the main content must keep its width, give it a
`min-inline-size`.

`side="start" | "end"` is logical and follows the inherited `dir`.
Rail content (icons with labels) can hide its labels through
`.salmon-sidebar-panel[data-collapsed]`.

## State and events

- `open` / `defaultOpen` / `onOpenChange(open, event)`: `defaultOpen` is
  `true` in push mode and `false` in overlay mode. The event has `reason`
  (`'trigger' | 'outside' | 'escape' | 'breakpoint'`) and `source`
  (`'pointer' | 'keyboard' | 'touch' | 'api'`).
- `onBeforeOpenChange(event)` runs first, synchronously, with the proposed
  `open`; `event.preventDefault()` cancels the change. Event objects are only
  created when a handler is bound and are frozen in development builds.
- `collapsed` / `defaultCollapsed` / `onCollapsedChange(collapsed, event)`
  drive the rail.
- There are no show, hide or toggle methods: the open state is a
  prop, so methods would let the panel and the application state disagree.
  Set the state you pass to `open`, or render a `SidebarTrigger`.

```tsx
const [open, setOpen] = useState(true);

<SidebarLayout open={open} onOpenChange={setOpen}>
  …
</SidebarLayout>
```

- `closeOnOutsidePress` (push presentation) closes the panel on a press
  outside it and its triggers (push half). When the panel closes
  while focus is inside it, focus moves to its trigger (push half).

## collapseBelow: adaptive presentation

`collapseBelow` switches between push and overlay by width: a
number is the layout's own
inline size in px (a container query; the layout is a size container named
`salmon-sidebar-layout`), a string is a media query that matches when narrow
(`'(max-width: 767px)'`).

The server cannot know the width, so the server markup carries a
per-instance `<style>` rule that hides the panel below the breakpoint before
JavaScript runs. After mount the layout measures itself (one
`ResizeObserver` or one media query list, loaded as a small lazy chunk),
sets `data-presentation`, and takes over: becoming narrow closes the panel
(`reason: 'breakpoint'`), becoming wide restores the last wide state. The
server render and the hydration render are identical.

## Overlay mode

`mode="overlay"` (and the narrow side of `collapseBelow`) presents the panel
over the main content. It renders only after hydration, so server markup is
always closed. The overlay presentation is a lazily loaded module that
renders the panel in the same drawer core as the Layout category's
[Drawer](https://salmonui.dev/docs/layout/dialog#drawer) (native `<dialog>` `showModal()`,
backdrop, focus containment, inert background), without importing
`@salmonui/overlays`. Focus moves into the panel when it opens; Escape and a press on
the backdrop close it, and focus returns to the trigger.
The sidebar stylesheet does not carry the drawer rules (they would put it
over its size budget): import `@salmonui/overlays/drawer/styles.css` too when
a layout can show the overlay presentation. Overlay mode uses the top
layer instead of a `z-index` prop: the top layer is above every
`z-index` stacking context, and push mode is in normal flow, so there is
nothing to manage. Use `style` in push mode if you need a `z-index`.

Edge-swipe gestures are not offered: they conflict with the
browser's back and forward gestures on mobile and would need pointer
tracking on the default path. Provide a visible `SidebarTrigger`; Escape
closes the overlay.

## Theme surface

The panel carries `data-salmon-surface="sidebar"`, so a theme with
`surfaces.sidebar` colors it: a dark sidebar beside a light page is one
theme setting, not a nested color scheme.

```tsx
const theme = { surfaces: { sidebar: '#192225' } };

<ThemeStyle theme={theme} />
<ThemeProvider theme={theme}>
  <SidebarLayout>
    <SidebarPanel navigation aria-label="Main navigation">…</SidebarPanel>
    <SidebarMain>…</SidebarMain>
  </SidebarLayout>
</ThemeProvider>
```

The theme derives the panel's foreground, muted text, hover, selected and
border colors from the seed with the theme's contrast guarantees, and
remaps the color variables inside the panel, so Salmon UI components in
it (tree view, buttons, fields) follow without any override. Custom
navigation markup reads `--salmon-surface-sidebar-fg`, `-muted`, `-hover`,
`-selected-bg`, `-selected-fg` and `-border` (see
[Surfaces](https://salmonui.dev/docs/data-grid/appearance#surfaces)). Without
`surfaces.sidebar` the attribute matches nothing.

## Localization

`localeText` overrides `SIDEBAR_LAYOUT_DEFAULT_LOCALE_TEXT`
(`{ panel: 'Sidebar', toggle: 'Toggle sidebar', collapse: 'Collapse sidebar' }`).

## API reference

[`@salmonui/navigation/sidebar-layout`](https://salmonui.dev/docs/reference/navigation/sidebar-layout):
`SidebarLayout`, `SidebarPanel`, `SidebarMain`, `SidebarTrigger`,
`SIDEBAR_LAYOUT_DEFAULT_LOCALE_TEXT` and their prop and event types.

### Live demo: Sidebar layout basics

Live demo: https://salmonui.dev/demos/navigation/sidebar-layout-basics

A push-mode SidebarLayout with a navigation panel, an icon rail toggle, a cancellable close and a container breakpoint that switches to the overlay presentation.

`Demo.tsx`:

```tsx
'use client';
import { useState } from 'react';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { SidebarLayout, SidebarMain, SidebarPanel, SidebarTrigger } from '@salmonui/navigation/sidebar-layout';

// Placeholder links target this demo's own title, which exists on both the
// docs page and the demo page, so no link dangles in the static export.
const DEMO_ANCHOR = '#demo-sidebar-layout-basics-title';

const glyph = (d: string) => (
  <svg viewBox="0 0 16 16" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true" style={{ flex: 'none' }}>
    <path d={d} />
  </svg>
);
const links = [
  { label: 'Inbox', icon: glyph('M1.75 9.5 3.5 3h9l1.75 6.5v3.5H1.75zM1.75 9.5h3.5l1 1.5h3.5l1-1.5h3.5') },
  { label: 'Drafts', icon: glyph('M10.5 2.5l3 3-7.5 7.5H3v-3zM8.5 4.5l3 3') },
  { label: 'Sent', icon: glyph('M14.25 1.75 7 9M14.25 1.75 9.75 14.25 7 9 1.75 6.25z') },
  { label: 'Archive', icon: glyph('M1.75 2.75h12.5v3H1.75zM2.75 5.75v7.5h10.5v-7.5M6.25 8.5h3.5') },
];

export default function SidebarLayoutBasicsDemo() {
  const [keepOpen, setKeepOpen] = useState(false);
  const [last, setLast] = useState('nothing yet');
  const [rail, setRail] = useState(false);
  return (
    <SidebarLayout
      collapseBelow={640}
      size="14rem"
      style={{ blockSize: 320, border: '1px solid var(--salmon-border-default)', borderRadius: 'var(--salmon-radius-lg)' }}
      onCollapsedChange={(collapsed) => setRail(collapsed)}
      onBeforeOpenChange={(event) => {
        if (keepOpen && !event.open) event.preventDefault();
      }}
      onOpenChange={(open, event) => setLast(`${open ? 'opened' : 'closed'} (${event.reason}, ${event.source})`)}
    >
      <SidebarPanel navigation aria-label="Mail folders">
        <ul style={{ display: 'grid', gap: 2, listStyle: 'none', margin: 0, padding: 8 }}>
          {links.map((link, index) => (
            <li key={link.label}>
              <a
                href={DEMO_ANCHOR}
                aria-current={index === 0 ? 'page' : undefined}
                aria-label={rail ? link.label : undefined}
                title={rail ? link.label : undefined}
                style={{
                  display: 'flex',
                  alignItems: 'center',
                  gap: 8,
                  minBlockSize: 32,
                  paddingInline: 12,
                  borderRadius: 'var(--salmon-radius-sm)',
                  whiteSpace: 'nowrap',
                  overflow: 'hidden',
                  textDecoration: 'none',
                  color: index === 0 ? 'var(--salmon-color-primary-text)' : 'var(--salmon-text-primary)',
                  background: index === 0 ? 'var(--salmon-surface-selected)' : undefined,
                }}
              >
                {link.icon}
                {/* The icon rail keeps only the icons. */}
                {rail ? null : link.label}
              </a>
            </li>
          ))}
        </ul>
      </SidebarPanel>
      <SidebarMain style={{ padding: 12 }}>
        <div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
          <SidebarTrigger />
          <SidebarTrigger action="collapse" />
          <Checkbox checked={keepOpen} onCheckedChange={(checked) => setKeepOpen(checked)}>
            Keep open
          </Checkbox>
        </div>
        <p className="demo-status">Last change: {last}</p>
      </SidebarMain>
    </SidebarLayout>
  );
}
```

### Live demo: Sidebar layout modes

Live demo: https://salmonui.dev/demos/navigation/sidebar-layout-modes

A task list SidebarLayout bounded to its own box, switching between push and overlay mode, start and end side, left-to-right and right-to-left, with closeOnOutsidePress and focus return to the trigger.

`Demo.tsx`:

```tsx
'use client';
import { useState } from 'react';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Select } from '@salmonui/dropdowns/select';
import { SidebarLayout, SidebarMain, SidebarPanel, SidebarTrigger, type SidebarPresentation } from '@salmonui/navigation/sidebar-layout';

// Placeholder links target this demo's own title (no dangling links in the static export).
const DEMO_ANCHOR = '#demo-sidebar-layout-modes-title';

const MODES = [
  { value: 'push', label: 'push' },
  { value: 'overlay', label: 'overlay' },
];
const SIDES = [
  { value: 'start', label: 'start' },
  { value: 'end', label: 'end' },
];

const CLOSE_ICON = (
  <svg viewBox="0 0 16 16" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" aria-hidden="true">
    <path d="M4 4l8 8M12 4l-8 8" />
  </svg>
);

const filters = ['All tasks', 'Assigned to me', 'Due this week', 'Completed'];

export default function SidebarLayoutModesDemo() {
  const [mode, setMode] = useState<SidebarPresentation>('push');
  const [side, setSide] = useState<'start' | 'end'>('start');
  const [rtl, setRtl] = useState(false);
  const [outsidePress, setOutsidePress] = useState(true);
  const [open, setOpen] = useState(true);
  const [last, setLast] = useState('nothing yet');
  return (
    <div dir={rtl ? 'rtl' : 'ltr'}>
      <div className="demo-toolbar">
        <label>
          Mode{' '}
          <Select
            aria-label="Mode"
            data={MODES}
            value={mode}
            onValueChange={(value) => {
              if (value == null) return;
              const next = value as SidebarPresentation;
              setMode(next);
              setOpen(next === 'push');
            }}
          />
        </label>
        <label>
          Side{' '}
          <Select aria-label="Side" data={SIDES} value={side} onValueChange={(value) => value != null && setSide(value as 'start' | 'end')} />
        </label>
        <Checkbox checked={rtl} onCheckedChange={(checked) => setRtl(checked)}>
          Right to left
        </Checkbox>
        <Checkbox checked={outsidePress} disabled={mode === 'overlay'} onCheckedChange={(checked) => setOutsidePress(checked)}>
          Close on outside press (push)
        </Checkbox>
      </div>
      {/* The layout bounds its panel to its own box: place it inside the container it belongs to. */}
      <SidebarLayout
        mode={mode}
        side={side}
        size="13rem"
        open={open}
        closeOnOutsidePress={outsidePress}
        onOpenChange={(next, event) => {
          setOpen(next);
          setLast(`${next ? 'opened' : 'closed'} (${event.reason}, ${event.source})`);
        }}
        style={{ blockSize: 300, maxInlineSize: 560, border: '1px solid var(--salmon-border-default)', borderRadius: 'var(--salmon-radius-lg)' }}
      >
        <SidebarPanel navigation aria-label="Task filters">
          <div style={{ display: 'grid', gap: 8, padding: 12 }}>
            <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 8 }}>
              <strong>Filters</strong>
              <SidebarTrigger aria-label="Close filters" icon={CLOSE_ICON} />
            </div>
            <ul style={{ display: 'grid', gap: 8, listStyle: 'none', margin: 0, padding: 0 }}>
              {filters.map((label) => (
                <li key={label} style={{ whiteSpace: 'nowrap' }}>
                  <a href={DEMO_ANCHOR}>{label}</a>
                </li>
              ))}
            </ul>
          </div>
        </SidebarPanel>
        <SidebarMain style={{ padding: 12 }}>
          <div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
            <SidebarTrigger aria-label="Filters" />
            <strong>Tasks</strong>
          </div>
          <p>Press here to test an outside press. Focus a filter link and press the close button inside the panel: focus returns to the Filters button.</p>
          <p>
            Last change: <output>{last}</output>
          </p>
        </SidebarMain>
      </SidebarLayout>
    </div>
  );
}
```
