Fix scroll direction detection for Safari rubber-banding

This commit is contained in:
Sam Becker 2025-04-03 09:33:53 -05:00
parent e5b8f29d59
commit c959e88ebb

View File

@ -1,14 +1,18 @@
import { useEffect, useState, useRef } from 'react';
export default function useScrollDirection() {
const [scrollDirection, setScrollDirection] =
useState<'up' | 'down'>('down');
const [scrollDirection, setScrollDirection] = useState<'up' | 'down'>('down');
const lastScrollY = useRef(0);
useEffect(() => {
const handleScroll = () => {
setScrollDirection(window.scrollY > lastScrollY.current ? 'down' : 'up');
const pageHeight =
document.documentElement.scrollHeight - window.innerHeight;
setScrollDirection((
window.scrollY >= lastScrollY.current ||
lastScrollY.current > pageHeight
) ? 'down' : 'up');
lastScrollY.current = window.scrollY;
};
window.addEventListener('scroll', handleScroll);