import {Dialog, Transition} from '@headlessui/react' import {XMarkIcon} from '@heroicons/react/24/outline' import clsx from 'clsx' import {Fragment, ReactNode, useEffect, useRef} from 'react' export const MODAL_CLASS = 'items-center gap-4 rounded-md bg-canvas-0 sm:px-8 px-4 pt-6 pb-2 text-ink-1000 h-[calc(100dvh-var(--hloss)-120px)] sm:h-[calc(95dvh-var(--hloss)-120px)] ' export const SCROLLABLE_MODAL_CLASS = '!overflow-auto' // From https://tailwindui.com/components/application-ui/overlays/modals export function Modal(props: { children: ReactNode open: boolean setOpen?: (open: boolean) => void size?: 'sm' | 'md' | 'lg' | 'xl' position?: 'center' | 'top' | 'bottom' className?: string onClose?: () => void }) { const {children, position = 'center', open, setOpen, size = 'md', className, onClose} = props const sizeClass = { sm: 'sm:max-w-sm', md: 'sm:max-w-md', lg: 'max-w-2xl', xl: 'max-w-5xl', }[size] const positionClass = { center: 'sm:items-center', top: 'sm:items-start', bottom: 'sm:items-end', }[position] const wasOpenRef = useRef(open) useEffect(() => { if (wasOpenRef.current && !open && onClose) { onClose() } wasOpenRef.current = open }, [open, onClose]) return ( {})} // prevent modal from re-opening from bubbled event if Modal is child of the open button onClick={(e: any) => e.stopPropagation()} > {/* background cover */}
focus trap
{children} {setOpen && ( )}
) }