-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathModal.tsx
More file actions
149 lines (132 loc) · 4.49 KB
/
Copy pathModal.tsx
File metadata and controls
149 lines (132 loc) · 4.49 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
145
146
147
148
149
import React, { useEffect, useRef } from 'react';
import { createPortal } from 'react-dom';
export interface ModalProps {
isOpen: boolean;
onClose: () => void;
title?: React.ReactNode;
children: React.ReactNode;
footer?: React.ReactNode;
size?: 'sm' | 'md' | 'lg' | 'xl' | 'full';
closeOnOverlayClick?: boolean;
className?: string;
ariaLabel?: string;
}
export const Modal: React.FC<ModalProps> = ({
isOpen,
onClose,
title,
children,
footer,
size = 'md',
closeOnOverlayClick = true,
className = '',
ariaLabel = 'Modal dialog',
}) => {
const modalRef = useRef<HTMLDivElement>(null);
const previousFocusRef = useRef<HTMLElement | null>(null);
useEffect(() => {
if (isOpen) {
previousFocusRef.current = document.activeElement as HTMLElement;
document.body.style.overflow = 'hidden';
if (modalRef.current) {
modalRef.current.focus();
}
} else {
document.body.style.overflow = '';
if (previousFocusRef.current) {
previousFocusRef.current.focus();
}
}
return () => {
document.body.style.overflow = '';
};
}, [isOpen]);
useEffect(() => {
const handleKeyDown = (e: globalThis.KeyboardEvent) => {
if (e.key === 'Escape' && isOpen) {
onClose();
}
};
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, [isOpen, onClose]);
const handleOverlayClick = (e: React.MouseEvent<HTMLDivElement>) => {
if (closeOnOverlayClick && e.target === e.currentTarget) {
onClose();
}
};
const handleTabKey = (e: React.KeyboardEvent) => {
if (e.key !== 'Tab') return;
const focusableElements = modalRef.current?.querySelectorAll(
'a[href], area[href], input:not([disabled]):not([type="hidden"]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, [tabindex]:not([tabindex="-1"]), [contenteditable]'
);
if (!focusableElements || focusableElements.length === 0) {
e.preventDefault();
return;
}
const firstElement = focusableElements[0] as HTMLElement;
const lastElement = focusableElements[focusableElements.length - 1] as HTMLElement;
if (e.shiftKey) {
if (document.activeElement === firstElement) {
e.preventDefault();
lastElement.focus();
}
} else {
if (document.activeElement === lastElement) {
e.preventDefault();
firstElement.focus();
}
}
};
if (!isOpen) return null;
const sizeClasses = {
sm: 'max-w-sm',
md: 'max-w-md',
lg: 'max-w-lg',
xl: 'max-w-xl',
full: 'max-w-full m-4',
};
const modalContent = (
<div
className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm transition-opacity"
onClick={handleOverlayClick}
role="presentation"
>
<div
ref={modalRef}
className={`bg-white dark:bg-gray-800 rounded-lg shadow-xl w-full mx-4 transform transition-all ${sizeClasses[size]} ${className}`}
role="dialog"
aria-modal="true"
aria-labelledby={title ? 'modal-title' : undefined}
aria-label={!title ? ariaLabel : undefined}
tabIndex={-1}
onKeyDown={handleTabKey}
>
<div className="flex justify-between items-center px-6 py-4 border-b border-gray-200 dark:border-gray-700">
{title ? (
<h2 className="text-xl font-semibold text-gray-800 dark:text-white" id="modal-title">{title}</h2>
) : <div />}
<button
onClick={onClose}
className="text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 focus:outline-none focus:ring-2 focus:ring-primary-500 rounded-full p-1 transition-colors"
aria-label="Close modal"
>
<svg className="w-6 h-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<div className="px-6 py-4 max-h-[70vh] overflow-y-auto">
{children}
</div>
{footer && (
<div className="px-6 py-4 border-t border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-800/50 rounded-b-lg flex justify-end space-x-2">
{footer}
</div>
)}
</div>
</div>
);
return typeof document !== 'undefined' ? createPortal(modalContent, document.body) : null;
};
export default Modal;