2021-11-09 13:46:07 +01:00
|
|
|
import { MouseEvent, ReactNode, useRef } from 'react';
|
2021-05-21 18:55:21 +02:00
|
|
|
|
2021-05-12 18:31:02 +02:00
|
|
|
import classes from './Modal.module.css';
|
|
|
|
|
2021-11-09 13:46:07 +01:00
|
|
|
interface Props {
|
2021-05-12 18:31:02 +02:00
|
|
|
isOpen: boolean;
|
2021-05-21 18:55:21 +02:00
|
|
|
setIsOpen: Function;
|
2021-11-09 13:46:07 +01:00
|
|
|
children: ReactNode;
|
2021-05-12 18:31:02 +02:00
|
|
|
}
|
|
|
|
|
2021-11-09 13:46:07 +01:00
|
|
|
export const Modal = (props: Props): JSX.Element => {
|
2021-05-21 18:55:21 +02:00
|
|
|
const modalRef = useRef(null);
|
2021-11-09 13:46:07 +01:00
|
|
|
const modalClasses = [
|
|
|
|
classes.Modal,
|
|
|
|
props.isOpen ? classes.ModalOpen : classes.ModalClose,
|
|
|
|
].join(' ');
|
2021-05-12 18:31:02 +02:00
|
|
|
|
2021-05-21 18:55:21 +02:00
|
|
|
const clickHandler = (e: MouseEvent) => {
|
|
|
|
if (e.target === modalRef.current) {
|
|
|
|
props.setIsOpen(false);
|
|
|
|
}
|
2021-11-09 13:46:07 +01:00
|
|
|
};
|
2021-05-21 18:55:21 +02:00
|
|
|
|
2021-05-12 18:31:02 +02:00
|
|
|
return (
|
2021-05-21 18:55:21 +02:00
|
|
|
<div className={modalClasses} onClick={clickHandler} ref={modalRef}>
|
2021-05-12 18:31:02 +02:00
|
|
|
{props.children}
|
|
|
|
</div>
|
2021-11-09 13:46:07 +01:00
|
|
|
);
|
|
|
|
};
|