-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathhooks.ts
More file actions
77 lines (64 loc) · 1.8 KB
/
Copy pathhooks.ts
File metadata and controls
77 lines (64 loc) · 1.8 KB
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
import { useEffect, useRef, useState } from "react";
import { Animated } from "react-native";
import { SIZE_DEFAULT } from "./constants";
import { ThumbButton, ThumbChildrenPlacement, TrackBar } from "./types";
export const useTitleTextColor = (
toggleValue: boolean,
activeColor: string,
inActiveColor: string,
title: string | null,
placement: ThumbChildrenPlacement
) => {
if (!title) return null;
const onColor = toggleValue ? inActiveColor : activeColor;
const offColor = toggleValue ? activeColor : inActiveColor;
const textColor = placement === "left" ? onColor : offColor;
return { textColor };
};
export const useToggleValue = (
value: boolean,
thumbButton: ThumbButton,
trackBar: TrackBar,
animationDuration: number,
onPress: (val?: boolean) => void,
managed: boolean
) => {
const fadeAnim = useRef(new Animated.Value(0)).current;
const [toggleValue, setToggleValue] = useState(value);
useEffect(() => {
updateThumbButton(toggleValue);
}, [toggleValue]);
useEffect(() => {
setToggleValue(value);
}, [value]);
const updateThumbButton = (toggleState) => {
const thumbBtnWidth = thumbButton.width ?? SIZE_DEFAULT.thumbBtnWidth;
const trackBarW = trackBar.width ?? SIZE_DEFAULT.trackBarWidth;
const distance = trackBarW - thumbBtnWidth;
const toValue = toggleState ? distance : 0;
Animated.timing(fadeAnim, {
toValue,
duration: animationDuration,
useNativeDriver: true,
}).start();
};
const handleToggle = () => {
const val = !toggleValue;
if(!managed) {
setToggleValue(val);
}
onPress(val);
};
const handlePress = () => {
handleToggle();
};
const handleLongPress = () => {
handleToggle();
};
return {
toggleValue,
handlePress,
handleLongPress,
fadeAnim,
};
};