Files
WebHooker/app/composables/useBackdropGuard.ts
T
翅膀 a8418c06d9 fix(editors): ignore accidental backdrop dismissal
Do not discard unsaved route or group edits when the user clicks outside the dialog. Preserve Escape, Cancel and explicit close, including popovers and confirmation dialogs stacked above the editor.
2026-10-17 08:12:55 +08:00

47 lines
1.6 KiB
TypeScript

/**
* Keeps an editor dialog open when the user clicks outside it, so a stray click
* on the backdrop never throws away unsaved input. RcDialog exposes no hook for
* reka's `interactOutside`, so this watches pointer-downs while the dialog is
* open: a press outside the dialog (and outside any popover or dialog stacked
* above it) marks the close request it triggers as one to ignore. Escape, the
* close button and Cancel keep closing the dialog.
*/
export function useBackdropGuard(isOpen: () => boolean, dialogSelector: string) {
let pressedOutside = false;
function onPointerDown(event: PointerEvent): void {
const target = event.target as Element | null;
pressedOutside = !target?.closest?.(
`${dialogSelector}, [data-dismissable-layer], [data-reka-popper-content-wrapper]`,
);
}
function onKeyDown(): void {
pressedOutside = false;
}
function listen(on: boolean): void {
if (typeof document === "undefined") return;
if (on) {
document.addEventListener("pointerdown", onPointerDown, true);
document.addEventListener("keydown", onKeyDown, true);
return;
}
document.removeEventListener("pointerdown", onPointerDown, true);
document.removeEventListener("keydown", onKeyDown, true);
pressedOutside = false;
}
watch(isOpen, listen, { immediate: true });
onBeforeUnmount(() => listen(false));
/** Whether the pending close request came from a press outside the dialog. */
function isBackdropClose(): boolean {
const outside = pressedOutside;
pressedOutside = false;
return outside;
}
return { isBackdropClose };
}