-
Notifications
You must be signed in to change notification settings - Fork 0
/
Overlay.js
69 lines (62 loc) · 1.86 KB
/
Overlay.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
import React, { Component } from 'react';
import { StyleSheet } from 'react-native';
import PropTypes from 'prop-types';
import * as Animatable from 'react-native-animatable';
class Overlay extends Component {
constructor(props) {
super(props);
this.state = {
renderChildren: props.visible,
};
}
componentWillReceiveProps(nextProps) {
if (this.props.visible !== nextProps.visible) {
setTimeout(() => {
this.setState({ renderChildren: nextProps.visible });
}, nextProps.visible ? 0 : this.props.duration);
}
}
render() {
const { states, visible, duration, style, children, ...otherProps } = this.props;
const transitions = [];
const additionalStyles = {};
for (const [key, value] of Object.entries(states)) {
if (!transitions.includes(key)) {
transitions.push(key);
}
if (key === 'opacity') {
additionalStyles.opacity = visible ? value[1] : value[0];
} else {
additionalStyles.transform = additionalStyles.transform || [];
const item = {};
item[key] = visible ? value[1] : value[0];
additionalStyles.transform.push(item);
}
}
return (
<Animatable.View
pointerEvents={this.state.renderChildren ? 'auto' : 'none'}
duration={duration}
transition={transitions}
style={[style, StyleSheet.absoluteFill, additionalStyles]}
{...otherProps}>
{this.state.renderChildren && children}
</Animatable.View>
);
}
}
Overlay.propTypes = {
visible: PropTypes.bool.isRequired,
states: PropTypes.objectOf(PropTypes.arrayOf(PropTypes.number)).isRequired,
duration: PropTypes.number,
style: PropTypes.any,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
]),
};
Overlay.defaultProps = {
visible: true,
duration: 200,
};
export default Overlay;