vant/src-next/hooks/use-global-event.ts
2020-07-07 15:06:16 +08:00

47 lines
738 B
TypeScript

import { on, off } from '../utils/dom/event';
import {
Ref,
watch,
onMounted,
onActivated,
onUnmounted,
onDeactivated
} from 'vue';
export function useGlobalEvent(
target: EventTarget,
event: string,
handler: EventListener,
passive = false,
flag?: Ref<boolean>
) {
let binded: boolean;
function add() {
if (binded || (flag && !flag.value)) {
return;
}
on(target, event, handler, passive);
binded = true;
}
function remove() {
if (binded) {
off(target, event, handler);
binded = false;
}
}
if (flag) {
watch(() => {
flag.value ? add() : remove();
});
}
onMounted(add);
onActivated(add);
onUnmounted(remove);
onDeactivated(remove);
}