-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathListWithHeader.tsx
More file actions
146 lines (130 loc) · 3.45 KB
/
ListWithHeader.tsx
File metadata and controls
146 lines (130 loc) · 3.45 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import React, { useEffect } from 'react';
import {
Platform,
ScrollViewProps,
SectionList,
SectionListProps,
} from 'react-native';
import Animated, {
SharedValue,
useAnimatedRef,
useScrollViewOffset,
useAnimatedReaction,
useSharedValue,
useAnimatedProps,
useAnimatedStyle,
runOnJS,
withSpring,
} from 'react-native-reanimated';
import Header, { HEADER_HEIGHT } from './Header';
import {
Gesture,
GestureDetector,
GestureType,
} from 'react-native-gesture-handler';
const IS_ANDROID = Platform.OS === 'android';
export function ListWithHeader<ItemT, SectionT>(
props: SectionListProps<ItemT, SectionT>
) {
const scrollOffset = useSharedValue(0);
const scrollEnabled = useSharedValue(true);
const androidDragDist = useSharedValue(0);
function enableScroll() {
scrollEnabled.value = true;
}
const dragGesture = Gesture.Pan()
.onChange((e) => {
if (!IS_ANDROID) {
return;
}
if (scrollOffset.value <= 0) {
androidDragDist.value -= e.changeY;
scrollOffset.value = -Math.pow(
Math.max(-androidDragDist.value, 0),
0.85
);
if (scrollOffset.value < 0) {
scrollEnabled.value = false;
} else {
runOnJS(enableScroll)();
}
} else {
runOnJS(enableScroll)();
}
})
.onFinalize(() => {
if (IS_ANDROID && scrollOffset.value <= 0) {
scrollOffset.value = withSpring(0, { damping: 50, stiffness: 500 });
runOnJS(enableScroll)();
}
androidDragDist.value = 0;
});
const containerProps = useAnimatedStyle(() => {
return {
paddingTop: IS_ANDROID ? Math.max(-scrollOffset.value, 0) : 0,
};
});
return (
<GestureDetector gesture={dragGesture}>
<Animated.View style={[{ flex: 1 }, containerProps]}>
<Header scrollOffset={scrollOffset} />
<SectionList
{...props}
renderScrollComponent={(props) => (
<ScrollComponentWithOffset
{...props}
scrollOffset={scrollOffset}
dragGesture={dragGesture}
animatedScrollEnabled={scrollEnabled}
/>
)}
/>
</Animated.View>
</GestureDetector>
);
}
interface ScrollComponentWithOffsetProps extends ScrollViewProps {
scrollOffset: SharedValue<number>;
animatedScrollEnabled: SharedValue<boolean>;
dragGesture: GestureType;
ref?: React.RefObject<Animated.ScrollView | null>;
}
const ScrollComponentWithOffset = ({
ref,
...props
}: ScrollComponentWithOffsetProps) => {
const scrollRef = useAnimatedRef<Animated.ScrollView>();
const scrollViewOffset = useScrollViewOffset(scrollRef);
useAnimatedReaction(
() => {
return scrollViewOffset.value;
},
(offset) => {
props.scrollOffset.value = offset;
}
);
useEffect(() => {
if (ref) {
ref.current = scrollRef.current;
}
}, [ref, scrollRef]);
const scrollProps = useAnimatedProps(() => {
return {
scrollEnabled: props.animatedScrollEnabled.value,
};
});
const scrollGesture = Gesture.Native()
.disallowInterruption(true)
.simultaneousWithExternalGesture(props.dragGesture);
return (
<GestureDetector gesture={scrollGesture}>
<Animated.ScrollView
{...props}
ref={scrollRef}
contentContainerStyle={{ paddingTop: HEADER_HEIGHT }}
scrollEventThrottle={1}
animatedProps={scrollProps}
/>
</GestureDetector>
);
};