Streaming Message
Renders an AI response token by token with full markdown — without the jitter most chat UIs have.
Preview
Installation
- 1
Run the following command
npx shadcn@latest add https://localhostui.com/r/streaming-message.json
Also copy markdown.tsx from the same folder — it holds the renderer, the incomplete-markdown repair and the code block.
Usage
import { Message, MessageList, useStreamingText } from "@/components/lokalhost_io/micro-ai-components/streaming-message/streaming-message";
const { text, status } = useStreamingText(stream);
<Message status={status === "idle" ? "pending" : status}>
<Message.Content content={text} />
</Message>How it stays smooth
Block memoization. The content is split into top-level blocks on blank lines (never inside a code fence). Every finished block is a React.memo component whose only prop is its own string, so it is parsed once and never touched again. Only the last, still-growing block re-parses on each token. A 2,000-word answer costs the same per token as a 20-word one.
Incomplete markdown repair. Before the active block is parsed, repairMarkdown closes an unterminated code fence (so it renders as an open code block), closes dangling **, *, ` and ~~, and drops a trailing half-typed marker — raw asterisks never flash.
Only new words animate. A tiny rehype plugin wraps each word in a span. React keeps the existing spans in place, so the CSS fade or blur only runs on spans that are newly mounted — no per-word state, no re-animation of old text.
The caret travels. A second plugin appends the ▍ caret to the deepest last element, so it sits at the end of a list item or inside an open code block. Code is syntax-highlighted with Shiki (lazy-loaded) only after its fence closes.
Examples
With fetch + ReadableStream
async function* readText(res: Response) {
const reader = res.body!.pipeThrough(new TextDecoderStream()).getReader();
for (;;) {
const { value, done } = await reader.read();
if (done) return;
yield value;
}
}
const [stream, setStream] = useState<AsyncIterable<string> | null>(null);
const { text, status, stop } = useStreamingText(stream);
const ask = async (prompt: string) => {
const res = await fetch("/api/chat", { method: "POST", body: JSON.stringify({ prompt }) });
setStream(readText(res));
};With the Vercel AI SDK useChat
useChat already accumulates text, so pass it straight to Message.Content:
const { messages, status } = useChat();
<MessageList>
{messages.map((m, i) => {
const last = i === messages.length - 1 && m.role === "assistant";
const text = m.parts.filter((p) => p.type === "text").map((p) => p.text).join("");
return (
<Message key={m.id} role={m.role === "user" ? "user" : "assistant"} status={last && status === "streaming" ? "streaming" : last && status === "submitted" ? "pending" : "done"}>
<Message.Content content={text} />
</Message>
);
})}
</MessageList>Sticky auto-scroll
MessageList sticks to the bottom while content grows. Scroll up and it stops following; a New messages pill scales in to jump back down.
Props
Message
| Prop | Type | Default | Description |
|---|---|---|---|
role | "assistant""user" | "assistant" | User messages render as a right-aligned muted bubble (max 80% wide); assistant messages are full width with an avatar. |
status | "pending""streaming""done""error" | "done" | pending shows the typing dots, streaming animates new words and the caret, done fades in the footer. |
avatar | ReactNode | null | a sparkle badge | Assistant avatar. Pass null to hide it. |
name | string | — | Name shown above assistant content. |
timestamp | ReactNode | — | Shown in a footer that fades in when the message is done. |
Message.Content
| Prop | Type | Default | Description |
|---|---|---|---|
content* | string | — | Markdown (partial while streaming). |
animation | "fade""blur""none" | "fade" | Per-word enter animation for newly streamed words. |
markdown | boolean | true for assistant, false for user | Render as markdown. |
caret | boolean | true | Show the blinking ▍ caret while streaming. |
remarkPlugins | PluggableList | — | Extra remark plugins (e.g. remarkCitations). remark-gfm is always on. |
components | Components | — | Override or add element renderers. Memoize the object. |
Message.Error
| Prop | Type | Default | Description |
|---|---|---|---|
onRetry | () => void | Promise<unknown> | — | Shows a Retry button. Return a promise to keep the icon spinning until it settles. |
message | ReactNode | "Something went wrong while generating this response." | Error text. |
MessageList
| Prop | Type | Default | Description |
|---|---|---|---|
threshold | number | 48 | Distance from the bottom (px) that still counts as stuck. |
contentClassName | string | — | Classes on the inner content wrapper (gap, padding). |
useStreamingText(stream)
| Prop | Type | Default | Description |
|---|---|---|---|
stream* | AsyncIterable<string> | null | — | Pass a new iterable to (re)start. null resets to idle. |
→ text | string | — | Accumulated text. |
→ status | "idle""pending""streaming""done""error" | — | Lifecycle of the current stream. |
→ stop | () => void | — | Stops reading and keeps the text received so far. |
→ error | unknown | — | Whatever the stream threw. |
Accessibility
- The content container sets
aria-busywhile streaming. The finished text is announced once through a visually hidden polite live region, so screen readers aren't spammed per token. - The typing dots have
role="status"and a label. - With reduced motion, words appear without fades and the caret doesn't blink.