Skip to content
Browse documentation

One call before you clear a field: its text lifts away left to right as fine dust in its own colour, in under half a second.

Used forPrompt fieldsSearchComposersForms

Install with the shadcn CLI

$ npx shadcn@latest add https://hairlineui.com/r/clear-to-dust.json

Registered the namespace? Then it’s shadcn add @hairline/clear-to-dust. Set it up

Templates

Need the whole page?

61 Next.js landing pages, each designed from scratch. Try any of them live; one is $39, all of them $159, and 6 are free.

All 61 templates

Where it’s used

  • Prompt fields

    Clear an image or chat prompt with its own clear button.

  • Search

    Wipe a search query when the clear button is pressed.

  • Composers

    Clear a message box after sending, or on Escape.

  • Forms

    Reset a field without a jarring blank.

What it handles for you

A clear button usually blanks the field in one frame, with nothing to say what was there or that it went on purpose.

Instead of wiring uptext clear animationinput reset animationparticle text effectframer-motion text exit
  1. One call, your state

    clearToDust(field) only draws; you clear the value yourself, on the same line, the way you already do.

  2. Exactly where it was

    The text is drawn with the field’s own font, padding, alignment and scroll, so the dust starts where the letters were.

  3. Textareas too

    Lines are wrapped to the field’s width and follow its scroll, so a long prompt clears line by line.

  4. Never blinks

    The first frame is drawn at the call, so the text can’t vanish for a frame when your state clears.

  5. Short on purpose

    About 0.45s and a few dozen pixels, the budget for something done many times a day.

  6. Passwords stay dots

    A password field turns into dust as its dots, never its text.

Overview

Clearing a prompt is something people do many times a day, and it usually just blanks. Call clearToDust on the input or textarea right before you clear its value: it takes a snapshot of the text exactly where it sits (font, padding, scroll position, wrapped lines), so your state can clear on the very next line, and then the letters lift away from left to right as fine dust in the text’s own colour. It is short on purpose, about 0.45s and a few dozen pixels of travel: you notice it the first time and stop noticing it after that.

States

clearing
The text is drawn over the field and a front crosses it in the first 40% of the run; behind it each sampled point lifts 8 to 34px up and 12 to 57px right as it fades. 450ms by default.
cleared
The last dust fades and the layer is removed; the promise resolves. The field shows whatever your state now holds (its placeholder).
reduced motion
Nothing is drawn; the field simply clears.

Usage

usage.tsx
import { clearToDust } from "@/components/clear-to-dust";<button  aria-label="Clear"  onClick={() => {    clearToDust(inputRef.current);    setPrompt("");  }}/>

On Escape and on send

Clear the composer with dust when someone presses Escape, and after a message is sent.

example.tsx
"use client";import * as React from "react";import { clearToDust } from "@/components/clear-to-dust";export function Composer({ onSend }: { onSend: (text: string) => Promise<void> }) {  const field = React.useRef<HTMLTextAreaElement>(null);  const [text, setText] = React.useState("");  const clear = () => {    clearToDust(field.current);    setText("");  };  return (    <textarea      ref={field}      value={text}      onChange={(e) => setText(e.target.value)}      onKeyDown={async (e) => {        if (e.key === "Escape") clear();        if (e.key === "Enter" && !e.shiftKey) {          e.preventDefault();          await onSend(text);          clear();        }      }}    />  );}

Props

clearToDust(field, options?)
Promise<void>
Blows the field’s current text away. Call it right before you clear the value; it resolves when the dust has gone.
options.duration= 450
number
Milliseconds for the whole clear.

Accessibility and motion

  • It never changes the field’s value or focus; your state stays the one source of truth.
  • The dust is drawn on one canvas in a layer over the page, removed when it ends, and follows the field if the page scrolls meanwhile.
  • Text is sampled every 1.5 CSS pixels at device resolution, so thin and thick fonts both read as their own letters.
  • Under reduced motion it draws nothing and resolves at once.

Requirements

React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

Source

