Last active
July 16, 2018 09:27
-
-
Save asvae/c28ea25d1e82e02852b060ee2a94ff32 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// ... | |
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