import Button from "./Button"; import React, { Fragment, ReactNode, useState } from "react"; import Stack from "./Stack"; import Typography from "./Typography"; import classNames from "./lib/classNames"; import { Dialog, Transition } from "@headlessui/react"; import { XIcon } from "@heroicons/react/outline"; const { Overlay, Title, Description } = Dialog; export interface CustomModalProps { children?: ReactNode | (({ onClose }: { onClose?: () => void }) => ReactNode); overlayClassName?: string; contentClassName?: string; showCloseButton?: boolean; onClose?: () => void; description?: string; heading?: string; } export interface ModalProps extends CustomModalProps {} export function getModalOverlayStyles() { const modalOverlayStyles = { base: "bg-black bg-opacity-40 fixed inset-0", }; return classNames(modalOverlayStyles.base); } export function getModalContentStyles() { const modalContentStyles = { base: "bg-white w-full max-w-md rounded-lg fixed divide-y left-1/2 top-1/2 transform -translate-x-1/2 -translate-y-1/2 focus:outline-none max-h-[90vh] flex flex-col", }; return classNames(modalContentStyles.base); } export default function Modal({ showCloseButton = true, overlayClassName, contentClassName, description, children, onClose, heading, }: ModalProps) { const [open, setOpen] = useState(true); function handleClose() { if (!onClose) return; setOpen(false); setTimeout(() => { onClose(); }, 200); } return (
{heading && ( {heading && ( <Typography.Heading level={3}>{heading}</Typography.Heading> )} {showCloseButton && ( )} )} {description && ( {description} )} {children && (typeof children === "function" ? children({ onClose: handleClose }) : children)}
); }