-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuseDetectScroll.ts
More file actions
163 lines (142 loc) · 5.1 KB
/
useDetectScroll.ts
File metadata and controls
163 lines (142 loc) · 5.1 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import {useCallback, useEffect, useRef, useState} from 'react'
import {
Axis,
Direction,
ScrollInfo,
ScrollPosition,
ScrollProps,
} from './useDetectScroll.types'
/**
* useDetectScroll hook.
*
* This hook provides a mechanism to detect the scroll direction and position.
* It will return the scroll direction as a string (up, down, left, right, or still) based on user scrolling,
* as well as the scroll position from the top, bottom, left, and right edges of the page.
*
* @example
*
* import useDetectScroll, { Axis, Direction } from '@smakss/react-scroll-direction';
*
* function App() {
* const customElementRef = useRef<HTMLDivElement>(null);
* const [customElement, setCustomElement] = useState<HTMLDivElement>();
*
* const { scrollDir, scrollPosition } = useDetectScroll({
* target: customElement,
* thr: 100,
* axis: Axis.Y,
* scrollUp: Direction.Up,
* scrollDown: Direction.Down,
* still: Direction.Still
* });
*
* useEffect(() => {
* if (customElementRef.current) {
* setCustomElement(customElementRef.current);
* }
* }, [customElementRef]);
*
* return (
* <div>
* <p>Current scroll direction: {scrollDir}</p>
* <p>Scroll position - Top: {scrollPosition.top}, Bottom: {scrollPosition.bottom},
* Left: {scrollPosition.left}, Right: {scrollPosition.right}</p>
* </div>
* );
* }
*
* @param {ScrollProps} props - The properties related to scrolling.
* @returns {ScrollInfo} - The current direction and position of scrolling.
*/
function useDetectScroll(props: ScrollProps = {}): ScrollInfo {
const {
target = typeof window !== 'undefined' ? window : undefined,
thr = 0,
axis = Axis.Y,
scrollUp = axis === Axis.Y ? Direction.Up : Direction.Left,
scrollDown = axis === Axis.Y ? Direction.Down : Direction.Right,
still = Direction.Still,
} = props
const [scrollDir, setScrollDir] = useState<Direction>(still)
const [scrollPosition, setScrollPosition] = useState<ScrollPosition>({
top: 0,
bottom: 0,
left: 0,
right: 0,
})
const threshold = Math.max(0, thr)
const ticking = useRef(false)
const lastScroll = useRef(0)
/** Function to update scroll direction */
const updateScrollDir = useCallback(() => {
if (!target) return
let scroll: number
if (target instanceof Window) {
scroll = axis === Axis.Y ? target.scrollY : target.scrollX
} else {
scroll = axis === Axis.Y ? target.scrollTop : target.scrollLeft
}
if (Math.abs(scroll - lastScroll.current) >= threshold) {
setScrollDir(scroll > lastScroll.current ? scrollDown : scrollUp)
lastScroll.current = Math.max(0, scroll)
}
ticking.current = false
}, [target, axis, threshold, scrollDown, scrollUp])
useEffect(() => {
if (!target) {
console.warn(
'useDetectScroll: target is not set. Falling back to window.',
)
return
}
/** Function to update scroll position */
const updateScrollPosition = () => {
if (!target) return
const top =
target instanceof Window ? target.scrollY : target.scrollTop
const left =
target instanceof Window ? target.scrollX : target.scrollLeft
const bottom =
(target instanceof Window
? document.documentElement.scrollHeight - target.innerHeight
: target.scrollHeight - target.clientHeight) - top
const right =
(target instanceof Window
? document.documentElement.scrollWidth - target.innerWidth
: target.scrollWidth - target.clientWidth) - left
setScrollPosition({top, bottom, left, right})
}
updateScrollPosition()
const targetElement = target as EventTarget
targetElement.addEventListener('scroll', updateScrollPosition)
return () => {
targetElement.removeEventListener('scroll', updateScrollPosition)
}
}, [target])
useEffect(() => {
if (!target) {
console.warn(
'useDetectScroll: target is not set. Falling back to window.',
)
return
}
if (target instanceof Window) {
lastScroll.current =
axis === Axis.Y ? target.scrollY : target.scrollX
} else {
lastScroll.current =
axis === Axis.Y ? target.scrollTop : target.scrollLeft
}
const onScroll = () => {
if (!ticking.current) {
window.requestAnimationFrame(updateScrollDir)
ticking.current = true
}
}
const targetElement = target as EventTarget
targetElement.addEventListener('scroll', onScroll)
return () => targetElement.removeEventListener('scroll', onScroll)
}, [target, axis, updateScrollDir])
return {scrollDir, scrollPosition}
}
export default useDetectScroll