@@ -2,16 +2,18 @@ import React, { useState, useEffect } from 'react';
2
2
import ReactDOM from 'react-dom' ;
3
3
import PropTypes from 'prop-types' ;
4
4
5
- export function ModalPortal ( { children, isOpen } ) {
5
+ export function ModalPortal ( { children, title , isOpen } ) {
6
6
const [ container , setContainer ] = useState ( ) ;
7
7
8
8
useEffect ( ( ) => {
9
- const body = document . getElementsByTagName ( 'body' ) [ 0 ] ;
9
+ const modalPortalsElem = getModalPortalsElem ( ) ;
10
10
11
11
if ( ! container ) {
12
12
const containerElem = document . createElement ( 'div' ) ;
13
13
14
- body . appendChild ( containerElem ) ;
14
+ containerElem . dataset . title = title ;
15
+
16
+ modalPortalsElem . appendChild ( containerElem ) ;
15
17
setContainer ( containerElem ) ;
16
18
}
17
19
@@ -20,9 +22,9 @@ export function ModalPortal({ children, isOpen }) {
20
22
return ;
21
23
}
22
24
23
- body . removeChild ( container ) ;
25
+ modalPortalsElem . removeChild ( container ) ;
24
26
} ;
25
- } , [ container ] ) ;
27
+ } , [ container , title ] ) ;
26
28
27
29
//FIXME: prop to define if modal will be always included into DOM
28
30
if ( ! container || ! isOpen ) {
@@ -41,3 +43,17 @@ export function ModalPortal({ children, isOpen }) {
41
43
ModalPortal . propTypes = {
42
44
children : PropTypes . oneOfType ( [ PropTypes . node , PropTypes . arrayOf ( PropTypes . node ) ] ) ,
43
45
} ;
46
+
47
+ function getModalPortalsElem ( ) {
48
+ let modalPortalsElem = document . querySelector ( '#modal-portals' ) ;
49
+
50
+ if ( ! modalPortalsElem ) {
51
+ const body = document . getElementsByTagName ( 'body' ) [ 0 ] ;
52
+
53
+ modalPortalsElem = document . createElement ( 'div' ) ;
54
+ modalPortalsElem . id = 'modal-portals' ;
55
+ body . appendChild ( modalPortalsElem ) ;
56
+ }
57
+
58
+ return modalPortalsElem ;
59
+ }
0 commit comments