← Components

Communication

Streaming text

Text that arrives word by word. New words fade in from a soft blur; shown words never re-animate; a caret pill pulses while streaming.

Examples and variants

Streaming

Replay to watch it again.

Example
function StreamingDemo() {
  const [count, setCount] = useState(0)
  const words = sentence.split(" ")
  useEffect(() => {
    if (count >= words.length) return
    const timer = window.setTimeout(() => setCount((c) => c + 1), tokenMs("--duration-stagger") * 2)
    return () => window.clearTimeout(timer)
  }, [count, words.length])
  return (
    <div className="flex w-full max-w-sm flex-col gap-3">
      <StreamingText text={words.slice(0, count).join(" ")} streaming={count < words.length} />
      <Button size="sm" variant="secondary" className="self-start" onClick={() => setCount(0)}>
        Replay
      </Button>
    </div>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/streaming-text.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install cn
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/streaming-text.tsxstreaming-text
      components/ui/streaming-text.tsx
      "use client"
      
      import * as React from "react"
      import { cn } from "cn"
      
      /*
        Streaming text: text that arrives word by word. New words fade in from a
        soft blur; words already shown never re-animate. While streaming, a soft
        caret pill pulses at the end. Screen readers get completed text only.
      */
      function StreamingText({
        text,
        streaming = false,
        className,
      }: {
        /** The full text so far. Append to it as tokens arrive. */
        text: string
        streaming?: boolean
        className?: string
      }) {
        // Keyed by position, so words already shown stay mounted and never re-animate;
        // only newly arrived words mount and play their fade-in.
        const words = text.split(/(\s+)/)
        return (
          <p data-slot="streaming-text" className={cn("text-sm leading-relaxed whitespace-pre-wrap", className)}>
            <span aria-hidden>
              {words.map((word, index) => (
                <span key={index} className="stream-word">
                  {word}
                </span>
              ))}
              {streaming ? <span className="stream-caret ms-0.5 inline-block h-[1em] w-1.5 translate-y-[0.15em] rounded-(--shape-full) bg-(--stream-caret)" /> : null}
            </span>
            <span className="sr-only">{streaming ? "" : text}</span>
          </p>
        )
      }
      
      export { StreamingText }
      

Imports

What the examples on this page import:

Imports
import { Attachment } from "@/components/ui/attachment"
import { Avatar, AvatarFallback } from "@/components/ui/avatar"
import { Bubble, BubbleGroup, BubbleReactions } from "@/components/ui/bubble"
import { Button } from "@/components/ui/button"
import { CodeBlock } from "@/components/ui/code-block"
import { Marker } from "@/components/ui/marker"
import { Message, MessageAvatar, MessageContent, MessageFooter, MessageHeader } from "@/components/ui/message"
import { MessageScroller } from "@/components/ui/message-scroller"
import { PromptInput } from "@/components/ui/prompt-input"
import { Questionnaire } from "@/components/ui/questionnaire"
import { StreamingText } from "@/components/ui/streaming-text"
import { Trace, TraceItem } from "@/components/ui/trace"
import { VoiceInput } from "@/components/ui/voice-input"
import { tokenMs } from "@/lib/motion/token-ms"

Anatomy

  1. 1Words · --stream-word-duration, --stream-blur.
  2. 2Caret · --stream-caret.

Props

PropTypeDefaultDescription
textstring—Full text so far; append as tokens arrive.
streamingboolean—Shows the caret; hides text from screen readers until done.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

TokenPoints toLightDark
--stream-word-durationvar(--duration-quick)160mssame
--stream-blurvar(--motion-blur-enter)2pxsame
--stream-caretvar(--on-surface)oklch(0.22 0.01 260)oklch(1 0 0)

Motion

TriggerWhat happensTokens
New wordFades in from blur.--stream-word-duration

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • Screen readers get the finished text, not every token.

Usage

Do

  • Use for model output.

Don’t

  • Don't animate text that was already there.

Code

components/ui/streaming-text.tsx
"use client"

import * as React from "react"
import { cn } from "cn"

/*
  Streaming text: text that arrives word by word. New words fade in from a
  soft blur; words already shown never re-animate. While streaming, a soft
  caret pill pulses at the end. Screen readers get completed text only.
*/
function StreamingText({
  text,
  streaming = false,
  className,
}: {
  /** The full text so far. Append to it as tokens arrive. */
  text: string
  streaming?: boolean
  className?: string
}) {
  // Keyed by position, so words already shown stay mounted and never re-animate;
  // only newly arrived words mount and play their fade-in.
  const words = text.split(/(\s+)/)
  return (
    <p data-slot="streaming-text" className={cn("text-sm leading-relaxed whitespace-pre-wrap", className)}>
      <span aria-hidden>
        {words.map((word, index) => (
          <span key={index} className="stream-word">
            {word}
          </span>
        ))}
        {streaming ? <span className="stream-caret ms-0.5 inline-block h-[1em] w-1.5 translate-y-[0.15em] rounded-(--shape-full) bg-(--stream-caret)" /> : null}
      </span>
      <span className="sr-only">{streaming ? "" : text}</span>
    </p>
  )
}

export { StreamingText }