File size: 1,395 Bytes
d530f14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useEffect, useState } from 'react';

interface ScrambleTextProps {
  text: string;
  className?: string;
  duration?: number;
  delay?: number;
  isInView?: boolean;
}

export default function ScrambleText({ text, className = '', duration = 1, delay = 0, isInView = true }: ScrambleTextProps) {
  const [displayText, setDisplayText] = useState(text);
  const [isScrambling, setIsScrambling] = useState(false);

  useEffect(() => {
    if (isScrambling) {
      const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
      const durationMs = duration * 1000; // Convert seconds to milliseconds
      const interval = setInterval(() => {
        setDisplayText(
          text
            .split('')
            .map((char) => (Math.random() > 0.5 ? chars[Math.floor(Math.random() * chars.length)] : char))
            .join('')
        );
      }, 50);

      setTimeout(() => {
        clearInterval(interval);
        setDisplayText(text);
        setIsScrambling(false);
      }, durationMs);

      return () => clearInterval(interval);
    }
  }, [text, isScrambling, duration]);

  useEffect(() => {
    if (isInView) {
      const timeout = setTimeout(() => {
        setIsScrambling(true);
      }, delay);
      return () => clearTimeout(timeout);
    }
  }, [text, delay, isInView]);

  return <span className={className}>{displayText}</span>;
}