From 00170c8e4a9ab22efd303bd4132483711a6c83e8 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Mon, 30 Dec 2024 02:13:30 +0100 Subject: [PATCH] Properly calculate auto scroll px amount "scrollBy" and "scrollTo" require a change of at least 1px, otherwise, nothing will happen --- src/modules/core/hooks/useAutomaticScrolling.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/modules/core/hooks/useAutomaticScrolling.ts b/src/modules/core/hooks/useAutomaticScrolling.ts index 94880e12..411cffe1 100644 --- a/src/modules/core/hooks/useAutomaticScrolling.ts +++ b/src/modules/core/hooks/useAutomaticScrolling.ts @@ -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 =>