From fea9265532dbd45677a4d41b8ff48356cb7f1ab5 Mon Sep 17 00:00:00 2001 From: Justin Leone Date: Wed, 11 Sep 2024 14:04:07 -0400 Subject: [PATCH] Instantiate the ResizeObserver on the correct window --- src/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 21d1350..c1d7436 100644 --- a/src/index.ts +++ b/src/index.ts @@ -102,10 +102,14 @@ function useResizeObserver( resizeObserverRef.current.box !== opts.box || resizeObserverRef.current.round !== round ) { + // It's possible that the global window is different than the window that the element is rendered in. + // Therefore we need to get the element's window's ResizeObserver constructor + const currentDocument = element.ownerDocument; + const currentWindow = currentDocument?.defaultView ?? window; resizeObserverRef.current = { box: opts.box, round, - instance: new ResizeObserver((entries) => { + instance: new currentWindow.ResizeObserver((entries) => { const entry = entries[0]; const boxProp =