Spaces:
Sleeping
Sleeping
File size: 2,246 Bytes
712f4ea a45d518 712f4ea a45d518 712f4ea a45d518 712f4ea a45d518 712f4ea a45d518 712f4ea a45d518 712f4ea | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | import React, { useEffect, useCallback } from 'react';
import { X } from 'lucide-react';
import { clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
function cn(...inputs: Parameters<typeof clsx>) {
return twMerge(clsx(inputs));
}
interface ModalProps {
isOpen: boolean;
onClose: () => void;
title: string;
children: React.ReactNode;
size?: 'sm' | 'md' | 'lg' | 'xl';
}
const sizeStyles = {
sm: 'max-w-sm',
md: 'max-w-md',
lg: 'max-w-2xl',
xl: 'max-w-4xl',
};
export function Modal({ isOpen, onClose, title, children, size = 'md' }: ModalProps) {
const handleKeyDown = useCallback(
(e: KeyboardEvent) => {
if (e.key === 'Escape') onClose();
},
[onClose]
);
useEffect(() => {
if (isOpen) {
document.addEventListener('keydown', handleKeyDown);
document.body.style.overflow = 'hidden';
}
return () => {
document.removeEventListener('keydown', handleKeyDown);
document.body.style.overflow = '';
};
}, [isOpen, handleKeyDown]);
if (!isOpen) return null;
return (
<div className="fixed inset-0 z-50 flex items-center justify-center p-3 sm:p-4">
{/* Backdrop */}
<div
className="absolute inset-0 bg-black/80 backdrop-blur-sm"
onClick={onClose}
/>
{/* Panel */}
<div
className={cn(
'relative w-full max-h-[92vh] flex flex-col card shadow-2xl animate-fade-in my-auto',
sizeStyles[size]
)}
onClick={(e) => e.stopPropagation()}
>
{/* Header */}
<div className="shrink-0 flex items-center justify-between px-4 sm:px-6 py-3.5 sm:py-4 border-b border-border">
<h2 className="text-sm sm:text-base font-semibold text-text-primary truncate">{title}</h2>
<button
onClick={onClose}
className="p-1.5 rounded-lg text-text-secondary hover:text-text-primary hover:bg-bg-elevated transition-colors ml-2 shrink-0"
>
<X size={16} />
</button>
</div>
{/* Body (Scrollable for mobile keyboards and small screens) */}
<div className="flex-1 overflow-y-auto px-4 sm:px-6 py-4 sm:py-5 custom-scrollbar">{children}</div>
</div>
</div>
);
}
|