Make the modal work again

This commit is contained in:
Dane Everitt 2020-07-04 10:15:06 -07:00
parent ebe588718c
commit d260200947
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 50 additions and 33 deletions

View File

@ -1,9 +1,10 @@
import React, { useEffect, useMemo, useState } from 'react'; import React, { useEffect, useMemo, useState } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faTimes } from '@fortawesome/free-solid-svg-icons/faTimes'; import { faTimes } from '@fortawesome/free-solid-svg-icons/faTimes';
import { CSSTransition } from 'react-transition-group';
import Spinner from '@/components/elements/Spinner'; import Spinner from '@/components/elements/Spinner';
import classNames from 'classnames'; import tw from 'twin.macro';
import styled from 'styled-components/macro';
import Fade from '@/components/elements/Fade';
export interface RequiredModalProps { export interface RequiredModalProps {
visible: boolean; visible: boolean;
@ -12,20 +13,38 @@ export interface RequiredModalProps {
top?: boolean; top?: boolean;
} }
type Props = RequiredModalProps & { interface Props extends RequiredModalProps {
dismissable?: boolean; dismissable?: boolean;
closeOnEscape?: boolean; closeOnEscape?: boolean;
closeOnBackground?: boolean; closeOnBackground?: boolean;
showSpinnerOverlay?: boolean; showSpinnerOverlay?: boolean;
children: React.ReactNode;
} }
export default ({ visible, appear, dismissable, showSpinnerOverlay, top = true, closeOnBackground = true, closeOnEscape = true, onDismissed, children }: Props) => { const ModalMask = styled.div`
const [render, setRender] = useState(visible); ${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`};
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);
const isDismissable = useMemo(() => { const isDismissable = useMemo(() => {
return (dismissable || true) && !(showSpinnerOverlay || false); return (dismissable || true) && !(showSpinnerOverlay || false);
}, [dismissable, showSpinnerOverlay]); }, [ dismissable, showSpinnerOverlay ]);
const handleEscapeEvent = (e: KeyboardEvent) => { const handleEscapeEvent = (e: KeyboardEvent) => {
if (isDismissable && closeOnEscape && e.key === 'Escape') { if (isDismissable && closeOnEscape && e.key === 'Escape') {
@ -33,52 +52,47 @@ export default ({ visible, appear, dismissable, showSpinnerOverlay, top = true,
} }
}; };
useEffect(() => { useEffect(() => setRender(visible), [ visible ]);
setRender(visible);
}, [visible]);
useEffect(() => { useEffect(() => {
window.addEventListener('keydown', handleEscapeEvent); window.addEventListener('keydown', handleEscapeEvent);
return () => window.removeEventListener('keydown', handleEscapeEvent); return () => window.removeEventListener('keydown', handleEscapeEvent);
}, [render]); }, [ render ]);
return ( return (
<CSSTransition <Fade timeout={250} appear={appear} in={render} unmountOnExit onExited={onDismissed}>
timeout={250} <ModalMask
classNames={'fade'} onClick={e => {
appear={appear} if (isDismissable && closeOnBackground) {
in={render} e.stopPropagation();
unmountOnExit={true} if (e.target === e.currentTarget) {
onExited={() => onDismissed()} setRender(false);
> }
<div className={'modal-mask'} onClick={e => {
if (isDismissable && closeOnBackground) {
e.stopPropagation();
if (e.target === e.currentTarget) {
setRender(false);
} }
} }}
}}> >
<div className={classNames('modal-container', { top })}> <ModalContainer alignTop={top}>
{isDismissable && {isDismissable &&
<div className={'modal-close-icon'} onClick={() => setRender(false)}> <div className={'close-icon'} onClick={() => setRender(false)}>
<FontAwesomeIcon icon={faTimes}/> <FontAwesomeIcon icon={faTimes}/>
</div> </div>
} }
{showSpinnerOverlay && {showSpinnerOverlay &&
<div <div
className={'absolute w-full h-full rounded flex items-center justify-center'} css={tw`absolute w-full h-full rounded flex items-center justify-center`}
style={{ background: 'hsla(211, 10%, 53%, 0.25)' }} style={{ background: 'hsla(211, 10%, 53%, 0.25)' }}
> >
<Spinner/> <Spinner/>
</div> </div>
} }
<div className={'modal-content p-6'}> <div css={tw`bg-neutral-800 p-6 rounded shadow-md overflow-y-scroll transition-all duration-250`}>
{children} {children}
</div> </div>
</div> </ModalContainer>
</div> </ModalMask>
</CSSTransition> </Fade>
); );
}; };
export default Modal;

View File

@ -116,6 +116,9 @@ module.exports = {
fontSize: { fontSize: {
'2xs': '0.625rem', '2xs': '0.625rem',
}, },
transitionDuration: {
250: '250ms',
},
borderColor: theme => ({ borderColor: theme => ({
default: theme('colors.neutral.400', 'cuurrentColor'), default: theme('colors.neutral.400', 'cuurrentColor'),
}), }),