-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContextMenu.tsx
More file actions
235 lines (201 loc) · 6.93 KB
/
ContextMenu.tsx
File metadata and controls
235 lines (201 loc) · 6.93 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import React, { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
export type MenuItem =
| {
label: string;
action?: () => void;
icon?: React.FC<{ className?: string }>;
disabled?: boolean;
shortcut?: string;
children?: MenuItem[];
}
| { type: 'separator' };
interface ContextMenuProps {
isOpen: boolean;
position: { x: number; y: number };
items: MenuItem[];
onClose: () => void;
}
const EDGE_MARGIN = 8;
const ContextMenu: React.FC<ContextMenuProps> = ({ isOpen, position, items, onClose }) => {
const menuRef = useRef<HTMLDivElement>(null);
const [activeSubmenu, setActiveSubmenu] = useState<{ depth: number; index: number } | null>(null);
const [menuStyle, setMenuStyle] = useState<{ top: number; left: number; maxHeight: number; overflowY: React.CSSProperties['overflowY'] }>({
top: position.y,
left: position.x,
maxHeight: 0,
overflowY: 'visible',
});
const recalculatePosition = useCallback(() => {
const menu = menuRef.current;
if (!menu) return;
const { innerWidth, innerHeight } = window;
const rect = menu.getBoundingClientRect();
const maxHeight = Math.max(innerHeight - EDGE_MARGIN * 2, 0);
let left = rect.left;
let top = rect.top;
if (rect.right > innerWidth - EDGE_MARGIN) {
left = Math.max(EDGE_MARGIN, innerWidth - rect.width - EDGE_MARGIN);
}
if (left < EDGE_MARGIN) {
left = EDGE_MARGIN;
}
if (rect.bottom > innerHeight - EDGE_MARGIN) {
top = Math.max(EDGE_MARGIN, innerHeight - rect.height - EDGE_MARGIN);
}
if (top < EDGE_MARGIN) {
top = EDGE_MARGIN;
}
const overflowY: React.CSSProperties['overflowY'] = rect.height > maxHeight ? 'auto' : 'visible';
setMenuStyle((previous) => {
if (
previous.top === top &&
previous.left === left &&
previous.maxHeight === maxHeight &&
previous.overflowY === overflowY
) {
return previous;
}
return {
top,
left,
maxHeight,
overflowY,
};
});
}, []);
useEffect(() => {
if (!isOpen) return;
const handleClickOutside = (event: MouseEvent) => {
if (menuRef.current && !menuRef.current.contains(event.target as Node)) {
onClose();
}
};
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
onClose();
}
}
document.addEventListener('mousedown', handleClickOutside);
window.addEventListener('keydown', handleKeyDown);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
window.removeEventListener('keydown', handleKeyDown);
};
}, [isOpen, onClose]);
useEffect(() => {
if (!isOpen) {
setActiveSubmenu(null);
}
}, [isOpen]);
useLayoutEffect(() => {
if (!isOpen) return;
setMenuStyle((previous) => {
if (previous.top === position.y && previous.left === position.x) {
return previous;
}
return {
top: position.y,
left: position.x,
maxHeight: previous.maxHeight,
overflowY: previous.overflowY,
};
});
const frame = requestAnimationFrame(() => {
recalculatePosition();
});
return () => cancelAnimationFrame(frame);
}, [isOpen, position.x, position.y, recalculatePosition]);
useLayoutEffect(() => {
if (!isOpen) return;
const frame = requestAnimationFrame(() => {
recalculatePosition();
});
return () => cancelAnimationFrame(frame);
}, [isOpen, items, recalculatePosition]);
useEffect(() => {
if (!isOpen) return;
const handleResize = () => {
recalculatePosition();
};
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}, [isOpen, recalculatePosition]);
if (!isOpen) return null;
const overlayRoot = document.getElementById('overlay-root');
if (!overlayRoot) return null;
const renderMenuItems = (menuItems: MenuItem[], depth = 0) => (
<ul className="space-y-1">
{menuItems.map((item, index) => {
if ('type' in item) {
return <li key={`separator-${depth}-${index}`} className="h-px bg-border-color my-1.5" />;
}
const { label, action, icon: Icon, disabled, shortcut, children } = item;
const hasChildren = Array.isArray(children) && children.length > 0;
const isSubmenuActive = activeSubmenu?.depth === depth && activeSubmenu.index === index;
return (
<li
key={`${label}-${depth}-${index}`}
className="relative"
onMouseEnter={() => setActiveSubmenu({ depth, index })}
onMouseLeave={() => setActiveSubmenu(prev => (prev?.depth === depth && prev.index === index ? null : prev))}
>
<button
onClick={() => {
if (!disabled && action && !hasChildren) {
action();
onClose();
}
}}
disabled={disabled}
className="w-full flex items-center justify-between text-left px-2 py-1.5 text-xs rounded-md transition-colors text-text-main disabled:text-text-secondary/50 disabled:cursor-not-allowed hover:bg-primary hover:text-primary-text focus:bg-primary focus:text-primary-text focus:outline-none"
>
<div className="flex items-center gap-3">
{Icon && <Icon className="w-4 h-4" />}
<span>{label}</span>
</div>
<div className="flex items-center gap-2 text-xs text-text-secondary">
{shortcut && <span>{shortcut}</span>}
{hasChildren && <span aria-hidden="true">›</span>}
</div>
</button>
{hasChildren && isSubmenuActive && (
<div className="absolute top-0 left-full ml-1">
<div className="w-[16rem] rounded-md bg-secondary p-1.5 shadow-2xl border border-border-color animate-fade-in-fast">
{renderMenuItems(children, depth + 1)}
</div>
</div>
)}
</li>
);
})}
</ul>
);
return ReactDOM.createPortal(
<div
ref={menuRef}
style={{
top: menuStyle.top,
left: menuStyle.left,
maxHeight: menuStyle.maxHeight ? menuStyle.maxHeight : undefined,
overflowY: menuStyle.overflowY,
}}
className="fixed z-50 w-[16.8rem] rounded-md bg-secondary p-1.5 shadow-2xl border border-border-color animate-fade-in-fast"
>
{renderMenuItems(items)}
<style>{`
@keyframes fade-in-fast {
from { opacity: 0; transform: scale(0.95); }
to { opacity: 1; transform: scale(1); }
}
.animate-fade-in-fast {
animation: fade-in-fast 0.1s ease-out forwards;
}
`}</style>
</div>,
overlayRoot
);
};
export default ContextMenu;