forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModal.tsx
More file actions
68 lines (60 loc) · 1.92 KB
/
Copy pathModal.tsx
File metadata and controls
68 lines (60 loc) · 1.92 KB
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
'use client';
import { ReactNode, useEffect } from 'react';
interface ModalProps {
open: boolean;
onClose: () => void;
title?: string;
children: ReactNode;
size?: 'sm' | 'md' | 'lg';
footer?: ReactNode;
}
const sizeClasses = {
sm: 'max-w-sm',
md: 'max-w-md',
lg: 'max-w-lg',
};
export default function Modal({ open, onClose, title, children, size = 'md', footer }: ModalProps) {
useEffect(() => {
if (!open) return;
function onKey(e: KeyboardEvent) {
if (e.key === 'Escape') onClose();
}
document.addEventListener('keydown', onKey);
return () => document.removeEventListener('keydown', onKey);
}, [open, onClose]);
if (!open) return null;
return (
<div
className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4"
onClick={(e) => { if (e.target === e.currentTarget) onClose(); }}
role="dialog"
aria-modal="true"
aria-labelledby={title ? 'modal-title' : undefined}
>
<div className={`relative w-full ${sizeClasses[size]} rounded-xl bg-white shadow-xl dark:bg-gray-900`}>
{title && (
<div className="flex items-center justify-between border-b border-gray-200 px-6 py-4 dark:border-gray-700">
<h2 id="modal-title" className="text-base font-semibold text-gray-900 dark:text-gray-100">
{title}
</h2>
<button
onClick={onClose}
className="text-gray-400 hover:text-gray-600 dark:hover:text-gray-200 transition-colors"
aria-label="Close"
>
✕
</button>
</div>
)}
<div className="px-6 py-4 text-sm text-gray-700 dark:text-gray-300">
{children}
</div>
{footer && (
<div className="flex justify-end gap-2 border-t border-gray-200 px-6 py-3 dark:border-gray-700">
{footer}
</div>
)}
</div>
</div>
);
}