'use client'; import React, { useEffect, useRef } from 'react'; import { Icon } from './Icon'; export interface ModalProps { open: boolean; onClose: () => void; children: React.ReactNode; className?: string; size?: 'sm' | 'md' | 'lg' | 'xl'; } export const Modal: React.FC = ({ open, onClose, children, className = '', size = 'md', }) => { const dialogRef = useRef(null); const previousActiveElement = useRef(null); useEffect(() => { if (!open) return; previousActiveElement.current = document.activeElement as HTMLElement; const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') { onClose(); } // Simple focus trap if (e.key === 'Tab' && dialogRef.current) { const focusable = dialogRef.current.querySelectorAll( 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])' ); if (focusable.length === 0) return; const first = focusable[0]; const last = focusable[focusable.length - 1]; if (e.shiftKey && document.activeElement === first) { e.preventDefault(); last.focus(); } else if (!e.shiftKey && document.activeElement === last) { e.preventDefault(); first.focus(); } } }; document.addEventListener('keydown', handleKeyDown); document.body.style.overflow = 'hidden'; // Focus the first focusable element or the dialog setTimeout(() => { dialogRef.current?.focus(); }, 50); return () => { document.removeEventListener('keydown', handleKeyDown); document.body.style.overflow = ''; previousActiveElement.current?.focus(); }; }, [open, onClose]); if (!open) return null; return (
{ if (e.target === e.currentTarget) onClose(); }} >
{children}
); }; export const ModalHeader: React.FC> = ({ children, className = '', ...props }) => (
{children}
); export const ModalTitle: React.FC> = ({ children, className = '', ...props }) => (

{children}

); export const ModalClose: React.FC<{ onClick: () => void; className?: string }> = ({ onClick, className = '', }) => ( ); export const ModalBody: React.FC> = ({ children, className = '', ...props }) => (
{children}
); export const ModalFooter: React.FC> = ({ children, className = '', ...props }) => (
{children}
);