Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 2x 20x 20x 20x 9x 2x 2x 2x 20x 2x 2x 2x 2x 20x 20x 20x 20x 20x | /** Holds a navigation or a tab close while `dirty` holds, until the user leaves anyway. */
export const useLeaveGuard = (dirty: () => boolean, onLeave: () => void) => {
const leaving = ref(false);
const destination = ref<string | null>(null);
onBeforeRouteLeave((to) => {
if (!dirty()) return true;
destination.value = to.fullPath;
leaving.value = true;
return false;
});
const leave = async () => {
const to = destination.value;
onLeave();
leaving.value = false;
Eif (to) await navigateTo(to);
};
Eif (import.meta.client) {
const warn = (event: BeforeUnloadEvent) => {
if (dirty()) event.preventDefault();
};
onMounted(() => window.addEventListener("beforeunload", warn));
onBeforeUnmount(() => window.removeEventListener("beforeunload", warn));
}
return { leaving, leave };
};
|