1- import { useState , useEffect , useCallback , useRef } from 'react' ;
1+ import { React , useState , useEffect , useCallback , useRef } from 'react' ;
22
33/** Enumeration for axis values */
44export enum Axis {
@@ -69,6 +69,10 @@ type ScrollInfo = {
6969
7070/** Type declaration for scroll properties */
7171type ScrollProps = {
72+ /**
73+ * The target represents the scrollable element to check for scroll detection.
74+ */
75+ target ?: React . RefObject < HTMLDivElement >
7276 /**
7377 * The thr represents the threshold value for scroll detection.
7478 */
@@ -125,6 +129,7 @@ type ScrollProps = {
125129 */
126130function useDetectScroll ( props : ScrollProps = { } ) : ScrollInfo {
127131 const {
132+ target = window ,
128133 thr = 0 ,
129134 axis = Axis . Y ,
130135 scrollUp = axis === Axis . Y ? Direction . Up : Direction . Left ,
@@ -158,23 +163,25 @@ function useDetectScroll(props: ScrollProps = {}): ScrollInfo {
158163 useEffect ( ( ) => {
159164 /** Function to update scroll position */
160165 const updateScrollPosition = ( ) => {
161- const top = window . scrollY ;
162- const left = window . scrollX ;
163- const bottom =
164- document . documentElement . scrollHeight - window . innerHeight - top ;
165- const right =
166- document . documentElement . scrollWidth - window . innerWidth - left ;
166+ const top = target === window ? target . scrollY : target . scrollTop ;
167+ const left = target === window ? target . scrollX : target . scrollLeft ;
168+ const bottom = target === window ?
169+ document . documentElement . scrollHeight - window . innerHeight - top :
170+ document . documentElement . scrollHeight - target . scrollHeight - top ;
171+ const right = target === window ?
172+ document . documentElement . scrollWidth - window . innerWidth - left :
173+ document . documentElement . scrollHeight - target . scrollWidth - left ;
167174
168175 setScrollPosition ( { top, bottom, left, right } ) ;
169176 } ;
170177
171178 /** Call the update function when the component mounts */
172179 updateScrollPosition ( ) ;
173180
174- window . addEventListener ( 'scroll' , updateScrollPosition ) ;
181+ target === window ? window . addEventListener ( 'scroll' , updateScrollPosition ) : target . addEventListener ( 'scroll' , updateScrollPosition ) ;
175182
176183 return ( ) => {
177- window . removeEventListener ( 'scroll' , updateScrollPosition ) ;
184+ target === window ? window . removeEventListener ( 'scroll' , updateScrollPosition ) : target . removeEventListener ( 'scroll' , updateScrollPosition ) ;
178185 } ;
179186 } , [ ] ) ;
180187
@@ -184,14 +191,14 @@ function useDetectScroll(props: ScrollProps = {}): ScrollInfo {
184191 /** Function to handle onScroll event */
185192 const onScroll = ( ) => {
186193 if ( ! ticking . current ) {
187- window . requestAnimationFrame ( updateScrollDir ) ;
194+ target === window ? window . requestAnimationFrame ( updateScrollDir ) : target . requestAnimationFrame ( updateScrollDir ) ;
188195 ticking . current = true ;
189196 }
190197 } ;
191198
192- window . addEventListener ( 'scroll' , onScroll ) ;
199+ target === window ? window . addEventListener ( 'scroll' , onScroll ) : target . addEventListener ( 'scroll' , onScroll ) ;
193200
194- return ( ) => window . removeEventListener ( 'scroll' , onScroll ) ;
201+ return ( ) => target === window ? window . removeEventListener ( 'scroll' , onScroll ) : target . removeEventListener ( 'scroll' , onScroll ) ;
195202 } , [ axis , updateScrollDir ] ) ;
196203
197204 return { scrollDir, scrollPosition } ;
0 commit comments