'use client' import { useEffect, useRef } from 'react' import { Transition } from '@headlessui/react' type ModalProps = { children: React.ReactNode id: string ariaLabel: string show: boolean handleClose: () => void } export default function Modal({ children, id, ariaLabel, show, handleClose }: ModalProps) { const modalContent = useRef(null) // close the modal on click outside useEffect(() => { const clickHandler = ({ target }: { target: EventTarget | null }): void => { if (!show || modalContent.current?.contains(target as Node)) return handleClose() } document.addEventListener('click', clickHandler) return () => document.removeEventListener('click', clickHandler) }, [show, handleClose, modalContent]) // close the modal if the esc key is pressed useEffect(() => { const keyHandler = ({ keyCode }: { keyCode: number }) => { if (keyCode !== 27) return handleClose() } document.addEventListener('keydown', keyHandler) return () => document.removeEventListener('keydown', keyHandler) }, [handleClose]) return ( <> {/* Modal backdrop */}