{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"glass-etch-text","type":"registry:component","title":"Glass Etch Text","description":"Real, selectable heading text rendered as glass: a lit bevel, a graduated rim and a hairline colour fringe, raised or etched in.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/glass-etch-text.tsx","content":"\"use client\";\n\nimport { useEffect, useId, useRef, useState } from \"react\";\nimport type { CSSProperties, ReactNode } from \"react\";\n\n/**\n * GlassEtchText — a headline made of glass.\n *\n * The letters stay real, selectable HTML text; an SVG filter replaces how they\n * are painted. Each glyph becomes a clear pane with a faint body, a bevel lit\n * from one direction (bright on the edges that face the light, shadowed on\n * the far walls), a thin rim that is graduated rather than a uniform outline,\n * and a colour fringe no wider than a pixel or two that lives only in the rim\n * band, the way a real edge splits light. Whatever sits behind shows through\n * the letters.\n *\n * Two cuts:\n * - \"raised\": glass letters standing on the page, with a soft shadow cast\n *   away from the light.\n * - \"etched\": letters cut INTO a glass surface: the wall facing the light\n *   falls into shadow and the far wall catches it.\n *\n * The filter is a plain CSS filter: url(), which Chrome, Safari and Firefox\n * all run on HTML. Every length in it scales with the element's computed font\n * size, so it holds from 40px to 240px and with fluid clamp() type.\n *\n * Needs Tailwind v4 (or v3.4+). No dependencies beyond React.\n */\n\ntype Cut = \"raised\" | \"etched\";\n\n/** \"#rrggbb\" if valid, otherwise the fallback. Never interpolated unchecked. */\nfunction safeHex(hex: string | undefined, fallback: string): string {\n  return /^#[0-9a-f]{6}$/i.test(hex || \"\") ? (hex as string) : fallback;\n}\n\nconst r2 = (n: number) => Math.round(n * 100) / 100;\n\nexport function GlassEtchText({\n  children,\n  as: Tag = \"h2\",\n  size = 120,\n  weight = 700,\n  cut = \"raised\",\n  light = 225,\n  tint,\n  fringe = 0.5,\n  className = \"\",\n  style,\n}: {\n  children: ReactNode;\n  as?: \"h1\" | \"h2\" | \"h3\" | \"p\" | \"span\";\n  /** Font size: px, or any CSS length such as \"clamp(48px, 9vw, 160px)\". */\n  size?: number | string;\n  /** Glass needs body: 600 or heavier reads best. */\n  weight?: number;\n  cut?: Cut;\n  /** Where the light comes from, in degrees: 0 right, 90 below, 180 left, 270 above. 225 is the upper left. */\n  light?: number;\n  /** Colour of the glass body, \"#rrggbb\". Default clear. */\n  tint?: string;\n  /** Strength of the colour fringe at the rim, 0–1. */\n  fringe?: number;\n  className?: string;\n  style?: CSSProperties;\n}) {\n  const ref = useRef<HTMLElement | null>(null);\n  const id = \"get\" + useId().replace(/[^a-zA-Z0-9]/g, \"\");\n  const [px, setPx] = useState(typeof size === \"number\" ? size : 96);\n\n  // The filter is drawn in the element's own pixels, so its lengths follow\n  // the real, computed font size (fluid type changes it on resize).\n  useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const measure = () => {\n      const v = parseFloat(getComputedStyle(el).fontSize);\n      if (v > 0) setPx(Math.round(v));\n    };\n    const ro = new ResizeObserver(measure);\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n\n  const body = safeHex(tint, \"#ffffff\");\n  const f = Math.max(0, Math.min(1, fringe));\n  // A non-number from a CMS or URL param falls back to the upper left.\n  const deg = Number.isFinite(light) ? (((light % 360) + 360) % 360) : 225;\n  const az = deg * (Math.PI / 180);\n  // Unit vector pointing TOWARD the light (y down).\n  const lx = Math.cos(az);\n  const ly = Math.sin(az);\n  const raised = cut === \"raised\";\n\n  const bevel = r2(Math.max(0.8, px * 0.035));\n  const surface = r2(Math.max(1, px * 0.05));\n  const d = Math.max(0.6, px * 0.018);\n  const rimW = r2(Math.max(0.5, px * 0.009));\n  const split = r2(Math.min(3, Math.max(0.5, px * 0.007)));\n  // The inner shadow: the outside of the glyph, shifted so it lands on the\n  // far wall (raised) or on the wall facing the light (etched).\n  const inX = r2((raised ? lx : -lx) * d);\n  const inY = r2((raised ? ly : -ly) * d);\n  // Probes for which rim faces the light: sample one rim-width toward it.\n  const pX = r2(-lx * rimW * 1.5);\n  const pY = r2(-ly * rimW * 1.5);\n\n  return (\n    <Tag\n      ref={(n: HTMLElement | null) => {\n        ref.current = n;\n      }}\n      className={\"relative m-0 leading-[0.95] tracking-[-0.03em] \" + className}\n      style={{\n        fontSize: size,\n        fontWeight: weight,\n        // Opaque, so the filter's source alpha is exactly the glyph shapes.\n        color: \"#ffffff\",\n        filter: \"url(#\" + id + \")\",\n        ...style,\n      }}\n    >\n      {children}\n      <svg aria-hidden width=\"0\" height=\"0\" style={{ position: \"absolute\" }}>\n        <filter id={id} x=\"-8%\" y=\"-25%\" width=\"116%\" height=\"150%\" colorInterpolationFilters=\"sRGB\">\n          {/* Height map: the glyph's alpha, softened into a bevel. */}\n          <feGaussianBlur in=\"SourceAlpha\" stdDeviation={bevel} result=\"h\" />\n          <feComponentTransfer in=\"SourceAlpha\" result=\"inv\">\n            <feFuncA type=\"table\" tableValues=\"1 0\" />\n          </feComponentTransfer>\n          <feGaussianBlur in=\"inv\" stdDeviation={bevel} result=\"hInv\" />\n          {/* Specular on the bevel: convex for raised, concave for etched. */}\n          <feSpecularLighting\n            in={raised ? \"h\" : \"hInv\"}\n            surfaceScale={surface}\n            specularConstant=\"1.15\"\n            specularExponent=\"24\"\n            lightingColor=\"#ffffff\"\n            result=\"sp\"\n          >\n            <feDistantLight azimuth={deg} elevation=\"48\" />\n          </feSpecularLighting>\n          <feComposite in=\"sp\" in2=\"SourceAlpha\" operator=\"in\" result=\"spec\" />\n          {/* Inner shadow on the far wall (raised) or the near wall (etched). */}\n          <feOffset in=\"hInv\" dx={inX} dy={inY} result=\"invOff\" />\n          <feFlood floodColor=\"#0a0614\" floodOpacity={raised ? 0.42 : 0.55} />\n          <feComposite in2=\"invOff\" operator=\"in\" />\n          <feComposite in2=\"SourceAlpha\" operator=\"in\" result=\"inner\" />\n          {/* The body: faint, so what is behind shows through. */}\n          <feFlood floodColor={body} floodOpacity={raised ? 0.12 : 0.2} />\n          <feComposite in2=\"SourceAlpha\" operator=\"in\" result=\"fill\" />\n          {/* The rim band, then the part of it facing the light. */}\n          <feMorphology in=\"SourceAlpha\" operator=\"erode\" radius={rimW} result=\"core\" />\n          <feComposite in=\"SourceAlpha\" in2=\"core\" operator=\"out\" result=\"edge\" />\n          <feOffset in=\"SourceAlpha\" dx={pX} dy={pY} result=\"probe\" />\n          <feComposite in=\"edge\" in2=\"probe\" operator=\"out\" result=\"edgeLit\" />\n          <feComposite in=\"edge\" in2=\"edgeLit\" operator=\"out\" result=\"edgeFar\" />\n          <feFlood floodColor=\"#ffffff\" floodOpacity={raised ? 0.85 : 0.1} />\n          <feComposite in2=\"edgeLit\" operator=\"in\" result=\"rimLit\" />\n          <feFlood floodColor=\"#ffffff\" floodOpacity={raised ? 0.16 : 0.6} />\n          <feComposite in2=\"edgeFar\" operator=\"in\" result=\"rimFar\" />\n          {/* Dispersion: red and blue split by a pixel or two, rim band only,\n              and only where the two copies part (the sides of a stroke), so\n              horizontal edges never get a uniform tinted outline. */}\n          <feOffset in=\"edge\" dx={split} dy=\"0\" result=\"er\" />\n          <feOffset in=\"edge\" dx={-split} dy=\"0\" result=\"eb\" />\n          <feComposite in=\"er\" in2=\"eb\" operator=\"out\" result=\"erOnly\" />\n          <feComposite in=\"eb\" in2=\"er\" operator=\"out\" result=\"ebOnly\" />\n          <feFlood floodColor=\"#ff3d7f\" floodOpacity={r2(0.7 * f)} />\n          <feComposite in2=\"erOnly\" operator=\"in\" result=\"fr\" />\n          <feFlood floodColor=\"#33c4ff\" floodOpacity={r2(0.7 * f)} />\n          <feComposite in2=\"ebOnly\" operator=\"in\" result=\"fb\" />\n          {/* Raised glass casts a soft shadow away from the light. */}\n          <feGaussianBlur in=\"SourceAlpha\" stdDeviation={r2(px * 0.05)} />\n          <feOffset dx={r2(-lx * d * 1.8)} dy={r2(-ly * d * 1.8)} result=\"ds\" />\n          <feFlood floodColor=\"#000000\" floodOpacity={raised ? 0.24 : 0} />\n          <feComposite in2=\"ds\" operator=\"in\" />\n          <feComposite in2=\"SourceAlpha\" operator=\"out\" result=\"drop\" />\n          <feMerge>\n            <feMergeNode in=\"drop\" />\n            <feMergeNode in=\"fill\" />\n            <feMergeNode in=\"inner\" />\n            <feMergeNode in=\"fr\" />\n            <feMergeNode in=\"fb\" />\n            <feMergeNode in=\"rimFar\" />\n            <feMergeNode in=\"rimLit\" />\n            <feMergeNode in=\"spec\" />\n          </feMerge>\n        </filter>\n      </svg>\n    </Tag>\n  );\n}\n","type":"registry:component","target":"components/viberdy/glass-etch-text.tsx"}],"meta":{"tier":"free","tags":["text effect","glass","svg filter","bevel","headline","liquid glass"],"href":"https://viberdy.dev/library/glass-etch-text","aiPrompt":"Create a React + Tailwind client component, GlassEtchText: real HTML heading text rendered as glass by an SVG filter, in a \"raised\" (glass letters on the page) or \"etched\" (letters cut into glass) cut.\n\nProps: children, as (\"h1\" | \"h2\" | \"h3\" | \"p\" | \"span\", default h2), size (px number or any CSS length, default 120), weight (default 700), cut (\"raised\" | \"etched\"), light (degrees, SVG azimuth: 0 right, 90 below, 180 left, 270 above; default 225 = upper left), tint (\"#rrggbb\" for the glass body, regex-validated, default white), fringe (0–1, default 0.5), className, style. The element gets font-size, font-weight, line-height 0.95, tracking −0.03em, color #fff (opaque, so the filter's SourceAlpha is exactly the glyphs) and filter: url(#id), a plain CSS filter that Chrome, Safari and Firefox all run on HTML. The <filter> lives in a 0×0 absolutely positioned aria-hidden <svg> inside the element (x −8%, y −25%, 116% × 150%, color-interpolation-filters sRGB). Measure the computed font size with a ResizeObserver (fluid type changes it) and scale every filter length from it (px):\n\n- bevel blur σ = max(0.8, 0.035px); surfaceScale = max(1, 0.05px); shadow distance d = max(0.6, 0.018px); rim width = max(0.5, 0.009px); fringe split = clamp(0.007px, 0.5, 3), so dispersion never exceeds 3px.\n- L = (cos light, sin light) points toward the light (y down).\n- h = SourceAlpha blurred by σ; inv = SourceAlpha inverted (feFuncA table 1 0); hInv = inv blurred by σ.\n- Specular: feSpecularLighting (specularConstant 1.15, exponent 24, white, feDistantLight at the light azimuth, elevation 48) on h for raised or hInv for etched (a concave bevel), composited in SourceAlpha.\n- Inner shadow: hInv offset by +L·d (raised: lands on the far wall) or −L·d (etched: lands on the wall facing the light), flooded #0a0614 at 0.42 (0.55 etched), kept inside the glyph.\n- Body: the tint at 0.12 (0.2 etched) inside the glyph, so the backdrop shows through.\n- Rim, graduated not uniform: edge = SourceAlpha out (SourceAlpha eroded by the rim width); probe = SourceAlpha offset by −L·1.5·rim; edgeLit = edge out probe (the rim facing the light), edgeFar = edge out edgeLit. White at 0.85 on edgeLit and 0.16 on edgeFar for raised; 0.1 and 0.6 for etched (the lip facing the light sits in shadow).\n- Fringe: offset the edge band +split and −split px horizontally; keep each copy only where it does not overlap the other (feComposite out both ways), so the colour appears on the sides of strokes and never as a tinted outline along horizontal edges; flood #ff3d7f and #33c4ff at 0.7·fringe into them.\n- Raised only: a drop shadow (SourceAlpha blurred 0.05px, offset −L·1.8d, black 0.24) kept outside the glyph.\n- Merge: drop, body, inner shadow, the two fringes, far rim, lit rim, specular.\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"}}