Open wmertens opened 2 months ago
So that I understand correctly, if the user was to navigate to this form page, and then fill out a form, let's say they hit the browser back button
, this would prevent them from going to the previous page?
I sometimes do this on accident and see that the browser sometimes prevents me from going back to the previous URL and I'm relieved. I wonder at what point this would be a frustrating experience.
@thejackshelton yes correct. I hope it's not a frustrating experience when the navigation is only prevented when there are changes. By making it a hook, it can get at all the metadata it needs to make that decision.
This is terrific functionality, and a common need in web apps when the page is in a dirty state. The UX way to avoid frustration is to ensure there is either:
These wouldn't need to be enforced in any way, but could be mentioned in documentation as best practices.
thanks @AnthonyPAlicea ! Important point!
Now that it's merged and will soon be released as "experimental" we would love a docs PR with your expertise sprinkled all over it 🙏🙌
Proposed in #14
Champion
@wmertens
Summary:
usePreventNavigate((url?: URL) => boolean | Promise<boolean>)
What's the motivation for this proposal?
Problems you are trying to solve:
Goals you are trying to achieve:
Proposed Solution / Feature
What do you propose?
We add
usePreventNavigate(cb)
, which registers a handler that will be called when there is a navigation event. When the handler returnstrue
, navigation is blocked.This is actually not trivial to implement correctly because not all navigation passes through qwik-city, and when the browser is involved the answer need to be synchronous.
If the navigation attempt is going through qwik-city, the callback will receive the URL of the desired target. This makes it possible to perform the navigation later.
Code examples
using a modal library:
Using a separate modal:
PRs/ Links / References
PRs:
https://github.com/QwikDev/qwik/pull/6825
Reference:
Remix Router has
useBlocker
, there are interesting tidbits in their decisions document.