@towsila/expo-ui-library 1.5.13 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/components/ui/toggle.tsx +94 -0
package/package.json
CHANGED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { useRef, useEffect } from "react";
|
|
2
|
+
import { Pressable, Animated, StyleSheet } from "react-native";
|
|
3
|
+
import { useAppTheme } from "../../context/ThemeContext";
|
|
4
|
+
|
|
5
|
+
interface ToggleCardProps {
|
|
6
|
+
value: boolean;
|
|
7
|
+
onValueChange: (value: boolean) => void;
|
|
8
|
+
activeColor?: string;
|
|
9
|
+
inactiveColor?: string;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const TRACK_WIDTH = 44;
|
|
14
|
+
const TRACK_HEIGHT = 22;
|
|
15
|
+
const THUMB_SIZE = 16;
|
|
16
|
+
const THUMB_PADDING = (TRACK_HEIGHT - THUMB_SIZE) / 2; // keeps thumb centered inside track
|
|
17
|
+
|
|
18
|
+
export default function ToggleCard({
|
|
19
|
+
value,
|
|
20
|
+
onValueChange,
|
|
21
|
+
activeColor,
|
|
22
|
+
inactiveColor,
|
|
23
|
+
disabled = false,
|
|
24
|
+
}: ToggleCardProps) {
|
|
25
|
+
const { colors } = useAppTheme();
|
|
26
|
+
const tint = value ? (activeColor ?? colors.semantic.success) : (inactiveColor ?? colors.semantic.border);
|
|
27
|
+
|
|
28
|
+
const anim = useRef(new Animated.Value(value ? 1 : 0)).current;
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
Animated.timing(anim, {
|
|
32
|
+
toValue: value ? 1 : 0,
|
|
33
|
+
duration: 180,
|
|
34
|
+
useNativeDriver: false, // animating backgroundColor/left needs JS driver
|
|
35
|
+
}).start();
|
|
36
|
+
}, [value]);
|
|
37
|
+
|
|
38
|
+
const thumbLeft = anim.interpolate({
|
|
39
|
+
inputRange: [0, 1],
|
|
40
|
+
outputRange: [THUMB_PADDING, TRACK_WIDTH - THUMB_SIZE - THUMB_PADDING],
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
|
|
46
|
+
<Pressable
|
|
47
|
+
onPress={() => !disabled && onValueChange(!value)}
|
|
48
|
+
disabled={disabled}
|
|
49
|
+
hitSlop={8}
|
|
50
|
+
>
|
|
51
|
+
<Animated.View
|
|
52
|
+
style={[
|
|
53
|
+
styles.track,
|
|
54
|
+
{
|
|
55
|
+
backgroundColor: colors.background.main,
|
|
56
|
+
opacity: disabled ? 0.5 : 1,
|
|
57
|
+
},
|
|
58
|
+
]}
|
|
59
|
+
>
|
|
60
|
+
<Animated.View
|
|
61
|
+
style={[
|
|
62
|
+
styles.thumb,
|
|
63
|
+
{
|
|
64
|
+
left: thumbLeft,
|
|
65
|
+
backgroundColor: tint,
|
|
66
|
+
},
|
|
67
|
+
]}
|
|
68
|
+
/>
|
|
69
|
+
</Animated.View>
|
|
70
|
+
</Pressable>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const styles = StyleSheet.create({
|
|
75
|
+
track: {
|
|
76
|
+
width: TRACK_WIDTH,
|
|
77
|
+
height: TRACK_HEIGHT,
|
|
78
|
+
borderRadius: TRACK_HEIGHT / 2,
|
|
79
|
+
justifyContent: "center",
|
|
80
|
+
},
|
|
81
|
+
thumb: {
|
|
82
|
+
position: "absolute",
|
|
83
|
+
width: THUMB_SIZE,
|
|
84
|
+
height: THUMB_SIZE,
|
|
85
|
+
borderRadius: THUMB_SIZE / 2,
|
|
86
|
+
top: THUMB_PADDING,
|
|
87
|
+
// subtle shadow so thumb reads as elevated inside the track
|
|
88
|
+
shadowColor: "#000",
|
|
89
|
+
shadowOffset: { width: 0, height: 1 },
|
|
90
|
+
shadowOpacity: 0.2,
|
|
91
|
+
shadowRadius: 2,
|
|
92
|
+
elevation: 2,
|
|
93
|
+
},
|
|
94
|
+
});
|