File size: 4,224 Bytes
50a8ea3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import React, { useState, useEffect } from 'react';

interface RotatingTextProps {
  texts: string[];
  transitionDuration?: number;
  rotationInterval?: number;
  staggerDuration?: number;
  staggerFrom?: 'first' | 'last' | 'center' | 'random';
  splitBy?: 'characters' | 'words' | 'lines';
  mainClassName?: string;
  splitLevelClassName?: string;
  elementLevelClassName?: string;
  animatePresence?: boolean;
}

export const RotatingText: React.FC<RotatingTextProps> = ({

  texts,

  rotationInterval = 2800,

  staggerDuration = 0.025,

  splitBy = 'characters',

  mainClassName = '',

  splitLevelClassName = '',

  elementLevelClassName = '',

}) => {
  const [currentIndex, setCurrentIndex] = useState(0);
  const [animationKey, setAnimationKey] = useState(0);

  useEffect(() => {
    if (!texts || texts.length <= 1) return;

    const interval = setInterval(() => {
      setCurrentIndex((prev) => (prev + 1) % texts.length);
      setAnimationKey((prev) => prev + 1);
    }, rotationInterval);

    return () => clearInterval(interval);
  }, [texts, rotationInterval]);

  const currentText = texts[currentIndex] || '';

  const renderElements = () => {
    if (splitBy === 'characters') {
      const words = currentText.split(' ');
      let charCounter = 0;

      return words.map((word, wordIndex) => (
        <span

          key={`word-${wordIndex}-${animationKey}`}

          className={`inline-flex whitespace-nowrap ${splitLevelClassName}`}

          style={{ transformStyle: 'preserve-3d' }}

        >

          {word.split('').map((char) => {

            const index = charCounter++;

            const delay = index * staggerDuration;



            return (

              <span

                key={`char-${index}-${animationKey}`}

                className={`inline-block will-change-transform ${elementLevelClassName}`}

                style={{

                  transformStyle: 'preserve-3d',

                  animation: `rotateCharIn 0.55s cubic-bezier(0.22, 1, 0.36, 1) ${delay}s both`,

                }}

              >

                {char}

              </span>

            );

          })}

        </span>
      ));
    }

    if (splitBy === 'words') {
      const words = currentText.split(' ');
      return words.map((word, wordIndex) => {
        const delay = wordIndex * (staggerDuration * 2.5);
        return (
          <span

            key={`word-${wordIndex}-${animationKey}`}

            className={`inline-block will-change-transform ${elementLevelClassName}`}

            style={{

              transformStyle: 'preserve-3d',

              animation: `rotateWordIn 0.5s cubic-bezier(0.22, 1, 0.36, 1) ${delay}s both`,

            }}

          >

            {word}

          </span>
        );
      });
    }

    return (
      <span

        key={`line-${animationKey}`}

        className={`inline-block will-change-transform ${elementLevelClassName}`}

        style={{

          transformStyle: 'preserve-3d',

          animation: 'rotateWordIn 0.5s cubic-bezier(0.22, 1, 0.36, 1) 0s both',

        }}

      >

        {currentText}

      </span>
    );
  };

  return (
    <span

      className={`inline-flex flex-wrap items-center justify-center gap-x-[0.25em] ${mainClassName}`}

      style={{ perspective: '1000px' }}

      aria-label={currentText}

    >

      {renderElements()}



      <style>{`

        @keyframes rotateCharIn {

          0% {

            opacity: 0;

            transform: translateY(120%) rotateX(-90deg);

            filter: blur(4px);

          }

          40% {

            opacity: 0.7;

          }

          100% {

            opacity: 1;

            transform: translateY(0%) rotateX(0deg);

            filter: blur(0px);

          }

        }

        @keyframes rotateWordIn {

          0% {

            opacity: 0;

            transform: translateY(100%) rotateX(-80deg);

            filter: blur(4px);

          }

          100% {

            opacity: 1;

            transform: translateY(0%) rotateX(0deg);

            filter: blur(0px);

          }

        }

      `}</style>

    </span>
  );
};