-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.tsx
218 lines (189 loc) · 8.45 KB
/
index.tsx
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* eslint-disable es/no-optional-chaining, es/no-nullish-coalescing-operators, react/prop-types */
import type {ForwardedRef, MutableRefObject} from 'react';
import React, {useCallback, useEffect, useMemo, useRef} from 'react';
import type {FlatListProps, NativeScrollEvent, NativeSyntheticEvent} from 'react-native';
import {FlatList} from 'react-native';
function mergeRefs(...args: Array<MutableRefObject<FlatList> | ForwardedRef<FlatList> | null>) {
return function forwardRef(node: FlatList) {
args.forEach((ref) => {
if (ref == null) {
return;
}
if (typeof ref === 'function') {
ref(node);
return;
}
if (typeof ref === 'object') {
// eslint-disable-next-line no-param-reassign
ref.current = node;
return;
}
console.error(`mergeRefs cannot handle Refs of type boolean, number or string, received ref ${String(ref)}`);
});
};
}
function useMergeRefs(...args: Array<MutableRefObject<FlatList> | ForwardedRef<FlatList> | null>) {
return useMemo(
() => mergeRefs(...args),
// eslint-disable-next-line
[...args],
);
}
function getScrollableNode(flatList: FlatList | null): HTMLElement | undefined {
return flatList?.getScrollableNode() as HTMLElement | undefined;
}
function MVCPFlatList<TItem>({maintainVisibleContentPosition, horizontal = false, onScroll, ...props}: FlatListProps<TItem>, ref: ForwardedRef<FlatList>) {
const {minIndexForVisible: mvcpMinIndexForVisible, autoscrollToTopThreshold: mvcpAutoscrollToTopThreshold} = maintainVisibleContentPosition ?? {};
const scrollRef = useRef<FlatList | null>(null);
const prevFirstVisibleOffsetRef = useRef(0);
const firstVisibleViewRef = useRef<HTMLElement | null>(null);
const mutationObserverRef = useRef<MutationObserver | null>(null);
const lastScrollOffsetRef = useRef(0);
const isListRenderedRef = useRef(false);
const mvcpAutoscrollToTopThresholdRef = useRef(mvcpAutoscrollToTopThreshold);
mvcpAutoscrollToTopThresholdRef.current = mvcpAutoscrollToTopThreshold;
const getScrollOffset = useCallback((): number => {
if (!scrollRef.current) {
return 0;
}
return horizontal ? getScrollableNode(scrollRef.current)?.scrollLeft ?? 0 : getScrollableNode(scrollRef.current)?.scrollTop ?? 0;
}, [horizontal]);
const getContentView = useCallback(() => getScrollableNode(scrollRef.current)?.childNodes[0], []);
const scrollToOffset = useCallback(
(offset: number, animated: boolean) => {
const behavior = animated ? 'smooth' : 'instant';
getScrollableNode(scrollRef.current)?.scroll(horizontal ? {left: offset, behavior} : {top: offset, behavior});
},
[horizontal],
);
const prepareForMaintainVisibleContentPosition = useCallback(() => {
if (mvcpMinIndexForVisible == null) {
return;
}
const contentView = getContentView();
if (contentView == null) {
return;
}
const scrollOffset = getScrollOffset();
lastScrollOffsetRef.current = scrollOffset;
const contentViewLength = contentView.childNodes.length;
for (let i = mvcpMinIndexForVisible; i < contentViewLength; i++) {
const subview = contentView.childNodes[i] as HTMLElement;
const subviewOffset = horizontal ? subview.offsetLeft : subview.offsetTop;
if (subviewOffset > scrollOffset) {
prevFirstVisibleOffsetRef.current = subviewOffset;
firstVisibleViewRef.current = subview;
break;
}
}
}, [getContentView, getScrollOffset, mvcpMinIndexForVisible, horizontal]);
const adjustForMaintainVisibleContentPosition = useCallback(() => {
if (mvcpMinIndexForVisible == null) {
return;
}
const firstVisibleView = firstVisibleViewRef.current;
const prevFirstVisibleOffset = prevFirstVisibleOffsetRef.current;
if (firstVisibleView == null || prevFirstVisibleOffset == null) {
return;
}
const firstVisibleViewOffset = horizontal ? firstVisibleView.offsetLeft : firstVisibleView.offsetTop;
const delta = firstVisibleViewOffset - prevFirstVisibleOffset;
if (Math.abs(delta) > 0.5) {
const scrollOffset = getScrollOffset();
prevFirstVisibleOffsetRef.current = firstVisibleViewOffset;
scrollToOffset(scrollOffset + delta, false);
if (mvcpAutoscrollToTopThresholdRef.current != null && scrollOffset <= mvcpAutoscrollToTopThresholdRef.current) {
scrollToOffset(0, true);
}
}
}, [getScrollOffset, scrollToOffset, mvcpMinIndexForVisible, horizontal]);
const setupMutationObserver = useCallback(() => {
const contentView = getContentView();
if (contentView == null) {
return;
}
mutationObserverRef.current?.disconnect();
const mutationObserver = new MutationObserver(() => {
// This needs to execute after scroll events are dispatched, but
// in the same tick to avoid flickering. rAF provides the right timing.
requestAnimationFrame(() => {
// Chrome adjusts scroll position when elements are added at the top of the
// view. We want to have the same behavior as react-native / Safari so we
// reset the scroll position to the last value we got from an event.
const lastScrollOffset = lastScrollOffsetRef.current;
const scrollOffset = getScrollOffset();
if (lastScrollOffset !== scrollOffset) {
scrollToOffset(lastScrollOffset, false);
}
adjustForMaintainVisibleContentPosition();
prepareForMaintainVisibleContentPosition();
});
});
mutationObserver.observe(contentView, {
attributes: true,
childList: true,
subtree: true,
});
mutationObserverRef.current = mutationObserver;
}, [adjustForMaintainVisibleContentPosition, prepareForMaintainVisibleContentPosition, getContentView, getScrollOffset, scrollToOffset]);
useEffect(() => {
if (!isListRenderedRef.current) {
return;
}
const animationFrame = requestAnimationFrame(() => {
prepareForMaintainVisibleContentPosition();
setupMutationObserver();
});
return () => {
cancelAnimationFrame(animationFrame);
};
}, [prepareForMaintainVisibleContentPosition, setupMutationObserver]);
const setMergedRef = useMergeRefs(scrollRef, ref);
const onRef = useCallback(
(newRef: FlatList) => {
// Make sure to only call refs and re-attach listeners if the node changed.
if (newRef == null || newRef === scrollRef.current) {
return;
}
setMergedRef(newRef);
prepareForMaintainVisibleContentPosition();
setupMutationObserver();
},
[prepareForMaintainVisibleContentPosition, setMergedRef, setupMutationObserver],
);
useEffect(() => {
const mutationObserver = mutationObserverRef.current;
return () => {
mutationObserver?.disconnect();
mutationObserverRef.current = null;
};
}, []);
const onScrollInternal = useCallback(
(event: NativeSyntheticEvent<NativeScrollEvent>) => {
prepareForMaintainVisibleContentPosition();
onScroll?.(event);
},
[prepareForMaintainVisibleContentPosition, onScroll],
);
return (
<FlatList
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
maintainVisibleContentPosition={maintainVisibleContentPosition}
horizontal={horizontal}
onScroll={onScrollInternal}
scrollEventThrottle={1}
ref={onRef}
onLayout={(e) => {
isListRenderedRef.current = true;
if (!mutationObserverRef.current) {
prepareForMaintainVisibleContentPosition();
setupMutationObserver();
}
props.onLayout?.(e);
}}
/>
);
}
MVCPFlatList.displayName = 'MVCPFlatList';
export default React.forwardRef(MVCPFlatList);