diff --git a/app/composables/useEventListener.ts b/app/composables/useEventListener.ts new file mode 100644 index 0000000..0819a2c --- /dev/null +++ b/app/composables/useEventListener.ts @@ -0,0 +1,35 @@ +/** + * A composable to automatically register and clean up an event listener on a given target. + * + * @template T - The type of event to listen for (extends Event). + * @param {EventTarget | Ref} target - The target to attach the event listener to. Can be a direct EventTarget or a ref to one. + * @param {string} event - The name of the event to listen for (e.g., 'click', 'keydown'). + * @param {(e: T) => void} handler - The callback function to handle the event. + * @param {AddEventListenerOptions} [options] - Optional options to pass to `addEventListener`. + * + * @example + * ```ts + * const button = ref(null) + * + * useEventListener(button, 'click', (e) => { + * console.log('Button clicked!', e) + * }) + * ``` + */ + +export function useEventListener( + target: EventTarget | Ref, + event: string, + handler: (e: T) => void, + options?: AddEventListenerOptions +) { + onMounted(() => { + const el = unref(target) + el?.addEventListener(event, handler as EventListener, options) + }) + + onBeforeUnmount(() => { + const el = unref(target) + el?.removeEventListener(event, handler as EventListener) + }) +}