import * as SwitchPrimitives from '@rn-primitives/switch'; import * as React from 'react'; import { Platform } from 'react-native'; import Animated, { interpolateColor, useAnimatedStyle, useDerivedValue, withTiming, } from 'react-native-reanimated'; import { useColorScheme } from '@/lib/theme/useColorScheme'; import { cn } from '@/lib/utils'; const SwitchWeb = React.forwardRef( ({ className, ...props }, ref) => ( ) ); SwitchWeb.displayName = 'SwitchWeb'; const RGB_COLORS = { light: { primary: 'rgb(24, 24, 27)', input: 'rgb(228, 228, 231)', }, dark: { primary: 'rgb(250, 250, 250)', input: 'rgb(39, 39, 42)', }, } as const; const SwitchNative = React.forwardRef( ({ className, ...props }, ref) => { const { colorScheme } = useColorScheme(); const translateX = useDerivedValue(() => (props.checked ? 18 : 0)); const animatedRootStyle = useAnimatedStyle(() => { return { backgroundColor: interpolateColor( translateX.value, [0, 18], [RGB_COLORS[colorScheme].input, RGB_COLORS[colorScheme].primary] ), }; }); const animatedThumbStyle = useAnimatedStyle(() => ({ transform: [{ translateX: withTiming(translateX.value, { duration: 200 }) }], })); return ( ); } ); SwitchNative.displayName = 'SwitchNative'; const Switch = Platform.select({ web: SwitchWeb, default: SwitchNative, }); export { Switch };