Skip to content

Commit fdc2b37

Browse files
committed
feat: forcing max scroll feature
1 parent 867499f commit fdc2b37

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

src/store/ChartAdapterStore.ts

+28-15
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,10 @@ export default class ChartAdapterStore {
4444
deltaYTotal?: number;
4545
x?: number;
4646
y?: number;
47-
yOnTouchEnd?: number;
4847
} = {
4948
deltaYTotal: 0,
5049
x: 0,
5150
y: 0,
52-
yOnTouchEnd: 0,
5351
};
5452

5553
isOverFlutterCharts = false;
@@ -243,21 +241,17 @@ export default class ChartAdapterStore {
243241
}
244242

245243
onTouch(e: TouchEvent) {
246-
const chartNode = this.mainStore.chart.chartNode;
247244
// Prevent vertical scroll on the chart for touch devices by forcing scroll on a scrollable parent of the chart:
245+
const chartNode = this.mainStore.chart.chartNode;
248246
if (
249247
chartNode &&
250248
this.scrollableChartParent &&
251249
!this.mainStore.state.isVerticalScrollEnabled &&
252250
e.changedTouches.length === 1
253251
) {
254252
const { pageX, pageY } = e.changedTouches[0];
255-
if (['touchstart', 'touchend'].includes(e.type)) {
256-
this.touchValues =
257-
e.type === 'touchstart'
258-
? { x: pageX, y: pageY }
259-
: { yOnTouchEnd: pageY, deltaYTotal: this.touchValues.deltaYTotal };
260-
} else if (e.type === 'touchmove') {
253+
254+
if (['touchmove', 'touchend'].includes(e.type)) {
261255
const nonScrollableAreaWidth = chartNode.offsetWidth - this.mainStore.chart.yAxisWidth;
262256
const { left } = chartNode.getBoundingClientRect();
263257

@@ -267,20 +261,39 @@ export default class ChartAdapterStore {
267261
this.touchValues.deltaYTotal = (this.touchValues.deltaYTotal ?? 0) + (this.touchValues.y - pageY);
268262
const isVerticalScroll = deltaY > deltaX;
269263
const x = pageX - left;
270-
if (x < nonScrollableAreaWidth && isVerticalScroll && !this.scrollChartParentOnTouchTimer) {
271-
this.touchValues.yOnTouchEnd = undefined;
272-
this.scrollChartParentOnTouchTimer = setTimeout(() => {
273-
this.scrollableChartParent?.scrollBy({
274-
top: this.touchValues.deltaYTotal,
264+
const isForcedScrollArea = x < nonScrollableAreaWidth;
265+
const shouldForceMaxScroll =
266+
Math.abs(Number(this.touchValues.deltaYTotal)) > 10 && e.type === 'touchend';
267+
if (isForcedScrollArea) {
268+
if (shouldForceMaxScroll) {
269+
clearTimeout(this.scrollChartParentOnTouchTimer);
270+
this.scrollableChartParent?.scrollTo({
271+
top:
272+
Number(this.touchValues.deltaYTotal) < 0
273+
? 0
274+
: this.scrollableChartParent.scrollHeight,
275275
behavior: 'smooth',
276276
});
277277
this.scrollChartParentOnTouchTimer = undefined;
278278
this.touchValues = { ...this.touchValues, deltaYTotal: 0 };
279-
}, 100);
279+
} else if (isVerticalScroll && !this.scrollChartParentOnTouchTimer) {
280+
this.scrollChartParentOnTouchTimer = setTimeout(() => {
281+
this.scrollableChartParent?.scrollBy({
282+
top: this.touchValues.deltaYTotal,
283+
behavior: 'smooth',
284+
});
285+
this.scrollChartParentOnTouchTimer = undefined;
286+
this.touchValues = { ...this.touchValues, deltaYTotal: 0 };
287+
}, 100);
288+
}
280289
}
281290
}
282291
this.touchValues = { ...this.touchValues, x: pageX, y: pageY };
283292
}
293+
if (['touchstart', 'touchend'].includes(e.type)) {
294+
this.touchValues =
295+
e.type === 'touchstart' ? { x: pageX, y: pageY } : { deltaYTotal: this.touchValues.deltaYTotal };
296+
}
284297
}
285298
}
286299

0 commit comments

Comments
 (0)