-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Modal.ts
96 lines (85 loc) · 2.57 KB
/
Modal.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
import Onyx from 'react-native-onyx';
import ONYXKEYS from '@src/ONYXKEYS';
const closeModals: Array<(isNavigating?: boolean) => void> = [];
let onModalClose: null | (() => void);
let isNavigate: undefined | boolean;
let shouldCloseAll: boolean | undefined;
/**
* Allows other parts of the app to call modal close function
*/
function setCloseModal(onClose: () => void) {
if (!closeModals.includes(onClose)) {
closeModals.push(onClose);
}
return () => {
const index = closeModals.indexOf(onClose);
if (index === -1) {
return;
}
closeModals.splice(index, 1);
};
}
/**
* Close topmost modal
*/
function closeTop() {
if (closeModals.length === 0) {
return;
}
if (onModalClose) {
closeModals[closeModals.length - 1](isNavigate);
closeModals.pop();
return;
}
closeModals[closeModals.length - 1]();
closeModals.pop();
}
/**
* Close modal in other parts of the app
*/
function close(onModalCloseCallback: () => void, isNavigating = true, shouldCloseAllModals = false) {
if (closeModals.length === 0) {
onModalCloseCallback();
return;
}
onModalClose = onModalCloseCallback;
shouldCloseAll = shouldCloseAllModals;
isNavigate = isNavigating;
closeTop();
}
function onModalDidClose() {
if (!onModalClose) {
return;
}
if (closeModals.length && shouldCloseAll) {
closeTop();
return;
}
onModalClose();
onModalClose = null;
isNavigate = undefined;
}
/**
* Allows other parts of the app to know when a modal has been opened or closed
*/
function setModalVisibility(isVisible: boolean) {
Onyx.merge(ONYXKEYS.MODAL, {isVisible});
}
/**
* Allows other parts of the app to set whether modals should be dismissable using the Escape key
*/
function setDisableDismissOnEscape(disableDismissOnEscape: boolean) {
Onyx.merge(ONYXKEYS.MODAL, {disableDismissOnEscape});
}
/**
* Allows other parts of app to know that an alert modal is about to open.
* This will trigger as soon as a modal is opened but not yet visible while animation is running.
* isPopover indicates that the next open modal is popover or bottom docked
*/
function willAlertModalBecomeVisible(isVisible: boolean, isPopover = false) {
Onyx.merge(ONYXKEYS.MODAL, {willAlertModalBecomeVisible: isVisible, isPopover});
}
function areAllModalsHidden() {
return closeModals.length === 0;
}
export {setCloseModal, close, onModalDidClose, setModalVisibility, willAlertModalBecomeVisible, setDisableDismissOnEscape, closeTop, areAllModalsHidden};