@@ -3,12 +3,17 @@ import ReactDOM from 'react-dom';
3
3
import PropTypes from 'prop-types' ;
4
4
import ModalPortal from './ModalPortal' ;
5
5
import * as ariaAppHider from '../helpers/ariaAppHider' ;
6
- import SafeHTMLElement from '../helpers/safeHTMLElement' ;
6
+ import SafeHTMLElement , {
7
+ canUseDOM
8
+ } from '../helpers/safeHTMLElement' ;
7
9
8
10
export const portalClassName = 'ReactModalPortal' ;
9
11
export const bodyOpenClassName = 'ReactModal__Body--open' ;
10
12
11
- const renderSubtreeIntoContainer = ReactDOM . unstable_renderSubtreeIntoContainer ;
13
+ const isReact16 = ReactDOM . createPortal !== undefined ;
14
+ const createPortal = isReact16 ?
15
+ ReactDOM . createPortal :
16
+ ReactDOM . unstable_renderSubtreeIntoContainer ;
12
17
13
18
function getParentElement ( parentSelector ) {
14
19
return parentSelector ( ) ;
@@ -19,16 +24,6 @@ export default class Modal extends Component {
19
24
ariaAppHider . setElement ( element ) ;
20
25
}
21
26
22
- /* eslint-disable no-console */
23
- static injectCSS ( ) {
24
- ( process . env . NODE_ENV !== "production" )
25
- && console . warn (
26
- 'React-Modal: injectCSS has been deprecated ' +
27
- 'and no longer has any effect. It will be removed in a later version'
28
- ) ;
29
- }
30
- /* eslint-enable no-console */
31
-
32
27
/* eslint-disable react/no-unused-prop-types */
33
28
static propTypes = {
34
29
isOpen : PropTypes . bool . isRequired ,
@@ -97,16 +92,21 @@ export default class Modal extends Component {
97
92
} ;
98
93
99
94
componentDidMount ( ) {
100
- this . node = document . createElement ( 'div' ) ;
95
+ if ( ! canUseDOM ) return ;
96
+
97
+ if ( ! isReact16 ) {
98
+ this . node = document . createElement ( 'div' ) ;
99
+ }
101
100
this . node . className = this . props . portalClassName ;
102
101
103
102
const parent = getParentElement ( this . props . parentSelector ) ;
104
103
parent . appendChild ( this . node ) ;
105
104
106
- this . renderPortal ( this . props ) ;
105
+ ( ! isReact16 ) && this . renderPortal ( this . props ) ;
107
106
}
108
107
109
108
componentWillReceiveProps ( newProps ) {
109
+ if ( ! canUseDOM ) return ;
110
110
const { isOpen } = newProps ;
111
111
// Stop unnecessary renders if modal is remaining closed
112
112
if ( ! this . props . isOpen && ! isOpen ) return ;
@@ -119,17 +119,18 @@ export default class Modal extends Component {
119
119
newParent . appendChild ( this . node ) ;
120
120
}
121
121
122
- this . renderPortal ( newProps ) ;
122
+ ( ! isReact16 ) && this . renderPortal ( newProps ) ;
123
123
}
124
124
125
125
componentWillUpdate ( newProps ) {
126
+ if ( ! canUseDOM ) return ;
126
127
if ( newProps . portalClassName !== this . props . portalClassName ) {
127
128
this . node . className = newProps . portalClassName ;
128
129
}
129
130
}
130
131
131
132
componentWillUnmount ( ) {
132
- if ( ! this . node || ! this . portal ) return ;
133
+ if ( ! canUseDOM || ! this . node || ! this . portal ) return ;
133
134
134
135
const state = this . portal . state ;
135
136
const now = Date . now ( ) ;
@@ -149,18 +150,34 @@ export default class Modal extends Component {
149
150
}
150
151
151
152
removePortal = ( ) => {
152
- ReactDOM . unmountComponentAtNode ( this . node ) ;
153
+ ( ! isReact16 ) && ReactDOM . unmountComponentAtNode ( this . node ) ;
153
154
const parent = getParentElement ( this . props . parentSelector ) ;
154
155
parent . removeChild ( this . node ) ;
155
156
}
156
157
158
+ portalRef = ref => { this . portal = ref ; }
159
+
157
160
renderPortal = props => {
158
- this . portal = renderSubtreeIntoContainer ( this , (
161
+ const portal = createPortal ( this , (
159
162
< ModalPortal defaultStyles = { Modal . defaultStyles } { ...props } />
160
163
) , this . node ) ;
164
+ this . portalRef ( portal ) ;
161
165
}
162
166
163
167
render ( ) {
164
- return null ;
168
+ if ( ! canUseDOM || ! isReact16 ) {
169
+ return null ;
170
+ }
171
+
172
+ if ( ! this . node && isReact16 ) {
173
+ this . node = document . createElement ( 'div' ) ;
174
+ }
175
+
176
+ return createPortal (
177
+ < ModalPortal ref = { this . portalRef }
178
+ defaultStyles = { Modal . defaultStyles }
179
+ { ...this . props } /> ,
180
+ this . node
181
+ ) ;
165
182
}
166
183
}
0 commit comments