Components

Scratch Reveal

Redacted text you scratch away with your cursor or finger to reveal what's underneath.

Loading…

Features

  • Works inline: wrap any words in a sentence and they become a redaction bar.
  • Click and drag to scratch, or rub with a finger on touch screens. A fast stroke keeps going even if it slips off the bar.
  • Scratches are kept when the text resizes or reflows, or when coverColor changes.
  • The brush has a ragged, grainy edge, and fast strokes are filled in so they leave a continuous scratch.
  • Behaves like a real scratch card: whatever you leave unscratched stays covered. Nothing fades away on its own.
  • onReveal fires once enough of the bar has been scratched off (threshold), so you can react without changing the bar.
  • The bar takes the current text color by default, so it follows your theme. Set coverColor to override it.
  • Hidden before hydration, so the words never flash on load.
  • Accessible: the bar can be reached with Tab, and Enter or Space clears it completely, since keyboard users can't scratch.
  • A single file with no dependencies.

Installation

pnpm dlx shadcn@latest add @sans/scratch-reveal

Usage

import { ScratchReveal } from "@/components/ui/scratch-reveal"
<p>
  My secret skill is <ScratchReveal>baking sourdough</ScratchReveal>.
</p>

Examples

Custom cover color

<ScratchReveal coverColor="#f43f5e">a surprise</ScratchReveal>

Bigger brush

A wider brush clears more with each stroke, and a lower threshold fires onReveal sooner.

<ScratchReveal brushSize={18} threshold={0.35}>
  the answer
</ScratchReveal>

Reacting to the reveal

<ScratchReveal onReveal={() => confetti()}>you won</ScratchReveal>

API reference

ScratchReveal

Prop

Type

References

  • Classified documents with redacted lines, and lottery scratch cards

Was this helpful?