forked from reactjs/react-modal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModal.js
More file actions
154 lines (132 loc) · 4.24 KB
/
Copy pathModal.js
File metadata and controls
154 lines (132 loc) · 4.24 KB
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
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import elementClass from 'element-class';
import ModalPortal from './ModalPortal';
import * as ariaAppHider from '../helpers/ariaAppHider';
const renderSubtreeIntoContainer = ReactDOM.unstable_renderSubtreeIntoContainer;
function getParentElement (parentSelector) {
return parentSelector();
}
export default class Modal extends Component {
/* eslint-disable react/no-unused-prop-types */
static propTypes = {
isOpen: React.PropTypes.bool.isRequired,
style: React.PropTypes.shape({
content: React.PropTypes.object,
overlay: React.PropTypes.object
}),
portalClassName: React.PropTypes.string,
bodyOpenClassName: React.PropTypes.string,
/**
* A function that returns the appElement that will be aria-hidden
* when the modal is open. The function should return a DOMElement or
* an array of DOMElements.
*/
getAppElement: React.PropTypes.func.isRequired,
onAfterOpen: React.PropTypes.func,
onRequestClose: React.PropTypes.func,
closeTimeoutMS: React.PropTypes.number,
ariaHideApp: React.PropTypes.bool,
shouldCloseOnOverlayClick: React.PropTypes.bool,
parentSelector: React.PropTypes.func,
role: React.PropTypes.string,
contentLabel: React.PropTypes.string.isRequired
};
/* eslint-enable react/no-unused-prop-types */
static defaultProps = {
isOpen: false,
portalClassName: 'ReactModalPortal',
bodyOpenClassName: 'ReactModal__Body--open',
ariaHideApp: true,
closeTimeoutMS: 0,
shouldCloseOnOverlayClick: true,
parentSelector () { return document.body; }
};
static defaultStyles = {
overlay: {
position: 'fixed',
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: 'rgba(255, 255, 255, 0.75)'
},
content: {
position: 'absolute',
top: '40px',
left: '40px',
right: '40px',
bottom: '40px',
border: '1px solid #ccc',
background: '#fff',
overflow: 'auto',
WebkitOverflowScrolling: 'touch',
borderRadius: '4px',
outline: 'none',
padding: '20px'
}
};
static injectCSS () {
return process.env.NODE_ENV !== 'production'
&& console.warn('React-Modal: injectCSS has been deprecated ' +
'and no longer has any effect. It will be removed in a later version');
}
componentDidMount () {
this.node = document.createElement('div');
this.node.className = this.props.portalClassName;
const parent = getParentElement(this.props.parentSelector);
parent.appendChild(this.node);
this.renderPortal(this.props);
}
componentWillReceiveProps (newProps) {
const currentParent = getParentElement(this.props.parentSelector);
const newParent = getParentElement(newProps.parentSelector);
if (newParent !== currentParent) {
currentParent.removeChild(this.node);
newParent.appendChild(this.node);
}
this.renderPortal(newProps);
}
componentWillUnmount () {
if (this.props.ariaHideApp) {
ariaAppHider.show(this.props.getAppElement());
}
const state = this.portal.state;
const now = Date.now();
const closesAt = state.isOpen && this.props.closeTimeoutMS
&& (state.closesAt
|| now + this.props.closeTimeoutMS);
if (closesAt) {
if (!state.beforeClose) {
this.portal.closeWithTimeout();
}
setTimeout(this.removePortal.bind(this), closesAt - now);
} else {
this.removePortal();
}
}
removePortal () {
ReactDOM.unmountComponentAtNode(this.node);
const parent = getParentElement(this.props.parentSelector);
parent.removeChild(this.node);
elementClass(document.body).remove(this.props.bodyOpenClassName);
}
renderPortal (props) {
if (props.isOpen) {
elementClass(document.body).add(this.props.bodyOpenClassName);
} else {
elementClass(document.body).remove(this.props.bodyOpenClassName);
}
if (props.ariaHideApp) {
ariaAppHider.toggle(this.props.getAppElement(), props.isOpen);
}
this.portal = renderSubtreeIntoContainer(this,
<ModalPortal
{...props}
defaultStyles={Modal.defaultStyles}
/>, this.node);
}
render () {
return null;
}
}