Skip to content

Commit 867499f

Browse files
committed
fix: scroll on touch improvement
1 parent a8bf8e3 commit 867499f

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/store/ChartAdapterStore.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,16 @@ export default class ChartAdapterStore {
4141
bottomIndex: 0,
4242
};
4343
touchValues: {
44+
deltaYTotal?: number;
4445
x?: number;
4546
y?: number;
4647
yOnTouchEnd?: number;
47-
} = {};
48+
} = {
49+
deltaYTotal: 0,
50+
x: 0,
51+
y: 0,
52+
yOnTouchEnd: 0,
53+
};
4854

4955
isOverFlutterCharts = false;
5056
enableVerticalScrollTimer?: ReturnType<typeof setTimeout>;
@@ -247,28 +253,33 @@ export default class ChartAdapterStore {
247253
) {
248254
const { pageX, pageY } = e.changedTouches[0];
249255
if (['touchstart', 'touchend'].includes(e.type)) {
250-
this.touchValues = e.type === 'touchstart' ? { x: pageX, y: pageY } : { yOnTouchEnd: pageY };
256+
this.touchValues =
257+
e.type === 'touchstart'
258+
? { x: pageX, y: pageY }
259+
: { yOnTouchEnd: pageY, deltaYTotal: this.touchValues.deltaYTotal };
251260
} else if (e.type === 'touchmove') {
252261
const nonScrollableAreaWidth = chartNode.offsetWidth - this.mainStore.chart.yAxisWidth;
253262
const { left } = chartNode.getBoundingClientRect();
254263

255264
if (this.touchValues.x && this.touchValues.y) {
256265
const deltaX = Math.abs(pageX - this.touchValues.x);
257266
const deltaY = Math.abs(pageY - this.touchValues.y);
267+
this.touchValues.deltaYTotal = (this.touchValues.deltaYTotal ?? 0) + (this.touchValues.y - pageY);
258268
const isVerticalScroll = deltaY > deltaX;
259269
const x = pageX - left;
260270
if (x < nonScrollableAreaWidth && isVerticalScroll && !this.scrollChartParentOnTouchTimer) {
261271
this.touchValues.yOnTouchEnd = undefined;
262272
this.scrollChartParentOnTouchTimer = setTimeout(() => {
263273
this.scrollableChartParent?.scrollBy({
264-
top: pageY - Number(this.touchValues.yOnTouchEnd ?? this.touchValues.y),
274+
top: this.touchValues.deltaYTotal,
265275
behavior: 'smooth',
266276
});
267277
this.scrollChartParentOnTouchTimer = undefined;
278+
this.touchValues = { ...this.touchValues, deltaYTotal: 0 };
268279
}, 100);
269280
}
270281
}
271-
this.touchValues = { x: pageX, y: pageY };
282+
this.touchValues = { ...this.touchValues, x: pageX, y: pageY };
272283
}
273284
}
274285
}

0 commit comments

Comments
 (0)