-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathListWithHeader.tsx
More file actions
161 lines (143 loc) · 4.15 KB
/
Copy pathListWithHeader.tsx
File metadata and controls
161 lines (143 loc) · 4.15 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import React, { useEffect } from 'react';
import type { ScrollViewProps, SectionListProps } from 'react-native';
import { Platform, SectionList, StyleSheet } from 'react-native';
import type { GestureType } from 'react-native-gesture-handler';
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
import type { AnimatedRef, SharedValue } from 'react-native-reanimated';
import Animated, {
runOnJS,
useAnimatedProps,
useAnimatedReaction,
useAnimatedRef,
useAnimatedStyle,
useScrollViewOffset,
useSharedValue,
withSpring,
} from 'react-native-reanimated';
import Header, { HEADER_HEIGHT } from './Header';
const IS_ANDROID = Platform.OS === 'android';
export function ListWithHeader<ItemT, SectionT>({
scrollViewTestID,
...props
}: SectionListProps<ItemT, SectionT> & { scrollViewTestID?: string }) {
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,
};
});
const contentContainerStyle =
StyleSheet.flatten(props.contentContainerStyle) || {};
if (typeof contentContainerStyle.paddingTop !== 'number') {
contentContainerStyle.paddingTop = 0;
console.error(
'ListWithHeader: contentContainerStyle.paddingTop should be a number.'
);
}
return (
<GestureDetector gesture={dragGesture}>
<Animated.View
style={[
{ flex: 1, marginTop: contentContainerStyle.paddingTop },
containerProps,
]}>
<Header scrollOffset={scrollOffset} />
<SectionList
{...props}
renderScrollComponent={(props) => (
<ScrollComponentWithOffset
{...props}
testID={scrollViewTestID}
scrollOffset={scrollOffset}
dragGesture={dragGesture}
animatedScrollEnabled={scrollEnabled}
/>
)}
/>
</Animated.View>
</GestureDetector>
);
}
interface ScrollComponentWithOffsetProps extends ScrollViewProps {
scrollOffset: SharedValue<number>;
animatedScrollEnabled: SharedValue<boolean>;
dragGesture: GestureType;
ref?: AnimatedRef<Animated.ScrollView>;
}
const ScrollComponentWithOffset = ({
ref,
scrollOffset,
animatedScrollEnabled,
dragGesture,
...props
}: ScrollComponentWithOffsetProps) => {
const scrollRef = useAnimatedRef<Animated.ScrollView>();
const scrollViewOffset = useScrollViewOffset(scrollRef);
useAnimatedReaction(
() => {
return scrollViewOffset.value;
},
(offset) => {
scrollOffset.value = offset;
}
);
useEffect(() => {
if (ref) {
ref.current = scrollRef.current;
}
}, [ref, scrollRef]);
const scrollProps = useAnimatedProps(() => {
return {
scrollEnabled: animatedScrollEnabled.value,
};
});
const scrollGesture = Gesture.Native()
.disallowInterruption(true)
.simultaneousWithExternalGesture(dragGesture);
return (
<GestureDetector gesture={scrollGesture}>
<Animated.ScrollView
{...props}
ref={scrollRef}
contentContainerStyle={[
props.contentContainerStyle,
{ paddingTop: HEADER_HEIGHT },
]}
scrollEventThrottle={1}
animatedProps={scrollProps}
/>
</GestureDetector>
);
};