Skip to content

Commit 7c4f81f

Browse files
committed
fix: detect vertical scroll on touchend
1 parent fe673eb commit 7c4f81f

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/store/ChartAdapterStore.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ export default class ChartAdapterStore {
4141
bottomIndex: 0,
4242
};
4343
touchValues: {
44+
deltaXTotal?: number;
4445
deltaYTotal?: number;
4546
x?: number;
4647
y?: number;
4748
} = {
49+
deltaXTotal: 0,
4850
deltaYTotal: 0,
4951
x: 0,
5052
y: 0,
@@ -256,15 +258,22 @@ export default class ChartAdapterStore {
256258
const { left } = chartNode.getBoundingClientRect();
257259

258260
if (this.touchValues.x && this.touchValues.y) {
259-
const deltaX = Math.abs(pageX - this.touchValues.x);
260-
const deltaY = Math.abs(pageY - this.touchValues.y);
261+
this.touchValues.deltaXTotal = (this.touchValues.deltaXTotal ?? 0) + (this.touchValues.x - pageX);
261262
this.touchValues.deltaYTotal = (this.touchValues.deltaYTotal ?? 0) + (this.touchValues.y - pageY);
263+
const deltaX =
264+
e.type === 'touchend'
265+
? Math.abs(this.touchValues.deltaXTotal)
266+
: Math.abs(pageX - this.touchValues.x);
267+
const deltaY =
268+
e.type === 'touchend'
269+
? Math.abs(this.touchValues.deltaYTotal)
270+
: Math.abs(pageY - this.touchValues.y);
262271
const isVerticalScroll = deltaY > deltaX;
263272
const x = pageX - left;
264273
const isForcedScrollArea = x < nonScrollableAreaWidth;
265274
const shouldForceMaxScroll =
266275
Math.abs(Number(this.touchValues.deltaYTotal)) > 10 && e.type === 'touchend';
267-
if (isForcedScrollArea) {
276+
if (isForcedScrollArea && isVerticalScroll) {
268277
if (shouldForceMaxScroll) {
269278
clearTimeout(this.scrollChartParentOnTouchTimer);
270279
this.scrollableChartParent?.scrollTo({
@@ -275,15 +284,15 @@ export default class ChartAdapterStore {
275284
behavior: 'smooth',
276285
});
277286
this.scrollChartParentOnTouchTimer = undefined;
278-
this.touchValues = { ...this.touchValues, deltaYTotal: 0 };
279-
} else if (isVerticalScroll && !this.scrollChartParentOnTouchTimer) {
287+
this.touchValues = { ...this.touchValues, deltaYTotal: 0, deltaXTotal: 0 };
288+
} else if (!this.scrollChartParentOnTouchTimer) {
280289
this.scrollChartParentOnTouchTimer = setTimeout(() => {
281290
this.scrollableChartParent?.scrollBy({
282291
top: this.touchValues.deltaYTotal,
283292
behavior: 'smooth',
284293
});
285294
this.scrollChartParentOnTouchTimer = undefined;
286-
this.touchValues = { ...this.touchValues, deltaYTotal: 0 };
295+
this.touchValues = { ...this.touchValues, deltaYTotal: 0, deltaXTotal: 0 };
287296
}, 100);
288297
}
289298
}
@@ -292,7 +301,9 @@ export default class ChartAdapterStore {
292301
}
293302
if (['touchstart', 'touchend'].includes(e.type)) {
294303
this.touchValues =
295-
e.type === 'touchstart' ? { x: pageX, y: pageY } : { deltaYTotal: this.touchValues.deltaYTotal };
304+
e.type === 'touchstart'
305+
? { x: pageX, y: pageY }
306+
: { deltaYTotal: this.touchValues.deltaYTotal, deltaXTotal: this.touchValues.deltaXTotal };
296307
}
297308
}
298309
}

0 commit comments

Comments
 (0)