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
What is debouncing and when should I use it?
Setup
Install with the shadcn CLI
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
Run in your terminal:
$npm install clsx tailwind-merge - 2
Copy
lib/utils.tsfrom the Code tab. Skip if you already havecn(). - 3
Copy
styles/tokens.cssfrom the Code tab and import it in your global CSS after Tailwind:@import "./styles/tokens.css"; - 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
Import and render:
Exampleimport { 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:
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
animation | New 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:
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
| Prop | Type | Default | What it does |
|---|---|---|---|
content | string | — | The text received so far. |
streaming | boolean | false | Shows the caret while more text is coming. |
animation | "fade" | "blur" | "none" | "fade" | How new words appear. |
useTextStream(text, options)
| Option / return | Type | What it is |
|---|---|---|
speed | number | About how many words per second. Default 15. Changes apply mid-stream. |
autoStart | boolean | Start playing right away. Default true. |
content | string | The text so far. Pass it to StreamingMessage. |
streaming | boolean | True until the whole text has played. |
restart / stop | () => void | Play 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