{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "app-shell",
  "title": "App shell",
  "description": "Header frame (navigation toggle, breadcrumb, actions), a sidebar, the page and an aside, one gutter apart; the sidebar is a full-height column that can collapse to a rail; below a width of its own the sidebar and aside become sheets.",
  "dependencies": [
    "@base-ui/react@1.9.0"
  ],
  "registryDependencies": [
    "@keyline-ui/keyline",
    "utils",
    "@keyline-ui/keyline-motion",
    "@keyline-ui/keyline-render",
    "@keyline-ui/keyline-scope",
    "@keyline-ui/nav",
    "@keyline-ui/sheet"
  ],
  "files": [
    {
      "path": "src/registry/keyline/ui/app-shell.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport type { Dialog as DialogPrimitive } from \"@base-ui/react/dialog\";\nimport { mergeProps } from \"@base-ui/react/merge-props\";\nimport { useRender } from \"@base-ui/react/use-render\";\n\nimport { cn } from \"@/lib/utils\";\nimport { resolveClassName, resolveStyle, type PartProps } from \"../lib/keyline-render\";\nimport { tokenMs } from \"../lib/keyline-motion\";\nimport { useMergedRef } from \"../lib/keyline-scope\";\nimport { NavExpandContext, NavRailContext } from \"./nav\";\nimport { Sheet, SheetPopup } from \"./sheet\";\n\n/* ------------------------------------------------------------------ *\n * STATE\n * The root measures its own width and owns the sidebar's state; the parts read it from context. The width decides\n * three layouts: wide (sidebar, page and aside side by side), medium (the aside is a sheet), narrow (both are sheets).\n * ------------------------------------------------------------------ */\n\ntype ShellBand = \"narrow\" | \"medium\" | \"wide\";\ntype OpenChangeDetails = DialogPrimitive.Root.ChangeEventDetails;\n\ntype ShellContextValue = {\n  band: ShellBand;\n  navId: string;\n  navLabel: string;\n  collapse: \"hide\" | \"rail\";\n  navOpen: boolean;\n  setNavOpen: (open: boolean) => void;\n  navSheet: boolean;\n  setNavSheet: (open: boolean) => void;\n  rail: boolean;\n  stickyHeader: boolean;\n  headRef: React.RefObject<HTMLElement | null>;\n  asideOpen: boolean;\n  onAsideOpenChange?: (open: boolean, eventDetails: OpenChangeDetails) => void;\n  setHasAside: React.Dispatch<React.SetStateAction<number>>;\n  container?: React.ComponentProps<typeof SheetPopup>[\"container\"];\n};\n\nconst ShellContext = React.createContext<ShellContextValue | null>(null);\n\nfunction useShell(part: string): ShellContextValue {\n  const ctx = React.useContext(ShellContext);\n  if (!ctx) throw new Error(`Keyline: <${part}> must be placed inside <AppShellRoot>.`);\n  return ctx;\n}\n\n/** The breakpoints the root's own utilities carry (data-kl-at); any others bring their own rules (see shellRules). */\nconst DEFAULT_AT = \"960-1400\";\n\n/**\n * The layout before any script for breakpoints other than the default: a container condition cannot read a custom\n * property, so these rules are written per shell. In @layer utilities, with an attribute more than the columns' own\n * utilities, so they win as the default breakpoints' container variants do.\n */\nconst shellRules = (at: string, narrowAt: number, inlineAt: number) => {\n  const s = `[data-slot=\"app-shell\"][data-kl-at=\"${at}\"]`;\n  return `@layer utilities{@container kl-shell (width < ${narrowAt}px){${s}>:is([data-slot=\"app-shell-nav\"],[data-slot=\"app-shell-aside\"]){display:none}${s}>[data-slot=\"app-shell-main\"]{grid-column:1/-1}}@container kl-shell (${narrowAt}px <= width < ${inlineAt}px){${s}>[data-slot=\"app-shell-aside\"]{display:none}${s}>[data-slot=\"app-shell-main\"]{grid-column-end:-1}}}`;\n};\n\n/* ------------------------------------------------------------------ *\n * CLASSES\n * The rules over the shell's children have no specificity (:where), so a child's own utilities win, as they did\n * over the shell's stylesheet. --kl-shell-top, --kl-shell-nav and --kl-shell-aside are read with their defaults as\n * fallbacks, never set here, so a page's own value for them wins wherever it sets it.\n * ------------------------------------------------------------------ */\n\n/*\n * APP SHELL: the header frame across the top; sidebar, page and aside one gutter apart (C1). A column that holds\n * nothing takes no track and no gutter, so the page's end edge lies on the header's. The narrow layout (an attribute\n * more) wins over all three. The shell is its own size container: its columns answer its width in CSS, so the\n * server's HTML is laid out for the shell it lands in.\n */\nconst root = cn(\n  \"@container/kl-shell relative grid min-w-0 items-start gap-(--kl-gutter) [:where(&>*)]:min-w-0\",\n  \"grid-cols-[auto_minmax(0,1fr)_auto] [grid-template-areas:'head_head_head'_'nav_main_aside']\",\n  \"[&:where(:has(>[data-slot=app-shell-nav]:not([hidden])):not(:has(>[data-slot=app-shell-aside])))]:grid-cols-[auto_minmax(0,1fr)]\",\n  \"[&:where(:has(>[data-slot=app-shell-nav]:not([hidden])):not(:has(>[data-slot=app-shell-aside])))]:[grid-template-areas:'head_head'_'nav_main']\",\n  \"[&:where(:has(>[data-slot=app-shell-aside]):not(:has(>[data-slot=app-shell-nav]:not([hidden]))))]:grid-cols-[minmax(0,1fr)_auto]\",\n  \"[&:where(:has(>[data-slot=app-shell-aside]):not(:has(>[data-slot=app-shell-nav]:not([hidden]))))]:[grid-template-areas:'head_head'_'main_aside']\",\n  \"[&:where(:not(:has(>[data-slot=app-shell-nav]:not([hidden]))):not(:has(>[data-slot=app-shell-aside])))]:grid-cols-[minmax(0,1fr)]\",\n  \"[&:where(:not(:has(>[data-slot=app-shell-nav]:not([hidden]))):not(:has(>[data-slot=app-shell-aside])))]:[grid-template-areas:'head'_'main']\",\n  \"data-[kl-shell=narrow]:grid-cols-[minmax(0,1fr)] data-[kl-shell=narrow]:[grid-template-areas:'head'_'main']\",\n  // with a sticky header, where the columns hold while the page scrolls: below the header and one gutter\n  \"data-kl-sticky-head:[--kl-shell-cols-top:calc(var(--kl-shell-top,var(--kl-gutter))+var(--kl-shell-head-h,0px)+var(--kl-gutter))]\",\n);\n\n/** the width sentinel: spans the shell, no height, takes no grid cell */\nconst sentinel = \"pointer-events-none invisible absolute inset-x-0 top-0 h-0\";\n\n/*\n * The header: one region frame of cells. STICKY (stickyHeader): it holds at --kl-shell-top and the strips above it\n * and between it and the columns are painted in the page background (--kl-shell-bg), so scrolled content never shows\n * through them.\n */\n// the header reads its own actions (a :has() on the header itself) and hands the crumbs their flex as properties: a\n// :has() outside a rule's subject restyles the whole document on every DOM change\nconst header = cn(\n  \"kl-frame kl-cells min-h-[calc(var(--kl-ctl)+2*var(--kl-k3))] [grid-area:head]\",\n  \"[--kl-shell-crumbs-flex:initial] [--kl-shell-crumbs-min:initial]\",\n  \"has-[>[data-slot=app-shell-actions]>[data-slot=toolbar]]:[--kl-shell-crumbs-min:min(14rem,45%)]\",\n  \"has-[>[data-slot=app-shell-actions]>[data-slot=toolbar]]:[--kl-shell-crumbs-flex:1_1_0]\",\n);\nconst headerSticky = cn(\n  \"sticky top-[var(--kl-shell-top,var(--kl-gutter))] z-3\",\n  \"[box-shadow:0_calc(-1*var(--kl-shell-top,var(--kl-gutter)))_0_var(--kl-shell-bg,var(--kl-paper)),0_var(--kl-gutter)_0_var(--kl-shell-bg,var(--kl-paper))]\",\n);\n\n/** the toggle: a square key cell; its start padding is the cut's corner room, so the glyph clears the bevel */\nconst toggle = cn(\n  \"kl-cell kl-key w-[calc(var(--kl-ctl)+4px)] flex-none cursor-pointer justify-center border-0 bg-transparent p-0\",\n  \"ps-[calc(var(--kl-c,0px)*0.35)] text-kl-ink hover:bg-kl-tint\",\n  \"focus-visible:outline-(length:--kl-ring) focus-visible:outline-offset-(--kl-ring-in) focus-visible:outline-kl-focus focus-visible:outline-solid\",\n);\n\n/*\n * The header's content: a Breadcrumb there is a run of cells the full height of the frame, not a strip along its top.\n * With a Toolbar as the actions the crumbs give up room first, down to their minimum, then the toolbar moves its last\n * items into More: the crumbs' basis is 0, so the toolbar keeps its natural width while there is room.\n */\nconst crumbs = cn(\n  \"flex min-w-[var(--kl-shell-crumbs-min,0px)] flex-[var(--kl-shell-crumbs-flex,1_1_auto)] items-stretch\",\n  \"[:where(&>*)]:min-w-0 [:where(&>*)]:flex-auto\",\n  \"[&_[data-slot=breadcrumb-list]]:min-h-0 [&_[data-slot=breadcrumb-list]]:flex-auto [:where(&>[data-slot=breadcrumb])]:flex\",\n);\n/** a framed toolbar is w-max and doesn't flex: as the actions it shrinks from its natural width */\nconst actions = cn(\n  \"kl-cells flex-none items-stretch\",\n  \"has-[>[data-slot=toolbar]]:min-w-[calc(var(--kl-ctl)+4px)] has-[>[data-slot=toolbar]]:flex-[0_1_auto]\",\n  \"*:data-[slot=toolbar]:min-w-0 *:data-[slot=toolbar]:flex-auto\",\n);\n\n/*\n * A Nav straight in the sidebar or its sheet fills it: its list takes the rest and scrolls inside, its footer stays at\n * the bottom, and the key column's seam runs the full height, through the group labels and the empty end.\n */\nconst navFill = cn(\n  \"[&>[data-slot=nav]>[data-slot=nav-list]]:min-h-0 [&>[data-slot=nav]>[data-slot=nav-list]]:flex-auto [&>[data-slot=nav]>[data-slot=nav-list]]:overflow-y-auto\",\n  \"[&>[data-slot=nav][data-kl-icons]:not([data-kl-rail])>[data-slot=nav-list]]:bg-no-repeat\",\n  \"[&>[data-slot=nav][data-kl-icons]:not([data-kl-rail])>[data-slot=nav-list]]:[background-image:linear-gradient(to_right,transparent_calc(44px_-_var(--kl-k1)),var(--kl-seam)_0_44px,transparent_0)]\",\n  \"kl-rtl:[&>[data-slot=nav][data-kl-icons]:not([data-kl-rail])>[data-slot=nav-list]]:[background-image:linear-gradient(to_left,transparent_calc(44px_-_var(--kl-k1)),var(--kl-seam)_0_44px,transparent_0)]\",\n);\n\n/*\n * The sidebar: a region frame holding the Nav as a cell. It is a column, not a box: sticky one gutter from the top,\n * stretched to the row (the page sets it: size containment keeps the sidebar out of the row's sizing) and capped at\n * the viewport from where it starts (measured), so it ends with the page or the viewport, whichever is first. The rail\n * is the key cells' width; the toggle animates nothing (a layout change that moves the page is immediate).\n */\nconst nav = cn(\n  \"kl-frame sticky flex w-[var(--kl-shell-nav,15rem)] flex-col self-stretch overscroll-contain [contain:size] [grid-area:nav]\",\n  \"max-h-[var(--kl-shell-nav-h,calc(100dvh_-_2*var(--kl-gutter)))] [&[hidden]]:hidden\",\n  // the default breakpoints (data-kl-at): below narrowAt the sidebar takes no room\n  \"@max-[960px]/kl-shell:[[data-kl-at='960-1400']>&]:hidden\",\n  \"[:where(&>[data-slot=nav])]:min-h-0 [:where(&>[data-slot=nav])]:flex-auto\",\n  navFill,\n);\n/** the default breakpoints (data-kl-at): below narrowAt the page spans the row; below asideInlineAt it runs to the end */\nconst page = cn(\n  \"min-w-0 [grid-area:main]\",\n  \"@max-[960px]/kl-shell:[[data-kl-at='960-1400']>&]:col-[1/-1]\",\n  \"@min-[960px]/kl-shell:@max-[1400px]/kl-shell:[[data-kl-at='960-1400']>&]:col-end-[-1]\",\n);\nconst aside = cn(\n  \"sticky flex w-[var(--kl-shell-aside,22rem)] flex-col [grid-area:aside]\",\n  \"[:where(&>*)]:min-h-0 [:where(&>*)]:flex-auto\",\n  // the default breakpoints (data-kl-at): below asideInlineAt the aside takes no room\n  \"@max-[1400px]/kl-shell:[[data-kl-at='960-1400']>&]:hidden\",\n);\n/** the narrow layout: the sheet is the frame, so a framed child (Inspector, a Nav left at layout=\"control\") gives up\n * its own border and cut and fills it */\nconst sheet = cn(\n  \"[:where(&>:is(.kl-frame,.kl-ctl))]:[--kl-bw:0px] [:where(&>:is(.kl-frame,.kl-ctl))]:[--kl-cut-size:0px]\",\n  \"[:where(&>:is(.kl-frame,.kl-ctl))]:min-h-0 [:where(&>:is(.kl-frame,.kl-ctl))]:flex-auto [:where(&>:is(.kl-frame,.kl-ctl))]:overflow-y-auto\",\n);\n\nconst PanelGlyph = ({ open }: { open: boolean }) => (\n  <svg viewBox=\"0 0 16 16\" width=\"16\" height=\"16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.75\" aria-hidden>\n    <rect x=\"1.5\" y=\"2.5\" width=\"13\" height=\"11\" />\n    <path d=\"M6 2.5v11\" />\n    {open ? <path d=\"M3.25 5.5h1M3.25 8h1\" strokeWidth=\"1.5\" /> : null}\n  </svg>\n);\n\n/* ------------------------------------------------------------------ *\n * PARTS\n * ------------------------------------------------------------------ */\n\ntype AppShellState = { layout: ShellBand };\ntype AppShellPartState = Record<string, never>;\nconst PART_STATE: AppShellPartState = {};\n\ntype AppShellRootProps = PartProps<\"div\", AppShellState> & {\n  /** Names the sidebar: the toggle cell says \"Show <navLabel>\", the narrow sheet is called this. */\n  navLabel?: string;\n  /** Wide layout: whether the sidebar column is shown. Controlled or not (default open). */\n  navOpen?: boolean;\n  defaultNavOpen?: boolean;\n  onNavOpenChange?: (open: boolean) => void;\n  /**\n   * Wide layout: what closing the sidebar does. \"hide\" (default) removes the column; \"rail\" keeps a narrow\n   * column of the Nav's key cells (items need `icon`), with each label as a tooltip on hover and focus.\n   */\n  collapse?: \"hide\" | \"rail\";\n  /**\n   * Keep the header in view while the page scrolls. The sidebar and aside then sit one gutter below it and\n   * never move: the page scrolls past a fixed frame. The header sticks at --kl-shell-top (default one gutter;\n   * set it to where the shell starts on the page, so nothing shifts on the first scroll), and the columns\n   * leave the same room at the bottom.\n   */\n  stickyHeader?: boolean;\n  /** Whether the aside (AppShellAside) is shown: beside the page where there is room, else as an end sheet. */\n  asideOpen?: boolean;\n  /** Called when the aside's sheet asks to close (Escape, the scrim), with Base UI's eventDetails. */\n  onAsideOpenChange?: (open: boolean, eventDetails: OpenChangeDetails) => void;\n  /** Below this width of the shell itself (not the viewport), the sidebar and aside become sheets. Default 960. */\n  narrowAt?: number;\n  /**\n   * The aside sits beside the page only from this shell width (default narrowAt + 440: room for the\n   * sidebar, the aside and a page still worth reading). Between narrowAt and this, the sidebar stays and\n   * the aside opens as a sheet, so opening it never crushes the page.\n   */\n  asideInlineAt?: number;\n  /** Wide-layout widths of the sidebar and the inline aside (any CSS length; numbers are px). Defaults 15rem and 22rem. */\n  navSize?: number | string;\n  asideSize?: number | string;\n  /** Portal container for the sheets (the keyline scope travels with them either way). */\n  container?: React.ComponentProps<typeof SheetPopup>[\"container\"];\n};\n\n/**\n * The shell's root: the grid, the width it measures, and the sidebar's state. Holds AppShellHeader, AppShellNav,\n * AppShellMain and AppShellAside as direct children.\n */\nfunction AppShellRoot({\n  navLabel = \"Navigation\",\n  navOpen: navOpenProp,\n  defaultNavOpen = true,\n  onNavOpenChange,\n  collapse = \"hide\",\n  stickyHeader = false,\n  asideOpen = false,\n  onAsideOpenChange,\n  narrowAt = 960,\n  asideInlineAt,\n  container,\n  navSize,\n  asideSize,\n  render,\n  className,\n  style,\n  children,\n  ...props\n}: AppShellRootProps): React.ReactElement {\n  const navId = React.useId();\n  const headRef = React.useRef<HTMLElement | null>(null);\n  // the shell re-renders only when its width crosses a breakpoint (narrow, aside inline), not on every\n  // pixel: a scrollbar appearing as the inspector opens must not re-render the page under a measuring toast\n  const inlineAt = asideInlineAt ?? narrowAt + 440;\n  const at = `${narrowAt}-${inlineAt}`;\n  // until it is measured (and on the server) the shell renders every column; its CSS hides what the width has no room for\n  const [band, setBand] = React.useState<ShellBand>(\"wide\");\n  const narrow = band === \"narrow\";\n  const [navInner, setNavInner] = React.useState(defaultNavOpen);\n  const navOpen = navOpenProp ?? navInner;\n  const setNavOpen = (o: boolean) => {\n    if (navOpenProp === undefined) setNavInner(o);\n    onNavOpenChange?.(o);\n  };\n  const [navSheet, setNavSheet] = React.useState(false);\n  // AppShellAside registers itself, so the root can say whether an aside is open beside the page\n  const [hasAside, setHasAside] = React.useState(0);\n\n  // the width is read from a zero-height sentinel spanning the shell: observing the shell itself would also\n  // report its height, which changes while Base UI's ScrollArea measures the inspector, and WebKit reports\n  // that as a ResizeObserver loop\n  const widthRef = React.useRef<HTMLDivElement>(null);\n  React.useLayoutEffect(() => {\n    const el = widthRef.current;\n    if (!el) return;\n    const measure = () => {\n      const w = el.getBoundingClientRect().width;\n      setBand(w < narrowAt ? \"narrow\" : w >= inlineAt ? \"wide\" : \"medium\");\n    };\n    measure();\n    const ro = new ResizeObserver(measure);\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, [narrowAt, inlineAt]);\n  // a sheet left open while the shell widens would cover the page it now sits beside\n  React.useEffect(() => {\n    if (!narrow) setNavSheet(false);\n  }, [narrow]);\n\n  const rail = !narrow && !navOpen && collapse === \"rail\";\n  const state: AppShellState = { layout: band };\n  const ctx: ShellContextValue = {\n    band,\n    navId,\n    navLabel,\n    collapse,\n    navOpen,\n    setNavOpen,\n    navSheet,\n    setNavSheet,\n    rail,\n    stickyHeader,\n    headRef,\n    asideOpen,\n    onAsideOpenChange,\n    setHasAside,\n    container,\n  };\n  const own = {\n    \"data-slot\": \"app-shell\",\n    \"data-kl-shell\": band,\n    \"data-kl-nav\": !narrow && navOpen ? \"open\" : rail ? \"rail\" : \"closed\",\n    \"data-kl-aside\": band === \"wide\" && asideOpen && hasAside > 0 ? \"open\" : \"closed\",\n    \"data-kl-sticky-head\": stickyHeader ? \"\" : undefined,\n    \"data-kl-at\": at,\n    className: resolveClassName(root, className)(state),\n    style: resolveStyle(\n      {\n        ...(navSize != null ? { \"--kl-shell-nav\": typeof navSize === \"number\" ? `${navSize}px` : navSize } : null),\n        ...(asideSize != null\n          ? { \"--kl-shell-aside\": typeof asideSize === \"number\" ? `${asideSize}px` : asideSize }\n          : null),\n      } as React.CSSProperties,\n      style,\n    )(state),\n    children: (\n      <ShellContext.Provider value={ctx}>\n        {at === DEFAULT_AT ? null : <style>{shellRules(at, narrowAt, inlineAt)}</style>}\n        <div ref={widthRef} data-slot=\"app-shell-sentinel\" className={sentinel} aria-hidden />\n        {children}\n      </ShellContext.Provider>\n    ),\n  };\n  return useRender({ defaultTagName: \"div\", render, state, props: mergeProps<\"div\">(props, own) });\n}\n\n/** The header frame: a row of cells across the top (AppShellToggle, AppShellCrumbs, AppShellActions). */\nfunction AppShellHeader({\n  render,\n  className,\n  style,\n  ref: refProp,\n  ...props\n}: PartProps<\"header\", AppShellPartState>): React.ReactElement {\n  const ctx = useShell(\"AppShellHeader\");\n  const setRef = useMergedRef(ctx.headRef, refProp);\n  const { stickyHeader } = ctx;\n  // a sticky header: its height places the columns below it (it can grow, e.g. a wrapped title)\n  React.useLayoutEffect(() => {\n    const h = ctx.headRef.current;\n    const shell = h?.parentElement;\n    if (!h || !shell || !stickyHeader) return;\n    const set = () => shell.style.setProperty(\"--kl-shell-head-h\", `${h.getBoundingClientRect().height}px`);\n    set();\n    const ro = new ResizeObserver(set);\n    ro.observe(h);\n    return () => {\n      ro.disconnect();\n      shell.style.removeProperty(\"--kl-shell-head-h\");\n    };\n  }, [stickyHeader, ctx.headRef]);\n  const own = {\n    \"data-slot\": \"app-shell-header\",\n    className: resolveClassName(cn(header, stickyHeader && headerSticky), className)(PART_STATE),\n    style: resolveStyle(undefined, style)(PART_STATE),\n  };\n  return useRender({\n    defaultTagName: \"header\",\n    render,\n    ref: setRef,\n    state: PART_STATE,\n    props: mergeProps<\"header\">(props, own),\n  });\n}\n\ntype AppShellToggleState = { expanded: boolean };\n\n/**\n * The header's first cell: shows and hides the sidebar column (wide), collapses it to its rail (collapse=\"rail\"),\n * or opens the start sheet (narrow). Named from the root's navLabel; its glyph is the default children.\n */\nfunction AppShellToggle({\n  render,\n  className,\n  style,\n  children,\n  ...props\n}: PartProps<\"button\", AppShellToggleState>): React.ReactElement {\n  const { band, navOpen, setNavOpen, navSheet, setNavSheet, collapse, navLabel, navId } = useShell(\"AppShellToggle\");\n  const narrow = band === \"narrow\";\n  const expanded = narrow ? navSheet : navOpen;\n  const state: AppShellToggleState = { expanded };\n  const railVerb = collapse === \"rail\" && !narrow;\n  const own = {\n    type: \"button\" as const,\n    \"aria-expanded\": expanded,\n    \"aria-controls\": narrow ? undefined : navId,\n    \"aria-label\": `${expanded ? (railVerb ? \"Collapse\" : \"Hide\") : railVerb ? \"Expand\" : \"Show\"} ${navLabel.toLowerCase()}`,\n    onClick: () => (narrow ? setNavSheet(!navSheet) : setNavOpen(!navOpen)),\n    \"data-slot\": \"app-shell-toggle\",\n    className: resolveClassName(toggle, className)(state),\n    style: resolveStyle(undefined, style)(state),\n    children: children ?? <PanelGlyph open={expanded} />,\n  };\n  return useRender({ defaultTagName: \"button\", render, state, props: mergeProps<\"button\">(props, own) });\n}\n\n/** The header's content after the toggle, taking the rest of the row: usually a <Breadcrumb layout=\"cell\" />. */\nfunction AppShellCrumbs({\n  render,\n  className,\n  style,\n  ...props\n}: PartProps<\"div\", AppShellPartState>): React.ReactElement {\n  const own = {\n    \"data-slot\": \"app-shell-crumbs\",\n    className: resolveClassName(crumbs, className)(PART_STATE),\n    style: resolveStyle(undefined, style)(PART_STATE),\n  };\n  return useRender({ defaultTagName: \"div\", render, state: PART_STATE, props: mergeProps<\"div\">(props, own) });\n}\n\n/**\n * Cells at the header's end. For several commands put a `<Toolbar layout=\"cell\" overflow=\"menu\">` here: it is one\n * Tab stop, and when the header narrows the crumbs give up room first, then the toolbar's last items move into More.\n */\nfunction AppShellActions({\n  render,\n  className,\n  style,\n  ...props\n}: PartProps<\"div\", AppShellPartState>): React.ReactElement {\n  const own = {\n    \"data-slot\": \"app-shell-actions\",\n    className: resolveClassName(actions, className)(PART_STATE),\n    style: resolveStyle(undefined, style)(PART_STATE),\n  };\n  return useRender({ defaultTagName: \"div\", render, state: PART_STATE, props: mergeProps<\"div\">(props, own) });\n}\n\ntype AppShellNavState = { rail: boolean };\n\n/**\n * The sidebar: usually a <Nav layout=\"cell\" />. Wide and medium, a sticky column the toggle shows and hides (or\n * collapses to a rail); narrow, a start sheet the toggle opens, closed by choosing a place. `className`, `style` and\n * `render` reach the column.\n */\nfunction AppShellNav({\n  render,\n  className,\n  style,\n  children,\n  ref: refProp,\n  ...props\n}: PartProps<\"div\", AppShellNavState>): React.ReactElement {\n  const { band, navOpen, setNavOpen, navSheet, setNavSheet, rail, navId, navLabel, stickyHeader, headRef, container } =\n    useShell(\"AppShellNav\");\n  const narrow = band === \"narrow\";\n  const navRef = React.useRef<HTMLDivElement>(null);\n  const setRef = useMergedRef(navRef, refProp);\n  // the sidebar column reaches the viewport's bottom edge from wherever it starts (it is sticky one gutter\n  // from the top), so its footer is always in view; measured, as the shell may not start at the page top\n  React.useLayoutEffect(() => {\n    const el = navRef.current;\n    if (!el || narrow) return;\n    let frame = 0;\n    const fit = () => {\n      frame = 0;\n      const g = parseFloat(getComputedStyle(el).top) || 0;\n      const top = Math.max(g, el.getBoundingClientRect().top);\n      // room left at the bottom: one gutter, or with a sticky header the header's own inset, so the column\n      // ends where the page ends and is never pushed up by the end of its row\n      const head = stickyHeader ? headRef.current : null;\n      const end = head ? parseFloat(getComputedStyle(head).top) || 0 : g;\n      // the cap from the viewport; the page column's end is CSS (stretched to the row, size-contained), so\n      // nothing observes main, whose height moves whenever a scroll area inside it gains a bar\n      // written only when it changes: this runs on every scroll of the page or any scroller in it\n      const h = `${Math.max(240, window.innerHeight - top - end)}px`;\n      if (el.style.getPropertyValue(\"--kl-shell-nav-h\") !== h) el.style.setProperty(\"--kl-shell-nav-h\", h);\n    };\n    const queue = () => {\n      if (!frame) frame = requestAnimationFrame(fit);\n    };\n    fit();\n    window.addEventListener(\"scroll\", queue, { passive: true, capture: true });\n    window.addEventListener(\"resize\", queue);\n    return () => {\n      cancelAnimationFrame(frame);\n      window.removeEventListener(\"scroll\", queue, { capture: true });\n      window.removeEventListener(\"resize\", queue);\n    };\n  }, [narrow, navOpen, rail, stickyHeader, headRef]);\n  const state: AppShellNavState = { rail };\n  const own = {\n    id: navId,\n    hidden: !navOpen && !rail,\n    \"data-slot\": \"app-shell-nav\",\n    \"data-kl-rail\": rail ? \"\" : undefined,\n    className: resolveClassName(\n      cn(\n        nav,\n        stickyHeader ? \"top-(--kl-shell-cols-top)\" : \"top-(--kl-gutter)\",\n        rail && \"w-[var(--kl-shell-rail,56px)]\",\n      ),\n      className,\n    )(state),\n    style: resolveStyle(undefined, style)(state),\n    children: (\n      <NavRailContext.Provider value={rail}>\n        <NavExpandContext.Provider value={rail ? () => setNavOpen(true) : null}>{children}</NavExpandContext.Provider>\n      </NavRailContext.Provider>\n    ),\n  };\n  const column = useRender({ defaultTagName: \"div\", render, ref: setRef, state, props: mergeProps<\"div\">(props, own) });\n  if (!narrow) return column;\n  return (\n    <NavSheet open={navSheet} setOpen={setNavSheet} label={navLabel} container={container}>\n      {children}\n    </NavSheet>\n  );\n}\n\n/** The scrollable box between `el` and `stop` that holds `el`, if any. */\nfunction scrollerOf(el: HTMLElement, stop: HTMLElement) {\n  for (let n = el.parentElement; n && n !== stop; n = n.parentElement) {\n    if (n.scrollHeight > n.clientHeight + 1 && /auto|scroll/.test(getComputedStyle(n).overflowY)) return n;\n  }\n  return null;\n}\n\n/**\n * The narrow sidebar: a start sheet.\n * - It opens on your place: the current item (or the closed parent holding it) is centred in the list and takes\n *   focus, so a long index never opens at its top while you are deep in it.\n * - Choosing a place closes it once the choice has landed: the band arrives on the item (--kl-stretch-trail, 0 under\n *   reduced motion), then the sheet folds. Closing on the press itself folded the sheet over a band still in flight.\n */\nfunction NavSheet({\n  open,\n  setOpen,\n  label,\n  container,\n  children,\n}: {\n  open: boolean;\n  setOpen: (open: boolean) => void;\n  label: string;\n  container: ShellContextValue[\"container\"];\n  children: React.ReactNode;\n}) {\n  const popRef = React.useRef<HTMLDivElement>(null);\n  const closing = React.useRef(0);\n  React.useEffect(() => () => window.clearTimeout(closing.current), []);\n  const onOpenChange = (o: boolean) => {\n    window.clearTimeout(closing.current);\n    setOpen(o);\n  };\n  const initialFocus = React.useCallback(() => {\n    const pop = popRef.current;\n    const here = pop?.querySelector<HTMLElement>(\n      \"[data-kl-item][aria-current='page'], [data-kl-item][aria-current='true'], [data-kl-item][data-kl-holds]\",\n    );\n    if (!pop || !here) return true;\n    const box = scrollerOf(here, pop);\n    if (box) {\n      const b = box.getBoundingClientRect();\n      const r = here.getBoundingClientRect();\n      box.scrollTop += r.top - b.top - (b.height - r.height) / 2;\n    }\n    return here;\n  }, []);\n  // a nested item's disclosure (aria-expanded) opens its list in place, and a disabled item goes nowhere: neither is a place\n  const closeOnChoose = (e: React.MouseEvent<HTMLDivElement>) => {\n    if (!(e.target as Element).closest(\"[data-kl-item]:not([aria-expanded], [aria-disabled='true'], :disabled)\"))\n      return;\n    const hold = tokenMs(e.currentTarget, \"--kl-stretch-trail\", 220);\n    window.clearTimeout(closing.current);\n    if (hold <= 0) setOpen(false);\n    else closing.current = window.setTimeout(() => setOpen(false), hold);\n  };\n  return (\n    <Sheet open={open} onOpenChange={onOpenChange}>\n      <SheetPopup\n        ref={popRef}\n        side=\"start\"\n        size={300}\n        container={container}\n        aria-label={label}\n        data-kl-shell-sheet=\"nav\"\n        className={cn(sheet, navFill)}\n        initialFocus={initialFocus}\n        onClick={closeOnChoose}\n      >\n        {children}\n      </SheetPopup>\n    </Sheet>\n  );\n}\n\n/** The page: a <main> landmark in the shell's middle column. */\nfunction AppShellMain({\n  render,\n  className,\n  style,\n  ...props\n}: PartProps<\"main\", AppShellPartState>): React.ReactElement {\n  useShell(\"AppShellMain\");\n  const own = {\n    \"data-slot\": \"app-shell-main\",\n    className: resolveClassName(page, className)(PART_STATE),\n    style: resolveStyle(undefined, style)(PART_STATE),\n  };\n  return useRender({ defaultTagName: \"main\", render, state: PART_STATE, props: mergeProps<\"main\">(props, own) });\n}\n\ntype AppShellAsideProps = PartProps<\"div\", AppShellPartState> & {\n  /** Names the aside's sheet on narrow and medium layouts. Default \"Details\". */\n  label?: string;\n  /**\n   * Where focus goes when the aside's sheet closes (Base UI finalFocus). The sheet is opened by state, not\n   * by a trigger, so pass the control that opened it (a ref): WebKit does not focus a button on click, and\n   * without this focus would fall back to whatever was focused before.\n   */\n  finalFocus?: React.ComponentProps<typeof SheetPopup>[\"finalFocus\"];\n};\n\n/**\n * A contextual panel at the end (an Inspector), shown while the root's `asideOpen`: beside the page from\n * asideInlineAt, an end sheet below it (focus moves in, Escape and the scrim close it, focus returns).\n * `className`, `style` and `render` reach the column.\n */\nfunction AppShellAside({\n  label = \"Details\",\n  finalFocus,\n  render,\n  className,\n  style,\n  children,\n  ...props\n}: AppShellAsideProps): React.ReactElement | null {\n  const { band, asideOpen, onAsideOpenChange, setHasAside, stickyHeader, container } = useShell(\"AppShellAside\");\n  React.useLayoutEffect(() => {\n    setHasAside((n) => n + 1);\n    return () => setHasAside((n) => n - 1);\n  }, [setHasAside]);\n  const own = {\n    \"data-slot\": \"app-shell-aside\",\n    className: resolveClassName(\n      cn(\n        aside,\n        stickyHeader\n          ? \"top-(--kl-shell-cols-top) max-h-[calc(100dvh_-_2*var(--kl-shell-top,var(--kl-gutter))_-_var(--kl-shell-head-h,0px)_-_var(--kl-gutter))]\"\n          : \"top-(--kl-gutter) max-h-[calc(100dvh_-_2*var(--kl-gutter))]\",\n      ),\n      className,\n    )(PART_STATE),\n    style: resolveStyle(undefined, style)(PART_STATE),\n    children,\n  };\n  const column = useRender({ defaultTagName: \"div\", render, state: PART_STATE, props: mergeProps<\"div\">(props, own) });\n  if (band === \"wide\") return asideOpen ? column : null;\n  return (\n    <Sheet open={asideOpen} onOpenChange={(o, details) => onAsideOpenChange?.(o, details)}>\n      <SheetPopup\n        side=\"end\"\n        size={400}\n        container={container}\n        aria-label={label}\n        finalFocus={finalFocus}\n        data-kl-shell-sheet=\"aside\"\n        className={sheet}\n      >\n        {children}\n      </SheetPopup>\n    </Sheet>\n  );\n}\n\n/* ------------------------------------------------------------------ *\n * CONVENIENCE\n * ------------------------------------------------------------------ */\n\ntype AppShellProps = Omit<AppShellRootProps, \"children\" | \"render\"> & {\n  /** The sidebar: usually <Nav layout=\"cell\" …/>. */\n  nav: React.ReactNode;\n  /** Header content after the toggle cell: usually <Breadcrumb layout=\"cell\" …/>. */\n  header?: React.ReactNode;\n  /**\n   * Cells at the header's end. For several commands pass a `<Toolbar layout=\"cell\" overflow=\"menu\">`: it is\n   * one Tab stop, and when the header narrows the crumbs give up room first, then the toolbar's last items\n   * move into More.\n   */\n  actions?: React.ReactNode;\n  /** Contextual panel at the end (an Inspector). Shown while `asideOpen`. */\n  aside?: React.ReactNode;\n  /** Names the aside's sheet on narrow layouts. */\n  asideLabel?: string;\n  /** Where focus goes when the aside's sheet closes (AppShellAside's finalFocus). */\n  asideFinalFocus?: AppShellAsideProps[\"finalFocus\"];\n  /** Props for the <main> element (AppShellMain). */\n  mainProps?: React.ComponentProps<\"main\">;\n  children?: React.ReactNode;\n};\n\n/**\n * The page around a product's screens: a header frame, the sidebar, the page, and an optional aside, built from\n * the parts above (AppShellRoot, AppShellHeader with AppShellToggle, AppShellCrumbs and AppShellActions,\n * AppShellNav, AppShellMain, AppShellAside).\n *\n *   ┌──┬──────────────────────────────────────────────────┬───────┐\n *   │▥ │ Shipments / Rotterdam / SHP-24018                 │ acts  │   header frame: toggle cell · crumbs · actions\n *   └──┴──────────────────────────────────────────────────┴───────┘\n *   ┌────────┐ ┌──────────────────────────────┐ ┌──────────────┐\n *   │ Nav    │ │ main                         │ │ aside        │   one gutter apart (C1)\n *   └────────┘ └──────────────────────────────┘ └──────────────┘\n *\n * Three layouts, by the shell's own width:\n * - wide (≥ asideInlineAt): sidebar, page and aside side by side. The toggle cell shows and hides the\n *   sidebar column (aria-expanded, aria-controls). The sidebar and aside are sticky, one gutter from the top.\n * - medium (≥ narrowAt): the sidebar stays; the aside opens as an end Sheet, so it never crushes the page.\n * - narrow: the sidebar is a start Sheet opened by the same cell and closed by choosing an item; the aside\n *   is an end Sheet bound to `asideOpen` (focus moves in, Escape and the scrim close it, focus returns).\n * - The width is the shell's own, so a shell inside a pane or a preview frame decides for itself. CSS lays the\n *   columns out by it (a container query), so the server's HTML is already right for the width it lands in;\n *   the measurement (ResizeObserver) then decides only which parts become sheets, and moves nothing.\n * - Motion: the sheets unfold from their edge; the wide column toggles without animation (a layout change\n *   that moves the page should be immediate).\n */\nfunction AppShell({\n  nav,\n  header,\n  actions,\n  aside,\n  asideLabel = \"Details\",\n  asideFinalFocus,\n  mainProps,\n  children,\n  ...props\n}: AppShellProps): React.ReactElement {\n  return (\n    <AppShellRoot {...props}>\n      <AppShellHeader>\n        <AppShellToggle />\n        <AppShellCrumbs>{header}</AppShellCrumbs>\n        {actions ? <AppShellActions>{actions}</AppShellActions> : null}\n      </AppShellHeader>\n      <AppShellNav>{nav}</AppShellNav>\n      <AppShellMain {...mainProps}>{children}</AppShellMain>\n      {aside ? (\n        <AppShellAside label={asideLabel} finalFocus={asideFinalFocus}>\n          {aside}\n        </AppShellAside>\n      ) : null}\n    </AppShellRoot>\n  );\n}\n\nexport {\n  AppShell,\n  AppShellRoot,\n  AppShellHeader,\n  AppShellToggle,\n  AppShellCrumbs,\n  AppShellActions,\n  AppShellNav,\n  AppShellMain,\n  AppShellAside,\n  type AppShellProps,\n  type AppShellRootProps,\n  type AppShellAsideProps,\n};\n",
      "type": "registry:ui",
      "target": "components/keyline/ui/app-shell.tsx"
    }
  ],
  "type": "registry:ui"
}