-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathabsolute-modal.tsx
More file actions
60 lines (58 loc) · 1.78 KB
/
Copy pathabsolute-modal.tsx
File metadata and controls
60 lines (58 loc) · 1.78 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
import React, { useRef } from "react";
export default function AbsoluteModal({
children,
selected,
setSelected,
maxWidth,
maxHeight,
height,
className,
}: {
children: React.ReactNode;
selected: any;
setSelected: (selected: any) => void;
maxWidth?: string;
maxHeight?: string;
height?: string;
className?: string;
}) {
// Track if mousedown started on backdrop to prevent closing when selecting text
const mouseDownOnBackdrop = useRef(false);
return (
<div
className={`fixed top-0 right-0 left-0 bottom-0 flex justify-center items-center text-white transition-all duration-300 z-800 w-full h-full backdrop-blur-[10px] gap-2 px-4 ${selected ? "opacity-100 visible" : "opacity-0 invisible"}`}
role="button"
tabIndex={0}
onMouseDown={(e) => {
// Track if mousedown started on the backdrop itself
mouseDownOnBackdrop.current = e.target === e.currentTarget;
}}
onClick={(e) => {
// Only close if both mousedown AND click happened on the backdrop
// This prevents closing when selecting text and dragging outside
if (e.target === e.currentTarget && mouseDownOnBackdrop.current) {
setSelected(null);
}
mouseDownOnBackdrop.current = false;
}}
onKeyDown={(e) => {
if (e.key === "Escape") {
e.preventDefault();
setSelected(null);
}
}}
aria-label="Close modal"
>
<div
className={`flex flex-col items-center justify-center overflow-hidden ${className || "bg-white dark:bg-gray-700 rounded-lg border border-gray-800"}`}
style={{
maxWidth: maxWidth || "100%",
maxHeight: maxHeight || "100%",
height: height || "",
}}
>
{children}
</div>
</div>
);
}