Skip to content

Commit 0af2eb6

Browse files
authored
IBX-9462: React Popup fix (#1446)
1 parent b6c6fd3 commit 0af2eb6

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

src/bundle/ui-dev/src/modules/common/popup/popup.component.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const Popup = ({
4040
}) => {
4141
const rootDOMElement = getRootDOMElement();
4242
const modalRef = useRef(null);
43+
const latestBootstrapModal = useRef(null);
4344
const Translator = getTranslator();
4445
const bootstrap = getBootstrap();
4546

@@ -50,9 +51,21 @@ const Popup = ({
5051
if (isVisible) {
5152
showPopup();
5253
modalRef.current.addEventListener('hidden.bs.modal', onClose);
54+
} else {
55+
if (latestBootstrapModal.current) {
56+
latestBootstrapModal.current.hide();
57+
}
5358
}
5459
}, [isVisible]);
5560

61+
useEffect(() => {
62+
return () => {
63+
if (latestBootstrapModal.current) {
64+
latestBootstrapModal.current.hide();
65+
}
66+
};
67+
}, []);
68+
5669
if (!isVisible) {
5770
return null;
5871
}
@@ -65,26 +78,19 @@ const Popup = ({
6578
});
6679
const closeBtnLabel = Translator.trans(/*@Desc("Close")*/ 'popup.close.label', {}, 'ibexa_universal_discovery_widget');
6780
const hidePopup = () => {
68-
modalRef.current.removeEventListener('hidden.bs.modal', onClose);
69-
bootstrap.Modal.getOrCreateInstance(modalRef.current).hide();
81+
latestBootstrapModal.current.hide();
7082
rootDOMElement.classList.remove(CLASS_MODAL_OPEN, CLASS_NON_SCROLLABLE);
7183
};
7284
const showPopup = () => {
73-
const bootstrapModal = bootstrap.Modal.getOrCreateInstance(modalRef.current, {
74-
...MODAL_CONFIG,
75-
keyboard: !noKeyboard,
76-
focus: hasFocus,
77-
});
78-
const initializedBackdropRootElement = bootstrapModal._backdrop._config.rootElement;
85+
const initializedBackdropRootElement = latestBootstrapModal.current._backdrop._config.rootElement;
7986

8087
if (initializedBackdropRootElement !== rootDOMElement) {
81-
bootstrapModal._backdrop._config.rootElement = rootDOMElement;
88+
latestBootstrapModal.current._backdrop._config.rootElement = rootDOMElement;
8289
}
8390

84-
bootstrapModal.show();
91+
latestBootstrapModal.current.show();
8592
};
8693
const handleOnClick = (event, onClick, preventClose) => {
87-
modalRef.current.removeEventListener('hidden.bs.modal', onClose);
8894
if (!preventClose) {
8995
hidePopup();
9096
}
@@ -110,7 +116,21 @@ const Popup = ({
110116
};
111117

112118
return (
113-
<div ref={modalRef} className={modalClasses} tabIndex={hasFocus ? -1 : undefined}>
119+
<div
120+
ref={(ref) => {
121+
modalRef.current = ref;
122+
123+
if (ref) {
124+
latestBootstrapModal.current = bootstrap.Modal.getOrCreateInstance(modalRef.current, {
125+
...MODAL_CONFIG,
126+
keyboard: !noKeyboard,
127+
focus: hasFocus,
128+
});
129+
}
130+
}}
131+
className={modalClasses}
132+
tabIndex={hasFocus ? -1 : undefined}
133+
>
114134
<div className={`modal-dialog c-popup__dialog ${MODAL_SIZE_CLASS[size]}`} role="dialog">
115135
<div className="modal-content c-popup__content">
116136
{noHeader

0 commit comments

Comments
 (0)