{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"focus-outline","type":"registry:ui","title":"Focus Outline","description":"A soft, glowing outline that snaps into razor focus wherever the cursor comes near. WebGL signed distance fields.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/focus-outline.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport type FocusOutlineShape = \"rounded\" | \"circle\" | \"nested\";\n\nexport interface FocusOutlineProps {\n  shape?: FocusOutlineShape;\n  color?: string;\n  /** Second color the outline shifts toward around its perimeter. */\n  accent?: string;\n  /** Shape size as a fraction of the shorter side. */\n  size?: number;\n  /** Outline width in px when fully sharp. */\n  thickness?: number;\n  /** Blur radius in px away from the cursor. */\n  blur?: number;\n  /** Px around the cursor where the outline comes into focus. */\n  focus?: number;\n  className?: string;\n}\n\nfunction hexToVec(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] as const;\n}\n\nconst VERT = `attribute vec2 p;void main(){gl_Position=vec4(p,0.,1.);}`;\n\n// Signed distance to the outline; the gaussian width of the stroke grows with distance from the focus point.\nconst FRAG = `precision highp float;\nuniform vec2 uRes, uMouse;\nuniform float uSize, uThick, uBlur, uFocus, uShape, uDpr, uTime;\nuniform vec3 uA, uB;\nfloat box(vec2 p, vec2 b, float r){ vec2 q = abs(p) - b + r; return length(max(q, 0.)) + min(max(q.x, q.y), 0.) - r; }\nfloat stroke(float d, float w){ float e = max(abs(d) - uThick * .5 * uDpr, 0.); return exp(-e * e / (w * w)); }\nvoid main(){\n  vec2 p = gl_FragCoord.xy - uRes * .5;\n  float S = min(uRes.x, uRes.y) * uSize * .5;\n  float f = smoothstep(0., uFocus * uDpr, length(gl_FragCoord.xy - uMouse));\n  float w = mix(.7 * uDpr, uBlur * uDpr, f);\n  float peak = mix(1.25, .55, f);\n  float a = 0.;\n  if (uShape < .5 || uShape > 1.5) a += stroke(box(p, vec2(S), S * .3), w);\n  if (uShape > .5) a = max(a, stroke(length(p) - S * (uShape > 1.5 ? .55 : 1.), w));\n  a *= peak;\n  float hue = .5 + .5 * sin(atan(p.y, p.x) * 1. + uTime * .4);\n  vec3 col = mix(uA, uB, hue);\n  col = mix(col, vec3(1.), (1. - f) * .35);\n  a = clamp(a, 0., 1.);\n  gl_FragColor = vec4(col * a, a);\n}`;\n\nexport function FocusOutline({\n  shape = \"nested\",\n  color = \"#c6ff3d\",\n  accent = \"#22d3ee\",\n  size = 0.62,\n  thickness = 2,\n  blur = 26,\n  focus = 170,\n  className,\n}: FocusOutlineProps) {\n  const ref = useRef<HTMLDivElement>(null);\n\n  useEffect(() => {\n    const el = ref.current!;\n    const host = el.parentElement ?? el;\n    // A fresh canvas per effect run: a context lost in cleanup can never be reused.\n    const canvas = document.createElement(\"canvas\");\n    canvas.style.cssText = \"position:absolute;inset:0;width:100%;height:100%;display:block\";\n    el.appendChild(canvas);\n    const gl = canvas.getContext(\"webgl\", { antialias: false });\n    if (!gl) return () => canvas.remove();\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n\n    const sh = (type: number, code: string) => {\n      const s = gl.createShader(type)!;\n      gl.shaderSource(s, code);\n      gl.compileShader(s);\n      return s;\n    };\n    const prog = gl.createProgram()!;\n    gl.attachShader(prog, sh(gl.VERTEX_SHADER, VERT));\n    gl.attachShader(prog, sh(gl.FRAGMENT_SHADER, FRAG));\n    gl.linkProgram(prog);\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    const u = (n: string) => gl.getUniformLocation(prog, n);\n    gl.uniform1f(u(\"uShape\"), [\"rounded\", \"circle\", \"nested\"].indexOf(shape));\n    gl.uniform1f(u(\"uSize\"), size);\n    gl.uniform1f(u(\"uThick\"), thickness);\n    gl.uniform1f(u(\"uBlur\"), blur);\n    gl.uniform1f(u(\"uFocus\"), focus);\n    gl.uniform3fv(u(\"uA\"), hexToVec(color));\n    gl.uniform3fv(u(\"uB\"), hexToVec(accent));\n\n    let dpr = 1;\n    let raf = 0;\n    let visible = true;\n    const pointer = { x: 0, y: 0, inside: false };\n    const pos = { x: 0, y: 0 };\n    const t0 = performance.now();\n\n    const frame = () => {\n      const t = (performance.now() - t0) / 1000;\n      const W = canvas.width;\n      const H = canvas.height;\n      // Without a cursor the focus point laps the shape, so it never sits fully blurred.\n      let tx = W / 2 + Math.cos(t * 0.7) * Math.min(W, H) * size * 0.5;\n      let ty = H / 2 + Math.sin(t * 0.7) * Math.min(W, H) * size * 0.5;\n      if (pointer.inside) {\n        tx = pointer.x * dpr;\n        ty = H - pointer.y * dpr;\n      }\n      pos.x += (tx - pos.x) * 0.12;\n      pos.y += (ty - pos.y) * 0.12;\n      gl.uniform2f(u(\"uMouse\"), pos.x, pos.y);\n      gl.uniform1f(u(\"uTime\"), t);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n    };\n    const loop = () => {\n      frame();\n      raf = visible ? requestAnimationFrame(loop) : 0;\n    };\n\n    const resize = () => {\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      canvas.width = canvas.clientWidth * dpr;\n      canvas.height = canvas.clientHeight * dpr;\n      gl.viewport(0, 0, canvas.width, canvas.height);\n      gl.uniform2f(u(\"uRes\"), canvas.width, canvas.height);\n      gl.uniform1f(u(\"uDpr\"), dpr);\n      pos.x = canvas.width / 2;\n      pos.y = canvas.height;\n      if (reduced) frame();\n    };\n    const onMove = (e: PointerEvent) => {\n      const r = canvas.getBoundingClientRect();\n      pointer.x = e.clientX - r.left;\n      pointer.y = e.clientY - r.top;\n      pointer.inside = true;\n    };\n    const onLeave = () => {\n      pointer.inside = false;\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(canvas);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && !raf && !reduced) raf = requestAnimationFrame(loop);\n    });\n    io.observe(canvas);\n    host.addEventListener(\"pointermove\", onMove, { passive: true });\n    host.addEventListener(\"pointerleave\", onLeave);\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      host.removeEventListener(\"pointermove\", onMove);\n      host.removeEventListener(\"pointerleave\", onLeave);\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\n    };\n  }, [shape, color, accent, size, thickness, blur, focus]);\n\n  return (\n    <div\n      ref={ref}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, overflow: \"hidden\", pointerEvents: \"none\" }}\n    />\n  );\n}\n"}]}