Files
cronmaster/app/_components/ui/Modal.tsx
2025-08-18 09:25:53 +01:00

110 lines
2.7 KiB
TypeScript

'use client'
import { useEffect, useRef } from 'react';
import { X } from 'lucide-react';
import { cn } from '@/app/_utils/cn';
import { Button } from './Button';
interface ModalProps {
isOpen: boolean;
onClose: () => void;
title: string;
children: React.ReactNode;
size?: 'sm' | 'md' | 'lg' | 'xl';
showCloseButton?: boolean;
}
export function Modal({
isOpen,
onClose,
title,
children,
size = 'md',
showCloseButton = true
}: ModalProps) {
const modalRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const handleEscape = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
onClose();
}
};
if (isOpen) {
document.addEventListener('keydown', handleEscape);
document.body.style.overflow = 'hidden';
}
return () => {
document.removeEventListener('keydown', handleEscape);
document.body.style.overflow = 'unset';
};
}, [isOpen, onClose]);
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (modalRef.current && !modalRef.current.contains(event.target as Node)) {
onClose();
}
};
if (isOpen) {
document.addEventListener('mousedown', handleClickOutside);
}
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [isOpen, onClose]);
if (!isOpen) return null;
const sizeClasses = {
sm: 'max-w-md',
md: 'max-w-lg',
lg: 'max-w-2xl',
xl: 'max-w-4xl'
};
return (
<div className="fixed inset-0 z-50 flex items-end justify-center sm:items-center p-0 sm:p-4">
{/* Backdrop */}
<div className="absolute inset-0 bg-black/50 backdrop-blur-sm" />
{/* Modal - Bottom sheet on mobile, centered on desktop */}
<div
ref={modalRef}
className={cn(
'relative w-full bg-card border border-border shadow-lg overflow-y-auto',
// Mobile: bottom sheet style (no rounded corners, full width)
'max-h-[85vh]',
// Desktop: original perfect modal behavior
'sm:rounded-lg sm:max-h-[90vh] sm:w-full',
sizeClasses[size]
)}
>
{/* Header */}
<div className="flex items-center justify-between p-4 sm:p-6 border-b border-border sticky top-0 bg-card z-10">
<h2 className="text-lg font-semibold text-foreground">{title}</h2>
{showCloseButton && (
<Button
variant="ghost"
size="icon"
onClick={onClose}
className="h-8 w-8"
>
<X className="h-4 w-4" />
</Button>
)}
</div>
{/* Content */}
<div className="p-4 sm:p-6">
{children}
</div>
</div>
</div>
);
}