rmariuzzo / react-new-window

🔲 Pop new windows in React, using `window.open`.

Home Page:https://rmariuzzo.github.io/react-new-window/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Close new window on parent tab close.

rodrigo-i-sosa opened this issue · comments

Is there a way to implement this behavior? currently the only way to close the window is by closing the browser completely.

Hey there! I apologize for the very long reply. Currently, you can handle using the onOpen callback to capture the window instance.

Here's a simplified example:

const [popup, setPopup] = useState()

useEffect(() => {
  window.addEventListener('unload', () => {
    if (popup) popup.close()
  })
}, [])

return <NewWindow onOpen={e => setPopup(e)} />