-
-
Notifications
You must be signed in to change notification settings - Fork 800
/
Copy pathuseScrollableInternal.ts
118 lines (111 loc) · 3.36 KB
/
useScrollableInternal.ts
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
import { useCallback } from 'react';
import { findNodeHandle, NativeScrollEvent } from 'react-native';
import {
useAnimatedRef,
useAnimatedScrollHandler,
useSharedValue,
scrollTo,
runOnUI,
useAnimatedProps,
} from 'react-native-reanimated';
import { useBottomSheetInternal } from './useBottomSheetInternal';
import type { Scrollable } from '../types';
import {
ANIMATION_STATE,
SCROLLABLE_DECELERATION_RATE_MAPPER,
SCROLLABLE_STATE,
} from '../constants';
export const useScrollableInternal = () => {
// refs
const scrollableRef = useAnimatedRef<Scrollable>();
const scrollableContentOffsetY = useSharedValue<number>(0);
// hooks
const {
animatedScrollableState,
animatedAnimationState,
scrollableContentOffsetY: _rootScrollableContentOffsetY,
setScrollableRef,
removeScrollableRef,
} = useBottomSheetInternal();
// variables
const scrollableAnimatedProps = useAnimatedProps(() => ({
decelerationRate:
SCROLLABLE_DECELERATION_RATE_MAPPER[animatedScrollableState.value],
}));
// callbacks
const handleScrollEvent = useAnimatedScrollHandler({
onBeginDrag: ({ contentOffset: { y } }: NativeScrollEvent) => {
if (animatedScrollableState.value === SCROLLABLE_STATE.LOCKED) {
scrollableContentOffsetY.value = 0;
_rootScrollableContentOffsetY.value = 0;
return;
}
scrollableContentOffsetY.value = y;
_rootScrollableContentOffsetY.value = y;
},
onScroll: () => {
if (animatedScrollableState.value === SCROLLABLE_STATE.LOCKED) {
// @ts-ignore
scrollTo(scrollableRef, 0, 0, false);
scrollableContentOffsetY.value = 0;
return;
}
},
onEndDrag: ({ contentOffset: { y } }: NativeScrollEvent) => {
if (animatedScrollableState.value === SCROLLABLE_STATE.LOCKED) {
// @ts-ignore
scrollTo(scrollableRef, 0, 0, false);
scrollableContentOffsetY.value = 0;
return;
}
if (animatedAnimationState.value !== ANIMATION_STATE.RUNNING) {
scrollableContentOffsetY.value = y;
_rootScrollableContentOffsetY.value = y;
}
},
onMomentumEnd: ({ contentOffset: { y } }: NativeScrollEvent) => {
if (animatedScrollableState.value === SCROLLABLE_STATE.LOCKED) {
// @ts-ignore
scrollTo(scrollableRef, 0, 0, false);
scrollableContentOffsetY.value = 0;
return;
}
if (animatedAnimationState.value !== ANIMATION_STATE.RUNNING) {
scrollableContentOffsetY.value = y;
_rootScrollableContentOffsetY.value = y;
}
},
});
const handleSettingScrollable = useCallback(() => {
// set current content offset
runOnUI(() => {
_rootScrollableContentOffsetY.value = scrollableContentOffsetY.value;
})();
// set current scrollable ref
const id = findNodeHandle(scrollableRef.current);
if (id) {
setScrollableRef({
id: id,
node: scrollableRef,
didResize: false,
});
} else {
console.warn(`Couldn't find the scrollable node handle id!`);
}
return () => {
removeScrollableRef(scrollableRef);
};
}, [
_rootScrollableContentOffsetY,
removeScrollableRef,
scrollableContentOffsetY,
scrollableRef,
setScrollableRef,
]);
return {
scrollableRef,
scrollableAnimatedProps,
handleScrollEvent,
handleSettingScrollable,
};
};