|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | + |
| 3 | +// Hook to animate text content of React elements |
| 4 | +export const useAnimatedText = ( |
| 5 | + children: React.ReactNode, |
| 6 | + speed = 10, |
| 7 | + enabled = true, |
| 8 | +): { animatedChildren: React.ReactNode; isAnimating: boolean } => { |
| 9 | + const [animatedChildren, setAnimatedChildren] = useState<React.ReactNode>(); |
| 10 | + const [textIndex, setTextIndex] = useState(0); |
| 11 | + |
| 12 | + // Effect to increment textIndex at a specified speed |
| 13 | + useEffect(() => { |
| 14 | + const interval = setInterval(() => { |
| 15 | + setTextIndex((prevIndex) => prevIndex + 1); |
| 16 | + }, speed); |
| 17 | + |
| 18 | + return () => clearInterval(interval); |
| 19 | + }, [speed]); |
| 20 | + |
| 21 | + // Function to calculate the total length of text within nested elements |
| 22 | + const calculateTotalTextLength = (nodes: React.ReactNode): number => { |
| 23 | + let length = 0; |
| 24 | + React.Children.forEach(nodes, (child) => { |
| 25 | + if (typeof child === "string") { |
| 26 | + length += child.length; |
| 27 | + } else if (React.isValidElement(child)) { |
| 28 | + length += calculateTotalTextLength(child.props.children); |
| 29 | + } |
| 30 | + }); |
| 31 | + return length; |
| 32 | + }; |
| 33 | + |
| 34 | + // Function to recursively clone children and apply text animation |
| 35 | + const cloneChildren = (nodes: React.ReactNode, currentIndex: number): React.ReactNode => { |
| 36 | + let currentTextIndex = currentIndex; |
| 37 | + return React.Children.map(nodes, (child) => { |
| 38 | + if (typeof child === "string") { |
| 39 | + // Only include text nodes if their animation has started |
| 40 | + if (currentTextIndex > 0) { |
| 41 | + const visibleText = child.slice(0, currentTextIndex); |
| 42 | + currentTextIndex -= child.length; |
| 43 | + return visibleText; |
| 44 | + } |
| 45 | + return null; |
| 46 | + } else if (React.isValidElement(child)) { |
| 47 | + const totalChildTextLength = calculateTotalTextLength(child.props.children); |
| 48 | + // Only include elements if their text animation has started |
| 49 | + if (currentTextIndex > 0) { |
| 50 | + const clonedChild = React.cloneElement(child, {}, cloneChildren(child.props.children, currentTextIndex)); |
| 51 | + currentTextIndex -= totalChildTextLength; |
| 52 | + return clonedChild; |
| 53 | + } else if (currentTextIndex === 0 && totalChildTextLength === 0) { |
| 54 | + return child; |
| 55 | + } |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + return child; |
| 60 | + }); |
| 61 | + }; |
| 62 | + |
| 63 | + // Effect to update animated children based on the current text index |
| 64 | + useEffect(() => { |
| 65 | + if (enabled) { |
| 66 | + const totaLength = calculateTotalTextLength(children); |
| 67 | + if (textIndex <= totaLength) { |
| 68 | + setAnimatedChildren(cloneChildren(children, textIndex)); |
| 69 | + } |
| 70 | + } |
| 71 | + }, [children, textIndex, enabled]); |
| 72 | + |
| 73 | + return { |
| 74 | + animatedChildren: enabled ? animatedChildren : children, |
| 75 | + isAnimating: enabled && textIndex < calculateTotalTextLength(children), |
| 76 | + }; |
| 77 | +}; |
| 78 | + |
| 79 | +export default useAnimatedText; |
0 commit comments