{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "scene",
  "type": "registry:component",
  "title": "Scene",
  "description": "The wrapper every 3D component is used inside: a transparent WebGL canvas that mounts late, pauses off screen and falls back to a static block.",
  "categories": [
    "three"
  ],
  "dependencies": [
    "@react-three/fiber"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/cn"
  ],
  "files": [
    {
      "path": "registry/three/scene.tsx",
      "type": "registry:component",
      "target": "components/three/scene.tsx",
      "content": "\"use client\";\n/**\n * @name scene\n * @category three\n * @description The wrapper every 3D component is used inside: a transparent WebGL canvas that mounts late, pauses off screen and falls back to a static block.\n * @when Around orb, particle-field, floating-shapes, globe, model-stage or your own react-three-fiber content. Give it a size through the parent or `aspect`. For a decorative background that needs no 3D, use a plain token-coloured block.\n * @example\n * <Scene aspect={16 / 9} label=\"A slowly morphing sphere\">\n *   <Orb />\n * </Scene>\n *\n * <div className=\"h-80\">\n *   <Scene fallback={<img src=\"/globe.png\" alt=\"\" className=\"size-full object-contain\" />}>\n *     <Globe markers={[{ lat: 52.37, lon: 4.9 }]} />\n *   </Scene>\n * </div>\n */\nimport { Canvas, type CanvasProps, type RootState } from \"@react-three/fiber\";\nimport { Component, useCallback, useEffect, useState, type ComponentProps, type ReactNode } from \"react\";\nimport { cn } from \"@/lib/cn\";\n\nexport type SceneProps = Omit<ComponentProps<\"div\">, \"children\"> & {\n  /** react-three-fiber content: the three components, lights, meshes. */\n  children?: ReactNode;\n  /** Shown until the canvas has drawn, when WebGL is missing or fails, and under reduced motion. Default: a flat muted block. */\n  fallback?: ReactNode;\n  /** Width / height, as a number (16 / 9) or a CSS ratio (\"4 / 3\"). Without it the scene fills its parent's height. */\n  aspect?: number | string;\n  /** Under reduced motion, draw one still frame instead of the fallback. */\n  allowReducedMotion?: boolean;\n  /** Describes the scene to assistive technology (`role=\"img\"`). Without it the scene is decoration and hidden from it. */\n  label?: string;\n  /** \"always\" renders every frame while visible; \"demand\" only when something invalidates. */\n  frameloop?: \"always\" | \"demand\";\n  /** The default camera. */\n  camera?: CanvasProps[\"camera\"];\n  /** How far outside the viewport the scene starts and keeps running (an IntersectionObserver margin). */\n  margin?: string;\n};\n\nlet webgl: boolean | undefined;\n\nfunction hasWebGL(): boolean {\n  if (webgl !== undefined) return webgl;\n  try {\n    const canvas = document.createElement(\"canvas\");\n    const context = canvas.getContext(\"webgl2\") ?? canvas.getContext(\"webgl\");\n    webgl = context !== null;\n    // Give the probe's context back at once: browsers allow only a few.\n    context?.getExtension(\"WEBGL_lose_context\")?.loseContext();\n  } catch {\n    webgl = false;\n  }\n  return webgl;\n}\n\n/** Error boundaries can only be classes. A scene that throws (a shader that fails, a lost device) falls back. */\nclass SceneBoundary extends Component<{ onError: () => void; children: ReactNode }, { failed: boolean }> {\n  override state = { failed: false };\n  static getDerivedStateFromError(): { failed: boolean } {\n    return { failed: true };\n  }\n  override componentDidCatch(): void {\n    this.props.onError();\n  }\n  override render(): ReactNode {\n    return this.state.failed ? null : this.props.children;\n  }\n}\n\nconst DEFAULT_CAMERA: CanvasProps[\"camera\"] = { position: [0, 0, 6], fov: 40, near: 0.1, far: 100 };\nconst GL: CanvasProps[\"gl\"] = { alpha: true, antialias: true, powerPreference: \"high-performance\" };\nconst DPR: [number, number] = [1, 2];\n\nexport function Scene({\n  children,\n  fallback,\n  aspect,\n  allowReducedMotion = false,\n  label,\n  frameloop = \"always\",\n  camera = DEFAULT_CAMERA,\n  margin = \"200px\",\n  className,\n  style,\n  ...props\n}: SceneProps) {\n  const [root, setRoot] = useState<HTMLDivElement | null>(null);\n  /** null until mounted: the server and the first client render show the fallback. */\n  const [client, setClient] = useState<{ webgl: boolean; reduced: boolean; observer: boolean } | null>(null);\n  const [near, setNear] = useState(false);\n  const [seen, setSeen] = useState(false);\n  const [tabVisible, setTabVisible] = useState(true);\n  const [ready, setReady] = useState(false);\n  const [failed, setFailed] = useState(false);\n\n  useEffect(() => {\n    const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    const update = (): void => setClient({ webgl: hasWebGL(), reduced: query.matches, observer: typeof IntersectionObserver !== \"undefined\" });\n    const visibility = (): void => setTabVisible(document.visibilityState !== \"hidden\");\n    update();\n    visibility();\n    query.addEventListener(\"change\", update);\n    document.addEventListener(\"visibilitychange\", visibility);\n    return () => {\n      query.removeEventListener(\"change\", update);\n      document.removeEventListener(\"visibilitychange\", visibility);\n    };\n  }, []);\n\n  useEffect(() => {\n    if (!root || typeof IntersectionObserver === \"undefined\") return;\n    const observer = new IntersectionObserver(\n      (entries) => {\n        const inside = entries.some((entry) => entry.isIntersecting);\n        setNear(inside);\n        if (inside) setSeen(true);\n      },\n      { rootMargin: margin },\n    );\n    observer.observe(root);\n    return () => observer.disconnect();\n  }, [root, margin]);\n\n  const fail = useCallback((): void => {\n    setFailed(true);\n    setReady(false);\n  }, []);\n\n  const created = useCallback(\n    (state: RootState): void => {\n      // The device can take the context away (too many canvases, a driver reset): show the fallback again.\n      state.gl.domElement.addEventListener(\"webglcontextlost\", fail, { once: true });\n      setReady(true);\n    },\n    [fail],\n  );\n\n  const still = client?.reduced === true;\n  // Without IntersectionObserver the scene cannot know where it is, so it counts as in view.\n  const unobserved = client !== null && !client.observer;\n  const active = client !== null && client.webgl && !failed && (seen || unobserved) && (!still || allowReducedMotion);\n  const loop = !(near || unobserved) || !tabVisible ? \"never\" : still ? \"demand\" : frameloop;\n  const shown = active && ready;\n\n  return (\n    <div\n      ref={setRoot}\n      data-slot=\"scene\"\n      data-state={shown ? \"ready\" : \"fallback\"}\n      role={label ? \"img\" : undefined}\n      aria-label={label}\n      aria-hidden={label ? undefined : true}\n      className={cn(\"relative w-full overflow-hidden\", aspect === undefined && \"h-full\", className)}\n      style={aspect === undefined ? style : { aspectRatio: String(aspect), ...style }}\n      {...props}\n    >\n      <div\n        data-slot=\"scene-fallback\"\n        className={cn(\"absolute inset-0 transition-opacity duration-slow ease-out motion-reduce:transition-none\", shown && \"pointer-events-none opacity-0\")}\n      >\n        {fallback ?? <div className=\"size-full rounded-surface bg-muted\" />}\n      </div>\n      {active ? (\n        <div data-slot=\"scene-canvas\" className={cn(\"absolute inset-0 transition-opacity duration-slow ease-out motion-reduce:transition-none\", !ready && \"opacity-0\")}>\n          <SceneBoundary onError={fail}>\n            <Canvas flat dpr={DPR} gl={GL} camera={camera} frameloop={loop} onCreated={created}>\n              {children}\n            </Canvas>\n          </SceneBoundary>\n        </div>\n      ) : null}\n    </div>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "three",
    "when": "Around orb, particle-field, floating-shapes, globe, model-stage or your own react-three-fiber content. Give it a size through the parent or `aspect`. For a decorative background that needs no 3D, use a plain token-coloured block.",
    "example": "<Scene aspect={16 / 9} label=\"A slowly morphing sphere\">\n  <Orb />\n</Scene>\n\n<div className=\"h-80\">\n  <Scene fallback={<img src=\"/globe.png\" alt=\"\" className=\"size-full object-contain\" />}>\n    <Globe markers={[{ lat: 52.37, lon: 4.9 }]} />\n  </Scene>\n</div>",
    "client": true
  }
}
