{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"morphing-hamburger-icon","type":"registry:component","title":"Morphing Hamburger Icon","description":"A menu button whose three bars morph on springs into a close X, a back arrow or a minus, with a lit pill, press squash and a live panel in the demo. Inherits currentColor, controlled or uncontrolled, no dependencies.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/morphing-hamburger-icon.tsx","content":"\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport type { ButtonHTMLAttributes, MouseEvent } from \"react\";\n\ntype MenuMorph = \"x\" | \"arrow\" | \"minus\";\ntype Pose = { x: number; y: number; r: number; s: number; o: number };\ntype PoseSet = { t: Pose; m: Pose; b: Pose };\ntype Spring = { x: number; v: number };\n\nexport type MorphingMenuButtonProps = Omit<ButtonHTMLAttributes<HTMLButtonElement>, \"color\" | \"children\"> & {\n  /** Width of the three-bar icon in px; the button is size + 24. */\n  size?: number;\n  /** \"ink\" paints the bars with currentColor, \"red\" with a fixed red. */\n  color?: \"ink\" | \"red\";\n  /** Id of the panel this button controls (aria-controls). */\n  menuId?: string;\n  /** What the bars become when open. */\n  morph?: MenuMorph;\n  /** Controlled open state. Leave undefined for uncontrolled. */\n  open?: boolean;\n  defaultOpen?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  openLabel?: string;\n  closeLabel?: string;\n};\n\nconst clamp01 = (v: number) => (v < 0 ? 0 : v > 1 ? 1 : v);\nconst lerp = (a: number, b: number, t: number) => a + (b - a) * t;\nconst r2 = (v: number) => Math.round(v * 100) / 100;\nconst COS45 = 0.7071;\n\nfunction closedSet(w: number): PoseSet {\n  const g = w * 0.3;\n  return {\n    t: { x: 0, y: -g, r: 0, s: 1, o: 1 },\n    m: { x: 0, y: 0, r: 0, s: 1, o: 1 },\n    b: { x: 0, y: g, r: 0, s: 1, o: 1 },\n  };\n}\n\nfunction endSet(morph: MenuMorph, w: number): PoseSet {\n  if (morph === \"arrow\") {\n    const len = 0.56;\n    const tip = -0.47 * w;\n    const c = ((len * w) / 2) * COS45;\n    return {\n      t: { x: tip + c, y: -c, r: -45, s: len, o: 1 },\n      m: { x: 0, y: 0, r: 0, s: 1, o: 1 },\n      b: { x: tip + c, y: c, r: 45, s: len, o: 1 },\n    };\n  }\n  if (morph === \"minus\") {\n    return {\n      t: { x: 0, y: 0, r: 0, s: 1, o: 0 },\n      m: { x: 0, y: 0, r: 0, s: 0.72, o: 1 },\n      b: { x: 0, y: 0, r: 0, s: 1, o: 0 },\n    };\n  }\n  return {\n    t: { x: 0, y: 0, r: 45, s: 0.88, o: 1 },\n    m: { x: 0, y: 0, r: 0, s: 0.2, o: 0 },\n    b: { x: 0, y: 0, r: -45, s: 0.88, o: 1 },\n  };\n}\n\nfunction barFrame(a: Pose, b: Pose, p: number, hoverScale: number, w: number) {\n  const po = clamp01(p * 1.4);\n  const s = lerp(a.s, b.s, p) * hoverScale;\n  const x = lerp(a.x, b.x, p) + (1 - hoverScale) * (w / 2) * (1 - clamp01(p));\n  const y = lerp(a.y, b.y, p);\n  const r = lerp(a.r, b.r, p);\n  const o = lerp(a.o, b.o, po);\n  return {\n    transform: \"translate(\" + r2(x) + \"px,\" + r2(y) + \"px) rotate(\" + r2(r) + \"deg) scaleX(\" + r2(s * 1000) / 1000 + \")\",\n    opacity: String(r2(o * 100) / 100),\n  };\n}\n\nfunction frames(w: number, e: PoseSet, pt: number, pm: number, pb: number, hv: number) {\n  const c = closedSet(w);\n  return {\n    t: barFrame(c.t, e.t, pt, 1, w),\n    m: barFrame(c.m, e.m, pm, 1 - 0.3 * hv * (1 - clamp01(pm)), w),\n    b: barFrame(c.b, e.b, pb, 1, w),\n  };\n}\n\nconst SPRINGS = {\n  reduced: { t: [900, 60], m: [900, 60], b: [900, 60], hv: [900, 60], pr: [900, 60] },\n  alive: { t: [230, 18], m: [340, 29], b: [195, 16], hv: [240, 22], pr: [420, 27] },\n} as const;\n\nexport function MorphingMenuButton({\n  size = 28,\n  menuId = \"morphing-menu-panel\",\n  color = \"ink\",\n  morph = \"x\",\n  open: openProp,\n  defaultOpen = false,\n  onOpenChange,\n  openLabel = \"Open menu\",\n  closeLabel = \"Close menu\",\n  className = \"\",\n  style,\n  onClick,\n  onPointerMove,\n  onPointerDown,\n  ...rest\n}: MorphingMenuButtonProps) {\n  const [inner, setInner] = useState(defaultOpen);\n  const controlled = openProp !== undefined;\n  const open = controlled ? openProp : inner;\n\n  const btnRef = useRef<HTMLButtonElement>(null);\n  const iconRef = useRef<HTMLSpanElement>(null);\n  const topRef = useRef<HTMLSpanElement>(null);\n  const midRef = useRef<HTMLSpanElement>(null);\n  const botRef = useRef<HTMLSpanElement>(null);\n  const live = useRef({ open, morph, size, hover: false, press: false });\n  const wake = useRef<(() => void) | null>(null);\n\n  // First-render bar styles, frozen so React never rewrites what the loop owns.\n  const [init] = useState(() => frames(size, endSet(morph, size), open ? 1 : 0, open ? 1 : 0, open ? 1 : 0, 0));\n\n  useEffect(() => {\n    live.current.open = open;\n    live.current.morph = morph;\n    live.current.size = size;\n    wake.current?.();\n  }, [open, morph, size]);\n\n  useEffect(() => {\n    const btn = btnRef.current;\n    const icon = iconRef.current;\n    const top = topRef.current;\n    const mid = midRef.current;\n    const bot = botRef.current;\n    if (!btn || !icon || !top || !mid || !bot) return;\n    const L = live.current;\n    const start = L.open ? 1 : 0;\n    const sp = {\n      t: { x: start, v: 0 } as Spring,\n      m: { x: start, v: 0 } as Spring,\n      b: { x: start, v: 0 } as Spring,\n      hv: { x: 0, v: 0 } as Spring,\n      pr: { x: 0, v: 0 } as Spring,\n    };\n    const cur = endSet(L.morph, L.size);\n    const mq = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    let reduce = mq.matches;\n    let raf = 0;\n    let last = 0;\n    let dirty = true;\n\n    const step = (s: Spring, target: number, k: number, c: number, dt: number) => {\n      const n = Math.max(1, Math.ceil(dt * 120));\n      const h = dt / n;\n      for (let i = 0; i < n; i++) {\n        s.v += (-k * (s.x - target) - c * s.v) * h;\n        s.x += s.v * h;\n      }\n      if (Math.abs(s.x - target) < 5e-4 && Math.abs(s.v) < 2e-3) {\n        s.x = target;\n        s.v = 0;\n        return false;\n      }\n      return true;\n    };\n    const ease = (poseA: Pose, poseB: Pose, a: number) => {\n      let moving = false;\n      for (const key of [\"x\", \"y\", \"r\", \"s\", \"o\"] as const) {\n        const d = poseB[key] - poseA[key];\n        if (Math.abs(d) < 1e-3) poseA[key] = poseB[key];\n        else {\n          poseA[key] += d * a;\n          moving = true;\n        }\n      }\n      return moving;\n    };\n\n    const frame = (ts: number) => {\n      raf = 0;\n      const dt = last ? Math.min(0.05, (ts - last) / 1000) : 1 / 60;\n      last = ts;\n      const P = reduce ? SPRINGS.reduced : SPRINGS.alive;\n      const target = L.open ? 1 : 0;\n      let moving = dirty;\n      dirty = false;\n      if (step(sp.t, target, P.t[0], P.t[1], dt)) moving = true;\n      if (step(sp.m, target, P.m[0], P.m[1], dt)) moving = true;\n      if (step(sp.b, target, P.b[0], P.b[1], dt)) moving = true;\n      if (step(sp.hv, L.hover && !reduce ? 1 : 0, P.hv[0], P.hv[1], dt)) moving = true;\n      if (step(sp.pr, L.press && !reduce ? 1 : 0, P.pr[0], P.pr[1], dt)) moving = true;\n      const goal = endSet(L.morph, L.size);\n      const a = reduce ? 1 : 1 - Math.exp(-14 * dt);\n      if (ease(cur.t, goal.t, a)) moving = true;\n      if (ease(cur.m, goal.m, a)) moving = true;\n      if (ease(cur.b, goal.b, a)) moving = true;\n      const f = frames(L.size, cur, sp.t.x, sp.m.x, sp.b.x, sp.hv.x);\n      top.style.transform = f.t.transform;\n      top.style.opacity = f.t.opacity;\n      mid.style.transform = f.m.transform;\n      mid.style.opacity = f.m.opacity;\n      bot.style.transform = f.b.transform;\n      bot.style.opacity = f.b.opacity;\n      icon.style.transform = \"scale(\" + r2((1 - 0.13 * sp.pr.x) * 1000) / 1000 + \")\";\n      if (moving) raf = requestAnimationFrame(frame);\n      else last = 0;\n    };\n    const kick = () => {\n      dirty = true;\n      if (!raf) {\n        last = 0;\n        raf = requestAnimationFrame(frame);\n      }\n    };\n    const onMq = () => {\n      reduce = mq.matches;\n      kick();\n    };\n    wake.current = kick;\n    mq.addEventListener(\"change\", onMq);\n    kick();\n    return () => {\n      mq.removeEventListener(\"change\", onMq);\n      if (raf) cancelAnimationFrame(raf);\n      raf = 0;\n      wake.current = null;\n    };\n  }, []);\n\n  const toggle = (e: MouseEvent<HTMLButtonElement>) => {\n    onClick?.(e);\n    if (e.defaultPrevented) return;\n    const next = !open;\n    if (!controlled) setInner(next);\n    onOpenChange?.(next);\n  };\n  const setPress = (v: boolean) => {\n    live.current.press = v;\n    wake.current?.();\n  };\n  const setHover = (v: boolean) => {\n    live.current.hover = v;\n    wake.current?.();\n  };\n\n  const thick = Math.max(2, Math.round(size * 0.09 * 10) / 10);\n  const bar = color === \"red\" ? \"#ef4444\" : \"currentColor\";\n  const barStyle = (f: { transform: string; opacity: string }) => ({\n    top: \"calc(50% - \" + thick / 2 + \"px)\",\n    width: size,\n    height: thick,\n    borderRadius: thick,\n    background: bar,\n    transform: f.transform,\n    opacity: f.opacity,\n  });\n\n  return (\n    <button\n      type=\"button\"\n      data-morph={morph}\n      {...rest}\n      ref={btnRef}\n      aria-label={open ? closeLabel : openLabel}\n      aria-expanded={open}\n      aria-controls={menuId}\n      onClick={toggle}\n      onPointerEnter={(e) => {\n        if (e.pointerType !== \"touch\") setHover(true);\n      }}\n      onPointerLeave={() => {\n        setHover(false);\n        setPress(false);\n      }}\n      onPointerDown={(e) => {\n        onPointerDown?.(e);\n        setPress(true);\n      }}\n      onPointerUp={() => setPress(false)}\n      onPointerCancel={() => setPress(false)}\n      onKeyDown={(e) => {\n        rest.onKeyDown?.(e);\n        if (e.key === \" \" || e.key === \"Enter\") setPress(true);\n      }}\n      onKeyUp={(e) => {\n        rest.onKeyUp?.(e);\n        if (e.key === \" \" || e.key === \"Enter\") setPress(false);\n      }}\n      onBlur={(e) => {\n        rest.onBlur?.(e);\n        setPress(false);\n      }}\n      onPointerMove={(e) => {\n        onPointerMove?.(e);\n        if (e.pointerType === \"touch\") return;\n        const el = btnRef.current;\n        if (!el) return;\n        const rect = el.getBoundingClientRect();\n        const k = rect.width ? el.offsetWidth / rect.width : 1;\n        el.style.setProperty(\"--mmb-x\", (e.clientX - rect.left) * k + \"px\");\n        el.style.setProperty(\"--mmb-y\", (e.clientY - rect.top) * k + \"px\");\n      }}\n      className={\n        \"group relative inline-flex shrink-0 cursor-pointer touch-manipulation select-none items-center justify-center transition-[background-color] duration-200 hover:bg-[color-mix(in_srgb,currentColor_6%,transparent)] focus-visible:!outline-2 focus-visible:!outline-offset-2 focus-visible:!outline-[currentColor] \" +\n        className\n      }\n      style={{\n        width: size + 24,\n        height: size + 24,\n        borderRadius: 9999,\n        border: \"1px solid\",\n        borderColor: \"color-mix(in srgb, currentColor 22%, transparent)\",\n        WebkitTapHighlightColor: \"transparent\",\n        ...style,\n      }}\n    >\n      <span\n        aria-hidden\n        className=\"pointer-events-none absolute inset-0 opacity-0 transition-opacity duration-300 group-hover:opacity-100 motion-reduce:transition-none\"\n        style={{\n          borderRadius: 9999,\n          background:\n            \"radial-gradient(circle 46px at var(--mmb-x, 50%) var(--mmb-y, 50%), color-mix(in srgb, currentColor 14%, transparent), transparent)\",\n        }}\n      />\n      <span ref={iconRef} aria-hidden className=\"pointer-events-none relative block\" style={{ width: size, height: size }}>\n        <span ref={topRef} className=\"absolute left-0 block\" style={barStyle(init.t)} />\n        <span ref={midRef} className=\"absolute left-0 block\" style={barStyle(init.m)} />\n        <span ref={botRef} className=\"absolute left-0 block\" style={barStyle(init.b)} />\n      </span>\n    </button>\n  );\n}\n","type":"registry:component","target":"components/viberdy/morphing-hamburger-icon.tsx"}],"meta":{"tier":"free","tags":["icon","menu","hamburger","toggle","spring","morph","navigation"],"href":"https://viberdy.dev/library/morphing-hamburger-icon","aiPrompt":"Create a single-file React + Tailwind component named MorphingMenuButton (\"use client\", React only, no animation library). It is a round menu button whose three bars morph on springs into a close X, a back arrow or a minus.\n\nProps (all optional): size 28 (icon width in px; the button is size + 24 square, fully round); color \"ink\" | \"red\" (default \"ink\": bars use currentColor; \"red\" uses #ef4444); menuId \"morphing-menu-panel\" (aria-controls); morph \"x\" | \"arrow\" | \"minus\" (default \"x\"); open (controlled), defaultOpen false, onOpenChange(open); openLabel \"Open menu\", closeLabel \"Close menu\"; className; every other button attribute is passed through to the button (onClick is called first and can preventDefault).\n\nStructure: button type=\"button\" with aria-label (open ? closeLabel : openLabel), aria-expanded, aria-controls. Inside: a pointer-light span (absolute, radial-gradient circle 46px at --mmb-x/--mmb-y of currentColor 14%, opacity 0 -> 1 on hover) and an aria-hidden icon box (size x size) holding three absolutely positioned bar spans, each width size and thickness max(2, size*0.09) with round caps, centred vertically in the box via top calc(50% - t/2) and moved only by transform. The button border is 1px of currentColor at 22% (color-mix), hover background currentColor at 6%. Never use the site's colour tokens. Focus-visible: 2px outline in currentColor, offset 2px (important, so a global focus rule cannot override it), border-radius kept at 9999px inline.\n\nPoses (W = size, g = 0.3W). Closed: top y -g, middle y 0, bottom y +g, all rotate 0, scaleX 1, opacity 1. Open x: top and bottom y 0, rotate 45 / -45, scaleX 0.88; middle scaleX 0.2, opacity 0. Open arrow (points left): arm length 0.56W, tip at x -0.47W, c = arm/2 * 0.7071; top x tip+c, y -c, rotate -45, scaleX 0.56; bottom x tip+c, y +c, rotate +45, scaleX 0.56; middle unchanged (the shaft). Open minus: top and bottom y 0, opacity 0; middle scaleX 0.72. Each bar's pose is lerp(closed, open, p) (rotation, position, scale unclamped so springs overshoot); opacity uses lerp with min(1, p*1.4). Bar transform string: translate(x,y) rotate(r) scaleX(s).\n\nMotion in ONE requestAnimationFrame loop inside a mount effect that writes style.transform / style.opacity straight to the DOM and sleeps when every spring is at rest (never re-render per frame; first-render bar styles come from the same pure function and are frozen in useState so React never rewrites them). Semi-implicit Euler springs with substeps of at most 1/120 s: top k 230 c 18, bottom k 195 c 16, middle k 340 c 29 (target 1 when open, 0 closed; slightly different values give the stagger and an overshoot of a few degrees). The open pose itself eases toward the current morph's pose with rate 14/s so changing morph while open glides. Hover (mouse and pen only): spring k 240 c 22 shortens the middle bar to 70% anchored at its right end, only when closed. Press (pointerdown, Space/Enter): spring k 420 c 27 scales the icon to 0.87. Pointer light follows the pointer using layout px ((clientX - rect.left) * offsetWidth / rect.width).\n\nprefers-reduced-motion (matchMedia, live): every spring becomes k 900 c 60 (critically damped, no overshoot, about 150ms), hover and press motion off, the pose ease snaps. Touch: no hover light, press squash only. Controlled/uncontrolled as described. Output one self-contained component file.","license":"https://viberdy.dev/license"}}