components/clear-to-dust.tsx
"use client";/* ───────────────────────────────────────────────────────── * CLEAR TO DUST: a field's text blows away when you clear it * * One call before you clear an input or a textarea: *   clearToDust(inputRef.current); setPrompt(""); * It takes a snapshot of the text exactly where it sits (font, * padding, scroll, wrapping), so your state can clear in the * same line; the letters then lift away left to right as fine * dust in the text's own colour. Many times a day, so it is * short: about 0.45s, and it travels a few dozen pixels. * ───────────────────────────────────────────────────────── */export interface ClearToDustOptions {  /** Milliseconds for the whole clear. */  duration?: number;}type Dot = { x: number; y: number; vx: number; vy: number; a: number; s: number; delay: number };const PAD = 48;let layer: HTMLDivElement | null = null;function topLayer() {  if (layer && layer.isConnected) return layer;  layer = document.createElement("div");  layer.setAttribute("aria-hidden", "true");  layer.style.cssText = "position:fixed;inset:0;pointer-events:none;z-index:2147483000;contain:layout style;overflow:clip;";  document.body.appendChild(layer);  return layer;}/** The lines a textarea shows: its own line breaks, then words wrapped to its width. */function wrap(ctx: CanvasRenderingContext2D, text: string, width: number) {  const lines: string[] = [];  for (const paragraph of text.split("\n")) {    let line = "";    for (const word of paragraph.split(/(\s+)/)) {      const next = line + word;      if (line && ctx.measureText(next).width > width) {        lines.push(line);        line = word.trimStart();      } else line = next;    }    lines.push(line);  }  return lines;}/** Blows the field's current text away as dust. Resolves when the dust has gone; clear the field's value yourself, straight after calling it. */export function clearToDust(field: HTMLInputElement | HTMLTextAreaElement | null | undefined, options: ClearToDustOptions = {}): Promise<void> {  if (!field || !field.value || typeof window === "undefined") return Promise.resolve();  if (window.matchMedia?.("(prefers-reduced-motion: reduce)").matches) return Promise.resolve();  const text = field.value;  const rect = field.getBoundingClientRect();  const cs = getComputedStyle(field);  const dpr = Math.min(window.devicePixelRatio || 1, 2);  const w = rect.width;  const h = rect.height;  const left = parseFloat(cs.paddingLeft) + parseFloat(cs.borderLeftWidth);  const right = parseFloat(cs.paddingRight) + parseFloat(cs.borderRightWidth);  const top = parseFloat(cs.paddingTop) + parseFloat(cs.borderTopWidth);  const bottom = parseFloat(cs.paddingBottom) + parseFloat(cs.borderBottomWidth);  const fontSize = parseFloat(cs.fontSize) || 14;  const lineHeight = parseFloat(cs.lineHeight) || fontSize * 1.4;  // The text as it sits in the field, drawn once at device resolution.  const glyphs = document.createElement("canvas");  glyphs.width = Math.max(1, Math.round(w * dpr));  glyphs.height = Math.max(1, Math.round(h * dpr));  const g = glyphs.getContext("2d", { willReadFrequently: true });  if (!g) return Promise.resolve();  g.scale(dpr, dpr);  g.font = `${cs.fontStyle} ${cs.fontWeight} ${cs.fontSize} ${cs.fontFamily}`;  if ("letterSpacing" in g && cs.letterSpacing !== "normal") (g as CanvasRenderingContext2D & { letterSpacing: string }).letterSpacing = cs.letterSpacing;  g.fillStyle = cs.color;  g.textBaseline = "middle";  g.beginPath();  g.rect(left, top, w - left - right, h - top - bottom);  g.clip();  const inner = w - left - right;  if (field instanceof HTMLTextAreaElement) {    wrap(g, text, inner).forEach((line, i) => g.fillText(line, left, top + lineHeight * (i + 0.5) - field.scrollTop));  } else {    const shown = field.type === "password" ? "•".repeat(text.length) : text;    const measured = g.measureText(shown).width;    const x = cs.textAlign === "center" ? left + (inner - measured) / 2 : cs.textAlign === "right" || cs.textAlign === "end" ? w - right - measured : left;    g.fillText(shown, x - field.scrollLeft, h / 2);  }  const data = g.getImageData(0, 0, glyphs.width, glyphs.height).data;  const dots: Dot[] = [];  const step = Math.max(1, Math.round(1.5 * dpr));  for (let y = 0; y < glyphs.height; y += step)    for (let x = 0; x < glyphs.width; x += step) {      const a = data[(y * glyphs.width + x) * 4 + 3];      if (a < 70) continue;      const nx = x / dpr / w;      dots.push({ x: x / dpr, y: y / dpr, vx: 0.4 + Math.random() * 1.5, vy: -0.4 - Math.random() * 1.3, a: a / 255, s: 1.1 + Math.random() * 0.9, delay: nx * 0.4 });    }  if (!dots.length) return Promise.resolve();  const canvas = document.createElement("canvas");  canvas.width = Math.round((w + PAD * 2) * dpr);  canvas.height = Math.round((h + PAD * 2) * dpr);  canvas.style.cssText = `position:absolute;left:0;top:0;width:${w + PAD * 2}px;height:${h + PAD * 2}px;transform:translate(${rect.left - PAD}px, ${rect.top - PAD}px);will-change:transform;`;  const ctx = canvas.getContext("2d");  if (!ctx) return Promise.resolve();  ctx.setTransform(dpr, 0, 0, dpr, 0, 0);  ctx.fillStyle = cs.color;  ctx.imageSmoothingQuality = "high";  topLayer().appendChild(canvas);  const duration = options.duration ?? 450;  const draw = (p: number) => {    ctx.clearRect(0, 0, w + PAD * 2, h + PAD * 2);    // Text the front hasn't reached yet stays solid, exactly as it was in the field.    const front = (p / 0.4) * w;    if (front < w) {      ctx.save();      ctx.beginPath();      ctx.rect(PAD + front, 0, w - front + PAD, h + PAD * 2);      ctx.clip();      ctx.drawImage(glyphs, PAD, PAD, w, h);      ctx.restore();    }    for (const d of dots) {      const life = (p - d.delay) / (1 - d.delay);      if (life <= 0 || life >= 1) continue;      const ease = 1 - Math.pow(1 - life, 2);      ctx.globalAlpha = d.a * (1 - life);      ctx.fillRect(PAD + d.x + d.vx * ease * 30, PAD + d.y + d.vy * ease * 20, d.s, d.s);    }    ctx.globalAlpha = 1;  };  // The first frame is drawn now, so the text never blinks when your state clears the field. The placeholder waits  // until the dust has gone, so the two never show on top of each other.  draw(0);  const placeholder = field.getAttribute("placeholder");  if (placeholder) field.setAttribute("placeholder", "");  return new Promise((resolve) => {    const begin = performance.now();    const frame = (now: number) => {      const p = Math.min(1, (now - begin) / duration);      if (field.isConnected) {        const r = field.getBoundingClientRect();        canvas.style.transform = `translate(${r.left - PAD}px, ${r.top - PAD}px)`;      }      draw(p);      if (p < 1) requestAnimationFrame(frame);      else {        canvas.remove();        if (placeholder && field.getAttribute("placeholder") === "") field.setAttribute("placeholder", placeholder);        resolve();      }    };    requestAnimationFrame(frame);  });}

Registry item: /r/clear-to-dust.json