{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "menubar",
  "title": "Menubar",
  "description": "A desktop-style app menubar with File/Edit/View-style dropdown menus, checkbox and radio items, submenus and full arrow-key navigation between and within menus.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/menubar/menubar.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { createPortal } from \"react-dom\";\nimport { motion, AnimatePresence } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\n// useLayoutEffect logs a warning when it runs server-side (no DOM to\n// measure before paint there); fall back to useEffect there and only\n// take the synchronous-before-paint measurement in the browser.\nconst useIsomorphicLayoutEffect = typeof window !== \"undefined\" ? React.useLayoutEffect : React.useEffect;\n\n// Scrolls a focused row into view within its own menu panel only. Plain\n// scrollIntoView can walk up to the document and scroll the whole page\n// (including horizontally, to reveal a flyout submenu near the viewport\n// edge), which visibly shifts unrelated content — so this only ever\n// touches the panel's own scrollTop.\nfunction scrollRowIntoView(el: HTMLElement | null) {\n  if (!el) return;\n  const panel = el.closest('[role=\"menu\"]') as HTMLElement | null;\n  if (!panel) return;\n  const panelRect = panel.getBoundingClientRect();\n  const rowRect = el.getBoundingClientRect();\n  if (rowRect.top < panelRect.top) panel.scrollTop -= panelRect.top - rowRect.top;\n  else if (rowRect.bottom > panelRect.bottom) panel.scrollTop += rowRect.bottom - panelRect.bottom;\n}\n\nexport type MenubarItemKind = \"item\" | \"checkbox\" | \"radio\" | \"separator\" | \"label\" | \"submenu\";\nexport type MenubarSize = \"sm\" | \"md\" | \"lg\";\n\nexport interface MenubarItem {\n  id: string;\n  kind: MenubarItemKind;\n  label?: string;\n  shortcut?: string;\n  checked?: boolean;\n  disabled?: boolean;\n  danger?: boolean;\n  items?: MenubarItem[];\n}\n\nexport interface MenubarMenu {\n  id: string;\n  label: string;\n  items: MenubarItem[];\n  disabled?: boolean;\n}\n\nexport interface MenubarProps {\n  menus: MenubarMenu[];\n  onSelect?: (menuId: string, item: MenubarItem) => void;\n  size?: MenubarSize;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.5)\", faint: \"rgba(245,244,241,0.28)\", bar: \"rgba(255,255,255,0.03)\", barBorder: \"rgba(255,255,255,0.08)\", hover: \"rgba(255,255,255,0.08)\", active: \"rgba(255,255,255,0.12)\", panelBg: \"#0E0E0E\", panelBorder: \"rgba(255,255,255,0.1)\", separator: \"rgba(255,255,255,0.08)\", kbdBg: \"rgba(255,255,255,0.06)\", kbdBorder: \"rgba(255,255,255,0.1)\", danger: \"#FF7A6B\", check: \"#F5F4F1\", checkFg: \"#0A0A0A\", shadow: \"0 20px 50px rgba(0,0,0,0.55)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.5)\", faint: \"rgba(10,10,10,0.28)\", bar: \"#FFFFFF\", barBorder: \"rgba(10,10,10,0.1)\", hover: \"rgba(10,10,10,0.06)\", active: \"rgba(10,10,10,0.08)\", panelBg: \"#FFFFFF\", panelBorder: \"rgba(10,10,10,0.12)\", separator: \"rgba(10,10,10,0.08)\", kbdBg: \"rgba(10,10,10,0.04)\", kbdBorder: \"rgba(10,10,10,0.1)\", danger: \"#E5484D\", check: \"#0A0A0A\", checkFg: \"#FFFFFF\", shadow: \"0 18px 44px rgba(0,0,0,0.16)\" },\n};\n\nconst SIZES: Record<MenubarSize, { font: number; triggerH: number; padX: number; rowH: number; radius: number }> = {\n  sm: { font: 13, triggerH: 30, padX: 10, rowH: 30, radius: 9 },\n  md: { font: 14, triggerH: 34, padX: 12, rowH: 33, radius: 10 },\n  lg: { font: 15, triggerH: 38, padX: 14, rowH: 36, radius: 11 },\n};\n\ntype Palette = (typeof PALETTES)[\"dark\"];\n\nfunction isFocusable(item: MenubarItem) {\n  return item.kind !== \"separator\" && item.kind !== \"label\" && !item.disabled;\n}\n\nfunction Kbd({ children, p }: { children: string; p: Palette }) {\n  return (\n    <span className=\"inline-flex shrink-0 items-center justify-center whitespace-nowrap rounded-md font-medium\" style={{ height: 18, padding: \"0 5px\", background: p.kbdBg, border: `1px solid ${p.kbdBorder}`, color: p.muted, fontSize: 10.5 }}>\n      {children}\n    </span>\n  );\n}\n\nfunction CheckGlyph({ p }: { p: Palette }) {\n  return (\n    <svg width=\"9\" height=\"9\" viewBox=\"0 0 12 12\" fill=\"none\" aria-hidden=\"true\">\n      <path d=\"M2.5 6.3L4.8 8.6L9.5 3.5\" stroke={p.checkFg} strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n    </svg>\n  );\n}\n\ninterface RowProps {\n  item: MenubarItem;\n  s: (typeof SIZES)[\"md\"];\n  p: Palette;\n  focused: boolean;\n  submenuOpen: boolean;\n  onHover: () => void;\n  onActivate: () => void;\n  setRef: (el: HTMLDivElement | null) => void;\n}\n\nfunction Row({ item, s, p, focused, submenuOpen, onHover, onActivate, setRef }: RowProps) {\n  if (item.kind === \"separator\") return <div role=\"separator\" style={{ margin: \"5px 8px\", height: 1, background: p.separator }} />;\n  if (item.kind === \"label\")\n    return (\n      <div className=\"font-semibold uppercase\" style={{ padding: \"7px 12px 4px\", color: p.faint, fontSize: 10.5, letterSpacing: \"0.07em\" }}>\n        {item.label}\n      </div>\n    );\n\n  const isCheck = item.kind === \"checkbox\" || item.kind === \"radio\";\n  return (\n    <div\n      ref={setRef}\n      role={item.kind === \"checkbox\" ? \"menuitemcheckbox\" : item.kind === \"radio\" ? \"menuitemradio\" : \"menuitem\"}\n      aria-checked={isCheck ? Boolean(item.checked) : undefined}\n      aria-haspopup={item.kind === \"submenu\" ? \"menu\" : undefined}\n      aria-expanded={item.kind === \"submenu\" ? submenuOpen : undefined}\n      aria-disabled={item.disabled || undefined}\n      tabIndex={-1}\n      onMouseEnter={item.disabled ? undefined : onHover}\n      onClick={item.disabled ? undefined : onActivate}\n      className=\"mx-1.5 flex cursor-pointer items-center outline-none select-none\"\n      style={{ gap: 9, height: s.rowH, padding: `0 10px`, borderRadius: s.radius - 2, background: focused || submenuOpen ? p.hover : \"transparent\", color: item.disabled ? p.faint : item.danger ? p.danger : p.text, fontSize: s.font - 0.5, fontWeight: 500, cursor: item.disabled ? \"default\" : \"pointer\" }}\n    >\n      {item.kind === \"checkbox\" && (\n        <span className=\"flex shrink-0 items-center justify-center rounded\" style={{ width: 14, height: 14, border: `1.5px solid ${item.checked ? p.check : p.barBorder}`, background: item.checked ? p.check : \"transparent\" }}>\n          {item.checked && <CheckGlyph p={p} />}\n        </span>\n      )}\n      {item.kind === \"radio\" && (\n        <span className=\"flex shrink-0 items-center justify-center rounded-full\" style={{ width: 14, height: 14, border: `1.5px solid ${item.checked ? p.check : p.barBorder}` }}>\n          {item.checked && <span style={{ width: 6, height: 6, borderRadius: 999, background: p.check }} />}\n        </span>\n      )}\n      <span className=\"min-w-0 flex-1 truncate\">{item.label}</span>\n      {item.shortcut && <Kbd p={p}>{item.shortcut}</Kbd>}\n      {item.kind === \"submenu\" && (\n        <svg width=\"13\" height=\"13\" viewBox=\"0 0 16 16\" fill=\"none\" stroke={p.muted} strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n          <path d=\"M6 3.5L10.5 8L6 12.5\" />\n        </svg>\n      )}\n    </div>\n  );\n}\n\ninterface MenuListProps {\n  items: MenubarItem[];\n  s: (typeof SIZES)[\"md\"];\n  p: Palette;\n  width: number;\n  placement: \"below\" | \"right\";\n  /** Viewport rect of the row that opened this flyout; required (and only used) when placement is \"right\", since that panel is portaled to the body to escape the parent panel's own scroll clipping. */\n  anchorRect?: DOMRect | null;\n  autoFocus: \"first\" | \"last\" | \"none\";\n  onActivate: (item: MenubarItem) => void;\n  onClose: () => void;\n  onCloseAll: () => void;\n  onSiblingNav?: (dir: 1 | -1) => void;\n}\n\nfunction MenuList({ items, s, p, width, placement, anchorRect, autoFocus, onActivate, onClose, onCloseAll, onSiblingNav }: MenuListProps) {\n  const focusableIds = items.filter(isFocusable).map((n) => n.id);\n  const [focusedId, setFocusedId] = React.useState<string | null>(autoFocus === \"last\" ? (focusableIds[focusableIds.length - 1] ?? null) : (focusableIds[0] ?? null));\n  const [openSubmenu, setOpenSubmenu] = React.useState<string | null>(null);\n  const [submenuAnchor, setSubmenuAnchor] = React.useState<DOMRect | null>(null);\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const rowRefs = React.useRef<Record<string, HTMLDivElement | null>>({});\n\n  useIsomorphicLayoutEffect(() => {\n    setSubmenuAnchor(openSubmenu ? (rowRefs.current[openSubmenu]?.getBoundingClientRect() ?? null) : null);\n  }, [openSubmenu]);\n\n  React.useEffect(() => {\n    // For a \"right\" flyout, the panel renders nothing (see the early return\n    // below) until anchorRect is measured, so rootRef.current is still null\n    // on this effect's first pass; re-running once anchorRect lands makes\n    // sure focus actually reaches the now-real DOM node.\n    if (autoFocus !== \"none\") rootRef.current?.focus({ preventScroll: true });\n  }, [autoFocus, anchorRect]);\n\n  const move = (dir: 1 | -1) => {\n    if (focusableIds.length === 0) return;\n    const i = focusedId ? focusableIds.indexOf(focusedId) : -1;\n    const next = (i + dir + focusableIds.length) % focusableIds.length;\n    setFocusedId(focusableIds[next]);\n  };\n\n  const activate = (item: MenubarItem) => {\n    if (item.disabled) return;\n    if (item.kind === \"submenu\") {\n      setOpenSubmenu(item.id);\n      return;\n    }\n    onActivate(item);\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent) => {\n    if (openSubmenu !== null) {\n      if (e.key === \"Escape\") {\n        e.preventDefault();\n        setOpenSubmenu(null);\n      }\n      return;\n    }\n    const focusedItem = items.find((n) => n.id === focusedId);\n    if (e.key === \"ArrowDown\") {\n      e.preventDefault();\n      move(1);\n    } else if (e.key === \"ArrowUp\") {\n      e.preventDefault();\n      move(-1);\n    } else if (e.key === \"ArrowRight\") {\n      if (focusedItem?.kind === \"submenu\") {\n        e.preventDefault();\n        setOpenSubmenu(focusedItem.id);\n      } else if (placement === \"below\" && onSiblingNav) {\n        e.preventDefault();\n        onSiblingNav(1);\n      }\n    } else if (e.key === \"ArrowLeft\") {\n      if (placement === \"right\") {\n        e.preventDefault();\n        onClose();\n      } else if (onSiblingNav) {\n        e.preventDefault();\n        onSiblingNav(-1);\n      }\n    } else if (e.key === \"Enter\" || e.key === \" \") {\n      e.preventDefault();\n      if (focusedItem) activate(focusedItem);\n    } else if (e.key === \"Escape\") {\n      e.preventDefault();\n      onCloseAll();\n    } else if (e.key === \"Home\") {\n      e.preventDefault();\n      if (focusableIds.length) setFocusedId(focusableIds[0]);\n    } else if (e.key === \"End\") {\n      e.preventDefault();\n      if (focusableIds.length) setFocusedId(focusableIds[focusableIds.length - 1]);\n    }\n  };\n\n  React.useEffect(() => {\n    if (focusedId) scrollRowIntoView(rowRefs.current[focusedId]);\n  }, [focusedId]);\n\n  // A flyout (placement \"right\") is portaled to the body and positioned in\n  // fixed viewport coordinates from anchorRect, instead of CSS-anchoring to\n  // its trigger row: that row lives inside a panel with overflow-y:auto,\n  // and a browser can't scroll one axis while leaving the other visible —\n  // overflow-x would get forced to hidden too and clip the flyout.\n  if (placement === \"right\" && !anchorRect) return null;\n\n  const node = (\n    <motion.div\n      ref={rootRef}\n      role=\"menu\"\n      tabIndex={-1}\n      onKeyDown={onKeyDown}\n      className={placement === \"below\" ? \"absolute overflow-hidden outline-none\" : \"fixed overflow-hidden outline-none\"}\n      style={{\n        top: placement === \"below\" ? \"calc(100% + 6px)\" : (anchorRect?.top ?? 0) - 8,\n        left: placement === \"below\" ? 0 : (anchorRect?.right ?? 0) + 6,\n        width,\n        maxHeight: 360,\n        overflowY: \"auto\",\n        padding: 6,\n        borderRadius: s.radius + 4,\n        background: p.panelBg,\n        border: `1px solid ${p.panelBorder}`,\n        boxShadow: p.shadow,\n        zIndex: 50,\n      }}\n      initial={{ opacity: 0, scale: 0.96, y: placement === \"below\" ? -4 : 0 }}\n      animate={{ opacity: 1, scale: 1, y: 0 }}\n      exit={{ opacity: 0, scale: 0.97 }}\n      transition={{ duration: 0.14, ease: [0.16, 1, 0.3, 1] }}\n    >\n      {items.map((item) => (\n        <div key={item.id} className=\"relative\">\n          <Row\n            item={item}\n            s={s}\n            p={p}\n            focused={item.id === focusedId}\n            submenuOpen={item.id === openSubmenu}\n            onHover={() => {\n              setFocusedId(item.id);\n              setOpenSubmenu(item.kind === \"submenu\" ? item.id : null);\n            }}\n            onActivate={() => activate(item)}\n            setRef={(el) => {\n              rowRefs.current[item.id] = el;\n            }}\n          />\n          <AnimatePresence>\n            {item.id === openSubmenu && item.items && (\n              <MenuList\n                items={item.items}\n                s={s}\n                p={p}\n                width={width}\n                placement=\"right\"\n                anchorRect={submenuAnchor}\n                autoFocus=\"first\"\n                onActivate={onActivate}\n                onClose={() => {\n                  setOpenSubmenu(null);\n                  rootRef.current?.focus({ preventScroll: true });\n                }}\n                onCloseAll={onCloseAll}\n              />\n            )}\n          </AnimatePresence>\n        </div>\n      ))}\n    </motion.div>\n  );\n\n  return placement === \"right\" ? createPortal(node, document.body) : node;\n}\n\nexport function Menubar({ menus, onSelect, size = \"md\", theme = \"dark\", className }: MenubarProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const triggerRefs = React.useRef<Record<string, HTMLButtonElement | null>>({});\n  const [openId, setOpenId] = React.useState<string | null>(null);\n  const [focusIndex, setFocusIndex] = React.useState(0);\n  const enabledIndexes = menus.map((m, i) => (m.disabled ? -1 : i)).filter((i) => i >= 0);\n\n  const closeAll = (returnFocus = true) => {\n    setOpenId(null);\n    if (returnFocus) {\n      const id = menus[focusIndex]?.id;\n      if (id) triggerRefs.current[id]?.focus({ preventScroll: true });\n    }\n  };\n\n  React.useEffect(() => {\n    if (!openId) return;\n    const onDown = (e: MouseEvent) => {\n      if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpenId(null);\n    };\n    document.addEventListener(\"mousedown\", onDown);\n    return () => document.removeEventListener(\"mousedown\", onDown);\n  }, [openId]);\n\n  const openMenuAt = (index: number) => {\n    const menu = menus[index];\n    if (!menu || menu.disabled) return;\n    setFocusIndex(index);\n    setOpenId(menu.id);\n  };\n\n  const siblingNav = (dir: 1 | -1) => {\n    if (enabledIndexes.length === 0) return;\n    const pos = enabledIndexes.indexOf(focusIndex);\n    const next = enabledIndexes[(pos === -1 ? 0 : pos + dir + enabledIndexes.length) % enabledIndexes.length];\n    openMenuAt(next);\n  };\n\n  const onTriggerKeyDown = (e: React.KeyboardEvent, index: number) => {\n    if (e.key === \"ArrowRight\") {\n      e.preventDefault();\n      const pos = enabledIndexes.indexOf(index);\n      const next = enabledIndexes[(pos + 1) % enabledIndexes.length];\n      setFocusIndex(next);\n      triggerRefs.current[menus[next].id]?.focus({ preventScroll: true });\n      if (openId) openMenuAt(next);\n    } else if (e.key === \"ArrowLeft\") {\n      e.preventDefault();\n      const pos = enabledIndexes.indexOf(index);\n      const next = enabledIndexes[(pos - 1 + enabledIndexes.length) % enabledIndexes.length];\n      setFocusIndex(next);\n      triggerRefs.current[menus[next].id]?.focus({ preventScroll: true });\n      if (openId) openMenuAt(next);\n    } else if (e.key === \"ArrowDown\" || e.key === \"Enter\" || e.key === \" \") {\n      e.preventDefault();\n      openMenuAt(index);\n    } else if (e.key === \"Escape\") {\n      setOpenId(null);\n    }\n  };\n\n  const handleActivate = (menuId: string) => (item: MenubarItem) => {\n    onSelect?.(menuId, item);\n    closeAll();\n  };\n\n  return (\n    <div\n      ref={rootRef}\n      role=\"menubar\"\n      aria-orientation=\"horizontal\"\n      className={cn(\"inline-flex items-center\", className)}\n      style={{ gap: 2, padding: 4, borderRadius: s.radius + 4, background: p.bar, border: `1px solid ${p.barBorder}`, fontFamily: \"Inter, sans-serif\" }}\n    >\n      {menus.map((menu, index) => {\n        const isOpen = openId === menu.id;\n        return (\n          <div key={menu.id} className=\"relative\">\n            <button\n              ref={(el) => {\n                triggerRefs.current[menu.id] = el;\n              }}\n              type=\"button\"\n              role=\"menuitem\"\n              aria-haspopup=\"menu\"\n              aria-expanded={isOpen}\n              disabled={menu.disabled}\n              tabIndex={index === focusIndex ? 0 : -1}\n              onClick={() => (isOpen ? setOpenId(null) : openMenuAt(index))}\n              onMouseEnter={() => openId && !menu.disabled && openMenuAt(index)}\n              onKeyDown={(e) => onTriggerKeyDown(e, index)}\n              className=\"cursor-pointer border-none bg-transparent font-medium outline-none focus-visible:ring-2 disabled:cursor-not-allowed disabled:opacity-40\"\n              style={{ height: s.triggerH, padding: `0 ${s.padX}px`, borderRadius: s.radius, color: p.text, fontSize: s.font, background: isOpen ? p.active : \"transparent\", [\"--tw-ring-color\" as string]: p.text }}\n              onMouseDown={(e) => e.preventDefault()}\n            >\n              {menu.label}\n            </button>\n            <AnimatePresence>\n              {isOpen && (\n                <MenuList\n                  items={menu.items}\n                  s={s}\n                  p={p}\n                  width={220}\n                  placement=\"below\"\n                  autoFocus=\"first\"\n                  onActivate={handleActivate(menu.id)}\n                  onClose={() => setOpenId(null)}\n                  onCloseAll={() => closeAll()}\n                  onSiblingNav={siblingNav}\n                />\n              )}\n            </AnimatePresence>\n          </div>\n        );\n      })}\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}