|
| 1 | +import { |
| 2 | + motion, |
| 3 | + useAnimationFrame, |
| 4 | + useMotionValue, |
| 5 | + useTransform, |
| 6 | +} from 'motion/react'; |
| 7 | +import type React from 'react'; |
| 8 | +import { useCallback, useEffect, useRef, useState } from 'react'; |
| 9 | + |
| 10 | +interface ShinyTextProps { |
| 11 | + text: string; |
| 12 | + disabled?: boolean; |
| 13 | + speed?: number; |
| 14 | + className?: string; |
| 15 | + color?: string; |
| 16 | + shineColor?: string; |
| 17 | + spread?: number; |
| 18 | + yoyo?: boolean; |
| 19 | + pauseOnHover?: boolean; |
| 20 | + direction?: 'left' | 'right'; |
| 21 | + delay?: number; |
| 22 | +} |
| 23 | + |
| 24 | +const ShinyText: React.FC<ShinyTextProps> = ({ |
| 25 | + text, |
| 26 | + disabled = false, |
| 27 | + speed = 2, |
| 28 | + className = '', |
| 29 | + color = '#b5b5b5', |
| 30 | + shineColor = '#ffffff', |
| 31 | + spread = 120, |
| 32 | + yoyo = false, |
| 33 | + pauseOnHover = false, |
| 34 | + direction = 'left', |
| 35 | + delay = 0, |
| 36 | +}) => { |
| 37 | + const [isPaused, setIsPaused] = useState(false); |
| 38 | + const progress = useMotionValue(0); |
| 39 | + const elapsedRef = useRef(0); |
| 40 | + const lastTimeRef = useRef<number | null>(null); |
| 41 | + const directionRef = useRef(direction === 'left' ? 1 : -1); |
| 42 | + |
| 43 | + const animationDuration = speed * 1000; |
| 44 | + const delayDuration = delay * 1000; |
| 45 | + |
| 46 | + useAnimationFrame((time) => { |
| 47 | + if (disabled || isPaused) { |
| 48 | + lastTimeRef.current = null; |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if (lastTimeRef.current === null) { |
| 53 | + lastTimeRef.current = time; |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const deltaTime = time - lastTimeRef.current; |
| 58 | + lastTimeRef.current = time; |
| 59 | + |
| 60 | + elapsedRef.current += deltaTime; |
| 61 | + |
| 62 | + // Animation goes from 0 to 100 |
| 63 | + if (yoyo) { |
| 64 | + const cycleDuration = animationDuration + delayDuration; |
| 65 | + const fullCycle = cycleDuration * 2; |
| 66 | + const cycleTime = elapsedRef.current % fullCycle; |
| 67 | + |
| 68 | + if (cycleTime < animationDuration) { |
| 69 | + // Forward animation: 0 -> 100 |
| 70 | + const p = (cycleTime / animationDuration) * 100; |
| 71 | + progress.set(directionRef.current === 1 ? p : 100 - p); |
| 72 | + } else if (cycleTime < cycleDuration) { |
| 73 | + // Delay at end |
| 74 | + progress.set(directionRef.current === 1 ? 100 : 0); |
| 75 | + } else if (cycleTime < cycleDuration + animationDuration) { |
| 76 | + // Reverse animation: 100 -> 0 |
| 77 | + const reverseTime = cycleTime - cycleDuration; |
| 78 | + const p = 100 - (reverseTime / animationDuration) * 100; |
| 79 | + progress.set(directionRef.current === 1 ? p : 100 - p); |
| 80 | + } else { |
| 81 | + // Delay at start |
| 82 | + progress.set(directionRef.current === 1 ? 0 : 100); |
| 83 | + } |
| 84 | + } else { |
| 85 | + const cycleDuration = animationDuration + delayDuration; |
| 86 | + const cycleTime = elapsedRef.current % cycleDuration; |
| 87 | + |
| 88 | + if (cycleTime < animationDuration) { |
| 89 | + // Animation phase: 0 -> 100 |
| 90 | + const p = (cycleTime / animationDuration) * 100; |
| 91 | + progress.set(directionRef.current === 1 ? p : 100 - p); |
| 92 | + } else { |
| 93 | + // Delay phase - hold at end (shine off-screen) |
| 94 | + progress.set(directionRef.current === 1 ? 100 : 0); |
| 95 | + } |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + // biome-ignore lint/correctness/useExhaustiveDependencies: progress.set is stable |
| 100 | + useEffect(() => { |
| 101 | + directionRef.current = direction === 'left' ? 1 : -1; |
| 102 | + elapsedRef.current = 0; |
| 103 | + progress.set(0); |
| 104 | + }, [direction]); |
| 105 | + |
| 106 | + // Transform: p=0 -> 150% (shine off right), p=100 -> -50% (shine off left) |
| 107 | + const backgroundPosition = useTransform( |
| 108 | + progress, |
| 109 | + (p) => `${150 - p * 2}% center`, |
| 110 | + ); |
| 111 | + |
| 112 | + const handleMouseEnter = useCallback(() => { |
| 113 | + if (pauseOnHover) setIsPaused(true); |
| 114 | + }, [pauseOnHover]); |
| 115 | + |
| 116 | + const handleMouseLeave = useCallback(() => { |
| 117 | + if (pauseOnHover) setIsPaused(false); |
| 118 | + }, [pauseOnHover]); |
| 119 | + |
| 120 | + const gradientStyle: React.CSSProperties = { |
| 121 | + backgroundImage: `linear-gradient(${spread}deg, ${color} 0%, ${color} 35%, ${shineColor} 50%, ${color} 65%, ${color} 100%)`, |
| 122 | + backgroundSize: '200% auto', |
| 123 | + WebkitBackgroundClip: 'text', |
| 124 | + backgroundClip: 'text', |
| 125 | + WebkitTextFillColor: 'transparent', |
| 126 | + }; |
| 127 | + |
| 128 | + return ( |
| 129 | + <motion.span |
| 130 | + className={`inline-block ${className}`} |
| 131 | + style={{ ...gradientStyle, backgroundPosition }} |
| 132 | + onMouseEnter={handleMouseEnter} |
| 133 | + onMouseLeave={handleMouseLeave} |
| 134 | + > |
| 135 | + {text} |
| 136 | + </motion.span> |
| 137 | + ); |
| 138 | +}; |
| 139 | + |
| 140 | +export default ShinyText; |
0 commit comments