Skip to content

Commit

Permalink
simplify useGlobalEventListener onBeforeMount
Browse files Browse the repository at this point in the history
  • Loading branch information
JoCa96 committed Jul 10, 2024
1 parent f2b38f7 commit 9f6c3a9
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions packages/headless/src/composables/helpers/useGlobalListener.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { onBeforeMount, onBeforeUnmount, reactive, watch, type Ref } from "vue";
import { onBeforeMount, onBeforeUnmount, reactive, watchEffect, type Ref } from "vue";

type DocumentEventType = keyof DocumentEventMap;
type GlobalListener<K extends DocumentEventType = DocumentEventType> = (
Expand Down Expand Up @@ -54,18 +54,11 @@ export const useGlobalEventListener = <K extends DocumentEventType>({
listener,
disabled,
}: UseGlobalEventListenerOptions<K>) => {
onBeforeMount(() => {
if (!disabled) {
addGlobalListener(type, listener);
return;
}
watch(
disabled,
() =>
disabled.value ? removeGlobalListener(type, listener) : addGlobalListener(type, listener),
{ immediate: true },
);
});
onBeforeMount(() =>
watchEffect(() =>
disabled?.value ? removeGlobalListener(type, listener) : addGlobalListener(type, listener),
),
);

onBeforeUnmount(() => removeGlobalListener(type, listener));
};

0 comments on commit 9f6c3a9

Please sign in to comment.