-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clickOutsideClass.js
51 lines (42 loc) · 1.46 KB
/
clickOutsideClass.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
█▀ █▄█ █▀▀ █░█ █▀▀ █░█
▄█ ░█░ █▄▄ █▀█ ██▄ ▀▄▀
Author: <Anton Sychev> (anton at sychev dot xyz)
clickOutside.js (c) 2024
Created: 2024-08-05 21:58:07
Desc: Directive to detect click outside an element detecmined by class
Sample:
main.js
import { setupClickOutsideClass } from "@/directives/clickOutside";
setupClickOutsideClass(app);
or
import clickOutside from "@/directives/clickOutside";
app.directive("click-outside-class", clickOutside);
template
<div v-click-outside-class="{ class: ['.menu', '.user-menu', '.user-wishlist'], handler: configuration.setCloseAllMenus }">
*/
const onClickOutsideClass = {
beforeMount(el, { value }) {
el.clickOutsideEvent = (event) => {
const classes =
typeof value.class === "string" ? value.class : value.class.join(",");
const dom = document.querySelectorAll(classes);
// Check if event.target does not contain any element from "dom"
let isOutside = true;
dom.forEach((item) => {
if (item.contains(event.target)) {
isOutside = false;
}
});
if (isOutside) value.handler();
};
document.addEventListener("click", el.clickOutsideEvent);
},
unmounted(el) {
document.removeEventListener("click", el.clickOutsideEvent);
},
};
export const setupClickOutsideClass = (app) => {
app.directive("click-outside-class", onClickOutsideClass);
};
export default onClickOutsideClass;