1"use client";
2
3import { useEffect, useRef, useState } from "react";
4import { useInView } from "motion/react";
5
6interface SpecialTextProps {
7 children: string;
8 speed?: number;
9 delay?: number;
10 className?: string;
11 inView?: boolean;
12 once?: boolean;
13}
14
15const RANDOM_CHARS = "_!X$0-+*#";
16
17function getRandomChar(prevChar?: string): string {
18 let char: string;
19 do {
20 char = RANDOM_CHARS[Math.floor(Math.random() * RANDOM_CHARS.length)];
21 } while (char === prevChar);
22 return char;
23}
24
25export function SpecialText({
26 children,
27 speed = 20,
28 delay = 0,
29 className = "",
30 inView = false,
31 once = true,
32}: SpecialTextProps) {
33 const containerRef = useRef<HTMLSpanElement>(null);
34 const isInView = useInView(containerRef, { once, margin: "-100px" });
35 const shouldAnimate = inView ? isInView : true;
36 const [hasStarted, setHasStarted] = useState(() => !inView && delay <= 0);
37 const text = children;
38 const [displayText, setDisplayText] = useState<string>(
39 " ".repeat(text.length),
40 );
41 const [currentPhase, setCurrentPhase] = useState<"phase1" | "phase2">(
42 "phase1",
43 );
44 const [animationStep, setAnimationStep] = useState<number>(0);
45 const intervalRef = useRef<NodeJS.Timeout | null>(null);
46 const startTimeoutRef = useRef<number | null>(null);
47
48 function clearStartTimeout() {
49 if (startTimeoutRef.current === null) return;
50 window.clearTimeout(startTimeoutRef.current);
51 startTimeoutRef.current = null;
52 }
53
54 function startAnimation() {
55 setHasStarted(true);
56 setDisplayText(" ".repeat(text.length));
57 setCurrentPhase("phase1");
58 setAnimationStep(0);
59 }
60
61 const runPhase1 = () => {
62 const maxSteps = text.length * 2;
63 const currentLength = Math.min(animationStep + 1, text.length);
64
65 const chars: string[] = [];
66 for (let i = 0; i < currentLength; i++) {
67 const prevChar = i > 0 ? chars[i - 1] : undefined;
68 chars.push(getRandomChar(prevChar));
69 }
70
71 for (let i = currentLength; i < text.length; i++) {
72 chars.push("\u00A0");
73 }
74
75 setDisplayText(chars.join(""));
76
77 if (animationStep < maxSteps - 1) {
78 setAnimationStep((prev) => prev + 1);
79 } else {
80 setCurrentPhase("phase2");
81 setAnimationStep(0);
82 }
83 };
84
85 const runPhase2 = () => {
86 const revealedCount = Math.floor(animationStep / 2);
87 const chars: string[] = [];
88
89 for (let i = 0; i < revealedCount && i < text.length; i++) {
90 chars.push(text[i]);
91 }
92
93 if (revealedCount < text.length) {
94 if (animationStep % 2 === 0) {
95 chars.push("_");
96 } else {
97 chars.push(getRandomChar());
98 }
99 }
100
101 for (let i = chars.length; i < text.length; i++) {
102 chars.push(getRandomChar());
103 }
104
105 setDisplayText(chars.join(""));
106
107 if (animationStep < text.length * 2 - 1) {
108 setAnimationStep((prev) => prev + 1);
109 } else {
110 setDisplayText(text);
111 if (intervalRef.current) {
112 clearInterval(intervalRef.current);
113 intervalRef.current = null;
114 }
115 }
116 };
117
118 useEffect(() => {
119 if (shouldAnimate && !hasStarted) {
120 clearStartTimeout();
121 if (delay <= 0) {
122 startAnimation();
123 return;
124 }
125 startTimeoutRef.current = window.setTimeout(() => {
126 startTimeoutRef.current = null;
127 startAnimation();
128 }, delay * 1000);
129 }
130 return () => clearStartTimeout();
131 }, [shouldAnimate, hasStarted, delay, text.length]);
132
133 useEffect(() => {
134 if (!hasStarted) {
135 return;
136 }
137
138 if (intervalRef.current) {
139 clearInterval(intervalRef.current);
140 }
141
142 intervalRef.current = setInterval(() => {
143 if (currentPhase === "phase1") {
144 runPhase1();
145 } else {
146 runPhase2();
147 }
148 }, speed);
149
150 return () => {
151 if (intervalRef.current) {
152 clearInterval(intervalRef.current);
153 }
154 };
155 }, [currentPhase, animationStep, text, speed, hasStarted]);
156
157 useEffect(() => {
158 if (hasStarted) {
159 setDisplayText(" ".repeat(text.length));
160 setCurrentPhase("phase1");
161 setAnimationStep(0);
162 }
163
164 return () => {
165 clearStartTimeout();
166 if (intervalRef.current) {
167 clearInterval(intervalRef.current);
168 }
169 };
170 }, [text, hasStarted]);
171
172 return (
173 <span
174 ref={containerRef}
175 className={`h-4.5 leading-5 inline-flex font-mono font-medium ${className}`}
176 >
177 {displayText}
178 </span>
179 );
180}
181