{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "streaming-text",
  "type": "registry:ui",
  "title": "StreamingText",
  "description": "متن کلمه‌به‌کلمه",
  "dependencies": [
    "@kashi-ui/persian@^2.26.0"
  ],
  "registryDependencies": [
    "https://kashi.sepantapouya.com/r/cx.json",
    "https://kashi.sepantapouya.com/r/kashi-provider.json"
  ],
  "files": [
    {
      "path": "registry/kashi/StreamingText.tsx",
      "type": "registry:ui",
      "target": "components/kashi/StreamingText.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useRef, useState, type ElementType } from \"react\";\nimport { cx } from \"./cx\";\nimport { useMessages } from \"./KashiProvider\";\nimport { faStreaming } from \"@kashi-ui/persian\";\n\nexport interface StreamingTextProps {\n  /** متن تا این لحظه؛ در پاسخ زندهٔ مدل زبانی با هر تکهٔ تازه بلندتر می‌شود */\n  text: string;\n  /** هنوز در حال نوشتن است: نشانگر چشمک‌زن و aria-busy @default false */\n  streaming?: boolean;\n  /**\n   * متن کامل را خودش کلمه‌به‌کلمه نشان می‌دهد (برای وقتی که پاسخ یک‌جا رسیده)؛\n   * عدد، کلمه در ثانیه است. بدون آن فقط کلمه‌های تازهٔ text نرم ظاهر می‌شوند.\n   */\n  speed?: number;\n  /** پس از نمایش کامل متن (در حالت speed) */\n  onDone?: () => void;\n  /** نشانگر انتهای متن @default true */\n  cursor?: boolean;\n  /** @default \"p\" */\n  as?: ElementType;\n  className?: string;\n}\n\nconst reduced = () => typeof matchMedia === \"function\" && matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n/** کلمه‌ها با فاصلهٔ بعدشان؛ نیم‌فاصله داخل کلمه می‌ماند */\nconst words = (s: string) => s.match(/\\S+\\s*|\\s+/g) ?? [];\n\n/**\n * متنی که کلمه‌به‌کلمه نوشته می‌شود؛ برای پاسخ زندهٔ دستیار هوش مصنوعی.\n * صفحه‌خوان هر کلمه را جدا نمی‌خواند: متن تا پایان aria-busy است و پایان پاسخ یک بار اعلام می‌شود.\n */\nexport function StreamingText({ text, streaming = false, speed, onDone, cursor = true, as: Tag = \"p\", className }: StreamingTextProps) {\n  const M = useMessages({ streaming: faStreaming });\n  const all = words(text);\n  const [shown, setShown] = useState(() => (speed && !reduced() ? 0 : all.length));\n  // کلمه‌هایی که پیش از این دیده شده‌اند انیمیشن نمی‌گیرند\n  const seen = useRef(speed ? 0 : all.length);\n  const done = useRef(onDone);\n  done.current = onDone;\n\n  useEffect(() => {\n    if (!speed || reduced()) {\n      setShown(all.length);\n      return;\n    }\n    if (shown >= all.length) {\n      done.current?.();\n      return;\n    }\n    const t = setTimeout(() => setShown((n) => n + 1), 1000 / speed);\n    return () => clearTimeout(t);\n  }, [speed, shown, all.length]);\n\n  const animated = !!speed && !reduced();\n  const count = animated ? Math.min(shown, all.length) : all.length;\n  useEffect(() => {\n    seen.current = count;\n  });\n\n  const typing = streaming || (animated && count < all.length);\n  // پایان نوشتن یک بار برای صفحه‌خوان اعلام می‌شود\n  const was = useRef(typing);\n  const [announce, setAnnounce] = useState(\"\");\n  useEffect(() => {\n    if (was.current && !typing) setAnnounce(M.streaming.done);\n    if (typing) setAnnounce(\"\");\n    was.current = typing;\n  }, [typing, M.streaming.done]);\n  const visible = all.slice(0, count);\n  const from = seen.current;\n  return (\n    <Tag className={cx(\"k-stream\", className)} dir=\"auto\" aria-busy={typing || undefined} data-streaming={typing ? \"\" : undefined}>\n      {visible.map((w, i) => (i < from ? w : <span key={i} className=\"k-stream__word\">{w}</span>))}\n      {cursor && typing && <span className=\"k-stream__cursor\" aria-hidden=\"true\" />}\n      <span className=\"k-sr-only\" aria-live=\"polite\">{announce}</span>\n    </Tag>\n  );\n}\n"
    }
  ]
}