Pre-conditions (route guards) to handle close modal
See original GitHub issueHello, svelte-spa-router is really fine and works as expected. Thank you.
But I need to handle the browser history back to close a modal. So i have read the doc at https://github.com/ItalyPaleAle/svelte-spa-router/blob/master/Advanced Usage.md#route-pre-conditions
I have a store variable which tells me if I accept to route or not.
If not, I expected “svelte-spa-router” to do nothing. Sure it does not load any route but it seems to unload the current component.
With Vue I dit it like this :
router.beforeEach((to, from, next) => {
if (store.state.isOpenModal) {
store.state.isOpenModal = false // close modal
next(false) // cancel routing
return
}
next() // accept routing
})
This works perfectly even if I don’t understand how they prevent onpopstate(). I transposed like this in Svelte :
const routes = {
'/': wrap(
Home,
() => { return nextOrNot() }
),
'/about': wrap(
About,
() => { return nextOrNot() }
)
}
function nextOrNot () {
if (isOpenModal) {
iisOpenModal.set(false)
return false
}
return true
}
My modal is closed but my current component is gone.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Routing to Angular Material Dialogs | by John Crowson | ngconf
open/close handled in DialogEntryComponent }. Declare the dummy component and configure the Router Module to load it on 'dialog' navigation ...
Read more >Router tutorial: tour of heroes - Angular
In this tutorial, you build upon a basic router configuration to explore features such as child routes, route parameters, lazy load NgModules, guard...
Read more >How to deal with a modal shown in a guard - Stack Overflow
When I type an unauthorized route in my browser's address bar, the server-side rendering shows an inert modal, then when the client-side takes ......
Read more >Angular Basics: CanActivate—Introduction to Routing Guards
We'll look at ensuring the routes we create in Angular are accessed by the right people and preventing unauthorized access to routes that ......
Read more >How to Create a Modal Route with React Router - Morioh
modal -wrapper , which would close the modal when the actual .modal element is activated. import React from 'react'; import { withRouter }...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’ll take a look at this and see what can be done
I’m not sure how I would do that, I’d have to experiment.
One lead (which might or might not work) could be using a route guard to block the new component from mounting if the form is dirty, and inside the route guard invoke
push()to send the user back to the page they were on before. This would likely still cause the component to be un-mounted, so you’d have to restore the state of the form manually.