fix: router maintains scroll position after navigation #6377
Labels
area:fast-router
Pertains to fast-router
improvement
A non-feature-adding improvement
status:needs-investigation
Needs additional investigation
Milestone
🐛 Bug Report
When using
Router
, if you have a link towards the bottom of a page that navigates to another route, after navigating to the new route, the scroll position will be at the bottom of the new page💻 Repro or Code Sample
Will add a functioning example later.
Steps to repro are:
After the navigation is complete the page's scroll position will not be at the top of the second page.
🤔 Expected Behavior
After navigation the scroll position should be reset to be at the top of the new page.
😯 Current Behavior
After navigation the scroll position is not reset.
💁 Possible Solution
I'm not sure what the best solution would be here so open to ideas. Since the scroll container can really be any element, it would be difficult for Router to reset the scroll position without walking the entire dom tree looking for containers and setting the scroll position.
One possible solution would be to set the scroll position on both the new route's page element, the body element, and on the router itself. This has the drawback of not working at all if the author is using a different element for the primary content scrolling.
Another solution could be to add a 'route-change' event to
Router
and provide guidance to authors through documentation to listen to that event in order to reset the scroll position where needed. This solution is the most flexible that I can think of at the moment, it still allows authors to build pages however they like and gives them a hook into when navigation happens.🔦 Context
Building an application with
Router
where multiple pages have quite a bit of content and links to other pages scattered throughout.🌍 Your Environment
fast-router - latest alpha
fast-element - latest beta
fast-foundation - latest alpha
adaptive-ui - latest alpha
vite - latest
Windows 11 - latest public release
Microsoft Edge - latest public release
Microsoft Edge - latest canary release
The text was updated successfully, but these errors were encountered: