Redact
Words scribbled out with the pen, shown when clicked and scribbled over again on the next.
The launch is on , and the code name is . Click to read them.
import { Redact } from "@/components/ui/redact";
export default function RedactDemo() {
return (
<p className="max-w-sm text-lg leading-relaxed text-ink-2">
The launch is on <Redact seed="redact-date">October 14th</Redact>, and the code name is{" "}
<Redact seed="redact-name">Blue Biro</Redact>. Click to read them.
</p>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ redact"use client";
import { useMemo, useState, type ReactNode } from "react";
import { cn } from "@/lib/utils";
import { penStyle, useInkBox, useInkSeed, usePen, type Pen } from "@/hooks/use-ink-box";
import { InkSvg, Stroke } from "@/lib/ink";
import { createRng, shadeFill } from "@/lib/ink-sketch";
/**
* Words scribbled out with the pen, the way you'd black out a line in a
* notebook. Clicking (or Enter, or Space) pulls the scribble back off and
* shows them; again, and they're scribbled over. Until then screen readers
* hear only that something is hidden, not what.
*/
function Redact({
children,
label = "Hidden text",
defaultRevealed = false,
revealed: revealedProp,
onRevealedChange,
className,
seed,
roughness,
weight,
speed,
}: Pick<Pen, "roughness" | "weight" | "speed"> & {
children: ReactNode;
/** What screen readers hear while it's hidden. */
label?: string;
defaultRevealed?: boolean;
revealed?: boolean;
onRevealedChange?: (revealed: boolean) => void;
className?: string;
seed?: string | number;
}) {
const pen = usePen({ roughness, weight, speed });
const s = useInkSeed(seed);
const [own, setOwn] = useState(defaultRevealed);
const revealed = revealedProp ?? own;
const [ref, [w, h]] = useInkBox([100, 26]);
const q = pen.roughness ?? 1;
// Shaded over twice, at two angles, so no word shows through the gaps.
const passes = useMemo(() => {
const r = createRng(s);
return [
shadeFill(s, w + 2, h * 0.62, { gap: 1.8, angle: -2 + (r() - 0.5) * 2 * q, overrun: 1.5 }),
shadeFill(s + 1, w + 2, h * 0.5, { gap: 2.4, angle: 3 + (r() - 0.5) * 2 * q, overrun: 1 }),
];
}, [s, w, h, q]);
const toggle = () => {
if (revealedProp === undefined) setOwn(!revealed);
onRevealedChange?.(!revealed);
};
return (
<button
type="button"
data-slot="redact"
// data-checked while hidden: the scribble's state strokes stay drawn.
data-checked={revealed ? undefined : ""}
aria-pressed={revealed}
className={cn(
"relative inline cursor-pointer rounded-[2px] text-inherit outline-none [font:inherit]",
"focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:outline-offset-2 focus-visible:outline-ring",
className,
)}
style={penStyle(pen)}
onClick={toggle}
>
<InkSvg ref={ref} box={[-3, 0, w + 6, h]} stretch className="inset-y-0 -left-[3px] h-full w-[calc(100%+6px)] text-ink">
<g transform={`translate(-1 ${h * 0.2})`}>
<Stroke d={passes[0]} draw="checked" duration={Math.min(700, 260 + w * 3)} width={2} />
</g>
<g transform={`translate(-1 ${h * 0.26})`}>
<Stroke d={passes[1]} draw="checked" delay={80} duration={Math.min(600, 220 + w * 2.4)} width={1.6} opacity={0.85} />
</g>
</InkSvg>
{!revealed && <span className="sr-only">{label}, press to show</span>}
<span aria-hidden={!revealed} className={cn("transition-opacity duration-(--dur-state)", revealed ? "opacity-100 delay-150" : "opacity-0")}>
{children}
</span>
</button>
);
}
export { Redact };Usage
import { Redact } from "@/components/ui/redact"
<p>The code name is <Redact>Blue Biro</Redact>.</p>Examples
Reveal them all
Control revealed and the scribbles come off together; clicking one still reports through onRevealedChange.
- Codename
- Launch
- Budget
"use client";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Redact } from "@/components/ui/redact";
const lines = [
["Codename", "Blue Biro"],
["Launch", "October 14th"],
["Budget", "$1,200"],
] as const;
export default function RedactControlled() {
const [revealed, setRevealed] = useState(false);
return (
<div className="flex w-full max-w-sm flex-col gap-5">
<dl className="flex flex-col gap-2 text-lg text-ink-2">
{lines.map(([term, value]) => (
<div key={term} className="flex gap-3">
<dt className="w-24 text-ink-3">{term}</dt>
<dd>
<Redact revealed={revealed} onRevealedChange={setRevealed} label={`${term}, hidden`} seed={`rc-${term}`}>
{value}
</Redact>
</dd>
</div>
))}
</dl>
<Button variant="outline" size="sm" className="w-fit" onClick={() => setRevealed((r) => !r)} seed="rc-toggle">
{revealed ? "Cover them up" : "Show everything"}
</Button>
</div>
);
}In a letter
Names, places and sums scribbled over; click each to read it, and again to cover it.
Dear ,
We write about the project at . The budget stands at and the first test is set for .
Please tell no one. Click a bar to read it, and again to cover it.
import { Redact } from "@/components/ui/redact";
export default function RedactLetter() {
return (
<div className="flex w-full max-w-md flex-col gap-3 text-lg leading-relaxed text-ink-2">
<p>
Dear <Redact seed="rl-name">Dr Hopper</Redact>,
</p>
<p>
We write about the project at <Redact seed="rl-place">Harvard</Redact>. The budget stands at <Redact seed="rl-sum">£48,000</Redact> and the first test is
set for <Redact seed="rl-date">March 3rd</Redact>.
</p>
<p>Please tell no one. Click a bar to read it, and again to cover it.</p>
</div>
);
}Secrets in a table
Keys that stay hidden until asked for, each with its own screen-reader label.
| Key | Secret | Status |
|---|---|---|
| Production | Active | |
| Staging | Active | |
| Old laptop | Revoked |
import { Redact } from "@/components/ui/redact";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
const keys = [
["Production", "sk_live_51Hx9QeK2", "Active"],
["Staging", "sk_test_4eC39HqLy", "Active"],
["Old laptop", "sk_live_7dYpm0Zt", "Revoked"],
];
export default function RedactTable() {
return (
<Table>
<TableHeader>
<TableRow>
<TableHead>Key</TableHead>
<TableHead>Secret</TableHead>
<TableHead className="text-right">Status</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{keys.map(([name, secret, status]) => (
<TableRow key={name}>
<TableCell className="font-bold">{name}</TableCell>
<TableCell className="font-mono text-sm">
<Redact label={`${name} secret`} seed={`rt-${name}`}>
{secret}
</Redact>
</TableCell>
<TableCell className="text-right text-ink-2">{status}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
);
}API reference
- labeldefault
"Hidden text" stringWhat screen readers hear while it's hidden; they don't get the words until it's shown.
- revealed / defaultRevealeddefault
false booleanShown or scribbled over, controlled or not.
- onRevealedChange
(revealed: boolean) => voidCalled as it's clicked.
- seed
string | numberPins the scribble.
Pen settings
The drawing also takes roughness, weight and speed, as props or from the nearest InkProvider. What each one does.