xref: /MusicFree/src/components/base/switch.tsx (revision 4da0658b1bf77de935d6a60ad1a26e4b92d13606)
1277c5280S猫头猫import React, {useEffect} from 'react';
2277c5280S猫头猫import {
3277c5280S猫头猫    StyleSheet,
4277c5280S猫头猫    SwitchProps,
5277c5280S猫头猫    TouchableWithoutFeedback,
6277c5280S猫头猫    View,
7277c5280S猫头猫} from 'react-native';
895c9734fS猫头猫import useColors from '@/hooks/useColors';
9277c5280S猫头猫import rpx from '@/utils/rpx';
10277c5280S猫头猫import Animated, {
11277c5280S猫头猫    useAnimatedStyle,
12277c5280S猫头猫    useSharedValue,
13277c5280S猫头猫    withTiming,
14277c5280S猫头猫} from 'react-native-reanimated';
15277c5280S猫头猫import {timingConfig} from '@/constants/commonConst';
1695c9734fS猫头猫
1795c9734fS猫头猫interface ISwitchProps extends SwitchProps {}
18*4da0658bSmaotoumao
19277c5280S猫头猫const fixedWidth = rpx(40);
20277c5280S猫头猫
2195c9734fS猫头猫export default function ThemeSwitch(props: ISwitchProps) {
22277c5280S猫头猫    const {value, onValueChange} = props;
2395c9734fS猫头猫    const colors = useColors();
24277c5280S猫头猫
25277c5280S猫头猫    const sharedValue = useSharedValue(value ? 1 : 0);
26277c5280S猫头猫
27277c5280S猫头猫    useEffect(() => {
28*4da0658bSmaotoumao        sharedValue.value = value ? 1 : 0;
29277c5280S猫头猫    }, [value]);
30277c5280S猫头猫
31277c5280S猫头猫    const thumbStyle = useAnimatedStyle(() => {
32277c5280S猫头猫        return {
33277c5280S猫头猫            transform: [
34277c5280S猫头猫                {
35*4da0658bSmaotoumao                    translateX: withTiming(
36*4da0658bSmaotoumao                        sharedValue.value * fixedWidth,
37*4da0658bSmaotoumao                        timingConfig.animationNormal,
38*4da0658bSmaotoumao                    ),
39277c5280S猫头猫                },
40277c5280S猫头猫            ],
41277c5280S猫头猫        };
42277c5280S猫头猫    });
43277c5280S猫头猫
4495c9734fS猫头猫    return (
45277c5280S猫头猫        <TouchableWithoutFeedback
46277c5280S猫头猫            onPress={() => {
47277c5280S猫头猫                onValueChange?.(!value);
48277c5280S猫头猫            }}>
49277c5280S猫头猫            <View
50277c5280S猫头猫                style={[
51277c5280S猫头猫                    styles.container,
52277c5280S猫头猫                    {
53277c5280S猫头猫                        backgroundColor: value
54277c5280S猫头猫                            ? colors.primary
55277c5280S猫头猫                            : colors.textSecondary,
56277c5280S猫头猫                    },
571131451eS猫头猫                    props?.style,
58277c5280S猫头猫                ]}>
59277c5280S猫头猫                <Animated.View style={[styles.thumb, thumbStyle]} />
60277c5280S猫头猫            </View>
61277c5280S猫头猫        </TouchableWithoutFeedback>
6295c9734fS猫头猫    );
6395c9734fS猫头猫}
64277c5280S猫头猫
65277c5280S猫头猫const styles = StyleSheet.create({
66277c5280S猫头猫    container: {
67277c5280S猫头猫        width: rpx(80),
68277c5280S猫头猫        height: rpx(40),
69277c5280S猫头猫        borderRadius: rpx(40),
70277c5280S猫头猫        justifyContent: 'center',
71277c5280S猫头猫    },
72277c5280S猫头猫    thumb: {
73277c5280S猫头猫        width: rpx(34),
74277c5280S猫头猫        height: rpx(34),
75277c5280S猫头猫        borderRadius: rpx(17),
76277c5280S猫头猫        backgroundColor: 'white',
77277c5280S猫头猫        left: rpx(3),
78277c5280S猫头猫    },
79277c5280S猫头猫});
80