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