@@ -9,6 +9,7 @@ export function Modal({ title, body, onClose, isOpen, footer, staticBackdrop, sc
9
9
10
10
function closeIfEscape ( event ) {
11
11
if ( keyboard && event . which === ESCAPE_KEYCODE ) {
12
+ hideModal ( modalRef ) ;
12
13
onClose ( ) ;
13
14
}
14
15
}
@@ -22,17 +23,10 @@ export function Modal({ title, body, onClose, isOpen, footer, staticBackdrop, sc
22
23
} , [ keyboard ] ) ;
23
24
24
25
useEffect ( ( ) => {
25
- const body = document . querySelector ( 'body' ) ;
26
-
27
26
if ( isOpen ) {
28
- body . classList . add ( 'modal-open' ) ;
29
- modalRef . current . style . display = 'block' ;
30
- modalRef . current . classList . add ( 'show' ) ;
31
- modalRef . current . focus ( ) ;
27
+ showModal ( modalRef ) ;
32
28
} else {
33
- body . classList . remove ( 'modal-open' ) ;
34
- modalRef . current . style . display = 'none' ;
35
- modalRef . current . classList . remove ( 'show' ) ;
29
+ hideModal ( modalRef ) ;
36
30
}
37
31
} , [ isOpen ] ) ;
38
32
@@ -45,6 +39,7 @@ export function Modal({ title, body, onClose, isOpen, footer, staticBackdrop, sc
45
39
onClick = { ( e ) => {
46
40
e . stopPropagation ( ) ;
47
41
if ( ! staticBackdrop ) {
42
+ hideModal ( modalRef ) ;
48
43
onClose ( ) ;
49
44
}
50
45
} }
@@ -92,6 +87,23 @@ Modal.propTypes = {
92
87
title : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . element ] ) ,
93
88
} ;
94
89
90
+ function hideModal ( modalRef ) {
91
+ const body = document . querySelector ( 'body' ) ;
92
+
93
+ body . classList . remove ( 'modal-open' ) ;
94
+ modalRef . current . style . display = 'none' ;
95
+ modalRef . current . classList . remove ( 'show' ) ;
96
+ }
97
+
98
+ function showModal ( modalRef ) {
99
+ const body = document . querySelector ( 'body' ) ;
100
+
101
+ body . classList . add ( 'modal-open' ) ;
102
+ modalRef . current . style . display = 'block' ;
103
+ modalRef . current . classList . add ( 'show' ) ;
104
+ modalRef . current . focus ( ) ;
105
+ }
106
+
95
107
function renderObjectOrFunction ( content , params ) {
96
108
if ( typeof content === 'function' ) {
97
109
return content ( params ) ;
0 commit comments