feat(router-core,history): Global blocking status & proceed/reset actions #4398
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces a global “blocker” state in and the callbacks needed to continue (
proceed
) or cancel (reset
) a blocked navigation globally, to be consumed directly from the router state.This enables controlling the blocking status out side the context of the
useBlocker
that was used to initiate the block.The per-route
useBlocker
API remains unchanged—it now also updates the router-level state.Discussed previously here.
This change adds
BLOCK
andDISMISS-BLOCK
actions to the history notify function.where the
BLOCK
action holds a ref to the current awaited promise resolver thats currently blocking the navigation, which is then consumed through the router state.Why?
I needed a single, root-level modal that appears whenever any navigation is blocked (unsaved changes, etc.), instead of re-instantiating the modal wherever
useBlocker
is used.I expected the router to expose some global blocking state, this PR attempts to provide this functionality.
A short example of the usage:
A runnable demo is available in examples/global-blocking-state.
Any feedback is appreciated!