35
Componentregistry:componentClassic collection

Scratch Card

A scratch-off card over any content. Drag to scrape away brushed foil with a ragged edge and leftover specks, while flakes tumble off and fall. Past a threshold the rest fades away. Silver, gold, copper or any hex, an optional pressed-in label, progress callbacks, and Enter to reveal from the keyboard.

Preview

Love Coupon

this entitles you to

Drag across the foil to scratch it off, or press Enter to reveal
Scratch here
Coupon
Foil
Brush28 px
Clears at60%

Scratched0%

Installation

$ npx shadcn@latest add @creative-ui/scratch-card

On an older shadcn CLI, use the URL: npx shadcn@latest add https://ui.krutikraut.com/r/scratch-card.json

Usage

Example.tsx
import { ScratchCard } from "@/components/scratch-card"

export function Coupon() {
  return (
    <ScratchCard className="h-20 w-64 rounded-md" onReveal={() => track("coupon_revealed")}>
      <div className="grid size-full place-items-center bg-muted font-serif italic">
        Breakfast in bed
      </div>
    </ScratchCard>
  )
}

// Gold foil with a label, a wider brush, and the rest falling away at 40%.
<ScratchCard foil="gold" foilLabel="Scratch here" brushSize={36} threshold={0.4} onProgress={setProgress}>
  <Prize />
</ScratchCard>

// Controlled. Remount with a new key to lay fresh foil over a half-scratched card.
const [revealed, setRevealed] = React.useState(false)

<ScratchCard key={round} revealed={revealed} onRevealedChange={setRevealed}>
  <Prize />
</ScratchCard>

Props

PropTypeDefaultDescription
children*React.ReactNode—What sits under the foil. The card takes its size from this, or from className. It is inert and hidden from screen readers until revealed.
foil"silver" | "gold" | "copper" | string"silver"A named foil or any #rrggbb hex. The sheen, brushing and flakes are mixed from it.
brushSizenumber28Width of the scratch in px. The cursor ring matches it, up to 60 px.
thresholdnumber0.6Share of foil, from 0 to 1, that has to be scratched before the rest fades away.
revealedboolean—Controlled revealed state. Pair with onRevealedChange. Setting it back to false lays fresh foil.
defaultRevealedbooleanfalseInitial state when uncontrolled.
onRevealedChange(revealed: boolean) => void—Called with true when the card is revealed by scratching or the keyboard.
onReveal() => void—Called once when the card is revealed.
onProgress(progress: number) => void—Called as the foil comes off, with the scratched share from 0 to 1, at most once per percent.
foilLabelstring—Text pressed into the foil, set in capitals.
flakesbooleantrueShed small flakes of foil that tumble and fall while scratching. Off with reduced motion.
labelstring"Scratch to reveal"Accessible name of the foil, which is a button.
hintstring"Drag across the foil to scratch it off, or press Enter to reveal"Instructions read by screen readers through aria-describedby.
revealedLabelstring"Revealed"Announced through a polite live region once revealed.
seednumber3Seed for the foil's grain and the ragged edge of the scratch. Give each card on a page its own.
hapticsbooleantrueBuzz the device on reveal where the Vibration API exists (most Android browsers).

Also accepts every div prop, which is spread onto the root element.

Also exported

scratchCardFoils
The three named foils as hex, for building your own picker.

Notes

  • Enter or Space on the focused foil reveals the card at once, and focus moves to what was underneath.
  • The foil sets touch-action: none, so a finger on it scratches instead of scrolling the page. Keep cards small enough on mobile that there's room around them to scroll.
  • Flakes fly up to 32 px past the sides and 96 px below the card. A parent with overflow: hidden clips them.
  • Scratches are stored relative to the card's size, so a resize redraws the foil and replays them.
  • Until the canvas paints, the foil is a solid block of its middle colour, so the prize never flashes on load. The prize is still in the HTML, so don't hide anything secret under it: decide the prize on the server only once it's revealed.
  • The colour must be hex, because the foil is mixed from it in JavaScript. CSS variables and named colours fall back to silver.

Dependencies

shadcn/utils