import React, { useEffect, useRef } from 'react'; import { View, Text, Animated } from 'react-native'; import { useTheme } from '../../theme/useTheme'; import type { GradientTextProps } from './GradientText.types'; export const GradientText: React.FC = ({ colors, animated = false, speed = 2000, style, children, }) => { const theme = useTheme(); const defaultColors = [ theme.colors.primary, theme.colors.secondary, '#EC4899', theme.colors.info, ]; const colorList = colors ?? defaultColors; const offsetAnim = useRef(new Animated.Value(0)).current; useEffect(() => { if (!animated) return; const loop = Animated.loop( Animated.timing(offsetAnim, { toValue: colorList.length, duration: speed, useNativeDriver: false, }), { resetBeforeIteration: true } ); loop.start(); return () => loop.stop(); }, [animated, speed, colorList.length, offsetAnim]); const chars = String(children).split(''); const n = colorList.length; return ( {chars.map((char, index) => { if (animated) { const inputRange = colorList.map((_, i) => i); const outputRange = inputRange.map( (_, offset) => colorList[(index + offset) % n] ); const colorInterpolation = offsetAnim.interpolate({ inputRange, outputRange, }); return ( {char} ); } const colorIndex = index % colorList.length; const color = colorList[colorIndex]; return ( {char} ); })} ); };