@@ -2,43 +2,6 @@ import { useEffect } from 'react';
22
33import { findResizingParent , findScrollingParent } from './utils' ;
44
5- // Helper function to find all scrolling parents
6- const findAllScrollingParents = ( element : HTMLElement ) : HTMLElement [ ] => {
7- const scrollingParents : HTMLElement [ ] = [ ] ;
8- let currentElement = element . parentElement ;
9-
10- while ( currentElement && currentElement !== document . body ) {
11- const { overflow, overflowY, overflowX } = getComputedStyle ( currentElement ) ;
12- if (
13- [ overflow , overflowY , overflowX ] . some ( ( val ) =>
14- [ 'scroll' , 'auto' ] . includes ( val )
15- )
16- ) {
17- scrollingParents . push ( currentElement ) ;
18- }
19- currentElement = currentElement . parentElement ;
20- }
21-
22- // Always include window for document-level scrolling
23- if (
24- scrollingParents . length === 0 ||
25- ! scrollingParents . includes ( document . documentElement )
26- ) {
27- scrollingParents . push ( document . documentElement ) ;
28- }
29-
30- return scrollingParents ;
31- } ;
32-
33- // Debounce utility
34- const createDebounce = ( func : ( ) => void , delay : number ) => {
35- let timeoutId : ReturnType < typeof setTimeout > ;
36- return ( ) => {
37- clearTimeout ( timeoutId ) ;
38- timeoutId = setTimeout ( func , delay ) ;
39- } ;
40- } ;
41-
425export const useScrollingParentEffect = (
436 targetRef : React . RefObject <
447 Pick < HTMLDivElement , 'getBoundingClientRect' | 'contains' >
@@ -49,42 +12,17 @@ export const useScrollingParentEffect = (
4912 if ( ! targetRef . current ) {
5013 return ;
5114 }
52-
53- const target = targetRef . current as unknown as HTMLElement ;
54- const scrollingParents = findAllScrollingParents ( target ) ;
55-
56- const updatePosition = ( ) => {
15+ const scrollingParent = findScrollingParent (
16+ targetRef . current as unknown as HTMLElement
17+ ) ;
18+ if ( ! scrollingParent ?. addEventListener ) {
19+ return ;
20+ }
21+ const handler = ( ) => {
5722 setTargetRect ( targetRef ?. current ?. getBoundingClientRect ( ) ) ;
5823 } ;
59-
60- // Debounced version for performance during rapid scrolling
61- const debouncedUpdate = createDebounce ( updatePosition , 10 ) ;
62-
63- // For immediate updates during scroll
64- const immediateUpdate = ( ) => {
65- updatePosition ( ) ;
66- } ;
67-
68- const cleanup : ( ( ) => void ) [ ] = [ ] ;
69-
70- // Add listeners to all scrolling parents
71- scrollingParents . forEach ( ( parent ) => {
72- if ( parent . addEventListener ) {
73- // Use immediate update for smoother experience
74- parent . addEventListener ( 'scroll' , immediateUpdate , { passive : true } ) ;
75- cleanup . push ( ( ) =>
76- parent . removeEventListener ( 'scroll' , immediateUpdate )
77- ) ;
78- }
79- } ) ;
80-
81- // Also listen to window scroll as a fallback
82- window . addEventListener ( 'scroll' , immediateUpdate , { passive : true } ) ;
83- cleanup . push ( ( ) => window . removeEventListener ( 'scroll' , immediateUpdate ) ) ;
84-
85- return ( ) => {
86- cleanup . forEach ( ( fn ) => fn ( ) ) ;
87- } ;
24+ scrollingParent . addEventListener ( 'scroll' , handler ) ;
25+ return ( ) => scrollingParent . removeEventListener ( 'scroll' , handler ) ;
8826 } , [ targetRef , setTargetRect ] ) ;
8927} ;
9028
0 commit comments