|
| 1 | +import { cn } from "@/lib/utils"; |
| 2 | +import { |
| 3 | + AnimatePresence, |
| 4 | + motion, |
| 5 | + Transition, |
| 6 | + type AnimationControls, |
| 7 | + type Target, |
| 8 | + type TargetAndTransition, |
| 9 | + type VariantLabels, |
| 10 | +} from "framer-motion"; |
| 11 | +import React, { |
| 12 | + forwardRef, |
| 13 | + useCallback, |
| 14 | + useEffect, |
| 15 | + useImperativeHandle, |
| 16 | + useMemo, |
| 17 | + useState, |
| 18 | +} from "react"; |
| 19 | + |
| 20 | +export interface RotatingTextRef { |
| 21 | + next: () => void; |
| 22 | + previous: () => void; |
| 23 | + jumpTo: (index: number) => void; |
| 24 | + reset: () => void; |
| 25 | +} |
| 26 | + |
| 27 | +export interface RotatingTextProps |
| 28 | + extends Omit< |
| 29 | + React.ComponentPropsWithoutRef<typeof motion.span>, |
| 30 | + "children" | "transition" | "initial" | "animate" | "exit" |
| 31 | + > { |
| 32 | + texts: string[]; |
| 33 | + transition?: Transition; |
| 34 | + initial?: boolean | Target | VariantLabels; |
| 35 | + animate?: boolean | VariantLabels | AnimationControls | TargetAndTransition; |
| 36 | + exit?: Target | VariantLabels; |
| 37 | + animatePresenceMode?: "sync" | "wait"; |
| 38 | + animatePresenceInitial?: boolean; |
| 39 | + rotationInterval?: number; |
| 40 | + staggerDuration?: number; |
| 41 | + staggerFrom?: "first" | "last" | "center" | "random" | number; |
| 42 | + loop?: boolean; |
| 43 | + auto?: boolean; |
| 44 | + splitBy?: string; |
| 45 | + onNext?: (index: number) => void; |
| 46 | + mainClassName?: string; |
| 47 | + splitLevelClassName?: string; |
| 48 | + elementLevelClassName?: string; |
| 49 | +} |
| 50 | + |
| 51 | +export const RotatingText = forwardRef<RotatingTextRef, RotatingTextProps>( |
| 52 | + ( |
| 53 | + { |
| 54 | + texts, |
| 55 | + transition = { type: "spring", damping: 25, stiffness: 300 }, |
| 56 | + initial = { y: "100%", opacity: 0 }, |
| 57 | + animate = { y: 0, opacity: 1 }, |
| 58 | + exit = { y: "-120%", opacity: 0 }, |
| 59 | + animatePresenceMode = "wait", |
| 60 | + animatePresenceInitial = false, |
| 61 | + rotationInterval = 2000, |
| 62 | + staggerDuration = 0, |
| 63 | + staggerFrom = "first", |
| 64 | + loop = true, |
| 65 | + auto = true, |
| 66 | + splitBy = "characters", |
| 67 | + onNext, |
| 68 | + mainClassName, |
| 69 | + splitLevelClassName, |
| 70 | + elementLevelClassName, |
| 71 | + ...rest |
| 72 | + }, |
| 73 | + ref, |
| 74 | + ) => { |
| 75 | + const [currentTextIndex, setCurrentTextIndex] = useState<number>(0); |
| 76 | + |
| 77 | + const splitIntoCharacters = (text: string): string[] => { |
| 78 | + if (typeof Intl !== "undefined" && Intl.Segmenter) { |
| 79 | + const segmenter = new Intl.Segmenter("en", { granularity: "grapheme" }); |
| 80 | + return Array.from( |
| 81 | + segmenter.segment(text), |
| 82 | + (segment) => segment.segment, |
| 83 | + ); |
| 84 | + } |
| 85 | + return Array.from(text); |
| 86 | + }; |
| 87 | + |
| 88 | + const elements = useMemo(() => { |
| 89 | + const currentText: string = texts[currentTextIndex]; |
| 90 | + if (splitBy === "characters") { |
| 91 | + const words = currentText.split(" "); |
| 92 | + return words.map((word, i) => ({ |
| 93 | + characters: splitIntoCharacters(word), |
| 94 | + needsSpace: i !== words.length - 1, |
| 95 | + })); |
| 96 | + } |
| 97 | + if (splitBy === "words") { |
| 98 | + return currentText.split(" ").map((word, i, arr) => ({ |
| 99 | + characters: [word], |
| 100 | + needsSpace: i !== arr.length - 1, |
| 101 | + })); |
| 102 | + } |
| 103 | + if (splitBy === "lines") { |
| 104 | + return currentText.split("\n").map((line, i, arr) => ({ |
| 105 | + characters: [line], |
| 106 | + needsSpace: i !== arr.length - 1, |
| 107 | + })); |
| 108 | + } |
| 109 | + |
| 110 | + return currentText.split(splitBy).map((part, i, arr) => ({ |
| 111 | + characters: [part], |
| 112 | + needsSpace: i !== arr.length - 1, |
| 113 | + })); |
| 114 | + }, [texts, currentTextIndex, splitBy]); |
| 115 | + |
| 116 | + const getStaggerDelay = useCallback( |
| 117 | + (index: number, totalChars: number): number => { |
| 118 | + const total = totalChars; |
| 119 | + if (staggerFrom === "first") return index * staggerDuration; |
| 120 | + if (staggerFrom === "last") |
| 121 | + return (total - 1 - index) * staggerDuration; |
| 122 | + if (staggerFrom === "center") { |
| 123 | + const center = Math.floor(total / 2); |
| 124 | + return Math.abs(center - index) * staggerDuration; |
| 125 | + } |
| 126 | + if (staggerFrom === "random") { |
| 127 | + const randomIndex = Math.floor(Math.random() * total); |
| 128 | + return Math.abs(randomIndex - index) * staggerDuration; |
| 129 | + } |
| 130 | + return Math.abs((staggerFrom as number) - index) * staggerDuration; |
| 131 | + }, |
| 132 | + [staggerFrom, staggerDuration], |
| 133 | + ); |
| 134 | + |
| 135 | + const handleIndexChange = useCallback( |
| 136 | + (newIndex: number) => { |
| 137 | + setCurrentTextIndex(newIndex); |
| 138 | + if (onNext) onNext(newIndex); |
| 139 | + }, |
| 140 | + [onNext], |
| 141 | + ); |
| 142 | + |
| 143 | + const next = useCallback(() => { |
| 144 | + const nextIndex = |
| 145 | + currentTextIndex === texts.length - 1 |
| 146 | + ? loop |
| 147 | + ? 0 |
| 148 | + : currentTextIndex |
| 149 | + : currentTextIndex + 1; |
| 150 | + if (nextIndex !== currentTextIndex) { |
| 151 | + handleIndexChange(nextIndex); |
| 152 | + } |
| 153 | + }, [currentTextIndex, texts.length, loop, handleIndexChange]); |
| 154 | + |
| 155 | + const previous = useCallback(() => { |
| 156 | + const prevIndex = |
| 157 | + currentTextIndex === 0 |
| 158 | + ? loop |
| 159 | + ? texts.length - 1 |
| 160 | + : currentTextIndex |
| 161 | + : currentTextIndex - 1; |
| 162 | + if (prevIndex !== currentTextIndex) { |
| 163 | + handleIndexChange(prevIndex); |
| 164 | + } |
| 165 | + }, [currentTextIndex, texts.length, loop, handleIndexChange]); |
| 166 | + |
| 167 | + const jumpTo = useCallback( |
| 168 | + (index: number) => { |
| 169 | + const validIndex = Math.max(0, Math.min(index, texts.length - 1)); |
| 170 | + if (validIndex !== currentTextIndex) { |
| 171 | + handleIndexChange(validIndex); |
| 172 | + } |
| 173 | + }, |
| 174 | + [texts.length, currentTextIndex, handleIndexChange], |
| 175 | + ); |
| 176 | + |
| 177 | + const reset = useCallback(() => { |
| 178 | + if (currentTextIndex !== 0) { |
| 179 | + handleIndexChange(0); |
| 180 | + } |
| 181 | + }, [currentTextIndex, handleIndexChange]); |
| 182 | + |
| 183 | + useImperativeHandle( |
| 184 | + ref, |
| 185 | + () => ({ |
| 186 | + next, |
| 187 | + previous, |
| 188 | + jumpTo, |
| 189 | + reset, |
| 190 | + }), |
| 191 | + [next, previous, jumpTo, reset], |
| 192 | + ); |
| 193 | + |
| 194 | + useEffect(() => { |
| 195 | + if (!auto) return; |
| 196 | + const intervalId = setInterval(next, rotationInterval); |
| 197 | + return () => clearInterval(intervalId); |
| 198 | + }, [next, rotationInterval, auto]); |
| 199 | + |
| 200 | + return ( |
| 201 | + <motion.span |
| 202 | + className={cn( |
| 203 | + "flex flex-wrap whitespace-pre-wrap relative", |
| 204 | + mainClassName, |
| 205 | + )} |
| 206 | + {...rest} |
| 207 | + layout |
| 208 | + transition={transition} |
| 209 | + > |
| 210 | + <span className="sr-only">{texts[currentTextIndex]}</span> |
| 211 | + <AnimatePresence |
| 212 | + mode={animatePresenceMode} |
| 213 | + initial={animatePresenceInitial} |
| 214 | + > |
| 215 | + <motion.div |
| 216 | + key={currentTextIndex} |
| 217 | + className={cn( |
| 218 | + splitBy === "lines" |
| 219 | + ? "flex flex-col w-full" |
| 220 | + : "flex flex-wrap whitespace-pre-wrap relative", |
| 221 | + )} |
| 222 | + layout |
| 223 | + aria-hidden="true" |
| 224 | + > |
| 225 | + {elements.map((wordObj, wordIndex, array) => { |
| 226 | + const previousCharsCount = array |
| 227 | + .slice(0, wordIndex) |
| 228 | + .reduce((sum, word) => sum + word.characters.length, 0); |
| 229 | + return ( |
| 230 | + <span |
| 231 | + key={wordIndex} |
| 232 | + className={cn("inline-flex", splitLevelClassName)} |
| 233 | + > |
| 234 | + {wordObj.characters.map((char, charIndex) => ( |
| 235 | + <motion.span |
| 236 | + key={charIndex} |
| 237 | + initial={initial} |
| 238 | + animate={animate} |
| 239 | + exit={exit} |
| 240 | + transition={{ |
| 241 | + ...transition, |
| 242 | + delay: getStaggerDelay( |
| 243 | + previousCharsCount + charIndex, |
| 244 | + array.reduce( |
| 245 | + (sum, word) => sum + word.characters.length, |
| 246 | + 0, |
| 247 | + ), |
| 248 | + ), |
| 249 | + }} |
| 250 | + className={cn("inline-block", elementLevelClassName)} |
| 251 | + > |
| 252 | + {char} |
| 253 | + </motion.span> |
| 254 | + ))} |
| 255 | + {wordObj.needsSpace && ( |
| 256 | + <span className="whitespace-pre"> </span> |
| 257 | + )} |
| 258 | + </span> |
| 259 | + ); |
| 260 | + })} |
| 261 | + </motion.div> |
| 262 | + </AnimatePresence> |
| 263 | + </motion.span> |
| 264 | + ); |
| 265 | + }, |
| 266 | +); |
| 267 | + |
| 268 | +RotatingText.displayName = "RotatingText"; |
0 commit comments