app-route icon indicating copy to clipboard operation
app-route copied to clipboard

Add hooks necessary to save and restore scroll position when user returns to page via back button

Open arthurevans opened this issue 9 years ago • 1 comments

Description

With carbon-route, we can't distinguish between forward navigation and backward navigation. This would be useful so we could restore scroll position when navigating back to a page, which is what the browser does natively.

There may be an obvious approach to this that I'm missing, but I don't understand the timing. If we use plain <a> tags to navigate, <carbon-route>/<carbon-location> handle the changes and ultimately (via <iron-location>) call pushState or replaceState. So the component that consumes the route has no ability to specify a state object.

How could we handle this? Maybe <carbon-route> could expose a callback just before deactivating a route, so the active component could supply its current state. And all of the active carbon-routes could somehow... integrate their states into a big state glob before calling pushState/replaceState.

This doesn't sound pretty, but the other alternative is probably "app does a bunch of magic and still gets it wrong sometimes."

Browsers Affected

  • [x] All da browsers.

arthurevans avatar Apr 21 '16 21:04 arthurevans

Conceptually this is pretty straightforward I think, the main open questions are around how best to plumb the information that describes the nature of the most recent navigation through the system.

iron-location can fire an event on window, kinda analogous to beforeunload, that lets the page know that it's about to call pushState/replaceState, giving the opportunity to save the scroll position.

iron-location also knows the source of a navigation. If it's a history based navigation, we get a popstate event. It might be that the easiest thing would be to plumb this through the route objects.

rictic avatar May 05 '16 21:05 rictic