forked from StellarSend/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModal.tsx
More file actions
144 lines (133 loc) · 3.46 KB
/
Copy pathModal.tsx
File metadata and controls
144 lines (133 loc) · 3.46 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import React, { useEffect, useRef } from 'react'
import { createPortal } from 'react-dom'
import { X } from 'lucide-react'
import { cn } from '@/lib/utils'
import { Button } from './Button'
interface ModalProps {
isOpen: boolean
onClose?: () => void
title?: string
description?: string
children: React.ReactNode
size?: 'sm' | 'md' | 'lg' | 'xl'
showClose?: boolean
closeOnBackdrop?: boolean
className?: string
}
const sizeClasses = {
sm: 'max-w-sm',
md: 'max-w-md',
lg: 'max-w-lg',
xl: 'max-w-xl',
}
export function Modal({
isOpen,
onClose,
title,
description,
children,
size = 'md',
showClose = true,
closeOnBackdrop = true,
className,
}: ModalProps) {
const overlayRef = useRef<HTMLDivElement>(null)
// Lock scroll when open
useEffect(() => {
if (isOpen) {
document.body.style.overflow = 'hidden'
} else {
document.body.style.overflow = ''
}
return () => {
document.body.style.overflow = ''
}
}, [isOpen])
// Close on Escape
useEffect(() => {
if (!isOpen || !onClose) return
const handler = (e: KeyboardEvent) => {
if (e.key === 'Escape') onClose()
}
document.addEventListener('keydown', handler)
return () => document.removeEventListener('keydown', handler)
}, [isOpen, onClose])
if (!isOpen) return null
const handleBackdropClick = (e: React.MouseEvent<HTMLDivElement>) => {
if (closeOnBackdrop && onClose && e.target === overlayRef.current) {
onClose()
}
}
return createPortal(
<div
ref={overlayRef}
onClick={handleBackdropClick}
className={cn(
'fixed inset-0 z-50 flex items-center justify-center p-4',
'bg-black/70 backdrop-blur-sm',
'animate-fade-in',
)}
>
<div
className={cn(
'relative w-full bg-navy-800 border border-navy-600/50 rounded-2xl shadow-2xl',
'animate-slide-up',
sizeClasses[size],
className,
)}
role="dialog"
aria-modal="true"
aria-labelledby={title ? 'modal-title' : undefined}
>
{/* Header */}
{(title || showClose) && (
<div className="flex items-start justify-between p-6 pb-0">
<div>
{title && (
<h2 id="modal-title" className="text-lg font-semibold text-white">
{title}
</h2>
)}
{description && (
<p className="text-sm text-slate-400 mt-1">{description}</p>
)}
</div>
{showClose && onClose && (
<Button
variant="ghost"
size="xs"
onClick={onClose}
className="ml-4 shrink-0 -mt-1"
aria-label="Close"
>
<X size={16} />
</Button>
)}
</div>
)}
{/* Body */}
<div className="p-6">{children}</div>
</div>
</div>,
document.body,
)
}
// ─── Modal footer helper ──────────────────────────────────────────────────────
export function ModalFooter({
children,
className,
}: {
children: React.ReactNode
className?: string
}) {
return (
<div
className={cn(
'flex items-center justify-end gap-3 pt-4 border-t border-navy-700/50',
className,
)}
>
{children}
</div>
)
}