Skip to content

Commit

Permalink
chore!: Upgrade to dom-helpers v5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
taion committed Mar 30, 2020
1 parent 3ac1826 commit cd06035
Show file tree
Hide file tree
Showing 4 changed files with 906 additions and 715 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
"homepage": "https://github.com/taion/scroll-behavior#readme",
"dependencies": {
"dom-helpers": "^3.4.0",
"dom-helpers": "^5.1.3",
"invariant": "^2.2.4",
"page-lifecycle": "^0.1.2"
},
Expand Down
34 changes: 16 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/* eslint-disable no-underscore-dangle */

import off from 'dom-helpers/events/off';
import on from 'dom-helpers/events/on';
import scrollLeft from 'dom-helpers/query/scrollLeft';
import scrollTop from 'dom-helpers/query/scrollTop';
import requestAnimationFrame from 'dom-helpers/util/requestAnimationFrame';
import * as animationFrame from 'dom-helpers/animationFrame';
import scrollLeft from 'dom-helpers/scrollLeft';
import scrollTop from 'dom-helpers/scrollTop';
import invariant from 'invariant';
import PageLifecycle from 'page-lifecycle/dist/lifecycle.es5';

Expand Down Expand Up @@ -41,10 +39,10 @@ export default class ScrollBehavior {
// We have to listen to each window scroll update rather than to just
// location updates, because some browsers will update scroll position
// before emitting the location change.
on(window, 'scroll', this._onWindowScroll);
window.addEventListener('scroll', this._onWindowScroll);

const handleTransition = (saveWindowPosition) => {
requestAnimationFrame.cancel(this._saveWindowPositionHandle);
animationFrame.cancel(this._saveWindowPositionHandle);
this._saveWindowPositionHandle = null;

if (saveWindowPosition && !this._ignoreScrollEvents) {
Expand All @@ -53,7 +51,7 @@ export default class ScrollBehavior {

Object.keys(this._scrollElements).forEach((key) => {
const scrollElement = this._scrollElements[key];
requestAnimationFrame.cancel(scrollElement.savePositionHandle);
animationFrame.cancel(scrollElement.savePositionHandle);
scrollElement.savePositionHandle = null;

// It's always fine to save element scroll positions here; the browser
Expand Down Expand Up @@ -106,7 +104,7 @@ export default class ScrollBehavior {

onScroll: () => {
if (!scrollElement.savePositionHandle && !this._ignoreScrollEvents) {
scrollElement.savePositionHandle = requestAnimationFrame(
scrollElement.savePositionHandle = animationFrame.request(
saveElementPosition,
);
}
Expand All @@ -115,13 +113,13 @@ export default class ScrollBehavior {

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

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

this._updateElementScroll(key, null, context);
}
Expand All @@ -137,8 +135,8 @@ export default class ScrollBehavior {
key
];

off(element, 'scroll', onScroll);
requestAnimationFrame.cancel(savePositionHandle);
element.removeEventListener('scroll', onScroll);
animationFrame.cancel(savePositionHandle);

delete this._scrollElements[key];
}
Expand All @@ -148,7 +146,7 @@ export default class ScrollBehavior {
// 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._saveWindowPositionHandle = animationFrame.request(
this._saveWindowPosition,
);
}
Expand Down Expand Up @@ -196,7 +194,7 @@ export default class ScrollBehavior {
stop() {
this._restoreScrollRestoration();

off(window, 'scroll', this._onWindowScroll);
window.removeEventListener('scroll', this._onWindowScroll);
this._cancelCheckWindowScroll();

this._removeTransitionHook();
Expand All @@ -221,7 +219,7 @@ export default class ScrollBehavior {
// we have to enqueue the update, then potentially cancel it if we observe
// a location update.
if (!this._saveWindowPositionHandle) {
this._saveWindowPositionHandle = requestAnimationFrame(
this._saveWindowPositionHandle = animationFrame.request(
this._saveWindowPosition,
);
}
Expand All @@ -245,7 +243,7 @@ export default class ScrollBehavior {
};

_cancelCheckWindowScroll() {
requestAnimationFrame.cancel(this._checkWindowScrollHandle);
animationFrame.cancel(this._checkWindowScrollHandle);
this._checkWindowScrollHandle = null;
}

Expand Down Expand Up @@ -359,7 +357,7 @@ export default class ScrollBehavior {
}

return new Promise((resolve) => {
this._checkWindowScrollHandle = requestAnimationFrame(() =>
this._checkWindowScrollHandle = animationFrame.request(() =>
resolve(this._checkWindowScrollPosition()),
);
});
Expand Down
8 changes: 4 additions & 4 deletions test/ScrollBehavior.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import offset from 'dom-helpers/query/offset';
import scrollLeft from 'dom-helpers/query/scrollLeft';
import scrollTop from 'dom-helpers/query/scrollTop';
import offset from 'dom-helpers/offset';
import scrollLeft from 'dom-helpers/scrollLeft';
import scrollTop from 'dom-helpers/scrollTop';
import createBrowserHistory from 'history/lib/createBrowserHistory';
import createHashHistory from 'history/lib/createHashHistory';
import PageLifecycle from 'page-lifecycle/dist/lifecycle.es5';
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('ScrollBehavior', () => {
});

describe('default behavior', () => {
it('should emulate browser scroll behavior', (done) => {
it.only('should emulate browser scroll behavior', (done) => {
const history = withRoutes(withScroll(createHistory()));
const child1 = document.getElementById('child1');
const child2 = document.getElementById('child2-id');
Expand Down
Loading

0 comments on commit cd06035

Please sign in to comment.