{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "streaming-ink-dry",
  "title": "Streaming Ink Dry",
  "description": "Streaming LLM text where the newest tokens arrive light and translucent, then dry to full opacity a beat behind the stream head. Width-stable, so committed text never reflows.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/core/streaming-ink-dry/component.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useRef, useState, type CSSProperties } from \"react\";\n\n// Streaming LLM text where the still-provisional tail visibly hasn't dried\n// yet. Each new token starts light — opacity .55, a hair of blur — then\n// rides up to opacity 1 and no blur over ~600ms, ease-out-expo. font-weight\n// is locked to 400 for the token's entire life (never part of the\n// animation): interpolating the variable-font weight axis mid-flight\n// changes glyph advance widths frame by frame, and because tokens arrive\n// faster than one token's dry time, several spans would be mid-ramp at\n// once — the trailing settled text would visibly compact as their widths\n// crept. Keeping weight constant and animating only opacity/blur (neither\n// affects layout) means arrival never reflows already-rendered text.\n//\n// Every token is a real, pure-CSS `animation: ... both` — arrival IS the\n// stagger, nothing is scheduled or replayed from JS, and because tokens is\n// append-only with stable index keys, React never remounts an already-dried\n// span when new ones arrive. prefers-reduced-motion collapses the whole\n// ramp to a plain settled span (see the media query below) rather than\n// skipping the animation via JS, so there's no flash-of-wet-then-cut.\n//\n// Screen readers get a second, parallel channel rather than reading the\n// decorative spans (which are aria-hidden): a visually-hidden log holds the\n// real text, split into already-announced sentences (each its own static\n// span — a plain node appended to a polite live region is itself the\n// announcement) and one trailing `aria-busy` span for whatever hasn't hit a\n// sentence boundary yet. Mutating a busy span's text doesn't get announced\n// fragment by fragment; it's released into a settled sentence (and read\n// once, whole) the moment punctuation closes it, or after a short idle gap\n// if the stream stalls or ends mid-clause.\n\nexport interface WetInkProps {\n  /**\n   * Every token received so far, oldest first, append-only. Include\n   * whatever whitespace the model actually emitted with each token — this\n   * component never inserts its own spacing between them. Shrinking the\n   * array (a fresh stream) resets both the ink and the live-region state.\n   */\n  tokens: string[];\n  /** Ms for one token to fully dry. Default 600. */\n  dryMs?: number;\n  /**\n   * Ms of arrival silence before an unfinished (no sentence-ending\n   * punctuation yet) tail is flushed to the live region anyway. Default 900.\n   */\n  idleFlushMs?: number;\n  /** extra classes merged onto the rendered root element */\n  className?: string;\n}\n\nconst DEFAULT_DRY_MS = 600;\nconst DEFAULT_IDLE_FLUSH_MS = 900;\n\n// Last index worth flushing a sentence at: a run of text ending in ./!/?\n// followed by whitespace-or-end, or a bare newline (paragraph break) at any\n// position. Deliberately a heuristic (an abbreviation like \"Mr.\" will flush\n// early) — good enough for batching, not a sentence parser.\nfunction lastFlushBoundary(s: string): number {\n  let idx = -1;\n  for (let i = 0; i < s.length; i++) {\n    const c = s[i];\n    if (c === \"\\n\") {\n      idx = i;\n      continue;\n    }\n    if ((c === \".\" || c === \"!\" || c === \"?\") && (i === s.length - 1 || /\\s/.test(s[i + 1] ?? \"\"))) {\n      idx = i;\n    }\n  }\n  return idx;\n}\n\nexport function WetInk({\n  tokens,\n  dryMs = DEFAULT_DRY_MS,\n  idleFlushMs = DEFAULT_IDLE_FLUSH_MS,\n  className = \"\",\n}: WetInkProps) {\n  const [settled, setSettled] = useState<string[]>([]);\n  const [tail, setTail] = useState(\"\");\n  const bufRef = useRef({ settled: [] as string[], tail: \"\" });\n  const consumedLenRef = useRef(0);\n  const idleTimerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);\n\n  useEffect(() => {\n    const fullText = tokens.join(\"\");\n\n    // Shorter than what we've already consumed => a new stream started.\n    if (fullText.length < consumedLenRef.current) {\n      consumedLenRef.current = 0;\n      bufRef.current = { settled: [], tail: \"\" };\n    }\n\n    const delta = fullText.slice(consumedLenRef.current);\n    consumedLenRef.current = fullText.length;\n\n    if (delta) {\n      let nextTail = bufRef.current.tail + delta;\n      let nextSettled = bufRef.current.settled;\n      const boundary = lastFlushBoundary(nextTail);\n      if (boundary >= 0) {\n        nextSettled = [...nextSettled, nextTail.slice(0, boundary + 1)];\n        nextTail = nextTail.slice(boundary + 1);\n      }\n      bufRef.current = { settled: nextSettled, tail: nextTail };\n      setSettled(nextSettled);\n      setTail(nextTail);\n    }\n\n    clearTimeout(idleTimerRef.current);\n    idleTimerRef.current = setTimeout(() => {\n      if (bufRef.current.tail) {\n        const flushed = [...bufRef.current.settled, bufRef.current.tail];\n        bufRef.current = { settled: flushed, tail: \"\" };\n        setSettled(flushed);\n        setTail(\"\");\n      }\n    }, idleFlushMs);\n\n    return () => clearTimeout(idleTimerRef.current);\n  }, [tokens, idleFlushMs]);\n\n  return (\n    <div\n      role=\"log\"\n      aria-live=\"polite\"\n      aria-atomic=\"false\"\n      className={className}\n      style={\n        dryMs !== DEFAULT_DRY_MS\n          ? ({ \"--ns-streaming-ink-dry-dry-ms\": `${dryMs}ms` } as CSSProperties)\n          : undefined\n      }\n    >\n      <p aria-hidden=\"true\" className=\"ns-streaming-ink-dry-visual whitespace-pre-wrap\">\n        {tokens.map((tok, i) =>\n          tok === \"\" ? null : (\n            <span key={i} className=\"ns-streaming-ink-dry-token\">\n              {tok}\n            </span>\n          )\n        )}\n      </p>\n      <span className=\"sr-only\">\n        {settled.map((chunk, i) => (\n          <span key={i}>{chunk}</span>\n        ))}\n        <span aria-busy={tail.length > 0}>{tail}</span>\n      </span>\n      <style>{CSS}</style>\n    </div>\n  );\n}\n\nconst CSS = `\n.ns-streaming-ink-dry-token{\n  font-weight:400;\n  font-variation-settings:'wght' 400;\n  animation:ns-streaming-ink-dry-dry var(--ns-streaming-ink-dry-dry-ms,600ms) cubic-bezier(.16,1,.3,1) both;\n}\n@keyframes ns-streaming-ink-dry-dry{\n  from{opacity:.55;filter:blur(.4px)}\n  to{opacity:1;filter:blur(0)}\n}\n@media (prefers-reduced-motion: reduce){\n  .ns-streaming-ink-dry-token{animation:none;font-weight:400;font-variation-settings:'wght' 400;opacity:1;filter:none}\n}\n`;\n",
      "type": "registry:ui",
      "target": "components/ui/streaming-ink-dry.tsx"
    }
  ],
  "meta": {
    "collection": "core",
    "tags": [
      "text",
      "streaming",
      "ai",
      "chat",
      "variable-font",
      "accessibility"
    ],
    "instruction": "<WetInk tokens={string[]}> renders a live streaming-text surface: pass the cumulative, append-only array of tokens received so far (each token exactly as the model emitted it, whitespace included — the component never inserts its own spacing) and it re-renders as the array grows. Every newly appended token mounts as its own span starting at opacity .55 and a 0.4px blur, then rides a single ~600ms ease-out-expo CSS animation (`animation: ... both`, no JS scheduling) up to opacity 1, zero blur. font-weight stays locked at 400 for the token's entire life — it is never part of the animation, deliberately: interpolating a variable font's weight axis changes glyph advance widths frame by frame, and because tokens can arrive faster than one token's dry time several spans would be mid-ramp at once, so any weight animation would read as the settled text ahead of it visibly compacting while you watch. Keeping weight constant and animating only opacity/blur (neither affects layout) means committed text never shifts horizontally, and arrival time alone still provides the stagger since tokens is append-only with stable index keys (already-dried spans are never remounted when new ones land). A still frame therefore always reads as a gradient of certainty: dried body, a drying middle, and a wet tail trailing the newest token. Accessibility is a second, parallel channel: the outer element is role=log aria-live=polite holding a visually-hidden transcript, not the decorative (aria-hidden) animated spans — settled sentences are each their own static span (a plain node landing in a polite live region is itself the announcement), and unresolved text sits in one trailing span carrying aria-busy=true so a screen reader is told to hold off, not left to infer 'wet' from opacity alone; that busy span's content is released into a new settled sentence the instant sentence-ending punctuation (or a newline) closes it, or after a short configurable idle gap (default 900ms, `idleFlushMs`) if the stream stalls mid-clause, so screen readers get whole sentences, never token-by-token fragments. Shrinking the `tokens` array (a fresh message) resets both the ink and the live-region bookkeeping. prefers-reduced-motion is handled entirely in CSS: the media query drops the animation and pins every token straight to its settled opacity/blur, so reduced-motion users see plain legible text arrive with no ramp, not a stall. `dryMs` (default 600) retunes the drying duration via a CSS custom property. Pure DOM + CSS, no canvas, zero dependencies — distinct from text-decrypt (a one-shot monospace scramble-to-decode entrance on a fixed string) and text-variable-weight (a decorative, cursor-driven weight morph with no underlying state): this is a live container where opacity/blur encode a real 'not yet settled' state and keeps running for the life of the stream."
  },
  "type": "registry:ui"
}