mefechoel / svelte-navigator

Simple, accessible routing for Svelte
Other
502 stars 39 forks source link

Prompt for unsaved forms #45

Open iluuu1994 opened 2 years ago

iluuu1994 commented 2 years ago

Thank you for this library!

I'm looking for something like React routers Prompt.

When there is unsaved work there are three ways the user should be warned:

  1. window.onbeforeunload: The user presses refresh in the browser or navigates to another website
  2. navigate: The user presses on an internal link that will change the page in the router
  3. popstate: The user presses back in the browser

1 and 2 are solvable relatively easily. createHistory could allow registering callbacks for handling prompts. If any of these listeners return an unsuccessful value (as in there is unsaved work) a prompt would be displayed.

3 is a bit more tricky since the browser doesn't offer a way to block the URL change. Thus on cancel we'd have to navigate back to the page the user just left. This would have to be done before the router removes anything from the page so that no state is lost. This is what react-router does, I think.

Has any work in this area been done? I'll create an implementation if you don't see anything wrong with this approach.

IsAvaible commented 2 years ago
  1. Can be solved by adding the following code to the Githubissues.
  2. Githubissues is a development platform for aggregating issues.