2020-10-15 03:13:36 +00:00
|
|
|
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
2019-07-17 04:43:11 +00:00
|
|
|
import Spinner from '@/components/elements/Spinner';
|
2020-07-04 17:15:06 +00:00
|
|
|
import tw from 'twin.macro';
|
2020-09-13 17:33:12 +00:00
|
|
|
import styled, { css } from 'styled-components/macro';
|
2020-09-13 17:02:25 +00:00
|
|
|
import { breakpoint } from '@/theme';
|
2020-07-04 17:15:06 +00:00
|
|
|
import Fade from '@/components/elements/Fade';
|
2020-10-15 03:13:36 +00:00
|
|
|
import { createPortal } from 'react-dom';
|
2019-07-10 05:41:09 +00:00
|
|
|
|
2019-08-03 05:22:01 +00:00
|
|
|
export interface RequiredModalProps {
|
2019-07-10 05:41:09 +00:00
|
|
|
visible: boolean;
|
|
|
|
onDismissed: () => void;
|
2019-12-23 04:51:50 +00:00
|
|
|
appear?: boolean;
|
2020-04-03 23:39:55 +00:00
|
|
|
top?: boolean;
|
2019-08-03 05:22:01 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 04:35:11 +00:00
|
|
|
export interface ModalProps extends RequiredModalProps {
|
2019-07-10 05:41:09 +00:00
|
|
|
dismissable?: boolean;
|
|
|
|
closeOnEscape?: boolean;
|
|
|
|
closeOnBackground?: boolean;
|
2019-07-17 04:43:11 +00:00
|
|
|
showSpinnerOverlay?: boolean;
|
2019-07-10 05:41:09 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 05:10:16 +00:00
|
|
|
export const ModalMask = styled.div`
|
2020-07-04 17:15:06 +00:00
|
|
|
${tw`fixed z-50 overflow-auto flex w-full inset-0`};
|
2022-06-26 19:13:52 +00:00
|
|
|
background: rgba(0, 0, 0, 0.7);
|
2020-07-04 17:15:06 +00:00
|
|
|
`;
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const ModalContainer = styled.div<{ alignTop?: boolean }>`
|
2020-09-13 17:33:12 +00:00
|
|
|
max-width: 95%;
|
|
|
|
max-height: calc(100vh - 8rem);
|
|
|
|
${breakpoint('md')`max-width: 75%`};
|
|
|
|
${breakpoint('lg')`max-width: 50%`};
|
2020-09-13 17:02:25 +00:00
|
|
|
|
2020-07-04 17:15:06 +00:00
|
|
|
${tw`relative flex flex-col w-full m-auto`};
|
2022-06-26 19:13:52 +00:00
|
|
|
${(props) =>
|
|
|
|
props.alignTop &&
|
|
|
|
css`
|
|
|
|
margin-top: 20%;
|
|
|
|
${breakpoint('md')`margin-top: 10%`};
|
|
|
|
`};
|
2020-10-03 18:22:37 +00:00
|
|
|
|
2020-10-05 08:36:47 +00:00
|
|
|
margin-bottom: auto;
|
2022-06-26 19:13:52 +00:00
|
|
|
|
2020-07-04 17:15:06 +00:00
|
|
|
& > .close-icon {
|
|
|
|
${tw`absolute right-0 p-2 text-white cursor-pointer opacity-50 transition-all duration-150 ease-linear hover:opacity-100`};
|
2020-09-13 17:33:12 +00:00
|
|
|
top: -2.5rem;
|
2022-06-26 19:13:52 +00:00
|
|
|
|
|
|
|
&:hover {
|
|
|
|
${tw`transform rotate-90`}
|
|
|
|
}
|
|
|
|
|
2020-09-13 17:33:12 +00:00
|
|
|
& > svg {
|
|
|
|
${tw`w-6 h-6`};
|
|
|
|
}
|
2020-07-04 17:15:06 +00:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const Modal: React.FC<ModalProps> = ({
|
|
|
|
visible,
|
|
|
|
appear,
|
|
|
|
dismissable,
|
|
|
|
showSpinnerOverlay,
|
|
|
|
top = true,
|
|
|
|
closeOnBackground = true,
|
|
|
|
closeOnEscape = true,
|
|
|
|
onDismissed,
|
|
|
|
children,
|
|
|
|
}) => {
|
|
|
|
const [render, setRender] = useState(visible);
|
2020-03-27 22:32:33 +00:00
|
|
|
|
|
|
|
const isDismissable = useMemo(() => {
|
|
|
|
return (dismissable || true) && !(showSpinnerOverlay || false);
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [dismissable, showSpinnerOverlay]);
|
2019-07-10 05:41:09 +00:00
|
|
|
|
2020-09-10 03:55:23 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!isDismissable || !closeOnEscape) return;
|
2019-07-10 05:41:09 +00:00
|
|
|
|
2020-09-10 03:55:23 +00:00
|
|
|
const handler = (e: KeyboardEvent) => {
|
|
|
|
if (e.key === 'Escape') setRender(false);
|
|
|
|
};
|
2019-07-10 05:41:09 +00:00
|
|
|
|
2020-09-10 03:55:23 +00:00
|
|
|
window.addEventListener('keydown', handler);
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('keydown', handler);
|
|
|
|
};
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [isDismissable, closeOnEscape, render]);
|
2019-07-10 05:41:09 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
useEffect(() => setRender(visible), [visible]);
|
2019-07-10 05:41:09 +00:00
|
|
|
|
|
|
|
return (
|
2022-06-26 19:13:52 +00:00
|
|
|
<Fade in={render} timeout={150} appear={appear || true} unmountOnExit onExited={() => onDismissed()}>
|
2020-07-04 17:15:06 +00:00
|
|
|
<ModalMask
|
2022-06-26 19:13:52 +00:00
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
onContextMenu={(e) => e.stopPropagation()}
|
|
|
|
onMouseDown={(e) => {
|
2020-07-04 17:15:06 +00:00
|
|
|
if (isDismissable && closeOnBackground) {
|
|
|
|
e.stopPropagation();
|
|
|
|
if (e.target === e.currentTarget) {
|
|
|
|
setRender(false);
|
|
|
|
}
|
2019-07-10 05:41:09 +00:00
|
|
|
}
|
2020-07-04 17:15:06 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<ModalContainer alignTop={top}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{isDismissable && (
|
|
|
|
<div className={'close-icon'} onClick={() => setRender(false)}>
|
|
|
|
<svg
|
|
|
|
xmlns={'http://www.w3.org/2000/svg'}
|
|
|
|
fill={'none'}
|
|
|
|
viewBox={'0 0 24 24'}
|
|
|
|
stroke={'currentColor'}
|
|
|
|
>
|
|
|
|
<path
|
|
|
|
strokeLinecap={'round'}
|
|
|
|
strokeLinejoin={'round'}
|
|
|
|
strokeWidth={'2'}
|
|
|
|
d={'M6 18L18 6M6 6l12 12'}
|
|
|
|
/>
|
|
|
|
</svg>
|
2020-08-18 05:04:24 +00:00
|
|
|
</div>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
|
|
|
{showSpinnerOverlay && (
|
|
|
|
<Fade timeout={150} appear in>
|
|
|
|
<div
|
|
|
|
css={tw`absolute w-full h-full rounded flex items-center justify-center`}
|
|
|
|
style={{ background: 'hsla(211, 10%, 53%, 0.35)', zIndex: 9999 }}
|
|
|
|
>
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
</Fade>
|
|
|
|
)}
|
|
|
|
<div
|
|
|
|
css={tw`bg-neutral-800 p-3 sm:p-4 md:p-6 rounded shadow-md overflow-y-scroll transition-all duration-150`}
|
|
|
|
>
|
2020-03-27 22:32:33 +00:00
|
|
|
{children}
|
2019-07-10 05:41:09 +00:00
|
|
|
</div>
|
2020-07-04 17:15:06 +00:00
|
|
|
</ModalContainer>
|
|
|
|
</ModalMask>
|
|
|
|
</Fade>
|
2019-07-10 05:41:09 +00:00
|
|
|
);
|
|
|
|
};
|
2020-07-04 17:15:06 +00:00
|
|
|
|
2020-10-15 03:13:36 +00:00
|
|
|
const PortaledModal: React.FC<ModalProps> = ({ children, ...props }) => {
|
|
|
|
const element = useRef(document.getElementById('modal-portal'));
|
|
|
|
|
|
|
|
return createPortal(<Modal {...props}>{children}</Modal>, element.current!);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PortaledModal;
|