# App Shell

Sidebar application frame with a data-driven navigation, sticky header, and content region.

Page: https://sui.draco.dev/docs/layouts/app-shell

Adapted from the [shadcn-admin](https://github.com/satnaing/shadcn-admin) layout. `AppShell` composes SUI Sidebar into a full admin frame: it provides layout preferences and a skip link around an `AppSidebar` and an `AppInset`, the page's main landmark. The layouts do not depend on a router. Pass the current location and your router's Link once on `AppShell`, and every navigation inside reads them.

## Sidebar frame

Switch the sidebar style and collapse behavior. With `collapsible="icon"`, collapse the sidebar: sections with nested links open a menu beside it. Scroll the content to see the fixed header gain a shadow.

### Example: layout-app-shell

```tsx
"use client";

import { Avatar, AvatarFallback } from "@workspace/ui/components/avatar";
import {
  Breadcrumb,
  BreadcrumbItem,
  BreadcrumbList,
  BreadcrumbPage,
} from "@workspace/ui/components/breadcrumb";
import { Button } from "@workspace/ui/components/button";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectList,
  SelectTrigger,
  SelectValue,
} from "@workspace/ui/components/select";
import {
  SidebarMenu,
  SidebarMenuButton,
  SidebarMenuItem,
} from "@workspace/ui/components/sidebar";
import { AppHeader } from "@workspace/ui/layouts/app-header";
import { AppMain } from "@workspace/ui/layouts/app-main";
import { AppInset, AppShell } from "@workspace/ui/layouts/app-shell";
import { AppSidebar } from "@workspace/ui/layouts/app-sidebar";
import type {
  LayoutCollapsible,
  LayoutVariant,
  NavGroupData,
} from "@workspace/ui/layouts/types";
import {
  Bell,
  Command,
  LayoutDashboard,
  ListTodo,
  MessagesSquare,
  Settings,
  Users,
} from "lucide-react";
import { useState } from "react";
import { useDemoRouter } from "../support/layout-router";
import type { ExampleProps } from "../types";

export default function LayoutAppShell({ locale }: ExampleProps) {
  const zh = locale === "zh-CN";
  const router = useDemoRouter("/");
  const [variant, setVariant] = useState<LayoutVariant>("inset");
  const [collapsible, setCollapsible] = useState<LayoutCollapsible>("icon");
  const navGroups: NavGroupData[] = [
    {
      title: zh ? "常用" : "General",
      items: [
        {
          title: zh ? "仪表盘" : "Dashboard",
          href: "/",
          icon: LayoutDashboard,
        },
        { title: zh ? "任务" : "Tasks", href: "/tasks", icon: ListTodo },
        {
          title: zh ? "消息" : "Chats",
          href: "/chats",
          icon: MessagesSquare,
          badge: 3,
        },
        { title: zh ? "用户" : "Users", href: "/users", icon: Users },
      ],
    },
    {
      title: zh ? "其他" : "Other",
      items: [
        {
          title: zh ? "设置" : "Settings",
          icon: Settings,
          items: [
            { title: zh ? "个人资料" : "Profile", href: "/settings" },
            { title: zh ? "账户" : "Account", href: "/settings/account" },
            {
              title: zh ? "通知" : "Notifications",
              href: "/settings/notifications",
              icon: Bell,
            },
          ],
        },
      ],
    },
  ];
  const title = navGroups
    .flatMap((group) => group.items.flatMap((item) => item.items ?? [item]))
    .find((item) => item.href === router.currentPath)?.title;

  return (
    <AppShell
      {...router}
      variant={variant}
      collapsible={collapsible}
      className="relative isolate h-[32rem] min-h-0 overflow-hidden rounded-xl border"
    >
      <AppSidebar
        className="absolute! inset-y-0! h-full!"
        navGroups={navGroups}
        header={
          <SidebarMenu>
            <SidebarMenuItem>
              <SidebarMenuButton size="lg">
                <div className="flex size-8 shrink-0 items-center justify-center rounded-lg bg-sidebar-primary text-sidebar-primary-foreground">
                  <Command className="size-4" />
                </div>
                <div className="grid flex-1 text-start text-sm leading-tight">
                  <span className="truncate font-semibold">Acme Inc</span>
                  <span className="truncate text-xs">Enterprise</span>
                </div>
              </SidebarMenuButton>
            </SidebarMenuItem>
          </SidebarMenu>
        }
        footer={
          <SidebarMenu>
            <SidebarMenuItem>
              <SidebarMenuButton size="lg">
                <Avatar className="size-8 shrink-0 rounded-lg">
                  <AvatarFallback className="rounded-lg">SN</AvatarFallback>
                </Avatar>
                <div className="grid flex-1 text-start text-sm leading-tight">
                  <span className="truncate font-semibold">satnaing</span>
                  <span className="truncate text-xs">m@example.com</span>
                </div>
              </SidebarMenuButton>
            </SidebarMenuItem>
          </SidebarMenu>
        }
      />
      <AppInset>
        <AppHeader
          fixed
          breadcrumb={
            <Breadcrumb>
              <BreadcrumbList>
                <BreadcrumbItem>
                  <BreadcrumbPage>{title}</BreadcrumbPage>
                </BreadcrumbItem>
              </BreadcrumbList>
            </Breadcrumb>
          }
        >
          <Button variant="outline" size="sm" className="ms-auto">
            {zh ? "新建" : "New"}
          </Button>
        </AppHeader>
        <AppMain className="space-y-4">
          <div className="flex flex-wrap gap-3">
            <Select
              value={variant}
              onValueChange={(value) =>
                value && setVariant(value as LayoutVariant)
              }
            >
              <SelectTrigger
                size="sm"
                className="w-fit"
                aria-label={zh ? "侧栏样式" : "Sidebar variant"}
              >
                <SelectValue />
              </SelectTrigger>
              <SelectContent>
                <SelectList>
                  <SelectItem value="inset">inset</SelectItem>
                  <SelectItem value="sidebar">sidebar</SelectItem>
                  <SelectItem value="floating">floating</SelectItem>
                </SelectList>
              </SelectContent>
            </Select>
            <Select
              value={collapsible}
              onValueChange={(value) =>
                value && setCollapsible(value as LayoutCollapsible)
              }
            >
              <SelectTrigger
                size="sm"
                className="w-fit"
                aria-label={zh ? "折叠方式" : "Collapse behavior"}
              >
                <SelectValue />
              </SelectTrigger>
              <SelectContent>
                <SelectList>
                  <SelectItem value="icon">icon</SelectItem>
                  <SelectItem value="offcanvas">offcanvas</SelectItem>
                  <SelectItem value="none">none</SelectItem>
                </SelectList>
              </SelectContent>
            </Select>
          </div>
          <div className="grid gap-4 sm:grid-cols-3">
            {[1, 2, 3].map((index) => (
              <div key={index} className="h-28 rounded-2xl bg-muted/60" />
            ))}
          </div>
          <div className="h-[28rem] rounded-2xl bg-muted/40" />
        </AppMain>
      </AppInset>
    </AppShell>
  );
}
```

## Top navigation

`TopNav` renders horizontal links from the `lg` breakpoint and a menu button below it. Set `sidebarTrigger={false}` on `AppHeader` when the page has no sidebar.

### Example: layout-top-nav

```tsx
"use client";

import { AppHeader } from "@workspace/ui/layouts/app-header";
import { AppMain } from "@workspace/ui/layouts/app-main";
import { LayoutProvider } from "@workspace/ui/layouts/layout-provider";
import { TopNav } from "@workspace/ui/layouts/top-nav";
import { useDemoRouter } from "../support/layout-router";
import type { ExampleProps } from "../types";

export default function LayoutTopNav({ locale }: ExampleProps) {
  const zh = locale === "zh-CN";
  const router = useDemoRouter("/overview");
  const links = [
    { title: zh ? "概览" : "Overview", href: "/overview" },
    { title: zh ? "客户" : "Customers", href: "/customers" },
    { title: zh ? "产品" : "Products", href: "/products" },
    { title: zh ? "设置" : "Settings", href: "/settings", disabled: true },
  ];
  return (
    <LayoutProvider {...router}>
      <div className="h-80 w-full overflow-auto rounded-xl border">
        <AppHeader fixed sidebarTrigger={false} className="border-b">
          <TopNav
            links={links}
            labels={zh ? { navigation: "主导航", menu: "打开导航" } : undefined}
          />
        </AppHeader>
        <AppMain>
          <p className="text-muted-foreground text-sm">
            {zh ? "当前路径：" : "Current path: "}
            <code>{router.currentPath}</code>
          </p>
          <div className="mt-4 h-96 rounded-2xl bg-muted/40" />
        </AppMain>
      </div>
    </LayoutProvider>
  );
}
```

## Installation

```bash
npx shadcn@latest add @sui/app-shell @sui/app-sidebar @sui/app-header @sui/app-main
```

With pnpm, Yarn or Bun, use `pnpm dlx`, `yarn dlx` or `bunx --bun` in place of `npx`, and `pnpm add`, `yarn add` or `bun add` in place of `npm install`.

Add `@sui/top-nav` for horizontal navigation. Follow the [installation guide](/docs/installation) for registry, dependency, and style setup.

## Usage

```tsx
import { Link, useLocation, useNavigate } from "@tanstack/react-router"
import { AppHeader } from "@workspace/ui/layouts/app-header"
import { AppMain } from "@workspace/ui/layouts/app-main"
import { AppInset, AppShell } from "@workspace/ui/layouts/app-shell"
import { AppSidebar } from "@workspace/ui/layouts/app-sidebar"
import type { LayoutLinkProps } from "@workspace/ui/layouts/types"

function RouterLink({ href, ...props }: LayoutLinkProps) {
  return <Link to={href} {...props} />
}

export function AuthenticatedLayout({ children }: { children: React.ReactNode }) {
  const pathname = useLocation({ select: (location) => location.pathname })
  const navigate = useNavigate()
  return (
    <AppShell
      currentPath={pathname}
      linkComponent={RouterLink}
      onNavigate={(href) => navigate({ to: href })}
    >
      <AppSidebar header={<TeamSwitcher />} navGroups={navGroups} footer={<NavUser />} />
      <AppInset>
        <AppHeader fixed breadcrumb={<PageBreadcrumb />}>
          <Search className="ms-auto" />
        </AppHeader>
        <AppMain>{children}</AppMain>
      </AppInset>
    </AppShell>
  )
}
```

`navGroups` is a list of `{ title?, items }`. An item is a link `{ title, href, icon?, badge?, disabled? }` or a section `{ title, icon?, items: links }`. A section opens by default when one of its links is active.

Persist preferences yourself: pass `defaultVariant`, `defaultCollapsible`, and `defaultOpen` from a cookie or storage, and save them in `onVariantChange`, `onCollapsibleChange`, and `onOpenChange`. Reading them on the server avoids a flash of the wrong layout. Read and change preferences anywhere inside with `useLayout()`.

## API

### AppShell

Accepts every `SidebarProvider` prop (`defaultOpen`, `open`, `onOpenChange`, `className`) and every `LayoutProvider` prop.

| Prop | Default | Description |
| --- | --- | --- |
| `variant`, `defaultVariant`, `onVariantChange` | `"inset"` | Sidebar style: `sidebar`, `floating`, or `inset`. |
| `collapsible`, `defaultCollapsible`, `onCollapsibleChange` | `"icon"` | Collapse behavior: `offcanvas`, `icon`, or `none`. |
| `currentPath` | `""` | Current location for active links. |
| `linkComponent` | `<a>` | Router link receiving `href`, `className`, `aria-current`, and `onClick`. |
| `onNavigate` | `location.assign` | Navigation for controls that are not links. |
| `matcher` | Exact match | `(href, currentPath) => boolean`. The default compares paths exactly, ignoring the query and hash. |
| `skipLabel` | `"Skip to main content"` | Text of the skip link. |

### AppInset

Content region beside the sidebar, the page's main landmark and the skip link target. Accepts every `SidebarInset` prop. A `fixed` AppMain inside pins it to the viewport height.

### AppSidebar

Accepts `Sidebar` props. `variant` and `collapsible` default to the AppShell preferences.

| Prop | Default | Description |
| --- | --- | --- |
| `header`, `footer` | — | Brand or team switcher; user menu. |
| `navGroups` | `[]` | Navigation data rendered with `NavGroup`. |
| `rail` | `true` | Edge rail that toggles the sidebar. |

`NavGroup` renders one group and can be used on its own inside `SidebarContent`.

### AppHeader

| Prop | Default | Description |
| --- | --- | --- |
| `fixed` | `false` | Sticks to the top of its scroll container and gains a shadow and blur once content scrolls under it. |
| `sidebarTrigger` | `true` | Shows the sidebar toggle. Requires an AppShell. |
| `breadcrumb` | — | Placed after the toggle. |

### AppMain

| Prop | Default | Description |
| --- | --- | --- |
| `fixed` | `false` | Fills the remaining viewport height and scrolls inside, for boards and tables. |
| `fluid` | `false` | Spans the full width instead of centering at `max-w-7xl`. |

### TopNav

| Prop | Default | Description |
| --- | --- | --- |
| `links` | Required | Links `{ title, href, icon?, disabled? }`. |
| `labels` | English | `navigation` (landmark name) and `menu` (menu button). |
