Properly calculate auto scroll px amount

"scrollBy" and "scrollTo" require a change of at least 1px, otherwise, nothing will happen
This commit is contained in:
schroda
2024-12-30 02:13:30 +01:00
parent ce49084a89
commit 00170c8e4a

View File

@@ -10,23 +10,19 @@ import { MutableRefObject, useCallback, useEffect, useMemo, useRef, useState } f
import { ScrollDirection } from '@/modules/core/Core.types.ts';
import { useResizeObserver } from '@/modules/core/hooks/useResizeObserver.tsx';
// "scrollBy" and "scrollTo" both require at least a change of 1px, otherwise, nothing happens
const MIN_SCROLL_AMOUNT_PX = 1.5;
const getScrollAmount = (
amountPerMs: number,
speedMs: number,
isRTL: boolean = false,
invert: boolean = false,
): { amountPx: number; speedMs: number } => {
const amountPx = amountPerMs * speedMs;
): number => {
const pxPerMs = Math.max(amountPerMs * speedMs, MIN_SCROLL_AMOUNT_PX);
const pxPerMsReadingMode = isRTL ? -pxPerMs : pxPerMs;
const pxPerMsInverted = invert ? pxPerMsReadingMode * -1 : pxPerMsReadingMode;
if (amountPx >= MIN_SCROLL_AMOUNT_PX) {
const pxPerMsReadingMode = isRTL ? -amountPx : amountPx;
const pxPerMsInverted = invert ? pxPerMsReadingMode * -1 : pxPerMsReadingMode;
return { amountPx: pxPerMsInverted, speedMs };
}
return getScrollAmount(amountPerMs, speedMs + 1);
return pxPerMsInverted;
};
const getPxPerMs = (size: number, scrollAmountPercentage: number, scrollSpeedMs: number): number =>