1
1
import React , { Component } from 'react' ;
2
- import ReactDOM from 'react-dom' ;
2
+ import ReactDOM , { findDOMNode } from 'react-dom' ;
3
3
import PropTypes from 'prop-types' ;
4
4
import ExecutionEnvironment from 'exenv' ;
5
5
import elementClass from 'element-class' ;
@@ -13,10 +13,6 @@ const renderSubtreeIntoContainer = ReactDOM.unstable_renderSubtreeIntoContainer;
13
13
const SafeHTMLElement = EE . canUseDOM ? window . HTMLElement : { } ;
14
14
const AppElement = EE . canUseDOM ? document . body : { appendChild ( ) { } } ;
15
15
16
- function getParentElement ( parentSelector ) {
17
- return parentSelector ( ) ;
18
- }
19
-
20
16
export default class Modal extends Component {
21
17
static setAppElement ( element ) {
22
18
ariaAppHider . setElement ( element || AppElement ) ;
@@ -55,7 +51,6 @@ export default class Modal extends Component {
55
51
closeTimeoutMS : PropTypes . number ,
56
52
ariaHideApp : PropTypes . bool ,
57
53
shouldCloseOnOverlayClick : PropTypes . bool ,
58
- parentSelector : PropTypes . func ,
59
54
role : PropTypes . string ,
60
55
contentLabel : PropTypes . string . isRequired
61
56
} ;
@@ -67,8 +62,7 @@ export default class Modal extends Component {
67
62
bodyOpenClassName : 'ReactModal__Body--open' ,
68
63
ariaHideApp : true ,
69
64
closeTimeoutMS : 0 ,
70
- shouldCloseOnOverlayClick : true ,
71
- parentSelector ( ) { return document . body ; }
65
+ shouldCloseOnOverlayClick : true
72
66
} ;
73
67
74
68
static defaultStyles = {
@@ -97,13 +91,8 @@ export default class Modal extends Component {
97
91
} ;
98
92
99
93
componentDidMount ( ) {
100
- this . node = document . createElement ( 'div' ) ;
101
- this . node . className = this . props . portalClassName ;
102
-
103
94
if ( this . props . isOpen ) refCount . add ( this ) ;
104
95
105
- const parent = getParentElement ( this . props . parentSelector ) ;
106
- parent . appendChild ( this . node ) ;
107
96
this . renderPortal ( this . props ) ;
108
97
}
109
98
@@ -114,23 +103,10 @@ export default class Modal extends Component {
114
103
115
104
if ( isOpen ) refCount . add ( this ) ;
116
105
if ( ! isOpen ) refCount . remove ( this ) ;
117
- const currentParent = getParentElement ( this . props . parentSelector ) ;
118
- const newParent = getParentElement ( newProps . parentSelector ) ;
119
-
120
- if ( newParent !== currentParent ) {
121
- currentParent . removeChild ( this . node ) ;
122
- newParent . appendChild ( this . node ) ;
123
- }
124
106
125
107
this . renderPortal ( newProps ) ;
126
108
}
127
109
128
- componentWillUpdate ( newProps ) {
129
- if ( newProps . portalClassName !== this . props . portalClassName ) {
130
- this . node . className = newProps . portalClassName ;
131
- }
132
- }
133
-
134
110
componentWillUnmount ( ) {
135
111
if ( ! this . node ) return ;
136
112
@@ -159,8 +135,7 @@ export default class Modal extends Component {
159
135
160
136
removePortal = ( ) => {
161
137
ReactDOM . unmountComponentAtNode ( this . node ) ;
162
- const parent = getParentElement ( this . props . parentSelector ) ;
163
- parent . removeChild ( this . node ) ;
138
+ this . portal = null ;
164
139
165
140
if ( refCount . count ( ) === 0 ) {
166
141
elementClass ( document . body ) . remove ( this . props . bodyOpenClassName ) ;
@@ -183,7 +158,14 @@ export default class Modal extends Component {
183
158
) , this . node ) ;
184
159
}
185
160
161
+ setNodeRef = ref => {
162
+ this . node = ref ;
163
+ }
164
+
186
165
render ( ) {
187
- return null ;
166
+ return (
167
+ < div ref = { this . setNodeRef }
168
+ className = { this . props . portalClassName } > </ div >
169
+ ) ;
188
170
}
189
171
}
0 commit comments