mirror of
https://github.com/youzan/vant.git
synced 2025-10-22 11:54:02 +00:00
chore: rename api folder to composition
This commit is contained in:
46
src/composition/use-global-event.ts
Normal file
46
src/composition/use-global-event.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
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);
|
||||
}
|
Reference in New Issue
Block a user