-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Fix scroll related issues #8061
Conversation
const results = apiRunner(`shouldUpdateScroll`, { | ||
prevRouterProps, | ||
pathname, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that if we leave pathname
here for backward compatibility, this change would potentially could go in
Not sure why |
This should be good to go, let me know if there's anything else missing from my side. |
You should update the docs @ https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/utils/api-browser-docs.js#L56 |
pathname, | ||
routerProps, | ||
savedScrollPositions: this._stateStorage, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The naming here doesn't match the actual behavior. You don't get positions, you only get the storage.
But getting the storage here, implies you have to know how it works.
AFAIK, it isn't documented as it wasn't really a public API up until now. And when exposed to public, it should get documented, at least an example for how one might use it in shouldUpdateScroll
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, will update the docs with an example. Maybe it's enough to expose getSavedScrollPosition: this._stateStorage.read
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That'd seem to be better, yeah.
pathname, | ||
routerProps, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm scared about history
being passed here, same as I'm not a fan of location.replace
/location.reload
.
I wouldn't expose functionality that could be abused outside the normal loop of things potentially causing unexpected results.
Updated the docs, plucked off |
|
Help! Could anybody give me some hints how to turn this into proper jsdoc, thanks! /**
* Allow a plugin to decide if the scroll position should update or
* not on a route change.
*
* @callback getSavedScrollPosition
* @param {object} location A location object.
* @param {string} location.pathname The path of the route.
* @returns {Array|null} An [x, y] array of coordinates, or `null` if nothing was found.
*
* @function shouldUpdateScroll
* @param {object} $0
* @param {object} $0.prevRouterProps The previous state of the router before the route change.
* @param {object} $0.routerProps The current state of the router.
* @param {string} $0.pathname The new pathname (for backwards compatibility with v1).
* @param {getSavedScrollPosition} $0.getSavedScrollPosition Takes a location and returns the coordinates of
* the last scroll position for that location, or `null`. Gatsby saves scroll positions for each route
* in `SessionStorage`, so they are available after page reload.
* @returns {boolean|string|Array} Should return either an [x, y] Array of coordinates to scroll to,
* a string of the `id` or `name` of an element to scroll to, `false` to not update the scroll position,
* or `true` for the default behavior.
* @example
* exports.shouldUpdateScroll = ({
* routerProps: { location },
* getSavedScrollPosition
* }) => {
* const currentPosition = getSavedScrollPosition(location)
* const queriedPosition = getSavedScrollPosition({ pathname: '/random' })
*
* window.scrollTo(...(currentPosition || [0, 0]))
*
* return false
* }
*/
exports.shouldUpdateScroll = true |
@stefanprobst what exactly is the problem over there? |
@joltmode Neither Unfortunately I could not find an example in the gatsby docs to emulate - the closest seems So my question is how to document |
Updated the docs. Should be good now, apart from the fact that the return values don't show on the website, |
b70dd5a
to
fc4ca3e
Compare
fc4ca3e
to
2116fff
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @stefanprobst! Finally got chance to get back to this PR
Great, thanks! |
…gatsbyjs#8061) * Move scroll handling from Link to shouldUpdateScroll * Pass all router props to shouldUpdateScroll * Pass saved scroll positions to shouldUpdateScroll * Let scroll-behavior handle everything * Let scroll-behavior also handle element checking * Keep pathname for backwards compatibility * Make tests pass * Don't expose history * Only expose read method * Update docs * Update docs * Lint * Trigger travis rebuild (empty commit) * remove TODO, scrollbehaviour in fact does that
Attempt to solve some scroll related issues:
<Link />
and put it inshouldUpdateScroll
. Also, letscroll-behavior
handle scrolling to element ids. This should fix Fix handling of hash links & scrolling #7017, Gatsby's Link overrides scrolling behavior with anchors #7450, Back button from anchor link does not scroll to previous location #8019.shouldUpdateScroll
browser API more flexible by passing the whole routerProps object, and allowing access to the saved scroll positions. This allows things like delaying scroll updates until after a page transition animation has finished, should fix [using-page-transitions] Scrolls to top before animating #7921. Maybe this one is a little late for v2 and should be separated into another PR.