{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rising-captions",
  "title": "Rising Captions",
  "description": "Captions that rise one after another through a pinned stage as you scroll, slower than the page: each fades in low, climbs steadily and fades out before the top. No dependencies.",
  "files": [
    {
      "path": "registry/mewo/ui/rising-captions.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { cn } from \"@/lib/utils\"\n\nexport interface RisingCaptionsProps extends Omit<React.ComponentProps<\"section\">, \"children\"> {\n  /** The captions, rising one after another through the view as the reader scrolls. */\n  captions?: React.ReactNode[]\n  /** View heights of scroll each caption takes to rise through. */\n  step?: number\n  /** Merged onto every caption: its width and type (text-white over a dark picture, say). */\n  captionClassName?: string\n  /** Called with the index of the caption that comes up. */\n  onCaptionChange?: (index: number) => void\n}\n\nexport type RisingCaptionsLayout = \"desktop\" | \"tablet\" | \"phone\"\n\n// Each caption's rise, in a step of scroll: it fades in with its top edge at `from` (% of the view's height),\n// rises steadily to `to` over SPAN of the step, and is fully shown FADE_IN into it until FADE_OUT before the end.\n// The sequence starts FADE_IN in, so the first caption is already there when the stage pins.\nconst RISE: Record<RisingCaptionsLayout, { from: number; to: number }> = {\n  desktop: { from: 61, to: 31.22 },\n  tablet: { from: 54.22, to: 30.89 },\n  phone: { from: 54.68, to: 31.16 },\n}\nconst SPAN = 0.95\nconst FADE_IN = 0.15\nconst FADE_OUT = 0.3\n\n/** The layout for a box: a wide one from 900px across, else a tablet, or a phone below an aspect of 13 : 20. */\nexport function layoutFor(width: number, height: number): RisingCaptionsLayout {\n  if (width >= 900) return \"desktop\"\n  return width / Math.max(1, height) >= 13 / 20 ? \"tablet\" : \"phone\"\n}\n\n/** Caption i at `progress` view heights after the stage pins, each taking `step`: [top edge (% of the view's\n * height), opacity] into out. The first is fully shown from the start. With `reduced`, each holds still half way up\n * and shows for its whole stretch. */\nexport function captionAt(layout: RisingCaptionsLayout, i: number, progress: number, step: number, out: number[], reduced = false) {\n  const { from, to } = RISE[layout]\n  const s = Math.max(0.05, step)\n  const t = (progress - i * s) / s + FADE_IN\n  if (reduced) {\n    out[0] = (from + to) / 2\n    out[1] = t >= 0 && t < SPAN ? 1 : 0\n    return out\n  }\n  const k = Math.min(SPAN, Math.max(0, t))\n  out[0] = from - ((from - to) * k) / SPAN\n  out[1] = t <= 0 || t >= SPAN ? 0 : Math.min(1, t / FADE_IN, (SPAN - t) / FADE_OUT)\n  return out\n}\n\n/** The nearest ancestor that scrolls vertically, or null when the page itself does. */\nfunction scrollParent(node: HTMLElement): HTMLElement | null {\n  for (let el = node.parentElement; el && el !== document.body && el !== document.documentElement; el = el.parentElement) {\n    const { overflowY } = getComputedStyle(el)\n    if (overflowY === \"auto\" || overflowY === \"scroll\") return el\n  }\n  return null\n}\n\n/**\n * Captions that rise one after another through the middle of a pinned stage as the reader scrolls, slower than the\n * page: the first is there from the start; each climbs steadily and fades out before it reaches the top, and the\n * next fades in low and follows. Pins to its nearest scroll container, or to the page. Only transform and opacity\n * change as it scrolls.\n */\nexport function RisingCaptions({\n  captions = [\n    \"Who carved it, and why it was buried, are not recorded.\",\n    \"Each repair, find and move left a trace.\",\n    \"Some are written in museum ledgers.\",\n    \"Others survive only in photographs.\",\n    \"Following one object's path is the work of provenance research.\",\n  ],\n  step = 1,\n  captionClassName,\n  onCaptionChange,\n  className,\n  style,\n  ...props\n}: RisingCaptionsProps) {\n  const stageRef = React.useRef<HTMLDivElement>(null)\n  const onCaptionChangeRef = React.useRef(onCaptionChange)\n  const count = captions.length\n\n  React.useEffect(() => {\n    onCaptionChangeRef.current = onCaptionChange\n  })\n\n  React.useEffect(() => {\n    const stage = stageRef.current\n    // The root's ref stays the consumer's, so the section is reached through the stage.\n    const section = stage?.parentElement\n    if (!stage || !section) return\n    const scroller = scrollParent(section)\n    const reduced = window.matchMedia?.(\"(prefers-reduced-motion: reduce)\").matches ?? false\n    const items = [...stage.querySelectorAll<HTMLElement>('[data-slot=\"rising-captions-caption\"]')]\n    const out = [0, 0]\n    let widths: number[] = []\n    let layout: RisingCaptionsLayout = \"desktop\"\n    let width = 1\n    let height = 1\n    let near = true\n    let shown = -1\n    let raf = 0\n    const last: string[] = []\n\n    const measure = () => {\n      // Inside a scroll container the stage pins to that container, not the window.\n      if (scroller) section.style.setProperty(\"--mewo-viewport\", `${scroller.clientHeight}px`)\n      width = Math.max(1, stage.clientWidth)\n      height = Math.max(1, stage.clientHeight)\n      layout = layoutFor(width, height)\n      widths = items.map((el) => el.offsetWidth)\n      last.length = 0\n    }\n    const update = () => {\n      raf = 0\n      const viewport = scroller ? scroller.clientHeight : window.innerHeight\n      const top = section.getBoundingClientRect().top - (scroller ? scroller.getBoundingClientRect().top : 0)\n      const progress = -top / Math.max(1, viewport)\n      let best = -1\n      let bestOpacity = 0\n      items.forEach((el, i) => {\n        const [y, o] = captionAt(layout, i, progress, step, out, reduced) as [number, number]\n        if (o > bestOpacity) {\n          best = i\n          bestOpacity = o\n        }\n        const key = `${layout}|${y.toFixed(2)}|${o.toFixed(3)}`\n        if (key === last[i]) return\n        last[i] = key\n        el.style.transform = `translate(${((width - widths[i]!) / 2).toFixed(1)}px, ${((y / 100) * height).toFixed(1)}px)`\n        el.style.opacity = o.toFixed(3)\n        el.style.visibility = o < 0.002 ? \"hidden\" : \"visible\"\n      })\n      if (best !== shown) {\n        shown = best\n        if (best >= 0) onCaptionChangeRef.current?.(best)\n      }\n    }\n    const schedule = () => {\n      if (!raf && near) raf = requestAnimationFrame(update)\n    }\n\n    measure()\n    schedule()\n    const resizeObserver = new ResizeObserver(() => {\n      measure()\n      schedule()\n    })\n    resizeObserver.observe(stage)\n    if (scroller) resizeObserver.observe(scroller)\n    // far from view it doesn't measure on every scroll (against the window, so a scroll box leaving the page counts)\n    const intersection = new IntersectionObserver(\n      (entries) => {\n        near = entries.some((entry) => entry.isIntersecting)\n        if (near) {\n          last.length = 0\n          schedule()\n        }\n      },\n      { rootMargin: \"50% 0px\" }\n    )\n    intersection.observe(section)\n    const target = scroller ?? window\n    target.addEventListener(\"scroll\", schedule, { passive: true })\n    return () => {\n      cancelAnimationFrame(raf)\n      resizeObserver.disconnect()\n      intersection.disconnect()\n      target.removeEventListener(\"scroll\", schedule)\n    }\n  }, [count, step])\n\n  return (\n    <section\n      data-slot=\"rising-captions\"\n      className={cn(\"relative h-[calc(var(--mewo-viewport,100vh)*var(--mewo-length))]\", className)}\n      style={{ \"--mewo-length\": 1 + count * Math.max(0.05, step), ...style } as React.CSSProperties}\n      {...props}\n    >\n      <div ref={stageRef} data-slot=\"rising-captions-stage\" className=\"sticky top-0 h-[var(--mewo-viewport,100vh)] w-full overflow-hidden [container-type:size]\">\n        {captions.map((caption, i) => (\n          <p\n            key={i}\n            data-slot=\"rising-captions-caption\"\n            className={cn(\n              \"invisible absolute top-0 left-0 w-[min(105cqh,84cqw)] text-center font-heading text-[length:min(5.2cqh,7.5cqw)] leading-[1.12] tracking-[-0.01em] opacity-0 will-change-transform\",\n              captionClassName\n            )}\n          >\n            {caption}\n          </p>\n        ))}\n      </div>\n    </section>\n  )\n}\n",
      "type": "registry:ui"
    }
  ],
  "meta": {
    "added": "2026-10-27"
  },
  "categories": [
    "scroll",
    "text"
  ],
  "type": "registry:ui"
}