{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "transition-shared-morph",
  "type": "registry:component",
  "title": "Transition Shared Morph",
  "description": "Elements that carry the same data-morph name in both views fly from their old box to their new one while the rest of the views crossfade. Good for a list row that opens into a detail card.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "transitions",
    "shared element",
    "morph",
    "flip",
    "layout",
    "transition"
  ],
  "docs": "https://hyperblur.dev/effects/transition-shared-morph",
  "files": [
    {
      "path": "registry/hyperblur/transition-shared-morph.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/transition-shared-morph.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface TransitionSharedMorphProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"children\"> {\n  children?: React.ReactNode;\n  from?: React.ReactNode;\n  to?: React.ReactNode;\n  active?: boolean;\n  defaultActive?: boolean;\n  onActiveChange?: (active: boolean) => void;\n  autoPlay?: boolean;\n  interval?: number;\n  duration?: number;\n  easing?: string;\n}\n\nconst css = `\n.hb-vj4 {\n  position: relative;\n  display: grid;\n  grid-template: minmax(0, 1fr) / minmax(0, 1fr);\n  overflow: hidden;\n  isolation: isolate;\n  box-sizing: border-box;\n  font-family: inherit;\n}\n.hb-vj4 *, .hb-vj4 *::before, .hb-vj4 *::after { box-sizing: border-box; }\n.hb-vj4-l {\n  grid-area: 1 / 1;\n  display: grid;\n  min-width: 0;\n  min-height: 0;\n  opacity: 0;\n  visibility: hidden;\n  transform: translateY(8px) scale(0.985);\n  filter: blur(4px);\n  transition:\n    opacity calc(var(--hb-vj4-d) * 0.4) cubic-bezier(0.4, 0, 1, 1),\n    transform calc(var(--hb-vj4-d) * 0.4) cubic-bezier(0.4, 0, 1, 1),\n    filter calc(var(--hb-vj4-d) * 0.4) ease,\n    visibility 0s linear calc(var(--hb-vj4-d) * 0.4);\n}\n.hb-vj4-l[data-on=\"true\"] {\n  opacity: 1;\n  visibility: visible;\n  transform: none;\n  filter: none;\n  transition:\n    opacity calc(var(--hb-vj4-d) * 0.6) cubic-bezier(0.16, 1, 0.3, 1) calc(var(--hb-vj4-d) * 0.3),\n    transform calc(var(--hb-vj4-d) * 0.7) cubic-bezier(0.16, 1, 0.3, 1) calc(var(--hb-vj4-d) * 0.25),\n    filter calc(var(--hb-vj4-d) * 0.6) ease calc(var(--hb-vj4-d) * 0.3),\n    visibility 0s linear 0s;\n}\n.hb-vj4-fx {\n  position: absolute;\n  inset: 0;\n  z-index: 5;\n  pointer-events: none;\n}\n.hb-vj4-fx > * {\n  position: absolute;\n  margin: 0;\n  transform-origin: 0 0;\n  will-change: transform, opacity;\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-vj4-l, .hb-vj4-l[data-on=\"true\"] { transition: none; }\n}\n`;\n\nfunction useSwap(\n  active: boolean | undefined,\n  defaultActive: boolean,\n  onActiveChange: ((active: boolean) => void) | undefined,\n  autoPlay: boolean,\n  interval: number,\n) {\n  const [inner, setInner] = React.useState(defaultActive);\n  const controlled = active !== undefined;\n  const value = controlled ? active : inner;\n  const set = React.useCallback(\n    (next: boolean) => {\n      if (!controlled) setInner(next);\n      onActiveChange?.(next);\n    },\n    [controlled, onActiveChange],\n  );\n  React.useEffect(() => {\n    if (!autoPlay) return;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    const id = window.setTimeout(() => set(!value), interval);\n    return () => window.clearTimeout(id);\n  }, [autoPlay, interval, value, set]);\n  return value;\n}\n\nfunction DefaultRow() {\n  return (\n    <div style={{ display: \"grid\", alignContent: \"center\", padding: 16, minHeight: 180, background: \"#0f0f10\", color: \"#ededed\" }}>\n      <div style={{ display: \"flex\", alignItems: \"center\", gap: 12, padding: 10, borderRadius: 12, background: \"#161618\", border: \"1px solid #262626\" }}>\n        <div data-morph=\"cover\" style={{ width: 44, height: 44, borderRadius: 8, background: \"linear-gradient(135deg, #ff8a3d, #c2185b)\" }} />\n        <div style={{ display: \"grid\", gap: 2 }}>\n          <strong data-morph=\"title\" style={{ fontSize: 14, fontWeight: 600 }}>Low Tide</strong>\n          <span style={{ fontSize: 12, color: \"#8f8f8f\" }}>Harbor Lights</span>\n        </div>\n      </div>\n    </div>\n  );\n}\n\nfunction DefaultDetail() {\n  return (\n    <div style={{ display: \"grid\", justifyItems: \"center\", alignContent: \"center\", gap: 10, padding: 16, minHeight: 180, background: \"#0f0f10\", color: \"#ededed\" }}>\n      <div data-morph=\"cover\" style={{ width: 104, height: 104, borderRadius: 18, background: \"linear-gradient(135deg, #ff8a3d, #c2185b)\" }} />\n      <strong data-morph=\"title\" style={{ fontSize: 18, fontWeight: 600 }}>Low Tide</strong>\n    </div>\n  );\n}\n\ntype Box = { x: number; y: number; w: number; h: number };\n\nexport function TransitionSharedMorph({\n  children,\n  from,\n  to,\n  active,\n  defaultActive = false,\n  onActiveChange,\n  autoPlay = false,\n  interval = 2800,\n  duration = 760,\n  easing = \"cubic-bezier(0.32, 0.72, 0, 1)\",\n  className,\n  style,\n  ...rest\n}: TransitionSharedMorphProps) {\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const fromRef = React.useRef<HTMLDivElement>(null);\n  const toRef = React.useRef<HTMLDivElement>(null);\n  const fxRef = React.useRef<HTMLDivElement>(null);\n  const prevRef = React.useRef<boolean | null>(null);\n  const value = useSwap(active, defaultActive, onActiveChange, autoPlay, interval);\n  const kids = React.Children.toArray(children);\n  const a = from ?? kids[0] ?? <DefaultRow />;\n  const b = to ?? kids[1] ?? <DefaultDetail />;\n\n  React.useLayoutEffect(() => {\n    if (prevRef.current === null) {\n      prevRef.current = value;\n      return;\n    }\n    if (prevRef.current === value) return;\n    prevRef.current = value;\n    const root = rootRef.current;\n    const fx = fxRef.current;\n    const outgoing = value ? fromRef.current : toRef.current;\n    const incoming = value ? toRef.current : fromRef.current;\n    if (!root || !fx || !outgoing || !incoming) return;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    const box = (el: Element, layer: HTMLElement): Box => {\n      const lr = layer.getBoundingClientRect();\n      const s = layer.offsetWidth > 0 ? lr.width / layer.offsetWidth : 1;\n      const r = el.getBoundingClientRect();\n      return {\n        x: (r.left - lr.left) / s + layer.offsetLeft,\n        y: (r.top - lr.top) / s + layer.offsetTop,\n        w: r.width / s,\n        h: r.height / s,\n      };\n    };\n    const animations: Animation[] = [];\n    const clones: HTMLElement[] = [];\n    const targets = incoming.querySelectorAll<HTMLElement>(\"[data-morph]\");\n    targets.forEach((target) => {\n      const id = target.getAttribute(\"data-morph\");\n      if (!id) return;\n      const source = Array.from(outgoing.querySelectorAll<HTMLElement>(\"[data-morph]\")).find((el) => el.getAttribute(\"data-morph\") === id);\n      if (!source) return;\n      const A = box(source, outgoing);\n      const B = box(target, incoming);\n      if (A.w === 0 || B.w === 0 || A.h === 0 || B.h === 0) return;\n      const radiusA = getComputedStyle(source).borderRadius;\n      const radiusB = getComputedStyle(target).borderRadius;\n      const make = (el: HTMLElement, at: Box) => {\n        const c = el.cloneNode(true) as HTMLElement;\n        c.removeAttribute(\"data-morph\");\n        c.removeAttribute(\"id\");\n        c.style.left = `${at.x}px`;\n        c.style.top = `${at.y}px`;\n        c.style.width = `${at.w}px`;\n        c.style.height = `${at.h}px`;\n        fx.appendChild(c);\n        clones.push(c);\n        return c;\n      };\n      const out = make(source, A);\n      const inn = make(target, B);\n      const toB = `translate(${(B.x - A.x).toFixed(2)}px, ${(B.y - A.y).toFixed(2)}px) scale(${(B.w / A.w).toFixed(4)}, ${(B.h / A.h).toFixed(4)})`;\n      const fromA = `translate(${(A.x - B.x).toFixed(2)}px, ${(A.y - B.y).toFixed(2)}px) scale(${(A.w / B.w).toFixed(4)}, ${(A.h / B.h).toFixed(4)})`;\n      const opts: KeyframeAnimationOptions = { duration, easing, fill: \"both\" };\n      animations.push(\n        out.animate(\n          [\n            { transform: \"none\", opacity: 1, borderRadius: radiusA },\n            { transform: toB, opacity: 0, borderRadius: radiusA },\n          ],\n          opts,\n        ),\n      );\n      animations.push(\n        inn.animate(\n          [\n            { transform: fromA, opacity: 0, borderRadius: radiusB },\n            { transform: fromA, opacity: 0.4, offset: 0.12, borderRadius: radiusB },\n            { transform: \"none\", opacity: 1, borderRadius: radiusB },\n          ],\n          opts,\n        ),\n      );\n      [source, target].forEach((el) => {\n        animations.push(el.animate([{ visibility: \"hidden\" }, { visibility: \"hidden\" }], { duration, fill: \"none\" }));\n      });\n    });\n    let done = 0;\n    const finish = () => {\n      done += 1;\n      if (done < animations.length) return;\n      clones.forEach((c) => c.remove());\n    };\n    animations.forEach((anim) => {\n      anim.onfinish = finish;\n    });\n    return () => {\n      animations.forEach((anim) => anim.cancel());\n      clones.forEach((c) => c.remove());\n    };\n  }, [value, duration, easing]);\n\n  return (\n    <div\n      ref={rootRef}\n      className={[\"hb-vj4\", className].filter(Boolean).join(\" \")}\n      style={{ \"--hb-vj4-d\": `${duration}ms`, ...style } as React.CSSProperties}\n      data-active={value}\n      {...rest}\n    >\n      <style href=\"hb-transition-shared-morph\" precedence=\"default\">{css}</style>\n      <div ref={fromRef} className=\"hb-vj4-l\" data-on={!value} aria-hidden={value} inert={value}>{a}</div>\n      <div ref={toRef} className=\"hb-vj4-l\" data-on={value} aria-hidden={!value} inert={!value}>{b}</div>\n      <div ref={fxRef} className=\"hb-vj4-fx\" aria-hidden=\"true\" />\n    </div>\n  );\n}\n"
    }
  ]
}