Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ export default class ScrollBehavior {
},
};

// In case no scrolling occurs, save the initial position
if (!scrollElement.savePositionHandle) {
scrollElement.savePositionHandle = requestAnimationFrame(
saveElementPosition,
);
}

this._scrollElements[key] = scrollElement;
on(element, 'scroll', scrollElement.onScroll);

Expand All @@ -126,7 +133,15 @@ export default class ScrollBehavior {
}

updateScroll(prevContext, context) {
this._updateWindowScroll(prevContext, context);
this._updateWindowScroll(prevContext, context).then(() => {
// Save the position immediately after a transition so that if no
// scrolling occurs, there is still a saved position
if (!this._saveWindowPositionHandle) {
this._saveWindowPositionHandle = requestAnimationFrame(
this._saveWindowPosition,
);
}
});

Object.keys(this._scrollElements).forEach(key => {
this._updateElementScroll(key, prevContext, context);
Expand Down Expand Up @@ -216,7 +231,7 @@ export default class ScrollBehavior {
// scroll it isn't enough. Instead, try to scroll a few times until it
// works.
this._numWindowScrollAttempts = 0;
this._checkWindowScrollPosition();
return this._checkWindowScrollPosition();
}

_updateElementScroll(key, prevContext, context) {
Expand Down Expand Up @@ -282,7 +297,7 @@ export default class ScrollBehavior {
// Still, check anyway just in case.
/* istanbul ignore if: paranoid guard */
if (!this._windowScrollTarget) {
return;
return Promise.resolve();
}

this.scrollToTarget(window, this._windowScrollTarget);
Expand All @@ -291,13 +306,16 @@ export default class ScrollBehavior {

/* istanbul ignore if: paranoid guard */
if (this._numWindowScrollAttempts >= MAX_SCROLL_ATTEMPTS) {
// This might happen if the scroll position was already set to the target
this._windowScrollTarget = null;
return;
return Promise.resolve();
}

this._checkWindowScrollHandle = requestAnimationFrame(
this._checkWindowScrollPosition,
);
return new Promise(resolve => {
this._checkWindowScrollHandle = requestAnimationFrame(() =>
resolve(this._checkWindowScrollPosition()),
);
});
};

scrollToTarget(element, target) {
Expand Down
26 changes: 26 additions & 0 deletions test/ScrollBehavior.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,32 @@ describe('ScrollBehavior', () => {
},
]);
});

it('should save position even if it does not change', done => {
const history = withRoutes(
withScroll(createHistory(), (prevLoc, loc) =>
loc.action === 'PUSH' ? [10, 20] : true,
),
);

unlisten = run(history, [
() => {
history.push('/detail');
},
() => {
history.push('/');
},
() => {
history.push('/detail');
},
() => history.goBack(),
() => {
expect(scrollLeft(window)).to.equal(10);
expect(scrollTop(window)).to.equal(20);
done();
},
]);
});
});

describe('scroll element', () => {
Expand Down
4 changes: 3 additions & 1 deletion test/run.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export function delay(cb) {
// Give throttled scroll listeners time to settle down.
requestAnimationFrame(() => requestAnimationFrame(cb));
requestAnimationFrame(() =>
requestAnimationFrame(() => requestAnimationFrame(cb)),
);
}

export default function run(history, steps) {
Expand Down