@@ -8,7 +8,11 @@ import SafeHTMLElement from '../helpers/safeHTMLElement';
88export const portalClassName = 'ReactModalPortal' ;
99export const bodyOpenClassName = 'ReactModal__Body--open' ;
1010
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 ;
1216
1317function getParentElement ( parentSelector ) {
1418 return parentSelector ( ) ;
@@ -97,16 +101,17 @@ export default class Modal extends Component {
97101 } ;
98102
99103 componentDidMount ( ) {
100- this . node = document . createElement ( 'div' ) ;
104+ if ( ! canUseDOM ) return ;
101105 this . node . className = this . props . portalClassName ;
102106
103107 const parent = getParentElement ( this . props . parentSelector ) ;
104108 parent . appendChild ( this . node ) ;
105109
106- this . renderPortal ( this . props ) ;
110+ ( ! isReact16 ) && this . renderPortal ( this . props ) ;
107111 }
108112
109113 componentWillReceiveProps ( newProps ) {
114+ if ( ! canUseDOM ) return ;
110115 const { isOpen } = newProps ;
111116 // Stop unnecessary renders if modal is remaining closed
112117 if ( ! this . props . isOpen && ! isOpen ) return ;
@@ -119,17 +124,18 @@ export default class Modal extends Component {
119124 newParent . appendChild ( this . node ) ;
120125 }
121126
122- this . renderPortal ( newProps ) ;
127+ ( ! isReact16 ) && this . renderPortal ( newProps ) ;
123128 }
124129
125130 componentWillUpdate ( newProps ) {
131+ if ( ! canUseDOM ) return ;
126132 if ( newProps . portalClassName !== this . props . portalClassName ) {
127133 this . node . className = newProps . portalClassName ;
128134 }
129135 }
130136
131137 componentWillUnmount ( ) {
132- if ( ! this . node || ! this . portal ) return ;
138+ if ( ! canUseDOM || ! this . node || ! this . portal ) return ;
133139
134140 const state = this . portal . state ;
135141 const now = Date . now ( ) ;
@@ -149,18 +155,34 @@ export default class Modal extends Component {
149155 }
150156
151157 removePortal = ( ) => {
152- ReactDOM . unmountComponentAtNode ( this . node ) ;
158+ ( ! isReact16 ) && ReactDOM . unmountComponentAtNode ( this . node ) ;
153159 const parent = getParentElement ( this . props . parentSelector ) ;
154160 parent . removeChild ( this . node ) ;
155161 }
156162
163+ portalRef = ref => { this . portal = ref ; }
164+
157165 renderPortal = props => {
158- this . portal = renderSubtreeIntoContainer ( this , (
166+ const portal = createPortal ( this , (
159167 < ModalPortal defaultStyles = { Modal . defaultStyles } { ...props } />
160168 ) , this . node ) ;
169+ this . portalRef ( portal ) ;
161170 }
162171
163172 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+ ) ;
165187 }
166188}
0 commit comments