# GlassCard

> A card made of frosted glass. Your content sits on it; its face blurs and tints whatever lies behind so the text stays readable, and its rim pulls it towards the centre.

```tsx
import { Cloud, Navigation } from "lucide-react";
import { useEffect, useState, type CSSProperties } from "react";

import { GlassCard } from "@vitrumui/react/card";

// Two rows of colour slide behind two cards, and the rims bend them as they pass. The rows
// move, so the cards are `live`: they repaint what is behind them every frame.
const colours = [
  "#0a84ff",
  "#ff6b3d",
  "#30d158",
  "#ff375f",
  "#ffd60a",
  "#bf5af2",
];

function Row({
  shift,
  seconds,
  reverse = false,
}: {
  shift: number;
  seconds: number;
  reverse?: boolean;
}) {
  const pills = [...colours, ...colours].map(
    (_, i) => colours[(i + shift) % colours.length],
  );
  return (
    // Two identical halves slide by exactly one half, on the kit's own marquee (it stops under reduced motion).
    <div
      data-vt-marquee
      className="flex w-max"
      style={
        {
          "--vt-marquee-duration": `${seconds}s`,
          animationDirection: reverse ? "reverse" : undefined,
        } as CSSProperties
      }
    >
      {[0, 1].map((half) => (
        <div key={half} className="flex gap-3 pr-3">
          {pills.map((colour, i) => (
            <span
              key={i}
              className="h-12 w-24 shrink-0 rounded-full"
              style={{ background: colour }}
            />
          ))}
        </div>
      ))}
    </div>
  );
}

type ProgressiveBlurProps = {
  className?: string;
  backgroundColor?: string;
  position?: "left" | "right";
  width?: string;
  blurAmount?: string;
};

/** A progressive blur at one end of the rows: the stage colour over a backdrop blur, fading towards the middle. */
function ProgressiveBlur({
  className = "",
  backgroundColor = "var(--stage, #ececee)",
  position = "left",
  width = "150px",
  blurAmount = "4px",
}: ProgressiveBlurProps) {
  const isLeft = position === "left";
  return (
    <div
      aria-hidden
      className={`pointer-events-none absolute top-0 h-full select-none ${className}`}
      style={{
        [isLeft ? "left" : "right"]: 0,
        width,
        background: `linear-gradient(to ${isLeft ? "left" : "right"}, transparent, ${backgroundColor})`,
        maskImage: `linear-gradient(to ${isLeft ? "right" : "left"}, #000 50%, transparent)`,
        WebkitMaskImage: `linear-gradient(to ${isLeft ? "right" : "left"}, #000 50%, transparent)`,
        WebkitBackdropFilter: `blur(${blurAmount})`,
        backdropFilter: `blur(${blurAmount})`,
        WebkitUserSelect: "none",
        userSelect: "none",
      }}
    />
  );
}

/** Today, read after mount so a prerendered page doesn't freeze the day it was built. */
function useToday() {
  const [today, setToday] = useState<Date | null>(null);
  useEffect(() => setToday(new Date()), []);
  return today;
}

export default function Demo() {
  const today = useToday();
  const events = [
    {
      title: "Design review",
      note: "10:00 · Studio",
      href: "/docs",
      colour: "#0a84ff",
    },
    {
      title: "Release notes",
      note: "14:30 · Main hall",
      href: "/changelog",
      colour: "#ff6b3d",
    },
  ];
  return (
    <div className="relative flex w-full flex-wrap items-center justify-center gap-5 py-14">
      <div
        aria-hidden
        className="absolute inset-0 flex flex-col justify-center gap-3 overflow-hidden"
      >
        <Row shift={0} seconds={40} />
        <Row shift={3} seconds={48} reverse />
        <ProgressiveBlur position="left" />
        <ProgressiveBlur position="right" />
      </div>

      <GlassCard live className="flex size-44 flex-col justify-between p-4">
        <div>
          <p className="flex items-center gap-1 text-[13px] font-semibold">
            Istanbul
            <Navigation className="size-2.5 fill-current" />
          </p>
          <p className="mt-0.5 text-[44px] leading-none font-light tracking-[-0.04em]">
            16°
          </p>
        </div>
        <div className="text-[12px] leading-snug font-semibold">
          <Cloud className="mb-1 size-4 fill-current" />
          <p>Cloudy</p>
          <p className="opacity-60">H:19° L:15°</p>
        </div>
      </GlassCard>

      <GlassCard live className="flex h-44 w-64 flex-col justify-between p-5">
        <div className="flex items-baseline justify-between">
          <p className="text-[14px] font-semibold">
            {today
              ? today.toLocaleDateString(undefined, { weekday: "long" })
              : "Today"}
          </p>
          <p className="text-[34px] leading-none font-semibold tracking-[-0.05em] tabular-nums">
            {today?.getDate()}
          </p>
        </div>
        <ul className="flex flex-col gap-2">
          {events.map((e) => (
            <li key={e.title}>
              <a
                href={e.href}
                className="flex items-stretch gap-2.5 rounded-sm"
              >
                <span
                  className="w-1 shrink-0 rounded-full"
                  style={{ background: e.colour }}
                />
                <span>
                  <span className="block text-[13px] leading-tight font-semibold">
                    {e.title}
                  </span>
                  <span className="block text-[11px] font-medium opacity-60">
                    {e.note}
                  </span>
                </span>
              </a>
            </li>
          ))}
        </ul>
      </GlassCard>
    </div>
  );
}
```

**Use it for**

- A small surface of content over colour, imagery or motion: a widget, a now-playing card, a summary.
- One or two cards, where what is behind them is part of the design.

**Not for**

- Long text and forms: the backdrop shows through. Use a plain card.
- A card on a flat background: there is nothing to bend.
- One clickable action: use GlassButton.

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

## Usage

```tsx
import { GlassCard } from '@vitrumui/react/card'

