Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | 2x 2x 2x 2x 2x 2x 15x 15x 15x 15x 15x 15x 15x 2x 15x 15x 15x 15x 15x | import { useEffect, useRef } from "react";
import {
Animated,
Pressable,
Text,
TextStyle,
View,
ViewStyle,
} from "react-native";
import { makeStyles } from "@repo/ui/themes/makeStyles";
import { useTheme } from "@repo/ui/themes/ThemeContext";
export interface CustomSwitchProps {
value: boolean;
onValueChange: (value: boolean) => void;
label?: string;
disabled?: boolean;
disableLabelStyle?: boolean;
containerStyle?: ViewStyle;
labelStyle?: TextStyle;
errorMessage?: string;
testID?: string;
}
const TRACK_WIDTH = 51;
const TRACK_HEIGHT = 28;
const THUMB_SIZE = 24;
const THUMB_MARGIN = 2;
const THUMB_TRAVEL = TRACK_WIDTH - THUMB_SIZE - THUMB_MARGIN * 2;
export const Switch = ({
value,
onValueChange,
label,
disabled = false,
containerStyle,
labelStyle,
testID,
errorMessage,
}: CustomSwitchProps) => {
const { theme } = useTheme();
const styles = useStyles();
const resolvedValue = Boolean(value);
const animValue = useRef(new Animated.Value(resolvedValue ? 1 : 0)).current;
useEffect(() => {
Animated.timing(animValue, {
toValue: resolvedValue ? 1 : 0,
duration: 200,
useNativeDriver: false,
}).start();
}, [animValue, resolvedValue]);
const handlePress = () => {
Eif (!disabled) onValueChange(!resolvedValue);
};
const trackColor = animValue.interpolate({
inputRange: [0, 1],
outputRange: [theme.colors.gray40, theme.colors.blue80],
});
const thumbTranslate = animValue.interpolate({
inputRange: [0, 1],
outputRange: [THUMB_MARGIN, THUMB_MARGIN + THUMB_TRAVEL],
});
const thumbStyle = {
transform: [{ translateX: thumbTranslate }],
shadowColor: "#000",
shadowOffset: { width: 0, height: 2 },
shadowOpacity: disabled ? 0.1 : 0.25,
shadowRadius: 2.5,
elevation: 2,
};
return (
<View style={[styles.container, containerStyle]}>
{label ? <Text style={[styles.label, labelStyle]}>{label}</Text> : null}
<Pressable
testID={testID}
onPress={handlePress}
disabled={disabled}
accessibilityRole="switch"
accessibilityLabel={label}
accessibilityState={{ checked: resolvedValue, disabled }}
hitSlop={{ top: 8, bottom: 8, left: 8, right: 8 }}
>
<Animated.View
style={[
styles.track,
{ backgroundColor: trackColor },
disabled &&
(resolvedValue
? styles.trackDisabledActive
: styles.trackDisabled),
]}
>
<Animated.View
style={[styles.thumb, thumbStyle, disabled && styles.thumbDisabled]}
/>
</Animated.View>
</Pressable>
{errorMessage && (
<Text style={styles.error} accessibilityLiveRegion="polite">
{errorMessage}
</Text>
)}
</View>
);
};
const useStyles = makeStyles((theme) => ({
container: {
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
gap: theme.metrics.spacing[2],
marginVertical: theme.metrics.spacing[1],
},
label: {
fontSize: theme.metrics.textSize[16],
color: theme.colors.text.info,
flex: 1,
flexShrink: 1,
},
track: {
width: TRACK_WIDTH,
height: TRACK_HEIGHT,
borderRadius: TRACK_HEIGHT / 2,
justifyContent: "center",
},
thumb: {
position: "absolute",
width: THUMB_SIZE,
height: THUMB_SIZE,
borderRadius: THUMB_SIZE / 2,
backgroundColor: theme.colors.white,
},
error: {
position: "absolute",
bottom: -theme.metrics.spacing[4.5],
fontSize: theme.metrics.textSize[12],
color: theme.colors.text.error,
},
trackDisabled: {
backgroundColor: theme.colors.gray30,
},
trackDisabledActive: {
backgroundColor: theme.colors.blue70,
},
thumbDisabled: {
backgroundColor: theme.colors.gray10,
},
}));
|