'use client'; import { ReactNode, RefObject, useEffect, useRef, useState } from 'react'; import { motion } from 'framer-motion'; import { clsx } from 'clsx/lite'; import useClickInsideOutside from '@/utility/useClickInsideOutside'; import { useRouter } from 'next/navigation'; import AnimateItems from './AnimateItems'; import { PATH_ROOT } from '@/app/paths'; import usePrefersReducedMotion from '@/utility/usePrefersReducedMotion'; import useEscapeHandler from '@/utility/useEscapeHandler'; import { useTheme } from 'next-themes'; export default function Modal({ onClosePath, onClose, className, anchor = 'center', container = true, children, noPadding, fast, }: { onClosePath?: string onClose?: () => void className?: string anchor?: 'top' | 'center' container?: boolean children: ReactNode noPadding?: boolean fast?: boolean }) { const router = useRouter(); const prefersReducedMotion = usePrefersReducedMotion(); const contentRef = useRef(null); const [htmlElements, setHtmlElements] = useState[]>([]); useEffect(() => { if (contentRef.current) { setHtmlElements([contentRef]); } }, []); const { resolvedTheme } = useTheme(); useClickInsideOutside({ htmlElements, onClickOutside: () => { if (onClose) { onClose(); } else { router.push( onClosePath ?? PATH_ROOT, { scroll: false }, ); } }, }); useEscapeHandler({ onKeyDown: onClose, ignoreShouldRespondToKeyboardCommands: true, }); return ( {children} ]} /> ); };