forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypewriter.tsx
More file actions
65 lines (56 loc) · 1.61 KB
/
Copy pathtypewriter.tsx
File metadata and controls
65 lines (56 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"use client";
import { useState, useEffect } from "react";
import ReactMarkdown from "react-markdown";
interface TypewriterProps {
text: string;
speed?: number;
className?: string;
}
export function Typewriter({
text,
speed = 30,
className = "",
}: TypewriterProps) {
const [displayedText, setDisplayedText] = useState("");
const [currentIndex, setCurrentIndex] = useState(0);
const [isComplete, setIsComplete] = useState(false);
const [showCursor, setShowCursor] = useState(true);
// Reset when text changes
useEffect(() => {
setDisplayedText("");
setCurrentIndex(0);
setIsComplete(false);
setShowCursor(true);
}, [text]);
// Typing effect
useEffect(() => {
if (currentIndex < text.length) {
const timeout = setTimeout(() => {
setDisplayedText((prev) => prev + text[currentIndex]);
setCurrentIndex((prev) => prev + 1);
}, speed);
return () => clearTimeout(timeout);
} else {
setIsComplete(true);
// Stop blinking cursor after typing is complete
setTimeout(() => setShowCursor(false), 1000);
}
}, [currentIndex, text, speed]);
// Blinking cursor effect
useEffect(() => {
if (!isComplete) {
const cursorInterval = setInterval(() => {
setShowCursor((prev) => !prev);
}, 1000);
return () => clearInterval(cursorInterval);
}
}, [isComplete]);
return (
<div className={className}>
<ReactMarkdown>{displayedText}</ReactMarkdown>
{showCursor && !isComplete && (
<span className="inline-block w-2 h-4 bg-blue-500 ml-1 animate-pulse"></span>
)}
</div>
);
}