<GlassCard className="w-64 p-5">
  <p className="font-semibold">Wednesday</p>
  <p className="text-sm opacity-60">Design review · 10:00</p>
</GlassCard>

// Something moves behind it: repaint what it bends every frame.
<GlassCard live className="size-44 p-4">…</GlassCard>

// Clearer glass: no frost, less tint.
<GlassCard blur={0} tint={0.3} className="w-64 p-5">…</GlassCard>
```

## Examples

### Widgets over a wallpaper

```tsx
import { Flame, Headphones, Smartphone, Watch } from "lucide-react";

import { GlassCard } from "@vitrumui/react/card";

// A home screen of widgets over a still wallpaper: bands of colour and three hard-edged discs,
// all plain CSS, and the rims bend them. Nothing moves behind the cards, so they need no
// `live`: like every glass in the kit, they repaint only when something changes, and stop
// drawing at rest.
const wallpaper =
  "linear-gradient(155deg, #0a84ff 0 24%, #5e5ce6 24% 46%, #bf5af2 46% 66%, #ff375f 66% 84%, #ff9f0a 84%)";

/** A progress ring in plain CSS: a conic gradient with its middle masked out. */
function Ring({ value, colour }: { value: number; colour: string }) {
  const hole =
    "radial-gradient(farthest-side, transparent calc(100% - 9px), #000 calc(100% - 8px))";
  return (
    <span
      role="img"
      aria-label={`${Math.round(value * 100)}%`}
      className="block aspect-square rounded-full"
      style={{
        background: `conic-gradient(${colour} ${value * 100}%, color-mix(in srgb, ${colour} 22%, transparent) 0)`,
        maskImage: hole,
        WebkitMaskImage: hole,
      }}
    />
  );
}

const move = { done: 420, goal: 600 };
const devices = [
  { name: "Phone", level: 82, icon: Smartphone },
  { name: "Earbuds", level: 64, icon: Headphones },
  { name: "Watch", level: 18, icon: Watch },
];
const track = {
  title: "Slow Liquid",
  artist: "Glasswork",
  played: 0.38,
  elapsed: "1:12",
  left: "-2:04",
};

