From 3b3ec6df3e13494fbd5eddf823c0ead8915dc2d6 Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Fri, 22 Nov 2024 16:44:53 +0100 Subject: [PATCH] fix: better autoscroll --- src/components/LiveLeaderboard.tsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/components/LiveLeaderboard.tsx b/src/components/LiveLeaderboard.tsx index 50deb6a..f954d5d 100644 --- a/src/components/LiveLeaderboard.tsx +++ b/src/components/LiveLeaderboard.tsx @@ -55,13 +55,15 @@ export const LiveLeaderboard = ({ roomId }: Props) => { const timerRef = useRef(null) useEffect(() => { + const PIXELS_PER_MILLISECOND = [1, 1000 / 30] // e.g. [1, 10] means 1px every 10ms + if (autoscroll !== false) { timerRef.current = setInterval(() => { console.log('scrolling') window.scrollBy({ - top: autoscroll === 'down' ? 10 : -10, - behavior: 'smooth', + top: autoscroll === 'down' ? PIXELS_PER_MILLISECOND[0] : -PIXELS_PER_MILLISECOND[0], + behavior: 'instant', }) if (window.scrollY + window.innerHeight + 1 > document.body.scrollHeight) { @@ -71,7 +73,7 @@ export const LiveLeaderboard = ({ roomId }: Props) => { if (window.scrollY === 0) { setAutoscroll('down') } - }, 100) + }, PIXELS_PER_MILLISECOND[1]) return () => { if (timerRef.current) {