Skip to content

Get the window that the element is rendered in to instantiate ResizeObserver #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ function useResizeObserver<T extends Element>(
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 =
Expand Down