{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"refract-panel","type":"registry:ui","title":"Refract Panel","description":"A liquid-glass panel whose rim bends the backdrop through an SVG displacement map, with chromatic fringes, blur and a specular edge.","dependencies":["react-use-measure"],"registryDependencies":["utils"],"files":[{"path":"registry/ui/refract-panel.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useId, useRef, type ReactNode } from \"react\";\nimport useMeasure from \"react-use-measure\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface RefractPanelProps {\n  children?: ReactNode;\n  /** Corner radius in px. */\n  radius?: number;\n  /** Width of the refracting rim in px. */\n  bezel?: number;\n  /** Strength of the rim refraction in px. */\n  refraction?: number;\n  /** Px of chromatic split at the rim. */\n  aberration?: number;\n  /** Backdrop blur in px. */\n  blur?: number;\n  /** Backdrop saturation multiplier. */\n  saturation?: number;\n  /** Opacity of the white tint, 0 to 1. */\n  tint?: number;\n  className?: string;\n}\n\n/**\n * Displacement map for a rounded rectangle: neutral gray inside, and along a\n * `bezel`-wide rim a vector pointing inward, strongest right at the edge.\n * Rendered at half resolution; the filter stretches it back up.\n */\nfunction rimMap(w: number, h: number, radius: number, bezel: number) {\n  const s = 0.5;\n  const cw = Math.max(1, Math.round(w * s));\n  const ch = Math.max(1, Math.round(h * s));\n  const c = document.createElement(\"canvas\");\n  c.width = cw;\n  c.height = ch;\n  const ctx = c.getContext(\"2d\")!;\n  const img = ctx.createImageData(cw, ch);\n  const hx = w / 2;\n  const hy = h / 2;\n  const r = Math.min(radius, hx, hy);\n  // Signed distance to the rounded box (negative inside).\n  const sdf = (x: number, y: number) => {\n    const qx = Math.abs(x - hx) - (hx - r);\n    const qy = Math.abs(y - hy) - (hy - r);\n    return Math.hypot(Math.max(qx, 0), Math.max(qy, 0)) + Math.min(Math.max(qx, qy), 0) - r;\n  };\n  for (let j = 0; j < ch; j++) {\n    for (let i = 0; i < cw; i++) {\n      const x = (i + 0.5) / s;\n      const y = (j + 0.5) / s;\n      const depth = -sdf(x, y);\n      let dx = 0;\n      let dy = 0;\n      if (depth < bezel) {\n        const t = 1 - Math.max(depth, 0) / bezel;\n        // Circular bevel profile: flat inside, steep at the very edge.\n        const k = 1 - Math.sqrt(1 - t * t);\n        const nx = sdf(x + 1, y) - sdf(x - 1, y);\n        const ny = sdf(x, y + 1) - sdf(x, y - 1);\n        const len = Math.hypot(nx, ny) || 1;\n        dx = (-nx / len) * k;\n        dy = (-ny / len) * k;\n      }\n      const p = (j * cw + i) * 4;\n      img.data[p] = 128 + dx * 127;\n      img.data[p + 1] = 128 + dy * 127;\n      img.data[p + 2] = 128;\n      img.data[p + 3] = 255;\n    }\n  }\n  ctx.putImageData(img, 0, 0);\n  return c.toDataURL();\n}\n\nconst channel = (r: number, g: number, b: number) => `${r} 0 0 0 0  0 ${g} 0 0 0  0 0 ${b} 0 0  0 0 0 1 0`;\n\nexport function RefractPanel({\n  children,\n  radius = 28,\n  bezel = 26,\n  refraction = 70,\n  aberration = 3,\n  blur = 2,\n  saturation = 1.6,\n  tint = 0.06,\n  className,\n}: RefractPanelProps) {\n  const [measureRef, { width, height }] = useMeasure();\n  const surface = useRef<HTMLDivElement>(null);\n  const mapRef = useRef<SVGFEImageElement>(null);\n  const id = `glass-surface-${useId().replace(/[^a-zA-Z0-9_-]/g, \"\")}`;\n\n  useEffect(() => {\n    if (!width || !height) return;\n    const el = surface.current!;\n    // SVG filters inside backdrop-filter only work in Chromium; elsewhere we\n    // fall back to plain frosted glass instead of losing the backdrop entirely.\n    const chromium = /Chrome\\/|Chromium\\/|Edg\\//.test(navigator.userAgent) && !/Firefox\\//.test(navigator.userAgent);\n    const base = `blur(${blur}px) saturate(${saturation})`;\n    const apply = (v: string) => {\n      el.style.setProperty(\"backdrop-filter\", v);\n      el.style.setProperty(\"-webkit-backdrop-filter\", v);\n    };\n    if (!chromium) return apply(`blur(${Math.max(blur, 10)}px) saturate(${saturation})`);\n    apply(base);\n    // Chromium caches the backdrop filter, so attach it only once the map image\n    // has decoded, and re-attach whenever the filter changes.\n    const url = rimMap(width, height, radius, bezel);\n    const img = new Image();\n    let alive = true;\n    img.onload = () => {\n      if (!alive) return;\n      mapRef.current?.setAttribute(\"href\", url);\n      requestAnimationFrame(() => alive && apply(`url(#${id}) ${base}`));\n    };\n    img.src = url;\n    return () => {\n      alive = false;\n    };\n  }, [id, width, height, radius, bezel, refraction, aberration, blur, saturation]);\n\n  return (\n    <div\n      ref={(el) => {\n        surface.current = el;\n        measureRef(el);\n      }}\n      className={cn(\"relative isolate\", className)}\n      style={{\n        borderRadius: radius,\n        background: `rgba(255,255,255,${tint})`,\n        boxShadow: [\n          \"inset 1.5px 1.5px 0 -0.5px rgba(255,255,255,.65)\",\n          \"inset -1px -1px 0 -0.5px rgba(255,255,255,.35)\",\n          \"inset 0 0 18px rgba(255,255,255,.12)\",\n          \"0 18px 50px -12px rgba(0,0,0,.45)\",\n        ].join(\",\"),\n      }}\n    >\n      {/* soft specular sheen across the top */}\n      <div\n        aria-hidden\n        className=\"pointer-events-none absolute inset-0\"\n        style={{\n          borderRadius: radius,\n          background: \"linear-gradient(160deg, rgba(255,255,255,.22), rgba(255,255,255,0) 38%, rgba(255,255,255,0) 70%, rgba(255,255,255,.08))\",\n        }}\n      />\n      <div className=\"relative\">{children}</div>\n      <svg aria-hidden width=\"0\" height=\"0\" className=\"absolute\">\n        <filter id={id} filterUnits=\"userSpaceOnUse\" x=\"0\" y=\"0\" width={width} height={height} colorInterpolationFilters=\"sRGB\">\n          <feImage ref={mapRef} x=\"0\" y=\"0\" width={width} height={height} preserveAspectRatio=\"none\" result=\"map\" />\n          <feDisplacementMap in=\"SourceGraphic\" in2=\"map\" scale={refraction} xChannelSelector=\"R\" yChannelSelector=\"G\" result=\"dr\" />\n          <feColorMatrix in=\"dr\" values={channel(1, 0, 0)} result=\"r\" />\n          <feDisplacementMap in=\"SourceGraphic\" in2=\"map\" scale={refraction + aberration} xChannelSelector=\"R\" yChannelSelector=\"G\" result=\"dg\" />\n          <feColorMatrix in=\"dg\" values={channel(0, 1, 0)} result=\"g\" />\n          <feDisplacementMap in=\"SourceGraphic\" in2=\"map\" scale={refraction + aberration * 2} xChannelSelector=\"R\" yChannelSelector=\"G\" result=\"db\" />\n          <feColorMatrix in=\"db\" values={channel(0, 0, 1)} result=\"b\" />\n          <feBlend in=\"r\" in2=\"g\" mode=\"screen\" result=\"rg\" />\n          <feBlend in=\"rg\" in2=\"b\" mode=\"screen\" />\n        </filter>\n      </svg>\n    </div>\n  );\n}\n"}]}