{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "scroll-portal",
  "title": "Scroll Portal",
  "description": "Scroll flies the camera through nested frames, each opening onto the next scene. No dependencies.",
  "files": [
    {
      "path": "registry/mewo/ui/scroll-portal.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { cn } from \"@/lib/utils\"\n\nexport interface ScrollPortalScene {\n  /** Any CSS background: colour, gradient or image. Defaults to the theme background. */\n  background?: string\n  content?: React.ReactNode\n  /** Overrides `frameRadius` for this scene's frame. */\n  radius?: number\n  /** Overrides `accent` for this scene's frame border. */\n  accent?: string\n}\n\nexport interface ScrollPortalProps extends Omit<React.ComponentProps<\"section\">, \"children\"> {\n  /** Ordered scenes; each may set background, content, radius and accent. */\n  scenes?: ScrollPortalScene[]\n  /** Distance between frames. Larger values nest the next frame smaller. */\n  frameDepth?: number\n  /** Focal length of the camera in pixels. Smaller values exaggerate depth. */\n  perspective?: number\n  /** Scroll runway per scene handoff, in viewport heights. */\n  scrollLength?: number\n  /** Corner radius of nested frames in pixels. */\n  frameRadius?: number\n  /** Border width of nested frames in pixels. */\n  frameBorder?: number\n  /** Catch-up time in seconds. 0 locks the camera to the scroll position. */\n  scrub?: number\n  /** Fraction of each handoff the camera rests before travelling. */\n  hold?: number\n  /** How much nested frames fade with distance, 0 to 1. */\n  dim?: number\n  /** Colour nested frames fade toward with distance. */\n  dimColor?: string\n  /** Fallback border colour when a scene sets no accent. */\n  accent?: string\n  /** Stage width below which depth and perspective are reduced. */\n  mobileBreakpoint?: number\n  /** Called when the scene nearest the camera changes. */\n  onSceneChange?: (index: number) => void\n}\n\n// Camera travel in pixels for one unit of `frameDepth`.\nconst DEPTH_UNIT = 1000\n// A scene the camera is leaving keeps growing; cap it so the maths stays finite.\nconst MAX_SCALE = 40\n// Radius and border ease out over this much scale as a frame fills the stage.\nconst EDGE_FADE = 0.12\nconst MOBILE_DEPTH = 0.6\nconst MOBILE_PERSPECTIVE = 0.8\n\nconst clamp = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value))\n\n/** Size of frame `index` on screen while the camera sits at `camera`; 1 fills the stage. */\nexport function portalFrameScale(index: number, camera: number, focal: number, depth: number): number {\n  const distance = focal + (index - camera) * depth\n  return distance > focal / MAX_SCALE ? focal / distance : MAX_SCALE\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\nexport function ScrollPortal({\n  scenes = [],\n  frameDepth = 1,\n  perspective = 1000,\n  scrollLength = 1,\n  frameRadius = 16,\n  frameBorder = 1,\n  scrub = 0.35,\n  hold = 0,\n  dim = 0.55,\n  dimColor = \"#000000\",\n  accent = \"color-mix(in oklch, var(--color-foreground) 18%, transparent)\",\n  mobileBreakpoint = 768,\n  onSceneChange,\n  className,\n  style,\n  ...props\n}: ScrollPortalProps) {\n  const stageRef = React.useRef<HTMLDivElement>(null)\n  const frameRefs = React.useRef<(HTMLDivElement | null)[]>([])\n  const contentRefs = React.useRef<(HTMLDivElement | null)[]>([])\n  const dimRefs = React.useRef<(HTMLDivElement | null)[]>([])\n  const borderRefs = React.useRef<(HTMLDivElement | null)[]>([])\n  const progressRef = React.useRef<number | null>(null)\n  const activeRef = React.useRef(-1)\n  const onSceneChangeRef = React.useRef(onSceneChange)\n\n  React.useEffect(() => {\n    onSceneChangeRef.current = onSceneChange\n  }, [onSceneChange])\n\n  const count = scenes.length\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 || count === 0) return\n\n    const scroller = scrollParent(section)\n    // Inside a scroll container the stage pins to that container, not the window.\n    const fit = () => {\n      if (scroller) section.style.setProperty(\"--mewo-viewport\", `${scroller.clientHeight}px`)\n    }\n    fit()\n    // Reduced motion gets the CSS fallback: scenes stacked as plain sections, nothing pinned.\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return\n\n    const catchUp = Math.max(0, scrub)\n    const rest = clamp(hold, 0, 0.95)\n    const fade = clamp(dim, 0, 1)\n\n    let target = 0\n    let raf = 0\n    let last = 0\n\n    const measure = () => {\n      const origin = scroller ? scroller.getBoundingClientRect().top + scroller.clientTop : 0\n      const scrolled = origin - section.getBoundingClientRect().top\n      const runway = section.offsetHeight - stage.offsetHeight\n      target = runway > 0 ? clamp(scrolled / runway, 0, 1) * (count - 1) : 0\n    }\n\n    const render = (progress: number) => {\n      const mobile = stage.clientWidth < mobileBreakpoint\n      const focal = Math.max(1, perspective * (mobile ? MOBILE_PERSPECTIVE : 1))\n      const depth = Math.max(0, frameDepth) * DEPTH_UNIT * (mobile ? MOBILE_DEPTH : 1)\n\n      // Each handoff rests for `hold` of its runway, then travels the rest.\n      const base = Math.min(Math.floor(progress), Math.max(0, count - 2))\n      const camera = base + clamp((progress - base - rest) / (1 - rest), 0, 1)\n      const active = clamp(Math.round(camera), 0, count - 1)\n\n      for (let i = 0; i < count; i++) {\n        const frame = frameRefs.current[i]\n        if (!frame) continue\n\n        // Once the next frame fills the stage this one can no longer be seen.\n        if (camera >= i + 1) {\n          frame.style.visibility = \"hidden\"\n          continue\n        }\n\n        const scale = portalFrameScale(i, camera, focal, depth)\n        // The composited frame never grows past the stage, so its layer is always\n        // rastered at 1x; past that only the content zooms toward the camera.\n        const outer = Math.min(scale, 1)\n        const edge = clamp((1 - outer) / EDGE_FADE, 0, 1)\n        const radius = scenes[i].radius ?? frameRadius\n\n        frame.style.visibility = \"visible\"\n        frame.style.transform = `translate3d(0,0,0) scale(${outer})`\n        frame.style.borderRadius = `${(radius * edge) / outer}px`\n        frame.style.pointerEvents = i === active ? \"auto\" : \"none\"\n\n        const content = contentRefs.current[i]\n        if (content) content.style.transform = scale > 1 ? `scale(${scale})` : \"\"\n\n        const shade = dimRefs.current[i]\n        if (shade) shade.style.opacity = `${fade * (1 - Math.exp(-1.2 * Math.max(0, i - camera)))}`\n\n        const border = borderRefs.current[i]\n        if (border) {\n          border.style.borderWidth = `${frameBorder / outer}px`\n          border.style.opacity = `${edge}`\n        }\n      }\n\n      if (active !== activeRef.current) {\n        activeRef.current = active\n        onSceneChangeRef.current?.(active)\n      }\n    }\n\n    const tick = (now: number) => {\n      const dt = clamp((now - last) / 1000, 0, 0.1)\n      last = now\n\n      let progress = progressRef.current ?? target\n      // Exponential catch-up that covers ~95% of the gap in `scrub` seconds.\n      progress = catchUp > 0 ? progress + (target - progress) * (1 - Math.exp((-3 * dt) / catchUp)) : target\n      if (Math.abs(target - progress) < 0.0004) progress = target\n\n      progressRef.current = progress\n      render(progress)\n      raf = progress === target ? 0 : requestAnimationFrame(tick)\n    }\n\n    const update = () => {\n      measure()\n      if (!raf) {\n        last = performance.now()\n        raf = requestAnimationFrame(tick)\n      }\n    }\n\n    const resize = () => {\n      fit()\n      update()\n      render(progressRef.current ?? target)\n    }\n\n    measure()\n    // Snap on mount; afterwards keep the eased position across prop changes.\n    progressRef.current = clamp(progressRef.current ?? target, 0, count - 1)\n    render(progressRef.current)\n    if (progressRef.current !== target) update()\n\n    const observer = new ResizeObserver(resize)\n    observer.observe(stage)\n    if (scroller) observer.observe(scroller)\n    // Scroll events do not bubble; capturing on the window hears a nested scroller too.\n    window.addEventListener(\"scroll\", update, { passive: true, capture: true })\n    window.addEventListener(\"resize\", resize)\n\n    return () => {\n      cancelAnimationFrame(raf)\n      observer.disconnect()\n      window.removeEventListener(\"scroll\", update, { capture: true })\n      window.removeEventListener(\"resize\", resize)\n    }\n  }, [scenes, count, frameDepth, perspective, scrollLength, frameRadius, frameBorder, scrub, hold, dim, mobileBreakpoint])\n\n  const restingDepth = Math.max(0, frameDepth) * DEPTH_UNIT\n\n  return (\n    <section\n      data-slot=\"scroll-portal\"\n      className={cn(\n        \"relative h-[calc(var(--mewo-viewport,100vh)*var(--mewo-length))] motion-reduce:h-auto\",\n        className\n      )}\n      style={{ \"--mewo-length\": 1 + Math.max(0, count - 1) * Math.max(0, scrollLength), ...style } as React.CSSProperties}\n      {...props}\n    >\n      <div\n        ref={stageRef}\n        data-slot=\"scroll-portal-stage\"\n        className=\"sticky top-0 h-[var(--mewo-viewport,100vh)] overflow-hidden motion-reduce:static motion-reduce:h-auto motion-reduce:overflow-visible\"\n      >\n        {scenes.map((scene, i) => (\n          <div\n            key={i}\n            ref={(el) => {\n              frameRefs.current[i] = el\n            }}\n            data-slot=\"scroll-portal-frame\"\n            className=\"absolute inset-0 overflow-hidden bg-background will-change-transform motion-reduce:relative motion-reduce:inset-auto motion-reduce:h-[var(--mewo-viewport,100vh)] motion-reduce:transform-none!\"\n            style={{\n              background: scene.background,\n              // Resting pose for the first paint; the effect drives it once mounted.\n              transform: `translate3d(0,0,0) scale(${Math.min(1, portalFrameScale(i, 0, Math.max(1, perspective), restingDepth))})`,\n            }}\n          >\n            <div\n              ref={(el) => {\n                contentRefs.current[i] = el\n              }}\n              className=\"absolute inset-0\"\n            >\n              {scene.content}\n            </div>\n            <div\n              ref={(el) => {\n                dimRefs.current[i] = el\n              }}\n              aria-hidden\n              className=\"pointer-events-none absolute inset-0 opacity-0\"\n              style={{ background: dimColor }}\n            />\n            <div\n              ref={(el) => {\n                borderRefs.current[i] = el\n              }}\n              aria-hidden\n              className=\"pointer-events-none absolute inset-0 rounded-[inherit] opacity-0\"\n              style={{ border: `${frameBorder}px solid ${scene.accent ?? accent}` }}\n            />\n          </div>\n        ))}\n      </div>\n    </section>\n  )\n}\n",
      "type": "registry:ui"
    }
  ],
  "css": {
    "@media (scripting: none)": {
      "[data-slot=\"scroll-portal\"]": {
        "height": "auto !important"
      },
      "[data-slot=\"scroll-portal-stage\"]": {
        "position": "static !important",
        "height": "auto !important",
        "overflow": "visible !important"
      },
      "[data-slot=\"scroll-portal-frame\"]": {
        "position": "relative !important",
        "height": "100vh !important",
        "transform": "none !important"
      }
    }
  },
  "meta": {
    "added": "2026-10-01"
  },
  "categories": [
    "scroll"
  ],
  "type": "registry:ui"
}