Skip to content

Commit

Permalink
fix: handle scrollable action destroy lifecycle + fix touch delta math
Browse files Browse the repository at this point in the history
  • Loading branch information
6eDesign committed Sep 17, 2021
1 parent b10331a commit 0b07ad3
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/lib/directives/scrollable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ export default (node, { y: yi = 0, step = scrollStep, maxSteps = Infinity }) =>
const updateY = (val) => {
y = Math.max(0, Math.min(maxSteps * step, val));
};
const wheel = ({ deltaY }) => updateY(y + deltaY);
const touchstart = ({ touches: [{ pageY }] }) => (lastTouch = pageY);
const touchmove = ({ touches: [{ pageY }] }) => {
updateY(y - pageY - lastTouch);
lastTouch = pageY;
};

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

node.addEventListener('wheel', (evt) => {
wheel(evt);
const wheelListener = ({ deltaY }) => {
updateY(y + deltaY);
emitY();
});
node.addEventListener('touchstart', (evt) => {
touchstart(evt);
};
const touchstartListener = ({ touches: [{ pageY }] }) => {
lastTouch = pageY;
emitY();
});
node.addEventListener('touchmove', (evt) => {
touchmove(evt);
};
const touchmoveListener = ({ touches: [{ pageY }] }) => {
updateY(y - (pageY - lastTouch));
lastTouch = pageY;
emitY();
});
};

node.addEventListener('wheel', wheelListener);
node.addEventListener('touchstart', touchstartListener);
node.addEventListener('touchmove', touchmoveListener);

return {
destroy() {
console.log('cleaning up');
node.removeEventListener('wheel', wheelListener);
node.removeEventListener('touchstart', touchstartListener);
node.removeEventListener('touchmove', touchmoveListener);
}
};
};

0 comments on commit 0b07ad3

Please sign in to comment.