2023-04-12 23:51:23 +08:00
|
|
|
import React, { useCallback, useEffect } from 'react';
|
2021-11-11 03:10:35 +08:00
|
|
|
import Styled from './styles';
|
2016-09-01 03:52:17 +08:00
|
|
|
|
2023-06-15 09:36:18 +08:00
|
|
|
const BaseModal = (props) => {
|
|
|
|
const {
|
|
|
|
setIsOpen,
|
|
|
|
modalName,
|
|
|
|
children,
|
|
|
|
isOpen,
|
|
|
|
onRequestClose,
|
|
|
|
className,
|
|
|
|
overlayClassName,
|
|
|
|
dataTest,
|
|
|
|
priority,
|
2023-04-12 23:51:23 +08:00
|
|
|
} = props;
|
|
|
|
|
2023-06-15 09:36:18 +08:00
|
|
|
const closeEventHandler = useCallback(() => {
|
|
|
|
setIsOpen(false);
|
|
|
|
}, []);
|
|
|
|
useEffect(() => {
|
2023-04-12 23:51:23 +08:00
|
|
|
// Only add event listener if name is specified
|
2023-07-20 00:22:50 +08:00
|
|
|
if (!modalName) return () => null;
|
2023-04-12 23:51:23 +08:00
|
|
|
|
|
|
|
const closeEventName = `CLOSE_MODAL_${modalName.toUpperCase()}`;
|
|
|
|
|
|
|
|
// Listen to close event on mount
|
|
|
|
document.addEventListener(closeEventName, closeEventHandler);
|
|
|
|
|
|
|
|
// Remove listener on unmount
|
|
|
|
return () => {
|
2023-07-20 00:22:50 +08:00
|
|
|
document.removeEventListener(closeEventName, closeEventHandler);
|
2023-04-12 23:51:23 +08:00
|
|
|
};
|
|
|
|
}, []);
|
2023-06-15 09:36:18 +08:00
|
|
|
const priorityValue = priority || 'low';
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Styled.BaseModal
|
|
|
|
portalClassName={`modal-${priorityValue}`}
|
|
|
|
parentSelector={() => document.querySelector('#modals-container')}
|
|
|
|
isOpen={isOpen}
|
|
|
|
onRequestClose={onRequestClose}
|
|
|
|
className={className}
|
|
|
|
overlayClassName={overlayClassName}
|
|
|
|
shouldReturnFocusAfterClose={false}
|
|
|
|
data={{
|
|
|
|
test: dataTest,
|
|
|
|
}}
|
|
|
|
{...props}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</Styled.BaseModal>
|
|
|
|
);
|
|
|
|
};
|
2023-04-12 23:51:23 +08:00
|
|
|
|
|
|
|
export default { BaseModal };
|