Skip to content

Commit

Permalink
fix: resolved click outside directive issues with Vue 3
Browse files Browse the repository at this point in the history
  • Loading branch information
dyersituations committed Jun 12, 2024
1 parent 97fd051 commit 7125c22
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/plugins/click-outside.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ export default {
* outside element and fires a callback function
* Sample Use:
* <div v-click-outside="nameOfCustomEventToCall">Some content</div>
* TODO Register directive globally?
* mixins: [
* clickOutside,
* ],
*/
clickOutside: {
bind(el, binding, vnode) {
beforeMount(el, binding) {
// eslint-disable-next-line no-param-reassign
el.clickOutsideEvent = event => {
// here I check that click was outside the el and his children
if (!(el === event.target || el.contains(event.target))) {
// and if it did, call method provided in attribute value
vnode.context[binding.expression](event);
binding.value(event, el);
}
};
document.body.addEventListener('click', el.clickOutsideEvent);
},
unbind(el) {
unmounted(el) {
document.body.removeEventListener('click', el.clickOutsideEvent);
},
}
Expand Down

0 comments on commit 7125c22

Please sign in to comment.