-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseSlideToHeader.ts
More file actions
57 lines (44 loc) · 1.47 KB
/
useSlideToHeader.ts
File metadata and controls
57 lines (44 loc) · 1.47 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
import {
Extrapolation,
getRelativeCoords,
interpolate,
useAnimatedRef,
useAnimatedScrollHandler,
useAnimatedStyle,
useSharedValue,
} from "react-native-reanimated";
const { CLAMP } = Extrapolation;
const TITLE_HEIGHT = 40;
function useSlideToHeader() {
const titleRef = useAnimatedRef();
const headerTitleY = useSharedValue(TITLE_HEIGHT);
const handler = useAnimatedScrollHandler({
onScroll: ({ contentOffset }) => {
const { y: titleY } = contentOffset;
const { y: relativeY } = getRelativeCoords(titleRef, 0, titleY);
headerTitleY.value = -relativeY;
},
});
const headerTitleStyle = useAnimatedStyle(() => {
const { value } = headerTitleY;
const translateIn = [-TITLE_HEIGHT, 0];
const translateOut = [0, TITLE_HEIGHT];
const translateY = interpolate(value, translateIn, translateOut, CLAMP);
const opacityThreshold = TITLE_HEIGHT / 2;
const opacity = interpolate(translateY, [0, opacityThreshold], [1, 0]);
return { transform: [{ translateY }], opacity: opacity };
});
const titleStyle = useAnimatedStyle(() => {
const opacityThreshold = -TITLE_HEIGHT / 2;
const opacityIn = [TITLE_HEIGHT, opacityThreshold];
const opacityOut = [1, 0.1];
const opacity = interpolate(headerTitleY.value, opacityIn, opacityOut);
return { opacity };
});
const style = {
headerTitle: headerTitleStyle,
title: titleStyle,
};
return { handler, style, titleRef };
}
export default useSlideToHeader;