# GlassButton

> A button made of glass. Clear glass bends the page behind it; tinted glass takes your background colour.

```tsx
import { Play } from "lucide-react";

import { GlassButton } from "@vitrumui/react/button";

const COLS = 13;
const ROWS = 7;

/** 1 around the middle of the grid, falling to 0 at its edge. */
const fade = (col: number, row: number) => {
  const distance = Math.hypot(
    (col - (COLS - 1) / 2) / (COLS / 2),
    (row - (ROWS - 1) / 2) / (ROWS / 2),
  );
  return Math.min(1, Math.max(0, (1 - distance) / 0.45));
};

// Clear glass bends whatever sits behind it: here, only a fine grid centred on the glass,
// on the page's own colour. Watch its lines bend at the rim. The grid is plain DOM: every
// cell draws its own top and left line, fainter the further it is from the glass.
export default function Demo() {
  return (
    <div className="relative isolate flex h-72 w-full max-w-xl items-center justify-center">
      <div
        aria-hidden
        className="absolute top-1/2 left-1/2 -z-10 grid -translate-x-1/2 -translate-y-1/2 grid-cols-[repeat(13,40px)]"
      >
        {Array.from({ length: COLS * ROWS }, (_, i) => (
          <span
            key={i}
            className="relative size-10"
            style={{ opacity: fade(i % COLS, Math.floor(i / COLS)) }}
          >
            <span className="absolute inset-x-0 top-0 h-px bg-black/10 dark:bg-white/10" />
            <span className="absolute inset-y-0 left-0 w-px bg-black/10 dark:bg-white/10" />
          </span>
        ))}
      </div>
      <GlassButton size="xl" tint={0} icon={<Play />} iconPosition="start">
        Play
      </GlassButton>
    </div>
  );
}
```

**Use it for**

- One action: a call to action, a toolbar button, a link that looks like a button.
- Clear glass over imagery or colour, where the bend shows.

**Not for**

- An async action whose result should show on the button: use GlassActionButton.
- Choosing between options: use GlassTabs.
- Every button of a dense form: keep the glass for the action that matters.

```bash
npm install @vitrumui/react # coming soon
```

## Usage

```tsx
import { GlassButton } from '@vitrumui/react/button'

<GlassButton>Get started</GlassButton>
<GlassButton material="tinted" className="bg-sky-500 text-white">Save</GlassButton>
<GlassButton asChild>
  <Link href="/docs">Read the docs</Link>
</GlassButton>
```

## Examples

### Tinted colours and sizes

```tsx
import { GlassButton } from "@vitrumui/react/button";

// Tinted glass takes the button's own background class, then adds light and shadow.
// Top row: your colours. Bottom row: the four sizes.
export default function Demo() {
  return (
    <div className="flex flex-col items-center gap-10">
      <div className="flex flex-wrap items-center justify-center gap-3">
        <GlassButton
          material="tinted"
          size="lg"
          className="bg-blue-600 text-white"
        >
          Continue
        </GlassButton>
        <GlassButton
          material="tinted"
          size="lg"
          className="bg-neutral-900 text-white dark:bg-white dark:text-neutral-900"
        >
          Publish
        </GlassButton>
        <GlassButton
          material="tinted"
          size="lg"
          className="bg-rose-600 text-white"
        >
          Delete
        </GlassButton>
        <GlassButton
          material="tinted"
          size="lg"
          className="border border-black/15 bg-white text-neutral-900"
        >
          Cancel
        </GlassButton>
      </div>
      <div className="flex flex-wrap items-center justify-center gap-3">
        <GlassButton
          material="tinted"
          size="sm"
          className="bg-emerald-600 text-white"
        >
          Small
        </GlassButton>
        <GlassButton
          material="tinted"
          size="md"
          className="bg-emerald-600 text-white"
        >
          Medium
        </GlassButton>
        <GlassButton
          material="tinted"
          size="lg"
          className="bg-emerald-600 text-white"
        >
          Large
        </GlassButton>
        <GlassButton
          material="tinted"
          size="xl"
          className="bg-emerald-600 text-white"
        >
          Extra large
        </GlassButton>
      </div>
    </div>
  );
}
```

Colour tinted glass with a normal class such as `bg-sky-500`; the glass paints itself in that colour and adds light and shadow. A `border-*` class becomes the glass rim.

### As a link (asChild)

```tsx
import { ArrowUpRight } from "lucide-react";

import { GlassButton } from "@vitrumui/react/button";

// `asChild` puts the glass on your own element. The same works with a Next.js or
// TanStack Router <Link>: it stays a real link with its own navigation.
export default function Demo() {
  return (
    <div className="flex flex-col items-center gap-6 text-center">
      <div>
        <p className="text-3xl font-semibold tracking-tight text-neutral-900 dark:text-white">
          Free and open source
        </p>
        <p className="mt-2 text-base text-neutral-500 dark:text-neutral-400">
          MIT licensed. Copy the files, they are yours.
        </p>
      </div>
      <GlassButton
        asChild
        material="tinted"
        size="lg"
        icon={<ArrowUpRight />}
        className="bg-violet-600 text-white"
      >
        <a href="/docs">Read the docs</a>
      </GlassButton>
    </div>
  );
}
```

