Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
scrollToTarget(element, target) {
if (typeof target === 'string') {
const targetElement =
document.getElementById(target) ||
document.getElementsByName(target)[0];
if (targetElement) {
targetElement.scrollIntoView();
return;
}
// Fallback to scrolling to top when target fragment doesn't exist.
target = [0, 0]; // eslint-disable-line no-param-reassign
}
const [left, top] = target;
scrollLeft(element, left);
scrollTop(element, top);
}
}
_updateElementScroll(key, prevContext, context) {
const { element, shouldUpdateScroll } = this._scrollElements[key];
const scrollTarget = this._getScrollTarget(
key, shouldUpdateScroll, prevContext, context
);
if (!scrollTarget) {
return;
}
// Unlike with the window, there shouldn't be any flakiness to deal with
// here.
const [x, y] = scrollTarget;
scrollLeft(element, x);
scrollTop(element, y);
}
_savePosition(key, element) {
// We have to directly update `DOMStateStorage`, because actually updating
// the location could cause e.g. React Router to re-render the entire page,
// which would lead to observably bad scroll performance.
saveState(
this._getKey(this._getCurrentLocation(), key),
[scrollLeft(element), scrollTop(element)]
);
}
_onWindowScroll = () => {
// 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
// location update.
if (this._saveWindowPositionHandle === null) {
this._saveWindowPositionHandle =
requestAnimationFrame(this._saveWindowPosition);
}
if (this._windowScrollTarget) {
const [xTarget, yTarget] = this._windowScrollTarget;
const x = scrollLeft(window);
const y = scrollTop(window);
if (x === xTarget && y === yTarget) {
this._windowScrollTarget = null;
this._cancelCheckWindowScroll();
}
}
};
() => {
expect(prevPosition).to.eql([10, 15000]);
expect(position).to.not.exist;
expect(scrollLeft(window)).to.not.equal(0);
expect(scrollTop(window)).to.not.equal(0);
scrollLeft(window, 0);
scrollTop(window, 0);
delay(() => history.goBack());
},
() => {
() => {
expect(prevPosition).to.eql([10, 15000]);
expect(position).to.not.exist;
expect(scrollLeft(window)).to.not.equal(0);
expect(scrollTop(window)).to.not.equal(0);
scrollLeft(window, 0);
scrollTop(window, 0);
delay(() => history.goBack());
},
() => {
_savePosition(key, element) {
this._stateStorage.save(this._getCurrentLocation(), key, [
scrollLeft(element),
scrollTop(element),
]);
}