1
- var React = require ( 'react' ) ;
2
- var ReactDOM = require ( 'react-dom' ) ;
3
- var DOMFactories = require ( 'react-dom-factories' ) ;
4
- var PropTypes = require ( 'prop-types' ) ;
5
- var ExecutionEnvironment = require ( 'exenv' ) ;
6
- var ModalPortal = React . createFactory ( require ( './ModalPortal' ) ) ;
7
- var ariaAppHider = require ( '../helpers/ariaAppHider' ) ;
8
- var refCount = require ( '../helpers/refCount' ) ;
9
- var elementClass = require ( 'element-class' ) ;
10
- var renderSubtreeIntoContainer = require ( "react-dom" ) . unstable_renderSubtreeIntoContainer ;
11
- var Assign = require ( 'lodash.assign' ) ;
12
- var createReactClass = require ( 'create-react-class' ) ;
13
-
14
- var SafeHTMLElement = ExecutionEnvironment . canUseDOM ? window . HTMLElement : { } ;
15
- var AppElement = ExecutionEnvironment . canUseDOM ? document . body : { appendChild : function ( ) { } } ;
1
+ import React , { Component } from 'react' ;
2
+ import ReactDOM from 'react-dom' ;
3
+ import PropTypes from 'prop-types' ;
4
+ import ExecutionEnvironment from 'exenv' ;
5
+ import ModalPortal from './ModalPortal' ;
6
+ import elementClass from 'element-class' ;
7
+ import * as ariaAppHider from '../helpers/ariaAppHider' ;
8
+ import * as refCount from '../helpers/refCount' ;
9
+
10
+ const renderSubtreeIntoContainer = ReactDOM . unstable_renderSubtreeIntoContainer ;
11
+
12
+ const SafeHTMLElement = ExecutionEnvironment . canUseDOM ? window . HTMLElement : { } ;
13
+ let AppElement = ExecutionEnvironment . canUseDOM ? document . body : { appendChild : function ( ) { } } ;
16
14
17
15
function getParentElement ( parentSelector ) {
18
16
return parentSelector ( ) ;
19
17
}
20
18
21
- var Modal = createReactClass ( {
19
+ export default class Modal extends Component {
20
+ static setAppElement = function ( element ) {
21
+ AppElement = ariaAppHider . setElement ( element ) ;
22
+ } ;
22
23
23
- displayName : 'Modal' ,
24
- statics : {
25
- setAppElement : function ( element ) {
26
- AppElement = ariaAppHider . setElement ( element ) ;
27
- } ,
28
- injectCSS : function ( ) {
29
- "production" !== process . env . NODE_ENV
30
- && console . warn ( 'React-Modal: injectCSS has been deprecated ' +
31
- 'and no longer has any effect. It will be removed in a later version' ) ;
32
- }
33
- } ,
24
+ static injectCSS = function ( ) {
25
+ "production" !== process . env . NODE_ENV
26
+ && console . warn ( 'React-Modal: injectCSS has been deprecated ' +
27
+ 'and no longer has any effect. It will be removed in a later version' ) ;
28
+ } ;
34
29
35
- propTypes : {
30
+ static propTypes = {
36
31
isOpen : PropTypes . bool . isRequired ,
37
32
style : PropTypes . shape ( {
38
33
content : PropTypes . object ,
@@ -49,52 +44,75 @@ var Modal = createReactClass({
49
44
parentSelector : PropTypes . func ,
50
45
role : PropTypes . string ,
51
46
contentLabel : PropTypes . string . isRequired
52
- } ,
53
-
54
- getDefaultProps : function ( ) {
55
- return {
56
- isOpen : false ,
57
- portalClassName : 'ReactModalPortal' ,
58
- bodyOpenClassName : 'ReactModal__Body--open' ,
59
- ariaHideApp : true ,
60
- closeTimeoutMS : 0 ,
61
- shouldCloseOnOverlayClick : true ,
62
- parentSelector : function ( ) { return document . body ; }
63
- } ;
64
- } ,
65
-
66
- componentDidMount : function ( ) {
47
+ } ;
48
+
49
+ static defaultProps = {
50
+ isOpen : false ,
51
+ portalClassName : 'ReactModalPortal' ,
52
+ bodyOpenClassName : 'ReactModal__Body--open' ,
53
+ ariaHideApp : true ,
54
+ closeTimeoutMS : 0 ,
55
+ shouldCloseOnOverlayClick : true ,
56
+ parentSelector : function ( ) { return document . body ; }
57
+ } ;
58
+
59
+ static defaultStyles = {
60
+ overlay : {
61
+ position : 'fixed' ,
62
+ top : 0 ,
63
+ left : 0 ,
64
+ right : 0 ,
65
+ bottom : 0 ,
66
+ backgroundColor : 'rgba(255, 255, 255, 0.75)'
67
+ } ,
68
+ content : {
69
+ position : 'absolute' ,
70
+ top : '40px' ,
71
+ left : '40px' ,
72
+ right : '40px' ,
73
+ bottom : '40px' ,
74
+ border : '1px solid #ccc' ,
75
+ background : '#fff' ,
76
+ overflow : 'auto' ,
77
+ WebkitOverflowScrolling : 'touch' ,
78
+ borderRadius : '4px' ,
79
+ outline : 'none' ,
80
+ padding : '20px'
81
+ }
82
+ } ;
83
+
84
+ componentDidMount ( ) {
67
85
this . node = document . createElement ( 'div' ) ;
68
86
this . node . className = this . props . portalClassName ;
69
87
70
88
if ( this . props . isOpen ) refCount . add ( this ) ;
71
89
72
- var parent = getParentElement ( this . props . parentSelector ) ;
90
+ const parent = getParentElement ( this . props . parentSelector ) ;
73
91
parent . appendChild ( this . node ) ;
74
92
this . renderPortal ( this . props ) ;
75
- } ,
93
+ }
76
94
77
- componentWillUpdate : function ( newProps ) {
95
+ componentWillUpdate ( newProps ) {
78
96
if ( newProps . portalClassName !== this . props . portalClassName ) {
79
97
this . node . className = newProps . portalClassName ;
80
98
}
81
- } ,
99
+ }
82
100
83
- componentWillReceiveProps : function ( newProps ) {
101
+ componentWillReceiveProps ( newProps ) {
84
102
if ( newProps . isOpen ) refCount . add ( this ) ;
85
103
if ( ! newProps . isOpen ) refCount . remove ( this ) ;
86
- var currentParent = getParentElement ( this . props . parentSelector ) ;
87
- var newParent = getParentElement ( newProps . parentSelector ) ;
104
+ const currentParent = getParentElement ( this . props . parentSelector ) ;
105
+ const newParent = getParentElement ( newProps . parentSelector ) ;
88
106
89
- if ( newParent !== currentParent ) {
107
+ if ( newParent !== currentParent ) {
90
108
currentParent . removeChild ( this . node ) ;
91
109
newParent . appendChild ( this . node ) ;
92
110
}
93
111
94
112
this . renderPortal ( newProps ) ;
95
- } ,
113
+ }
96
114
97
- componentWillUnmount : function ( ) {
115
+ componentWillUnmount ( ) {
98
116
if ( ! this . node ) return ;
99
117
100
118
refCount . remove ( this ) ;
@@ -103,9 +121,9 @@ var Modal = createReactClass({
103
121
ariaAppHider . show ( this . props . appElement ) ;
104
122
}
105
123
106
- var state = this . portal . state ;
107
- var now = Date . now ( ) ;
108
- var closesAt = state . isOpen && this . props . closeTimeoutMS
124
+ const state = this . portal . state ;
125
+ const now = Date . now ( ) ;
126
+ const closesAt = state . isOpen && this . props . closeTimeoutMS
109
127
&& ( state . closesAt
110
128
|| now + this . props . closeTimeoutMS ) ;
111
129
@@ -114,24 +132,24 @@ var Modal = createReactClass({
114
132
this . portal . closeWithTimeout ( ) ;
115
133
}
116
134
117
- var that = this ;
135
+ const that = this ;
118
136
setTimeout ( function ( ) { that . removePortal ( ) ; } , closesAt - now ) ;
119
137
} else {
120
138
this . removePortal ( ) ;
121
139
}
122
- } ,
140
+ }
123
141
124
- removePortal : function ( ) {
142
+ removePortal = ( ) => {
125
143
ReactDOM . unmountComponentAtNode ( this . node ) ;
126
- var parent = getParentElement ( this . props . parentSelector ) ;
144
+ const parent = getParentElement ( this . props . parentSelector ) ;
127
145
parent . removeChild ( this . node ) ;
128
146
129
147
if ( refCount . count ( ) === 0 ) {
130
148
elementClass ( document . body ) . remove ( this . props . bodyOpenClassName ) ;
131
149
}
132
- } ,
150
+ }
133
151
134
- renderPortal : function ( props ) {
152
+ renderPortal = props => {
135
153
if ( props . isOpen || refCount . count ( ) > 0 ) {
136
154
elementClass ( document . body ) . add ( this . props . bodyOpenClassName ) ;
137
155
} else {
@@ -142,37 +160,12 @@ var Modal = createReactClass({
142
160
ariaAppHider . toggle ( props . isOpen , props . appElement ) ;
143
161
}
144
162
145
- this . portal = renderSubtreeIntoContainer ( this , ModalPortal ( Assign ( { } , props , { defaultStyles : Modal . defaultStyles } ) ) , this . node ) ;
146
- } ,
147
-
148
- render : function ( ) {
149
- return DOMFactories . noscript ( ) ;
163
+ this . portal = renderSubtreeIntoContainer ( this , (
164
+ < ModalPortal defaultStyles = { Modal . defaultStyles } { ...props } />
165
+ ) , this . node ) ;
150
166
}
151
- } ) ;
152
-
153
- Modal . defaultStyles = {
154
- overlay : {
155
- position : 'fixed' ,
156
- top : 0 ,
157
- left : 0 ,
158
- right : 0 ,
159
- bottom : 0 ,
160
- backgroundColor : 'rgba(255, 255, 255, 0.75)'
161
- } ,
162
- content : {
163
- position : 'absolute' ,
164
- top : '40px' ,
165
- left : '40px' ,
166
- right : '40px' ,
167
- bottom : '40px' ,
168
- border : '1px solid #ccc' ,
169
- background : '#fff' ,
170
- overflow : 'auto' ,
171
- WebkitOverflowScrolling : 'touch' ,
172
- borderRadius : '4px' ,
173
- outline : 'none' ,
174
- padding : '20px'
167
+
168
+ render ( ) {
169
+ return null ;
175
170
}
176
171
}
177
-
178
- module . exports = Modal
0 commit comments