# AppLayout and SidebarToggle

The app shell: a top bar, a sidebar and the main area. `AppLayoutProvider` holds the sidebar’s open state; `SidebarToggle` and `useAppLayout` read and change it. This site is built from AppLayout.

```tsx
import {
  AppLayoutContent,
  AppLayoutMain,
  AppLayoutProvider,
  AppLayoutRoot,
  AppLayoutSidebar,
  AppLayoutTopBar,
  SidebarToggle,
} from '@blocks/design-system'
```

These are client components. A root layout that is a Server Component imports the parts by name; client-authored code can also use the dot object, `AppLayout.Root` and friends.

## Composing a shell

`AppLayoutRoot` fills the viewport, `AppLayoutTopBar` runs across the top, and `AppLayoutContent` holds `AppLayoutSidebar` (an `<aside>`) beside `AppLayoutMain` (the `<main>` landmark). Put your navigation, such as a NavList, in the sidebar.

`examples/app-layout/app-shell.tsx`:

```tsx
// app/layout.tsx in a Next app: a Server Component composing the named parts.
import {
  AppLayoutContent,
  AppLayoutMain,
  AppLayoutProvider,
  AppLayoutRoot,
  AppLayoutSidebar,
  AppLayoutTopBar,
  Link,
  NavList,
  NavListItem,
  SidebarToggle,
} from '@blocks/design-system'
import type { ReactNode } from 'react'

export default function BoardLayout({ children }: { children: ReactNode }) {
  return (
    <AppLayoutProvider>
      <AppLayoutRoot>
        <AppLayoutTopBar>
          <SidebarToggle />
          <Link href="/">Board</Link>
        </AppLayoutTopBar>
        <AppLayoutContent>
          <AppLayoutSidebar>
            <NavList aria-label="Boards">
              <NavListItem href="/boards/launch">Launch</NavListItem>
              <NavListItem href="/boards/roadmap">Roadmap</NavListItem>
            </NavList>
          </AppLayoutSidebar>
          <AppLayoutMain id="main">{children}</AppLayoutMain>
        </AppLayoutContent>
      </AppLayoutRoot>
    </AppLayoutProvider>
  )
}
```

`examples/app-layout/client-shell.tsx`:

```tsx
'use client'

// The same shell from client-authored code, with the dot API.
import { AppLayout, SidebarToggle } from '@blocks/design-system'
import type { ReactNode } from 'react'

export function ClientShell({ nav, children }: { nav: ReactNode; children: ReactNode }) {
  return (
    <AppLayout.Provider defaultSidebarOpen={false}>
      <AppLayout.Root>
        <AppLayout.TopBar>
          <SidebarToggle />
        </AppLayout.TopBar>
        <AppLayout.Content>
          <AppLayout.Sidebar>{nav}</AppLayout.Sidebar>
          <AppLayout.Main>{children}</AppLayout.Main>
        </AppLayout.Content>
      </AppLayout.Root>
    </AppLayout.Provider>
  )
}
```

## Wide and narrow screens

From the `md` breakpoint (48rem) the sidebar sits beside the main area and starts open (`defaultSidebarOpen`). Below it, the sidebar is a drawer over the page that starts closed. Opening the drawer moves focus into it; Escape closes it and returns focus to the toggle. A closed sidebar is hidden and inert, so its links are not in the Tab order. Crossing the breakpoint resets the state.

After following a link in the drawer, close it and move focus to the page, as this site does: `setSidebarOpen(false)` from `useAppLayout`, then focus `main`.

## Reading the state

`useAppLayout()` returns `isSidebarOpen`, `isDrawer`, `toggleSidebar` and `setSidebarOpen`. This example runs inside this site’s own provider.

**Example: This site’s sidebar** (`examples/app-layout/sidebar-state.tsx`)

```tsx
'use client'

import { Body, Button, Stack, useAppLayout } from '@blocks/design-system'

// This page sits inside the docs' own AppLayout, so this reads and drives it.
export default function SidebarState() {
  const { isSidebarOpen, isDrawer, setSidebarOpen } = useAppLayout()
  return (
    <Stack gap="sm" align="start">
      <Body margin="none" role="status" aria-label="Docs sidebar state">
        The docs sidebar is {isSidebarOpen ? 'open' : 'closed'}
        {isDrawer ? ', as a drawer on this narrow screen' : ''}.
      </Body>
      <Button type="button" onClick={() => setSidebarOpen(!isSidebarOpen)}>
        {isSidebarOpen ? 'Close' : 'Open'} the docs sidebar
      </Button>
    </Stack>
  )
}
```

## SidebarToggle

A button with `aria-expanded` and `aria-controls` pointing at the sidebar. Its name is “Open sidebar” or “Close sidebar”; pass `aria-label` to name it something else, as this site’s “Menu” does. `position="fixed"` pins it beside the sidebar’s edge.

## Props

**`AppLayoutProvider` props**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `defaultSidebarOpen` | `boolean` | `true` | Whether the sidebar starts open on wide screens. Defaults to `true`. On narrow screens it starts closed. |

`AppLayoutRoot` has no props of its own.

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

`AppLayoutTopBar` has no props of its own.

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

`AppLayoutContent` has no props of its own.

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

`AppLayoutSidebar` has no props of its own.

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

`AppLayoutMain` has no props of its own.

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

**`SidebarToggle` props**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `position` | `"relative" \| "fixed"` | `relative` | `"fixed"` pins it beside the sidebar's edge. Defaults to `"relative"`. |

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

## Accessibility

- Keep one `main` landmark, and give `AppLayoutMain` an `id` your skip link targets.
- Name the navigation inside the sidebar; the `<aside>` itself is a complementary landmark.
- The root scrolls inside its regions by default. To let the page scroll as a whole, as this site does, override the root, content and main heights through `dangerouslySetClassName`.
