Skip to content

Commit 75b3fd7

Browse files
committed
fix(modal): always remove .modal-open on body classList after onClose
event
1 parent 5d35c6c commit 75b3fd7

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

demo/DialogExamples.jsx

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function DialogExamples() {
1717
sunt, a eveniet nobis est ex magni nesciunt magnam. Eaque eius hic eligendi dolorum ut quas?
1818
</div>
1919
}
20+
keyboard={false}
2021
>
2122
<a href="" className="btn btn-primary">
2223
Simple Dialog

src/dialog/Modal.jsx

+21-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export function Modal({ title, body, onClose, isOpen, footer, staticBackdrop, sc
99

1010
function closeIfEscape(event) {
1111
if (keyboard && event.which === ESCAPE_KEYCODE) {
12+
hideModal(modalRef);
1213
onClose();
1314
}
1415
}
@@ -22,17 +23,10 @@ export function Modal({ title, body, onClose, isOpen, footer, staticBackdrop, sc
2223
}, [keyboard]);
2324

2425
useEffect(() => {
25-
const body = document.querySelector('body');
26-
2726
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);
3228
} else {
33-
body.classList.remove('modal-open');
34-
modalRef.current.style.display = 'none';
35-
modalRef.current.classList.remove('show');
29+
hideModal(modalRef);
3630
}
3731
}, [isOpen]);
3832

@@ -45,6 +39,7 @@ export function Modal({ title, body, onClose, isOpen, footer, staticBackdrop, sc
4539
onClick={(e) => {
4640
e.stopPropagation();
4741
if (!staticBackdrop) {
42+
hideModal(modalRef);
4843
onClose();
4944
}
5045
}}
@@ -92,6 +87,23 @@ Modal.propTypes = {
9287
title: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
9388
};
9489

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+
95107
function renderObjectOrFunction(content, params) {
96108
if (typeof content === 'function') {
97109
return content(params);

0 commit comments

Comments
 (0)