Dot Board button
A framed button that spells its label in bright dots on a board of dim ones, with a solid square on each corner. Use it for one main action.
npx shadcn@latest add https://impeccable-ui.com/r/dot-board-button.jsonRun it in a project set up with shadcn. No other packages needed. Or open and copy the file into components/ui/dot-board-button.tsx.
Prompts
Paste one into Claude Code, Cursor or any AI coding tool. Install adds this exact button and asks only where it goes. Blend asks three short questions (where, the dot and fill colors, and what the label says after a click), then fits it to your product without changing how it moves.
# Dot Board button
A 1-file React button from Impeccable. Add it to this project so it works and looks exactly like the source below, in the one place I choose, and change nothing else.
A square button drawn like a small light board: its face is a grid of tiny round dots, all dimmed, and the dots that spell the label are lit bright. A thin straight frame runs round it with a solid square on each of its four corners. The label is written in column by column when it first shows, the dim dots sink on hover, a click sends a band of light across the dots, and a new label switches the old one off and writes itself in.
## Before you build: one question
Look through this project first: its pages, its hero and header buttons, its "Get started", "Sign up", "Download" or "Copy" buttons. Then ask me this in one message and wait for my answer:
- **Where should it go?** Suggest the 1–3 buttons it could replace or the spots it fits best (for example a hero's main action or a "Copy command" button), with each one's label. Keep that button's label, click handler and form wiring.
*Why: a dot board reads like a small sign, so it belongs on one action people should notice, with a short label of two or three words.*
Ask nothing else. If I say *skip*, use the best suggestion.
## Keep it as it is
The square board of dimmed dots with the label lit in dot letters, the straight frame with its four solid corner squares, the label being written in column by column, the dim dots sinking on hover, the band of light on click and the quick switch to a new label are the design. Keep them exactly as in the source, with their sizes and timing. If something doesn't fit my project, tell me instead of changing it.
- Add only the component file and the one place I picked. If it replaces an existing button, keep that button's label, click handler and form wiring. Don't add packages, change other files, or restyle anything.
- Don't add the demo parts from the Impeccable website: the dotted card behind the button, its "Get Impeccable" and "Welcome in" labels and the timer that switches back.
## Fastest path
If this project has a `components.json` (shadcn), run `npx shadcn@latest add https://impeccable-ui.com/r/dot-board-button.json`.
If the command succeeds, only fix import paths to match this project and skip ahead to **Check**. If it fails or there is no `components.json`, build it by hand as below.
## Build it
- Stack: React 18 or 19 with TypeScript. No other packages: the dots are drawn on one small canvas, the letters are drawn in the file itself (7 dots tall), and the frame is plain CSS.
- JavaScript project? Name the file `dot-board-button.jsx` and remove the type annotations; nothing else changes.
- Create this file, in this project's usual place for components:
- `components/ui/dot-board-button.tsx`
- Keep these exports and their names: `DotBoardButton`, `DotBoardButtonProps`.
- If this project has no `@/` import alias, use a relative import to the file instead.
- Keep `"use client"` at the top in Next.js App Router projects. In other setups it is harmless.
- Keep the tuning values at the top exactly as written: `PITCH`, `DOT`, `INSET`, `PAD_COLS`, `HEIGHT`, `DIM`, `DIM_DARK`, `LIT`, `GLOW`, `WAKE_MS`, `WRITE_DELAY`, `WRITE_MS`, `COL_IN`, `OUT_MS`, `HOVER_IN`, `HOVER_OUT`, `HUSH`, `SPEED`, `LEAD`, `TAIL`, `BAND`, `MAX_BANDS`, `STEP_MS`, `GAP_COLS`, `HOLD_MS`, `FADE_COLS`, the `GLYPHS` letter drawings and the `CSS` block with its easing curves. They are the design and its timing: everything comes in slow and calm and leaves fast.
- Keep the fallbacks: with `prefers-reduced-motion: reduce` the label shows at once and a click only brightens the dim dots for a moment. Hover only works on devices with a real pointer, so nothing sticks after a tap; on a phone, pressing quiets the dots instead. Keyboard focus quiets them too and shows a soft ring. A disabled button fades and never reacts. Nothing is drawn while it is still, off screen or in a hidden tab.
## Use it
```tsx
"use client";
import { useState } from "react";
import { DotBoardButton } from "@/components/ui/dot-board-button";
export function GetStarted() {
const [sent, setSent] = useState(false);
return (
<DotBoardButton onClick={() => setSent(true)}>
{sent ? "Welcome in" : "Get Impeccable"}
</DotBoardButton>
);
}
```
- It is a real `<button>` and takes every button prop (`type`, `disabled`, `aria-*`). In a form, set `type="submit"`.
- The label is its children: plain text reads best. Two or three short words fit; a longer label scrolls. To show a result (like "Copied"), change the children and the board rewrites itself.
- `className` and `style` go on the button itself. It is 50px tall and sizes itself to the label. For a full-width button pass `style={{ width: "100%" }}`: the label stays centred. The corner squares reach 4px past the frame, so leave a little room inside a box with `overflow: hidden`.
## How the board works
Read this before you touch anything, so you know what the code does and why it must stay as it is.
1. **The board.** The button is 50px tall and as wide as its label needs (worked out from the letters, so it is the same on the server and in the browser). Its face is a grid of 2px round dots, 3px apart, kept 6px from the frame: 13 rows: 3 dim rows above and below the label and 8 dim columns either side of it, so the words have room to breathe. Every dot is dimmed (9% of the dot color on a light fill, 14% on a dark one).
2. **The letters.** The label is spelled with letters drawn 7 dots tall, one dark column between letters. Letters show as capitals, like a real light board, and accents are dropped ("é" shows as "E"); a character the file has no drawing for is traced from the browser's font. Screen readers hear the label as written, from a hidden text copy. Lit dots are 92% of the dot color; on a dark fill each one also gets a faint glow.
3. **The frame.** A 1px straight border in the dot color hugs the button's edge, with a solid 7px square centred on each corner. The corners and frame are crisp CSS, not drawn on the canvas.
4. **Waking up.** The first time it comes on screen, the dim dots fade up over 520ms, then the label is written in column by column, left to right, each column easing in over 320ms, about a second for the whole label.
5. **Hover.** On a real pointer the dim dots sink slowly (about 320ms) to 30% of their brightness and the lit dots go to full brightness, so only the label stays lit. The frame never moves. Leaving brings them back fast (about 90ms).
6. **Click.** A band of light rolls out across the dots from the spot you pressed at 0.32px per ms. Its front is soft (18px), so each dot lights up gently as it arrives, and its back is short (5px), so the dot goes out fast. Quick clicks overlap, up to three bands. A phone tap sends it from the finger, the keyboard from the middle.
7. **A new label.** When the label changes, the old words switch off in 150ms (fast, accelerating), then the new ones are written in column by column like the first time, while the button eases to its new width over 460ms.
8. **Too long to fit.** If the label is wider than the button (a narrow phone, a long label), it rests on its first letters for 1.6 seconds, then scrolls through one dot every 110ms with a 10 column gap, fading out over 4 columns at each side instead of being cut off.
9. **Timing.** Everything runs on time measured in milliseconds, so it is the same speed on slow phones and fast screens. The canvas loop runs only while something moves. Colors come from CSS variables and are read again when the theme changes, and without a `color` the dots pick near black or near white to read on the fill.
## Check
- It type-checks and the page renders with no console errors.
- At rest it is a square button with a straight frame and a solid square on each corner, its face a board of dim dots with the label lit in dot capitals. The first time it shows, the label is written in left to right in about a second.
- Hover: the dim dots sink slowly so only the label stays lit, and come back fast when the pointer leaves. The frame never moves. Click: a band of light rolls out across the dots from the spot you pressed. Changing the label switches the old one off fast and writes the new one in.
- It looks right in light and dark mode (check both): dark dots on a white board in light mode, light dots with a faint glow on a near black board in dark mode.
- On a phone, a tap sends the band from your finger, the dots don't stay quiet after the tap, and there is no sideways scroll.
- With `prefers-reduced-motion: reduce`, the label shows at once and a click only brightens the dots for a moment.
- Nothing else on the site looks or behaves differently.
- Finish with a short report: the files you added or changed, where it's placed, and every place where you had to do something differently from the source, and why.
## Source
The reference implementation. Copy it as-is, then apply only the changes listed above.
### `components/ui/dot-board-button.tsx`
```tsx
"use client";
/**
* Dot Board button, from Impeccable.
*
* A square button drawn like a small light board. Its face is a grid of tiny
* round dots: every dot is dimmed down, and the dots that spell the label are
* lit bright, so the words read like a sign made of lights. A thin straight
* frame runs round it with a solid square on each of its four corners.
*
* In light mode the lit dots are near black on white; in dark mode they are
* near white on black with a faint glow. Every color can be your own.
*
* - When it first comes into view, the dim dots wake up and the label is
* written in column by column, left to right.
* - Hover: the dim dots sink further into the dark, slowly, so only the
* label stays lit. The frame never moves.
* - Click: a soft band of light rolls out across the dots from the spot you
* pressed. On a phone, pressing quiets the dots the same way and the tap
* sends the band from your finger. The keyboard sends it from the middle.
* - Change the label (like "Save" to "Saved") and the old words switch off
* quickly, then the new ones are written in, column by column.
* - A label too long for the button scrolls through it slowly, one dot at a
* time, like a real light board.
*
* In slow and calm, out fast: everything that appears eases in, everything
* that leaves goes quickly.
*
* No packages: one canvas for the dots, drawn only while something moves.
*/
import { forwardRef, useEffect, useImperativeHandle, useRef, type ButtonHTMLAttributes, type CSSProperties, type ReactNode } from "react";
export type DotBoardButtonProps = ButtonHTMLAttributes<HTMLButtonElement> & {
/** The lit dots and the frame. Any CSS color, including var(--…). Default: near black on a light fill, near white on a dark one. */
color?: string;
/** The button's fill. Any CSS color, including var(--…). Default: white in light mode, near black in dark mode. */
background?: string;
};
/* ---------- tuning (the design: keep as written) ---------- */
const PITCH = 3; // px from one dot to the next
const DOT = 2; // px: the size of one dot
const INSET = 6; // px the dots keep from the frame
const PAD_COLS = 8; // dim columns on each side of the label, so the words have room to breathe
const HEIGHT = 50; // px: the button's height, room for 13 rows of dots (3 dim rows above and below the label)
const DIM = 0.09; // how bright an unlit dot is on a light fill
const DIM_DARK = 0.14; // and on a dark fill
const LIT = 0.92; // how bright a lit dot is at rest
const GLOW = 0.22; // the soft halo round each lit dot, on a dark fill only
const WAKE_MS = 520; // the dim dots wake up over this long when the button first shows
const WRITE_DELAY = 160; // ms after that the label starts being written
const WRITE_MS = 900; // the whole label is written in about this long, column by column
const COL_IN = 320; // ms each column takes to light up
const OUT_MS = 150; // ms the old label takes to switch off when the label changes
const HOVER_IN = 320; // ms: on hover the dim dots sink slowly...
const HOVER_OUT = 90; // ...and come back fast
const HUSH = 0.7; // how much of their brightness the dim dots lose on hover, so the label stands out alone
const SPEED = 0.32; // px per ms the click band rolls out
const LEAD = 18; // px: the band's soft front, so each dot it reaches lights up gently...
const TAIL = 5; // px: ...and its short back, so it switches off fast
const BAND = 0.6; // how much the band brightens the dim dots
const MAX_BANDS = 3; // quick clicks overlap, up to this many
const STEP_MS = 110; // ms per one dot step when a long label scrolls
const GAP_COLS = 10; // dim columns between the end of a scrolling label and its start again
const HOLD_MS = 1600; // ms a long label rests on its first letters before it starts to scroll
const FADE_COLS = 4; // a scrolling label fades out over this many columns at each side, never cut hard
/** Styles that can't be inline: frame, corners, hover, press, focus, disabled and reduced motion. */
const CSS = `
.idb { --idb-theme-bg: rgb(255 255 255); --idb-theme-ink: rgb(17 17 19); }
:is(.dark, [data-theme="dark"]) .idb { --idb-theme-bg: rgb(12 12 14); --idb-theme-ink: rgb(250 250 250); }
.idb { position: relative; display: inline-block; flex: none; box-sizing: border-box; width: var(--idb-w); max-width: 100%; height: ${HEIGHT}px; margin: 0; padding: 0; border: 0; border-radius: 0; vertical-align: middle; background: var(--idb-bg, var(--idb-theme-bg)); color: var(--idb-ink, var(--idb-auto-ink, var(--idb-theme-ink))); font: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; touch-action: manipulation; transition: width .46s cubic-bezier(.22,1,.36,1), scale .18s cubic-bezier(.23,1,.32,1); }
.idb-dots { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.idb-frame { position: absolute; inset: 0; border: 1px solid currentColor; pointer-events: none; }
.idb-corner { position: absolute; width: 7px; height: 7px; background: currentColor; }
.idb-corner:nth-child(1) { top: -4px; left: -4px; }
.idb-corner:nth-child(2) { top: -4px; right: -4px; }
.idb-corner:nth-child(3) { bottom: -4px; right: -4px; }
.idb-corner:nth-child(4) { bottom: -4px; left: -4px; }
.idb-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.idb:active:not(:disabled) { scale: .97; }
.idb:focus-visible { outline: 2px solid color-mix(in srgb, currentColor 40%, transparent); outline-offset: 9px; }
.idb:disabled { opacity: .45; cursor: not-allowed; }
@media (prefers-reduced-motion: reduce) { .idb { transition: none !important; } .idb:active:not(:disabled) { scale: 1; } }`;
/* ---------- the dot letters: 7 rows each, "#" is a lit dot ---------- */
const GLYPHS: Record<string, string> = {
A: ".###.|#...#|#...#|#####|#...#|#...#|#...#",
B: "####.|#...#|#...#|####.|#...#|#...#|####.",
C: ".###.|#...#|#....|#....|#....|#...#|.###.",
D: "####.|#...#|#...#|#...#|#...#|#...#|####.",
E: "#####|#....|#....|####.|#....|#....|#####",
F: "#####|#....|#....|####.|#....|#....|#....",
G: ".###.|#...#|#....|#.###|#...#|#...#|.####",
H: "#...#|#...#|#...#|#####|#...#|#...#|#...#",
I: "###|.#.|.#.|.#.|.#.|.#.|###",
J: "..###|...#.|...#.|...#.|...#.|#..#.|.##..",
K: "#...#|#..#.|#.#..|##...|#.#..|#..#.|#...#",
L: "#....|#....|#....|#....|#....|#....|#####",
M: "#...#|##.##|#.#.#|#.#.#|#...#|#...#|#...#",
N: "#...#|#...#|##..#|#.#.#|#..##|#...#|#...#",
O: ".###.|#...#|#...#|#...#|#...#|#...#|.###.",
P: "####.|#...#|#...#|####.|#....|#....|#....",
Q: ".###.|#...#|#...#|#...#|#.#.#|#..#.|.##.#",
R: "####.|#...#|#...#|####.|#.#..|#..#.|#...#",
S: ".####|#....|#....|.###.|....#|....#|####.",
T: "#####|..#..|..#..|..#..|..#..|..#..|..#..",
U: "#...#|#...#|#...#|#...#|#...#|#...#|.###.",
V: "#...#|#...#|#...#|#...#|#...#|.#.#.|..#..",
W: "#...#|#...#|#...#|#.#.#|#.#.#|#.#.#|.#.#.",
X: "#...#|#...#|.#.#.|..#..|.#.#.|#...#|#...#",
Y: "#...#|#...#|.#.#.|..#..|..#..|..#..|..#..",
Z: "#####|....#|...#.|..#..|.#...|#....|#####",
"0": ".###.|#...#|#..##|#.#.#|##..#|#...#|.###.",
"1": "..#..|.##..|..#..|..#..|..#..|..#..|.###.",
"2": ".###.|#...#|....#|...#.|..#..|.#...|#####",
"3": "####.|....#|....#|.###.|....#|....#|####.",
"4": "...#.|..##.|.#.#.|#..#.|#####|...#.|...#.",
"5": "#####|#....|####.|....#|....#|#...#|.###.",
"6": "..##.|.#...|#....|####.|#...#|#...#|.###.",
"7": "#####|....#|...#.|..#..|.#...|.#...|.#...",
"8": ".###.|#...#|#...#|.###.|#...#|#...#|.###.",
"9": ".###.|#...#|#...#|.####|....#|...#.|.##..",
" ": "...|...|...|...|...|...|...",
".": ".|.|.|.|.|.|#",
",": "..|..|..|..|..|.#|#.",
"!": "#|#|#|#|#|.|#",
"?": ".###.|#...#|....#|...#.|..#..|.....|..#..",
"'": "#|#|.|.|.|.|.",
"’": "#|#|.|.|.|.|.",
'"': "#.#|#.#|...|...|...|...|...",
":": ".|#|.|.|.|#|.",
";": "..|.#|..|..|..|.#|#.",
"-": "....|....|....|####|....|....|....",
"+": ".....|..#..|..#..|#####|..#..|..#..|.....",
"=": ".....|.....|#####|.....|#####|.....|.....",
"/": "....#|....#|...#.|..#..|.#...|#....|#....",
"(": "..#|.#.|#..|#..|#..|.#.|..#",
")": "#..|.#.|..#|..#|..#|.#.|#..",
"&": ".##..|#..#.|#.#..|.#...|#.#.#|#..#.|.##.#",
"@": ".###.|#...#|#.###|#.#.#|#.###|#....|.####",
"#": ".#.#.|.#.#.|#####|.#.#.|#####|.#.#.|.#.#.",
"%": "##..#|##..#|...#.|..#..|.#...|#..##|#..##",
"*": ".....|#.#.#|.###.|#####|.###.|#.#.#|.....",
$: "..#..|.####|#.#..|.###.|..#.#|####.|..#..",
_: ".....|.....|.....|.....|.....|.....|#####",
"→": ".....|..#..|...#.|#####|...#.|..#..|.....",
};
/** How many dot columns a letter takes. Letters the board has no drawing for take 5 and are traced from the browser's font. */
const glyphWidth = (ch: string) => (GLYPHS[ch] ? GLYPHS[ch].indexOf("|") : 5);
/** The label in the board's letters: capitals only, like a real light board, with accents dropped ("é" shows as "E"). */
const boardText = (s: string) =>
s.replace(/\s+/g, " ").trim().toUpperCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "").normalize("NFC");
/** How many dot columns the whole label takes, with one dark column between letters. */
function textColumns(text: string) {
const chars = Array.from(text);
return chars.reduce((n, ch) => n + glyphWidth(ch), 0) + Math.max(0, chars.length - 1);
}
/** The label's plain words, for screen readers and the width, from text or simple elements. */
function plainText(node: ReactNode): string {
if (node == null || typeof node === "boolean") return "";
if (typeof node === "string" || typeof node === "number") return String(node);
if (Array.isArray(node)) return node.map(plainText).join("");
if (typeof node === "object" && "props" in node) return plainText((node.props as { children?: ReactNode }).children);
return "";
}
/** Traces one letter the board has no drawing for from the browser's own font, into 5 by 7 dots. */
let tracer: CanvasRenderingContext2D | null = null;
function traceGlyph(ch: string) {
tracer ??= document.createElement("canvas").getContext("2d", { willReadFrequently: true });
const rows: string[] = [];
if (!tracer) return ".....|.....|.....|.....|.....|.....|.....";
const S = 4; // drawn 4 times larger, then each dot is the average of a 4 by 4 patch
tracer.canvas.width = 5 * S;
tracer.canvas.height = 7 * S;
tracer.clearRect(0, 0, 5 * S, 7 * S);
tracer.fillStyle = "#000";
tracer.font = `700 ${8 * S}px system-ui, sans-serif`;
tracer.textAlign = "center";
tracer.textBaseline = "alphabetic";
tracer.fillText(ch, 2.5 * S, 6.6 * S, 5 * S);
const data = tracer.getImageData(0, 0, 5 * S, 7 * S).data;
for (let y = 0; y < 7; y++) {
let row = "";
for (let x = 0; x < 5; x++) {
let sum = 0;
for (let v = 0; v < S; v++) for (let u = 0; u < S; u++) sum += data[((y * S + v) * 5 * S + x * S + u) * 4 + 3];
row += sum / (S * S * 255) > 0.35 ? "#" : ".";
}
rows.push(row);
}
return rows.join("|");
}
/** The label as a grid of lit (1) and dark (0) dots, 7 rows tall. */
function bitmap(text: string) {
const chars = Array.from(text);
const cols = textColumns(text);
const bits = new Uint8Array(cols * 7);
let x = 0;
chars.forEach((ch, k) => {
const rows = (GLYPHS[ch] ?? traceGlyph(ch)).split("|");
const gw = rows[0].length;
for (let y = 0; y < 7; y++) for (let i = 0; i < gw; i++) if (rows[y][i] === "#") bits[y * cols + x + i] = 1;
x += gw + (k < chars.length - 1 ? 1 : 0);
});
return { bits, cols };
}
/** True when a color reads as light, worked out by painting it, so any CSS color format works. */
let probe: CanvasRenderingContext2D | null = null;
function isLight(color: string) {
probe ??= document.createElement("canvas").getContext("2d", { willReadFrequently: true });
if (!probe) return false;
probe.clearRect(0, 0, 1, 1);
probe.fillStyle = "#000";
probe.fillStyle = color;
probe.fillRect(0, 0, 1, 1);
const [r, g, b] = probe.getImageData(0, 0, 1, 1).data;
const lin = (c: number) => ((c /= 255) <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4);
return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b) > 0.4;
}
const clamp = (v: number, a = 0, b = 1) => Math.min(b, Math.max(a, v));
const easeOut = (t: number) => 1 - (1 - t) ** 3; // arrives softly
const easeIn = (t: number) => t * t * t; // leaves quickly
type Layer = { bits: Uint8Array; cols: number; inAt: number; outAt: number; step: number };
type Band = { x: number; y: number; at: number; life: number };
/* ---------- the button ---------- */
export const DotBoardButton = forwardRef<HTMLButtonElement, DotBoardButtonProps>(function DotBoardButton(
{ color, background, type = "button", className, style, children, ...rest },
ref,
) {
const btnRef = useRef<HTMLButtonElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
const api = useRef({ wake: () => {}, setText: (_t: string) => {} });
useImperativeHandle(ref, () => btnRef.current!, []);
const words = plainText(children);
const text = boardText(words);
// The width the label needs, worked out from the letters alone, so it is the same on the server and in the browser.
const width = (textColumns(text) + PAD_COLS * 2 - 1) * PITCH + DOT + INSET * 2;
const disabled = !!rest.disabled;
const opts = useRef({ disabled });
opts.current = { disabled };
// A new label switches the old one off and writes the new one in.
useEffect(() => api.current.setText(text), [text]);
useEffect(() => api.current.wake(), [disabled, color, background]);
// With no color set, the dots pick dark or light to read on the fill, and check again when the theme changes.
useEffect(() => {
const btn = btnRef.current;
if (!btn) return;
const apply = () => {
btn.style.setProperty("--idb-auto-ink", isLight(getComputedStyle(btn).backgroundColor) ? "rgb(17 17 19)" : "rgb(250 250 250)");
api.current.wake();
};
apply();
const mo = new MutationObserver(apply);
mo.observe(document.documentElement, { attributes: true, attributeFilter: ["class", "style", "data-theme"] });
const mq = matchMedia("(prefers-color-scheme: dark)");
mq.addEventListener("change", apply);
return () => {
mo.disconnect();
mq.removeEventListener("change", apply);
};
}, [background]);
useEffect(() => {
const btn = btnRef.current, canvas = canvasRef.current;
if (!btn || !canvas) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;
const reduceMq = matchMedia("(prefers-reduced-motion: reduce)");
let reduced = reduceMq.matches;
let cw = 0, ch = 0, dpr = 1;
let raf = 0, last = 0, level = 0, onScreen = false;
let woke = -1; // when the dim dots woke up; -1 until the button is first on screen
let hover = false, pressed = false, focused = false;
let ink = "", glow = false, inkAt = -1e9;
let bands: Band[] = [];
let current = "";
let layers: Layer[] = [];
let lit = new Float32Array(0);
const makeLayer = (t: string, inAt: number): Layer => {
const { bits, cols } = bitmap(t);
return { bits, cols, inAt, outAt: Infinity, step: clamp(WRITE_MS / Math.max(1, cols), 6, 22) };
};
const local = (e: { clientX: number; clientY: number }) => {
const r = btn.getBoundingClientRect();
const sx = btn.offsetWidth ? r.width / btn.offsetWidth || 1 : 1;
const sy = btn.offsetHeight ? r.height / btn.offsetHeight || 1 : 1;
return { x: (e.clientX - r.left) / sx, y: (e.clientY - r.top) / sy };
};
/** How lit column gx of a label is while it is being written in (0 to 1). */
const written = (L: Layer, gx: number, now: number) => (reduced ? (now >= L.inAt ? 1 : 0) : easeOut(clamp((now - L.inAt - gx * L.step) / COL_IN)));
/** How much of a label is left while it switches off (1 to 0). */
const leaving = (L: Layer, now: number) => (now < L.outAt ? 1 : reduced ? 0 : 1 - easeIn(clamp((now - L.outAt) / OUT_MS)));
/** Draws one frame and says whether anything is still moving. */
const draw = (now: number) => {
// The button's own size (offsetWidth ignores scaling), so it also works inside a scaled-down preview.
const w = btn.offsetWidth, h = btn.offsetHeight;
if (!w || !h) return false;
const nd = Math.min(2, devicePixelRatio || 1);
if (w !== cw || h !== ch || nd !== dpr) {
cw = w; ch = h; dpr = nd;
canvas.width = Math.round(cw * dpr);
canvas.height = Math.round(ch * dpr);
}
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, cw, ch);
if (woke < 0) return false;
// The color is read from the page now and then, so theme colors follow light and dark mode.
if (now - inkAt > 250) {
ink = getComputedStyle(btn).color;
glow = isLight(ink);
inkAt = now;
}
const cols = Math.max(1, Math.floor((w - INSET * 2 - DOT) / PITCH) + 1);
const rows = Math.max(7, Math.floor((h - INSET * 2 - DOT) / PITCH) + 1);
const ox = (w - ((cols - 1) * PITCH + DOT)) / 2, oy = (h - ((rows - 1) * PITCH + DOT)) / 2;
const r0 = Math.floor((rows - 7) / 2);
if (lit.length !== cols * rows) lit = new Float32Array(cols * rows);
else lit.fill(0);
let moving = false;
// 1. The label: which dots are lit, and how much, while it is written in, switched off or scrolling.
for (const L of layers) {
const out = leaving(L, now);
if (out <= 0) continue;
if (out < 1) moving = true;
const fits = L.cols <= cols - 2;
let c0 = Math.floor((cols - L.cols) / 2), period = 0;
if (!fits) {
// Too long to fit: it scrolls through one dot at a time, starting from its first letter.
period = L.cols + GAP_COLS;
const since = now - (L.inAt + L.cols * L.step + COL_IN + HOLD_MS);
const s = reduced || since < 0 ? 0 : Math.floor(since / STEP_MS) % period;
c0 = FADE_COLS - s;
if (!reduced) moving = true;
}
for (let gx = 0; gx < L.cols; gx++) {
const f = written(L, gx, now) * out;
if (f < 1 && now < L.inAt + L.cols * L.step + COL_IN) moving = true;
if (f <= 0) continue;
for (let copy = 0; copy < (fits ? 1 : 2); copy++) {
const c = c0 + gx + copy * period;
if (c < 0 || c >= cols) continue;
// A scrolling label fades in and out at the sides instead of being cut off.
const fc = fits ? f : f * clamp((c + 0.5) / FADE_COLS) * clamp((cols - c - 0.5) / FADE_COLS);
if (fc <= 0) continue;
for (let gy = 0; gy < 7; gy++) {
if (!L.bits[gy * L.cols + gx]) continue;
const k = (r0 + gy) * cols + c;
if (fc > lit[k]) lit[k] = fc;
}
}
}
}
// 2. The dots: dim everywhere, brighter near the pointer and where a click band passes, bright where the label is.
const wake = reduced ? 1 : easeOut(clamp((now - woke) / WAKE_MS));
if (wake < 1) moving = true;
const dim = (glow ? DIM_DARK : DIM) * wake;
const litA = LIT + (1 - LIT) * level;
const hushed = dim * (1 - HUSH * level); // on hover the dim dots sink, so only the label is lit
const R = DOT / 2;
ctx.fillStyle = ink;
for (let j = 0; j < rows; j++) {
const y = oy + j * PITCH + R;
for (let i = 0; i < cols; i++) {
const x = ox + i * PITCH + R;
let a = hushed;
for (const b of bands) {
const age = now - b.at;
let v = 0.6;
if (!reduced) {
const d = Math.hypot(x - b.x, y - b.y) - SPEED * age; // > 0: the band hasn't reached this dot yet
v = d > 0 ? Math.exp(-((d / LEAD) ** 2)) : Math.exp(-((d / TAIL) ** 2));
}
a += BAND * v * (1 - clamp(age / b.life)) * wake;
}
const l = lit[j * cols + i];
if (l > 0) a = Math.max(a, l * litA);
if (a < 0.01) continue;
ctx.globalAlpha = Math.min(1, a);
ctx.beginPath();
ctx.arc(x, y, R, 0, Math.PI * 2);
ctx.fill();
}
}
// 3. On a dark fill the lit dots glow softly, like real lights.
if (glow) {
for (let j = 0; j < rows; j++) {
for (let i = 0; i < cols; i++) {
const l = lit[j * cols + i];
if (l <= 0) continue;
ctx.globalAlpha = GLOW * l * (0.75 + 0.25 * level);
ctx.beginPath();
ctx.arc(ox + i * PITCH + R, oy + j * PITCH + R, R * 2.1, 0, Math.PI * 2);
ctx.fill();
}
}
}
ctx.globalAlpha = 1;
return moving || bands.length > 0;
};
const frame = (now: number) => {
raf = 0;
const dt = Math.min(50, last ? now - last : 16);
last = now;
const target = !opts.current.disabled && (hover || pressed || focused) ? 1 : 0;
level += (target - level) * (1 - Math.exp(-dt / (target > level ? HOVER_IN : HOVER_OUT)));
if (target ? level > 0.995 : level < 0.005) level = target;
bands = bands.filter((b) => now - b.at < b.life);
layers = layers.filter((L) => leaving(L, now) > 0);
const moving = draw(now);
// The loop runs only while something moves, the button is on screen and the tab is visible.
if (onScreen && !document.hidden && (moving || level !== target)) raf = requestAnimationFrame(frame);
};
const wake = () => {
if (!raf && onScreen && !document.hidden) {
last = 0;
raf = requestAnimationFrame(frame);
}
};
const setText = (t: string) => {
if (t === current) return;
current = t;
const now = performance.now();
if (woke < 0) {
// Not shown yet: it is written in when the button first comes into view.
layers = [makeLayer(t, Infinity)];
return;
}
// The old label switches off fast, then the new one is written in.
for (const L of layers) if (L.outAt === Infinity) L.outAt = now;
layers.push(makeLayer(t, reduced ? now : now + OUT_MS));
inkAt = -1e9;
wake();
};
api.current = { wake: () => ((inkAt = -1e9), wake()), setText };
setText(text);
const startBand = (x: number, y: number) => {
if (opts.current.disabled || woke < 0) return;
const w = btn.offsetWidth, h = btn.offsetHeight;
const far = Math.max(Math.hypot(x, y), Math.hypot(w - x, y), Math.hypot(x, h - y), Math.hypot(w - x, h - y));
// With reduced motion nothing travels: the dim dots brighten once, all together, and fade.
bands.push({ x, y, at: performance.now(), life: reduced ? 420 : (far + LEAD * 2) / SPEED });
if (bands.length > MAX_BANDS) bands.shift();
wake();
};
const onEnter = (e: PointerEvent) => {
if (e.pointerType === "touch") return;
hover = true;
wake();
};
const onLeave = () => {
hover = false;
pressed = false;
wake();
};
const onDown = (e: PointerEvent) => {
if (e.button !== 0 || opts.current.disabled) return;
const at = local(e);
if (e.pointerType === "touch") {
pressed = true;
}
startBand(at.x, at.y);
};
const onUp = (e: PointerEvent) => {
if (e.pointerType !== "touch") return;
pressed = false;
wake();
};
// Keyboard focus quiets the dim dots too, like hover. A mouse click doesn't count.
const onFocus = () => {
focused = btn.matches(":focus-visible");
wake();
};
const onBlur = () => {
focused = false;
wake();
};
// Enter or Space on the keyboard: the band starts from the middle.
const onClick = (e: MouseEvent) => {
if (e.detail === 0) startBand(btn.offsetWidth / 2, btn.offsetHeight / 2);
};
const onVis = () => {
if (document.hidden) {
cancelAnimationFrame(raf);
raf = 0;
} else wake();
};
const onReduce = () => {
reduced = reduceMq.matches;
wake();
};
// The first time it comes on screen the dots wake up and the label is written in. Off screen, nothing is drawn.
const io = new IntersectionObserver(([entry]) => {
onScreen = entry.isIntersecting;
if (onScreen) {
if (woke < 0) {
woke = performance.now();
for (const L of layers) if (L.inAt === Infinity) L.inAt = reduced ? woke : woke + WRITE_DELAY;
}
wake();
} else {
cancelAnimationFrame(raf);
raf = 0;
}
});
io.observe(btn);
// A resize (a new label width, a full-width layout) redraws, with reduced motion too.
const ro = new ResizeObserver(() => wake());
ro.observe(btn);
btn.addEventListener("pointerenter", onEnter);
btn.addEventListener("focus", onFocus);
btn.addEventListener("blur", onBlur);
btn.addEventListener("pointerleave", onLeave);
btn.addEventListener("pointercancel", onLeave);
btn.addEventListener("pointerdown", onDown);
btn.addEventListener("pointerup", onUp);
btn.addEventListener("click", onClick);
document.addEventListener("visibilitychange", onVis);
reduceMq.addEventListener("change", onReduce);
return () => {
cancelAnimationFrame(raf);
raf = 0;
api.current = { wake: () => {}, setText: () => {} };
io.disconnect();
ro.disconnect();
reduceMq.removeEventListener("change", onReduce);
btn.removeEventListener("pointerenter", onEnter);
btn.removeEventListener("focus", onFocus);
btn.removeEventListener("blur", onBlur);
btn.removeEventListener("pointerleave", onLeave);
btn.removeEventListener("pointercancel", onLeave);
btn.removeEventListener("pointerdown", onDown);
btn.removeEventListener("pointerup", onUp);
btn.removeEventListener("click", onClick);
document.removeEventListener("visibilitychange", onVis);
};
// The label is handed over through setText, so the loop is set up once.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<>
<style>{CSS}</style>
<button
ref={btnRef}
type={type}
className={className ? `idb ${className}` : "idb"}
style={
{
"--idb-w": `${width}px`,
...(color ? { "--idb-ink": color } : null),
...(background ? { "--idb-bg": background } : null),
...style,
} as CSSProperties
}
{...rest}
>
<canvas ref={canvasRef} className="idb-dots" width={0} height={0} aria-hidden="true" />
<span className="idb-frame" aria-hidden="true">
<span className="idb-corner" />
<span className="idb-corner" />
<span className="idb-corner" />
<span className="idb-corner" />
</span>
<span className="idb-label">{words}</span>
</button>
</>
);
});
```
Usage
Use it like any button. Everything has a default. Give it a short label: two or three words read best.
"use client";
import { useState } from "react";
import { DotBoardButton } from "@/components/ui/dot-board-button";
export function InstallAction() {
const [copied, setCopied] = useState(false);
return (
<DotBoardButton
color="var(--foreground)" // optional: the lit dots and the frame
background="var(--background)" // optional: the fill
onClick={() => {
navigator.clipboard.writeText("npx shadcn@latest add https://impeccable-ui.com/r/dot-board-button.json");
setCopied(true);
}}
>
{copied ? "Copied" : "Copy command"}
</DotBoardButton>
);
}The corner squares reach 4px past the frame, so leave a little room round the button inside a box with overflow: hidden.
How it moves
A square button drawn like a small light board: a grid of tiny round dots, all dimmed, with the dots that spell the label lit bright. A thin straight frame runs round it with a solid square on each corner.
When it first comes into view, the dim dots wake up and the label is written in column by column, left to right, in about a second. Hover it and the dim dots sink slowly into the dark, to under a third of their brightness, so only the label stays lit; the frame never moves. Move away and they come back quickly. Click it and a band of light rolls out across the dots from the spot you pressed: each dot lights up gently as the band reaches it and goes out fast once it has passed. Quick clicks overlap, up to three bands at once. Change the label and the old words switch off in a blink, then the new ones are written in, while the button eases to its new width. A label too long for the button scrolls through it one dot at a time, like a real light board. In light mode the lit dots are near black on white; in dark mode they are near white on black with a faint glow. On a phone, pressing quiets the dots the same way and the tap sends the band from your finger. The keyboard sends it from the middle and its focus quiets the dots too. With reduced motion, the label shows at once and a click brightens the dim dots for a moment without anything travelling. Nothing is drawn while it is still or off screen.
Props
| Prop | Default | What it does |
|---|---|---|
children | — | The label, spelled in dots. Letters show as capitals, like a real light board, and screen readers hear your words as written. A label too long for the button scrolls through it slowly. Change it and the old words switch off as the new ones are written in. |
color | near black / near white | The lit dots and the frame with its four corner squares. Any CSS color, including var(--…). By default it picks near black on a light fill and near white on a dark one. The dim dots are this color, faded. |
background | white / near black | The fill. By default white in light mode and near black in dark mode (it follows a .dark class or data-theme="dark" on the page). |
onClick, type, disabled, … | type="button" | Everything a normal button takes. Disabled fades it and stops the hover and the click band. |
className / style | — | On the button itself. It is 50px tall and as wide as its label needs, with 8 dim columns either side of the words. Pass style={{ width: "100%" }} for a full-width button: the label stays centred. |