~james/core

core/web/src/lib/unsavedChanges.ts -rw-r--r-- 408 bytes
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
import { beforeNavigate } from "$app/navigation";

export const guardUnsavedChanges = (dirty: () => boolean) => {
	beforeNavigate((navigation) => {
		if (navigation.shallow && navigation.type === "goto") return;
		if (!dirty()) return;
		if (navigation.type === "leave") {
			navigation.cancel();
			return;
		}
		if (!confirm("You have unsaved changes. Leave without saving?")) navigation.cancel();
	});
};