Skip to content

Commit 0b07ad3

Browse files
committed
fix: handle scrollable action destroy lifecycle + fix touch delta math
1 parent b10331a commit 0b07ad3

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/lib/directives/scrollable.js

+23-15
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ export default (node, { y: yi = 0, step = scrollStep, maxSteps = Infinity }) =>
77
const updateY = (val) => {
88
y = Math.max(0, Math.min(maxSteps * step, val));
99
};
10-
const wheel = ({ deltaY }) => updateY(y + deltaY);
11-
const touchstart = ({ touches: [{ pageY }] }) => (lastTouch = pageY);
12-
const touchmove = ({ touches: [{ pageY }] }) => {
13-
updateY(y - pageY - lastTouch);
14-
lastTouch = pageY;
15-
};
1610

1711
const emitY = () => {
1812
if (Math.round(y / step) === Math.round(yi / step)) return;
@@ -27,16 +21,30 @@ export default (node, { y: yi = 0, step = scrollStep, maxSteps = Infinity }) =>
2721
);
2822
};
2923

30-
node.addEventListener('wheel', (evt) => {
31-
wheel(evt);
24+
const wheelListener = ({ deltaY }) => {
25+
updateY(y + deltaY);
3226
emitY();
33-
});
34-
node.addEventListener('touchstart', (evt) => {
35-
touchstart(evt);
27+
};
28+
const touchstartListener = ({ touches: [{ pageY }] }) => {
29+
lastTouch = pageY;
3630
emitY();
37-
});
38-
node.addEventListener('touchmove', (evt) => {
39-
touchmove(evt);
31+
};
32+
const touchmoveListener = ({ touches: [{ pageY }] }) => {
33+
updateY(y - (pageY - lastTouch));
34+
lastTouch = pageY;
4035
emitY();
41-
});
36+
};
37+
38+
node.addEventListener('wheel', wheelListener);
39+
node.addEventListener('touchstart', touchstartListener);
40+
node.addEventListener('touchmove', touchmoveListener);
41+
42+
return {
43+
destroy() {
44+
console.log('cleaning up');
45+
node.removeEventListener('wheel', wheelListener);
46+
node.removeEventListener('touchstart', touchstartListener);
47+
node.removeEventListener('touchmove', touchmoveListener);
48+
}
49+
};
4250
};

0 commit comments

Comments
 (0)