{
  "name": "cai-mega-menu",
  "type": "registry:ui",
  "description": "Accessible single-panel CAI mega navigation with four-column discovery layout",
  "files": [
    {
      "path": "components/ui/cai-mega-menu.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { createPortal } from \"react-dom\"\nimport { ChevronDown, ArrowUpRight } from \"lucide-react\"\nimport { cn } from \"@/lib/utils\"\n\n/**\n * CAI Mega Menu\n * Accessible, single-open-panel navigation inspired by the spatial rhythm of\n * React Bits' CardNav and the shadcn/Radix NavigationMenu pattern. This is a\n * CAI-owned implementation, not copied third-party source.\n *\n * The component is intentionally data-driven so multiple CAI surfaces can\n * provide their own navigation destinations and translated labels.\n */\nexport type CaiMegaMenuLink = {\n  label: string\n  href: string\n  description?: string\n  badge?: string\n}\n\nexport type CaiMegaMenuSection = {\n  heading: string\n  links: CaiMegaMenuLink[]\n}\n\nexport type CaiMegaMenuGroup = {\n  label: string\n  sections: CaiMegaMenuSection[]\n  footer?: CaiMegaMenuLink[]\n}\n\nexport type CaiMegaMenuProps = {\n  groups: CaiMegaMenuGroup[]\n  className?: string\n  panelClassName?: string\n  /** Set false when the host already owns the backdrop. */\n  backdrop?: boolean\n  pageReveal?: number\n  onNavigate?: () => void\n  renderVisual?: (group: CaiMegaMenuGroup, close: () => void) => React.ReactNode\n  LinkComponent?: React.ComponentType<{\n    href: string\n    className?: string\n    onClick?: () => void\n    children: React.ReactNode\n  }>\n}\n\nconst nativeLink = ({ href, className, onClick, children }: {\n  href: string; className?: string; onClick?: () => void; children: React.ReactNode\n}) => <a href={href} className={className} onClick={onClick}>{children}</a>\n\nexport function CaiMegaMenu({\n  groups,\n  className,\n  panelClassName,\n  backdrop = true,\n  pageReveal = 48,\n  onNavigate,\n  renderVisual,\n  LinkComponent = nativeLink,\n}: CaiMegaMenuProps) {\n  const [active, setActive] = React.useState<string | null>(null)\n  const [direction, setDirection] = React.useState<\"left\" | \"right\" | \"open\">(\"open\")\n  const activate = (label: string) => {\n    if (label === active) return\n    setDirection(active === null ? \"open\" : groups.findIndex(g => g.label === label) > groups.findIndex(g => g.label === active) ? \"right\" : \"left\")\n    setActive(label)\n  }\n  const rootRef = React.useRef<HTMLDivElement>(null)\n  const closeTimer = React.useRef<ReturnType<typeof setTimeout> | null>(null)\n  const restoringFocus = React.useRef(false)\n  const uid = React.useId().replace(/:/g, \"\")\n  const openedGroup = groups.find((group) => group.label === active)\n  const panelRef = React.useRef<HTMLDivElement>(null)\n  const [availableHeight, setAvailableHeight] = React.useState<number>()\n  const isOpen = Boolean(openedGroup)\n  React.useLayoutEffect(() => {\n    if (!isOpen) return\n    const panel = panelRef.current\n    if (!panel) return\n    const measure = () => {\n      const viewport = window.visualViewport\n      const bottom = viewport ? viewport.offsetTop + viewport.height : window.innerHeight\n      const top = panel.getBoundingClientRect().top\n      setAvailableHeight(Math.max(0, Math.floor(bottom - top - pageReveal)))\n    }\n    measure()\n    const observer = new ResizeObserver(measure)\n    const header = panel.offsetParent\n    if (header instanceof HTMLElement) observer.observe(header)\n    window.addEventListener(\"resize\", measure)\n    window.addEventListener(\"scroll\", measure, { passive: true })\n    window.visualViewport?.addEventListener(\"resize\", measure)\n    window.visualViewport?.addEventListener(\"scroll\", measure)\n    return () => {\n      observer.disconnect()\n      window.removeEventListener(\"resize\", measure)\n      window.removeEventListener(\"scroll\", measure)\n      window.visualViewport?.removeEventListener(\"resize\", measure)\n      window.visualViewport?.removeEventListener(\"scroll\", measure)\n    }\n  }, [isOpen, pageReveal])\n\n  // The full-width panel extends beyond the narrow trigger row. A short grace\n  // period lets the pointer move diagonally from Ventures or Resources into it.\n  const cancelClose = () => {\n    if (closeTimer.current !== null) {\n      clearTimeout(closeTimer.current)\n      closeTimer.current = null\n    }\n  }\n  const scheduleClose = () => {\n    cancelClose()\n    closeTimer.current = setTimeout(() => {\n      setActive(null)\n      closeTimer.current = null\n    }, 240)\n  }\n\n  React.useEffect(() => () => {\n    if (closeTimer.current !== null) clearTimeout(closeTimer.current)\n  }, [])\n\n  React.useEffect(() => {\n    if (!active) return\n    const closeOnEscape = (event: KeyboardEvent) => {\n      if (event.key === \"Escape\") {\n        cancelClose()\n        setActive(null)\n        restoringFocus.current = true\n        const trigger = Array.from(rootRef.current?.querySelectorAll<HTMLButtonElement>(\"[data-cai-mega-trigger]\") ?? []).find(\n          (el) => el.dataset.caiMegaTrigger === active,\n        )\n        trigger?.focus()\n        queueMicrotask(() => { restoringFocus.current = false })\n      }\n    }\n    const closeOnOutsideClick = (event: PointerEvent) => {\n      if (!rootRef.current?.contains(event.target as Node)) {\n        cancelClose()\n        setActive(null)\n      }\n    }\n    document.addEventListener(\"keydown\", closeOnEscape)\n    document.addEventListener(\"pointerdown\", closeOnOutsideClick)\n    return () => {\n      document.removeEventListener(\"keydown\", closeOnEscape)\n      document.removeEventListener(\"pointerdown\", closeOnOutsideClick)\n    }\n  }, [active])\n\n  const close = () => {\n    cancelClose()\n    setActive(null)\n    onNavigate?.()\n  }\n\n  return (\n    <div\n      ref={rootRef}\n      className={cn(\"flex items-center gap-1 lg:gap-2\", className)}\n      onMouseEnter={cancelClose}\n      onMouseLeave={scheduleClose}\n      onBlur={(event) => {\n        if (!event.currentTarget.contains(event.relatedTarget)) setActive(null)\n      }}\n    >\n      {groups.map((group) => {\n        const selected = active === group.label\n        return (\n          <button\n            key={group.label}\n            type=\"button\"\n            data-cai-mega-trigger={group.label}\n            aria-expanded={selected}\n            aria-haspopup=\"true\"\n            aria-controls={`cai-mega-${uid}`}\n            onMouseEnter={() => {\n              cancelClose()\n              activate(group.label)\n            }}\n            onFocus={() => {\n              if (!restoringFocus.current) {\n                cancelClose()\n                activate(group.label)\n              }\n            }}\n            onClick={() => selected ? setActive(null) : activate(group.label)}\n            className={cn(\n              \"group inline-flex h-11 shrink-0 items-center gap-1.5 px-3 text-[12px] font-medium tracking-[-0.015em] outline-offset-[-4px] transition-colors\",\n              selected ? \"text-foreground\" : \"text-muted-foreground hover:text-foreground\",\n            )}\n          >\n            {group.label}\n            <ChevronDown aria-hidden=\"true\" className={cn(\"size-3.5 transition-transform duration-200\", selected && \"rotate-180 text-emerald-500\")} />\n          </button>\n        )\n      })}\n\n      {openedGroup && backdrop && typeof document !== \"undefined\" && createPortal(\n        <div aria-hidden=\"true\" data-testid=\"cai-mega-backdrop\" className=\"fixed inset-0 z-40 bg-black/25 backdrop-blur-[8px] animate-in fade-in-0 duration-200 motion-reduce:animate-none\" />,\n        document.body,\n      )}\n      {openedGroup && (\n        <div\n          ref={panelRef}\n          id={`cai-mega-${uid}`}\n          data-testid=\"cai-mega-panel\"\n          onMouseEnter={cancelClose}\n          onMouseLeave={scheduleClose}\n          className={cn(\n            \"absolute inset-x-0 top-full z-[80] bg-[#f1f2f3] text-[#202626] shadow-[0_36px_90px_-18px_rgba(0,0,0,0.13)] dark:bg-[#141719] dark:text-[#f5f6f6] dark:shadow-[0_36px_90px_-18px_rgba(0,0,0,0.46)]\",\n            \"animate-in fade-in-0 slide-in-from-top-2 duration-200 motion-reduce:animate-none\",\n            panelClassName,\n          )}\n        >\n          <div style={{ maxHeight: availableHeight === undefined ? \"calc(100dvh - 112px)\" : availableHeight }} className=\"mx-auto w-full max-w-[1480px] overflow-y-auto overscroll-contain px-6 py-5 xl:px-10\">\n            <div key={openedGroup.label} className={cn(\"animate-in fade-in-0 duration-200 motion-reduce:animate-none\", direction === \"right\" && \"slide-in-from-right-3\", direction === \"left\" && \"slide-in-from-left-3\")}>\n            <div className={cn(\"grid gap-6\", renderVisual && \"xl:grid-cols-[minmax(0,1fr)_minmax(280px,0.42fr)]\")}>\n            <div className={cn(\"grid content-start gap-y-4 md:grid-cols-2 xl:grid-cols-4\", renderVisual ? \"gap-x-5\" : \"gap-x-10\")}>\n              {openedGroup.sections.map((section) => (\n                <div key={section.heading} className=\"min-w-0\">\n                  <h2 className=\"mb-3 text-[11px] font-semibold uppercase tracking-[0.14em] text-[#697274] dark:text-[#90999a]\">{section.heading}</h2>\n                  <ul className=\"space-y-1\">\n                    {section.links.map((link) => (\n                      <li key={link.href}>\n                        <LinkComponent href={link.href} onClick={close} className=\"group/link block -mx-2 rounded-[2px] px-2 py-1.5 outline-offset-[-2px] transition-colors hover:bg-black/[0.045] dark:hover:bg-white/[0.055] focus-visible:bg-black/[0.045] dark:focus-visible:bg-white/[0.055]\">\n                          <span className=\"flex items-center gap-2 text-[15px] font-semibold leading-5 text-[#202626] dark:text-[#f4f5f5] group-hover/link:text-emerald-700 dark:group-hover/link:text-emerald-300\">\n                            {link.label}\n                            {link.badge && <span className=\"border border-emerald-700/25 dark:border-emerald-300/25 px-1.5 py-0.5 text-[9px] font-medium uppercase tracking-wider text-emerald-700 dark:text-emerald-300\">{link.badge}</span>}\n                          </span>\n                          {link.description && <span className=\"mt-1 block max-w-[240px] text-[13px] leading-[1.45] text-[#5c6769] dark:text-[#969fa0]\">{link.description}</span>}\n                        </LinkComponent>\n                      </li>\n                    ))}\n                  </ul>\n                </div>\n              ))}\n            </div>\n            {renderVisual && <aside className=\"min-w-0\">{renderVisual(openedGroup, close)}</aside>}\n            </div>\n            {openedGroup.footer && openedGroup.footer.length > 0 && (\n              <div className=\"mt-7 grid gap-3 pt-5 sm:grid-cols-2\">\n                {openedGroup.footer.map((link) => (\n                  <LinkComponent key={link.href} href={link.href} onClick={close} className=\"group/link flex items-center justify-between gap-4 rounded-[2px] px-2 py-2 text-sm font-medium text-[#202626] dark:text-[#f4f5f5] transition-colors hover:bg-black/[0.045] dark:hover:bg-white/[0.055] hover:text-emerald-700 dark:hover:text-emerald-300\">\n                    <span><span className=\"block\">{link.label}</span>{link.description && <span className=\"mt-1 block text-xs font-normal text-[#5c6769] dark:text-[#969fa0]\">{link.description}</span>}</span>\n                    <ArrowUpRight aria-hidden=\"true\" className=\"size-4 shrink-0\" />\n                  </LinkComponent>\n                ))}\n              </div>\n            )}\n            </div>\n          </div>\n        </div>\n      )}\n    </div>\n  )\n}\n\nexport default CaiMegaMenu\n",
      "type": "registry:ui"
    }
  ]
}