{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-side-nav",
  "type": "registry:ui",
  "title": "react-tw/side-nav.tsx",
  "description": "Carbon `SideNav`: the left navigation. Default: slides over the page below `lg`, 256px from `lg`; Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [
    "@purpurabog/ui@^2.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/tw-header",
    "@kanok-ui/tw-i18n",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/side-nav.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/side-nav.tsx",
      "content": "import { createContext, useContext, type ComponentProps, type ReactNode } from 'react';\nimport { Shell as ShellPrimitive, SideNav as SideNavPrimitive } from '@purpurabog/ui/headless';\nimport { InSideNav } from './header.tsx';\nimport { useTranslation } from './lib/i18n.ts';\nimport { cn } from './lib/utils.ts';\n\n/*\n * Carbon UI Shell side navigation as React + Tailwind components, the look and rules of\n * `<ui-side-nav>` and its parts, and the `<main>` content area beside it. Carbon's `lg` breakpoint is\n * 1056px (Tailwind's own `lg` is 1024px, so `min-[1056px]:` / `max-[1056px]:` here). The behaviour\n * (Esc, focus leaving, the rail's hover, the overlay, the content following the mode) is the\n * headless `SideNav` / `Shell`'s.\n */\n\nexport type SideNavMode = SideNavPrimitive.SideNavMode;\n\nexport interface SideNavProps extends Omit<ComponentProps<'nav'>, 'expanded' | 'onToggle' | 'rail' | 'fixed' | 'collapsible' | 'aria-label' | 'id' | 'lang' | 'children'> {\n    /** Open (controlled; the HeaderMenuButton's `active`). */\n    expanded?: boolean;\n    /** Esc, the overlay or focus leaving asked to close it. */\n    onToggle?: (expanded: boolean) => void;\n    /** A 48px icon rail that expands on hover / focus. */\n    rail?: boolean;\n    /** Always 256px, pushing the Content aside (no overlay). */\n    fixed?: boolean;\n    /** Closed on large screens too until the HeaderMenuButton (with `collapsible`) opens it over the page (Carbon `isPersistent={false}`); the Content isn't pushed aside. */\n    collapsible?: boolean;\n    'aria-label'?: string;\n    id?: string;\n    lang?: string;\n    /** `<SideNavItems>` (and a `<HeaderNavigation>` copy for small screens). */\n    children: ReactNode;\n}\n\n/**\n * Carbon `SideNav`: the left navigation. Default: slides over the page below `lg`, 256px from `lg`;\n * `rail`: 48px icons, expanding on hover / focus; `fixed`: always 256px. Esc collapses it and gives\n * focus back; below `lg` an overlay covers the page while it is open. `collapsible` (Carbon\n * `isPersistent={false}`) keeps it closed on large screens too until it is opened over the page.\n */\nexport function SideNav({ expanded, onToggle, rail = false, fixed = false, collapsible = false, 'aria-label': label, id, lang, children, ...rest }: Readonly<SideNavProps>) {\n    return (\n        <SideNavPrimitive.Root expanded={expanded} onToggle={onToggle} rail={rail} fixed={fixed} collapsible={collapsible}>\n            <SideNavBody {...rest} aria-label={label} id={id} lang={lang}>\n                {children}\n            </SideNavBody>\n        </SideNavPrimitive.Root>\n    );\n}\n\n/** The overlay and the nav, inside the side nav's primitive (their width and overlay follow its state). */\nfunction SideNavBody({ 'aria-label': label, lang, children, ...rest }: Readonly<Omit<SideNavProps, 'expanded' | 'onToggle' | 'rail' | 'fixed' | 'collapsible'>>) {\n    const t = useTranslation(lang).common;\n    const { expanded, hoverOpen, mode } = SideNavPrimitive.useSideNavContext();\n    const fixed = mode === 'fixed';\n    let width = mode === 'collapsible' ? 'w-0' : 'w-0 min-[1056px]:w-64';\n    if (expanded || hoverOpen || fixed) width = 'w-64';\n    else if (mode === 'rail') width = 'w-12';\n\n    return (\n        <>\n            <SideNavPrimitive.Overlay\n                className={cn(\n                    'fixed start-0 top-12 h-0 w-0 bg-transparent opacity-0 transition-[opacity,background-color] duration-300 ease-[cubic-bezier(0.5,0,0.1,1)] motion-reduce:transition-none',\n                    expanded && !fixed && 'max-[1056px]:z-[6000] max-[1056px]:h-[100vh] max-[1056px]:w-[100vw] max-[1056px]:bg-overlay max-[1056px]:opacity-100',\n                )}\n            />\n            <SideNavPrimitive.Nav {...rest}\n                aria-label={label || t.sideNavigation}\n                className={cn(\n                    'fixed start-0 top-12 bottom-0 z-[8000] box-border flex max-w-64 flex-col overflow-hidden bg-canvas font-sans text-ink-muted outline-none will-change-[inline-size]',\n                    'transition-[inline-size,transform] duration-(--ui-duration-moderate) ease-(--ui-easing-exit) forced-colors:border-e forced-colors:border-solid forced-colors:border-e-[CanvasText]',\n                    width,\n                    fixed && !expanded && '[transform:translateX(-256px)] rtl:[transform:translateX(256px)]',\n                )}\n            >\n                <InSideNav.Provider value={true}>{children}</InSideNav.Provider>\n            </SideNavPrimitive.Nav>\n        </>\n    );\n}\n\n/** Carbon `SideNavItems`: the scrolling list inside a SideNav. */\nexport function SideNavItems({ children, ...rest }: Readonly<Omit<ComponentProps<'div'>, 'children'> & { children: ReactNode }>) {\n    return (\n        <div {...rest} className=\"block flex-[1_1_0%] overflow-x-hidden overflow-y-auto\">\n            <ul className=\"m-0 list-none px-0 pt-4 pb-0\">\n                {children}\n            </ul>\n        </div>\n    );\n}\n\n/** Carbon `SideNavDivider`: a 1px rule with 8px / 16px margins. */\nexport function SideNavDivider({ ...rest }: Readonly<ComponentProps<'li'>>) {\n    return (\n        <li {...rest} className=\"block\">\n            <hr className=\"mx-4 my-2 h-px border-0 bg-border-subtle-1\" />\n        </li>\n    );\n}\n\ntype LinkProps = Omit<ComponentProps<'a'>, 'className' | 'children'>;\n\n// --ui-nav-item-*: Carbon rows run edge to edge, square; a design can inset and round them\nconst linkClasses =\n    'group/link relative box-border mx-[var(--ui-nav-item-inset,0px)] flex min-h-8 items-center rounded-[var(--ui-nav-item-radius,0px)] px-4 text-ink-muted no-underline outline-none transition-[color,background-color,outline] duration-(--ui-duration-moderate) ease-[ease] hover:bg-canvas-hover focus-visible:outline-[length:var(--ui-focus-width,2px)] focus-visible:-outline-offset-2 focus-visible:outline-focus focus-visible:outline-solid';\n// the current page: a fill and a bar on the start edge (--ui-nav-current-*; Kanok: the tint alone, no bar)\nconst currentLink =\n    \"bg-[color:var(--ui-nav-current-background,var(--ui-canvas-selected))] before:absolute before:inset-y-[var(--ui-nav-current-mark-inset,0px)] before:start-0 before:w-[var(--ui-nav-current-mark-width,3px)] before:rounded-[var(--ui-nav-current-mark-radius,0px)] before:bg-[color:var(--ui-nav-current-mark,var(--ui-primary))] before:content-[''] forced-colors:before:w-[3px] forced-colors:before:bg-[Highlight]\";\nconst textClasses = 'overflow-hidden leading-5! tracking-[0.1px] text-ellipsis whitespace-nowrap text-ink-muted select-none [font:var(--ui-font-body-sm)] group-hover/link:text-ink';\nconst iconClasses = 'me-6 flex flex-[0_0_16px] items-center justify-center [&>svg]:size-4';\n\nexport interface SideNavLinkProps extends LinkProps {\n    /** The current page (`aria-current=\"page\"`, the 3px bar). */\n    current?: boolean;\n    /** A 48px row. */\n    large?: boolean;\n    /** A 16px icon before the text. */\n    icon?: ReactNode;\n    children: ReactNode;\n}\n\n/** Carbon `SideNavLink`: a navigation link with an optional icon. In a collapsed (non-rail) SideNav it leaves the tab order, as Carbon's. */\nexport function SideNavLink({ href = '#', current = false, large = false, icon, children, ...rest }: Readonly<SideNavLinkProps>) {\n    return (\n        <li className=\"block w-full overflow-hidden\">\n            <SideNavPrimitive.Link {...rest} href={href} current={current} className={cn(linkClasses, large && 'h-12', current && currentLink)}>\n                {icon ? <span className={cn(iconClasses, current ? '[&>svg]:fill-icon-primary' : '[&>svg]:fill-icon-secondary')}>{icon}</span> : null}\n                <span className={cn(textClasses, current && 'font-semibold! text-ink')}>{children}</span>\n            </SideNavPrimitive.Link>\n        </li>\n    );\n}\n\n/** The menu's items are indented past its icon. */\nconst ParentIcon = createContext(false);\n\nexport interface SideNavMenuProps extends Omit<ComponentProps<'li'>, 'label' | 'expanded' | 'defaultExpanded' | 'onToggle' | 'large' | 'icon' | 'children'> {\n    label: string;\n    /** Open (controlled). */\n    expanded?: boolean;\n    defaultExpanded?: boolean;\n    onToggle?: (expanded: boolean) => void;\n    large?: boolean;\n    icon?: ReactNode;\n    /** `<SideNavMenuItem>`s. */\n    children: ReactNode;\n}\n\nconst chevron = (open: boolean) => (\n    <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\" className={cn('size-4 fill-icon-secondary transition-[transform] duration-(--ui-duration-moderate) ease-[ease]', open && '[transform:rotate(180deg)]')}>\n        <path d=\"M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z\" />\n    </svg>\n);\n\n/** Carbon `SideNavMenu`: a collapsible group of SideNavMenuItems; collapsed with a `current` item, it shows the bar and bold title. */\nexport function SideNavMenu({ label, expanded, defaultExpanded = false, onToggle, large = false, icon, children, ...rest }: Readonly<SideNavMenuProps>) {\n    return (\n        <SideNavPrimitive.Menu {...rest} expanded={expanded} defaultExpanded={defaultExpanded} onExpandedChange={onToggle} className=\"block w-full overflow-hidden\">\n            <SideNavMenuBody label={label} large={large} icon={icon}>\n                {children}\n            </SideNavMenuBody>\n        </SideNavPrimitive.Menu>\n    );\n}\n\n/** The title button and the items, inside the menu's primitive (they follow its expanded and current state). */\nfunction SideNavMenuBody({ label, large, icon, children }: Readonly<Pick<SideNavMenuProps, 'label' | 'large' | 'icon' | 'children'>>) {\n    const { expanded, current: active } = SideNavPrimitive.useMenuContext();\n    return (\n        <>\n            <SideNavPrimitive.MenuTrigger\n                className={cn(\n                    'relative box-border mx-[var(--ui-nav-item-inset,0px)] flex h-8 w-[calc(100%-2*var(--ui-nav-item-inset,0px))] cursor-pointer items-center rounded-[var(--ui-nav-item-radius,0px)] border-0 bg-transparent px-4 text-start text-ink-muted type-body-emphasis leading-[18px]! outline-none select-none transition-[color,background-color] duration-(--ui-duration-moderate) ease-[ease]',\n                    'hover:bg-canvas-hover hover:text-ink focus-visible:outline-[length:var(--ui-focus-width,2px)] focus-visible:-outline-offset-2 focus-visible:outline-focus focus-visible:outline-solid',\n                    large && 'h-12',\n                    active && !expanded && cn('text-ink', currentLink),\n                )}\n            >\n                {icon ? <span className={cn(iconClasses, active ? '[&>svg]:fill-icon-primary' : '[&>svg]:fill-icon-secondary')}>{icon}</span> : null}\n                <span className={cn('overflow-hidden text-ellipsis whitespace-nowrap', active && 'font-semibold! text-ink')}>{label}</span>\n                <span className=\"flex flex-1 justify-end\">{chevron(expanded)}</span>\n            </SideNavPrimitive.MenuTrigger>\n            <SideNavPrimitive.MenuPopup className={cn('m-0 block list-none p-0', expanded ? 'max-h-[1500px] [visibility:inherit]' : 'invisible max-h-0')}>\n                <ParentIcon.Provider value={!!icon}>{children}</ParentIcon.Provider>\n            </SideNavPrimitive.MenuPopup>\n        </>\n    );\n}\n\nexport interface SideNavMenuItemProps extends LinkProps {\n    current?: boolean;\n    children: ReactNode;\n}\n\n/** Carbon `SideNavMenuItem`: a 32px link inside a SideNavMenu, indented 32px (72px when the menu has an icon). */\nexport function SideNavMenuItem({ href = '#', current = false, children, ...rest }: Readonly<SideNavMenuItemProps>) {\n    const parentIcon = useContext(ParentIcon);\n    return (\n        <li className=\"block w-full overflow-hidden\">\n            <SideNavPrimitive.MenuItem {...rest} href={href} current={current} className={cn(linkClasses, 'h-8 ps-8', parentIcon && 'ps-[72px]', current && currentLink)}>\n                <span className={cn(textClasses, 'font-normal!', current && 'font-semibold! text-ink')}>{children}</span>\n            </SideNavPrimitive.MenuItem>\n        </li>\n    );\n}\n\nexport interface ContentProps extends Omit<ComponentProps<'main'>, 'sideNav' | 'id' | 'className' | 'children'> {\n    /** How the SideNav beside it lays out; found on the page (or in the same shadow root) when left out. */\n    sideNav?: SideNavMode | 'none';\n    id?: string;\n    className?: string;\n    children?: ReactNode;\n}\n\n/** The offset per side-nav mode, on the primitive's `data-side-nav`. */\nconst NAV_OFFSET = '[--_nav:0px] data-[side-nav=fixed]:[--_nav:256px] data-[side-nav=rail]:[--_nav:48px] data-[side-nav=default]:min-[1056px]:[--_nav:256px]';\n\n/** Carbon `Content`: the `<main id=\"main-content\">` area under the 48px header, beside a fixed or rail SideNav (256px from `lg` for the default one); 32px padding. */\nexport function Content({ sideNav, id = 'main-content', className, children, ...rest }: Readonly<ContentProps>) {\n    return (\n        <ShellPrimitive.Content {...rest}\n            sideNav={sideNav}\n            id={id}\n            className={cn('relative start-(--_nav) block w-[calc(100%_-_var(--_nav))] bg-canvas font-sans text-ink', NAV_OFFSET, className)}\n        >\n            <div className=\"mt-12 p-8\">{children}</div>\n        </ShellPrimitive.Content>\n    );\n}\n"
    }
  ]
}
