import React, { useEffect } from 'react'; import { Animated, Pressable, StyleSheet, I18nManager, View, } from 'react-native'; import { withTheme, useTheme } from '../../core/theming'; import type { Theme } from '../../utils/types'; interface SwitchProps { onValueChange?: () => void; switchSize: number; isActive: boolean; activeColor?: string; inActiveColor?: string; knobColor?: string; isDisabled?: boolean; theme: Theme; } const TSwitch: React.FC = ({ onValueChange, switchSize, isActive, activeColor, inActiveColor, isDisabled = false, }): any => { const switchHeight = 24; const switchWidth = 42; const { colors } = useTheme(); const trackWidth = switchWidth; const trackHeight = switchHeight; const knobSize = isActive ? switchHeight - 2 : switchHeight - 4; const knobAnim = React.useRef(new Animated.Value(0)).current; const knobPos = I18nManager.isRTL ? isActive ? -36 / 4 : 34 / 4 : isActive ? 36 / 4 : -34 / 4; const trackAnim = React.useRef(new Animated.Value(0)).current; const trackColor = isActive ? 75 : -75; const interpolatedColorAnimation = trackAnim.interpolate({ inputRange: [-75, 75], outputRange: [ (inActiveColor = inActiveColor || colors.white), (activeColor = activeColor || colors.black), ], }); const knobColorAnimation = trackAnim.interpolate({ inputRange: [-75, 75], outputRange: [colors.black, colors.white], }); useEffect(() => { Animated.parallel([ Animated.spring(knobAnim, { toValue: knobPos, useNativeDriver: false, }), Animated.timing(trackAnim, { toValue: trackColor, duration: 200, useNativeDriver: false, }), ]).start(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [isActive]); const handleSwitch = () => { if (isDisabled) { return; } if (onValueChange) { onValueChange(); } }; const stylesWithProp = styles(); return ( ); }; export default withTheme(TSwitch); const styles = () => StyleSheet.create({ container: { alignItems: 'center', justifyContent: 'center', }, trackView: { position: 'absolute', borderWidth: 1, }, knobContainer: { alignItems: 'center', justifyContent: 'center', }, });