File size: 988 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
interface OpenPopupOptions {
url: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onMessage( message: any, popup: Window ): void;
}
export const openPopup = ( { url, onMessage }: OpenPopupOptions ) => {
let popup: Window | null;
try {
const width = 700;
const height = 600;
const top = window.screen.height / 2 - height / 2;
const left = window.screen.width / 2 - width / 2;
popup = window.open(
url,
undefined,
`popup=1,width=${ width },height=${ height },top=${ top },left=${ left }`
);
} catch {
return false;
}
const handleMessage = ( event: MessageEvent ) => {
const { data } = event;
if ( ! data || ! popup ) {
return;
}
onMessage( data, popup );
};
window.addEventListener( 'message', handleMessage );
const interval = window.setInterval( () => {
if ( ! popup || popup.closed ) {
clearInterval( interval );
window.removeEventListener( 'message', handleMessage );
}
}, 500 );
return true;
};
|