@@ -10,6 +10,10 @@ var Assign = require('lodash.assign');
10
10
var SafeHTMLElement = ExecutionEnvironment . canUseDOM ? window . HTMLElement : { } ;
11
11
var AppElement = ExecutionEnvironment . canUseDOM ? document . body : { appendChild : function ( ) { } } ;
12
12
13
+ function getParentElement ( parentSelector ) {
14
+ return parentSelector ( ) ;
15
+ }
16
+
13
17
var Modal = React . createClass ( {
14
18
15
19
displayName : 'Modal' ,
@@ -37,6 +41,7 @@ var Modal = React.createClass({
37
41
closeTimeoutMS : React . PropTypes . number ,
38
42
ariaHideApp : React . PropTypes . bool ,
39
43
shouldCloseOnOverlayClick : React . PropTypes . bool ,
44
+ parentSelector : React . PropTypes . func ,
40
45
role : React . PropTypes . string ,
41
46
contentLabel : React . PropTypes . string . isRequired
42
47
} ,
@@ -47,18 +52,29 @@ var Modal = React.createClass({
47
52
portalClassName : 'ReactModalPortal' ,
48
53
ariaHideApp : true ,
49
54
closeTimeoutMS : 0 ,
50
- shouldCloseOnOverlayClick : true
55
+ shouldCloseOnOverlayClick : true ,
56
+ parentSelector : ( ) => document . body
51
57
} ;
52
58
} ,
53
59
54
60
componentDidMount : function ( ) {
55
61
this . node = document . createElement ( 'div' ) ;
56
62
this . node . className = this . props . portalClassName ;
57
- document . body . appendChild ( this . node ) ;
63
+
64
+ var parent = getParentElement ( this . props . parentSelector ) ;
65
+ parent . appendChild ( this . node ) ;
58
66
this . renderPortal ( this . props ) ;
59
67
} ,
60
68
61
69
componentWillReceiveProps : function ( newProps ) {
70
+ var currentParent = getParentElement ( this . props . parentSelector ) ;
71
+ var newParent = getParentElement ( newProps . parentSelector ) ;
72
+
73
+ if ( newParent !== currentParent ) {
74
+ currentParent . removeChild ( this . node ) ;
75
+ newParent . appendChild ( this . node ) ;
76
+ }
77
+
62
78
this . renderPortal ( newProps ) ;
63
79
} ,
64
80
@@ -68,7 +84,8 @@ var Modal = React.createClass({
68
84
}
69
85
70
86
ReactDOM . unmountComponentAtNode ( this . node ) ;
71
- document . body . removeChild ( this . node ) ;
87
+ var parent = getParentElement ( this . props . parentSelector ) ;
88
+ parent . removeChild ( this . node ) ;
72
89
elementClass ( document . body ) . remove ( 'ReactModal__Body--open' ) ;
73
90
} ,
74
91
0 commit comments