{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"mirror-ripple","type":"registry:ui","title":"Mirror Ripple","description":"A slowly folding mirror-like chrome surface that ripples wherever the cursor moves or clicks. WebGL.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/mirror-ripple.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface MirrorRippleProps {\n  /** Metal tint. */\n  color?: string;\n  /** Animation speed multiplier. */\n  speed?: number;\n  /** Wave frequency; higher packs more folds in. */\n  scale?: number;\n  /** Height of the flowing folds. */\n  amplitude?: number;\n  /** Cursor and click ripples. */\n  interactive?: boolean;\n  className?: string;\n}\n\nconst MAX_RIPPLES = 8;\n\nconst FRAG = `precision highp float;\nuniform vec2 uRes;\nuniform float uTime;\nuniform vec3 uColor;\nuniform float uScale;\nuniform float uAmp;\nuniform vec4 uRip[${MAX_RIPPLES}];\n\nfloat field(vec2 p){\n  float t = uTime;\n  vec2 q = p * uScale;\n  q += 0.45 * vec2(sin(q.y * 1.2 + t * 0.6), sin(q.x * 1.1 - t * 0.5));\n  q += 0.25 * vec2(sin(q.y * 2.3 - t * 0.4 + 1.3), sin(q.x * 2.1 + t * 0.45 + 2.1));\n  float h = sin(q.x * 1.3 + t * 0.5) * 0.5 + sin(q.y * 1.7 - t * 0.4) * 0.5\n          + sin((q.x + q.y) * 0.9 + t * 0.3) * 0.35 + sin(length(q) * 1.1 - t * 0.7) * 0.2;\n  h *= uAmp / uScale;\n  for (int i = 0; i < ${MAX_RIPPLES}; i++) {\n    vec4 r = uRip[i];\n    if (r.w > 0.001) {\n      float x = length(p - r.xy) - r.z * 0.5;\n      h += sin(x * 42.0) * exp(-x * x * 90.0) * exp(-r.z * 1.4) * r.w * 0.02;\n    }\n  }\n  return h;\n}\n\nfloat studio(vec3 r){\n  float y = r.y * 0.85 + r.x * 0.3;\n  float bands = pow(0.5 + 0.5 * sin(y * 6.5 + 1.2), 5.0) * 0.9 + pow(0.5 + 0.5 * sin(y * 13.0 - 0.7), 8.0) * 0.35;\n  return 0.03 + bands + 0.35 * smoothstep(0.1, 1.0, y) + 0.08 * smoothstep(-0.2, -0.9, y);\n}\n\nvoid main(){\n  vec2 p = gl_FragCoord.xy / uRes.y;\n  float e = 1.5 / uRes.y;\n  float hx = field(p + vec2(e, 0.0)) - field(p - vec2(e, 0.0));\n  float hy = field(p + vec2(0.0, e)) - field(p - vec2(0.0, e));\n  vec3 n = normalize(vec3(-hx / (2.0 * e), -hy / (2.0 * e), 1.0 / 0.9));\n  vec3 r = reflect(vec3(0.0, 0.0, -1.0), n);\n\n  float c = studio(r);\n  vec3 L = normalize(vec3(-0.45, 0.6, 0.65));\n  float spec = pow(max(dot(r, L), 0.0), 70.0);\n  float fres = pow(1.0 - n.z, 2.0);\n  vec3 col = uColor * c + vec3(spec * 1.1) + uColor * fres * 0.6;\n  col = mix(col, col * vec3(0.8, 0.9, 1.1), smoothstep(0.0, 0.5, r.x)); // cool sky tint on one side\n  col += (fract(sin(dot(gl_FragCoord.xy, vec2(12.9898, 78.233))) * 43758.5453) - 0.5) / 255.0;\n  gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);\n}`;\n\nfunction hexToRgb(hex: string) {\n  const n = parseInt(hex.replace(\"#\", \"\").padEnd(6, \"0\").slice(0, 6), 16);\n  return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];\n}\n\ntype SetUniform = (name: string, v: number | number[], size?: number) => void;\n\n/** Full-screen-triangle WebGL runner: sizes to its host, pauses offscreen, one static frame under reduced motion. */\nfunction mountShader(host: HTMLElement, frag: string, frame: (set: SetUniform, dt: number) => void) {\n  const canvas = document.createElement(\"canvas\");\n  canvas.style.cssText = \"position:absolute;inset:0;width:100%;height:100%;display:block\";\n  const gl = canvas.getContext(\"webgl\", { antialias: false, premultipliedAlpha: true });\n  if (!gl) return null;\n  const prog = gl.createProgram()!;\n  const stages: [number, string][] = [\n    [gl.VERTEX_SHADER, \"attribute vec2 p;void main(){gl_Position=vec4(p,0.,1.);}\"],\n    [gl.FRAGMENT_SHADER, frag],\n  ];\n  for (const [type, src] of stages) {\n    const s = gl.createShader(type)!;\n    gl.shaderSource(s, src);\n    gl.compileShader(s);\n    if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) console.warn(gl.getShaderInfoLog(s));\n    gl.attachShader(prog, s);\n  }\n  gl.bindAttribLocation(prog, 0, \"p\");\n  gl.linkProgram(prog);\n  if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {\n    gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n    return null;\n  }\n  gl.useProgram(prog);\n  gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());\n  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);\n  gl.enableVertexAttribArray(0);\n  gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n  host.appendChild(canvas);\n\n  const locs = new Map<string, WebGLUniformLocation | null>();\n  const setters = [gl.uniform1fv, gl.uniform2fv, gl.uniform3fv, gl.uniform4fv];\n  const set: SetUniform = (name, v, size) => {\n    if (!locs.has(name)) locs.set(name, gl.getUniformLocation(prog, name));\n    const arr = typeof v === \"number\" ? [v] : v;\n    setters[(size ?? arr.length) - 1].call(gl, locs.get(name)!, arr);\n  };\n\n  const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n  let raf = 0;\n  let last = 0;\n  const render = (dt: number) => {\n    set(\"uRes\", [canvas.width, canvas.height]);\n    frame(set, dt);\n    gl.drawArrays(gl.TRIANGLES, 0, 3);\n  };\n  const loop = (now: number) => {\n    render(last ? Math.min((now - last) / 1000, 0.1) : 0);\n    last = now;\n    raf = requestAnimationFrame(loop);\n  };\n  const resize = () => {\n    const dpr = Math.min(window.devicePixelRatio || 1, 2);\n    canvas.width = Math.max(1, Math.round(host.clientWidth * dpr));\n    canvas.height = Math.max(1, Math.round(host.clientHeight * dpr));\n    gl.viewport(0, 0, canvas.width, canvas.height);\n    render(0);\n  };\n  const ro = new ResizeObserver(resize);\n  ro.observe(host);\n  const io = new IntersectionObserver(([e]) => {\n    cancelAnimationFrame(raf);\n    raf = 0;\n    last = 0;\n    if (e.isIntersecting && !reduced) raf = requestAnimationFrame(loop);\n  });\n  io.observe(host);\n\n  return {\n    redraw: () => {\n      if (!raf) render(0);\n    },\n    destroy: () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\n    },\n  };\n}\n\nexport function MirrorRipple({\n  color = \"#d4dcec\",\n  speed = 1,\n  scale = 3,\n  amplitude = 0.6,\n  interactive = true,\n  className,\n}: MirrorRippleProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ color: hexToRgb(color), speed, scale, amplitude, interactive });\n  const redraw = useRef<() => void>(undefined);\n\n  useEffect(() => {\n    opts.current = { color: hexToRgb(color), speed, scale, amplitude, interactive };\n    redraw.current?.();\n  }, [color, speed, scale, amplitude, interactive]);\n\n  useEffect(() => {\n    const el = host.current!;\n    const area = el.parentElement ?? el;\n    // Ripple ring buffer: x, y (container-height units, y up), age, strength.\n    const rip = new Float32Array(MAX_RIPPLES * 4);\n    let next = 0;\n    let t = 6;\n    let idle = 0;\n    let lastX = -1;\n    let lastY = -1;\n    const add = (x: number, y: number, s: number) => {\n      rip.set([x, y, 0, s], next * 4);\n      next = (next + 1) % MAX_RIPPLES;\n    };\n\n    const shader = mountShader(el, FRAG, (set, dt) => {\n      const o = opts.current;\n      t += dt * o.speed;\n      for (let i = 0; i < MAX_RIPPLES; i++) {\n        rip[i * 4 + 2] += dt;\n        if (rip[i * 4 + 2] > 3) rip[i * 4 + 3] = 0;\n      }\n      // A gentle drop now and then when nobody is touching it.\n      idle += dt;\n      if (o.interactive && idle > 2.6) {\n        idle = 0;\n        const aspect = el.clientWidth / Math.max(el.clientHeight, 1);\n        add((0.2 + Math.random() * 0.6) * aspect, 0.2 + Math.random() * 0.6, 0.8);\n      }\n      set(\"uTime\", t);\n      set(\"uColor\", o.color);\n      set(\"uScale\", Math.max(o.scale, 0.2));\n      set(\"uAmp\", o.amplitude);\n      set(\"uRip\", o.interactive ? Array.from(rip) : new Array(MAX_RIPPLES * 4).fill(0), 4);\n    });\n    if (!shader) return;\n    redraw.current = shader.redraw;\n\n    const local = (e: PointerEvent) => {\n      const r = el.getBoundingClientRect();\n      return [(e.clientX - r.left) / r.height, 1 - (e.clientY - r.top) / r.height];\n    };\n    const onMove = (e: PointerEvent) => {\n      const [x, y] = local(e);\n      idle = 0;\n      if (Math.hypot(x - lastX, y - lastY) < 0.06) return;\n      lastX = x;\n      lastY = y;\n      add(x, y, 0.6);\n    };\n    const onDown = (e: PointerEvent) => {\n      const [x, y] = local(e);\n      idle = 0;\n      add(x, y, 1.6);\n    };\n    area.addEventListener(\"pointermove\", onMove, { passive: true });\n    area.addEventListener(\"pointerdown\", onDown);\n    return () => {\n      redraw.current = undefined;\n      area.removeEventListener(\"pointermove\", onMove);\n      area.removeEventListener(\"pointerdown\", onDown);\n      shader.destroy();\n    };\n  }, []);\n\n  return (\n    <div\n      ref={host}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, overflow: \"hidden\", pointerEvents: \"none\" }}\n    />\n  );\n}\n"}]}