Last active
February 4, 2023 11:40
-
-
Save kirilkirkov/593e77baed0d205096f79d5e32d2dd0e to your computer and use it in GitHub Desktop.
DOM Element Click OutSide handler with Vanilla JS. Easy integration in Vue or React. ES6 Import Module Type
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
/** | |
* Make new class instance for each element | |
*/ | |
class ClickOutSide { | |
constructor() { | |
this.outsideHandler = null | |
this.element = null | |
this.documentEvent = (evt) => { | |
const flyoutEl = this.element | |
let targetEl = evt.target // clicked same el | |
do { | |
if (targetEl == flyoutEl) { | |
this.setEvent('inside') | |
return | |
} | |
// up to dom | |
targetEl = targetEl.parentNode | |
} while (targetEl) | |
this.setEvent('outside') | |
} | |
} | |
/** | |
* Attach element by its id | |
* | |
* @param {string} el - Element to handle | |
*/ | |
attachElementById(id) { | |
let el = document.getElementById(id) | |
return this.attachElement(el) | |
} | |
/** | |
* Attach vue ref element or ready intanced | |
* | |
* @param {*} el - Element to handle | |
*/ | |
attachElement(el) { | |
this.element = el | |
document.addEventListener('click', this.documentEvent) | |
return this | |
} | |
setEvent(type) { | |
if (type === 'outside') { | |
this.outsideHandler() | |
} | |
} | |
/** | |
* Event on clicked outside | |
* | |
* @param {func} anonFunc - anony func to execute | |
*/ | |
onOutside(anonFunc) { | |
this.outsideHandler = anonFunc | |
} | |
/** | |
* | |
* @param {*} el - Element to remove listener | |
*/ | |
detach() { | |
document.removeEventListener('click', this.documentEvent) | |
} | |
} | |
export default ClickOutSide | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage in Vue 3 Composition API: