Skip to content

Commit 656aef9

Browse files
committed
Revert "test and look over updates"
This reverts commit 15f2c46.
1 parent 15f2c46 commit 656aef9

File tree

2 files changed

+11
-96
lines changed

2 files changed

+11
-96
lines changed

packages/gamut/src/Popover/hooks.ts

Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,6 @@ import { useEffect } from 'react';
22

33
import { 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-
425
export 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

packages/gamut/src/PopoverContainer/PopoverContainer.tsx

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,32 +75,9 @@ export const PopoverContainer: React.FC<PopoverContainerProps> = ({
7575
setTargetRect(targetRef?.current?.getBoundingClientRect());
7676
}, [targetRef, isOpen, winW, winH, winX, winY]);
7777

78-
// Enhanced scroll handling that updates both targetRect and containers
79-
const updateTargetPosition = useCallback(
80-
(rect?: DOMRect) => {
81-
const target = targetRef?.current;
82-
if (!target) return;
83-
84-
const newRect = rect || target.getBoundingClientRect();
85-
setTargetRect(newRect);
86-
87-
// Get current scroll position for accurate container calculation
88-
const currentScrollX =
89-
window.pageXOffset || document.documentElement.scrollLeft;
90-
const currentScrollY =
91-
window.pageYOffset || document.documentElement.scrollTop;
92-
93-
// Also update containers to ensure positioning stays in sync
94-
setContainers(
95-
getContainers(target, inline, { x: currentScrollX, y: currentScrollY })
96-
);
97-
},
98-
[targetRef, inline]
99-
);
100-
101-
useScrollingParentEffect(targetRef, updateTargetPosition);
78+
useScrollingParentEffect(targetRef, setTargetRect);
10279

103-
useResizingParentEffect(targetRef, updateTargetPosition);
80+
useResizingParentEffect(targetRef, setTargetRect);
10481

10582
useIsomorphicLayoutEffect(() => {
10683
if (

0 commit comments

Comments
 (0)