|
| 1 | +import React, { ReactElement } from 'react'; |
| 2 | +import { Pressable, StyleSheet } from 'react-native'; |
| 3 | +import Animated, { |
| 4 | + Easing, |
| 5 | + interpolate, |
| 6 | + interpolateColor, |
| 7 | + useAnimatedStyle, |
| 8 | + useDerivedValue, |
| 9 | + useSharedValue, |
| 10 | + withTiming, |
| 11 | +} from 'react-native-reanimated'; |
| 12 | + |
| 13 | +import colors from '../styles/colors'; |
| 14 | +import { IThemeColors } from '../styles/themes'; |
| 15 | + |
| 16 | +const duration = 300; |
| 17 | +const defaultHeight = 32; |
| 18 | +const defaultWidth = 52; |
| 19 | + |
| 20 | +const Switch = ({ |
| 21 | + value, |
| 22 | + disabled, |
| 23 | + color, |
| 24 | + onValueChange, |
| 25 | +}: { |
| 26 | + value: boolean; |
| 27 | + disabled?: boolean; |
| 28 | + color?: keyof IThemeColors; |
| 29 | + onValueChange?: () => void; |
| 30 | +}): ReactElement => { |
| 31 | + const height = useSharedValue(defaultHeight); |
| 32 | + const width = useSharedValue(defaultWidth); |
| 33 | + const sharedValue = useDerivedValue(() => { |
| 34 | + return value ? 1 : 0; |
| 35 | + }); |
| 36 | + |
| 37 | + const thumbColor = disabled ? '#A0A0A0' : colors.white; |
| 38 | + const trackColor = color ? colors[color] : colors.brand; |
| 39 | + const trackColors = { on: trackColor, off: '#3A3A3C' }; |
| 40 | + |
| 41 | + const trackAnimatedStyle = useAnimatedStyle(() => { |
| 42 | + const animatedColor = interpolateColor( |
| 43 | + sharedValue.value, |
| 44 | + [0, 1], |
| 45 | + [trackColors.off, trackColors.on], |
| 46 | + ); |
| 47 | + const colorValue = withTiming(animatedColor, { |
| 48 | + duration, |
| 49 | + easing: Easing.inOut(Easing.ease), |
| 50 | + }); |
| 51 | + |
| 52 | + return { |
| 53 | + backgroundColor: colorValue, |
| 54 | + borderRadius: height.value / 2, |
| 55 | + }; |
| 56 | + }); |
| 57 | + |
| 58 | + const thumbAnimatedStyle = useAnimatedStyle(() => { |
| 59 | + const moveValue = interpolate( |
| 60 | + sharedValue.value, |
| 61 | + [0, 1], |
| 62 | + [0, width.value - height.value], |
| 63 | + ); |
| 64 | + const translateValue = withTiming(moveValue, { |
| 65 | + duration, |
| 66 | + easing: Easing.bezier(0.61, 0.46, 0.3, 1.07), |
| 67 | + }); |
| 68 | + |
| 69 | + return { |
| 70 | + transform: [{ translateX: translateValue }], |
| 71 | + borderRadius: height.value / 2, |
| 72 | + }; |
| 73 | + }); |
| 74 | + |
| 75 | + const onPress = (): void => { |
| 76 | + if (!disabled) { |
| 77 | + onValueChange?.(); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + return ( |
| 82 | + <Pressable onPress={onPress}> |
| 83 | + <Animated.View |
| 84 | + style={[styles.track, trackAnimatedStyle]} |
| 85 | + onLayout={(e) => { |
| 86 | + height.value = e.nativeEvent.layout.height; |
| 87 | + width.value = e.nativeEvent.layout.width; |
| 88 | + }}> |
| 89 | + <Animated.View |
| 90 | + style={[ |
| 91 | + styles.thumb, |
| 92 | + thumbAnimatedStyle, |
| 93 | + { backgroundColor: thumbColor }, |
| 94 | + ]} |
| 95 | + /> |
| 96 | + </Animated.View> |
| 97 | + </Pressable> |
| 98 | + ); |
| 99 | +}; |
| 100 | + |
| 101 | +const styles = StyleSheet.create({ |
| 102 | + track: { |
| 103 | + alignItems: 'flex-start', |
| 104 | + height: defaultHeight, |
| 105 | + width: defaultWidth, |
| 106 | + padding: 4, |
| 107 | + }, |
| 108 | + thumb: { |
| 109 | + height: '100%', |
| 110 | + aspectRatio: 1, |
| 111 | + }, |
| 112 | +}); |
| 113 | + |
| 114 | +export default Switch; |
0 commit comments