-
-
Notifications
You must be signed in to change notification settings - Fork 718
/
BottomSheetModalProvider.tsx
197 lines (181 loc) 路 5.82 KB
/
BottomSheetModalProvider.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
import React, { useCallback, useMemo, useRef } from 'react';
import { useSharedValue } from 'react-native-reanimated';
import { PortalProvider } from '@gorhom/portal';
import {
BottomSheetModalProvider,
BottomSheetModalInternalProvider,
} from '../../contexts';
import BottomSheetContainer from '../bottomSheetContainer';
import { MODAL_STACK_BEHAVIOR } from '../../constants';
import {
INITIAL_CONTAINER_HEIGHT,
INITIAL_CONTAINER_OFFSET,
} from '../bottomSheet/constants';
import type { BottomSheetModalStackBehavior } from '../bottomSheetModal';
import type {
BottomSheetModalProviderProps,
BottomSheetModalRef,
} from './types';
const BottomSheetModalProviderWrapper = ({
children,
}: BottomSheetModalProviderProps) => {
//#region layout variables
const animatedContainerHeight = useSharedValue(INITIAL_CONTAINER_HEIGHT);
const animatedContainerOffset = useSharedValue(INITIAL_CONTAINER_OFFSET);
//#endregion
//#region variables
const sheetsQueueRef = useRef<BottomSheetModalRef[]>([]);
//#endregion
//#region private methods
const handleMountSheet = useCallback(
(key: string, ref: any, stackBehavior: BottomSheetModalStackBehavior) => {
const _sheetsQueue = sheetsQueueRef.current.slice();
const sheetIndex = _sheetsQueue.findIndex(item => item.key === key);
const sheetOnTop = sheetIndex === _sheetsQueue.length - 1;
/**
* Exit the method, if sheet is already presented
* and at the top.
*/
if (sheetIndex !== -1 && sheetOnTop) {
return;
}
/**
* Minimize the current sheet if:
* - it exists.
* - it is not unmounting.
* - stack behavior is 'replace'.
*/
const currentMountedSheet = _sheetsQueue[_sheetsQueue.length - 1];
if (
currentMountedSheet &&
!currentMountedSheet.willUnmount &&
stackBehavior === MODAL_STACK_BEHAVIOR.replace
) {
currentMountedSheet.ref?.current?.minimize();
}
/**
* Restore and remove incoming sheet from the queue,
* if it was registered.
*/
if (sheetIndex !== -1) {
_sheetsQueue.splice(sheetIndex, 1);
ref?.current?.restore();
}
_sheetsQueue.push({
key,
ref,
willUnmount: false,
});
sheetsQueueRef.current = _sheetsQueue;
},
[]
);
const handleUnmountSheet = useCallback((key: string) => {
const _sheetsQueue = sheetsQueueRef.current.slice();
const sheetIndex = _sheetsQueue.findIndex(item => item.key === key);
const sheetOnTop = sheetIndex === _sheetsQueue.length - 1;
/**
* Here we remove the unmounted sheet and update
* the sheets queue.
*/
_sheetsQueue.splice(sheetIndex, 1);
sheetsQueueRef.current = _sheetsQueue;
/**
* Here we try to restore previous sheet position if unmounted
* sheet was on top. This is needed when user dismiss
* the modal by panning down.
*/
const hasMinimizedSheet = sheetsQueueRef.current.length > 0;
const minimizedSheet =
sheetsQueueRef.current[sheetsQueueRef.current.length - 1];
if (
sheetOnTop &&
hasMinimizedSheet &&
minimizedSheet &&
!minimizedSheet.willUnmount
) {
sheetsQueueRef.current[
sheetsQueueRef.current.length - 1
].ref?.current?.restore();
}
}, []);
const handleWillUnmountSheet = useCallback((key: string) => {
const _sheetsQueue = sheetsQueueRef.current.slice();
const sheetIndex = _sheetsQueue.findIndex(item => item.key === key);
const sheetOnTop = sheetIndex === _sheetsQueue.length - 1;
/**
* Here we mark the sheet that will unmount,
* so it won't be restored.
*/
if (sheetIndex !== -1) {
_sheetsQueue[sheetIndex].willUnmount = true;
}
/**
* Here we try to restore previous sheet position,
* This is needed when user dismiss the modal by fire the dismiss action.
*/
const hasMinimizedSheet = _sheetsQueue.length > 1;
if (sheetOnTop && hasMinimizedSheet) {
_sheetsQueue[_sheetsQueue.length - 2].ref?.current?.restore();
}
sheetsQueueRef.current = _sheetsQueue;
}, []);
//#endregion
//#region public methods
const handleDismiss = useCallback((key?: string) => {
const sheetToBeDismissed = key
? sheetsQueueRef.current.find(item => item.key === key)
: sheetsQueueRef.current[sheetsQueueRef.current.length - 1];
if (sheetToBeDismissed) {
sheetToBeDismissed.ref?.current?.dismiss();
return true;
}
return false;
}, []);
const handleDismissAll = useCallback(() => {
sheetsQueueRef.current.map(item => {
item.ref?.current?.dismiss();
});
}, []);
//#endregion
//#region context variables
const externalContextVariables = useMemo(
() => ({
dismiss: handleDismiss,
dismissAll: handleDismissAll,
}),
[handleDismiss, handleDismissAll]
);
const internalContextVariables = useMemo(
() => ({
containerHeight: animatedContainerHeight,
containerOffset: animatedContainerOffset,
mountSheet: handleMountSheet,
unmountSheet: handleUnmountSheet,
willUnmountSheet: handleWillUnmountSheet,
}),
[
animatedContainerHeight,
animatedContainerOffset,
handleMountSheet,
handleUnmountSheet,
handleWillUnmountSheet,
]
);
//#endregion
//#region renders
return (
<BottomSheetModalProvider value={externalContextVariables}>
<BottomSheetModalInternalProvider value={internalContextVariables}>
<BottomSheetContainer
containerOffset={animatedContainerOffset}
containerHeight={animatedContainerHeight}
children={null}
/>
<PortalProvider>{children}</PortalProvider>
</BottomSheetModalInternalProvider>
</BottomSheetModalProvider>
);
//#endregion
};
export default BottomSheetModalProviderWrapper;