AlexxNB / tinro

Highly declarative, tiny, dependency free router for Svelte's web applications.
MIT License
673 stars 30 forks source link

Action on each navigation #3

Closed frederikhors closed 4 years ago

frederikhors commented 4 years ago

In many routers is possible to have a function called after each navigation, useful for actions like:

window.scrollTo(0, 0)

Is this possible with tinro?

frederikhors commented 4 years ago

Trying the router after opening this issue I found I cannot "save" my scroll position using back and forward of the browser.

Is there a way to do so?

AlexxNB commented 4 years ago

You may do $: $router.path, window.scrollTo(0, 0) in your root component to run something when route changes. Or native router.subscribe( _ => window.scrollTo(0, 0));

Back and forward cause remounting of corresponding components so scroll position always be on the top. I think it is possible to save scroll position and restore it, but I must be sure whether other unwanted effects won't be.

AlexxNB commented 4 years ago

Just do some investigations about scroll restoration and found that it works out of the box in Chrome, but not in Firefox. So read this https://www.ccdatalab.org/blog/automatic-scroll-restoration-single-page-applications for more info. Look for polyfill for scroll restoration.