{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "section-runtime",
  "title": "Sections runtime",
  "description": "Shared runtime of the Sections collection: loads a section's heavy graphics the first time it nears the viewport, with a still frame meanwhile.",
  "dependencies": [],
  "devDependencies": [],
  "registryDependencies": [],
  "files": [
    {
      "path": "lib/section-runtime.ts",
      "content": "\"use client\"\n\n// Shared runtime of the Sections collection (ADR 0062): heavy graphics (ogl, big canvases) load the\n// first time the section nears the viewport; until then the section shows its still frame.\nimport { useEffect, useRef, useState, type RefObject } from \"react\"\n\nexport interface LazyGraphicsOptions {\n  /**\n   * Margin around the viewport, as in IntersectionObserver `rootMargin`: loading starts this far\n   * before the section scrolls in, so the graphics are usually ready when it appears.\n   */\n  margin?: string\n  /** Load right away, without waiting for the viewport (e.g. the reduced-motion still needs it). */\n  eager?: boolean\n}\n\nexport interface LazyGraphics<T> {\n  /** What `load` resolved to, `null` until then (and if it failed). */\n  module: T | null\n  /** True once `load` resolved: build the graphics now, show the still frame until then. */\n  ready: boolean\n  /** True if `load` rejected: keep the still frame for good. */\n  failed: boolean\n}\n\nconst noLoad = () => Promise.resolve(true as const)\n\n/**\n * Loads a section's heavy graphics once, the first time `ref` comes within `margin` of the\n * viewport (or right away with `eager`). `load` is usually a dynamic import, e.g.\n * `() => import(\"ogl\")`; without it the hook only reports when the section was first seen.\n * SSR-safe (nothing runs before mount) and never sets state after unmount.\n */\nexport function useLazyGraphics<T = true>(\n  ref: RefObject<Element | null>,\n  load?: () => Promise<T>,\n  { margin = \"200px\", eager = false }: LazyGraphicsOptions = {},\n): LazyGraphics<T> {\n  const loadRef = useRef(load)\n  useEffect(() => {\n    loadRef.current = load\n  })\n  const [seen, setSeen] = useState(false)\n  const [state, setState] = useState<{ module: T | null; failed: boolean }>({\n    module: null,\n    failed: false,\n  })\n  // Effects only run in the browser, so reading the global here never loads anything on the server.\n  const want = seen || eager || typeof IntersectionObserver === \"undefined\"\n\n  // First sighting: the first intersection, `margin` ahead of the viewport.\n  useEffect(() => {\n    const el = ref.current\n    if (want || !el) return\n    const io = new IntersectionObserver(\n      (entries) => {\n        if (entries.some((e) => e.isIntersecting)) setSeen(true)\n      },\n      { rootMargin: margin },\n    )\n    io.observe(el)\n    return () => {\n      io.disconnect()\n    }\n  }, [ref, want, margin])\n\n  useEffect(() => {\n    if (!want) return\n    let alive = true\n    const run = (loadRef.current ?? noLoad) as () => Promise<T>\n    run().then(\n      (module) => {\n        if (alive) setState({ module, failed: false })\n      },\n      () => {\n        if (alive) setState({ module: null, failed: true })\n      },\n    )\n    return () => {\n      alive = false\n    }\n  }, [want])\n\n  return { module: state.module, ready: state.module !== null, failed: state.failed }\n}\n",
      "type": "registry:lib"
    }
  ],
  "meta": {
    "tier": "free",
    "collection": "interface",
    "needsImage": false,
    "addedAt": "2026-11-12",
    "tags": [
      "sections",
      "runtime",
      "lazy"
    ],
    "tech": [],
    "presets": [],
    "keyboard": [],
    "heavy": false
  },
  "categories": [
    "internal"
  ],
  "type": "registry:lib"
}