1- import React , { useState , useEffect , useCallback , useRef } from 'react' ;
1+ import { useState , useEffect , useCallback , useRef } from 'react' ;
22
33/** Enumeration for axis values */
44export enum Axis {
@@ -72,7 +72,7 @@ type ScrollProps = {
7272 /**
7373 * The target represents the scrollable element to check for scroll detection.
7474 */
75- target ?: HTMLDivElement
75+ target ?: HTMLDivElement ;
7676 /**
7777 * The thr represents the threshold value for scroll detection.
7878 */
@@ -151,10 +151,14 @@ function useDetectScroll(props: ScrollProps = {}): ScrollInfo {
151151
152152 /** Function to update scroll direction */
153153 const updateScrollDir = useCallback ( ( ) => {
154- const scroll =
155- target instanceof Window ?
156- ( axis === Axis . Y ? window . scrollY : window . scrollX ) :
157- ( axis === Axis . Y ? target . scrollTop : target . scrollLeft ) ;
154+ const scroll =
155+ target instanceof Window
156+ ? axis === Axis . Y
157+ ? window . scrollY
158+ : window . scrollX
159+ : axis === Axis . Y
160+ ? target . scrollTop
161+ : target . scrollLeft ;
158162
159163 if ( Math . abs ( scroll - lastScroll . current ) >= threshold ) {
160164 setScrollDir ( scroll > lastScroll . current ? scrollDown : scrollUp ) ;
@@ -167,32 +171,43 @@ function useDetectScroll(props: ScrollProps = {}): ScrollInfo {
167171 /** Function to update scroll position */
168172 const updateScrollPosition = ( ) => {
169173 const top = target instanceof Window ? target . scrollY : target . scrollTop ;
170- const left = target instanceof Window ? target . scrollX : target . scrollLeft ;
171- const bottom = target instanceof Window ?
172- document . documentElement . scrollHeight - window . innerHeight - top :
173- document . documentElement . scrollHeight - target . scrollHeight - top ;
174- const right = target instanceof Window ?
175- document . documentElement . scrollWidth - window . innerWidth - left :
176- document . documentElement . scrollHeight - target . scrollWidth - left ;
174+ const left =
175+ target instanceof Window ? target . scrollX : target . scrollLeft ;
176+ const bottom =
177+ target instanceof Window
178+ ? document . documentElement . scrollHeight - window . innerHeight - top
179+ : document . documentElement . scrollHeight - target . scrollHeight - top ;
180+ const right =
181+ target instanceof Window
182+ ? document . documentElement . scrollWidth - window . innerWidth - left
183+ : document . documentElement . scrollHeight - target . scrollWidth - left ;
177184
178185 setScrollPosition ( { top, bottom, left, right } ) ;
179186 } ;
180187
181188 /** Call the update function when the component mounts */
182189 updateScrollPosition ( ) ;
183190
184- target instanceof Window ? window . addEventListener ( 'scroll' , updateScrollPosition ) : target . addEventListener ( 'scroll' , updateScrollPosition ) ;
191+ target instanceof Window
192+ ? window . addEventListener ( 'scroll' , updateScrollPosition )
193+ : target . addEventListener ( 'scroll' , updateScrollPosition ) ;
185194
186195 return ( ) => {
187- target instanceof Window ? window . removeEventListener ( 'scroll' , updateScrollPosition ) : target . removeEventListener ( 'scroll' , updateScrollPosition ) ;
196+ target instanceof Window
197+ ? window . removeEventListener ( 'scroll' , updateScrollPosition )
198+ : target . removeEventListener ( 'scroll' , updateScrollPosition ) ;
188199 } ;
189200 } , [ target ] ) ;
190201
191202 useEffect ( ( ) => {
192- lastScroll . current =
193- target instanceof Window ?
194- ( axis === Axis . Y ? window . scrollY : window . scrollX ) :
195- ( axis === Axis . Y ? target . scrollTop : target . scrollLeft ) ;
203+ lastScroll . current =
204+ target instanceof Window
205+ ? axis === Axis . Y
206+ ? window . scrollY
207+ : window . scrollX
208+ : axis === Axis . Y
209+ ? target . scrollTop
210+ : target . scrollLeft ;
196211
197212 /** Function to handle onScroll event */
198213 const onScroll = ( ) => {
@@ -202,9 +217,14 @@ function useDetectScroll(props: ScrollProps = {}): ScrollInfo {
202217 }
203218 } ;
204219
205- target instanceof Window ? window . addEventListener ( 'scroll' , onScroll ) : target . addEventListener ( 'scroll' , onScroll ) ;
220+ target instanceof Window
221+ ? window . addEventListener ( 'scroll' , onScroll )
222+ : target . addEventListener ( 'scroll' , onScroll ) ;
206223
207- return ( ) => target instanceof Window ? window . removeEventListener ( 'scroll' , onScroll ) : target . removeEventListener ( 'scroll' , onScroll ) ;
224+ return ( ) =>
225+ target instanceof Window
226+ ? window . removeEventListener ( 'scroll' , onScroll )
227+ : target . removeEventListener ( 'scroll' , onScroll ) ;
208228 } , [ target , axis , updateScrollDir ] ) ;
209229
210230 return { scrollDir, scrollPosition } ;
0 commit comments