{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "scroll-stagger-grid",
  "type": "registry:component",
  "title": "Scroll Stagger Grid",
  "description": "A grid of tiles that fly in one after another from alternating directions as it scrolls into view, and fly back out in reverse.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "motion",
    "scroll",
    "grid",
    "stagger",
    "reveal",
    "tiles",
    "gallery"
  ],
  "docs": "https://hyperblur.dev/effects/scroll-stagger-grid",
  "files": [
    {
      "path": "registry/hyperblur/scroll-stagger-grid.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/scroll-stagger-grid.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface ScrollStaggerTile {\n  title: string;\n  caption?: string;\n  from?: string;\n  to?: string;\n}\n\nexport interface ScrollStaggerGridProps extends React.HTMLAttributes<HTMLDivElement> {\n  tiles?: ScrollStaggerTile[];\n  columns?: number;\n  gap?: number;\n  distance?: number;\n  inset?: number;\n  aspect?: string;\n  scroller?: React.RefObject<HTMLElement | null> | HTMLElement | null;\n  height?: number | string;\n  autoplay?: boolean;\n  before?: React.ReactNode;\n  after?: React.ReactNode;\n}\n\ntype Scroller = React.RefObject<HTMLElement | null> | HTMLElement | null;\n\ninterface Frame {\n  sc: HTMLElement | null;\n  top: number;\n  vh: number;\n}\n\nconst defaultTiles: ScrollStaggerTile[] = [\n  { title: \"Dune\", caption: \"#F6C177\", from: \"#f6c177\", to: \"#d9573b\" },\n  { title: \"Lagoon\", caption: \"#5EEAD4\", from: \"#5eead4\", to: \"#2563eb\" },\n  { title: \"Orchid\", caption: \"#E879F9\", from: \"#e879f9\", to: \"#6d28d9\" },\n  { title: \"Moss\", caption: \"#A3E635\", from: \"#a3e635\", to: \"#166534\" },\n  { title: \"Ember\", caption: \"#FB923C\", from: \"#fb923c\", to: \"#be123c\" },\n  { title: \"Glacier\", caption: \"#BAE6FD\", from: \"#bae6fd\", to: \"#6366f1\" },\n  { title: \"Cocoa\", caption: \"#D6A77A\", from: \"#d6a77a\", to: \"#5b3a29\" },\n  { title: \"Citrus\", caption: \"#FDE047\", from: \"#fde047\", to: \"#f97316\" },\n];\n\nconst css = `\n.hb-qm4 {\n  --hb-qm4-q: 0;\n  position: relative;\n  width: 100%;\n  font-family: inherit;\n}\n.hb-qm4, .hb-qm4 * { box-sizing: border-box; }\n.hb-qm4-vp {\n  position: relative;\n  overflow-x: hidden;\n  overflow-y: auto;\n  overflow-anchor: none;\n  scrollbar-width: thin;\n  scrollbar-color: #2e2e2e transparent;\n  outline: none;\n}\n.hb-qm4-vp:focus-visible { outline: 2px solid #2f7bff; outline-offset: -2px; }\n.hb-qm4-grid {\n  display: grid;\n  grid-template-columns: repeat(var(--hb-qm4-cols, 4), minmax(0, 1fr));\n  gap: var(--hb-qm4-gap, 10px);\n  margin: 0 var(--hb-qm4-inset, 0px);\n}\n.hb-qm4-tile {\n  --hb-qm4-t: clamp(0, calc((var(--hb-qm4-q) - var(--hb-qm4-d)) / 0.4), 1);\n  --hb-qm4-e: calc(var(--hb-qm4-t) * (2 - var(--hb-qm4-t)));\n  --hb-qm4-x: 0;\n  --hb-qm4-y: 0;\n  position: relative;\n  display: flex;\n  align-items: flex-end;\n  min-width: 0;\n  aspect-ratio: var(--hb-qm4-aspect, 1 / 1);\n  overflow: hidden;\n  border: 1px solid rgba(255, 255, 255, 0.1);\n  border-radius: 0.8em;\n  background: linear-gradient(145deg, var(--hb-qm4-f), var(--hb-qm4-s));\n  opacity: var(--hb-qm4-e);\n  transform: translate3d(\n      calc((1 - var(--hb-qm4-e)) * var(--hb-qm4-x) * var(--hb-qm4-dist, 56px)),\n      calc((1 - var(--hb-qm4-e)) * var(--hb-qm4-y) * var(--hb-qm4-dist, 56px)),\n      0\n    )\n    scale(calc(0.86 + 0.14 * var(--hb-qm4-e)));\n  will-change: transform, opacity;\n}\n.hb-qm4-tile[data-dir=\"0\"] { --hb-qm4-x: -1; }\n.hb-qm4-tile[data-dir=\"1\"] { --hb-qm4-y: 1; }\n.hb-qm4-tile[data-dir=\"2\"] { --hb-qm4-x: 1; }\n.hb-qm4-tile[data-dir=\"3\"] { --hb-qm4-y: -1; }\n.hb-qm4-tile::before {\n  content: \"\";\n  position: absolute;\n  inset: 0;\n  background: radial-gradient(90% 70% at 20% 0%, rgba(255, 255, 255, 0.28), transparent 60%),\n    linear-gradient(180deg, transparent 38%, rgba(0, 0, 0, 0.58));\n}\n.hb-qm4-label {\n  position: relative;\n  display: grid;\n  gap: 0.15em;\n  min-width: 0;\n  padding: 0.6em 0.7em;\n  color: #fff;\n  line-height: 1.15;\n}\n.hb-qm4-name {\n  overflow: hidden;\n  font-size: 0.82em;\n  font-weight: 600;\n  text-overflow: ellipsis;\n  white-space: nowrap;\n}\n.hb-qm4-cap {\n  overflow: hidden;\n  font-size: 0.66em;\n  letter-spacing: 0.04em;\n  text-overflow: ellipsis;\n  white-space: nowrap;\n  color: rgba(255, 255, 255, 0.72);\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-qm4-tile { opacity: 1; transform: none; will-change: auto; }\n}\n`;\n\nfunction clamp(n: number) {\n  return Math.min(1, Math.max(0, n));\n}\n\nfunction pick(scroller: Scroller | undefined): HTMLElement | null {\n  if (!scroller) return null;\n  return \"current\" in scroller ? scroller.current : scroller;\n}\n\nfunction useScrollFrame(\n  rootRef: React.RefObject<HTMLElement | null>,\n  viewportRef: React.RefObject<HTMLElement | null>,\n  scroller: Scroller | undefined,\n  own: boolean,\n  onFrame: (frame: Frame) => void,\n) {\n  const latest = React.useRef(onFrame);\n\n  React.useEffect(() => {\n    latest.current = onFrame;\n  });\n\n  React.useEffect(() => {\n    const root = rootRef.current;\n    if (!root) return;\n    const sc = own ? viewportRef.current : pick(scroller);\n    const host: HTMLElement | Window = sc ?? window;\n    const run = () => {\n      const top = sc ? sc.getBoundingClientRect().top : 0;\n      const vh = sc ? sc.clientHeight : window.innerHeight;\n      latest.current({ sc, top, vh });\n    };\n    host.addEventListener(\"scroll\", run, { passive: true });\n    window.addEventListener(\"resize\", run);\n    const ro = new ResizeObserver(run);\n    ro.observe(root);\n    if (sc) ro.observe(sc);\n    return () => {\n      host.removeEventListener(\"scroll\", run);\n      window.removeEventListener(\"resize\", run);\n      ro.disconnect();\n    };\n  }, [rootRef, viewportRef, scroller, own]);\n}\n\nfunction useAutoScroll(viewportRef: React.RefObject<HTMLElement | null>, active: boolean) {\n  React.useEffect(() => {\n    const el = viewportRef.current;\n    if (!el || !active) return;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) {\n      el.scrollTop = (el.scrollHeight - el.clientHeight) * 0.45;\n      return;\n    }\n    let raf = 0;\n    let last = 0;\n    let pos = 0;\n    let dir = 1;\n    let hold = 700;\n    let idle = 0;\n    const touch = () => {\n      idle = performance.now() + 3200;\n    };\n    const tick = (now: number) => {\n      raf = requestAnimationFrame(tick);\n      const dt = Math.min(now - last, 50);\n      last = now;\n      const max = el.scrollHeight - el.clientHeight;\n      if (max <= 0) return;\n      if (now < idle) {\n        pos = el.scrollTop / max;\n        return;\n      }\n      if (hold > 0) {\n        hold -= dt;\n        return;\n      }\n      pos += (dir * dt) / 9000;\n      if (pos >= 1) {\n        pos = 1;\n        dir = -1;\n        hold = 900;\n      } else if (pos <= 0) {\n        pos = 0;\n        dir = 1;\n        hold = 900;\n      }\n      el.scrollTop = pos * max;\n    };\n    const io = new IntersectionObserver(([entry]) => {\n      if (entry.isIntersecting) {\n        if (!raf) {\n          last = performance.now();\n          raf = requestAnimationFrame(tick);\n        }\n      } else {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    });\n    io.observe(el);\n    const events = [\"wheel\", \"touchstart\", \"pointerdown\", \"keydown\"] as const;\n    for (const name of events) el.addEventListener(name, touch, { passive: true });\n    return () => {\n      cancelAnimationFrame(raf);\n      io.disconnect();\n      for (const name of events) el.removeEventListener(name, touch);\n    };\n  }, [viewportRef, active]);\n}\n\nexport function ScrollStaggerGrid({\n  tiles = defaultTiles,\n  columns = 4,\n  gap = 10,\n  distance = 56,\n  inset = 0,\n  aspect = \"1 / 1\",\n  scroller,\n  height,\n  autoplay = false,\n  before,\n  after,\n  className = \"\",\n  style,\n  ...rest\n}: ScrollStaggerGridProps) {\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const viewportRef = React.useRef<HTMLDivElement>(null);\n  const gridRef = React.useRef<HTMLDivElement>(null);\n  const own = height !== undefined;\n  const cols = Math.max(1, Math.round(columns));\n  const count = tiles.length;\n\n  useAutoScroll(viewportRef, own && autoplay);\n  useScrollFrame(rootRef, viewportRef, scroller, own, ({ top, vh }) => {\n    const grid = gridRef.current;\n    if (!grid) return;\n    const rect = grid.getBoundingClientRect();\n    const reach = Math.max(1, Math.min(rect.height, vh * 0.85));\n    const q = clamp((top + vh - rect.top) / reach);\n    rootRef.current?.style.setProperty(\"--hb-qm4-q\", q.toFixed(4));\n  });\n\n  const body = (\n    <>\n      {before}\n      <div ref={gridRef} className=\"hb-qm4-grid\">\n        {tiles.map((tile, i) => {\n          const row = Math.floor(i / cols);\n          const col = i % cols;\n          const delay = count > 1 ? (i / (count - 1)) * 0.6 : 0;\n          return (\n            <div\n              key={i}\n              className=\"hb-qm4-tile\"\n              data-dir={(row + col) % 4}\n              style={\n                {\n                  \"--hb-qm4-d\": delay.toFixed(3),\n                  \"--hb-qm4-f\": tile.from ?? \"#2f7bff\",\n                  \"--hb-qm4-s\": tile.to ?? \"#111111\",\n                } as React.CSSProperties\n              }\n            >\n              <div className=\"hb-qm4-label\">\n                <span className=\"hb-qm4-name\">{tile.title}</span>\n                {tile.caption ? <span className=\"hb-qm4-cap\">{tile.caption}</span> : null}\n              </div>\n            </div>\n          );\n        })}\n      </div>\n      {after}\n    </>\n  );\n\n  return (\n    <div\n      ref={rootRef}\n      className={`hb-qm4 ${className}`.trim()}\n      style={\n        {\n          \"--hb-qm4-cols\": cols,\n          \"--hb-qm4-gap\": `${gap}px`,\n          \"--hb-qm4-dist\": `${distance}px`,\n          \"--hb-qm4-inset\": `${inset}px`,\n          \"--hb-qm4-aspect\": aspect,\n          ...style,\n        } as React.CSSProperties\n      }\n      {...rest}\n    >\n      <style href=\"hb-scroll-stagger-grid\" precedence=\"default\">\n        {css}\n      </style>\n      {own ? (\n        <div\n          ref={viewportRef}\n          className=\"hb-qm4-vp\"\n          style={{ height }}\n          tabIndex={0}\n          role=\"region\"\n          aria-label=\"Scrollable content\"\n        >\n          {body}\n        </div>\n      ) : (\n        body\n      )}\n    </div>\n  );\n}\n"
    }
  ]
}