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

feat: add API to pause scroll position saving #306

Merged
merged 5 commits into from Dec 18, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/index.js
Expand Up @@ -54,6 +54,8 @@ export default class ScrollBehavior {
this._windowScrollTarget = null;
this._numWindowScrollAttempts = 0;

this._isTransitioning = false;

this._scrollElements = {};

// We have to listen to each window scroll update rather than to just
Expand All @@ -62,6 +64,8 @@ export default class ScrollBehavior {
on(window, 'scroll', this._onWindowScroll);

this._removeTransitionHook = addTransitionHook(() => {
this._isTransitioning = true;
hedgepigdaniel marked this conversation as resolved.
Show resolved Hide resolved

requestAnimationFrame.cancel(this._saveWindowPositionHandle);
this._saveWindowPositionHandle = null;

Expand Down Expand Up @@ -93,7 +97,12 @@ export default class ScrollBehavior {
shouldUpdateScroll,
savePositionHandle: null,

onScroll() {
onScroll: () => {
if (this._isTransitioning) {
// Don't save the scroll position until the transition is complete.
return;
}

if (!scrollElement.savePositionHandle) {
scrollElement.savePositionHandle = requestAnimationFrame(
saveElementPosition,
Expand Down Expand Up @@ -133,6 +142,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 +180,11 @@ export default class ScrollBehavior {
}

_onWindowScroll = () => {
if (this._isTransitioning) {
// Don't save the scroll position until 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