With `asChild` the glass goes on your single child element instead of a `<button>`: an `<a>`, a Next.js `<Link>`, a TanStack Router `<Link>`. Your element keeps its own props and navigation.

### Over colour with a scrim

```tsx
import { ArrowRight, Play } from "lucide-react";

import { GlassButton } from "@vitrumui/react/button";

// Clear glass over colour. The backdrop is plain DOM: flat discs of colour on a dark scene
// and a dark scrim over them. The glass sees both, and the text, so the buttons sit in the scene.
export default function Demo() {
  return (
    <div className="relative isolate flex h-96 w-full max-w-2xl flex-col items-start justify-end overflow-hidden rounded-3xl bg-[#0b1030] p-8">
      <div aria-hidden className="absolute inset-0 -z-20">
        <span className="absolute -top-24 -left-16 size-80 rounded-full bg-[#22d3ee]" />
        <span className="absolute -top-32 right-4 size-72 rounded-full bg-[#a855f7]" />
        <span className="absolute -right-20 -bottom-28 size-96 rounded-full bg-[#ec4899]" />
        <span className="absolute -bottom-40 left-24 size-80 rounded-full bg-[#1d4ed8]" />
      </div>
      <div aria-hidden className="absolute inset-0 -z-10 bg-black/35" />
      <p className="text-xs font-semibold tracking-[0.18em] text-white/70">
        TROMSØ · WINTER SEASON
      </p>
      <h3 className="mt-2 max-w-sm text-4xl leading-[1.05] font-semibold tracking-tight text-white">
        Chase the northern lights
      </h3>
      <div className="mt-6 flex flex-wrap gap-3">
        <GlassButton size="lg" tint={0.35} icon={<ArrowRight />}>
          Plan the trip
        </GlassButton>
        <GlassButton size="lg" tint={0.15} icon={<Play />} iconPosition="start">
          Watch the film
        </GlassButton>
      </div>
    </div>
  );
}
```

Clear glass repaints what lies behind it: background colours, gradients and images, `<img>` and `<video>` the way the page crops them (`object-fit`), text, inline SVG icons and backdrop blur, in their stacking order. Shadows, `::before`/`::after` and conic gradients are not painted.

> An image from another origin must be served with CORS headers (image CDNs usually send them): the glass requests it again with CORS, and leaves it out if the server refuses.

### Square corners

```tsx
import { Plus } from "lucide-react";

import { GlassButton } from "@vitrumui/react/button";

// The glass takes the button's own corners: add a `rounded-*` class and the lens follows.
// Fine ruled lines show the bend running along the straight edges and around each corner.
export default function Demo() {
  return (
    <div className="relative isolate flex h-72 w-full max-w-2xl flex-wrap items-center justify-center gap-4 overflow-hidden rounded-3xl">
      <div
        aria-hidden
        className="absolute inset-0 -z-10 flex flex-col justify-between"
      >
        {Array.from({ length: 33 }, (_, i) => (
          <span key={i} className="h-px bg-black/10 dark:bg-white/10" />
        ))}
      </div>
      <GlassButton
        size="xl"
        tint={0.3}
        className="rounded-2xl"
        icon={<Plus />}
        iconPosition="start"
      >
        New project
      </GlassButton>
      <GlassButton
        size="xl"
        material="tinted"
        className="rounded-2xl bg-neutral-900 text-white dark:bg-white dark:text-neutral-900"
      >
        Open
      </GlassButton>
    </div>
  );
}
```

The glass takes the corners of the element it sits on. Add a `rounded-*` class and the lens, its rim and its shadow follow; the bend stays as deep as on a pill.

## API reference

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `material` | `'clear' \| 'tinted'` | `'clear'` | Clear bends the page; tinted uses the background colour. |
| `size` | `'sm' \| 'md' \| 'lg' \| 'xl'` | `'md'` | Height 32, 40, 48 or 64 px. |
| `icon` | `ReactNode` | — | An icon element (e.g. a lucide-react icon). |
| `iconPosition` | `'start' \| 'end'` | `'end'` | Which side the icon sits on. |
| `tint` | `number` | `0.6` | Clear glass only: how much tint covers the backdrop (0–1). |
| `asChild` | `boolean` | `false` | Put the glass on the single child element instead of a `<button>`. |
| `…rest` | `button attributes` | — | Passed to the element (`onClick`, `disabled`, `type`…). |

## Accessibility

- A real `<button>` (or your own element with `asChild`): focus, Enter and Space work as usual.
- The glass canvas is `aria-hidden`; only your label is announced.
- Icon-only buttons need an `aria-label`.

Source: /docs/button
