Response Stream
prompt-kit/response-streamFreeComponent
A component to simulate streaming text on the client side, perfect for fake responses, or any controlled progressive text display.
Live preview
live · rendered by the registry
Rendered by prompt-kit on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://raw.githubusercontent.com/ibelick/prompt-kit/HEAD/public/c/response-stream.jsonSource
1"use client"23import { cn } from "@/lib/utils"4import React, { useCallback, useEffect, useRef, useState } from "react"56export type Mode = "typewriter" | "fade"78export type UseTextStreamOptions = {9 textStream: string | AsyncIterable<string>10 speed?: number11 mode?: Mode12 onComplete?: () => void13 fadeDuration?: number14 segmentDelay?: number15 characterChunkSize?: number16 onError?: (error: unknown) => void17}1819export type UseTextStreamResult = {20 displayedText: string21 isComplete: boolean22 segments: { text: string; index: number }[]23 getFadeDuration: () => number24 getSegmentDelay: () => number25 reset: () => void26 startStreaming: () => void27 pause: () => void28 resume: () => void29}3031function useTextStream({32 textStream,33 speed = 20,34 mode = "typewriter",35 onComplete,36 fadeDuration,37 segmentDelay,38 characterChunkSize,39 onError,40}: UseTextStreamOptions): UseTextStreamResult {41 const [displayedText, setDisplayedText] = useState("")42 const [isComplete, setIsComplete] = useState(false)43 const [segments, setSegments] = useState<{ text: string; index: number }[]>(44 []45 )4647 const speedRef = useRef(speed)48 const modeRef = useRef(mode)49 const currentIndexRef = useRef(0)50 const animationRef = useRef<number | null>(null)51 const fadeDurationRef = useRef(fadeDuration)52 const segmentDelayRef = useRef(segmentDelay)53 const characterChunkSizeRef = useRef(characterChunkSize)54 const streamRef = useRef<AbortController | null>(null)55 const completedRef = useRef(false)56 const onCompleteRef = useRef(onComplete)5758 useEffect(() => {59 speedRef.current = speed60 modeRef.current = mode61 fadeDurationRef.current = fadeDuration62 segmentDelayRef.current = segmentDelay63 characterChunkSizeRef.current = characterChunkSize64 }, [speed, mode, fadeDuration, segmentDelay, characterChunkSize])6566 useEffect(() => {67 onCompleteRef.current = onComplete68 }, [onComplete])6970 const getChunkSize = useCallback(() => {71 if (typeof characterChunkSizeRef.current === "number") {72 return Math.max(1, characterChunkSizeRef.current)73 }7475 const normalizedSpeed = Math.min(100, Math.max(1, speedRef.current))7677 if (modeRef.current === "typewriter") {78 if (normalizedSpeed < 25) return 179 return Math.max(1, Math.round((normalizedSpeed - 25) / 10))80 } else if (modeRef.current === "fade") {81 return 182 }8384 return 185 }, [])8687 const getProcessingDelay = useCallback(() => {88// … truncated
Files it writes
More from prompt-kit
All 23 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Chain Of Thoughtchain-of-thought | prompt-kit | Components | Free | 1 dep | |
| Chat Containerchat-container | prompt-kit | Components | Free | 1 dep | |
| Code Blockcode-block | prompt-kit | Components | Free | 1 dep | |
| Feedback Barfeedback-bar | prompt-kit | Components | Free | 1 dep | |
| File Uploadfile-upload | prompt-kit | Components | Free | no deps | |
| Imageimage | prompt-kit | Components | Free | no deps | |
| Jsx Previewjsx-preview | prompt-kit | Components | Free | 1 dep | |
| Loaderloader | prompt-kit | Components | Free | no deps |
