Skip to content
This repository has been archived by the owner on Jul 28, 2018. It is now read-only.

Add possibility to keep current scroll position in the Turbolinks.visit API #291

Closed
wants to merge 1 commit into from
Closed

Conversation

tzvetkoff
Copy link
Contributor

On certain occasions some might need to keep current scroll position.
In my case I call Turbolinks.visit(location.href) to reload the content of the current page -- this is an improvised replacement for location.reload().
Problem is, that if you want to reload the current page content for, say, new elements created by AJAX form submission, the page will scroll back to top, yet you want to keep the scroll position.

Partial replacements is a way to do this, but requires specific Rails code on the backend to just render that partial and then you're getting even more tied to the front-end.

So, I've added an extra option to keep the scroll position.

When invoking Turbolinks manually:

Turbolinks.visit(path, { keepScrollPosition: true });

Or by marking the element with the data-turbolinks-keep-scroll attribute:

<a href="/path" data-turbolinks-keep-scroll>Change the page and keep current scroll position.</a>

Edited: 2015-05-04

@tzvetkoff
Copy link
Contributor Author

I've rebased against current master, updated the code and changed the "logic" a bit to respect the name (keep_scroll)

@reed
Copy link
Collaborator

reed commented Mar 9, 2014

Would I be correct in saying that the only scenario where you'd use this is if you're reloading the current page? I mean, it wouldn't really make sense to keep the scroll position after visiting a different page, right? So maybe it would be cleaner and more intuitive to add a Turbolinks.reload() function instead of modifying the visit function. What do you think?

@morr
Copy link

morr commented Jul 29, 2014

@reed no it is not the only scenario. Keeping scroll position is also makes sense for applications with some turbolinks filters on left or right page side.
Please look at #66.

68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f313437393231352f3235343837342f31363438363435362d386266652d313165322d396565392d3231666536383139383461622e706e67

@ajb
Copy link
Contributor

ajb commented Sep 28, 2014

👍 -- can we get an updated status/reason for exclusion on this? This feature is useful to me in another way -- for appending items to a list via AJAX. The server responds with redirect_via_turbolinks_to, but the scroll position stays the same.

@drewhamlett
Copy link

I believe this will be more important with the new Turbolinks 3 partial changes. Right now when the partial changes the page scrolls to the top. This is not what you really want happening.

Right now I'm doing this. Which was posted in one of the other issues about scrolling.

$(document).on "page:change", ->
  window.prevPageYOffset = window.pageYOffset
  window.prevPageXOffset = window.pageXOffset

$(document).on "page:load", ->
  window.scrollTo window.prevPageXOffset, window.prevPageYOffset

@Thibaut
Copy link
Collaborator

Thibaut commented May 3, 2015

Right now when the partial changes the page scrolls to the top.

@drewhamlett This shouldn't happen with Turbolinks.replace / render :action, change: :key. Can you post the code that you're using?

@drewhamlett
Copy link

I must be missing something, because I'm using the redirect to in this case. I have a section on a page that gets updated, but the page requires a bunch of other data to re render that page, not specific the section that gets updated. If I just use render I have to duplicate a lot of code to fetch all the other pieces of the page.

Unless I'm doing something wrong?

@Thibaut
Copy link
Collaborator

Thibaut commented May 3, 2015

@drewhamlett Oops, I forgot that Turbolinks.visit / redirect_to can also do partial replacements. Expect a PR / fix soon.

@tzvetkoff
Copy link
Contributor Author

Rewrote the patch to conform to the latest Turbolinks code and updated the PR description.

@Thibaut
Copy link
Collaborator

Thibaut commented May 4, 2015

@tzvetkoff I'm working on a similar PR with tests for some of the edge cases and a fix for the problem @drewhamlett described. Will keep you updated.

@Thibaut Thibaut mentioned this pull request May 10, 2015
@vizo
Copy link

vizo commented May 12, 2015

👍 Thanks guys, very useful. Browsing will look like on SPA :)

@Thibaut
Copy link
Collaborator

Thibaut commented May 26, 2015

Turbolinks.visit(url, scroll: false) has been merged into master (#528). I will PR data-turbolinks-scroll=false and update the docs this week-end.

Thanks all for pushing this forward.

@Thibaut Thibaut closed this May 26, 2015
@ajb
Copy link
Contributor

ajb commented May 26, 2015

Thanks @Thibaut and everyone!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants