# Clear to dust

> 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.

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.

## Install

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

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

- npm: none
- Hairline UI components it needs (the CLI installs them too): none
- Docs and live preview: https://hairlineui.com/ui/clear-to-dust
- Registry item (source): https://hairlineui.com/r/clear-to-dust.json

## 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.

```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

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `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` | `number` | `450` | Milliseconds for the whole clear. |

## 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.

## What it handles

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

- **One call, your state**: clearToDust(field) only draws; you clear the value yourself, on the same line, the way you already do.
- **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.
- **Textareas too**: Lines are wrapped to the field’s width and follow its scroll, so a long prompt clears line by line.
- **Never blinks**: The first frame is drawn at the call, so the text can’t vanish for a frame when your state clears.
- **Short on purpose**: About 0.45s and a few dozen pixels, the budget for something done many times a day.
- **Passwords stay dots**: A password field turns into dust as its dots, never its text.

## Replaces

text clear animation, input reset animation, particle text effect, framer-motion text exit

## Where it fits

- **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.

## Notes

- 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.

License: MIT (https://hairlineui.com/license#components)
