Skip to content

Commit bff23cf

Browse files
committed
fix(modals): create only one modal backdrop
1 parent 2d8d870 commit bff23cf

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

src/dialog/Dialog.jsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React from 'react';
1+
import React, { useState, useEffect, useMemo } from 'react';
2+
import ReactDOM from 'react-dom';
23
import PropTypes from 'prop-types';
34
import { ModalPortal } from './ModalPortal';
45
import { Modal } from './Modal';

src/dialog/Modal.jsx

+40-1
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,21 @@ Modal.propTypes = {
9696
function hideModal(modalRef) {
9797
const body = document.querySelector('body');
9898

99-
body.classList.remove('modal-open');
10099
modalRef.current.style.display = 'none';
101100
modalRef.current.classList.remove('show');
101+
102+
if (getModals().length === 0) {
103+
hideModalBackdrop();
104+
body.classList.remove('modal-open');
105+
}
102106
}
103107

104108
function showModal(modalRef) {
105109
const body = document.querySelector('body');
106110

107111
body.classList.add('modal-open');
112+
showModalBackdrop();
113+
108114
modalRef.current.style.display = 'block';
109115
modalRef.current.classList.add('show');
110116
modalRef.current.focus();
@@ -117,3 +123,36 @@ function renderObjectOrFunction(content, params) {
117123

118124
return content;
119125
}
126+
127+
function showModalBackdrop() {
128+
const backdrop = getModalBackdrop();
129+
130+
backdrop.classList.remove('d-none');
131+
}
132+
133+
function hideModalBackdrop() {
134+
const backdrop = getModalBackdrop();
135+
136+
backdrop.classList.add('d-none');
137+
}
138+
139+
function getModalBackdrop() {
140+
const body = document.querySelector('body');
141+
let backdrop = document.querySelector('.modal-backdrop');
142+
143+
if (!backdrop) {
144+
backdrop = document.createElement('div');
145+
146+
backdrop.classList.add('modal-backdrop');
147+
backdrop.classList.add('fade');
148+
backdrop.classList.add('show');
149+
backdrop.classList.add('d-none');
150+
body.appendChild(backdrop);
151+
}
152+
153+
return backdrop;
154+
}
155+
156+
function getModals() {
157+
return document.querySelectorAll('#modal-portals .modal.show');
158+
}

src/dialog/ModalPortal.jsx

+2-8
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@ export function ModalPortal({ children, title, isOpen }) {
3131
return '';
3232
}
3333

34-
return ReactDOM.createPortal(
35-
<React.Fragment>
36-
{children}
37-
{isOpen && <div className="modal-backdrop fade show"></div>}
38-
</React.Fragment>,
39-
container
40-
);
34+
return ReactDOM.createPortal(children, container);
4135
}
4236

4337
ModalPortal.propTypes = {
@@ -48,7 +42,7 @@ function getModalPortalsElem() {
4842
let modalPortalsElem = document.querySelector('#modal-portals');
4943

5044
if (!modalPortalsElem) {
51-
const body = document.getElementsByTagName('body')[0];
45+
const body = document.querySelector('body');
5246

5347
modalPortalsElem = document.createElement('div');
5448
modalPortalsElem.id = 'modal-portals';

0 commit comments

Comments
 (0)