2020-03-27 22:32:33 +00:00
|
|
|
import React, { useEffect, useMemo, useState } from 'react';
|
2019-07-10 05:41:09 +00:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faTimes } from '@fortawesome/free-solid-svg-icons/faTimes';
|
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';
|
|
|
|
import styled from 'styled-components/macro';
|
|
|
|
import Fade from '@/components/elements/Fade';
|
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-07-04 17:15:06 +00:00
|
|
|
interface Props 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-07-04 17:15:06 +00:00
|
|
|
const ModalMask = styled.div`
|
|
|
|
${tw`fixed z-50 overflow-auto flex w-full inset-0`};
|
|
|
|
background: rgba(0, 0, 0, 0.70);
|
|
|
|
`;
|
|
|
|
|
|
|
|
const ModalContainer = styled.div<{ alignTop?: boolean }>`
|
|
|
|
${tw`relative flex flex-col w-full m-auto`};
|
2020-07-04 23:26:07 +00:00
|
|
|
max-height: calc(100vh - 8rem);
|
2020-07-04 17:15:06 +00:00
|
|
|
max-width: 50%;
|
|
|
|
// @todo max-w-screen-lg perhaps?
|
|
|
|
${props => props.alignTop && 'margin-top: 10%'};
|
|
|
|
|
|
|
|
& > .close-icon {
|
|
|
|
${tw`absolute right-0 p-2 text-white cursor-pointer opacity-50 transition-all duration-150 ease-linear hover:opacity-100`};
|
|
|
|
top: -2rem;
|
|
|
|
|
|
|
|
&:hover {${tw`transform rotate-90`}}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Modal: React.FC<Props> = ({ 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);
|
2020-07-04 17:15:06 +00:00
|
|
|
}, [ dismissable, showSpinnerOverlay ]);
|
2019-07-10 05:41:09 +00:00
|
|
|
|
|
|
|
const handleEscapeEvent = (e: KeyboardEvent) => {
|
2020-03-27 22:32:33 +00:00
|
|
|
if (isDismissable && closeOnEscape && e.key === 'Escape') {
|
2019-07-10 05:41:09 +00:00
|
|
|
setRender(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-04 17:15:06 +00:00
|
|
|
useEffect(() => setRender(visible), [ visible ]);
|
2019-07-10 05:41:09 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
window.addEventListener('keydown', handleEscapeEvent);
|
|
|
|
|
|
|
|
return () => window.removeEventListener('keydown', handleEscapeEvent);
|
2020-07-04 17:15:06 +00:00
|
|
|
}, [ render ]);
|
2019-07-10 05:41:09 +00:00
|
|
|
|
|
|
|
return (
|
2020-07-04 17:15:06 +00:00
|
|
|
<Fade timeout={250} appear={appear} in={render} unmountOnExit onExited={onDismissed}>
|
|
|
|
<ModalMask
|
|
|
|
onClick={e => {
|
|
|
|
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}>
|
2020-03-27 22:32:33 +00:00
|
|
|
{isDismissable &&
|
2020-07-04 17:15:06 +00:00
|
|
|
<div className={'close-icon'} onClick={() => setRender(false)}>
|
2019-07-10 05:41:09 +00:00
|
|
|
<FontAwesomeIcon icon={faTimes}/>
|
|
|
|
</div>
|
|
|
|
}
|
2020-03-27 22:32:33 +00:00
|
|
|
{showSpinnerOverlay &&
|
2019-07-17 04:43:11 +00:00
|
|
|
<div
|
2020-07-04 17:15:06 +00:00
|
|
|
css={tw`absolute w-full h-full rounded flex items-center justify-center`}
|
2019-07-17 04:43:11 +00:00
|
|
|
style={{ background: 'hsla(211, 10%, 53%, 0.25)' }}
|
|
|
|
>
|
2019-08-17 23:03:10 +00:00
|
|
|
<Spinner/>
|
2019-07-17 04:43:11 +00:00
|
|
|
</div>
|
|
|
|
}
|
2020-07-04 17:15:06 +00:00
|
|
|
<div css={tw`bg-neutral-800 p-6 rounded shadow-md overflow-y-scroll transition-all duration-250`}>
|
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
|
|
|
|
|
|
|
export default Modal;
|