Glass Shatter button
A dark button in a ring of glass that glows as your pointer comes near and shatters into shards on click, then seals. Use it for one main action.
npx shadcn@latest add https://impeccableui.vercel.app/r/glass-shatter-button.jsonRun it in a project set up with shadcn. No other packages needed. Or open and copy the file into components/ui/glass-shatter-button.tsx.
Prompts
Paste one into Claude Code, Cursor or any AI coding tool. Install adds this exact button and asks only where it goes. Blend asks four short questions (where, the fill and label colors, the glow color, and how far away the glass starts to glow), then fits it to your product without changing how it moves.
# Glass Shatter button
A 1-file React component from Impeccable. Add it to this project so it works and looks exactly like the source below, in the one place I choose, and change nothing else.
A solid button inside a thin ring of glass. The glass catches the light as the pointer comes near, brighter the closer it gets, and on click it cracks, bursts into shards, then pulls back together and seals. The click works straight away.
## Before you build: one question
Look through this project first: its landing and hero sections, its main call-to-action buttons and the pages where one action matters most. Then ask me this in one message and wait for my answer:
- **Where should it go?** Suggest the 1–3 buttons it could replace or the spots it fits best (for example the hero's main call to action, a "Get started" or launch button, or the final step of a sign-up). Keep the button's label, link and click handler. I can pick one or name another, and say whether it needs at least 60px of open space around it.
*Why: it reacts before anyone clicks and breaks on the click, so it suits one main action with room around it, not every button on the page.*
Ask nothing else. If I say *skip*, use the best suggestion.
## Keep it as it is
The solid middle in a thin ring of glass, the glow that grows on the side nearest the pointer, and the crack, burst, hang, return and seal on click, with their timing, are the design. Keep them exactly as in the source. If something doesn't fit my project, tell me instead of changing it.
- Add only the component file and the one place I picked. If it replaces an existing button, keep that button's label, link, click handler and form wiring. Don't add packages, change other files, or restyle anything.
- Don't add the demo parts from the Impeccable website: the dotted card behind the button and its "Break the glass" label.
## Fastest path
If this project has a `components.json` (shadcn), run `npx shadcn@latest add https://impeccableui.vercel.app/r/glass-shatter-button.json`.
If the command succeeds, only fix import paths to match this project and skip ahead. If it fails or there is no `components.json`, build it by hand as below.
## Build it
- Stack: React 18 or 19 with TypeScript. No other packages: the glass, the glow and the shards are drawn on one canvas around the button and the rest is plain CSS.
- JavaScript project? Name the file `glass-shatter-button.jsx` and remove the type annotations; nothing else changes.
- Create this file, in this project's usual place for components:
- `components/ui/glass-shatter-button.tsx`
- Keep these exports and their names: `GlassShatterButton`, `GlassShatterButtonProps`.
- Keep `"use client"` at the top in Next.js App Router projects. In other setups it is harmless.
- Keep the tuning values at the top exactly as written: `OVER`, `TINT`, `EDGE`, `SHINE`, `GLOW_IN_MS`, `GLOW_OUT_MS`, `GLOW_CURVE`, `CRACK_MS`, `BURST_MS`, `HANG_MS`, `RETURN_MS`, `RIPPLE`, `SEAL_MS`, `FLY`, `SPIN`, `DROP`, `CUT_MIN`, `CUT_MAX`, the `snapBack` easing and the `CSS` block. They are the design and its timing.
- Only the ring is glass. Keep the middle a plain, solid button: no blur, gradient or glass on it. The glow lights the glass only: never add a halo or shadow glow outside the border.
- Keep the fallbacks: with `prefers-reduced-motion: reduce` the cracks show and fade and nothing flies. On touch screens, pressing lights the glass under the finger and the tap shatters it. Enter or Space shatters it from the top centre, and focus shows a ring in the text color around it. A disabled button shows no glow and never shatters. A hidden tab stops drawing.
## Use it
```tsx
"use client";
import { GlassShatterButton } from "@/components/ui/glass-shatter-button";
export function HeroAction() {
return <GlassShatterButton onClick={() => router.push("/signup")}>Get started</GlassShatterButton>;
}
```
- It is a real `<button>` and takes every button prop (`onClick`, `type`, `disabled`, `aria-*`). For a link, put it inside the link or call the router in `onClick`.
- `className` and `style` go on the button itself. It is 56px tall (a 44px middle plus the 6px ring each side). Change `border-radius` here and the middle follows, minus the ring.
- Shards fly up to about 60px outside the button. Don't put it right against the edge of a box with `overflow: hidden`, or the shards get cut off there.
## How the glass works
Read this before you touch anything, so you know what the code does and why it must stay as it is.
1. **At rest.** A solid pill (`color`, near-black by default) with a sharp label, inside a 6px ring of glass (`ring`). The glass is a faint tint of `glassColor` (the page's text color by default, so it shows on light and dark pages), a soft white shine across its top half, a thin outer and inner edge line, and a glint along the top left. The ring is part of the button, so it is clickable too.
2. **The glow.** The pointer is watched on the whole page while the button is on screen. Within `range` px (120) of the button, the part of the ring nearest to the pointer catches `glowColor`. Closeness is curved (power 1.8), so the glow grows faster the closer the pointer gets: faint at the edge of the range, bright and about 70px wide once the pointer is on the button. The edges light up brightest. The light stays inside the glass: nothing glows outside the border or over the middle. It rises in about 90ms and fades in about 280ms.
3. **The crack.** A click goes through to `onClick` at once. On the press, cracks run out from the exact spot pressed for 90ms, and a soft flash of `glowColor` blooms in the glass there. The middle takes a small knock (it scales to 96.5% and back in 420ms).
4. **The burst.** The ring is cut into shards: about 5px apart near the impact and up to 22px apart on the far side, each cut slanted, and longer pieces split into two triangles. Shards fly away from the impact and outward from the button, up to 52px, the ones near the impact furthest and spinning most (up to 2.2 radians). They go out fast and slow down like in air over 520ms, sinking 10px, starting in a ripple from the impact (0.22ms per px of ring). Each shard's shine flickers as it turns, and shards near the pointer catch the glow.
5. **The return.** Shards hang for 80ms, drifting a little further, then pull back in over 540ms with a slow start and a tiny snap at the end. The far ones go back first and the ones at the impact last, so the glass closes toward where it was hit.
6. **The seal.** When the last shard lands, the cracks fade and a line of light runs both ways round the ring from the impact, meeting on the far side, in 320ms. The whole thing takes about a second and a half.
7. **Clicking again.** While it is in pieces, a new click blasts the shards again from where they are, never further than the room around the button. When the glass is whole, a new click breaks it fresh.
8. **Drawing.** One canvas sits behind the middle and reaches 88px past the button above and below, and on the left and right as far as the screen allows (never past it, so the page can't scroll sideways). The page does not move. A loop runs only while something changes and stops when the button is at rest. All motion runs on elapsed time, so it feels the same on slow phones and fast screens. Colors are read from the page on every frame, so theme colors follow light and dark mode.
## Check
- It type-checks and the page renders with no console errors.
- At rest it is a solid pill with a sharp label in a thin, faint ring of glass. Moving the pointer toward it from about 120px away lights the side of the ring nearest the pointer, faint at first and bright once on it. A click works at once, while the ring cracks from the exact spot clicked, bursts into shards that fly up to about 50px, hangs, pulls back in and seals with a line of light, in about 1.5 seconds.
- It looks right in light and dark mode (check both): the glass, its edges and the glow show on both.
- On a phone, pressing lights the glass under the finger, the tap shatters it, the button works on the first tap, and there is no sideways scroll.
- With `prefers-reduced-motion: reduce`, the cracks show and fade and nothing flies.
- Nothing else on the site looks or behaves differently.
- Finish with a short report: the files you added or changed, where it's placed, and every place where you had to do something differently from the source, and why.
## Source
The reference implementation. Copy it as-is, then apply only the changes listed above.
### `components/ui/glass-shatter-button.tsx`
```tsx
"use client";
/**
* Glass Shatter button, from Impeccable.
*
* A solid pill wrapped in a thin ring of glass. Only the ring is glass: the
* middle stays a plain button with a sharp label. As the pointer comes near,
* the part of the ring closest to it catches the light, and the closer the
* pointer gets the brighter and wider that light grows. Click it and the ring
* cracks from the exact spot you pressed, bursts into shards that scatter
* outward, hangs for a breath, then pulls back together and seals with a soft
* line of light running around it. The button works the whole time: the click
* goes through straight away.
*
* Phones have no hover, so pressing lights the ring under your finger and the
* tap shatters it. The keyboard shatters it from the top centre.
*
* No packages: one canvas around the button, and one requestAnimationFrame
* loop that only runs while something changes.
*/
import { forwardRef, useEffect, useImperativeHandle, useRef, type ButtonHTMLAttributes, type CSSProperties } from "react";
export type GlassShatterButtonProps = ButtonHTMLAttributes<HTMLButtonElement> & {
/** The fill of the solid middle. Any CSS color, including var(--…). Default: near-black. */
color?: string;
/** The label color. Default: black or white, whichever reads best on the fill. */
textColor?: string;
/** The glass tint and its edges. Default: the text color around the button, so it suits light and dark pages. */
glassColor?: string;
/** The light the glass catches near the pointer. Default: a soft violet that reads in light and dark mode. */
glowColor?: string;
/** How far in px from the button the glass starts to glow, 40 to 240. */
range?: number;
/** How thick the glass ring is in px, 4 to 12. */
ring?: number;
};
/* ---------- tuning (the design: keep as written) ---------- */
const OVER = 88; // px of room around the button where shards may fly
const TINT = 0.1; // how strong the glass tint is
const EDGE = 0.24; // how strong the glass's outer edge line is
const SHINE = 0.55; // the white shine along the glass's top edge
const GLOW_IN_MS = 90; // how fast the glow rises as the pointer comes closer
const GLOW_OUT_MS = 280; // how fast it fades as the pointer goes away
const GLOW_CURVE = 1.8; // > 1: the glow grows faster the closer the pointer gets
const CRACK_MS = 90; // cracks run out from the impact before the glass bursts
const BURST_MS = 520; // shards fly out, fast then slowing like in air
const HANG_MS = 80; // shards hang, drifting, before they come back
const RETURN_MS = 540; // shards pull back into place
const RIPPLE = 0.22; // ms per px of ring: shards near the impact go first and come back last
const SEAL_MS = 320; // light runs around the ring as the glass seals
const FLY = 52; // px the farthest shard flies
const SPIN = 2.2; // radians the fastest shard turns
const DROP = 10; // px shards sink while in the air
const CUT_MIN = 5; // px between cracks at the impact (small shards)
const CUT_MAX = 22; // px between cracks far from it (bigger shards)
/** Styles that can't be inline: sizing, press, focus, disabled and reduced motion. */
const CSS = `
.gsb { position: relative; display: inline-flex; min-width: 0; max-width: 100%; padding: var(--gsb-ring, 6px); border: 0; border-radius: var(--gsb-radius, 999px); background: transparent; color: inherit; font: inherit; font-size: 15px; font-weight: 600; letter-spacing: -0.01em; cursor: pointer; -webkit-tap-highlight-color: transparent; touch-action: manipulation; isolation: isolate; }
.gsb-glass { position: absolute; left: 0; top: -${OVER}px; width: 100%; height: calc(100% + ${OVER * 2}px); pointer-events: none; color: var(--gsb-glass, currentColor); }
.gsb-glow { position: absolute; width: 0; height: 0; color: var(--gsb-glow, color-mix(in oklab, rgb(150 120 255) 82%, currentColor)); }
.gsb-face { position: relative; z-index: 1; display: inline-flex; align-items: center; justify-content: center; min-width: 0; height: 44px; padding: 0 22px; border-radius: max(0px, calc(var(--gsb-radius, 999px) - var(--gsb-ring, 6px))); background: var(--gsb-fill, rgb(12 12 14)); box-shadow: inset 0 1px 0 rgb(255 255 255 / .1), 0 1px 2px rgb(0 0 0 / .18); transition: scale .18s cubic-bezier(.23,1,.32,1); }
.gsb-label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--gsb-text, var(--gsb-auto-text, rgb(255 255 255))); }
.gsb:active:not(:disabled) .gsb-face { scale: .97; }
.gsb:focus-visible { outline: 2px solid currentColor; outline-offset: 3px; }
.gsb:disabled { opacity: .5; cursor: not-allowed; }
@media (prefers-reduced-motion: reduce) { .gsb-face { transition: none; } .gsb:active:not(:disabled) .gsb-face { scale: 1; } }`;
/* ---------- small helpers ---------- */
const clamp = (v: number, a: number, b: number) => Math.min(b, Math.max(a, v));
const num = (v: unknown, fallback: number, lo: number, hi: number) =>
typeof v === "number" && Number.isFinite(v) ? clamp(v, lo, hi) : fallback;
const outCubic = (t: number) => 1 - (1 - t) ** 3;
/** Starts slow, pulls in, and settles with a small overshoot, like glass snapping back into its frame. */
const snapBack = (t: number) => {
const c = 1.37;
return t < 0.5
? ((2 * t) ** 2 * ((c + 1) * 2 * t - c)) / 2
: ((2 * t - 2) ** 2 * ((c + 1) * (t * 2 - 2) + c) + 2) / 2;
};
/** A rounded rectangle path, drawn by hand so older Safari works too. */
function rr(ctx: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, r: number) {
r = Math.max(0, Math.min(r, w / 2, h / 2));
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
/** True when a color reads as light, worked out by painting it, so any CSS color format works. */
let probe: CanvasRenderingContext2D | null = null;
function isLight(color: string) {
probe ??= document.createElement("canvas").getContext("2d", { willReadFrequently: true });
if (!probe) return false;
probe.clearRect(0, 0, 1, 1);
probe.fillStyle = "#000";
probe.fillStyle = color;
probe.fillRect(0, 0, 1, 1);
const [r, g, b] = probe.getImageData(0, 0, 1, 1).data;
const lin = (c: number) => ((c /= 255) <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4);
return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b) > 0.4;
}
/** The same color with no opacity, so gradients fade out in that color and not through gray. */
const clearCache = new Map<string, string>();
function clearOf(color: string) {
let c = clearCache.get(color);
if (c) return c;
probe ??= document.createElement("canvas").getContext("2d", { willReadFrequently: true });
if (!probe) return "transparent";
probe.clearRect(0, 0, 1, 1);
probe.fillStyle = "#000";
probe.fillStyle = color;
probe.fillRect(0, 0, 1, 1);
const [r, g, b] = probe.getImageData(0, 0, 1, 1).data;
c = `rgb(${r} ${g} ${b} / 0)`;
if (clearCache.size > 32) clearCache.clear();
clearCache.set(color, c);
return c;
}
/** A steady random number from 0 to 1. */
function rand(i: number, k: number) {
const s = Math.sin(i * 127.1 + k * 311.7) * 43758.5453;
return s - Math.floor(s);
}
/** One point on the middle line of the ring: where it is and which way is out. */
type RingPt = { x: number; y: number; nx: number; ny: number };
type Pose = { x: number; y: number; r: number };
type Shard = {
pts: number[]; // the shard's corners, relative to its centre
cx: number; cy: number; // its centre when whole
ny: number; // which way it faced when whole (up = negative), for its shine
seed: number;
delay: number; // ms after the impact before it moves (ripple)
from: Pose; to: Pose; // where it bursts from and to
};
type Crack = { x1: number; y1: number; x2: number; y2: number; at: number };
/* ---------- the button ---------- */
export const GlassShatterButton = forwardRef<HTMLButtonElement, GlassShatterButtonProps>(function GlassShatterButton(
{ color, textColor, glassColor, glowColor, range = 120, ring = 6, type = "button", className, style, children, ...rest },
ref,
) {
const btnRef = useRef<HTMLButtonElement>(null);
const faceRef = useRef<HTMLSpanElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
const glowRef = useRef<HTMLSpanElement>(null);
useImperativeHandle(ref, () => btnRef.current!, []);
const ringPx = Math.round(num(ring, 6, 4, 12));
// Props the running loop reads without restarting it.
const opts = useRef({ range: 120, ring: 6, disabled: false });
opts.current = { range: num(range, 120, 40, 240), ring: ringPx, disabled: !!rest.disabled };
// Draws the glass again when a prop changes while it is on screen.
const redrawRef = useRef<() => void>(() => {});
useEffect(() => redrawRef.current(), [ringPx, range, rest.disabled, color, glassColor, glowColor]);
// The label picks black or white on the fill, and checks again when the theme changes.
useEffect(() => {
const face = faceRef.current;
if (!face) return;
const apply = () =>
face.style.setProperty("--gsb-auto-text", isLight(getComputedStyle(face).backgroundColor) ? "rgb(10 10 10)" : "rgb(255 255 255)");
apply();
const mo = new MutationObserver(apply);
mo.observe(document.documentElement, { attributes: true, attributeFilter: ["class", "style", "data-theme"] });
const mq = matchMedia("(prefers-color-scheme: dark)");
mq.addEventListener("change", apply);
return () => {
mo.disconnect();
mq.removeEventListener("change", apply);
};
}, [color]);
useEffect(() => {
const btn = btnRef.current, face = faceRef.current, canvas = canvasRef.current, glowEl = glowRef.current;
if (!btn || !face || !canvas || !glowEl) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;
const reduceMq = matchMedia("(prefers-reduced-motion: reduce)");
let reduced = reduceMq.matches;
let cw = 0, ch = 0, dpr = 1, rad = 0, rw = 0; // button size, radius and ring width the path was built for
let padL = -1, padR = -1; // how far the canvas reaches past the button on the left and right
let line: RingPt[] = []; // the ring's middle line, one point per px
let raf = 0, last = 0;
let glow = 0; // 0 to 1, eased
let near = false; // pointer is on screen and not a finger
let pressed = false; // a finger is down on it
const P = { x: -1e4, y: -1e4 }; // the pointer, in the button's own px (client, so it is fresh on scroll)
let client = { x: -1e4, y: -1e4 };
// The shatter. `t0` is when the impact happened; null when the glass is whole.
let t0: number | null = null;
let shards: Shard[] = [];
let cracks: Crack[] = [];
let impact = { x: 0, y: 0, s: 0 };
let span = 0; // ms from impact until the last shard is back
let maxDelay = 0; // ms the ripple takes to reach the far side
let hitAt = 0; // when the last click landed, for its flash
const size = () => {
const w = btn.offsetWidth, h = btn.offsetHeight;
const r = Math.min(parseFloat(getComputedStyle(btn).borderTopLeftRadius) || 0, w / 2, h / 2);
const k = opts.current.ring;
if (w === cw && h === ch && r === rad && k === rw && line.length) return;
cw = w; ch = h; rad = r; rw = k;
// Walk the ring's middle line (inset by half the ring) and keep one point per px.
const ins = k / 2, x0 = ins, y0 = ins, W = w - k, H = h - k, R = Math.max(0, r - ins);
const segs: ((t: number) => RingPt)[] = [];
const lens: number[] = [];
const straight = (ax: number, ay: number, bx: number, by: number, nx: number, ny: number) => {
segs.push((t) => ({ x: ax + (bx - ax) * t, y: ay + (by - ay) * t, nx, ny }));
lens.push(Math.hypot(bx - ax, by - ay));
};
const corner = (cx: number, cy: number, a0: number) => {
segs.push((t) => {
const a = a0 + (Math.PI / 2) * t;
return { x: cx + Math.cos(a) * R, y: cy + Math.sin(a) * R, nx: Math.cos(a), ny: Math.sin(a) };
});
lens.push((Math.PI / 2) * R);
};
straight(x0 + R, y0, x0 + W - R, y0, 0, -1);
corner(x0 + W - R, y0 + R, -Math.PI / 2);
straight(x0 + W, y0 + R, x0 + W, y0 + H - R, 1, 0);
corner(x0 + W - R, y0 + H - R, 0);
straight(x0 + W - R, y0 + H, x0 + R, y0 + H, 0, 1);
corner(x0 + R, y0 + H - R, Math.PI / 2);
straight(x0, y0 + H - R, x0, y0 + R, -1, 0);
corner(x0 + R, y0 + R, Math.PI);
const total = lens.reduce((a, b) => a + b, 0);
const n = Math.max(8, Math.round(total));
line = [];
let si = 0, acc = 0;
for (let i = 0; i < n; i++) {
const d = (i / n) * total;
while (si < lens.length - 1 && d > acc + lens[si]) acc += lens[si++];
line.push(segs[si](lens[si] ? (d - acc) / lens[si] : 0));
}
};
/** The point on the ring's middle line at arc position `s`, wrapping round. */
const at = (s: number) => line[((Math.round(s) % line.length) + line.length) % line.length];
/** The arc position on the ring nearest to a point. */
const nearestS = (x: number, y: number) => {
let best = 0, bd = Infinity;
for (let i = 0; i < line.length; i += 2) {
const d = (line[i].x - x) ** 2 + (line[i].y - y) ** 2;
if (d < bd) { bd = d; best = i; }
}
return best;
};
const local = (cx: number, cy: number) => {
const r = btn.getBoundingClientRect();
const sx = btn.offsetWidth ? r.width / btn.offsetWidth || 1 : 1;
const sy = btn.offsetHeight ? r.height / btn.offsetHeight || 1 : 1;
return { x: (cx - r.left) / sx, y: (cy - r.top) / sy };
};
/** How close the pointer is, 0 (out of range) to 1 (on the button), and the spot on the ring nearest to it. */
const sense = () => {
if (opts.current.disabled || (!near && !pressed)) return { p: 0, s: 0 };
// Distance from the pointer to the button's rounded outline (0 inside).
const R = rad, qx = clamp(P.x, R, cw - R), qy = clamp(P.y, R, ch - R);
const out = Math.max(0, Math.hypot(P.x - qx, P.y - qy) - R);
const p = pressed ? 1 : clamp(1 - out / opts.current.range, 0, 1) ** GLOW_CURVE;
return { p, s: p > 0 ? nearestS(P.x, P.y) : 0 };
};
/** Break the ring into shards around the impact. */
const shatter = (x: number, y: number, now: number) => {
size();
const L = line.length, k = rw, half = L / 2;
const s0 = nearestS(x, y);
// Still in pieces from the last click? Blast the shards again from where they are, with no new cracks.
const whole = t0 === null || now - t0 >= span || reduced;
const poses = whole ? null : shards.map((sh) => pose(sh, now));
impact = { x, y, s: s0 };
hitAt = now;
t0 = whole ? now : now - CRACK_MS;
if (reduced) {
maxDelay = 0;
span = 0;
cracks = [];
}
if (whole) {
// Cut positions along the ring: close together at the impact, further apart away from it.
const cuts: number[] = [];
let d = 0, i = 0;
while (d < half) {
cuts.push(d);
const f = d / half;
d += CUT_MIN + (CUT_MAX - CUT_MIN) * f * f * (3 - 2 * f) + rand(i++, 3) * 4;
}
const arc = [...cuts.slice(1).map((c) => -c).reverse(), ...cuts.filter((c) => c < half - CUT_MIN)];
// Every cut is slanted a little, differently on the outer and inner edge, so the glass looks broken, not sliced.
const cutAt = arc.map((c, j) => ({ c, a: (rand(j, 7) - 0.5) * 6, b: (rand(j, 11) - 0.5) * 6 }));
shards = [];
cracks = [];
const edge = (s: number, side: 1 | -1) => {
const q = at(s0 + s);
return [q.x + q.nx * (k / 2 + 0.5) * side, q.y + q.ny * (k / 2 + 0.5) * side];
};
const crack = (x1: number, y1: number, x2: number, y2: number, s: number) =>
cracks.push({ x1, y1, x2, y2, at: Math.abs(s) * RIPPLE * 0.4 });
const make = (poly: number[], ny: number, j: number) => {
let cx = 0, cy = 0;
for (let m = 0; m < poly.length; m += 2) { cx += poly[m]; cy += poly[m + 1]; }
cx /= poly.length / 2;
cy /= poly.length / 2;
const pts = poly.map((v, m) => v - (m % 2 ? cy : cx));
shards.push({ pts, cx, cy, ny, seed: rand(j, 19), delay: 0, from: { x: 0, y: 0, r: 0 }, to: { x: 0, y: 0, r: 0 } });
};
for (let j = 0; j < cutAt.length; j++) {
const A = cutAt[j], B = cutAt[(j + 1) % cutAt.length];
const sa = A.c;
const sb = j === cutAt.length - 1 ? cutAt[0].c + L : B.c; // the last piece closes the ring
const oa = sa + A.a, ob = sb + B.a, ia = sa + A.b, ib = sb + B.b;
const outer: number[] = [], inner: number[] = [];
const nO = Math.max(1, Math.ceil(Math.abs(ob - oa) / 3)), nI = Math.max(1, Math.ceil(Math.abs(ib - ia) / 3));
for (let m = 0; m <= nO; m++) outer.push(...edge(oa + ((ob - oa) * m) / nO, 1));
for (let m = nI; m >= 0; m--) inner.push(...edge(ia + ((ib - ia) * m) / nI, -1));
// outer runs a to b, inner runs b back to a.
const [oax, oay] = outer, [obx, oby] = outer.slice(-2), [ibx, iby] = inner, [iax, iay] = inner.slice(-2);
const mid = at(s0 + (sa + sb) / 2);
crack(oax, oay, iax, iay, sa);
// Longer pieces split along a diagonal into two triangles, like real broken glass.
if (sb - sa > 9 && rand(j, 23) > 0.25) {
if (rand(j, 29) > 0.5) {
make([...outer, ibx, iby], mid.ny, j * 2);
make([...inner, oax, oay], mid.ny, j * 2 + 1);
crack(oax, oay, ibx, iby, sa);
} else {
make([...outer, iax, iay], mid.ny, j * 2);
make([obx, oby, ...inner], mid.ny, j * 2 + 1);
crack(obx, oby, iax, iay, sa);
}
} else make([...outer, ...inner], mid.ny, j * 2);
}
}
if (reduced) return wake();
// Where each shard flies: away from the impact and out from the button. Shards near the impact fly further and spin more.
maxDelay = 0;
shards.forEach((sh, j) => {
const si = nearestS(sh.cx, sh.cy);
const q = at(si);
const dl = Math.hypot(sh.cx - x, sh.cy - y) || 1;
const dx = ((sh.cx - x) / dl) * 0.5 + q.nx * 0.5, dy = ((sh.cy - y) / dl) * 0.5 + q.ny * 0.5;
const n = Math.hypot(dx, dy) || 1;
const sDist = Math.min(Math.abs(si - s0), L - Math.abs(si - s0));
const close = 1 - sDist / half;
const fly = FLY * (0.35 + 0.65 * close) * (0.6 + 0.4 * rand(j, 31));
sh.delay = whole ? sDist * RIPPLE : 0;
maxDelay = Math.max(maxDelay, sh.delay);
sh.from = poses ? poses[j] : { x: 0, y: 0, r: 0 };
// Hit again while in the air: still never further than the room around the button.
let tx = sh.from.x + (dx / n) * fly, ty = sh.from.y + (dy / n) * fly;
const far = Math.hypot(tx, ty), cap = FLY * 1.25;
if (far > cap) { tx *= cap / far; ty *= cap / far; }
sh.to = { x: tx, y: ty, r: sh.from.r + (rand(j, 37) - 0.5) * 2 * SPIN * (0.4 + 0.6 * close) };
});
span = CRACK_MS + BURST_MS + HANG_MS + maxDelay * 2 + RETURN_MS;
// The middle takes a small knock, so the hit is felt.
face.animate?.([{ transform: "scale(1)" }, { transform: "scale(.965)", offset: 0.22 }, { transform: "scale(1)" }], {
duration: 420,
easing: "cubic-bezier(.23,1,.32,1)",
});
wake();
};
/** Where a shard is at `now`: bursting out, hanging, or coming back (far ones first, the impact last). */
function pose(sh: Shard, now: number): Pose {
if (t0 === null) return { x: 0, y: 0, r: 0 };
const t = now - t0 - CRACK_MS;
const backAt = BURST_MS + HANG_MS + maxDelay * 2 - sh.delay;
const air = Math.min(t, backAt) - sh.delay; // time in the air, frozen once it starts back
const out = clamp(air / BURST_MS, 0, 1);
// Out fast then slowing like in air, drifting a little further while it hangs, sinking slightly.
const k = outCubic(out) + 0.05 * clamp((air - BURST_MS) / (HANG_MS + maxDelay * 2), 0, 1);
const hx = sh.from.x + (sh.to.x - sh.from.x) * k;
const hy = sh.from.y + (sh.to.y - sh.from.y) * k + DROP * out * out;
const hr = sh.from.r + (sh.to.r - sh.from.r) * k;
const b = 1 - snapBack(clamp((t - backAt) / RETURN_MS, 0, 1));
return { x: hx * b, y: hy * b, r: hr * b };
}
const draw = (now: number, g: number, gs: number) => {
// Left the page mid-shatter, or hidden: nothing to draw on.
if (!btn.isConnected || !btn.offsetWidth) return;
// Colors are read from the page on every frame, so they follow light and dark mode.
const glass = getComputedStyle(canvas).color;
const light = getComputedStyle(glowEl).color;
if (!glass || !light) return;
size();
const nd = Math.min(2, devicePixelRatio || 1);
// Room for shards on the left and right, but never past the screen's edge, so the page can't scroll sideways.
const box = btn.getBoundingClientRect();
const sc = box.width / cw || 1;
const vw = document.documentElement.clientWidth;
const oL = Math.round(clamp(box.left / sc, 0, OVER)), oR = Math.round(clamp((vw - box.right) / sc, 0, OVER));
if (oL !== padL || oR !== padR || canvas.style.width !== `${cw + oL + oR}px`) {
padL = oL;
padR = oR;
canvas.style.left = `${-oL}px`;
canvas.style.width = `${cw + oL + oR}px`;
}
const W = cw + oL + oR, H = ch + OVER * 2;
if (canvas.width !== Math.round(W * nd) || canvas.height !== Math.round(H * nd) || nd !== dpr) {
dpr = nd;
canvas.width = Math.round(W * dpr);
canvas.height = Math.round(H * dpr);
}
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, W, H);
ctx.translate(oL, OVER);
const lightClear = clearOf(light);
const k = rw, R = rad;
const age = t0 === null ? Infinity : now - t0;
const broken = !reduced && age >= CRACK_MS && age < span; // shards are out (the first moments show cracks on the whole ring)
const crackA = t0 === null ? 0 : reduced
? (age < 120 ? age / 120 : clamp(1 - (age - 420) / 300, 0, 1))
: age < span ? clamp(age / CRACK_MS, 0, 1) : clamp(1 - (age - span) / SEAL_MS, 0, 1);
const ringPath = () => {
ctx.beginPath();
rr(ctx, 0, 0, cw, ch, R);
rr(ctx, k, k, cw - 2 * k, ch - 2 * k, Math.max(0, R - k));
};
const gp = at(gs);
const glowFill = (alpha: number, radius: number) => {
const grd = ctx.createRadialGradient(gp.x, gp.y, 0, gp.x, gp.y, radius);
grd.addColorStop(0, light);
grd.addColorStop(1, lightClear);
ctx.globalAlpha = alpha;
ctx.fillStyle = grd;
};
if (!broken) {
// 1. The whole ring: a faint tint, a top shine, edge lines.
ringPath();
ctx.fillStyle = glass;
ctx.globalAlpha = TINT;
ctx.fill("evenodd");
const shine = ctx.createLinearGradient(0, 0, 0, ch);
shine.addColorStop(0, "rgb(255 255 255 / .55)");
shine.addColorStop(0.45, "rgb(255 255 255 / 0)");
ctx.globalAlpha = 1;
ctx.fillStyle = shine;
ctx.fill("evenodd");
ctx.lineWidth = 1;
ctx.globalAlpha = EDGE;
ctx.strokeStyle = glass;
ctx.beginPath();
rr(ctx, 0.5, 0.5, cw - 1, ch - 1, R - 0.5);
ctx.stroke();
ctx.globalAlpha = EDGE * 0.6;
ctx.beginPath();
rr(ctx, k - 0.5, k - 0.5, cw - 2 * k + 1, ch - 2 * k + 1, Math.max(0, R - k + 0.5));
ctx.stroke();
// A glint along the top left, where the light lands.
const glint = ctx.createLinearGradient(R * 0.4, 0, R + cw * 0.3, 0);
glint.addColorStop(0, "rgb(255 255 255 / 0)");
glint.addColorStop(0.35, `rgb(255 255 255 / ${SHINE})`);
glint.addColorStop(1, "rgb(255 255 255 / 0)");
ctx.globalAlpha = 1;
ctx.strokeStyle = glint;
ctx.beginPath();
rr(ctx, 1.5, 1.5, cw - 3, ch - 3, R - 1.5);
ctx.stroke();
// 2. The glow: the glass nearest the pointer catches the light, more as it comes closer.
if (g > 0.002) {
const radius = 16 + 52 * g;
ctx.save();
ringPath();
ctx.clip("evenodd");
glowFill(0.8 * g, radius);
ctx.fillRect(-OVER, -OVER, cw + OVER * 2, ch + OVER * 2);
ctx.restore();
// Edges light up brightest, like light running along cut glass.
ctx.save();
glowFill(Math.min(1, 1.2 * g), radius * 1.1);
ctx.strokeStyle = ctx.fillStyle;
ctx.lineWidth = 1.25;
ctx.beginPath();
rr(ctx, 0.6, 0.6, cw - 1.2, ch - 1.2, R - 0.6);
ctx.stroke();
ctx.beginPath();
rr(ctx, k - 0.6, k - 0.6, cw - 2 * k + 1.2, ch - 2 * k + 1.2, Math.max(0, R - k + 0.6));
ctx.stroke();
ctx.restore();
}
} else {
// 3. The shards: same glass, each with its own shine that flickers as it turns.
ctx.lineJoin = "round";
for (const sh of shards) {
const ps = pose(sh, now);
const x = sh.cx + ps.x, y = sh.cy + ps.y;
const c = Math.cos(ps.r), s = Math.sin(ps.r);
ctx.beginPath();
for (let m = 0; m < sh.pts.length; m += 2) {
const px = x + sh.pts[m] * c - sh.pts[m + 1] * s, py = y + sh.pts[m] * s + sh.pts[m + 1] * c;
if (m) ctx.lineTo(px, py);
else ctx.moveTo(px, py);
}
ctx.closePath();
ctx.fillStyle = glass;
ctx.globalAlpha = TINT * 1.6;
ctx.fill();
ctx.fillStyle = "rgb(255 255 255)";
ctx.globalAlpha = 0.22 * clamp(-sh.ny, 0, 1) + 0.04;
ctx.fill();
const flick = Math.max(0, Math.sin(ps.r * 2.4 + sh.seed * 6.28)) ** 3;
const lit = g > 0 ? Math.max(0, 1 - Math.hypot(x - P.x, y - P.y) / (40 + 60 * g)) * g : 0;
if (flick + lit > 0.02) {
ctx.fillStyle = light;
ctx.globalAlpha = clamp(0.4 * flick + 0.6 * lit, 0, 0.8);
ctx.fill();
}
ctx.strokeStyle = glass;
ctx.globalAlpha = EDGE * 1.4;
ctx.lineWidth = 0.75;
ctx.stroke();
}
}
// 4. Cracks: they run out from the impact, and fade as the glass seals.
if (!broken && crackA > 0.01 && cracks.length) {
ctx.lineWidth = 0.75;
ctx.strokeStyle = glass;
const grown = reduced ? Infinity : age;
ctx.beginPath();
for (const c of cracks) if (c.at <= grown) { ctx.moveTo(c.x1, c.y1); ctx.lineTo(c.x2, c.y2); }
ctx.globalAlpha = 0.5 * crackA;
ctx.stroke();
}
// 5. The impact flash and the seal: light at the impact, then running both ways round the ring.
if (t0 !== null) {
const flash = clamp(1 - (now - hitAt) / 260, 0, 1);
if (flash > 0) {
// The flash stays inside the glass too: nothing glows outside the border.
ctx.save();
ringPath();
ctx.clip("evenodd");
const grd = ctx.createRadialGradient(impact.x, impact.y, 0, impact.x, impact.y, 26 + 30 * (1 - flash));
grd.addColorStop(0, light);
grd.addColorStop(1, lightClear);
ctx.globalAlpha = 0.7 * flash * flash;
ctx.fillStyle = grd;
ctx.fillRect(impact.x - 60, impact.y - 60, 120, 120);
ctx.restore();
}
const sealT = reduced ? -1 : (age - span) / SEAL_MS;
if (sealT >= 0 && sealT <= 1) {
ctx.save();
ringPath();
ctx.clip("evenodd");
const L = line.length;
for (const dir of [1, -1]) {
const q = at(impact.s + dir * (L / 2) * outCubic(sealT));
const grd = ctx.createRadialGradient(q.x, q.y, 0, q.x, q.y, 22);
grd.addColorStop(0, light);
grd.addColorStop(1, lightClear);
ctx.globalAlpha = 0.9 * (1 - sealT);
ctx.fillStyle = grd;
ctx.fillRect(q.x - 24, q.y - 24, 48, 48);
}
ctx.restore();
}
}
ctx.globalAlpha = 1;
};
let lastG = -1, lastS = -1;
const frame = (now: number) => {
raf = 0;
const dt = Math.min(50, last ? now - last : 16);
last = now;
if (near) Object.assign(P, local(client.x, client.y));
const { p, s } = sense();
glow += (p - glow) * (1 - Math.exp(-dt / (p > glow ? GLOW_IN_MS : GLOW_OUT_MS)));
if (Math.abs(p - glow) < 0.004) glow = p;
const gs = p > 0 ? s : lastS < 0 ? s : lastS; // the light fades where it was
const age = t0 === null ? Infinity : now - t0;
const animating = age < (reduced ? 720 : span + SEAL_MS);
if (!animating && t0 !== null) { t0 = null; shards = []; cracks = []; }
// Draw only when something on screen changed.
if (animating || Math.abs(glow - lastG) > 0.001 || gs !== lastS || lastG < 0) {
draw(now, glow, gs);
lastG = glow;
lastS = gs;
}
if (animating || glow !== p) raf = requestAnimationFrame(frame);
};
const wake = () => {
if (!raf && !document.hidden) {
last = 0;
raf = requestAnimationFrame(frame);
}
};
const redraw = () => {
lastG = -1;
wake();
};
// The pointer is watched on the whole page, so the glass can feel it coming. Only while the button is on screen.
let onScreen = false;
const onPageMove = (e: PointerEvent) => {
if (e.pointerType === "touch") return;
near = true;
client = { x: e.clientX, y: e.clientY };
wake();
};
const onPageLeave = () => {
near = false;
wake();
};
const onScroll = () => near && wake();
const listen = (on: boolean) => {
if (on === onScreen) return;
onScreen = on;
if (on) {
window.addEventListener("pointermove", onPageMove, { passive: true });
document.documentElement.addEventListener("pointerleave", onPageLeave);
window.addEventListener("blur", onPageLeave);
window.addEventListener("scroll", onScroll, { passive: true, capture: true });
} else {
window.removeEventListener("pointermove", onPageMove);
document.documentElement.removeEventListener("pointerleave", onPageLeave);
window.removeEventListener("blur", onPageLeave);
window.removeEventListener("scroll", onScroll, { capture: true });
near = false;
}
};
const io = new IntersectionObserver(([en]) => listen(en.isIntersecting), { rootMargin: "240px" });
io.observe(btn);
const onDown = (e: PointerEvent) => {
if (e.button !== 0 || opts.current.disabled) return;
const pt = local(e.clientX, e.clientY);
if (e.pointerType === "touch") {
pressed = true;
Object.assign(P, pt);
}
shatter(pt.x, pt.y, performance.now());
};
const onUp = (e: PointerEvent) => {
if (e.pointerType === "touch") {
pressed = false;
wake();
}
};
// Enter or Space on the keyboard: the glass breaks from the top centre.
const onClick = (e: MouseEvent) => {
if (e.detail === 0 && !opts.current.disabled) shatter(btn.offsetWidth / 2, 0, performance.now());
};
const onVis = () => {
if (document.hidden) {
cancelAnimationFrame(raf);
raf = 0;
} else redraw();
};
const onReduce = () => {
reduced = reduceMq.matches;
redraw();
};
// Theme or size changes: draw the glass again.
const ro = new ResizeObserver(redraw);
ro.observe(btn);
window.addEventListener("resize", redraw);
const mo = new MutationObserver(redraw);
mo.observe(document.documentElement, { attributes: true, attributeFilter: ["class", "style", "data-theme"] });
btn.addEventListener("pointerdown", onDown);
btn.addEventListener("pointerup", onUp);
btn.addEventListener("pointercancel", onUp);
btn.addEventListener("click", onClick);
document.addEventListener("visibilitychange", onVis);
reduceMq.addEventListener("change", onReduce);
redrawRef.current = redraw;
redraw();
return () => {
cancelAnimationFrame(raf);
raf = 0;
redrawRef.current = () => {};
listen(false);
io.disconnect();
ro.disconnect();
mo.disconnect();
window.removeEventListener("resize", redraw);
btn.removeEventListener("pointerdown", onDown);
btn.removeEventListener("pointerup", onUp);
btn.removeEventListener("pointercancel", onUp);
btn.removeEventListener("click", onClick);
document.removeEventListener("visibilitychange", onVis);
reduceMq.removeEventListener("change", onReduce);
};
}, []);
return (
<>
<style>{CSS}</style>
<button
ref={btnRef}
type={type}
className={className ? `gsb ${className}` : "gsb"}
style={
{
"--gsb-ring": `${ringPx}px`,
...(color ? { "--gsb-fill": color } : null),
...(textColor ? { "--gsb-text": textColor } : null),
...(glassColor ? { "--gsb-glass": glassColor } : null),
...(glowColor ? { "--gsb-glow": glowColor } : null),
...style,
} as CSSProperties
}
{...rest}
>
<canvas ref={canvasRef} className="gsb-glass" width={0} height={0} aria-hidden="true" />
<span ref={glowRef} className="gsb-glow" aria-hidden="true" />
<span ref={faceRef} className="gsb-face">
<span className="gsb-label">{children}</span>
</span>
</button>
</>
);
});
```
Usage
Use it like any button. Everything has a default.
"use client";
import { GlassShatterButton } from "@/components/ui/glass-shatter-button";
export function HeroAction() {
return (
<GlassShatterButton
color="var(--foreground)" // optional: the solid middle (default near-black)
textColor="var(--background)" // optional: the label
glowColor="var(--primary)" // optional: the light the glass catches
range={120} // optional: how far away the glass starts to glow
onClick={() => router.push("/signup")}
>
Get started
</GlassShatterButton>
);
}The shards fly up to about 60px outside the button, so keep it away from the edge of a box that hides overflow.
How it moves
Only the ring around the button is glass. The middle is a plain, solid button, so the label always reads.
As your pointer comes within range (120px), the part of the ring nearest to it catches the light. The glow grows faster the closer you get: faint at the edge of the range, bright and wide once you are on the button, and its edges light up most, like light running along cut glass. Click it and the click goes through straight away while the glass breaks: cracks run out from the exact spot you pressed (90ms), then the ring bursts into shards, small ones near the impact and bigger ones further away. They fly out fast and slow down like in air, turning and catching the light as they spin (about half a second), hang for a breath, then pull back in, the far ones first and the ones at the impact last, settling with a tiny snap. A line of light runs both ways round the ring as it seals. The whole thing takes about a second and a half. Click again while it is in pieces and the shards are blasted again from where they are. On a phone, pressing lights the glass under your finger and the tap shatters it. The keyboard breaks it from the top. With reduced motion, the cracks show and fade, and nothing flies.
Props
| Prop | Default | What it does |
|---|---|---|
children | — | The label. Sharp text on the solid middle. It stays on one line and trims with … if it runs out of room. |
color | near-black | The fill of the solid middle. Any CSS color, including var(--…). |
textColor | black or white | The label color. By default it picks whichever reads best on the fill. |
glassColor | the text color around it | The glass tint and its edge lines. By default it follows your page's text color, so the glass shows on light and dark pages. |
glowColor | soft violet | The light the glass catches near the pointer, and the flash and seal on click. |
range | 120 | How far in px from the button the glass starts to glow, 40 to 240. The glow grows faster the closer the pointer gets. |
ring | 6 | How thick the glass ring is in px, 4 to 12. The middle keeps its 44px height, so the button grows by twice this. |
onClick, type, disabled, … | type="button" | Everything a normal button takes. The click fires straight away, while the glass is still breaking. Disabled stops the glow and the shatter. |
className / style | — | On the button itself. Change border-radius here: the middle's corners follow, minus the ring. |