From 51ba7aed3b3359b73e9af1b6c86053a1e555ca33 Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Thu, 3 Apr 2025 23:41:51 -0500 Subject: [PATCH] Only change direction when scrollY has changed --- src/utility/useScrollDirection.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/utility/useScrollDirection.ts b/src/utility/useScrollDirection.ts index 6c42f001..cc98bdef 100644 --- a/src/utility/useScrollDirection.ts +++ b/src/utility/useScrollDirection.ts @@ -8,17 +8,24 @@ export default function useScrollDirection() { useEffect(() => { const handleScroll = () => { + const scrollY = window.scrollY; const pageHeight = ( document.documentElement.scrollHeight - window.innerHeight ); - setScrollInfo(prev => ({ - scrollDirection: ( - window.scrollY >= prev.scrollY || - prev.scrollY > pageHeight - ) ? 'down' : 'up', - scrollY: window.scrollY, - })); + setScrollInfo(prev => { + let scrollDirection = prev.scrollDirection; + if (scrollY !== prev.scrollY) { + scrollDirection = ( + scrollY > prev.scrollY || + prev.scrollY > pageHeight + ) ? 'down' : 'up'; + } + return { + scrollDirection, + scrollY, + }; + }); }; window.addEventListener('scroll', handleScroll); return () => window.removeEventListener('scroll', handleScroll);