Skip to content

Commit 12f19ab

Browse files
authored
Merge pull request #82 from hoxyq/use-ResizeObserver-global-from-parentNode-realm
fix: use ResizeObserver global from parentNode realm to support case with multiple realms
2 parents 2fd2329 + a4a7af7 commit 12f19ab

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

src/AutoSizer.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,41 +29,40 @@ export class AutoSizer extends Component<Props, State> {
2929

3030
componentDidMount() {
3131
const { nonce } = this.props;
32+
const parentNode = this._autoSizer ? this._autoSizer.parentNode : null;
3233

3334
if (
34-
this._autoSizer &&
35-
this._autoSizer.parentNode &&
36-
this._autoSizer.parentNode.ownerDocument &&
37-
this._autoSizer.parentNode.ownerDocument.defaultView &&
38-
this._autoSizer.parentNode instanceof
39-
this._autoSizer.parentNode.ownerDocument.defaultView.HTMLElement
35+
parentNode != null &&
36+
parentNode.ownerDocument &&
37+
parentNode.ownerDocument.defaultView &&
38+
parentNode instanceof parentNode.ownerDocument.defaultView.HTMLElement
4039
) {
4140
// Delay access of parentNode until mount.
4241
// This handles edge-cases where the component has already been unmounted before its ref has been set,
4342
// As well as libraries like react-lite which have a slightly different lifecycle.
44-
this._parentNode = this._autoSizer.parentNode;
45-
46-
// Defer requiring resize handler in order to support server-side rendering.
47-
// See issue #41
48-
if (this._parentNode != null) {
49-
if (typeof ResizeObserver !== "undefined") {
50-
this._resizeObserver = new ResizeObserver(() => {
51-
// Guard against "ResizeObserver loop limit exceeded" error;
52-
// could be triggered if the state update causes the ResizeObserver handler to run long.
53-
// See https://github.com/bvaughn/react-virtualized-auto-sizer/issues/55
54-
this._timeoutId = setTimeout(this._onResize, 0);
55-
});
56-
this._resizeObserver.observe(this._parentNode);
57-
} else {
58-
this._detectElementResize = createDetectElementResize(nonce);
59-
this._detectElementResize.addResizeListener(
60-
this._parentNode,
61-
this._onResize
62-
);
63-
}
64-
65-
this._onResize();
43+
this._parentNode = parentNode;
44+
45+
// Use ResizeObserver from the same context where parentNode (which we will observe) was defined
46+
// Using just global can result into onResize events not being emitted in cases with multiple realms
47+
const ResizeObserverInstance =
48+
parentNode.ownerDocument.defaultView.ResizeObserver;
49+
50+
if (ResizeObserverInstance != null) {
51+
this._resizeObserver = new ResizeObserverInstance(() => {
52+
// Guard against "ResizeObserver loop limit exceeded" error;
53+
// could be triggered if the state update causes the ResizeObserver handler to run long.
54+
// See https://github.com/bvaughn/react-virtualized-auto-sizer/issues/55
55+
this._timeoutId = setTimeout(this._onResize, 0);
56+
});
57+
this._resizeObserver.observe(parentNode);
58+
} else {
59+
// Defer requiring resize handler in order to support server-side rendering.
60+
// See issue #41
61+
this._detectElementResize = createDetectElementResize(nonce);
62+
this._detectElementResize.addResizeListener(parentNode, this._onResize);
6663
}
64+
65+
this._onResize();
6766
}
6867
}
6968

0 commit comments

Comments
 (0)