Skip to content

Instantly share code, notes, and snippets.

@nexpr
Last active June 2, 2021 15:56
Show Gist options
  • Save nexpr/3a68d087a8cd680113cdb34eef946148 to your computer and use it in GitHub Desktop.
Save nexpr/3a68d087a8cd680113cdb34eef946148 to your computer and use it in GitHub Desktop.
prevent navigation module
const set = new Set()
window.addEventListener("beforeunload", (event) => {
if (set.size) {
event.returnValue = "prevent"
}
})
export const prevent = (key) => {
set.add(key)
}
export const unprevent = (key) => {
set.delete(key)
}
export const forceUnpreventAll = () => {
set.clear()
}
export const getReasons = () => [...set]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment