Skip to content

Instantly share code, notes, and snippets.

@asvae
Last active July 16, 2018 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asvae/c28ea25d1e82e02852b060ee2a94ff32 to your computer and use it in GitHub Desktop.
Save asvae/c28ea25d1e82e02852b060ee2a94ff32 to your computer and use it in GitHub Desktop.
// ...
methods: {
show () {
this.isDisplayed = true
this.$emit('input', true)
this.listen(true)
},
hide () {
this.isDisplayed = false
this.$emit('input', false)
this.listen(false)
},
listen (on: Boolean = true) {
// So that we won't add or remove same listener twice
if (this.isListening === on) {
return
}
// Adding or removing listeners.
if (on) {
setTimeout(() => {
document.body.addEventListener('click', this.hide, false)
})
} else {
document.body.removeEventListener('click', this.hide, false)
}
// Flag to know if listener is already registered.
this.isListening = on
}
},
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment