Skip to content

Commit

Permalink
fix: don't save the scroll position between transitionHook and update…
Browse files Browse the repository at this point in the history
…Scroll
  • Loading branch information
hedgepigdaniel committed Nov 13, 2019
1 parent d01bd47 commit 804f14d
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/index.js
Expand Up @@ -53,6 +53,7 @@ export default class ScrollBehavior {
this._checkWindowScrollHandle = null;
this._windowScrollTarget = null;
this._numWindowScrollAttempts = 0;
this._isTransitioning = false;

this._scrollElements = {};

Expand All @@ -62,6 +63,7 @@ export default class ScrollBehavior {
on(window, 'scroll', this._onWindowScroll);

this._removeTransitionHook = addTransitionHook(() => {
this.isTransitioning = true;
requestAnimationFrame.cancel(this._saveWindowPositionHandle);
this._saveWindowPositionHandle = null;

Expand All @@ -78,6 +80,7 @@ export default class ScrollBehavior {
}

registerElement(key, element, shouldUpdateScroll, context) {
const self = this;
invariant(
!this._scrollElements[key],
'ScrollBehavior: There is already an element registered for `%s`.',
Expand All @@ -94,6 +97,9 @@ export default class ScrollBehavior {
savePositionHandle: null,

onScroll() {
if (self.isTransitioning) {
return; // Don't save the scroll position until the transition is complete
}
if (!scrollElement.savePositionHandle) {
scrollElement.savePositionHandle = requestAnimationFrame(
saveElementPosition,
Expand Down Expand Up @@ -133,6 +139,8 @@ export default class ScrollBehavior {
}

updateScroll(prevContext, context) {
this.isTransitioning = false;

this._updateWindowScroll(prevContext, context).then(() => {
// Save the position immediately after a transition so that if no
// scrolling occurs, there is still a saved position
Expand Down Expand Up @@ -169,6 +177,11 @@ export default class ScrollBehavior {
}

_onWindowScroll = () => {
if (this.isTransitioning) {
// Don't save the scroll position unil the transition is complete
return;
}

// It's possible that this scroll operation was triggered by what will be a
// `POP` transition. Instead of updating the saved location immediately, we
// have to enqueue the update, then potentially cancel it if we observe a
Expand Down

0 comments on commit 804f14d

Please sign in to comment.