export default function Demo() {
  return (
    <div
      className="relative flex w-full max-w-2xl justify-center overflow-hidden rounded-3xl px-3 py-10 sm:px-5 sm:py-16"
      style={{ background: wallpaper }}
    >
      <div aria-hidden className="absolute inset-0">
        <span className="absolute -top-10 left-[8%] size-40 rounded-full bg-[#ffd60a]" />
        <span className="absolute top-[38%] right-[6%] size-56 rounded-full bg-[#30d158]" />
        <span className="absolute -bottom-16 left-[22%] size-48 rounded-full bg-white" />
      </div>

      <div className="relative grid w-full max-w-[316px] grid-cols-2 gap-3 sm:max-w-[372px] sm:gap-5">
        {/* Activity: one ring, one number. */}
        <GlassCard className="flex aspect-square flex-col justify-between p-3 sm:p-4">
          <span className="relative w-12 sm:w-[72px]">
            <Ring value={move.done / move.goal} colour="#ff375f" />
            <Flame className="absolute inset-0 m-auto size-4 fill-current text-[#ff375f] sm:size-5" />
          </span>
          <div>
            <p className="text-[20px] leading-none font-semibold tracking-[-0.03em] tabular-nums sm:text-[26px]">
              {move.done}
              <span className="opacity-50">/{move.goal}</span>
            </p>
            <p className="mt-1 text-[10px] font-semibold tracking-wide whitespace-nowrap text-[#ff375f] uppercase sm:text-[11px]">
              Move · kcal
            </p>
          </div>
        </GlassCard>

        {/* Batteries: a level for each device. */}
        <GlassCard className="flex aspect-square flex-col justify-between p-3 sm:p-4">
          {devices.map(({ name, level, icon: Icon }) => (
            <div key={name}>
              <p className="flex items-center gap-1 text-[11px] leading-none font-semibold sm:gap-1.5 sm:text-[12px]">
                <Icon className="size-3.5 shrink-0" />
                <span className="flex-1">{name}</span>
                <span className="tabular-nums opacity-60">{level}%</span>
              </p>
              <span className="mt-1.5 block h-1 overflow-hidden rounded-full bg-current/15">
                <span
                  className="block h-full rounded-full"
                  style={{
                    width: `${level}%`,
                    background: level > 20 ? "#30d158" : "#ff375f",
                  }}
                />
              </span>
            </div>
          ))}
        </GlassCard>

        {/* Now playing: the cover, the track, and how far in. */}
        <GlassCard className="col-span-2 flex items-center gap-3.5 p-3.5 sm:gap-5 sm:p-5">
          <span
            aria-hidden
            className="size-[72px] shrink-0 rounded-2xl sm:size-[104px]"
            style={{
              background:
                "radial-gradient(circle at 30% 30%, #ffd60a 0 22%, transparent 23%), linear-gradient(135deg, #ff375f, #5e5ce6)",
            }}
          />
          <div className="min-w-0 flex-1">
            <p className="text-[11px] font-semibold tracking-wide uppercase opacity-60">
              Now playing
            </p>
            <p className="mt-1 truncate text-[17px] leading-tight font-semibold tracking-[-0.01em]">
              {track.title}
            </p>
            <p className="truncate text-[13px] font-medium opacity-60">
              {track.artist}
            </p>
            <span className="mt-3 block h-1 overflow-hidden rounded-full bg-current/15">
              <span
                className="block h-full rounded-full bg-current"
                style={{ width: `${track.played * 100}%` }}
              />
            </span>
            <p className="mt-1.5 flex justify-between text-[11px] font-medium tabular-nums opacity-60">
              <span>{track.elapsed}</span>
              <span>{track.left}</span>
            </p>
          </div>
        </GlassCard>
      </div>
    </div>
  );
}
```

Over a still backdrop, like the wallpaper here, the card needs no `live`. Like every glass in the kit it repaints only when something changes, and draws nothing at rest. Turn `live` on only when content moves behind the card, as with the rows of colour above: the card then draws every frame while it is on screen.

The rim pulls what is behind towards the card's centre, by 6.5% of the viewport, fading over half the card, so type and colour stretch along the edge. The face then frosts the bent picture with a 5 px blur (`blur`) and covers it with a tint (`tint`): 70% white on a light page, 70% black on a dark one. A 1.3 px white line runs inside the rim; there is no colour split or light, only a soft drop shadow. Unlike a button, the card stays still under the pointer: it holds content, it does not swell. Size and padding come from your classes, and the glass takes the card's corners: `rounded-[28px]` by default, any `rounded-*` class to change them. Text on clear glass follows the tone the glass ends up (`data-vt-tone`), so it reads on light and dark backdrops. For a coloured card, use `material="tinted"` with a `bg-*` class.

## API reference

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | — | The card's content. |
| `material` | `'clear' \| 'tinted'` | `'clear'` | Clear bends what is behind; tinted uses the background colour. |
| `tint` | `number` | `0.7` | Clear glass only: how much tint covers the backdrop (0–1): white on a light page, black on a dark one. |
| `blur` | `number` | `5` | Clear glass only: how much the backdrop is frosted, in px like CSS `blur()`. `0` shows it sharp. |
| `live` | `boolean` | `false` | Repaint the backdrop every frame, for content that moves behind the card. |
| `className` | `string` | — | Size, padding, corners (`rounded-*`) and, for tinted glass, colour (`bg-*`). |
| `…rest` | `div attributes` | — | Passed to the `<div>` (`role`, `aria-label`, `onClick`…). |

## Accessibility

- A plain `<div>`: when the card stands for one thing, name it (`role="group"` with an `aria-label`).
- The glass canvas is `aria-hidden`; only your content is read.
- Content that moves behind it should respect reduced motion: the rows above stop.

Source: /docs/card
