Streaming Message

An AI reply that writes itself out. New words fade in as tokens arrive, a caret blinks at the end, and simple markdown renders cleanly even while it's half written.

Preview

Setup

Install with the shadcn CLI

$npx shadcn@latest add https://kinetikui.singhgourav.site/r/streaming-message.json

One command adds every file, the npm packages and any Kinetik components it builds on. Needs a components.json; the CLI offers to create one if your project has none.

Or install manually

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge
  2. 2

    Copy lib/utils.ts from the Code tab. Skip if you already have cn().

  3. 3

    Copy styles/tokens.css from the Code tab and import it in your global CSS after Tailwind: @import "./styles/tokens.css";

  4. 4

    Create a components/ui/streaming-message/ folder and copy every file from the Code tab into it. Each file does one job, so you can read or change a piece without the rest.

  5. 5

    Import and render:

    Example
    import { StreamingMessage } from "@/components/ui/streaming-message"
    
    <StreamingMessage content={text} streaming />

Usage

Pass everything received so far as content, and set streaming while more is coming. Each time content grows, only the new words animate in:

Example
import { StreamingMessage } from "@/components/ui/streaming-message"

const [text, setText] = useState("")
const [streaming, setStreaming] = useState(true)

// in your stream handler
for await (const chunk of stream) setText((t) => t + chunk)
setStreaming(false)

<StreamingMessage content={text} streaming={streaming} />

Animation

animationNew words
"fade"Fade in and rise slightly (default)
"blur"Come into focus from a blur
"none"Appear at once

People who turn on reduced motion get "none" automatically.

Markdown

It reads the markdown models usually write: paragraphs, # headings, - and 1. lists, **bold**, `code` and fenced code blocks. Half-written markup never shows up raw. For example, an unclosed ** already shows the text as bold, and an unclosed code fence already shows a code block.

Faking a stream

useTextStream plays back a finished string a few words at a time, with uneven gaps like a real model. It's handy for demos, landing pages and tests:

Example
import { StreamingMessage, useTextStream } from "@/components/ui/streaming-message"

const { content, streaming, restart, stop } = useTextStream(answer, { speed: 15 })

<StreamingMessage content={content} streaming={streaming} />

speed can come from state, so a slider can speed it up or slow it down while it's playing.

Props

PropTypeDefaultWhat it does
contentstring—The text received so far.
streamingbooleanfalseShows the caret while more text is coming.
animation"fade" | "blur" | "none""fade"How new words appear.

useTextStream(text, options)

Option / returnTypeWhat it is
speednumberAbout how many words per second. Default 15. Changes apply mid-stream.
autoStartbooleanStart playing right away. Default true.
contentstringThe text so far. Pass it to StreamingMessage.
streamingbooleanTrue until the whole text has played.
restart / stop() => voidPlay again from the start, or stop where it is.

Accessibility

  • The message is a polite live region and is marked busy while streaming, so screen readers don't read every word as it lands
  • The caret is decorative and hidden from screen readers
  • Word animations turn off for people who prefer reduced motion