Modal configuration and content.
Optionaltitle?: stringThe rendered Modal component.
export default function Modal({ isOpen, onClose, children, title = 'Modal' }: ModalProps) {
const modalRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const handleEscape = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
onClose();
}
};
if (isOpen) {
document.addEventListener('keydown', handleEscape);
document.body.style.overflow = 'hidden';
modalRef.current?.focus();
}
return () => {
document.removeEventListener('keydown', handleEscape);
document.body.style.overflow = 'unset';
};
}, [isOpen, onClose]);
if (!isOpen) return null;
const modalContent = (
<>
<div className="fixed inset-0 z-9999 bg-black/50 backdrop-blur-sm" onClick={onClose} aria-hidden="true" />
<div
ref={modalRef}
className="fixed top-0 right-0 z-10000 h-full w-2/3 translate-x-0 transform bg-white/95 shadow-2xl backdrop-blur-xl transition-transform duration-300 ease-in-out dark:bg-slate-900/95"
role="dialog"
aria-modal="true"
aria-labelledby={title}
tabIndex={-1}
>
{children}
</div>
</>
);
return createPortal(modalContent, document.body);
}
Modal component for consistent styling across the application.
Example