Skip to content

Commit a512a91

Browse files
authored
fix: Fix useIntersectionObserver not firing when running inside iframe on Firefox (#3579)
1 parent 3e0adb2 commit a512a91

File tree

1 file changed

+12
-1
lines changed
  • src/internal/hooks/use-intersection-observer

1 file changed

+12
-1
lines changed

src/internal/hooks/use-intersection-observer/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,18 @@ export function useIntersectionObserver<T extends HTMLElement>({
3030

3131
// Create a new observer with the target element
3232
if (targetElement) {
33-
observerRef.current = new IntersectionObserver(([entry]) => setIsIntersecting(entry.isIntersecting));
33+
// Fix for AWSUI-60898: In Firefox, IntersectionObserver instances inside an
34+
// iframe context can't detect visibility changes caused by changes to elements
35+
// outside the iframe (e.g. if an element wrapping the iframe is set to `display: none`).
36+
let TopLevelIntersectionObserver = IntersectionObserver;
37+
try {
38+
if (window.top) {
39+
TopLevelIntersectionObserver = (window.top as typeof window).IntersectionObserver;
40+
}
41+
} catch {
42+
// Tried to access a cross-origin iframe. Fall back to current IntersectionObserver.
43+
}
44+
observerRef.current = new TopLevelIntersectionObserver(([entry]) => setIsIntersecting(entry.isIntersecting));
3445
observerRef.current.observe(targetElement);
3546
}
3647
}, []);

0 commit comments

Comments
 (0)