{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"liquid-glass-button","type":"registry:component","title":"Liquid Glass Button","description":"A pill of liquid glass that bends what is behind it: real refraction at the rim, a rim and sheen lit by your pointer, and a gel-like press.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/liquid-glass-button.tsx","content":"\"use client\";\n\nimport { useEffect, useId, useRef } from \"react\";\nimport type { CSSProperties, PointerEvent as ReactPointerEvent, ReactNode } from \"react\";\n\n/**\n * LiquidGlassButton — a pill of liquid glass, after Apple's 2025 material:\n * it bends what is behind it rather than just blurring it.\n *\n * Three layers do the work:\n * 1. REFRACTION. In Chromium an SVG displacement map, generated for the\n *    button's exact size, is applied inside backdrop-filter, so the backdrop\n *    visibly curves at the rim like light through a lens. Safari and Firefox\n *    cannot run SVG filters as a backdrop-filter yet; they get blur,\n *    saturation and brightness only, which still reads as glass.\n * 2. RIM. A 1px rim graduated from bright (top) to faint (bottom), plus a\n *    second rim lit from wherever the pointer is.\n * 3. SPECULAR. A soft highlight that follows the pointer across the surface.\n *\n * Press squashes it like gel and it springs back. Keyboard focus gets a\n * visible ring. Glass needs something rich behind it: over a flat colour it\n * is a grey pill, which is a property of glass, not of this component.\n *\n * Needs Tailwind v4 (or v3.4+). No dependencies beyond React.\n */\n\ntype Tone = \"dark\" | \"light\";\ntype Surface = \"clear\" | \"frost\";\ntype Size = \"md\" | \"lg\";\n\n/** Allow relative paths, fragments, http(s), mailto and tel; anything else becomes \"#\". */\nfunction safeHref(href: string): string {\n  const v = href.replace(/[\\t\\n\\r]/g, \"\").trim();\n  if (v.indexOf(String.fromCharCode(92)) !== -1) return \"#\";\n  if (/^(https?:|mailto:|tel:)/i.test(v)) return v;\n  if (/^[/#?]/.test(v) && !/^\\/\\//.test(v)) return v;\n  return \"#\";\n}\n\n/** \"#rrggbb\" to \"r,g,b\", or null. Anything else is ignored, never interpolated. */\nfunction rgbOf(hex?: string): string | null {\n  const m = /^#([0-9a-f]{6})$/i.exec(hex || \"\");\n  if (!m) return null;\n  const n = parseInt(m[1], 16);\n  return ((n >> 16) & 255) + \",\" + ((n >> 8) & 255) + \",\" + (n & 255);\n}\n\n/**\n * The displacement map for a rounded rectangle, as a data URL. Red carries\n * the x offset and green the y offset (128 = none). Offsets point inward and\n * grow toward the rim, so the backdrop near the edge is pulled from further\n * in: the magnified, bent edge of a lens.\n */\nfunction lensMap(w: number, h: number, r: number, bezel: number): string {\n  const c = document.createElement(\"canvas\");\n  c.width = w;\n  c.height = h;\n  const ctx = c.getContext(\"2d\");\n  if (!ctx) return \"\";\n  const img = ctx.createImageData(w, h);\n  const hw = w / 2;\n  const hh = h / 2;\n  const sd = (x: number, y: number) => {\n    const qx = Math.abs(x) - (hw - r);\n    const qy = Math.abs(y) - (hh - 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 < h; j++) {\n    for (let i = 0; i < w; i++) {\n      const x = i + 0.5 - hw;\n      const y = j + 0.5 - hh;\n      const d = sd(x, y);\n      let dx = 0;\n      let dy = 0;\n      const t = -d;\n      if (t > 0 && t < bezel) {\n        const nx = sd(x + 0.5, y) - sd(x - 0.5, y);\n        const ny = sd(x, y + 0.5) - sd(x, y - 0.5);\n        const len = Math.hypot(nx, ny) || 1;\n        const m = Math.pow(1 - t / bezel, 2);\n        dx = (-nx / len) * m;\n        dy = (-ny / len) * m;\n      }\n      const k = (j * w + i) * 4;\n      img.data[k] = Math.round(128 + 127 * dx);\n      img.data[k + 1] = Math.round(128 + 127 * dy);\n      img.data[k + 2] = 128;\n      img.data[k + 3] = 255;\n    }\n  }\n  ctx.putImageData(img, 0, 0);\n  return c.toDataURL();\n}\n\nexport function LiquidGlassButton({\n  children,\n  href,\n  onClick,\n  tone = \"dark\",\n  surface = \"clear\",\n  size = \"md\",\n  tint,\n  refraction = 1,\n  icon = true,\n  className = \"\",\n}: {\n  children: ReactNode;\n  /** Renders a link instead of a button. Sanitised. */\n  href?: string;\n  onClick?: () => void;\n  /** Glass over a dark backdrop (white text) or a light one (ink text). */\n  tone?: Tone;\n  /** \"clear\" barely blurs, like Apple's clear variant; \"frost\" blurs more. */\n  surface?: Surface;\n  size?: Size;\n  /** Optional tint, \"#rrggbb\", mixed in at 15% (20% on light glass). */\n  tint?: string;\n  /** Refraction strength, 0–2. 0 turns the lens off. */\n  refraction?: number;\n  /** Trailing arrow that nudges on hover. */\n  icon?: boolean;\n  className?: string;\n}) {\n  const rootRef = useRef<HTMLElement | null>(null);\n  const filterId = \"lg\" + useId().replace(/[^a-zA-Z0-9]/g, \"\");\n  const mapRef = useRef<SVGFEImageElement>(null);\n  const dispRef = useRef<SVGFEDisplacementMapElement>(null);\n\n  const blur = surface === \"frost\" ? 14 : 3;\n  const base = \"blur(\" + blur + \"px) saturate(185%) brightness(\" + (tone === \"dark\" ? 1.08 : 1.04) + \")\";\n  const rgb = rgbOf(tint);\n  const fill =\n    tone === \"dark\"\n      ? rgb\n        ? \"rgba(\" + rgb + \",0.15)\"\n        : \"rgba(10,10,18,0.22)\"\n      : rgb\n        ? \"rgba(\" + rgb + \",0.2)\"\n        : \"rgba(255,255,255,0.34)\";\n\n  // Build the lens for the button's real size, and switch the backdrop filter\n  // to the refracting version only where the browser runs SVG filters there.\n  useEffect(() => {\n    const el = rootRef.current;\n    if (!el) return;\n    const ua = (navigator as Navigator & { userAgentData?: { brands?: { brand: string }[] } }).userAgentData;\n    const chromium = !!ua && !!ua.brands && ua.brands.some((b) => /Chromium/.test(b.brand));\n    if (!chromium || refraction <= 0) {\n      el.style.backdropFilter = base;\n      return;\n    }\n    const apply = () => {\n      const w = Math.round(el.offsetWidth);\n      const h = Math.round(el.offsetHeight);\n      if (!w || !h || !mapRef.current || !dispRef.current) return;\n      // A narrow rim: on a pill this thin a wide bezel would bend the whole\n      // surface and the middle would stop reading as clear glass.\n      const url = lensMap(w, h, h / 2, Math.min(h * 0.24, 12));\n      mapRef.current.setAttribute(\"href\", url);\n      mapRef.current.setAttribute(\"width\", String(w));\n      mapRef.current.setAttribute(\"height\", String(h));\n      dispRef.current.setAttribute(\"scale\", String(Math.round(28 * refraction)));\n      el.style.backdropFilter = \"url(#\" + filterId + \") \" + base;\n    };\n    apply();\n    const ro = new ResizeObserver(apply);\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, [filterId, base, refraction]);\n\n  // Track the pointer on move AND on press: a tap on a touch screen sends no\n  // move first, and the press glow must start under the finger.\n  function onPointerMove(e: ReactPointerEvent<HTMLElement>) {\n    const el = e.currentTarget;\n    const r = el.getBoundingClientRect();\n    el.style.setProperty(\"--lgx\", ((e.clientX - r.left) / r.width) * 100 + \"%\");\n    el.style.setProperty(\"--lgy\", ((e.clientY - r.top) / r.height) * 100 + \"%\");\n    el.style.setProperty(\"--lgo\", \"1\");\n  }\n  function onPointerLeave(e: ReactPointerEvent<HTMLElement>) {\n    e.currentTarget.style.setProperty(\"--lgo\", \"0\");\n  }\n\n  const ink = tone === \"dark\" ? \"#ffffff\" : \"#111111\";\n  const style = {\n    background: fill,\n    backdropFilter: base,\n    WebkitBackdropFilter: base,\n    color: ink,\n    boxShadow:\n      \"inset 0 1px 0 rgba(255,255,255,\" + (tone === \"dark\" ? 0.55 : 0.8) + \"), inset 0 -1px 0 rgba(255,255,255,0.18), 0 12px 40px rgba(0,0,0,\" + (tone === \"dark\" ? 0.28 : 0.12) + \")\",\n    \"--lgx\": \"30%\",\n    \"--lgy\": \"0%\",\n    \"--lgo\": \"0\",\n  } as CSSProperties;\n\n  const inner = (\n    <>\n      {/* Pointer-lit rim: a 1px ring, brightest nearest the pointer. */}\n      <span\n        aria-hidden\n        className=\"pointer-events-none absolute inset-0 rounded-full\"\n        style={{\n          padding: 1,\n          background:\n            \"radial-gradient(120px circle at var(--lgx) var(--lgy), rgba(255,255,255,calc(0.25 + var(--lgo) * 0.6)), rgba(255,255,255,0.14) 70%)\",\n          WebkitMask: \"linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0)\",\n          WebkitMaskComposite: \"xor\",\n          mask: \"linear-gradient(#000 0 0) content-box exclude, linear-gradient(#000 0 0)\",\n        }}\n      />\n      {/* Specular: a soft sheen that travels with the pointer. */}\n      <span\n        aria-hidden\n        className=\"pointer-events-none absolute inset-0 rounded-full transition-opacity duration-300\"\n        style={{\n          opacity: \"calc(0.35 + var(--lgo) * 0.65)\",\n          background:\n            \"radial-gradient(80% 140% at var(--lgx) var(--lgy), rgba(255,255,255,0.28), rgba(255,255,255,0) 60%)\",\n          mixBlendMode: \"screen\",\n        }}\n      />\n      {/* Press glow: the light starts under the pointer and spreads while held. */}\n      <span\n        aria-hidden\n        className=\"pointer-events-none absolute h-10 w-10 -translate-x-1/2 -translate-y-1/2 scale-50 rounded-full opacity-0 transition-all duration-200 ease-out group-active:scale-[7] group-active:opacity-100\"\n        style={{\n          left: \"var(--lgx)\",\n          top: \"var(--lgy)\",\n          background: \"radial-gradient(closest-side, rgba(255,255,255,0.4), rgba(255,255,255,0))\",\n        }}\n      />\n      <span\n        className=\"relative z-10 flex items-center gap-2 font-semibold tracking-[-0.01em]\"\n        style={{ textShadow: tone === \"dark\" ? \"0 1px 12px rgba(0,0,0,0.4)\" : \"none\" }}\n      >\n        {children}\n        {icon && (\n          <span\n            aria-hidden\n            className=\"inline-block transition-transform duration-300 ease-[cubic-bezier(0.32,0.72,0,1)] group-hover:translate-x-0.5 group-hover:-translate-y-0.5\"\n          >\n            ↗\n          </span>\n        )}\n      </span>\n      <svg aria-hidden width=\"0\" height=\"0\" style={{ position: \"absolute\" }}>\n        <filter id={filterId} x=\"0\" y=\"0\" width=\"100%\" height=\"100%\" colorInterpolationFilters=\"sRGB\">\n          <feImage ref={mapRef} x=\"0\" y=\"0\" preserveAspectRatio=\"none\" result=\"map\" />\n          <feDisplacementMap ref={dispRef} in=\"SourceGraphic\" in2=\"map\" scale=\"28\" xChannelSelector=\"R\" yChannelSelector=\"G\" />\n        </filter>\n      </svg>\n    </>\n  );\n\n  const cls =\n    \"group relative isolate inline-flex select-none items-center justify-center overflow-hidden rounded-full \" +\n    (size === \"lg\" ? \"h-14 px-7 text-[17px] \" : \"h-11 px-5 text-[15px] \") +\n    \"transition-transform duration-300 ease-[cubic-bezier(0.34,1.4,0.64,1)] active:scale-x-[0.98] active:scale-y-[0.97] active:duration-100 \" +\n    \"outline-none focus-visible:ring-2 focus-visible:ring-white/80 focus-visible:ring-offset-2 focus-visible:ring-offset-transparent \" +\n    className;\n\n  if (href) {\n    return (\n      <a\n        ref={(n) => {\n          rootRef.current = n;\n        }}\n        href={safeHref(href)}\n        onClick={onClick}\n        onPointerMove={onPointerMove}\n        onPointerDown={onPointerMove}\n        onPointerLeave={onPointerLeave}\n        className={cls}\n        style={style}\n      >\n        {inner}\n      </a>\n    );\n  }\n  return (\n    <button\n      ref={(n) => {\n        rootRef.current = n;\n      }}\n      type=\"button\"\n      onClick={onClick}\n      onPointerMove={onPointerMove}\n      onPointerDown={onPointerMove}\n      onPointerLeave={onPointerLeave}\n      className={cls}\n      style={style}\n    >\n      {inner}\n    </button>\n  );\n}\n","type":"registry:component","target":"components/viberdy/liquid-glass-button.tsx"}],"meta":{"tier":"free","tags":["button","glass","liquid glass","refraction","svg filter","cursor"],"href":"https://viberdy.dev/library/liquid-glass-button","aiPrompt":"Create a React + Tailwind client component, LiquidGlassButton: a pill of \"liquid glass\" in the style of Apple's 2025 material, which bends the backdrop instead of only blurring it.\n\nProps: children, href (renders an <a>; sanitise it: strip tabs and newlines, reject any backslash, allow http(s):, mailto:, tel:, and relative paths starting with / # or ? but not //, otherwise \"#\"), onClick, tone (\"dark\" | \"light\", default dark), surface (\"clear\" | \"frost\", default clear), size (\"md\" 44px tall, 20px padding, 15px text | \"lg\" 56px, 28px, 17px), tint (optional \"#rrggbb\", validated with a regex and converted to r,g,b, never interpolated raw), refraction (0–2, default 1), icon (trailing ↗ that nudges up-right on hover, default true), className.\n\nSurface: fill rgba(10,10,18,0.22) on dark, a dimming layer that keeps the label legible over bright content, or rgba(255,255,255,0.34) on light (or the tint at 0.15 / 0.2); backdrop-filter blur(3px) for clear or blur(14px) for frost, then saturate(185%) brightness(1.08 dark / 1.04 light), with the -webkit- copy. Box shadow: inset 0 1px 0 white at 0.55 (0.8 on light), inset 0 -1px 0 white at 0.18, and 0 12px 40px black at 0.28 (0.12 on light). Text white with a text shadow (0 1px 12px black at 0.4) on dark, #111 on light; semibold, tracking −0.01em.\n\nRefraction: build an SVG <filter> with a sanitised useId (alphanumerics only) holding an feImage (the displacement map) and an feDisplacementMap (in SourceGraphic, in2 the map, R for x and G for y, color-interpolation-filters sRGB). Generate the map on a canvas at the button's exact pixel size: for each pixel take the signed distance to a rounded rectangle whose radius is half the height; inside a narrow bezel of min(0.24·h, 12px) from the rim, offset toward the centre along the inward normal with magnitude (1 − t/bezel)², encoded as red = 128 + 127·dx and green = 128 + 127·dy. Set the displacement scale to 28 × refraction. Only in Chromium (detect navigator.userAgentData.brands containing \"Chromium\") set backdrop-filter to \"url(#id) \" followed by the base filter; everywhere else keep the base filter, because Safari and Firefox drop a backdrop-filter that contains url(). Rebuild the map with a ResizeObserver.\n\nLight: track the pointer into CSS variables --lgx and --lgy (percent) and --lgo (1 while hovering, 0 on leave) written straight to the element's style on pointermove AND pointerdown (a touch tap sends no move first), no React state. A 1px rim layer (a padded span masked to its border with mask-composite exclude and the -webkit- xor fallback) paints a 120px radial gradient at the pointer, white at 0.25 + 0.6·--lgo, fading to 0.14. A specular span paints an 80%×140% radial highlight at the pointer, white 0.28 to transparent, screen-blended, at opacity 0.35 + 0.65·--lgo. Press glow: a 40px circle positioned at --lgx/--lgy (translated −50%), a radial white 0.4 to transparent, at scale 0.5 and opacity 0, that grows to scale 7 and full opacity over 200ms while active, so the light starts under the finger and spreads.\n\nMotion: press squashes it like gel (scale-x 0.98, scale-y 0.97 over 100ms) and it springs back over 300ms with cubic-bezier(0.34, 1.4, 0.64, 1). Visible focus ring: 2px white at 80% with an offset. Rounded-full, overflow hidden, isolate, select-none.\n\nOutput one self-contained \"use client\" TSX file with no dependencies beyond React. Build strings with + rather than template literals.","license":"https://viberdy.dev/license"}}