Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent navigation if page has unsaved changes #137

Open
FeldrinH opened this issue Jul 28, 2022 · 1 comment
Open

Prevent navigation if page has unsaved changes #137

FeldrinH opened this issue Jul 28, 2022 · 1 comment

Comments

@FeldrinH
Copy link

Is there a way to prevent navigating away from a page under certain condition, such as when the page contains a form with unsaved changes?
(Something like beforeNavigate in SvelteKit)

@flipkickmedia
Copy link

flipkickmedia commented Dec 18, 2022

Set a flag in context which indicates there are changes isDirty, then do something like this:

let ctx = getContext("unsavedChanges");

window.onload = function() {
    window.addEventListener("beforeunload", function (e) {
        if (!ictx.isDirty) {
            return undefined;
        }

< do js to show the message >

    });
};

We could store this state inside the route config but you'd need to pass that into the config.

This repo is no longer being maintained, so head over to https://github.com/flipkickmedia/svelte-router-spa if you still want this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants