Skip to content

Commit

Permalink
fix: scroll on touch improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
maryia-deriv committed Aug 9, 2024
1 parent a8bf8e3 commit 867499f
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/store/ChartAdapterStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ export default class ChartAdapterStore {
bottomIndex: 0,
};
touchValues: {
deltaYTotal?: number;
x?: number;
y?: number;
yOnTouchEnd?: number;
} = {};
} = {
deltaYTotal: 0,
x: 0,
y: 0,
yOnTouchEnd: 0,
};

isOverFlutterCharts = false;
enableVerticalScrollTimer?: ReturnType<typeof setTimeout>;
Expand Down Expand Up @@ -247,28 +253,33 @@ export default class ChartAdapterStore {
) {
const { pageX, pageY } = e.changedTouches[0];
if (['touchstart', 'touchend'].includes(e.type)) {
this.touchValues = e.type === 'touchstart' ? { x: pageX, y: pageY } : { yOnTouchEnd: pageY };
this.touchValues =
e.type === 'touchstart'
? { x: pageX, y: pageY }
: { yOnTouchEnd: pageY, deltaYTotal: this.touchValues.deltaYTotal };
} else if (e.type === 'touchmove') {
const nonScrollableAreaWidth = chartNode.offsetWidth - this.mainStore.chart.yAxisWidth;
const { left } = chartNode.getBoundingClientRect();

if (this.touchValues.x && this.touchValues.y) {
const deltaX = Math.abs(pageX - this.touchValues.x);
const deltaY = Math.abs(pageY - this.touchValues.y);
this.touchValues.deltaYTotal = (this.touchValues.deltaYTotal ?? 0) + (this.touchValues.y - pageY);
const isVerticalScroll = deltaY > deltaX;
const x = pageX - left;
if (x < nonScrollableAreaWidth && isVerticalScroll && !this.scrollChartParentOnTouchTimer) {
this.touchValues.yOnTouchEnd = undefined;
this.scrollChartParentOnTouchTimer = setTimeout(() => {
this.scrollableChartParent?.scrollBy({
top: pageY - Number(this.touchValues.yOnTouchEnd ?? this.touchValues.y),
top: this.touchValues.deltaYTotal,
behavior: 'smooth',
});
this.scrollChartParentOnTouchTimer = undefined;
this.touchValues = { ...this.touchValues, deltaYTotal: 0 };
}, 100);
}
}
this.touchValues = { x: pageX, y: pageY };
this.touchValues = { ...this.touchValues, x: pageX, y: pageY };
}
}
}
Expand Down

0 comments on commit 867499f

Please sign in to comment.