diff --git a/lib/hooks/use-scroll.ts b/lib/hooks/use-scroll.ts index 3c8014e3..e28edda0 100644 --- a/lib/hooks/use-scroll.ts +++ b/lib/hooks/use-scroll.ts @@ -1,5 +1,10 @@ import { useCallback, useEffect, useState } from "react"; +/** + * React hook to observe scroll postion. + * @param threshold {number} The y-axis position to observe. + * @returns {boolean} `True` when the user's vertical scroll position is greater than the `threshold`; otherwise, `False`. + */ export default function useScroll(threshold: number) { const [scrolled, setScrolled] = useState(false); @@ -8,6 +13,8 @@ export default function useScroll(threshold: number) { }, [threshold]); useEffect(() => { + window && onScroll(); // Call on first render. This correctly updates the `scrolled` state to `True` if the user's scroll position is greater than the `threshold` during the initial load. + window.addEventListener("scroll", onScroll); return () => window.removeEventListener("scroll", onScroll); }, [onScroll]);