-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathindex.tsx
82 lines (70 loc) · 2.64 KB
/
index.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
import React, {useEffect, useRef, useState} from 'react';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import StatusBar from '@libs/StatusBar';
import CONST from '@src/CONST';
import BaseModal from './BaseModal';
import type BaseModalProps from './types';
import type {WindowState} from './types';
function Modal({fullscreen = true, onModalHide = () => {}, type, onModalShow = () => {}, children, shouldHandleNavigationBack, ...rest}: BaseModalProps) {
const theme = useTheme();
const StyleUtils = useStyleUtils();
const [previousStatusBarColor, setPreviousStatusBarColor] = useState<string>();
const setStatusBarColor = (color = theme.appBG) => {
if (!fullscreen) {
return;
}
StatusBar.setBackgroundColor(color);
};
const hideModal = () => {
setStatusBarColor(previousStatusBarColor);
onModalHide();
if ((window.history.state as WindowState)?.shouldGoBack) {
window.history.back();
}
};
const handlePopStateRef = useRef(() => {
rest.onClose();
});
const showModal = () => {
const statusBarColor = StatusBar.getBackgroundColor() ?? theme.appBG;
const isFullScreenModal =
type === CONST.MODAL.MODAL_TYPE.CENTERED ||
type === CONST.MODAL.MODAL_TYPE.CENTERED_UNSWIPEABLE ||
type === CONST.MODAL.MODAL_TYPE.RIGHT_DOCKED ||
CONST.MODAL.MODAL_TYPE.CENTERED_SWIPABLE_TO_RIGHT;
if (statusBarColor) {
setPreviousStatusBarColor(statusBarColor);
// If it is a full screen modal then match it with appBG, otherwise we use the backdrop color
setStatusBarColor(isFullScreenModal ? theme.appBG : StyleUtils.getThemeBackgroundColor(statusBarColor));
}
if (shouldHandleNavigationBack) {
window.history.pushState({shouldGoBack: true}, '', null);
window.addEventListener('popstate', handlePopStateRef.current);
}
onModalShow?.();
};
useEffect(
() => () => {
window.removeEventListener('popstate', handlePopStateRef.current);
},
[],
);
return (
<BaseModal
// eslint-disable-next-line react/jsx-props-no-spreading
{...rest}
onModalHide={hideModal}
onModalShow={showModal}
avoidKeyboard={false}
fullscreen={fullscreen}
useNativeDriver={false}
useNativeDriverForBackdrop={false}
type={type}
>
{children}
</BaseModal>
);
}
Modal.displayName = 'Modal';
export default Modal;