Skip to content

Instantly share code, notes, and snippets.

@ValentaTomas
Created February 20, 2023 14:58
Show Gist options
  • Save ValentaTomas/e602da12f60d981ace41f929ea4dcc21 to your computer and use it in GitHub Desktop.
Save ValentaTomas/e602da12f60d981ace41f929ea4dcc21 to your computer and use it in GitHub Desktop.
Open a popup modal window
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