# Streaming answer

> An answer as it arrives, with inline citations, actions, sources and follow-ups.

Renders whatever your stream has delivered so far. Only the words that just arrived fade in, citations land as small chips inside the sentence, and finished paragraphs don’t re-render while the last one grows. It knows when it was stopped or interrupted, and takes your own Markdown renderer when you need one. Underneath, the actions answer back: Copy blurs into a check that draws itself, a thumb fills in with a small thrown pop, and the sources count rolls as more arrive.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/streaming-answer.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `number-roll`
- Docs and live preview: https://hairlineui.com/ui/streaming-answer
- Registry item (source): https://hairlineui.com/r/streaming-answer.json

## Usage

```tsx
import { StreamingText, AnswerActions, FollowUps } from "@/components/streaming-answer";

<StreamingText
  content={["Signups are up 23%.", { type: "citation", label: "Q3 report" }, " Churn is down."]}
  status="streaming"
/>
<AnswerActions className="mt-4" copyText={text} onRegenerate={retry} sources={sources} />
<FollowUps className="mt-4" items={["Which channels bring the most signups"]} onSelect={ask} />

// Markdown: pass your renderer as children instead of content
<StreamingText status={status}>
  <ReactMarkdown>{text}</ReactMarkdown>
</StreamingText>
```

## With the AI SDK

```tsx
import { useChat } from "@ai-sdk/react";
import type { UIMessage } from "ai";
import { StreamingText, AnswerActions } from "@/components/streaming-answer";

function Answer({ message, isLast }: { message: UIMessage; isLast: boolean }) {
  const { status, regenerate } = useChat();
  const text = message.parts.map((p) => (p.type === "text" ? p.text : "")).join("");
  const sources = message.parts.filter((p) => p.type === "source-url");
  const live = isLast && status === "streaming";

  return (
    <>
      <StreamingText
        content={[text]}
        status={live ? "streaming" : isLast && status === "error" ? "error" : "done"}
        onRetry={() => regenerate()}
      />
      <AnswerActions
        className="mt-4"
        disabled={live}
        copyText={text}
        onRegenerate={() => regenerate({ messageId: message.id })}
        sources={sources.map((s) => ({ name: s.title ?? s.url, href: s.url }))}
      />
    </>
  );
}
// "stopped": useChat returns to "ready" after stop(), so remember which message you stopped
// and pass status="stopped" for it.
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `StreamingText content` | `(string \| { type: "citation"; label; href? })[]` | – | The answer so far. Blank lines start new paragraphs. |
| `StreamingText status` | `"streaming" \| "done" \| "stopped" \| "error"` | `"done"` | Caret while streaming; notes for stopped and error. |
| `StreamingText onRetry / children` | `() => void / ReactNode` | – | Retry button on error; your own renderer (e.g. Markdown) instead of content. |
| `AnswerActions copyText / onRegenerate` | `string / () => void` | – | Copy button (omit to hide) and regenerate button. |
| `AnswerActions feedback / defaultFeedback / onFeedback` | `"up" \| "down" \| null` | – | Thumbs up or down; controlled so you can restore it from your database. |
| `AnswerActions sources / onSourcesClick / disabled` | `AnswerSource[] / () => void / boolean` | – | Source favicons or dots with a count; disable while streaming. |
| `FollowUps items / onSelect` | `string[] / (item) => void` | – | Suggested next questions. |

## States

- **streaming**: Caret at the end; new words fade in; the paragraph is aria-busy.
- **done**: Plain, selectable text; actions enabled. Copy shrinks away through a blur as a green check draws itself (380ms); a chosen thumb fills (220ms) with a 420ms thrown pop.
- **stopped**: A quiet "Stopped" note when the user stopped generation.
- **error**: "The answer was interrupted." with a Retry button.

## What it handles

An AI answer arrives in pieces. Done badly, the whole text re-renders on every chunk and the page flickers. This renders what has arrived so far and animates only the new words.

- **Only new words fade in**: Words keep a stable key, so only the newly arrived ones mount and fade in, with a caret at the end.
- **Long answers stay smooth**: Finished paragraphs are memoised and don’t re-render while the last one grows.
- **Citations inside sentences**: Citations land as small chips in the sentence, ready to link to a source.
- **Actions that answer back**: Copy blurs into a drawn check, a thumb fills with a small pop, and the sources count rolls as more arrive.
- **Stopped and interrupted answers**: A quiet “Stopped” note, or “The answer was interrupted” with a Retry button.
- **Your own Markdown renderer**: Pass children instead of content to use any renderer, and add suggested follow-up questions underneath.

## Replaces

a streaming text renderer, a typing caret, citation chips, Copy and feedback buttons, follow-up suggestions, stopped and error notes

## Where it fits

- **AI chat apps**: Show the answer as it arrives from the model.
- **Search and research tools**: Answers with citations that point at their sources.
- **Support bots**: Answers followed by suggested next questions.
- **Documentation assistants**: Answers with the sources they were taken from.

## Notes

- Words keep a stable key, so only new words mount and fade in; the answer is ordinary text for copying and screen readers.
- Finished paragraphs are memoised, so long answers stay smooth while streaming.
- Every icon button has a label and the feedback buttons use aria-pressed; "Copied" is announced from a status region beside them.
- Installs Number roll. With reduced motion, words and citations appear without fading, the caret stops blinking and the actions change in place.

License: MIT (https://hairlineui.com/license#components)
