Skip to content

Commit 842ed67

Browse files
CXBoyySMAKSS
authored andcommitted
Fix minor misses where target was not properly added to calculation
1 parent b14d842 commit 842ed67

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function useDetectScroll(props: ScrollProps = {}): ScrollInfo {
136136
scrollDown = axis === Axis.Y ? Direction.Down : Direction.Right,
137137
still = Direction.Still
138138
} = props;
139-
139+
140140
const [scrollDir, setScrollDir] = useState<Direction>(still);
141141
const [scrollPosition, setScrollPosition] = useState<ScrollPosition>({
142142
top: 0,
@@ -151,14 +151,17 @@ function useDetectScroll(props: ScrollProps = {}): ScrollInfo {
151151

152152
/** Function to update scroll direction */
153153
const updateScrollDir = useCallback(() => {
154-
const scroll = axis === Axis.Y ? window.scrollY : window.scrollX;
154+
const scroll =
155+
target instanceof Window ?
156+
(axis === Axis.Y ? window.scrollY : window.scrollX) :
157+
(axis === Axis.Y ? target.scrollTop : target.scrollLeft);
155158

156159
if (Math.abs(scroll - lastScroll.current) >= threshold) {
157160
setScrollDir(scroll > lastScroll.current ? scrollDown : scrollUp);
158161
lastScroll.current = Math.max(0, scroll);
159162
}
160163
ticking.current = false;
161-
}, [axis, threshold, scrollDown, scrollUp]);
164+
}, [target, axis, threshold, scrollDown, scrollUp]);
162165

163166
useEffect(() => {
164167
/** Function to update scroll position */
@@ -178,15 +181,18 @@ function useDetectScroll(props: ScrollProps = {}): ScrollInfo {
178181
/** Call the update function when the component mounts */
179182
updateScrollPosition();
180183

181-
target === window ? window.addEventListener('scroll', updateScrollPosition) : target.addEventListener('scroll', updateScrollPosition);
184+
target instanceof Window ? window.addEventListener('scroll', updateScrollPosition) : target.addEventListener('scroll', updateScrollPosition);
182185

183186
return () => {
184-
target === window ? window.removeEventListener('scroll', updateScrollPosition) : target.removeEventListener('scroll', updateScrollPosition);
187+
target instanceof Window ? window.removeEventListener('scroll', updateScrollPosition) : target.removeEventListener('scroll', updateScrollPosition);
185188
};
186189
}, [target]);
187190

188191
useEffect(() => {
189-
lastScroll.current = axis === Axis.Y ? window.scrollY : window.scrollX;
192+
lastScroll.current =
193+
target instanceof Window ?
194+
(axis === Axis.Y ? window.scrollY : window.scrollX) :
195+
(axis === Axis.Y ? target.scrollTop : target.scrollLeft);
190196

191197
/** Function to handle onScroll event */
192198
const onScroll = () => {
@@ -196,9 +202,9 @@ function useDetectScroll(props: ScrollProps = {}): ScrollInfo {
196202
}
197203
};
198204

199-
target === window ? window.addEventListener('scroll', onScroll) : target.addEventListener('scroll', onScroll);
205+
target instanceof Window ? window.addEventListener('scroll', onScroll) : target.addEventListener('scroll', onScroll);
200206

201-
return () => target === window ? window.removeEventListener('scroll', onScroll) : target.removeEventListener('scroll', onScroll);
207+
return () => target instanceof Window ? window.removeEventListener('scroll', onScroll) : target.removeEventListener('scroll', onScroll);
202208
}, [target, axis, updateScrollDir]);
203209

204210
return { scrollDir, scrollPosition };

0 commit comments

Comments
 (0)