import React, { useEffect, useState } from 'react'; import { Dialog as HDialog } from '@headlessui/react'; import { Button } from '@/components/elements/button/index'; import { XIcon } from '@heroicons/react/solid'; import DialogIcon, { IconPosition } from '@/components/elements/dialog/DialogIcon'; import { AnimatePresence, motion, useAnimation } from 'framer-motion'; import ConfirmationDialog from '@/components/elements/dialog/ConfirmationDialog'; import DialogContext from './context'; import DialogFooter from '@/components/elements/dialog/DialogFooter'; import styles from './style.module.css'; export interface DialogProps { open: boolean; onClose: () => void; } export interface FullDialogProps extends DialogProps { hideCloseIcon?: boolean; preventExternalClose?: boolean; title?: string; description?: string | undefined; children?: React.ReactNode; } const spring = { type: 'spring', damping: 15, stiffness: 300, duration: 0.15 }; const variants = { open: { opacity: 1, scale: 1, transition: spring }, closed: { opacity: 0, scale: 0.85, transition: spring }, bounce: { scale: 0.95, transition: { type: 'linear', duration: 0.075 }, }, }; const Dialog = ({ open, title, description, onClose, hideCloseIcon, preventExternalClose, children, }: FullDialogProps) => { const controls = useAnimation(); const [icon, setIcon] = useState(); const [footer, setFooter] = useState(); const [iconPosition, setIconPosition] = useState('title'); const onDialogClose = (): void => { if (!preventExternalClose) { return onClose(); } controls .start('bounce') .then(() => controls.start('open')) .catch(console.error); }; useEffect(() => { controls.start(open ? 'open' : 'closed').catch(console.error); }, [open]); return ( {open && (
{iconPosition === 'container' && icon}
{iconPosition !== 'container' && icon}
{title && ( {title} )} {description && ( {description} )}
{children}
{footer} {/* Keep this below the other buttons so that it isn't the default focus if they're present. */} {!hideCloseIcon && (
)}
)} ); }; const _Dialog = Object.assign(Dialog, { Confirm: ConfirmationDialog, Footer: DialogFooter, Icon: DialogIcon, }); export default _Dialog;