# AppShell

> A dashboard frame with a top bar, a side navigation and the content area. The sidebar can be pinned or hidden, peeks open on hover, and becomes a drawer on a phone.

Source: https://platform.azmara.io/docs/components/shell

Group: Navigation · Status: new · Added in @azmr/ui 0.15.0

## Usage

```tsx
import { AppShell, NavGroup, NavItem, Sidebar, SidebarFooter, TopBar } from "@azmr/ui/shell";
```

```tsx
import { Button } from "@azmr/ui/button";
import { AppShell, NavGroup, NavItem, Sidebar, SidebarFooter, TopBar } from "@azmr/ui/shell";
import { Bell, Home, List, Settings, Users } from "lucide-react";

// The shell is a page frame. To show one inside a docs page it is `embedded` (the page already has a
// main landmark), given a height with --shell-vh, and put in a box with a transform so its edge
// hot zone and peek stay inside the box. `persist={false}` keeps the demo from writing the cookie.
export default function ShellBasic() {
  return (
    <div className="h-[28rem] overflow-hidden rounded-lg border border-border [transform:translateZ(0)]">
      <AppShell
        embedded
        persist={false}
        defaultMode="hidden"
        className="h-full [--shell-vh:28rem]"
        topBar={
          <TopBar
            brand={<span className="font-semibold text-sm">Acme</span>}
            end={
              <Button variant="ghost" size="icon" aria-label="Notifications">
                <Bell className="size-4" aria-hidden="true" />
              </Button>
            }
          />
        }
        sidebar={
          <Sidebar label="Demo navigation">
            <NavGroup title="Orders">
              <NavItem href="#home" icon={<Home className="size-4" />} current>
                Home
              </NavItem>
              <NavItem href="#orders" icon={<List className="size-4" />} badge={3}>
                My orders
              </NavItem>
            </NavGroup>
            <NavGroup title="Account">
              <NavItem href="#team" icon={<Users className="size-4" />}>
                Team
              </NavItem>
              <NavItem href="#settings" icon={<Settings className="size-4" />}>
                Settings
              </NavItem>
            </NavGroup>
            <SidebarFooter>
              <span className="text-xs">Signed in as Sam</span>
            </SidebarFooter>
          </Sidebar>
        }
      >
        <div className="p-6">
          <p className="font-medium text-sm">Rest the mouse on the left edge.</p>
          <p className="mt-2 max-w-prose text-muted-foreground text-sm">
            The hidden sidebar slides over this content. Click the button at the left of the top bar
            to pin it open, and click again to hide it. On a phone it opens as a drawer.
          </p>
        </div>
      </AppShell>
    </div>
  );
}
```

`AppShell` takes the top bar and the sidebar as props and the page as its children:

```tsx
<AppShell
  topBar={<TopBar brand={<Logo />} start={<SearchField />} end={<AccountMenu />} />}
  sidebar={
    <Sidebar label="Main">
      <NavGroup title="Orders">
        <NavItem href="/" icon={<Home />} current>Home</NavItem>
        <NavItem href="/orders" icon={<List />} badge={3}>My orders</NavItem>
      </NavGroup>
      <SidebarFooter>{/* optional: pinned below the list */}</SidebarFooter>
    </Sidebar>
  }
>
  {children}
</AppShell>
```

In a Client Component you can also write `AppShell.TopBar`, `AppShell.Sidebar`, `AppShell.NavGroup`, `AppShell.NavItem` and `AppShell.SidebarFooter`. In a Server Component use the named imports above: a Server Component cannot read a property of a client module.

## How the sidebar behaves

On a wide screen the sidebar is in one of two modes, and you choose between them with the button at the left of the top bar:

- **Pinned:** open beside the content, which makes room for it.
- **Hidden:** it takes no room. Rest the mouse on the left edge of the screen and it slides over the content; move away and it closes. Following a link in it closes it too. Click the top bar's button to pin it open for good, and click again to hide it.

Below the breakpoint (`lg` by default, set it with `breakpoint="md" | "lg" | "xl"`) there is no hover. The same button opens the sidebar as a drawer from the left: it traps focus, closes with Escape, the scrim or a link, and gives focus back to the button. Growing the window closes the drawer and leaves your pinned or hidden choice alone.

Only a real mouse peeks. A tap on a touch screen fires the same pointer events and does not.

## Remember the choice on the server

The mode is saved in a cookie called `azmr-sidebar`, and the server reads it so the first paint is already right, with no flash and no inline script. The reader lives in its own server-safe entry:

```tsx
// app/layout.tsx (a Server Component)
import { cookies } from "next/headers";
import { AppShell, Sidebar, TopBar } from "@azmr/ui/shell";
import { readSidebarMode } from "@azmr/ui/shell-state";

export default async function Layout({ children }: { children: React.ReactNode }) {
  const defaultMode = readSidebarMode((await cookies()).toString());
  return (
    <AppShell
      defaultMode={defaultMode}
      topBar={<TopBar brand="Acme" />}
      sidebar={<Sidebar label="Main">{/* your NavGroups */}</Sidebar>}
    >
      {children}
    </AppShell>
  );
}
```

`readSidebarMode` gives `"pinned"` when there is no cookie or it holds anything unexpected. Pass `persist={false}` to stop the shell writing the cookie.

### Marking the current page with Next.js

`NavItem` renders an `<a>` and takes `render` for your router's link. The current page comes from the URL, which a Server Component does not have, so wrap the item in a small Client Component:

```tsx
"use client";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { NavItem, type NavItemProps } from "@azmr/ui/shell";

export function NavLink({ href, ...props }: NavItemProps & { href: string }) {
  const current = usePathname() === href;
  return <NavItem {...props} current={current} render={<Link href={href} />} />;
}
```

