rmariuzzo / react-new-window

🔲 Pop new windows in React, using `window.open`.
https://rmariuzzo.github.io/react-new-window/
MIT License
448 stars 110 forks source link

onBlock is never called #152

Open gnekoz opened 1 year ago

gnekoz commented 1 year ago

In case the browser settings block the opening of a new popup/tab an error is raised and the onBlock callback is never invoked. The cause is the creation of the div right after the window creation:

    // Open a new window.
    this.window = window.open(url, name, toWindowFeatures(features))
    this.container = this.window.document.createElement('div')

In case of browser blocking the window opening this.window will be undefined and the second line will fail:

TypeError: Cannot read properties of null (reading 'document')
    at NewWindow.openChild (new-window.js:236:32)
    at NewWindow.componentDidMount (new-window.js:191:9)
    at commitLifeCycles (react-dom.development.js:20664:1)

Also the creation of the div element seems unnecessary since the container property will be set few lines below:

        if (this.window) {
            this.window.document.title = title;

            // Check if the container already exists as the window may have been already open
            this.container = this.window.document.getElementById('new-window-container');