{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "shader-metal",
  "title": "Shader Metal",
  "description": "Liquid chrome, gold, or copper ribbons twisting through a studio light rig: transparent WebGL, theme-aware, with a key light that follows the pointer.",
  "files": [
    {
      "path": "registry/shader-metal/shader-metal.tsx",
      "content": "\"use client\"\n\nimport { useEffect, useRef } from \"react\"\nimport { cn } from \"@/lib/utils\"\n\nexport type ShaderMetalTheme = \"light\" | \"dark\" | \"auto\"\n\nexport type ShaderMetalMetal = \"chrome\" | \"gold\" | \"copper\" | \"graphite\"\n\nexport type ShaderMetalOptions = {\n  /** Which metal the ribbons are cast in. Default `chrome`. */\n  metal?: ShaderMetalMetal\n  /** Custom metal tint, any CSS hex. Wins over `metal`. */\n  color?: string\n  /** How many ribbons, 1–5. Default `3`. */\n  ribbons?: number\n  /** Flow speed. Default `0.3`. */\n  speed?: number\n  /** Thin-film sheen on the turns, 0–1. `0` is bare metal. Default `0.2`. */\n  iridescence?: number\n  /** The key light follows the pointer and the ribbons lean toward it. Default `true`. */\n  interactive?: boolean\n  /**\n   * Studio lighting. Default `auto` follows shadcn / next-themes\n   * (`html.dark` class): a bright softbox studio in light mode, a black\n   * stage with strip lights in dark.\n   */\n  theme?: ShaderMetalTheme\n  /** Fires whenever resolved dark mode changes. */\n  onThemeChange?: (dark: boolean) => void\n}\n\nexport type ShaderMetalInstance = {\n  setOptions: (options: Partial<ShaderMetalOptions>) => void\n  destroy: () => void\n}\n\n/** Base reflectance per metal, as sRGB hex. */\nexport const METALS: Record<ShaderMetalMetal, string> = {\n  chrome: \"#F2F3F5\",\n  gold: \"#FFD38A\",\n  copper: \"#F7AE92\",\n  graphite: \"#8E9096\",\n}\n\nexport const DEFAULT_METAL: ShaderMetalMetal = \"chrome\"\nexport const DEFAULT_RIBBONS = 3\nexport const DEFAULT_SPEED = 0.3\nexport const DEFAULT_IRIDESCENCE = 0.2\n\nconst VERT = `\nattribute vec2 a_position;\nvoid main() {\n  gl_Position = vec4(a_position, 0.0, 1.0);\n}\n`\n\nconst FRAG = `\nprecision highp float;\n\nuniform vec2 u_resolution;\nuniform float u_time;\nuniform float u_count;\nuniform float u_iridescence;\nuniform float u_pull;\nuniform float u_dark;\nuniform vec2 u_mouse;\nuniform vec2 u_light;\nuniform vec3 u_f0;\n\nconst float TAU = 6.28318530718;\nconst float TILT = -0.32;\n\nfloat hash(vec2 p) {\n  vec3 p3 = fract(vec3(p.xyx) * 0.1031);\n  p3 += dot(p3, p3.yzx + 33.33);\n  return fract((p3.x + p3.y) * p3.z);\n}\n\n// Smooth 1D value noise, for the brushed grain that runs along each ribbon.\nfloat lines(float x, float seed) {\n  float i = floor(x);\n  float f = fract(x);\n  f = f * f * (3.0 - 2.0 * f);\n  return mix(hash(vec2(i, seed)), hash(vec2(i + 1.0, seed)), f);\n}\n\nvec2 rotate(vec2 p, float a) {\n  float c = cos(a);\n  float s = sin(a);\n  return vec2(c * p.x - s * p.y, s * p.x + c * p.y);\n}\n\n// The spine of ribbon k: three swells at different speeds, so it never\n// settles into a loop. Near the pointer it leans toward it.\nfloat spine(float x, float k, float lane, float t, vec2 m) {\n  float y = lane;\n  y += 0.13 * sin(1.15 * x + 0.55 * t + 2.1 * k);\n  y += 0.06 * sin(2.6 * x - 0.8 * t + 1.3 * k + 0.7);\n  y += 0.025 * sin(5.3 * x + 1.3 * t + 4.0 * k);\n  float near = exp(-(x - m.x) * (x - m.x) * 5.0);\n  y += (m.y - y) * 0.22 * u_pull * near;\n  return y;\n}\n\n// A studio to reflect: sky and floor meet at a crisp horizon, a big\n// softbox overhead, two strip lights and the key light. Linear light.\nvec3 studio(vec3 r, float t) {\n  float a = 0.28 * sin(t * 0.11);\n  r.xz = rotate(r.xz, a);\n  float y = r.y;\n\n  vec3 top = mix(vec3(0.82, 0.83, 0.86), vec3(0.025, 0.025, 0.028), u_dark);\n  vec3 horizon = mix(vec3(1.0), vec3(0.16, 0.16, 0.17), u_dark);\n  vec3 ground = mix(vec3(0.05, 0.05, 0.055), vec3(0.004), u_dark);\n  vec3 sky = mix(horizon, top, smoothstep(0.0, 0.85, y));\n  vec3 floor_ = mix(ground * 2.4, ground, smoothstep(0.0, -0.5, y));\n  vec3 c = mix(floor_, sky, smoothstep(-0.025, 0.025, y));\n\n  // Overhead softbox, feathered.\n  float box = smoothstep(0.6, 0.45, abs(r.x)) * smoothstep(0.42, 0.55, y) *\n              smoothstep(1.0, 0.85, y);\n  c += box * mix(1.6, 2.2, u_dark);\n\n  // Tall strip lights left and right: warm and cool, faintly.\n  float stripL = smoothstep(0.1, 0.03, abs(r.x + 0.78)) * smoothstep(-0.35, -0.1, y) * smoothstep(0.8, 0.5, y);\n  float stripR = smoothstep(0.08, 0.02, abs(r.x - 0.72)) * smoothstep(-0.3, -0.05, y) * smoothstep(0.75, 0.45, y);\n  c += stripL * vec3(1.25, 1.1, 0.95) * mix(1.0, 1.5, u_dark);\n  c += stripR * vec3(0.9, 1.05, 1.3) * mix(0.8, 1.3, u_dark);\n\n  // A low bounce card, so the undersides aren't dead.\n  float card = smoothstep(0.7, 0.2, abs(r.x)) * smoothstep(-0.95, -0.7, y) * smoothstep(-0.4, -0.65, y);\n  c += card * mix(0.35, 0.18, u_dark);\n  return c;\n}\n\nvec3 tonemap(vec3 x) {\n  x = max(x, 0.0);\n  return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0);\n}\n\nvec3 linearToSrgb(vec3 c) {\n  c = clamp(c, 0.0, 1.0);\n  vec3 lo = c * 12.92;\n  vec3 hi = 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055;\n  return mix(lo, hi, step(vec3(0.0031308), c));\n}\n\nvoid main() {\n  vec2 frag = gl_FragCoord.xy;\n  float px = 1.0 / u_resolution.y;\n  vec2 p = rotate((frag - 0.5 * u_resolution) * px, TILT);\n  vec2 m = rotate((u_mouse - 0.5) * u_resolution * px, TILT);\n  float t = u_time;\n\n  vec3 L = normalize(vec3(u_light, 0.85));\n  float count = u_count;\n  float spread = mix(0.0, 0.3, clamp((count - 1.0) / 4.0, 0.0, 1.0)) + 0.08 * step(1.5, count);\n  float baseWidth = mix(0.13, 0.075, clamp((count - 1.0) / 4.0, 0.0, 1.0));\n\n  // Premultiplied, composited back to front.\n  vec3 col = vec3(0.0);\n  float alpha = 0.0;\n\n  for (int i = 0; i < 5; i++) {\n    float k = float(i);\n    if (k >= count) break;\n    float f = count > 1.0 ? k / (count - 1.0) : 0.5;\n    float lane = (f - 0.5) * spread;\n    // Farther ribbons drift a touch slower: a hint of depth.\n    float tk = t * mix(0.82, 1.0, f);\n\n    float x = p.x;\n    float e = 0.004;\n    float c = spine(x, k, lane, tk, m);\n    float slope = (spine(x + e, k, lane, tk, m) - spine(x - e, k, lane, tk, m)) / (2.0 * e);\n    float stretch = inversesqrt(1.0 + slope * slope);\n    float d = (p.y - c) * stretch;\n\n    // Twist along the length; width breathes.\n    float theta = 1.5 * x + 0.45 * tk + 1.7 * k + 0.55 * sin(0.7 * x - 0.3 * tk + k);\n    float ct = cos(theta);\n    float st = sin(theta);\n    float hw = baseWidth * (0.82 + 0.22 * sin(0.9 * x + 0.35 * tk + 2.3 * k));\n    // Seen edge-on a ribbon still has a little thickness.\n    float proj = hw * max(abs(ct), 0.035);\n\n    // Soft contact shadow on whatever is behind, before the ribbon lands.\n    float sd = d + 0.045;\n    float shadowW = proj + 0.05;\n    float shadow = (1.0 - smoothstep(0.35, 1.0, abs(sd) / shadowW)) * mix(0.2, 0.32, u_dark);\n    col *= 1.0 - shadow;\n    alpha = alpha + shadow * (1.0 - alpha);\n\n    float v = d / (ct >= 0.0 ? proj : -proj);\n    float aa = 1.2 * px / proj;\n    float cover = 1.0 - smoothstep(1.0 - aa, 1.0 + aa, abs(v));\n    if (cover <= 0.0) continue;\n\n    // Ribbon frame: T along, N2 across in the screen plane, Z toward us.\n    vec3 T = normalize(vec3(1.0, slope, 0.0));\n    vec3 N2 = normalize(vec3(-slope, 1.0, 0.0));\n    vec3 B = ct * N2 + st * vec3(0.0, 0.0, 1.0);\n    vec3 n = -st * N2 + ct * vec3(0.0, 0.0, 1.0);\n\n    // A shallow arc across the width, rolling over at the edges, plus\n    // slow ripples running down the length and a brushed grain.\n    float roll = v * 0.45 + v * v * v * 0.5;\n    float ripple = sin(x * 13.0 - tk * 2.2 + k * 3.1) * 0.06 + sin(x * 31.0 + tk * 1.3 + k) * 0.015;\n    float grain = lines(v * 34.0 + k * 17.0, k) - 0.5;\n    n = normalize(n + B * (roll + grain * 0.015) + T * ripple);\n    // Edge-on, what shows is the strip's polished edge. It faces us, tipped\n    // up a little so it catches the sky rather than sitting on the horizon.\n    float edge = smoothstep(0.14, 0.03, abs(ct));\n    vec3 rim = B * (st >= 0.0 ? 1.0 : -1.0) + vec3(0.0, 0.2, 0.0);\n    n = normalize(mix(n, normalize(rim), edge));\n    float back = step(n.z, 0.0);\n    n *= 1.0 - 2.0 * back;\n\n    vec3 r = reflect(vec3(0.0, 0.0, -1.0), n);\n    vec3 env = studio(r, t);\n\n    float facing = clamp(n.z, 0.0, 1.0);\n    vec3 F = u_f0 + (1.0 - u_f0) * pow(1.0 - facing, 5.0);\n\n    // Thin film: thickness drifts along the ribbon, and the colour shifts\n    // with the viewing angle, strongest on the turns.\n    float film = 0.5 + 0.5 * sin(x * 1.6 + v * 0.9 + tk * 0.35 + k * 1.9);\n    vec3 rainbow = 0.5 + 0.5 * cos(TAU * (film * 1.6 + facing * 1.1 + vec3(0.0, 0.33, 0.67)));\n    // Keep the film's brightness level, so it tints the metal, never dims it.\n    rainbow = 0.2 + 0.8 * rainbow;\n    rainbow /= max(dot(rainbow, vec3(0.2126, 0.7152, 0.0722)), 0.2);\n    float sheen = u_iridescence * (0.45 + 0.55 * (1.0 - facing));\n    F *= mix(vec3(1.0), rainbow, sheen);\n\n    float rl = max(dot(r, L), 0.0);\n    float spec = pow(rl, 90.0) * 5.0 + pow(rl, 14.0) * 0.55;\n\n    vec3 lit = env * F + spec * F;\n    lit *= mix(1.0, 0.72, back) * (0.94 + grain * 0.12);\n    lit *= mix(0.86, 1.0, f);\n    vec3 rgb = linearToSrgb(tonemap(lit * 1.05));\n\n    col = col * (1.0 - cover) + rgb * cover;\n    alpha = alpha * (1.0 - cover) + cover;\n  }\n\n  // A hair of dither, so the long gradients never band. Faint shadows\n  // need it on alpha too, or they step into contour lines.\n  float dither = (hash(frag + fract(t) * 91.0) - 0.5) / 255.0;\n  col += dither * alpha;\n  alpha = clamp(alpha + dither * step(0.0005, alpha) * 1.5, 0.0, 1.0);\n  gl_FragColor = vec4(clamp(col, 0.0, 1.0), alpha);\n}\n`\n\nfunction isDev() {\n  return (\n    typeof process !== \"undefined\" && process.env?.NODE_ENV !== \"production\"\n  )\n}\n\nfunction clamp(value: number, min: number, max: number) {\n  return Math.min(max, Math.max(min, value))\n}\n\nfunction numberOr(value: unknown, fallback: number, min: number, max: number) {\n  if (typeof value !== \"number\" || !Number.isFinite(value)) return fallback\n  return clamp(value, min, max)\n}\n\n/** Hex → linear RGB, for the metal's base reflectance. */\nfunction hexToLinear(hex: string): [number, number, number] {\n  const h = hex.replace(\"#\", \"\").trim()\n  const full =\n    h.length === 3\n      ? h\n          .split(\"\")\n          .map((c) => c + c)\n          .join(\"\")\n      : h.padEnd(6, \"0\").slice(0, 6)\n  const n = Number.parseInt(full, 16)\n  const srgb = Number.isNaN(n)\n    ? [0.8, 0.8, 0.8]\n    : [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255]\n  return srgb.map((c) =>\n    c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4\n  ) as [number, number, number]\n}\n\n/** Resolves shadcn / next-themes dark mode (`attribute=\"class\"` → `html.dark`). */\nexport function isDarkTheme(): boolean {\n  if (typeof document === \"undefined\") return false\n  const root = document.documentElement\n  if (root.classList.contains(\"dark\")) return true\n  if (root.classList.contains(\"light\")) return false\n  const dataTheme = root.getAttribute(\"data-theme\")\n  if (dataTheme === \"dark\") return true\n  if (dataTheme === \"light\") return false\n  return window.matchMedia(\"(prefers-color-scheme: dark)\").matches\n}\n\nexport function resolveDark(theme: ShaderMetalTheme): boolean {\n  if (theme === \"dark\") return true\n  if (theme === \"light\") return false\n  return isDarkTheme()\n}\n\nfunction compile(gl: WebGLRenderingContext, type: number, source: string) {\n  const shader = gl.createShader(type)\n  if (!shader) return null\n  gl.shaderSource(shader, source)\n  gl.compileShader(shader)\n  if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {\n    if (isDev()) {\n      console.warn(\n        \"ShaderMetal: shader failed to compile\\n\",\n        gl.getShaderInfoLog(shader)\n      )\n    }\n    gl.deleteShader(shader)\n    return null\n  }\n  return shader\n}\n\n/** Most pixels we'll shade per frame; past this the canvas renders softer. */\nconst MAX_PIXELS = 2_400_000\n/** Where the key light rests when the pointer is away: up and to the left. */\nconst REST_LIGHT = { x: -0.45, y: 0.55 }\n\n/**\n * Liquid metal ribbons that twist through a studio light rig. Each ribbon\n * is a curved, brushed strip reflecting a softbox, strip lights and a\n * crisp horizon, with a key light the pointer can steer. The canvas is\n * transparent, so the ribbons and their soft shadows sit on any page.\n * Theme-aware; pauses off-screen and in hidden tabs; holds a still frame\n * under `prefers-reduced-motion`. Recovers from a lost WebGL context.\n */\nexport function createShaderMetal(\n  canvas: HTMLCanvasElement,\n  initial: ShaderMetalOptions = {}\n): ShaderMetalInstance | null {\n  let options: ShaderMetalOptions = {\n    interactive: true,\n    theme: \"auto\",\n    ...initial,\n  }\n\n  const mouse = { x: 0.5, y: 0.5 }\n  const target = { x: 0.5, y: 0.5 }\n  const light = { ...REST_LIGHT }\n  const lightTarget = { ...REST_LIGHT }\n  let pull = 0\n  let pullTarget = 0\n  let dark = resolveDark(options.theme ?? \"auto\")\n  let darkMix = dark ? 1 : 0\n  options.onThemeChange?.(dark)\n\n  const gl = canvas.getContext(\"webgl\", {\n    alpha: true,\n    premultipliedAlpha: true,\n    antialias: false,\n    depth: false,\n    stencil: false,\n    powerPreference: \"high-performance\",\n  })\n  if (!gl) return null\n\n  let vs: WebGLShader | null = null\n  let fs: WebGLShader | null = null\n  let program: WebGLProgram | null = null\n  let buf: WebGLBuffer | null = null\n  let uResolution: WebGLUniformLocation | null = null\n  let uTime: WebGLUniformLocation | null = null\n  let uCount: WebGLUniformLocation | null = null\n  let uIridescence: WebGLUniformLocation | null = null\n  let uPull: WebGLUniformLocation | null = null\n  let uDark: WebGLUniformLocation | null = null\n  let uMouse: WebGLUniformLocation | null = null\n  let uLight: WebGLUniformLocation | null = null\n  let uF0: WebGLUniformLocation | null = null\n\n  // Builds every GL resource. Runs at startup and again after a lost\n  // context is restored, since a restore hands back a blank context.\n  const initGl = () => {\n    vs = compile(gl, gl.VERTEX_SHADER, VERT)\n    fs = compile(gl, gl.FRAGMENT_SHADER, FRAG)\n    if (!vs || !fs) {\n      if (vs) gl.deleteShader(vs)\n      if (fs) gl.deleteShader(fs)\n      return false\n    }\n\n    program = gl.createProgram()\n    if (!program) {\n      gl.deleteShader(vs)\n      gl.deleteShader(fs)\n      return false\n    }\n    gl.attachShader(program, vs)\n    gl.attachShader(program, fs)\n    gl.linkProgram(program)\n    if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {\n      if (isDev()) {\n        console.warn(\n          \"ShaderMetal: program failed to link\\n\",\n          gl.getProgramInfoLog(program)\n        )\n      }\n      gl.deleteProgram(program)\n      gl.deleteShader(vs)\n      gl.deleteShader(fs)\n      return false\n    }\n    gl.useProgram(program)\n\n    buf = gl.createBuffer()\n    gl.bindBuffer(gl.ARRAY_BUFFER, buf)\n    gl.bufferData(\n      gl.ARRAY_BUFFER,\n      new Float32Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1]),\n      gl.STATIC_DRAW\n    )\n    const loc = gl.getAttribLocation(program, \"a_position\")\n    gl.enableVertexAttribArray(loc)\n    gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0)\n\n    const prog = program\n    const u = (name: string) => gl.getUniformLocation(prog, name)\n    uResolution = u(\"u_resolution\")\n    uTime = u(\"u_time\")\n    uCount = u(\"u_count\")\n    uIridescence = u(\"u_iridescence\")\n    uPull = u(\"u_pull\")\n    uDark = u(\"u_dark\")\n    uMouse = u(\"u_mouse\")\n    uLight = u(\"u_light\")\n    uF0 = u(\"u_f0\")\n    return true\n  }\n  if (!initGl()) return null\n\n  // Ribbon edges are hard, so this one renders at device resolution (up to 2x).\n  // Returns true when the backing store changed.\n  const resize = () => {\n    const parent = canvas.parentElement\n    if (!parent) return false\n    const w = parent.clientWidth\n    const h = parent.clientHeight\n    if (w <= 0 || h <= 0) return false\n    const ratio = Math.min(\n      window.devicePixelRatio || 1,\n      2,\n      Math.sqrt(MAX_PIXELS / (w * h))\n    )\n    const width = Math.max(1, Math.floor(w * ratio))\n    const height = Math.max(1, Math.floor(h * ratio))\n    canvas.style.width = `${w}px`\n    canvas.style.height = `${h}px`\n    wake()\n    // Assigning a size clears the canvas, even the same size.\n    if (width === canvas.width && height === canvas.height) return false\n    canvas.width = width\n    canvas.height = height\n    gl.viewport(0, 0, canvas.width, canvas.height)\n    return true\n  }\n\n  let f0Key = \"\"\n  const syncMetal = () => {\n    const hex =\n      options.color || METALS[options.metal ?? DEFAULT_METAL] || METALS.chrome\n    if (hex === f0Key) return\n    f0Key = hex\n    gl.uniform3fv(uF0, hexToLinear(hex))\n  }\n\n  const mqReduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\")\n  let onScreen = true\n  let raf = 0\n  let destroyed = false\n  let lost = false\n  let last = 0\n  let clock = 0\n\n  const visible = () => onScreen && document.visibilityState !== \"hidden\"\n\n  function wake() {\n    // An observer callback can still arrive after destroy.\n    if (raf || destroyed || lost) return\n    last = performance.now()\n    raf = requestAnimationFrame(tick)\n  }\n\n  // Still easing toward the pointer or the theme, even with motion paused.\n  const settling = () =>\n    Math.abs(target.x - mouse.x) > 1e-3 ||\n    Math.abs(target.y - mouse.y) > 1e-3 ||\n    Math.abs(lightTarget.x - light.x) > 1e-3 ||\n    Math.abs(lightTarget.y - light.y) > 1e-3 ||\n    Math.abs(pullTarget - pull) > 1e-3 ||\n    Math.abs((dark ? 1 : 0) - darkMix) > 1e-3\n\n  // An arrow, not a declaration, so TypeScript keeps `gl` narrowed inside.\n  const tick = (now: number) => {\n    raf = 0\n    if (lost) return\n    const dt = Math.min(0.05, Math.max(0, (now - last) / 1000))\n    last = now\n    if (!visible()) return\n    const flowing = !mqReduce.matches\n    if (flowing) clock += dt\n\n    // Frame-rate independent easing.\n    const ease = (rate: number) => 1 - Math.exp(-dt * rate)\n    mouse.x += (target.x - mouse.x) * ease(3)\n    mouse.y += (target.y - mouse.y) * ease(3)\n    light.x += (lightTarget.x - light.x) * ease(5)\n    light.y += (lightTarget.y - light.y) * ease(5)\n    pull += (pullTarget - pull) * ease(2.5)\n    darkMix += ((dark ? 1 : 0) - darkMix) * ease(6)\n\n    syncMetal()\n    const speed = numberOr(options.speed, DEFAULT_SPEED, 0, 4)\n    gl.uniform2f(uResolution, canvas.width, canvas.height)\n    gl.uniform1f(uTime, clock * speed * 3 + 11)\n    gl.uniform1f(\n      uCount,\n      Math.round(numberOr(options.ribbons, DEFAULT_RIBBONS, 1, 5))\n    )\n    gl.uniform1f(\n      uIridescence,\n      numberOr(options.iridescence, DEFAULT_IRIDESCENCE, 0, 1)\n    )\n    gl.uniform1f(uPull, options.interactive === false ? 0 : pull)\n    gl.uniform1f(uDark, darkMix)\n    gl.uniform2f(uMouse, mouse.x, mouse.y)\n    gl.uniform2f(uLight, light.x, light.y)\n    gl.clearColor(0, 0, 0, 0)\n    gl.clear(gl.COLOR_BUFFER_BIT)\n    gl.drawArrays(gl.TRIANGLES, 0, 6)\n\n    if (flowing || settling()) raf = requestAnimationFrame(tick)\n  }\n\n  /**\n   * Draws now: resizing cleared the canvas, and waiting for the next frame\n   * would show it blank for one.\n   */\n  const redraw = () => {\n    if (destroyed || lost) return\n    cancelAnimationFrame(raf)\n    raf = 0\n    last = performance.now()\n    tick(last)\n  }\n\n  const ro = new ResizeObserver(() => {\n    if (resize()) redraw()\n  })\n  if (canvas.parentElement) ro.observe(canvas.parentElement)\n  const io = new IntersectionObserver(([entry]) => {\n    onScreen = entry?.isIntersecting ?? true\n    wake()\n  })\n  io.observe(canvas)\n  const onVisibility = () => wake()\n  document.addEventListener(\"visibilitychange\", onVisibility)\n  const onReduce = () => wake()\n  mqReduce.addEventListener(\"change\", onReduce)\n\n  const syncTheme = () => {\n    const next = resolveDark(options.theme ?? \"auto\")\n    if (next === dark) return\n    dark = next\n    options.onThemeChange?.(dark)\n    wake()\n  }\n  const mo = new MutationObserver(syncTheme)\n  mo.observe(document.documentElement, {\n    attributes: true,\n    attributeFilter: [\"class\", \"data-theme\", \"style\"],\n  })\n  const mqDark = window.matchMedia(\"(prefers-color-scheme: dark)\")\n  mqDark.addEventListener(\"change\", syncTheme)\n\n  const onMove = (e: PointerEvent) => {\n    if (options.interactive === false) return\n    const rect = canvas.getBoundingClientRect()\n    if (rect.width <= 0 || rect.height <= 0) return\n    const x = (e.clientX - rect.left) / rect.width\n    const y = 1 - (e.clientY - rect.top) / rect.height\n    const inside = x >= 0 && x <= 1 && y >= 0 && y <= 1\n    pullTarget = inside ? 1 : 0\n    if (inside) {\n      target.x = x\n      target.y = y\n      lightTarget.x = (x * 2 - 1) * 0.9\n      lightTarget.y = (y * 2 - 1) * 0.9\n    } else {\n      lightTarget.x = REST_LIGHT.x\n      lightTarget.y = REST_LIGHT.y\n    }\n    wake()\n  }\n  window.addEventListener(\"pointermove\", onMove, { passive: true })\n\n  // Without preventDefault the browser never restores a lost context.\n  const onLost = (e: Event) => {\n    e.preventDefault()\n    lost = true\n    cancelAnimationFrame(raf)\n    raf = 0\n  }\n  const onRestored = () => {\n    if (destroyed || !initGl()) return\n    lost = false\n    f0Key = \"\"\n    gl.viewport(0, 0, canvas.width, canvas.height)\n    wake()\n  }\n  canvas.addEventListener(\"webglcontextlost\", onLost)\n  canvas.addEventListener(\"webglcontextrestored\", onRestored)\n\n  resize()\n  wake()\n\n  return {\n    setOptions(next) {\n      options = { ...options, ...next }\n      if (options.interactive === false) {\n        pullTarget = 0\n        lightTarget.x = REST_LIGHT.x\n        lightTarget.y = REST_LIGHT.y\n      }\n      syncTheme()\n      wake()\n    },\n    destroy() {\n      destroyed = true\n      cancelAnimationFrame(raf)\n      raf = 0\n      ro.disconnect()\n      io.disconnect()\n      mo.disconnect()\n      document.removeEventListener(\"visibilitychange\", onVisibility)\n      mqReduce.removeEventListener(\"change\", onReduce)\n      mqDark.removeEventListener(\"change\", syncTheme)\n      window.removeEventListener(\"pointermove\", onMove)\n      canvas.removeEventListener(\"webglcontextlost\", onLost)\n      canvas.removeEventListener(\"webglcontextrestored\", onRestored)\n      gl.deleteProgram(program)\n      gl.deleteShader(vs)\n      gl.deleteShader(fs)\n      gl.deleteBuffer(buf)\n      // Free the GPU context once the canvas has left the page; browsers cap\n      // live contexts at about 16. A canvas still in the page (a React\n      // Strict Mode replay) keeps it, so a new engine can reuse it.\n      const lose = gl.getExtension(\"WEBGL_lose_context\")\n      queueMicrotask(() => {\n        if (!canvas.isConnected && !gl.isContextLost()) lose?.loseContext()\n      })\n    },\n  }\n}\n\nexport type ShaderMetalProps = Omit<ShaderMetalOptions, \"onThemeChange\"> & {\n  className?: string\n}\n\n/**\n * Liquid metal ribbons twisting through a studio light rig: brushed,\n * curved strips with soft shadows on a transparent canvas. Theme-aware.\n */\nexport function ShaderMetal({\n  className,\n  metal = DEFAULT_METAL,\n  color,\n  ribbons = DEFAULT_RIBBONS,\n  speed = DEFAULT_SPEED,\n  iridescence = DEFAULT_IRIDESCENCE,\n  interactive = true,\n  theme = \"auto\",\n}: ShaderMetalProps) {\n  const canvasRef = useRef<HTMLCanvasElement>(null)\n  const instanceRef = useRef<ShaderMetalInstance | null>(null)\n\n  useEffect(() => {\n    const canvas = canvasRef.current\n    if (!canvas) return\n    instanceRef.current = createShaderMetal(canvas, {\n      metal,\n      color,\n      ribbons,\n      speed,\n      iridescence,\n      interactive,\n      theme,\n    })\n    return () => {\n      instanceRef.current?.destroy()\n      instanceRef.current = null\n    }\n    // Engine reads live options via setOptions; mount once.\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [])\n\n  useEffect(() => {\n    instanceRef.current?.setOptions({\n      metal,\n      color,\n      ribbons,\n      speed,\n      iridescence,\n      interactive,\n      theme,\n    })\n  }, [metal, color, ribbons, speed, iridescence, interactive, theme])\n\n  return (\n    <div\n      data-slot=\"shader-metal\"\n      aria-hidden\n      className={cn(\n        \"pointer-events-none absolute inset-0 overflow-hidden rounded-[inherit] [mask-image:linear-gradient(#000,#000)]\",\n        className\n      )}\n    >\n      {/* Firefox can hand a WebGL canvas straight to the system compositor,\n          which ignores rounded clips. The mask on the root keeps it in the\n          page's own layer, so the parent's radius holds. */}\n      <canvas\n        ref={canvasRef}\n        className=\"absolute inset-0 size-full rounded-[inherit]\"\n      />\n    </div>\n  )\n}\n",
      "type": "registry:ui",
      "target": "components/ui/shader-metal.tsx"
    }
  ],
  "type": "registry:ui"
}