From 4ce3be66bf2de4e86082c319bee35d9c9c6cc88d Mon Sep 17 00:00:00 2001 From: Jacob Date: Thu, 26 Oct 2023 20:52:41 +0100 Subject: [PATCH 1/2] Bug Fix Moved the initialization of this.container. If the browser blocks popups this.window is null and a TypeError is thrown. Also added OR operator in render() as if the new window is blocked, no container is created ad React can't create the portal. --- src/NewWindow.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/NewWindow.js b/src/NewWindow.js index 19dbfb2..2e78014 100644 --- a/src/NewWindow.js +++ b/src/NewWindow.js @@ -47,7 +47,7 @@ class NewWindow extends React.PureComponent { * Render the NewWindow component. */ render() { - if (!this.state.mounted) return null + if (!this.state.mounted || !this.container) return null return ReactDOM.createPortal(this.props.children, this.container) } @@ -102,7 +102,7 @@ class NewWindow extends React.PureComponent { // Open a new window. this.window = window.open(url, name, toWindowFeatures(features)) - this.container = this.window.document.createElement('div') + // When a new window use content from a cross-origin there's no way we can attach event // to it. Therefore, we need to detect in a interval when the new window was destroyed // or was closed. @@ -114,6 +114,7 @@ class NewWindow extends React.PureComponent { // Check if the new window was succesfully opened. if (this.window) { + this.container = this.window.document.createElement('div') this.window.document.title = title // Check if the container already exists as the window may have been already open From 550cfbc3bf34cc6466493620050358057446489c Mon Sep 17 00:00:00 2001 From: Jacob Date: Thu, 26 Oct 2023 21:09:26 +0100 Subject: [PATCH 2/2] Update NewWindow.js --- src/NewWindow.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/NewWindow.js b/src/NewWindow.js index 2e78014..1e606bc 100644 --- a/src/NewWindow.js +++ b/src/NewWindow.js @@ -114,7 +114,6 @@ class NewWindow extends React.PureComponent { // Check if the new window was succesfully opened. if (this.window) { - this.container = this.window.document.createElement('div') this.window.document.title = title // Check if the container already exists as the window may have been already open