{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"motion-navbar","type":"registry:ui","title":"Motion Navbar","description":"Site navbar with a hover pill that glides between links and a single dropdown that morphs its size and position between menus.","dependencies":["motion","react-use-measure"],"registryDependencies":[],"files":[{"path":"registry/ui/motion-navbar.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef, useState, type FocusEvent, type KeyboardEvent, type ReactNode } from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport useMeasure from \"react-use-measure\";\n\nexport interface MotionNavbarItem {\n  label: string;\n  /** Plain link when there is no dropdown. */\n  href?: string;\n  /** Dropdown panel. */\n  content?: ReactNode;\n}\n\nexport interface MotionNavbarProps {\n  items: MotionNavbarItem[];\n  /** Left slot (brand). */\n  logo?: ReactNode;\n  /** Right slot (buttons). */\n  actions?: ReactNode;\n  /** Spring bounce of the highlight and dropdown morph, 0 to 0.5. */\n  bounce?: number;\n  /** Seconds for the highlight glide and dropdown morph. */\n  duration?: number;\n  /** Ms of grace before the dropdown closes after the pointer leaves. */\n  closeDelay?: number;\n  className?: string;\n}\n\n/**\n * Navbar with a hover pill that glides between items and one shared dropdown that morphs\n * its width, height and position as you move between menus, sliding content the way you went.\n */\nexport function MotionNavbar({ items, logo, actions, bounce = 0.15, duration = 0.4, closeDelay = 150, className }: MotionNavbarProps) {\n  const [hl, setHl] = useState<{ left: number; width: number } | null>(null);\n  const [open, setOpen] = useState<number | null>(null);\n  const [dir, setDir] = useState(0);\n  const [anchor, setAnchor] = useState({ center: 0, width: 0 });\n  const [ref, bounds] = useMeasure({ offsetSize: true });\n  const reduce = useReducedMotion();\n  const nav = useRef<HTMLElement>(null);\n  const panel = useRef<HTMLDivElement>(null);\n  const triggers = useRef<(HTMLElement | null)[]>([]);\n  const timer = useRef<ReturnType<typeof setTimeout>>(undefined);\n\n  useEffect(() => () => clearTimeout(timer.current), []);\n\n  const cancelClose = () => clearTimeout(timer.current);\n  const scheduleClose = () => {\n    clearTimeout(timer.current);\n    timer.current = setTimeout(() => setOpen(null), closeDelay);\n  };\n\n  const show = (i: number) => {\n    cancelClose();\n    const el = triggers.current[i];\n    const n = nav.current;\n    if (el && n) {\n      const r = el.getBoundingClientRect();\n      const nr = n.getBoundingClientRect();\n      setHl({ left: el.offsetLeft, width: el.offsetWidth });\n      if (items[i].content) setAnchor({ center: r.left + r.width / 2 - nr.left, width: nr.width });\n    }\n    if (!items[i].content) {\n      setOpen(null);\n      return;\n    }\n    if (open !== null && open !== i) setDir(i > open ? 1 : -1);\n    else if (open === null) setDir(0);\n    setOpen(i);\n  };\n\n  const focusPanel = () =>\n    requestAnimationFrame(() => panel.current?.querySelector<HTMLElement>(\"a, button, input, [tabindex]:not([tabindex='-1'])\")?.focus());\n\n  const onTriggerKey = (e: KeyboardEvent, i: number) => {\n    if (!items[i].content) return;\n    if (e.key === \"ArrowDown\" || e.key === \"Enter\" || e.key === \" \") {\n      e.preventDefault();\n      show(i);\n      focusPanel();\n    }\n  };\n\n  const onKey = (e: KeyboardEvent) => {\n    if (e.key === \"Escape\" && open !== null) {\n      triggers.current[open]?.focus();\n      setOpen(null);\n    }\n  };\n\n  const onBlur = (e: FocusEvent) => {\n    if (!nav.current?.contains(e.relatedTarget as Node)) {\n      setOpen(null);\n      setHl(null);\n    }\n  };\n\n  const spring = reduce ? { duration: 0 } : { type: \"spring\" as const, bounce, duration };\n  const current = open !== null ? items[open] : null;\n  // Center the panel under its trigger, but keep it inside the bar when it fits.\n  const half = bounds.width / 2;\n  const left = half && bounds.width <= anchor.width ? Math.min(Math.max(anchor.center, half), anchor.width - half) : anchor.center;\n\n  return (\n    <nav\n      ref={nav}\n      aria-label=\"Main\"\n      onPointerEnter={cancelClose}\n      onPointerLeave={() => {\n        scheduleClose();\n        setHl(null);\n      }}\n      onKeyDown={onKey}\n      onBlur={onBlur}\n      className={`relative w-full ${className ?? \"\"}`}\n    >\n      <div className=\"flex h-14 items-center gap-4 rounded-2xl border border-border bg-card/80 px-3 shadow-[0_20px_50px_-30px_rgba(0,0,0,.8)] backdrop-blur-xl\">\n        {logo && <div className=\"shrink-0 pl-1\">{logo}</div>}\n        <ul className=\"relative mx-auto flex items-center\">\n          <AnimatePresence>\n            {hl && (\n              <motion.li\n                aria-hidden\n                initial={{ opacity: 0, left: hl.left, width: hl.width }}\n                animate={{ opacity: 1, left: hl.left, width: hl.width }}\n                exit={{ opacity: 0 }}\n                transition={spring}\n                className=\"pointer-events-none absolute inset-y-0 rounded-lg bg-muted\"\n              />\n            )}\n          </AnimatePresence>\n          {items.map((item, i) => {\n            const cls = `relative flex h-9 items-center gap-1 rounded-lg px-3.5 text-sm outline-none transition-colors focus-visible:ring-2 focus-visible:ring-ring ${\n              open === i ? \"text-foreground\" : \"text-muted-foreground hover:text-foreground\"\n            }`;\n            const common = {\n              ref: (el: HTMLElement | null) => {\n                triggers.current[i] = el;\n              },\n              onPointerEnter: () => show(i),\n              onFocus: () => setHl({ left: triggers.current[i]?.offsetLeft ?? 0, width: triggers.current[i]?.offsetWidth ?? 0 }),\n              className: cls,\n            };\n            return (\n              <li key={item.label}>\n                {item.content ? (\n                  <button\n                    type=\"button\"\n                    aria-expanded={open === i}\n                    aria-haspopup=\"true\"\n                    onClick={() => (open === i ? setOpen(null) : show(i))}\n                    onKeyDown={(e) => onTriggerKey(e, i)}\n                    {...common}\n                  >\n                    {item.label}\n                    <motion.svg\n                      aria-hidden\n                      viewBox=\"0 0 24 24\"\n                      className=\"size-3.5 opacity-60\"\n                      fill=\"none\"\n                      stroke=\"currentColor\"\n                      strokeWidth={2.2}\n                      strokeLinecap=\"round\"\n                      animate={{ rotate: open === i ? 180 : 0 }}\n                      transition={spring}\n                    >\n                      <path d=\"m6 9 6 6 6-6\" />\n                    </motion.svg>\n                  </button>\n                ) : (\n                  <a href={item.href ?? \"#\"} {...common}>\n                    {item.label}\n                  </a>\n                )}\n              </li>\n            );\n          })}\n        </ul>\n        {actions && <div className=\"flex shrink-0 items-center gap-2\">{actions}</div>}\n      </div>\n\n      <AnimatePresence>\n        {current?.content && (\n          <motion.div\n            key=\"dropdown\"\n            initial={{ opacity: 0, y: -8, scale: 0.97 }}\n            animate={{ opacity: 1, y: 0, scale: 1 }}\n            exit={{ opacity: 0, y: -8, scale: 0.97 }}\n            transition={reduce ? { duration: 0 } : { duration: 0.2, ease: [0.2, 0.7, 0.2, 1] }}\n            className=\"absolute left-0 top-full z-50 pt-2\"\n            style={{ transformOrigin: \"top center\" }}\n          >\n            <motion.div\n              ref={panel}\n              initial={false}\n              animate={{ left, width: bounds.width || \"auto\", height: bounds.height || \"auto\" }}\n              transition={spring}\n              style={{ translate: \"-50% 0\" }}\n              className=\"relative overflow-hidden rounded-2xl border border-border bg-card/95 shadow-[0_30px_70px_-25px_rgba(0,0,0,.8)] backdrop-blur-xl\"\n            >\n              <div ref={ref} className=\"w-max\">\n                <AnimatePresence mode=\"popLayout\" initial={false} custom={dir}>\n                  <motion.div\n                    key={open}\n                    custom={dir}\n                    variants={{\n                      enter: (d: number) => ({ opacity: 0, x: reduce ? 0 : d * 60 }),\n                      center: { opacity: 1, x: 0 },\n                      exit: (d: number) => ({ opacity: 0, x: reduce ? 0 : d * -60 }),\n                    }}\n                    initial=\"enter\"\n                    animate=\"center\"\n                    exit=\"exit\"\n                    transition={reduce ? { duration: 0 } : { duration: duration * 0.75, ease: [0.2, 0.7, 0.2, 1] }}\n                  >\n                    {current.content}\n                  </motion.div>\n                </AnimatePresence>\n              </div>\n            </motion.div>\n          </motion.div>\n        )}\n      </AnimatePresence>\n    </nav>\n  );\n}\n"}]}