1
0
mirror of https://gitee.com/vant-contrib/vant.git synced 2025-04-06 03:57:59 +08:00
vant/src-next/hooks/use-click-outside.ts
2020-07-04 21:20:01 +08:00

22 lines
533 B
TypeScript

import { Ref } from 'vue';
import { useGlobalEvent } from './use-global-event';
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);
}
}
useGlobalEvent(document, event, onClick, false, flag);
}