This repository has been archived by the owner on Sep 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (37 loc) · 1.65 KB
/
index.js
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
import {useEffect, useRef} from 'react';
import {NativeModules, ScrollView, findNodeHandle, Platform, ScrollViewProps} from 'react-native';
const {enableMaintainVisibleContentPosition, disableMaintainVisibleContentPosition} = NativeModules.ScrollViewMagic;
export {enableMaintainVisibleContentPosition, disableMaintainVisibleContentPosition};
export function enableMaintain() {
const isAndroid = Platform.OS === 'android';
const _ScrollView = ScrollView;
const ScrollViewRender = _ScrollView.render;
_ScrollView.render = function (props, ref) {
let scrollView;
if (isAndroid) {
const listRef = useRef();
useEffect(() => {
let cleanupPromise;
if (props.maintainVisibleContentPosition) {
const viewTag = findNodeHandle(listRef.current);
if (viewTag != null && !cleanupPromise) {
cleanupPromise = enableMaintainVisibleContentPosition(viewTag, props.maintainVisibleContentPosition.minIndexForVisible);
}
} else {
cleanupPromise?.then((handle) => disableMaintainVisibleContentPosition(handle));
cleanupPromise = null;
}
return () => {
cleanupPromise?.then((handle) => disableMaintainVisibleContentPosition(handle));
};
}, []);
scrollView = ScrollViewRender(props, (r) => {
ref(r);
listRef.current = r;
});
} else {
scrollView = ScrollViewRender(props, ref);
}
return scrollView;
};
}