mirror of
https://github.com/Ku6epXBOCTuK/easy-png-tools.git
synced 2026-11-13 17:08:20 +00:00
refactor: remove double logic, change names
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
// Popover dismiss: close on pointerdown outside the root and on Escape.
|
||||
// Subscribe inside `$effect` while the popover is open; returns unsubscribe.
|
||||
export function onDismiss(
|
||||
root: () => HTMLElement | null,
|
||||
close: () => void,
|
||||
): () => void {
|
||||
function onPointerDown(e: PointerEvent) {
|
||||
if (!root()?.contains(e.target as Node)) close();
|
||||
}
|
||||
function onKey(e: KeyboardEvent) {
|
||||
if (e.key === "Escape") close();
|
||||
}
|
||||
window.addEventListener("pointerdown", onPointerDown);
|
||||
window.addEventListener("keydown", onKey);
|
||||
return () => {
|
||||
window.removeEventListener("pointerdown", onPointerDown);
|
||||
window.removeEventListener("keydown", onKey);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user