-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathModal.ts
81 lines (72 loc) · 2.05 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
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;
/**
* 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);
return;
}
closeModals[closeModals.length - 1]();
}
/**
* Close modal in other parts of the app
*/
function close(onModalCloseCallback: () => void, isNavigating = true) {
if (closeModals.length === 0) {
onModalCloseCallback();
return;
}
onModalClose = onModalCloseCallback;
isNavigate = isNavigating;
closeTop();
}
function onModalDidClose() {
if (!onModalClose) {
return;
}
if (closeModals.length) {
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 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});
}
export {setCloseModal, close, onModalDidClose, setModalVisibility, willAlertModalBecomeVisible, closeTop};