Allow persisting scroll-to anchor as query param
Opened this issue · 9 comments
It would be nice to persist the scroll-to as a query param to allow sharing a link with an anchored element.
Here's one way it could be done:
- Create a ScrollToMixin that you mix into the controller of the route you want to allow persisting a scroll-to link
- That mixin handles detecting the presence of that query param and does the scrolling action when that param is added or changed.
We have a home-grown solution we're using on our project ( like this https://ibotta.com/how?st=%23how-receipt ) that we could bring to this project if there's interest.
sorry for the slow response on this @blimmer. Are you looking to have the scroll be animated? Otherwise this would be handled automatically by the browser with a hash in the url, right? Just want to make sure I'm fully understanding what you're talking about.
Thanks!
Now it's my turn to apologize for the slow response!
It doesn't particularly matter to me whether or not the scroll is animated. However, since Ember does the rendering after the original browser ready event, a hash in the URL won't do anything on an ember site. That's why we implemented a mixin that pays attention to a query parameter and uses Javascript to scroll the page.
Ahhh I gotcha now. That makes sense.
I feel like this is a lil outside of the scope of this particular addon but I could probably be convinced otherwise if you feel strongly about it. What do you think? It's jut not something I've needed myself.
I think one could argue that the purpose of this library is to "restore" and improve the browser's native jump-to-id behavior.
<a href='foo-id'>Jump to Foo</a>
...
<div id='foo-id'>
Foo text
</div>
It's already improved because of the easing effect we have. However, with the "restore" portion of the problem, we lose the behavior where you can click a jump-to-id anchor and share that link with a friend, having them redirected to a specific portion of the content.
I think it's definitely reasonable to think about whether or not this belongs in this addon! What do you think?
I think you make a great point with the "restoring" of the browser's natural behavior. I'm on board.
I think it's a lil unfortunate that with Ember's current approach to query params that this requires the consumer of the addon use a controller that they might not otherwise use. Seems unavoidable at this time though.
Mind putting together a PR whenever you get some time. Thanks @blimmer!