-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseAnimatedSearch.ts
More file actions
123 lines (96 loc) · 3.35 KB
/
useAnimatedSearch.ts
File metadata and controls
123 lines (96 loc) · 3.35 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
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
import {
interpolate,
runOnUI,
useAnimatedStyle,
useDerivedValue,
useSharedValue,
withTiming,
} from "react-native-reanimated";
import { useEffect } from "react";
import { Keyboard, LayoutChangeEvent } from "react-native";
import { ANDROID_DEVICE, IOS_DEVICE } from "~config/device";
const CONTAINER_MIN_WIDTH_PERCENT = 75;
enum KeyboardState {
Closed,
Open,
}
const { Closed, Open } = KeyboardState;
function useAnimatedSearch() {
const initialContainerWidth = useSharedValue(0);
const placeholderWidth = useSharedValue(0);
const keyboardHeight = useSharedValue(0);
const keyboardState = useDerivedValue(() => {
const input = [-keyboardHeight.value, Math.abs(keyboardHeight.value)];
return interpolate(keyboardHeight.value, input, [Closed, Open]);
}, [keyboardHeight]);
const containerWidth = useDerivedValue(() => {
const output = [100, CONTAINER_MIN_WIDTH_PERCENT];
const width = interpolate(keyboardState.value, [Closed, Open], output);
return withTiming(width);
}, [keyboardState]);
const setInitialContainerWidth = ({ nativeEvent }: LayoutChangeEvent) => {
runOnUI((value) => {
initialContainerWidth.value = value;
})(nativeEvent.layout.width);
};
const setPlaceholderWidth = ({ nativeEvent }: LayoutChangeEvent) => {
runOnUI((value) => {
placeholderWidth.value = value;
})(nativeEvent.layout.width);
};
const setKeyboardHeight = (height: number) => {
keyboardHeight.value = height;
};
useEffect(() => {
if (IOS_DEVICE) {
const event = Keyboard.addListener("keyboardWillChangeFrame", (evt) => {
const { screenY: startY } = evt.startCoordinates;
const { screenY: endY } = evt.endCoordinates;
runOnUI(setKeyboardHeight)(startY - endY);
});
return () => {
Keyboard.removeListener(event);
};
}
}, []);
useEffect(() => {
if (ANDROID_DEVICE) {
const didShowEvent = Keyboard.addListener("keyboardDidShow", (evt) => {
const { screenY: endY } = evt.endCoordinates;
runOnUI(setKeyboardHeight)(endY);
});
const didHideEvent = Keyboard.addListener("keyboardDidHide", (evt) => {
const { screenY: endY } = evt.endCoordinates;
runOnUI(setKeyboardHeight)(-endY);
});
return () => {
Keyboard.removeListener(didShowEvent);
Keyboard.removeListener(didHideEvent);
};
}
}, []);
const containerStyle = useAnimatedStyle(() => ({
width: `${containerWidth.value}%`,
}));
const placeholderStyle = useAnimatedStyle(() => {
const containerCenter = -(initialContainerWidth.value / 2);
const placeholderCenter = placeholderWidth.value / 2;
const paddingLeft = IOS_DEVICE ? 16 : 22;
const input = [100, CONTAINER_MIN_WIDTH_PERCENT];
const output = [0, containerCenter + placeholderCenter + paddingLeft];
const translateX = interpolate(containerWidth.value, input, output);
return { transform: [{ translateX }] };
});
const cancelStyle = useAnimatedStyle(() => {
const input = [100, CONTAINER_MIN_WIDTH_PERCENT];
const opacity = interpolate(containerWidth.value, input, [0, 1]);
return { opacity };
});
const style = {
container: containerStyle,
placeholder: placeholderStyle,
cancel: cancelStyle,
};
return { style, setInitialContainerWidth, setPlaceholderWidth };
}
export default useAnimatedSearch;