{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "streaming-text",
  "type": "registry:ui",
  "title": "Streaming Text",
  "description": "Text that grows as it arrives, with a caret while it is arriving, that is never read out word by word: it is announced once, when it is finished.",
  "categories": [
    "ui"
  ],
  "dependencies": [],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/announce",
    "@rivier-ui/cn"
  ],
  "files": [
    {
      "path": "registry/ui/streaming-text.tsx",
      "type": "registry:ui",
      "target": "components/ui/streaming-text.tsx",
      "content": "\"use client\";\n/**\n * @name streaming-text\n * @category ui\n * @description Text that grows as it arrives, with a caret while it is arriving, that is never read out word by word: it is announced once, when it is finished.\n * @when A reply from a model, a transcript or a log line that streams in. Pass the text so far and whether more is coming. It must not sit inside a live region (a thread is `aria-live=\"off\"`); say \"Writing a reply\" through a status-line while it streams. For text that is already complete use a plain element; for a headline that animates in use text-reveal.\n * @example\n * <MessageContent>\n *   <StreamingText text={reply} streaming={isStreaming} announceWhenDone onDone={saveReply} />\n * </MessageContent>\n *\n * // A shorter spoken form, and words that fade in as they land\n * <StreamingText text={reply} streaming={isStreaming} reveal=\"word\" announceWhenDone=\"Three places found. The nearest is ten minutes away.\" />\n *\n * // The project's own renderer (Markdown, for example)\n * <StreamingText text={reply} streaming={isStreaming}>{(text) => <Markdown>{text}</Markdown>}</StreamingText>\n */\nimport { useEffect, useEffectEvent, useRef, type ComponentProps, type ReactNode } from \"react\";\nimport { announce } from \"@/lib/announce\";\nimport { cn } from \"@/lib/cn\";\n\nexport type StreamingTextProps = Omit<ComponentProps<\"span\">, \"children\"> & {\n  /** The text so far. */\n  text: string;\n  /** Whether more is coming. While true the element is `aria-busy` and shows the caret. */\n  streaming?: boolean;\n  /**\n   * Said once through the shared live region when streaming ends: `true` says the whole text, a string says\n   * that instead (a shorter spoken form of a long reply). Leave it out when the project announces the reply\n   * some other way. Text that was complete when it was first shown is not announced.\n   */\n  announceWhenDone?: boolean | string;\n  /** Called once when `streaming` goes from true to false, with the finished text. */\n  onDone?: (text: string) => void;\n  /** The caret after the last word while streaming. Default true. */\n  caret?: boolean;\n  /** \"word\": each word fades in as it lands (opacity only, off under reduced motion). The text is in the page either way. Default \"none\". */\n  reveal?: \"none\" | \"word\";\n  /** Render the text yourself (Markdown, links). `reveal` then does not apply. */\n  children?: (text: string) => ReactNode;\n};\n\n/**\n * Nothing here is a live region, on purpose: a screen reader that announced every arriving word would be\n * unusable. The element only marks itself busy; the one announcement is made when the text is whole.\n * Arriving text never takes or moves focus.\n */\nexport function StreamingText({ className, text, streaming = false, announceWhenDone, onDone, caret = true, reveal = \"none\", children, ...props }: StreamingTextProps) {\n  const wasStreaming = useRef(streaming);\n  const finish = useEffectEvent((): void => {\n    onDone?.(text);\n    if (announceWhenDone === true) announce(text);\n    else if (typeof announceWhenDone === \"string\") announce(announceWhenDone);\n  });\n  useEffect(() => {\n    if (wasStreaming.current && !streaming) finish();\n    wasStreaming.current = streaming;\n  }, [streaming]);\n\n  return (\n    <span data-slot=\"streaming-text\" data-streaming={streaming ? \"\" : undefined} aria-busy={streaming || undefined} className={cn(\"wrap-anywhere whitespace-pre-wrap\", className)} {...props}>\n      {children ? children(text) : reveal === \"word\" ? <Words text={text} /> : text}\n      {streaming && caret ? (\n        <span\n          aria-hidden=\"true\"\n          data-slot=\"streaming-text-caret\"\n          className=\"ms-0.5 inline-block h-[1em] w-[0.5em] translate-y-[0.15em] animate-pulse rounded-control bg-current opacity-70 motion-reduce:animate-none\"\n        />\n      ) : null}\n    </span>\n  );\n}\n\n/**\n * The text as words and the spaces between them. A word keeps its key as the text grows, so only a word\n * that has just landed is mounted, and only that one fades in. The words are real text in the page.\n */\nfunction Words({ text }: { text: string }) {\n  const pieces = text.split(/(\\s+)/);\n  return (\n    <>\n      {pieces.map((piece, index) =>\n        index % 2 === 1 || piece === \"\" ? (\n          piece\n        ) : (\n          <span key={index} className=\"animation-duration-(--duration-slow) animate-in fade-in-0 motion-reduce:animate-none\">\n            {piece}\n          </span>\n        ),\n      )}\n    </>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "ui",
    "when": "A reply from a model, a transcript or a log line that streams in. Pass the text so far and whether more is coming. It must not sit inside a live region (a thread is `aria-live=\"off\"`); say \"Writing a reply\" through a status-line while it streams. For text that is already complete use a plain element; for a headline that animates in use text-reveal.",
    "example": "<MessageContent>\n  <StreamingText text={reply} streaming={isStreaming} announceWhenDone onDone={saveReply} />\n</MessageContent>\n\n// A shorter spoken form, and words that fade in as they land\n<StreamingText text={reply} streaming={isStreaming} reveal=\"word\" announceWhenDone=\"Three places found. The nearest is ten minutes away.\" />\n\n// The project's own renderer (Markdown, for example)\n<StreamingText text={reply} streaming={isStreaming}>{(text) => <Markdown>{text}</Markdown>}</StreamingText>",
    "client": true
  }
}
