Created
February 20, 2023 14:58
-
-
Save ValentaTomas/e602da12f60d981ace41f929ea4dcc21 to your computer and use it in GitHub Desktop.
Open a popup modal window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function openPopupModal(url: URL) { | |
const features = { | |
popup: 'yes', | |
width: 820, | |
height: 700, | |
top: 'auto', | |
left: 'auto', | |
toolbar: 'no', | |
menubar: 'no', | |
} | |
const strWindowsFeatures = Object.entries(features) | |
.reduce((str, [key, value]) => { | |
if (value == 'auto') { | |
if (key === 'top') { | |
const v = Math.round( | |
window.innerHeight / 2 - features.height / 2 | |
) | |
str += `top=${v},` | |
} else if (key === 'left') { | |
const v = Math.round( | |
window.innerWidth / 2 - features.width / 2 | |
) | |
str += `left=${v},` | |
} | |
return str | |
} | |
str += `${key}=${value},` | |
return str | |
}, '') | |
.slice(0, -1) // remove last ',' (comma) | |
return window.open(url, '_blank', strWindowsFeatures) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment