Add basic modal support

This commit is contained in:
Dane Everitt 2019-07-09 22:41:09 -07:00
parent bb3486f559
commit 61dc86421d
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 85 additions and 7 deletions

View file

@ -0,0 +1,61 @@
import React, { useEffect, useState } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faTimes } from '@fortawesome/free-solid-svg-icons/faTimes';
import { CSSTransition } from 'react-transition-group';
interface Props {
visible: boolean;
onDismissed: () => void;
dismissable?: boolean;
closeOnEscape?: boolean;
closeOnBackground?: boolean;
children: React.ReactChild;
}
export default (props: Props) => {
const [render, setRender] = useState(props.visible);
const handleEscapeEvent = (e: KeyboardEvent) => {
if (props.dismissable !== false && props.closeOnEscape !== false && e.key === 'Escape') {
setRender(false);
}
};
useEffect(() => setRender(props.visible), [props.visible]);
useEffect(() => {
window.addEventListener('keydown', handleEscapeEvent);
return () => window.removeEventListener('keydown', handleEscapeEvent);
}, [render]);
return (
<CSSTransition
timeout={250}
classNames={'fade'}
in={render}
unmountOnExit={true}
onExited={() => props.onDismissed()}
>
<div className={'modal-mask'} onClick={e => {
if (props.dismissable !== false && props.closeOnBackground !== false) {
e.stopPropagation();
if (e.target === e.currentTarget) {
setRender(false);
}
}
}}>
<div className={'modal-container top'}>
{props.dismissable !== false &&
<div className={'modal-close-icon'} onClick={() => setRender(false)}>
<FontAwesomeIcon icon={faTimes}/>
</div>
}
<div className={'modal-content p-6'}>
{props.children}
</div>
</div>
</div>
</CSSTransition>
);
};

View file

@ -0,0 +1,18 @@
import React, { useState } from 'react';
import { ServerDatabase } from '@/api/server/getServerDatabases';
import Modal from '@/components/elements/Modal';
export default ({ onCreated }: { onCreated: (database: ServerDatabase) => void }) => {
const [ visible, setVisible ] = useState(false);
return (
<React.Fragment>
<Modal visible={visible} onDismissed={() => setVisible(false)}>
<p>Testing</p>
</Modal>
<button className={'btn btn-primary btn-lg'} onClick={() => setVisible(true)}>
Create Database
</button>
</React.Fragment>
);
};

View file

@ -8,6 +8,7 @@ import FlashMessageRender from '@/components/FlashMessageRender';
import DatabaseRow from '@/components/server/databases/DatabaseRow';
import Spinner from '@/components/elements/Spinner';
import { CSSTransition } from 'react-transition-group';
import CreateDatabaseButton from '@/components/server/databases/CreateDatabaseButton';
export default () => {
const [ loading, setLoading ] = useState(true);
@ -45,10 +46,8 @@ export default () => {
It looks like you have no databases. Click the button below to create one now.
</p>
}
<div className={'mt-6 text-right'}>
<button className={'btn btn-primary btn-lg'}>
Create Database
</button>
<div className={'mt-6 flex justify-end'}>
<CreateDatabaseButton onCreated={database => setDatabases(s => [...s, database])}/>
</div>
</React.Fragment>
</CSSTransition>

View file

@ -7,13 +7,13 @@
@apply .relative .w-full .max-w-md .m-auto .flex-col .flex;
&.top {
margin-top: 15%;
margin-top: 10%;
}
& > .modal-close-icon {
@apply .absolute .pin-r .p-2 .text-white .cursor-pointer .opacity-50;
transition: opacity 150ms linear, transform 150ms ease-in;
top: -2.5rem;
top: -2rem;
&:hover {
@apply .opacity-100;
@ -22,7 +22,7 @@
}
& > .modal-content {
@apply .bg-white .rounded .shadow-md;
@apply .bg-neutral-900 .rounded .shadow-md;
transition: all 250ms ease;
}