Response Stream

A component for displaying text with streaming animations, perfect for chat interfaces, AI responses, or any text that should appear progressively.

Examples

Typewriter Mode

The default mode that types out text character by character, simulating a typing effect.

Fade Mode

The fade mode reveals text word by word with a smooth fade-in animation.

With Markdown

ResponseStream can be combined with the Markdown component to create rich, animated content, for that you need to use the useTextStream hook directly.

Note: If you want to use mode="fade", you need to manually render the segments with appropriate CSS animations. It can be hard to get it done with markdown, the way is to write a custom remarkPlugins. We have a demo but it's a bit too experimental to be included here, happy to receive a PR if you have a good solution.

Using the useTextStream Hook with fade mode

When using the useTextStream hook with fade mode, you need to manually render the segments with appropriate CSS animations.

Installation

npx shadcn add "https://prompt-kit.com/c/response-stream.json"

Component API

ResponseStream

PropTypeDefaultDescription
textStreamstring | AsyncIterable<string>The text to stream or an async iterable of text chunks
mode"typewriter" | "fade""typewriter"The animation mode to use
speednumber20Speed from 1-100, where 1 is slowest and 100 is fastest
classNamestring""Additional CSS classes
onComplete() => voidCallback function when streaming is complete
asstring"div"Element type to render
fadeDurationnumberCustom fade duration in ms (overrides speed)
segmentDelaynumberCustom delay between segments in ms (overrides speed)
characterChunkSizenumberCustom characters per frame for typewriter (overrides speed)

useTextStream Hook

Parameters

ParameterTypeDefaultDescription
textStreamstring | AsyncIterable<string>The text to stream or an async iterable of text chunks
speednumber20Speed from 1-100, where 1 is slowest and 100 is fastest
mode"typewriter" | "fade""typewriter"The animation mode to use
onComplete() => voidCallback function when streaming is complete
fadeDurationnumberCustom fade duration in ms (overrides speed)
segmentDelaynumberCustom delay between segments in ms (overrides speed)
characterChunkSizenumberCustom characters per frame for typewriter (overrides speed)
onError(error: unknown) => voidCallback function when an error occurs

Return Value

PropertyTypeDescription
displayedTextstringThe current text being displayed
isCompletebooleanWhether streaming is complete
segments{ text: string; index: number }[]Text segments for fade mode
getFadeDuration() => numberFunction to get the current fade duration
getSegmentDelay() => numberFunction to get the current segment delay
reset() => voidFunction to reset the streaming state
startStreaming() => voidFunction to start or restart streaming
pause() => voidFunction to pause streaming
resume() => voidFunction to resume streaming