-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathFlashList.tsx
More file actions
200 lines (184 loc) · 6.41 KB
/
FlashList.tsx
File metadata and controls
200 lines (184 loc) · 6.41 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import React, { useImperativeHandle, useMemo } from 'react';
import { StyleSheet, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import Animated, { AnimatedProps, isSharedValue, useAnimatedRef } from 'react-native-reanimated';
import { FlashList, FlashListProps, FlashListRef } from '@shopify/flash-list';
import type { SharedScrollContainerProps } from '.';
import FadingView from './FadingView';
import { useScrollContainerLogic } from './useScrollContainerLogic';
type AnimatedFlashListType<ItemT> = React.ComponentProps<
React.ComponentClass<AnimatedProps<FlashListProps<ItemT>>, any>
> &
SharedScrollContainerProps;
const AnimatedFlashList = Animated.createAnimatedComponent(FlashList) as <ItemT>(
props: AnimatedProps<FlashListProps<ItemT>> & { ref?: React.Ref<FlashListRef<ItemT>> }
) => React.ReactElement;
type FlashListWithHeadersProps<ItemT> = Omit<AnimatedFlashListType<ItemT>, 'onScroll'>;
const FlashListWithHeadersInputComp = <ItemT extends any = any>(
{
largeHeaderShown,
containerStyle,
LargeHeaderSubtitleComponent,
LargeHeaderComponent,
largeHeaderContainerStyle,
HeaderComponent,
onLargeHeaderLayout,
onScrollBeginDrag,
onScrollEndDrag,
onScrollWorklet,
onMomentumScrollBegin,
onMomentumScrollEnd,
ignoreLeftSafeArea,
ignoreRightSafeArea,
disableAutoFixScroll = false,
// We use this to ensure that the onScroll property isn't accidentally used.
// @ts-ignore
onScroll: _unusedOnScroll,
absoluteHeader = false,
initialAbsoluteHeaderHeight = 0,
contentContainerStyle = {},
automaticallyAdjustsScrollIndicatorInsets,
headerFadeInThreshold = 1,
disableLargeHeaderFadeAnim = false,
scrollIndicatorInsets = {},
...rest
}: FlashListWithHeadersProps<ItemT>,
ref: React.ForwardedRef<FlashListRef<ItemT>>
): ReturnType<ForwardedFlashListWithHeadersProps<ItemT>> => {
if (_unusedOnScroll) {
throw new Error(
"The 'onScroll' property is not supported. Please use onScrollWorklet to track the scroll container's state."
);
}
const insets = useSafeAreaInsets();
const scrollRef = useAnimatedRef<any>();
useImperativeHandle(ref, () => scrollRef.current);
const { maintainVisibleContentPosition } = rest;
const inverted = useMemo(() => {
if (maintainVisibleContentPosition === undefined) return false;
if (isSharedValue(maintainVisibleContentPosition)) return false;
return (
(maintainVisibleContentPosition as FlashListProps<ItemT>['maintainVisibleContentPosition'])
?.startRenderingFromBottom ?? false
);
}, [maintainVisibleContentPosition]);
const {
scrollY,
showNavBar,
largeHeaderHeight,
largeHeaderOpacity,
scrollHandler,
debouncedFixScroll,
onAbsoluteHeaderLayout,
scrollViewAdjustments,
} = useScrollContainerLogic({
scrollRef,
largeHeaderShown,
disableAutoFixScroll,
largeHeaderExists: !!LargeHeaderComponent,
absoluteHeader,
initialAbsoluteHeaderHeight,
headerFadeInThreshold,
inverted,
onScrollWorklet,
isFlashList: true,
});
return (
<View
style={[
styles.container,
containerStyle,
!ignoreLeftSafeArea && { paddingLeft: insets.left },
!ignoreRightSafeArea && { paddingRight: insets.right },
]}
>
{!absoluteHeader && HeaderComponent({ showNavBar, scrollY })}
<AnimatedFlashList<ItemT>
ref={scrollRef}
scrollEventThrottle={16}
overScrollMode="auto"
onScroll={scrollHandler}
automaticallyAdjustContentInsets={false}
onScrollBeginDrag={(e) => {
debouncedFixScroll.cancel();
if (onScrollBeginDrag) onScrollBeginDrag(e);
}}
onScrollEndDrag={(e) => {
debouncedFixScroll();
if (onScrollEndDrag) onScrollEndDrag(e);
}}
onMomentumScrollBegin={(e) => {
debouncedFixScroll.cancel();
if (onMomentumScrollBegin) onMomentumScrollBegin(e);
}}
onMomentumScrollEnd={(e) => {
debouncedFixScroll();
if (onMomentumScrollEnd) onMomentumScrollEnd(e);
}}
contentContainerStyle={{
// The reason why we do this is because FlashList does not support an array of
// styles (will throw a warning when you supply one).
...scrollViewAdjustments.contentContainerStyle,
...(contentContainerStyle as any),
}}
automaticallyAdjustsScrollIndicatorInsets={
automaticallyAdjustsScrollIndicatorInsets !== undefined
? automaticallyAdjustsScrollIndicatorInsets
: !absoluteHeader
}
scrollIndicatorInsets={{
...scrollViewAdjustments.scrollIndicatorInsets,
...scrollIndicatorInsets,
}}
ListHeaderComponent={
<>
{LargeHeaderComponent && (
<View
onLayout={(e) => {
largeHeaderHeight.value = e.nativeEvent.layout.height;
if (onLargeHeaderLayout) onLargeHeaderLayout(e.nativeEvent.layout);
}}
>
{!disableLargeHeaderFadeAnim ? (
<FadingView opacity={largeHeaderOpacity} style={largeHeaderContainerStyle}>
{LargeHeaderComponent({ scrollY, showNavBar })}
</FadingView>
) : (
<View style={largeHeaderContainerStyle}>
{LargeHeaderComponent({ scrollY, showNavBar })}
</View>
)}
</View>
)}
{LargeHeaderSubtitleComponent && LargeHeaderSubtitleComponent({ showNavBar, scrollY })}
</>
}
{...rest}
/>
{absoluteHeader && (
<View style={styles.absoluteHeader} onLayout={onAbsoluteHeaderLayout}>
{HeaderComponent({ showNavBar, scrollY })}
</View>
)}
</View>
);
};
type ForwardedFlashListWithHeadersProps<ItemT> = React.ForwardRefRenderFunction<
FlashListRef<ItemT>,
FlashListWithHeadersProps<ItemT>
>;
const FlashListWithHeaders = React.forwardRef(FlashListWithHeadersInputComp) as <ItemT = any>(
props: FlashListWithHeadersProps<ItemT> & {
ref?: React.RefObject<any>;
}
) => React.ReactElement;
export default FlashListWithHeaders;
const styles = StyleSheet.create({
container: { flex: 1 },
absoluteHeader: {
position: 'absolute',
top: 0,
right: 0,
left: 0,
},
});