import { FC, useRef, useEffect, useCallback } from 'react' import s from './Modal.module.css' import FocusTrap from '@lib/focus-trap' import { Cross } from '@components/icons' import { disableBodyScroll, clearAllBodyScrollLocks } from 'body-scroll-lock' interface ModalProps { className?: string children?: any onClose: () => void onEnter?: () => void | null } const Modal: FC = ({ children, onClose }) => { const ref = useRef() as React.MutableRefObject const handleKey = useCallback( (e: KeyboardEvent) => { if (e.key === 'Escape') { return onClose() } }, [onClose] ) useEffect(() => { if (ref.current) { disableBodyScroll(ref.current) window.addEventListener('keydown', handleKey) } return () => { window.removeEventListener('keydown', handleKey) clearAllBodyScrollLocks() } }, [open, handleKey]) return (
{children}
) } export default Modal