forked from rgabs/react-native-modal-overlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (89 loc) · 3.4 KB
/
index.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
import React from 'react';
import PropTypes from 'prop-types';
import { Modal, TouchableWithoutFeedback, View } from 'react-native';
import * as Animatable from 'react-native-animatable';
const AnimatableTouchableWithoutFeedback = Animatable.createAnimatableComponent(TouchableWithoutFeedback);
const styles = {
container: {
flex: 1,
justifyContent: 'center',
padding: 20,
backgroundColor: 'rgba(37, 8, 10, 0.78)',
},
innerContainer: {
alignItems: 'center',
backgroundColor: 'white',
padding: 20,
}
};
class Overlay extends React.Component {
state = {
visible: this.props.visible,
animationType: this.props.animationType,
overlayAnimationType: 'fadeIn'
};
static propTypes = {
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
animationType: PropTypes.string,
animationOutType: PropTypes.string,
easing: PropTypes.string,
visible: PropTypes.bool.isRequired,
closeOnTouchOutside: PropTypes.bool,
onClose: PropTypes.func.isRequired,
containerStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
childrenWrapperStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
animationDuration: PropTypes.number,
accessible: PropTypes.bool,
}
static defaultProps = {
children: null,
animationType: 'fadeIn',
animationOutType: 'fadeOut',
easing: 'ease',
visible: false,
closeOnTouchOutside: false,
onClose: () => { },
animationDuration: 500,
accessible: true,
}
static getDerivedStateFromProps(props) {
return { visible: props.visible, animationType: props.animationType };
}
_hideModal = () => {
const { animationOutType, animationDuration, onClose } = this.props;
this.setState({ animationType: animationOutType, overlayAnimationType: animationOutType });
let timer = setTimeout(() => {
onClose();
clearTimeout(timer);
this.setState({ overlayAnimationType: 'fadeIn' });
}, animationDuration - 100);
}
_stopPropagation = (e) => e.stopPropagation()
render() {
const { closeOnTouchOutside, animationDuration, children,
accessible,
containerStyle, childrenWrapperStyle, easing, ...extraProps } = this.props;
return (
<Modal
transparent
visible={this.state.visible}
onRequestClose={this._hideModal}
onDismiss={this._hideModal}
{...extraProps} animationType='none'>
<TouchableWithoutFeedback onPress={closeOnTouchOutside ? this._hideModal : null} accessible={accessible}>
<Animatable.View animation={this.state.overlayAnimationType} duration={animationDuration} easing={easing}
useNativeDriver style={[styles.container, containerStyle]}>
<AnimatableTouchableWithoutFeedback accessible={accessible} animation={this.state.animationType} easing={easing}
duration={animationDuration} useNativeDriver onPress={this._stopPropagation}>
<View style={[styles.innerContainer, childrenWrapperStyle]}>
{children instanceof Function ? children(this._hideModal, this.state) : children}
{/* Passing this reference in render prop so that the parent component can access all the children's methods */}
</View>
</AnimatableTouchableWithoutFeedback>
</Animatable.View>
</TouchableWithoutFeedback>
</Modal>
);
}
}
export default Overlay;