## Reading and driving it

`useAppShell()` returns `{ mode, setMode, peeking, drawerOpen, setDrawerOpen, isDesktop }`, for apps that need to react to the sidebar or change it from elsewhere. It throws if used outside `AppShell`.

## Props

| Prop          | Type                                | Default | Required | Description                                                                                                                                                                             |
| ------------- | ----------------------------------- | ------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `topBar`      | `ReactNode`                         |         | No       | An `AppShell.TopBar`. It also holds the button that pins the sidebar or opens the drawer.                                                                                               |
| `sidebar`     | `ReactNode`                         |         | No       | An `AppShell.Sidebar`. Without one the shell is just a top bar and content.                                                                                                             |
| `children`    | `ReactNode`                         |         | Yes      |                                                                                                                                                                                         |
| `defaultMode` | `"pinned" \| "hidden" \| undefined` |         | No       | The sidebar's starting mode: `pinned` (open beside the content) or `hidden` (peeks on hover). Read it from the cookie on the server with `readSidebarMode` so the first paint is right. |
| `persist`     | `boolean \| undefined`              |         | No       | Remember changes in a cookie (`azmr-sidebar`) so the server can render them. Default true.                                                                                              |
| `breakpoint`  | `"md" \| "lg" \| "xl" \| undefined` |         | No       | Below this width the sidebar becomes a drawer. Default `lg`.                                                                                                                            |
| `labels`      | `Partial<object>`                   |         | No       | Replace the shell's built-in English text.                                                                                                                                              |
| `embedded`    | `boolean \| undefined`              |         | No       | Use inside a page that already has a `main` landmark, such as a docs demo or a panel: the content and the top bar become plain elements, so the page keeps one `main` and one banner.   |
| `className`   | `string`                            |         | No       |                                                                                                                                                                                         |

`TopBar` takes `brand`, `start` (hidden below `md`) and `end`; the navigation button is added for you.

| Prop    | Type        | Default | Required | Description                                                                  |
| ------- | ----------- | ------- | -------- | ---------------------------------------------------------------------------- |
| `brand` | `ReactNode` |         | No       | Your logo or app name, after the navigation button.                          |
| `start` | `ReactNode` |         | No       | A search field or similar. Hidden below `md`, where there is no room for it. |
| `end`   | `ReactNode` |         | No       | Status and the account menu, at the right edge.                              |

`NavItem` also takes every attribute of an `<a>`.

| Prop       | Type                                                                                   | Default | Required | Description                                                                                          |
| ---------- | -------------------------------------------------------------------------------------- | ------- | -------- | ---------------------------------------------------------------------------------------------------- |
| `icon`     | `ReactNode`                                                                            |         | No       | Shown before the label. Decorative: hidden from assistive technology.                                |
| `badge`    | `string \| number \| undefined`                                                        |         | No       | A count or short word after the label. Hidden for 0 and an empty string; numbers over 99 read `99+`. |
| `current`  | `boolean \| undefined`                                                                 |         | No       | The page being shown. Sets `aria-current="page"` and the highlight.                                  |
| `render`   | `React.ReactElement<unknown, string \| React.JSXElementConstructor<any>> \| undefined` |         | No       | Render your router's `Link` instead of an `<a>`: `render={<NextLink href="/x" />}`.                  |
| `children` | `ReactNode`                                                                            |         | Yes      |                                                                                                      |

## Accessibility

- A skip link is the first thing a keyboard reaches, and it moves focus to the content.
- The sidebar is a `nav` landmark with the name you give it (`label` is required). Each group's heading names its list. The current page has `aria-current="page"`.
- Hover is only a shortcut. A hidden sidebar is inert, so it is not in the tab order. A keyboard user reaches the navigation through the top bar's button, which pins it open.
- The button reports `aria-expanded`, and names itself "Open navigation" or "Close navigation". Replace every piece of the shell's text, including the skip link and the drawer's name, with the `labels` prop.
- Pinning and unpinning move the content at once. Only the peek slides, using `transform`, as the library's motion rules allow, so there is no animated layout. Durations come from the motion tokens, which are switched off by `prefers-reduced-motion`.

| Key                | Action                                                                                                                                                                            |
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Tab`              | Reaches the skip link first, then the top bar's navigation button, then the sidebar's links in order.                                                                             |
| `Enter` or `Space` | On the navigation button: pins or unpins the sidebar on a wide screen, and opens or closes the drawer on a narrow one.                                                            |
| `Escape`           | Closes a peeking sidebar (from anywhere, unless a menu in it is open, which takes the first Escape) or the open drawer. If focus was inside, it returns to the navigation button. |
| `Enter`            | On a link in the sidebar: follows it, which also closes a peeking sidebar or the drawer.                                                                                          |

## Size

The `@azmr/ui/shell` entry is about 44 kB compressed, most of it the modal drawer and the scroll area it is built from. `@azmr/ui/shell-state` (the cookie reader for the server) is under 1 kB. Both limits are checked by `pnpm size`.

## Things to know

- The shell is a page frame: it is as tall as the window and renders the page's `main` and banner. To place one inside a page that already has a `main` (a panel, or a demo like the one above), pass `embedded`, give it a height with `className="[--shell-vh:28rem]"`, and put it in a box with `transform: translateZ(0)` so the hidden sidebar's edge zone and peek stay inside the box. The demo's source shows all three.
- Showing or hiding an item in the sidebar is not access control. Refuse the request on the server too.
- Pass text, not HTML. The shell never renders HTML from props.
