2022-11-25 20:25:03 +00:00
|
|
|
import { useRef, useState } from 'react';
|
|
|
|
import * as React from 'react';
|
2022-06-12 19:07:52 +00:00
|
|
|
import { Dialog as HDialog } from '@headlessui/react';
|
2022-06-05 18:56:42 +00:00
|
|
|
import { Button } from '@/components/elements/button/index';
|
|
|
|
import { XIcon } from '@heroicons/react/solid';
|
2022-07-03 17:29:23 +00:00
|
|
|
import { AnimatePresence, motion } from 'framer-motion';
|
|
|
|
import { DialogContext, IconPosition, RenderDialogProps, styles } from './';
|
2022-06-05 18:56:42 +00:00
|
|
|
|
2022-07-02 22:27:22 +00:00
|
|
|
const variants = {
|
2022-07-03 17:29:23 +00:00
|
|
|
open: {
|
|
|
|
scale: 1,
|
|
|
|
opacity: 1,
|
|
|
|
transition: {
|
|
|
|
type: 'spring',
|
|
|
|
damping: 15,
|
|
|
|
stiffness: 300,
|
|
|
|
duration: 0.15,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
closed: {
|
|
|
|
scale: 0.75,
|
|
|
|
opacity: 0,
|
|
|
|
transition: {
|
|
|
|
type: 'easeIn',
|
|
|
|
duration: 0.15,
|
|
|
|
},
|
|
|
|
},
|
2022-07-02 22:27:22 +00:00
|
|
|
bounce: {
|
|
|
|
scale: 0.95,
|
2022-07-03 17:29:23 +00:00
|
|
|
opacity: 1,
|
2022-07-02 22:27:22 +00:00
|
|
|
transition: { type: 'linear', duration: 0.075 },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-07-03 17:29:23 +00:00
|
|
|
export default ({
|
2022-07-02 22:27:22 +00:00
|
|
|
open,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
onClose,
|
|
|
|
hideCloseIcon,
|
|
|
|
preventExternalClose,
|
|
|
|
children,
|
2022-07-03 17:29:23 +00:00
|
|
|
}: RenderDialogProps) => {
|
|
|
|
const container = useRef<HTMLDivElement>(null);
|
2022-07-02 22:27:22 +00:00
|
|
|
const [icon, setIcon] = useState<React.ReactNode>();
|
|
|
|
const [footer, setFooter] = useState<React.ReactNode>();
|
|
|
|
const [iconPosition, setIconPosition] = useState<IconPosition>('title');
|
2022-07-03 17:29:23 +00:00
|
|
|
const [down, setDown] = useState(false);
|
|
|
|
|
|
|
|
const onContainerClick = (down: boolean, e: React.MouseEvent<HTMLDivElement>): void => {
|
|
|
|
if (e.target instanceof HTMLElement && container.current?.isSameNode(e.target)) {
|
|
|
|
setDown(down);
|
|
|
|
}
|
|
|
|
};
|
2022-07-02 22:27:22 +00:00
|
|
|
|
|
|
|
const onDialogClose = (): void => {
|
|
|
|
if (!preventExternalClose) {
|
|
|
|
return onClose();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-05 18:56:42 +00:00
|
|
|
return (
|
2022-06-12 19:07:52 +00:00
|
|
|
<AnimatePresence>
|
|
|
|
{open && (
|
2022-07-02 22:27:22 +00:00
|
|
|
<DialogContext.Provider value={{ setIcon, setFooter, setIconPosition }}>
|
2022-07-02 21:24:12 +00:00
|
|
|
<HDialog
|
|
|
|
static
|
|
|
|
as={motion.div}
|
|
|
|
initial={{ opacity: 0 }}
|
|
|
|
animate={{ opacity: 1 }}
|
|
|
|
exit={{ opacity: 0 }}
|
|
|
|
transition={{ duration: 0.15 }}
|
|
|
|
open={open}
|
2022-07-02 22:27:22 +00:00
|
|
|
onClose={onDialogClose}
|
2022-07-02 21:24:12 +00:00
|
|
|
>
|
|
|
|
<div className={'fixed inset-0 bg-gray-900/50 z-40'} />
|
|
|
|
<div className={'fixed inset-0 overflow-y-auto z-50'}>
|
2022-07-03 17:29:23 +00:00
|
|
|
<div
|
|
|
|
ref={container}
|
|
|
|
className={styles.container}
|
|
|
|
onMouseDown={onContainerClick.bind(this, true)}
|
|
|
|
onMouseUp={onContainerClick.bind(this, false)}
|
|
|
|
>
|
2022-07-02 21:24:12 +00:00
|
|
|
<HDialog.Panel
|
|
|
|
as={motion.div}
|
2022-07-03 17:29:23 +00:00
|
|
|
initial={'closed'}
|
|
|
|
animate={down ? 'bounce' : 'open'}
|
|
|
|
exit={'closed'}
|
2022-07-02 22:27:22 +00:00
|
|
|
variants={variants}
|
2022-07-02 21:24:12 +00:00
|
|
|
className={styles.panel}
|
|
|
|
>
|
2022-07-10 18:52:57 +00:00
|
|
|
<div className={'flex p-6 pb-0 overflow-y-auto'}>
|
2022-07-02 22:27:22 +00:00
|
|
|
{iconPosition === 'container' && icon}
|
2022-11-21 20:58:55 +00:00
|
|
|
<div className={'flex-1 max-h-[70vh] min-w-0'}>
|
2022-07-02 21:24:12 +00:00
|
|
|
<div className={'flex items-center'}>
|
2022-07-02 22:27:22 +00:00
|
|
|
{iconPosition !== 'container' && icon}
|
2022-07-02 21:24:12 +00:00
|
|
|
<div>
|
|
|
|
{title && (
|
|
|
|
<HDialog.Title className={styles.title}>{title}</HDialog.Title>
|
|
|
|
)}
|
|
|
|
{description && (
|
|
|
|
<HDialog.Description>{description}</HDialog.Description>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{children}
|
2022-07-10 18:52:57 +00:00
|
|
|
<div className={'invisible h-6'} />
|
2022-07-02 21:24:12 +00:00
|
|
|
</div>
|
2022-06-12 19:07:52 +00:00
|
|
|
</div>
|
2022-07-02 22:27:22 +00:00
|
|
|
{footer}
|
2022-07-02 21:24:12 +00:00
|
|
|
{/* Keep this below the other buttons so that it isn't the default focus if they're present. */}
|
|
|
|
{!hideCloseIcon && (
|
|
|
|
<div className={'absolute right-0 top-0 m-4'}>
|
|
|
|
<Button.Text
|
|
|
|
size={Button.Sizes.Small}
|
|
|
|
shape={Button.Shapes.IconSquare}
|
|
|
|
onClick={onClose}
|
|
|
|
className={'group'}
|
|
|
|
>
|
|
|
|
<XIcon className={styles.close_icon} />
|
|
|
|
</Button.Text>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</HDialog.Panel>
|
|
|
|
</div>
|
2022-06-05 18:56:42 +00:00
|
|
|
</div>
|
2022-07-02 21:24:12 +00:00
|
|
|
</HDialog>
|
|
|
|
</DialogContext.Provider>
|
2022-06-12 19:07:52 +00:00
|
|
|
)}
|
|
|
|
</AnimatePresence>
|
2022-06-05 18:56:42 +00:00
|
|
|
);
|
|
|
|
};
|