-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathBaseModal.js
184 lines (165 loc) · 7.25 KB
/
BaseModal.js
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
import React, {PureComponent} from 'react';
import {StatusBar, View} from 'react-native';
import PropTypes from 'prop-types';
import ReactNativeModal from 'react-native-modal';
import {SafeAreaInsetsContext} from 'react-native-safe-area-context';
import styles from '../../styles/styles';
import * as StyleUtils from '../../styles/StyleUtils';
import themeColors from '../../styles/themes/default';
import {propTypes as modalPropTypes, defaultProps as modalDefaultProps} from './modalPropTypes';
import * as Modal from '../../libs/actions/Modal';
import getModalStyles from '../../styles/getModalStyles';
import variables from '../../styles/variables';
const propTypes = {
...modalPropTypes,
/** The ref to the modal container */
forwardedRef: PropTypes.func,
};
const defaultProps = {
...modalDefaultProps,
forwardedRef: () => {},
};
class BaseModal extends PureComponent {
constructor(props) {
super(props);
this.hideModal = this.hideModal.bind(this);
}
componentDidMount() {
if (!this.props.isVisible) { return; }
// To handle closing any modal already visible when this modal is mounted, i.e. PopoverReportActionContextMenu
Modal.setCloseModal(this.props.onClose);
}
componentDidUpdate(prevProps) {
if (prevProps.isVisible === this.props.isVisible) {
return;
}
Modal.willAlertModalBecomeVisible(this.props.isVisible);
Modal.setCloseModal(this.props.isVisible ? this.props.onClose : null);
}
componentWillUnmount() {
// we don't want to call the onModalHide on unmount
this.hideModal(this.props.isVisible);
// To prevent closing any modal already unmounted when this modal still remains as visible state
Modal.setCloseModal(null);
}
/**
* Hides modal
* @param {Boolean} [callHideCallback=true] Should we call the onModalHide callback
*/
hideModal(callHideCallback = true) {
if (this.props.shouldSetModalVisibility) {
Modal.setModalVisibility(false);
}
if (callHideCallback) {
this.props.onModalHide();
}
}
render() {
const {
modalStyle,
modalContainerStyle,
swipeDirection,
animationIn,
animationOut,
shouldAddTopSafeAreaMargin,
shouldAddBottomSafeAreaMargin,
shouldAddTopSafeAreaPadding,
shouldAddBottomSafeAreaPadding,
hideBackdrop,
} = getModalStyles(
this.props.type,
{
windowWidth: this.props.windowWidth,
windowHeight: this.props.windowHeight,
isSmallScreenWidth: this.props.isSmallScreenWidth,
},
this.props.popoverAnchorPosition,
this.props.innerContainerStyle,
this.props.outerStyle,
);
return (
<ReactNativeModal
onBackdropPress={(e) => {
if (e && e.type === 'keydown' && e.key === 'Enter') {
return;
}
this.props.onClose();
}}
// Note: Escape key on web/desktop will trigger onBackButtonPress callback
// eslint-disable-next-line react/jsx-props-no-multi-spaces
onBackButtonPress={this.props.onClose}
onModalShow={() => {
if (this.props.shouldSetModalVisibility) {
Modal.setModalVisibility(true);
}
this.props.onModalShow();
}}
propagateSwipe={this.props.propagateSwipe}
onModalHide={this.hideModal}
onSwipeComplete={this.props.onClose}
swipeDirection={swipeDirection}
isVisible={this.props.isVisible}
backdropColor={themeColors.overlay}
backdropOpacity={hideBackdrop ? 0 : variables.overlayOpacity}
backdropTransitionOutTiming={0}
hasBackdrop={this.props.fullscreen}
coverScreen={this.props.fullscreen}
style={modalStyle}
// When `statusBarTranslucent` is true on Android, the modal fully covers the status bar.
// Since `windowHeight` doesn't include status bar height, it should be added in the `deviceHeight` calculation.
deviceHeight={this.props.windowHeight + ((this.props.statusBarTranslucent && StatusBar.currentHeight) || 0)}
deviceWidth={this.props.windowWidth}
animationIn={this.props.animationIn || animationIn}
animationOut={this.props.animationOut || animationOut}
useNativeDriver={this.props.useNativeDriver}
hideModalContentWhileAnimating={this.props.hideModalContentWhileAnimating}
animationInTiming={this.props.animationInTiming}
animationOutTiming={this.props.animationOutTiming}
statusBarTranslucent={this.props.statusBarTranslucent}
>
<SafeAreaInsetsContext.Consumer>
{(insets) => {
const {
paddingTop: safeAreaPaddingTop,
paddingBottom: safeAreaPaddingBottom,
paddingLeft: safeAreaPaddingLeft,
paddingRight: safeAreaPaddingRight,
} = StyleUtils.getSafeAreaPadding(insets);
const modalPaddingStyles = StyleUtils.getModalPaddingStyles({
safeAreaPaddingTop,
safeAreaPaddingBottom,
safeAreaPaddingLeft,
safeAreaPaddingRight,
shouldAddBottomSafeAreaMargin,
shouldAddTopSafeAreaMargin,
shouldAddBottomSafeAreaPadding,
shouldAddTopSafeAreaPadding,
modalContainerStyleMarginTop: modalContainerStyle.marginTop,
modalContainerStyleMarginBottom: modalContainerStyle.marginBottom,
modalContainerStylePaddingTop: modalContainerStyle.paddingTop,
modalContainerStylePaddingBottom: modalContainerStyle.paddingBottom,
});
return (
<View
style={{
...styles.defaultModalContainer,
...modalContainerStyle,
...modalPaddingStyles,
}}
ref={this.props.forwardedRef}
>
{this.props.children}
</View>
);
}}
</SafeAreaInsetsContext.Consumer>
</ReactNativeModal>
);
}
}
BaseModal.propTypes = propTypes;
BaseModal.defaultProps = defaultProps;
export default React.forwardRef((props, ref) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<BaseModal {...props} forwardedRef={ref} />
));