File size: 1,939 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { raf } from '@react-spring/rafz'
import { useRef, useState } from 'react'
import { useIsomorphicLayoutEffect } from './useIsomorphicEffect'

type SCROLL_UP = 'up'
type SCROLL_DOWN = 'down'
export type SCROLL_DIR = SCROLL_DOWN | SCROLL_UP

type UseWindowScrolling = (args: {
  threshold?: number | [down: number, up: number]
  active?: boolean
  yOffset?: number
  onScroll?: (e: Event) => void
}) => [direction: SCROLL_DIR | undefined, scrollTop: number]

export const useWindowScrolling: UseWindowScrolling = ({
  active = true,
  threshold = 0,
  yOffset = 0,
  onScroll,
}) => {
  const [direction, setDirection] = useState<SCROLL_DIR | undefined>(undefined)
  const [scrollTop, setScrollTop] = useState(0)

  const lastScrollY = useRef(0)
  const ticking = useRef(false)

  useIsomorphicLayoutEffect(() => {
    const updateScrollDir = () => {
      const scrollY = window.pageYOffset
      const direction = scrollY > lastScrollY.current ? 'down' : 'up'

      const thresholdValue = Array.isArray(threshold)
        ? threshold[direction === 'down' ? 0 : 1]
        : threshold

      if (!active) {
        setDirection(undefined)
        return
      }

      if (scrollY < yOffset) {
        ticking.current = false
        return
      }

      if (Math.abs(scrollY - lastScrollY.current) < thresholdValue) {
        ticking.current = false
        return
      }

      lastScrollY.current = scrollY > 0 ? scrollY : 0
      ticking.current = false

      setDirection(direction)
      setScrollTop(scrollY)
    }

    const handleScroll = (e?: Event) => {
      if (!ticking.current) {
        raf(updateScrollDir)
        ticking.current = true
      } else if (onScroll && e) {
        onScroll(e)
      }
    }

    updateScrollDir()

    window.addEventListener('scroll', handleScroll)

    return () => window.removeEventListener('scroll', handleScroll)
  }, [active, threshold])

  return [direction, scrollTop]
}