Content hidden under a scratch-off layer, like a lottery ticket or a coupon. The layer is painted on a 2D canvas and erased with a mouse, pen or finger. The content is in the HTML from the server but stays inert and hidden from screen readers until it is revealed, and a keyboard button reveals it without scratching.
Preview
Scratched: 0%
Installation
Usage
import { ScratchCard } from "@/components/ui/scratch-card"
export default function Example() {
return (
<ScratchCard className="w-72" onReveal={() => console.log("revealed")}>
<div className="p-6 text-center">
<p className="text-4xl font-bold">-40%</p>
<code>GOOSE40</code>
</div>
</ScratchCard>
)
}Examples
Finishes
foil follows your shadcn tokens. silver, gold and holographic are decorative palettes. Here the layer comes back two seconds after a reveal (resetDelay).
Foil
Silver
Gold
Holographic
<ScratchCard finish="gold" resetDelay={2000}>…</ScratchCard>
{/* "foil" | "silver" | "gold" | "holographic" */}Hover mode
With trigger="hover" a moving mouse erases without pressing. Touch screens always drag.
<ScratchCard trigger="hover" brushSize={32}>…</ScratchCard>Custom content and colors
Any markup goes inside. The layer colors are CSS variables, so a card can override them through style.
<ScratchCard
finish="gold"
style={{ "--scratch-text": "oklch(0.3 0.1 60)" } as React.CSSProperties}
>
<PartyPopper className="size-10" />
<p>You won a free month</p>
</ScratchCard>Imperative reveal and reset
A ref exposes reveal() and reset().
const ref = React.useRef<ScratchCardHandle>(null)
<ScratchCard ref={ref}>…</ScratchCard>
<Button onClick={() => ref.current?.reveal()}>Reveal</Button>
<Button onClick={() => ref.current?.reset()}>Reset</Button>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| children | ReactNode | - | Content under the layer (required) |
| finish | "foil" | "silver" | "gold" | "holographic" | "foil" | Layer look. foil uses shadcn tokens, the rest are decorative palettes |
| label | string | "Scratch to reveal" | Text on the layer. An empty string hides it |
| revealLabel | string | "Reveal" | Text and accessible name of the keyboard reveal button |
| brushSize | number | 24 | Eraser diameter in CSS pixels |
| trigger | "press" | "hover" | "press" | press erases while a pointer is down, hover also erases under a moving mouse |
| threshold | number | 0.6 | Erased share (0 to 1) at which the rest of the layer fades out |
| shimmer | boolean | true | Glint across the layer until the first scratch |
| flakes | boolean | true | Small flakes that fly off the eraser |
| resetDelay | number | 0 | Milliseconds after a reveal before the layer returns. 0 keeps it revealed |
| onScratch | (progress: number) => void | - | Erased share (0 to 1), called at most every ~120 ms |
| onReveal | () => void | - | Called once when the content is revealed |
| ref | Ref<ScratchCardHandle> | - | Handle with reveal() and reset() |
| className | string | - | Extra classes. The card takes its size from the content; set a height or width here |
CSS variables
--scratch-1 to --scratch-4 are the gradient stops, --scratch-text is the label and grain color, --scratch-shine is the shimmer and highlight color.
Accessibility
- Until the reveal the content is inert and aria-hidden, so Tab and screen readers skip it.
- A Reveal button is reachable with Tab, appears on focus and on hover, and reveals on Enter or Space. Focus then moves to the content.
- The layer, the shimmer and the flakes are hidden for prefers-reduced-motion: reduce (the shimmer and the fade), and flakes are not spawned.
- The layer uses touch-action: none, so a touch that starts on it scratches instead of scrolling the page.
Notes
- Resizing keeps the erased shape and scales it to the new size.
- Layer colors are read from CSS when it is painted. After a theme switch the layer keeps the old colors until it is reset.
- Callbacks are plain functions, so pass them from a client component.