xref: /MusicFree/src/components/base/switch.tsx (revision 1131451ed819ab870c19151c24ffbfcfa9968a09)
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 {}
18277c5280S猫头猫const fixedWidth = rpx(40);
19277c5280S猫头猫
2095c9734fS猫头猫export default function ThemeSwitch(props: ISwitchProps) {
21277c5280S猫头猫    const {value, onValueChange} = props;
2295c9734fS猫头猫    const colors = useColors();
23277c5280S猫头猫
24277c5280S猫头猫    const sharedValue = useSharedValue(value ? 1 : 0);
25277c5280S猫头猫
26277c5280S猫头猫    useEffect(() => {
27277c5280S猫头猫        sharedValue.value = withTiming(
28277c5280S猫头猫            value ? 1 : 0,
29277c5280S猫头猫            timingConfig.animationNormal,
30277c5280S猫头猫        );
31277c5280S猫头猫    }, [value]);
32277c5280S猫头猫
33277c5280S猫头猫    const thumbStyle = useAnimatedStyle(() => {
34277c5280S猫头猫        return {
35277c5280S猫头猫            transform: [
36277c5280S猫头猫                {
37277c5280S猫头猫                    translateX: sharedValue.value * fixedWidth,
38277c5280S猫头猫                },
39277c5280S猫头猫            ],
40277c5280S猫头猫        };
41277c5280S猫头猫    });
42277c5280S猫头猫
4395c9734fS猫头猫    return (
44277c5280S猫头猫        <TouchableWithoutFeedback
45277c5280S猫头猫            onPress={() => {
46277c5280S猫头猫                onValueChange?.(!value);
47277c5280S猫头猫            }}>
48277c5280S猫头猫            <View
49277c5280S猫头猫                style={[
50277c5280S猫头猫                    styles.container,
51277c5280S猫头猫                    {
52277c5280S猫头猫                        backgroundColor: value
53277c5280S猫头猫                            ? colors.primary
54277c5280S猫头猫                            : colors.textSecondary,
55277c5280S猫头猫                    },
56*1131451eS猫头猫                    props?.style,
57277c5280S猫头猫                ]}>
58277c5280S猫头猫                <Animated.View style={[styles.thumb, thumbStyle]} />
59277c5280S猫头猫            </View>
60277c5280S猫头猫        </TouchableWithoutFeedback>
6195c9734fS猫头猫    );
6295c9734fS猫头猫}
63277c5280S猫头猫
64277c5280S猫头猫const styles = StyleSheet.create({
65277c5280S猫头猫    container: {
66277c5280S猫头猫        width: rpx(80),
67277c5280S猫头猫        height: rpx(40),
68277c5280S猫头猫        borderRadius: rpx(40),
69277c5280S猫头猫        justifyContent: 'center',
70277c5280S猫头猫    },
71277c5280S猫头猫    thumb: {
72277c5280S猫头猫        width: rpx(34),
73277c5280S猫头猫        height: rpx(34),
74277c5280S猫头猫        borderRadius: rpx(17),
75277c5280S猫头猫        backgroundColor: 'white',
76277c5280S猫头猫        left: rpx(3),
77277c5280S猫头猫    },
78277c5280S猫头猫});
79