{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "paper-fold-heading",
  "title": "Paper Fold Heading",
  "description": "A heading folded like a concertina of paper. It opens as it comes into view, and folds back up while the pointer is over it.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/paper-fold-heading/paper-fold-heading.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface PaperFoldHeadingProps extends Omit<\n  React.HTMLAttributes<HTMLHeadingElement>,\n  \"children\"\n> {\n  text?: string;\n  as?: \"h1\" | \"h2\" | \"h3\" | \"p\";\n  /** Strips the heading is folded into. */\n  folds?: number;\n  /** Fold angle when closed, in degrees. */\n  angle?: number;\n  /** Fold it up while the pointer is over it. */\n  foldOnHover?: boolean;\n}\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (callback) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", callback);\n      return () => query.removeEventListener(\"change\", callback);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\nexport function PaperFoldHeading({\n  text = \"Unfolds like paper\",\n  as: Tag = \"h2\",\n  folds = 4,\n  angle = 68,\n  foldOnHover = true,\n  className,\n  style,\n  onPointerEnter,\n  onPointerLeave,\n  ...props\n}: PaperFoldHeadingProps) {\n  const reduce = useReducedMotion();\n  const rootRef = React.useRef<HTMLHeadingElement>(null);\n  const stripRefs = React.useRef<(HTMLSpanElement | null)[]>([]);\n  const goal = React.useRef(1);\n  const start = React.useRef<() => void>(() => {});\n\n  // Every strip hangs from the one above it, so a single angle sets the whole concertina.\n  React.useEffect(() => {\n    const root = rootRef.current;\n    if (!root) return;\n    let raf = 0;\n    let prev = 0;\n    let fold = reduce ? 0 : 1;\n    const paint = () => {\n      const height = root.clientHeight / folds;\n      const turn = (fold * angle * Math.PI) / 180;\n      const down = height * Math.cos(turn);\n      const back = height * Math.sin(turn);\n      const lift = (root.clientHeight - down * folds) / 2;\n      stripRefs.current.slice(0, folds).forEach((el, i) => {\n        if (!el) return;\n        const odd = i % 2 === 1;\n        el.style.transform = `translate3d(0, ${(lift + down * i - height * i).toFixed(2)}px, ${(odd ? -back : 0).toFixed(2)}px) rotateX(${((odd ? -1 : 1) * fold * angle).toFixed(2)}deg)`;\n        el.style.opacity = (1 - Math.sin(turn) * (odd ? 0.15 : 0.5)).toFixed(3);\n      });\n    };\n    const frame = (now: number) => {\n      const dt = Math.min(0.05, prev ? (now - prev) / 1000 : 1 / 60);\n      prev = now;\n      fold += (goal.current - fold) * (1 - Math.exp(-dt * 6));\n      const done = Math.abs(goal.current - fold) < 0.002;\n      if (done) fold = goal.current;\n      paint();\n      if (done) {\n        raf = 0;\n        prev = 0;\n      } else raf = requestAnimationFrame(frame);\n    };\n    start.current = () => {\n      if (reduce) paint();\n      else if (!raf) raf = requestAnimationFrame(frame);\n    };\n    paint();\n    const ro = new ResizeObserver(paint);\n    ro.observe(root);\n    const io = new IntersectionObserver(\n      ([entry]) => {\n        if (!entry.isIntersecting) return;\n        io.disconnect();\n        goal.current = 0;\n        start.current();\n      },\n      { threshold: 0.3 },\n    );\n    io.observe(root);\n    return () => {\n      ro.disconnect();\n      io.disconnect();\n      cancelAnimationFrame(raf);\n    };\n  }, [reduce, folds, angle]);\n\n  return (\n    <Tag\n      ref={rootRef}\n      aria-label={text}\n      className={`relative [perspective:900px] ${className ?? \"\"}`}\n      style={style}\n      onPointerEnter={(e: React.PointerEvent<HTMLHeadingElement>) => {\n        onPointerEnter?.(e);\n        if (!foldOnHover) return;\n        goal.current = 0.82;\n        start.current();\n      }}\n      onPointerLeave={(e: React.PointerEvent<HTMLHeadingElement>) => {\n        onPointerLeave?.(e);\n        goal.current = 0;\n        start.current();\n      }}\n      {...props}\n    >\n      {/* Holds the height. The strips are copies of it, each showing one band. */}\n      <span aria-hidden=\"true\" className=\"invisible block\">\n        {text}\n      </span>\n      {Array.from({ length: folds }, (_, i) => (\n        <span\n          key={i}\n          ref={(el) => {\n            stripRefs.current[i] = el;\n          }}\n          aria-hidden=\"true\"\n          className=\"absolute inset-0 block origin-top\"\n          style={{\n            top: `${(i / folds) * 100}%`,\n            bottom: \"auto\",\n            height: `${100 / folds}%`,\n            overflow: \"hidden\",\n            transformStyle: \"preserve-3d\",\n          }}\n        >\n          <span\n            className=\"absolute inset-x-0 block\"\n            style={{\n              top: `${-i * 100}%`,\n              height: `${folds * 100}%`,\n            }}\n          >\n            {text}\n          </span>\n        </span>\n      ))}\n    </Tag>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}