feat: add use-click-outside hook

This commit is contained in:
陈嘉涵
2020-01-12 08:58:58 +08:00
parent b6549ed721
commit f329fd9f6f
2 changed files with 29 additions and 6 deletions
+23
View File
@@ -0,0 +1,23 @@
import { Ref, onMounted } from 'vue';
import { useHandler } from './use-handler';
export type UseClickOutsideOpitons = {
event: string;
callback: EventListener;
element: Ref<Element>;
flag?: Ref<boolean>;
};
export function useClickOutside(options: UseClickOutsideOpitons) {
const { event = 'click', callback, element, flag } = options;
function onClick(event: Event) {
if (!element.value.contains(event.target as Node)) {
callback(event);
}
}
onMounted(() => {
useHandler(document, event, onClick, false, flag);
});
}