fix: 修复弹出菜单在新按钮点击后即开即关(改用 pointerdown 捕获关闭,WKWebView 下复现)
This commit is contained in:
1 parent
ee3b819fc5
commit
6307a017a3
1 file changed
+9
-3
@@ -73,13 +73,19 @@ export function ContextMenu({
|
||||
|
||||
useEffect(() => {
|
||||
if (!menu) return;
|
||||
const close = () => onClose();
|
||||
// pointerdown(capture) 关闭:打开菜单的那次交互已完成 pointerdown,
|
||||
// 不会被自己关掉(click 监听在 WKWebView 等环境存在"即开即关"竞态)
|
||||
const close = (e: Event) => {
|
||||
const panel = ref.current;
|
||||
if (panel && e.target instanceof Node && panel.contains(e.target)) return;
|
||||
onClose();
|
||||
};
|
||||
const onKey = (e: KeyboardEvent) => e.key === "Escape" && onClose();
|
||||
window.addEventListener("click", close);
|
||||
window.addEventListener("pointerdown", close, true);
|
||||
window.addEventListener("resize", close);
|
||||
window.addEventListener("keydown", onKey);
|
||||
return () => {
|
||||
window.removeEventListener("click", close);
|
||||
window.removeEventListener("pointerdown", close, true);
|
||||
window.removeEventListener("resize", close);
|
||||
window.removeEventListener("keydown", onKey);
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user