{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "sidebar-layout",
  "title": "Sidebar layout",
  "description": "Dashboard with a sidebar: drill-in groups and route layers that slide in with a back row, a top bar with a scope switcher and breadcrumbs, a drawer on phones.",
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "@dgit/menu"
  ],
  "files": [
    {
      "path": "registry/dgit/blocks/sidebar-layout.tsx",
      "content": "import { useEffect, useState, type ReactNode } from \"react\";\nimport { ChevronLeft, ChevronRight, ChevronsUpDown, Menu as MenuIcon, Search, X } from \"lucide-react\";\nimport { Menu, MenuSeparator } from \"@/registry/dgit/ui/menu\";\nimport { cn } from \"@/lib/utils\";\n\n/**\n * A dashboard with a sidebar instead of top pills, for apps with more places than fit in a row.\n *\n * - The sidebar has a root list; a **group** in it drills in: the sidebar slides over to that\n *   group's own list, with a back row on top. Groups are nav-only (no page of their own) and\n *   open their first child.\n * - A **layer** does the same for a place in the URL (inside a project: its Overview, Secrets,\n *   Settings…). While `layer` is set the sidebar shows it; its back row goes to `layer.back.to`.\n * - The top bar has a slot on the left (a `ScopeSwitcher` for the project you're in), breadcrumbs\n *   in the middle, and a slot on the right. Pages don't need their own big title.\n * - 256px, fixed from `md` up; below that a menu button opens it as a drawer.\n *\n * Routing stays yours: pass `pathname`, and `link` to render your router's links.\n *\n *   <SidebarLayout pathname={pathname} link={(p) => <Link to={p.to} {...p} />}\n *     brand={<Logo />} nav={NAV} layer={project ? projectLayer(project) : null}\n *     top={{ left: <ScopeSwitcher … />, crumbs }} footer={<AccountMenu side=\"above\" align=\"start\" … />}>\n *     <Outlet />\n *   </SidebarLayout>\n */\n\nexport interface NavLeaf {\n  key: string;\n  label: string;\n  to: string;\n  icon?: ReactNode;\n  /** A link out of the app (docs, the home page): opens as a plain link, never active. */\n  external?: boolean;\n}\nexport interface NavGroup {\n  key: string;\n  label: string;\n  icon?: ReactNode;\n  children: NavLeaf[];\n}\n/** A small muted title between rows, or (`label` omitted) a hairline. */\nexport interface NavHeading {\n  key: string;\n  heading: true;\n  label?: string;\n}\nexport type NavItem = NavLeaf | NavGroup | NavHeading;\n\nexport interface NavLayer {\n  key: string;\n  /** Shown in the back row, e.g. the project's name. */\n  title: string;\n  back: { label: string; to: string };\n  /** Above the list, e.g. a status line. */\n  header?: ReactNode;\n  items: NavItem[];\n}\n\nexport interface Crumb {\n  label: string;\n  to?: string;\n}\n\nexport type LinkRenderer = (props: { to: string; className: string; children: ReactNode; onClick?: () => void; \"aria-current\"?: \"page\" }) => ReactNode;\n\nconst isGroup = (i: NavItem): i is NavGroup => \"children\" in i;\nconst isHeading = (i: NavItem): i is NavHeading => \"heading\" in i;\nconst leavesOf = (items: NavItem[]): NavLeaf[] => items.flatMap((i) => (isGroup(i) ? i.children : isHeading(i) ? [] : [i]));\n\n/** The leaf a path belongs to: the longest `to` that is the path or a prefix of it at a `/`, so a detail page keeps its list lit. */\nexport function activeLeaf(pathname: string, items: NavItem[]): NavLeaf | null {\n  let best: NavLeaf | null = null;\n  for (const l of leavesOf(items)) {\n    if (l.external) continue;\n    // \"/\" (a home or overview) is lit only on itself, not as the prefix of every path.\n    const hit = pathname === l.to || (l.to !== \"/\" && pathname.startsWith(l.to.endsWith(\"/\") ? l.to : `${l.to}/`));\n    if (hit && (!best || l.to.length > best.to.length)) best = l;\n  }\n  return best;\n}\n\nconst defaultLink: LinkRenderer = ({ to, className, children, onClick, ...rest }) => (\n  <a href={to} className={className} onClick={onClick} {...rest}>\n    {children}\n  </a>\n);\n\nconst rowClass = (active: boolean) =>\n  cn(\n    \"flex h-9 w-full cursor-pointer items-center gap-2.5 rounded-lg px-2.5 text-left text-sm no-underline transition-colors [&_svg]:size-4 [&_svg]:shrink-0\",\n    active ? \"bg-accent font-medium text-foreground\" : \"text-muted-foreground hover:bg-accent/60 hover:text-foreground\",\n  );\n\nfunction Rows({ items, pathname, link, onGroup, onNavigate }: { items: NavItem[]; pathname: string; link: LinkRenderer; onGroup?: (g: NavGroup) => void; onNavigate?: () => void }) {\n  const active = activeLeaf(pathname, items);\n  return (\n    <ul className=\"flex flex-col gap-0.5\">\n      {items.map((i) => {\n        if (isHeading(i))\n          return i.label ? (\n            <li key={i.key} className=\"px-2.5 pt-4 pb-1 text-xs font-medium text-muted-foreground\">\n              {i.label}\n            </li>\n          ) : (\n            <li key={i.key} role=\"separator\" className=\"my-2 border-t\" />\n          );\n        if (isGroup(i)) {\n          const on = i.children.some((c) => c.key === active?.key);\n          return (\n            <li key={i.key}>\n              <button type=\"button\" className={rowClass(on)} onClick={() => onGroup?.(i)}>\n                {i.icon}\n                <span className=\"flex-1 truncate\">{i.label}</span>\n                <ChevronRight className=\"text-muted-foreground\" />\n              </button>\n            </li>\n          );\n        }\n        const on = active?.key === i.key;\n        return (\n          <li key={i.key}>\n            {i.external ? (\n              <a href={i.to} className={rowClass(false)} target=\"_blank\" rel=\"noopener noreferrer\">\n                {i.icon}\n                <span className=\"flex-1 truncate\">{i.label}</span>\n              </a>\n            ) : (\n              link({\n                to: i.to,\n                className: rowClass(on),\n                onClick: onNavigate,\n                ...(on ? { \"aria-current\": \"page\" as const } : {}),\n                children: (\n                  <>\n                    {i.icon}\n                    <span className=\"flex-1 truncate\">{i.label}</span>\n                  </>\n                ),\n              })\n            )}\n          </li>\n        );\n      })}\n    </ul>\n  );\n}\n\n/** One sliding layer; hidden ones are out of the tab order (inert). */\nfunction Pane({ hidden, from, children }: { hidden: boolean; from: \"left\" | \"right\"; children: ReactNode }) {\n  return (\n    <div\n      inert={hidden}\n      aria-hidden={hidden}\n      className={cn(\n        \"absolute inset-0 overflow-y-auto px-3 pb-4 transition-[translate,opacity] duration-180 ease-out motion-reduce:transition-none\",\n        hidden && (from === \"left\" ? \"-translate-x-full opacity-0\" : \"translate-x-full opacity-0\"),\n      )}\n    >\n      {children}\n    </div>\n  );\n}\n\nfunction BackRow({ label, title, onClick, to, link }: { label: string; title: string; onClick?: () => void; to?: string; link: LinkRenderer }) {\n  const inner = (\n    <>\n      <ChevronLeft />\n      <span className=\"sr-only\">{label}: </span>\n      <span className=\"flex-1 truncate font-medium text-foreground\">{title}</span>\n    </>\n  );\n  const cls = cn(rowClass(false), \"mb-2\");\n  return to ? link({ to, className: cls, children: inner }) : (\n    <button type=\"button\" className={cls} onClick={onClick} aria-label={`${label}: ${title}`}>\n      {inner}\n    </button>\n  );\n}\n\nexport function SidebarLayout({\n  pathname,\n  brand,\n  nav,\n  layer = null,\n  top = {},\n  footer,\n  link = defaultLink,\n  children,\n}: {\n  pathname: string;\n  brand: ReactNode;\n  nav: NavItem[];\n  layer?: NavLayer | null;\n  top?: { left?: ReactNode; crumbs?: Crumb[]; right?: ReactNode };\n  /** The bottom of the sidebar, usually <AccountMenu side=\"above\" align=\"start\" />. */\n  footer?: ReactNode;\n  link?: LinkRenderer;\n  children: ReactNode;\n}) {\n  // The group whose list is open: the route's own, until the user steps in or out by hand.\n  const routeGroup = nav.filter(isGroup).find((g) => g.children.some((c) => c.key === activeLeaf(pathname, nav)?.key))?.key ?? null;\n  const [openGroup, setOpenGroup] = useState<string | null>(routeGroup);\n  const [synced, setSynced] = useState(routeGroup);\n  if (synced !== routeGroup) {\n    setSynced(routeGroup);\n    setOpenGroup(routeGroup);\n  }\n  const [drawer, setDrawer] = useState(false);\n  useEffect(() => setDrawer(false), [pathname]);\n  const close = () => setDrawer(false);\n\n  const groups = nav.filter(isGroup);\n  const inGroup = !layer && openGroup ? groups.find((g) => g.key === openGroup) : undefined;\n  const title = [...(top.crumbs ?? [])].map((c) => c.label).at(-1);\n\n  const sidebar = (\n    <nav aria-label=\"Main\" className=\"flex h-full flex-col\">\n      <div className=\"flex h-14 shrink-0 items-center justify-between px-5\">\n        {brand}\n        <button type=\"button\" className=\"rounded-full p-1.5 text-muted-foreground hover:text-foreground md:hidden\" aria-label=\"Close menu\" onClick={close}>\n          <X className=\"size-4\" />\n        </button>\n      </div>\n      <div className=\"relative flex-1 overflow-hidden\">\n        <Pane hidden={Boolean(layer || inGroup)} from=\"left\">\n          <Rows items={nav} pathname={pathname} link={link} onNavigate={close} onGroup={(g) => setOpenGroup(g.key)} />\n        </Pane>\n        {groups.map((g) => (\n          <Pane key={g.key} hidden={inGroup?.key !== g.key} from=\"right\">\n            <BackRow label=\"Back\" title={g.label} onClick={() => setOpenGroup(null)} link={link} />\n            <Rows items={g.children} pathname={pathname} link={link} onNavigate={close} />\n          </Pane>\n        ))}\n        {layer && (\n          <Pane key={layer.key} hidden={false} from=\"right\">\n            <BackRow label={layer.back.label} title={layer.title} to={layer.back.to} link={link} />\n            {layer.header}\n            <Rows items={layer.items} pathname={pathname} link={link} onNavigate={close} />\n          </Pane>\n        )}\n      </div>\n      {footer && <div className=\"shrink-0 border-t p-3\">{footer}</div>}\n    </nav>\n  );\n\n  return (\n    <div className=\"min-h-dvh bg-background\">\n      {/* Sidebar: fixed from md up, a drawer below. */}\n      <div\n        className={cn(\n          \"fixed inset-y-0 left-0 z-40 w-64 border-r bg-background transition-transform duration-200 ease-out motion-reduce:transition-none md:translate-x-0\",\n          drawer ? \"translate-x-0 shadow-card\" : \"-translate-x-full\",\n        )}\n      >\n        {sidebar}\n      </div>\n      {drawer && <div className=\"fixed inset-0 z-30 bg-black/30 md:hidden\" onClick={close} aria-hidden />}\n      <div className=\"md:pl-64\">\n        <header className=\"sticky top-0 z-20 grid h-14 grid-cols-[auto_1fr_auto] items-center gap-3 border-b bg-background/90 px-4 backdrop-blur md:grid-cols-[1fr_2fr_1fr] md:px-6\">\n          <div className=\"flex min-w-0 items-center gap-2\">\n            <button type=\"button\" className=\"-ml-1 rounded-full p-1.5 text-muted-foreground hover:text-foreground md:hidden\" aria-label=\"Open menu\" onClick={() => setDrawer(true)}>\n              <MenuIcon className=\"size-4\" />\n            </button>\n            {top.left}\n          </div>\n          <Crumbs crumbs={top.crumbs ?? []} link={link} />\n          <div className=\"flex items-center justify-end gap-2\">{top.right}</div>\n        </header>\n        {title && <title>{title}</title>}\n        <main>{children}</main>\n      </div>\n    </div>\n  );\n}\n\n/** \"Projects / acme/api / Secrets\": parents muted and linked, the page itself in the text color. */\nfunction Crumbs({ crumbs, link }: { crumbs: Crumb[]; link: LinkRenderer }) {\n  if (!crumbs.length) return <div />;\n  return (\n    <ol className=\"hidden min-w-0 items-center justify-center gap-1.5 text-sm md:flex\" aria-label=\"Breadcrumb\">\n      {crumbs.map((c, i) => {\n        const last = i === crumbs.length - 1;\n        return (\n          <li key={i} className=\"flex min-w-0 items-center gap-1.5\">\n            {i > 0 && (\n              <svg viewBox=\"0 0 16 16\" className=\"size-4 shrink-0 text-border\" aria-hidden=\"true\">\n                <path d=\"M10.5 2.5 5.5 13.5\" stroke=\"currentColor\" strokeWidth=\"1.2\" strokeLinecap=\"round\" fill=\"none\" />\n              </svg>\n            )}\n            {last || !c.to ? (\n              <span className={cn(\"truncate\", last ? \"font-medium text-foreground\" : \"text-muted-foreground\")} aria-current={last ? \"page\" : undefined}>\n                {c.label}\n              </span>\n            ) : (\n              link({ to: c.to, className: \"truncate text-muted-foreground no-underline hover:text-foreground\", children: c.label })\n            )}\n          </li>\n        );\n      })}\n    </ol>\n  );\n}\n\n/**\n * The place you're in, and a menu to switch it: \"acme/api ⌄\" → other projects, \"All projects\",\n * and extra rows (Add project…). Searchable once there are more than seven.\n */\nexport function ScopeSwitcher({\n  current,\n  items,\n  all,\n  extra,\n  link = defaultLink,\n  label = \"Switch\",\n  icon,\n}: {\n  /** What's shown; null for \"all\" (then `all.label`). */\n  current: { key: string; label: string } | null;\n  items: { key: string; label: string; to: string; hint?: string }[];\n  all?: { label: string; to: string };\n  /** Rows under the list (\"Create project\"); a function gets `close` to shut the menu first. */\n  extra?: ReactNode | ((close: () => void) => ReactNode);\n  link?: LinkRenderer;\n  label?: string;\n  icon?: ReactNode;\n}) {\n  const [q, setQ] = useState(\"\");\n  const shown = items.filter((i) => i.label.toLowerCase().includes(q.trim().toLowerCase()));\n  const rowLink = (to: string, children: ReactNode, close: () => void, on: boolean) =>\n    link({ to, onClick: close, className: cn(\"flex h-9 items-center gap-2 rounded-md px-2 text-sm no-underline hover:bg-accent\", on ? \"text-foreground\" : \"text-muted-foreground hover:text-foreground\"), children });\n  return (\n    <Menu\n      label={label}\n      className=\"w-72\"\n      trigger={(p) => (\n        <button\n          type=\"button\"\n          onClick={p.toggle}\n          aria-expanded={p.open}\n          aria-haspopup=\"menu\"\n          className=\"flex h-8 max-w-full min-w-0 cursor-pointer items-center gap-2 rounded-full px-2.5 text-sm font-medium hover:bg-accent [&_svg]:size-4 [&_svg]:shrink-0\"\n        >\n          {icon}\n          <span className=\"truncate\">{current?.label ?? all?.label ?? label}</span>\n          <ChevronsUpDown className=\"text-muted-foreground\" />\n        </button>\n      )}\n    >\n      {({ close }) => (\n        <>\n          {items.length > 7 && (\n            <div className=\"mb-1 flex items-center gap-2 border-b px-2 pb-1.5\">\n              <Search className=\"size-4 text-muted-foreground\" />\n              <input autoFocus value={q} onChange={(e) => setQ(e.target.value)} placeholder=\"Find…\" className=\"h-8 w-full bg-transparent text-sm outline-none\" aria-label=\"Find\" />\n            </div>\n          )}\n          {all && rowLink(all.to, all.label, close, !current)}\n          {all && <MenuSeparator />}\n          <div className=\"max-h-72 overflow-y-auto\">\n            {shown.map((i) =>\n              rowLink(\n                i.to,\n                <>\n                  <span className=\"flex-1 truncate\">{i.label}</span>\n                  {i.hint && <span className=\"text-xs text-muted-foreground\">{i.hint}</span>}\n                </>,\n                close,\n                current?.key === i.key,\n              ),\n            )}\n            {!shown.length && <p className=\"px-2 py-2 text-sm text-muted-foreground\">Nothing matches</p>}\n          </div>\n          {extra && (\n            <>\n              <MenuSeparator />\n              {typeof extra === \"function\" ? extra(close) : extra}\n            </>\n          )}\n        </>\n      )}\n    </Menu>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:block"
}