Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scroll Position Logic Improvements #1

Closed
ryanwiemer opened this issue Jan 18, 2018 · 4 comments
Closed

Scroll Position Logic Improvements #1

ryanwiemer opened this issue Jan 18, 2018 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@ryanwiemer
Copy link
Owner

Come up with a better solution for when to remember or not remember the scroll position.

Remember Scroll Position

  • When a user clicks the back button
  • When a user manually refreshes the page

Start At The Top Of The Page

  • When a user clicks on a link
@ryanwiemer ryanwiemer added the enhancement New feature or request label Jan 18, 2018
@ryanwiemer ryanwiemer self-assigned this Jan 18, 2018
@dsbrianwebster
Copy link

How about this Ryan? Seems to satisfy all the above criteria for me when I include in in my gatsby-browser.js file?

exports.onRouteUpdate = args => {
  if (typeof window !== 'undefined' && args.action == "PUSH") {
    window.scrollTo(0, 0)
  }
}

exports.shouldUpdateScroll = args => {
  //  scroll on < back, forward >, refresh, but not link clicks
  return (
    args.prevRouterProps == null ||
    (args.prevRouterProps && args.prevRouterProps.history.action == "POP")
  ) ? true : false;
};

@ryanwiemer
Copy link
Owner Author

@dsbrianwebster thanks for the suggestion! After an initial test that appears to accomplish what I was after. I will have to do a little more testing to be sure but the logic looks good to me.

@ziyafenn
Copy link

Didn't really work for me. I see it working in a console but no effect on scrolling position.

@ryanwiemer
Copy link
Owner Author

ryanwiemer commented Jan 29, 2018

@ziyafenn,

Sorry for the double post if you get notified again. Not sure what your issue might be. For good measure I would clear your cache and make sure your are using the latest version of Gatsby. I used Gatsby version 1.9.172 when testing this solution and it seemed to work fine for me. I can't vouch for future or past versions though.

I thought for a moment I was able to replicate your issue but after checking my dependencies and clearing my cache it was working. 🤷

Sorry if that isn't too helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants