-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathBaseModal.js
139 lines (128 loc) · 5.29 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
import React, {PureComponent} from 'react';
import {View} from 'react-native';
import ReactNativeModal from 'react-native-modal';
import {SafeAreaInsetsContext} from 'react-native-safe-area-context';
import KeyboardShortcut from '../../libs/KeyboardShortcut';
import styles, {getModalPaddingStyles, getSafeAreaPadding} from '../../styles/styles';
import themeColors from '../../styles/themes/default';
import {propTypes, defaultProps} from './ModalPropTypes';
import getModalStyles from '../../styles/getModalStyles';
import {setModalVisibility} from '../../libs/actions/Modal';
class BaseModal extends PureComponent {
constructor(props) {
super(props);
this.hideModalAndRemoveEventListeners = this.hideModalAndRemoveEventListeners.bind(this);
this.subscribeToKeyEvents = this.subscribeToKeyEvents.bind(this);
this.unsubscribeFromKeyEvents = this.unsubscribeFromKeyEvents.bind(this);
}
componentWillUnmount() {
this.hideModalAndRemoveEventListeners();
}
/**
* Hides modal and unsubscribes from key event listeners
*/
hideModalAndRemoveEventListeners() {
this.unsubscribeFromKeyEvents();
setModalVisibility(false);
this.props.onModalHide();
}
/**
* Listens to specific keyboard keys when the modal has been opened
*/
subscribeToKeyEvents() {
KeyboardShortcut.subscribe('Escape', this.props.onClose, [], true);
KeyboardShortcut.subscribe('Enter', this.props.onSubmit, [], true);
}
/**
* Stops listening to keyboard keys when modal has been closed
*/
unsubscribeFromKeyEvents() {
KeyboardShortcut.unsubscribe('Escape');
KeyboardShortcut.unsubscribe('Enter');
}
render() {
const {
modalStyle,
modalContainerStyle,
swipeDirection,
animationIn,
animationOut,
shouldAddTopSafeAreaPadding,
shouldAddBottomSafeAreaPadding,
hideBackdrop,
} = getModalStyles(
this.props.type,
{
windowWidth: this.props.windowWidth,
windowHeight: this.props.windowHeight,
isSmallScreenWidth: this.props.isSmallScreenWidth,
},
this.props.popoverAnchorPosition,
);
return (
<ReactNativeModal
onBackdropPress={(e) => {
if (e && e.type === 'keydown' && e.key === 'Enter') {
return;
}
this.props.onClose();
}}
onBackButtonPress={this.props.onClose}
onModalShow={() => {
this.subscribeToKeyEvents();
setModalVisibility(true);
this.props.onModalShow();
}}
onModalHide={this.hideModalAndRemoveEventListeners}
onSwipeComplete={this.props.onClose}
swipeDirection={swipeDirection}
isVisible={this.props.isVisible}
backdropColor={themeColors.modalBackdrop}
backdropOpacity={hideBackdrop ? 0 : 0.5}
backdropTransitionOutTiming={0}
style={modalStyle}
deviceHeight={this.props.windowHeight}
deviceWidth={this.props.windowWidth}
animationIn={this.props.animationIn || animationIn}
animationOut={this.props.animationOut || animationOut}
useNativeDriver={this.props.useNativeDriver}
statusBarTranslucent
hideModalContentWhileAnimating={this.props.hideModalContentWhileAnimating}
animationInTiming={this.props.animationInTiming}
animationOutTiming={this.props.animationOutTiming}
>
<SafeAreaInsetsContext.Consumer>
{(insets) => {
const {
paddingTop: safeAreaPaddingTop,
paddingBottom: safeAreaPaddingBottom,
} = getSafeAreaPadding(insets);
const modalPaddingStyles = getModalPaddingStyles({
safeAreaPaddingTop,
safeAreaPaddingBottom,
shouldAddBottomSafeAreaPadding,
shouldAddTopSafeAreaPadding,
modalContainerStylePaddingTop: modalContainerStyle.paddingTop,
modalContainerStylePaddingBottom: modalContainerStyle.paddingBottom,
});
return (
<View
style={{
...styles.defaultModalContainer,
...modalContainerStyle,
...modalPaddingStyles,
}}
>
{this.props.children}
</View>
);
}}
</SafeAreaInsetsContext.Consumer>
</ReactNativeModal>
);
}
}
BaseModal.propTypes = propTypes;
BaseModal.defaultProps = defaultProps;
BaseModal.displayName = 'BaseModal';
export default BaseModal;