Components / AI Chat Interfaces
AI Streaming Text
A typewriter-style streaming text component that simulates token-by-token AI response generation with cursor animation.
Components / AI Chat Interfaces
A typewriter-style streaming text component that simulates token-by-token AI response generation with cursor animation.
Install the core libraries required for this component.
npm install reactimport { useEffect, useState } from "react";
interface AIStreamingText01Props {
text?: string; // optional prop; can default to self-contained text
speed?: number; // typing speed in ms per character
}
export default function AIStreamingText01({
text = "Hello! I am ChatGPT, and I generate responses token by token to simulate streaming output for your AI chat interface.",
speed = 30,
}: AIStreamingText01Props) {
const [displayedText, setDisplayedText] = useState("");
const [index, setIndex] = useState(0);
useEffect(() => {
if (index < text.length) {
const timeout = setTimeout(() => {
setDisplayedText((prev) => prev + text.charAt(index));
setIndex(index + 1);
}, speed);
return () => clearTimeout(timeout);
}
}, [index, text, speed]);
return (
<div className="font-sans text-base whitespace-pre-wrap break-words">
{displayedText}
<span className="animate-pulse inline-block w-1 bg-foreground ml-1"> </span>
</div>
);
} import { useEffect, useState } from "react";
interface AIStreamingText01Props {
text?: string; // optional prop; can default to self-contained text
speed?: number; // typing speed in ms per character
}
export default function AIStreamingText01({
text = "Hello! I am ChatGPT, and I generate responses token by token to simulate streaming output for your AI chat interface.",
speed = 30,
}: AIStreamingText01Props) {
const [displayedText, setDisplayedText] = useState("");
const [index, setIndex] = useState(0);
useEffect(() => {
if (index < text.length) {
const timeout = setTimeout(() => {
setDisplayedText((prev) => prev + text.charAt(index));
setIndex(index + 1);
}, speed);
return () => clearTimeout(timeout);
}
}, [index, text, speed]);
return (
<div className="font-sans text-base whitespace-pre-wrap break-words">
{displayedText}
<span className="animate-pulse inline-block w-1 bg-foreground ml-1"> </span>
</div>
);
} Types and interfaces defined in this component.
interface AIStreamingText01Props {
text?: string; // optional prop; can default to self-contained text
speed?: number; // typing speed in ms per character
} interface AIStreamingText01Props {
text?: string; // optional prop; can default to self-contained text
speed?: number; // typing speed in ms per character
} Internal functions used to handle component logic.
| Function | Parameters |
|---|---|
| AIStreamingText01() | { text = "Hello! I am ChatGPT, and I generate responses token by token to simulate streaming output for your AI chat interface.", speed = 30, }: AIStreamingText01Props |
React state variables managed within this component.
| Variable | Initial Value |
|---|---|
| displayedText | "" |
| index | 0 |
All variable declarations found in this component.
| Name | Kind | Value |
|---|---|---|
| timeout | | setTimeout(() => { |
Every JSX/HTML tag used in this component, with usage count and source.
| Tag | Count | Type | Source |
|---|---|---|---|
| <div> | 1× | | Native HTML |
| <span> | 1× | | Native HTML |