Add basic modal support
This commit is contained in:
parent
bb3486f559
commit
61dc86421d
4 changed files with 85 additions and 7 deletions
61
resources/scripts/components/elements/Modal.tsx
Normal file
61
resources/scripts/components/elements/Modal.tsx
Normal 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>
|
||||||
|
);
|
||||||
|
};
|
|
@ -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>
|
||||||
|
);
|
||||||
|
};
|
|
@ -8,6 +8,7 @@ import FlashMessageRender from '@/components/FlashMessageRender';
|
||||||
import DatabaseRow from '@/components/server/databases/DatabaseRow';
|
import DatabaseRow from '@/components/server/databases/DatabaseRow';
|
||||||
import Spinner from '@/components/elements/Spinner';
|
import Spinner from '@/components/elements/Spinner';
|
||||||
import { CSSTransition } from 'react-transition-group';
|
import { CSSTransition } from 'react-transition-group';
|
||||||
|
import CreateDatabaseButton from '@/components/server/databases/CreateDatabaseButton';
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
const [ loading, setLoading ] = useState(true);
|
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.
|
It looks like you have no databases. Click the button below to create one now.
|
||||||
</p>
|
</p>
|
||||||
}
|
}
|
||||||
<div className={'mt-6 text-right'}>
|
<div className={'mt-6 flex justify-end'}>
|
||||||
<button className={'btn btn-primary btn-lg'}>
|
<CreateDatabaseButton onCreated={database => setDatabases(s => [...s, database])}/>
|
||||||
Create Database
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
</React.Fragment>
|
</React.Fragment>
|
||||||
</CSSTransition>
|
</CSSTransition>
|
||||||
|
|
|
@ -7,13 +7,13 @@
|
||||||
@apply .relative .w-full .max-w-md .m-auto .flex-col .flex;
|
@apply .relative .w-full .max-w-md .m-auto .flex-col .flex;
|
||||||
|
|
||||||
&.top {
|
&.top {
|
||||||
margin-top: 15%;
|
margin-top: 10%;
|
||||||
}
|
}
|
||||||
|
|
||||||
& > .modal-close-icon {
|
& > .modal-close-icon {
|
||||||
@apply .absolute .pin-r .p-2 .text-white .cursor-pointer .opacity-50;
|
@apply .absolute .pin-r .p-2 .text-white .cursor-pointer .opacity-50;
|
||||||
transition: opacity 150ms linear, transform 150ms ease-in;
|
transition: opacity 150ms linear, transform 150ms ease-in;
|
||||||
top: -2.5rem;
|
top: -2rem;
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
@apply .opacity-100;
|
@apply .opacity-100;
|
||||||
|
@ -22,7 +22,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
& > .modal-content {
|
& > .modal-content {
|
||||||
@apply .bg-white .rounded .shadow-md;
|
@apply .bg-neutral-900 .rounded .shadow-md;
|
||||||
transition: all 250ms ease;
|
transition: all 250ms ease;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue