@@ -8,7 +8,11 @@ import SafeHTMLElement from '../helpers/safeHTMLElement';
8
8
export const portalClassName = 'ReactModalPortal' ;
9
9
export const bodyOpenClassName = 'ReactModal__Body--open' ;
10
10
11
- const renderSubtreeIntoContainer = ReactDOM . unstable_renderSubtreeIntoContainer ;
11
+ const canUseDOM = typeof window !== undefined ;
12
+ const isReact16 = ReactDOM . createPortal !== undefined ;
13
+ const createPortal = isReact16 ?
14
+ ReactDOM . createPortal :
15
+ ReactDOM . unstable_renderSubtreeIntoContainer ;
12
16
13
17
function getParentElement ( parentSelector ) {
14
18
return parentSelector ( ) ;
@@ -97,16 +101,17 @@ export default class Modal extends Component {
97
101
} ;
98
102
99
103
componentDidMount ( ) {
100
- this . node = document . createElement ( 'div' ) ;
104
+ if ( ! canUseDOM ) return ;
101
105
this . node . className = this . props . portalClassName ;
102
106
103
107
const parent = getParentElement ( this . props . parentSelector ) ;
104
108
parent . appendChild ( this . node ) ;
105
109
106
- this . renderPortal ( this . props ) ;
110
+ ( ! isReact16 ) && this . renderPortal ( this . props ) ;
107
111
}
108
112
109
113
componentWillReceiveProps ( newProps ) {
114
+ if ( ! canUseDOM ) return ;
110
115
const { isOpen } = newProps ;
111
116
// Stop unnecessary renders if modal is remaining closed
112
117
if ( ! this . props . isOpen && ! isOpen ) return ;
@@ -119,17 +124,18 @@ export default class Modal extends Component {
119
124
newParent . appendChild ( this . node ) ;
120
125
}
121
126
122
- this . renderPortal ( newProps ) ;
127
+ ( ! isReact16 ) && this . renderPortal ( newProps ) ;
123
128
}
124
129
125
130
componentWillUpdate ( newProps ) {
131
+ if ( ! canUseDOM ) return ;
126
132
if ( newProps . portalClassName !== this . props . portalClassName ) {
127
133
this . node . className = newProps . portalClassName ;
128
134
}
129
135
}
130
136
131
137
componentWillUnmount ( ) {
132
- if ( ! this . node || ! this . portal ) return ;
138
+ if ( ! canUseDOM || ! this . node || ! this . portal ) return ;
133
139
134
140
const state = this . portal . state ;
135
141
const now = Date . now ( ) ;
@@ -149,18 +155,34 @@ export default class Modal extends Component {
149
155
}
150
156
151
157
removePortal = ( ) => {
152
- ReactDOM . unmountComponentAtNode ( this . node ) ;
158
+ ( ! isReact16 ) && ReactDOM . unmountComponentAtNode ( this . node ) ;
153
159
const parent = getParentElement ( this . props . parentSelector ) ;
154
160
parent . removeChild ( this . node ) ;
155
161
}
156
162
163
+ portalRef = ref => { this . portal = ref ; }
164
+
157
165
renderPortal = props => {
158
- this . portal = renderSubtreeIntoContainer ( this , (
166
+ const portal = createPortal ( this , (
159
167
< ModalPortal defaultStyles = { Modal . defaultStyles } { ...props } />
160
168
) , this . node ) ;
169
+ this . portalRef ( portal ) ;
161
170
}
162
171
163
172
render ( ) {
164
- return null ;
173
+ if ( ! canUseDOM || ! isReact16 ) {
174
+ return null ;
175
+ }
176
+
177
+ if ( ! this . node ) {
178
+ this . node = document . createElement ( 'div' ) ;
179
+ }
180
+
181
+ return createPortal (
182
+ < ModalPortal ref = { this . portalRef }
183
+ defaultStyles = { Modal . defaultStyles }
184
+ { ...this . props } /> ,
185
+ this . node
186
+ ) ;
165
187
}
166
188
}
0 commit comments