import {useTheme} from '../../theme/ThemeProvider'; import {useEffect, useState} from 'react'; import { Platform, Switch as RNSwitch, StyleSheet, Text, TouchableOpacity, View, } from 'react-native'; import {warnDeprecated} from '../../helpers/deprecate'; import type {SwitchProps} from '../../types'; export const Switch = (props: SwitchProps) => { const {colors} = useTheme(); const { accessibilityHint, accessibilityLabel, backgroundColor, backgrounColor, border, borderColor, borderWidth = 2, isOn = false, fullWidth, justifyContent, onChange, testID, textStyle, text, } = props; const [isEnabled, setIsEnabled] = useState(isOn); const isWeb = Platform.OS === 'web'; if (backgrounColor !== undefined) { warnDeprecated( 'Switch: `backgrounColor` is deprecated, use `backgroundColor` instead.', ); } const onColor = backgroundColor ?? backgrounColor ?? colors.success; // Keep the internal state in sync when used as a controlled component. useEffect(() => { setIsEnabled(isOn); }, [isOn]); const toggleSwitch = () => { const next = !isEnabled; setIsEnabled(next); onChange?.(next); }; return ( {!!text && {text}} {isWeb ? ( ) : ( )} ); }; const styles = StyleSheet.create({ row: { flexDirection: 'row', alignItems: 'center', gap: 5, }, switchContainer: { width: 50, height: 30, borderRadius: 15, justifyContent: 'center', padding: 5, }, thumb: { width: 20, height: 20, borderRadius: 10, backgroundColor: '#FFF', }, });