xref: /MusicFree/src/components/base/switch.tsx (revision 277c528005b29b919b3eda695ee03717976a5a83)
1*277c5280S猫头猫import React, {useEffect} from 'react';
2*277c5280S猫头猫import {
3*277c5280S猫头猫    StyleSheet,
4*277c5280S猫头猫    SwitchProps,
5*277c5280S猫头猫    TouchableWithoutFeedback,
6*277c5280S猫头猫    View,
7*277c5280S猫头猫} from 'react-native';
895c9734fS猫头猫import useColors from '@/hooks/useColors';
9*277c5280S猫头猫import rpx from '@/utils/rpx';
10*277c5280S猫头猫import Animated, {
11*277c5280S猫头猫    useAnimatedStyle,
12*277c5280S猫头猫    useSharedValue,
13*277c5280S猫头猫    withTiming,
14*277c5280S猫头猫} from 'react-native-reanimated';
15*277c5280S猫头猫import {timingConfig} from '@/constants/commonConst';
1695c9734fS猫头猫
1795c9734fS猫头猫interface ISwitchProps extends SwitchProps {}
18*277c5280S猫头猫const fixedWidth = rpx(40);
19*277c5280S猫头猫
2095c9734fS猫头猫export default function ThemeSwitch(props: ISwitchProps) {
21*277c5280S猫头猫    const {value, onValueChange} = props;
2295c9734fS猫头猫    const colors = useColors();
23*277c5280S猫头猫
24*277c5280S猫头猫    const sharedValue = useSharedValue(value ? 1 : 0);
25*277c5280S猫头猫
26*277c5280S猫头猫    useEffect(() => {
27*277c5280S猫头猫        sharedValue.value = withTiming(
28*277c5280S猫头猫            value ? 1 : 0,
29*277c5280S猫头猫            timingConfig.animationNormal,
30*277c5280S猫头猫        );
31*277c5280S猫头猫    }, [value]);
32*277c5280S猫头猫
33*277c5280S猫头猫    const thumbStyle = useAnimatedStyle(() => {
34*277c5280S猫头猫        return {
35*277c5280S猫头猫            transform: [
36*277c5280S猫头猫                {
37*277c5280S猫头猫                    translateX: sharedValue.value * fixedWidth,
38*277c5280S猫头猫                },
39*277c5280S猫头猫            ],
40*277c5280S猫头猫        };
41*277c5280S猫头猫    });
42*277c5280S猫头猫
4395c9734fS猫头猫    return (
44*277c5280S猫头猫        <TouchableWithoutFeedback
45*277c5280S猫头猫            onPress={() => {
46*277c5280S猫头猫                onValueChange?.(!value);
47*277c5280S猫头猫            }}>
48*277c5280S猫头猫            <View
49*277c5280S猫头猫                style={[
50*277c5280S猫头猫                    styles.container,
51*277c5280S猫头猫                    {
52*277c5280S猫头猫                        backgroundColor: value
53*277c5280S猫头猫                            ? colors.primary
54*277c5280S猫头猫                            : colors.textSecondary,
55*277c5280S猫头猫                    },
56*277c5280S猫头猫                ]}>
57*277c5280S猫头猫                <Animated.View style={[styles.thumb, thumbStyle]} />
58*277c5280S猫头猫            </View>
59*277c5280S猫头猫        </TouchableWithoutFeedback>
6095c9734fS猫头猫    );
6195c9734fS猫头猫}
62*277c5280S猫头猫
63*277c5280S猫头猫const styles = StyleSheet.create({
64*277c5280S猫头猫    container: {
65*277c5280S猫头猫        width: rpx(80),
66*277c5280S猫头猫        height: rpx(40),
67*277c5280S猫头猫        borderRadius: rpx(40),
68*277c5280S猫头猫        justifyContent: 'center',
69*277c5280S猫头猫    },
70*277c5280S猫头猫    thumb: {
71*277c5280S猫头猫        width: rpx(34),
72*277c5280S猫头猫        height: rpx(34),
73*277c5280S猫头猫        borderRadius: rpx(17),
74*277c5280S猫头猫        backgroundColor: 'white',
75*277c5280S猫头猫        left: rpx(3),
76*277c5280S猫头猫    },
77*277c5280S猫头猫});
78