forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverlay.jsx
111 lines (89 loc) · 2.54 KB
/
overlay.jsx
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
let React = require('react');
let StylePropable = require('./mixins/style-propable');
let Transitions = require('./styles/transitions');
let Colors = require('./styles/colors');
let Overlay = React.createClass({
_originalBodyOverflow: '',
mixins: [StylePropable],
propTypes: {
autoLockScrolling: React.PropTypes.bool,
show: React.PropTypes.bool,
transitionEnabled: React.PropTypes.bool,
},
getDefaultProps() {
return {
autoLockScrolling: true,
transitionEnabled: true,
};
},
componentDidMount() {
this._originalBodyOverflow = document.getElementsByTagName('body')[0].style.oveflow;
},
componentDidUpdate() {
if (this.props.autoLockScrolling) (this.props.show) ? this._preventScrolling() : this._allowScrolling();
},
componentWillUnmount() {
this._allowScrolling();
},
setOpacity(opacity) {
let overlay = React.findDOMNode(this);
overlay.style.opacity = opacity;
},
getStyles() {
let styles = {
root: {
position: 'fixed',
height: '100%',
width: '100%',
zIndex: 9,
top: 0,
left: '-100%',
opacity: 0,
backgroundColor: Colors.lightBlack,
WebkitTapHighlightColor: 'rgba(0, 0, 0, 0)',
// Two ways to promote overlay to its own render layer
willChange: 'opacity',
transform: 'translateZ(0)',
transition:
this.props.transitionEnabled &&
Transitions.easeOut('0ms', 'left', '400ms') + ',' +
Transitions.easeOut('400ms', 'opacity'),
},
rootWhenShown: {
left: '0',
opacity: 1,
transition:
this.props.transitionEnabled &&
Transitions.easeOut('0ms', 'left') + ',' +
Transitions.easeOut('400ms', 'opacity'),
},
};
return styles;
},
render() {
let {
show,
style,
...other,
} = this.props;
let styles = this.mergeAndPrefix(this.getStyles().root, this.props.style, this.props.show && this.getStyles().rootWhenShown);
return (
<div {...other} style={styles} />
);
},
preventScrolling() {
if (!this.props.autoLockScrolling) this._preventScrolling();
},
allowScrolling() {
if (!this.props.autoLockScrolling) this._allowScrolling();
},
_preventScrolling() {
let body = document.getElementsByTagName('body')[0];
body.style.overflow = 'hidden';
},
_allowScrolling() {
let body = document.getElementsByTagName('body')[0];
body.style.overflow = this._originalBodyOverflow || '';
},
});
module.exports = Overlay;