{"version":3,"file":"use-event-listener.cjs","names":[],"sources":["../../src/hooks/use-event-listener.ts"],"sourcesContent":["import { useEffect } from \"react\";\nimport { useLatestRef } from \"./use-latest-ref\";\n\ntype AnyEventTarget = EventTarget | { current: EventTarget | null } | null | undefined;\n\n/**\n * Subscribe to a DOM event with React-friendly semantics.\n *\n * - Handler is stored in a ref so callers can pass inline functions without\n *   resubscribing on every render.\n * - `target` defaults to `window`. Accepts a raw `EventTarget` (window, document,\n *   element) OR a ref pointing at one.\n * - Returns nothing — cleanup is automatic on unmount or when `eventName`/`target` change.\n *\n * @tempest-limits param-count — deliberately the same positional shape as\n * `addEventListener(type, listener, options)`, with the target inserted where React\n * needs it. A hook that mirrors a DOM API is read by everyone who already knows the\n * DOM API; a bespoke options bag would make the familiar call unfamiliar.\n */\nexport function useEventListener<K extends keyof WindowEventMap>(\n    eventName: K,\n    handler: (event: WindowEventMap[K]) => void,\n    target?: AnyEventTarget,\n    options?: AddEventListenerOptions | boolean,\n): void;\nexport function useEventListener<K extends keyof DocumentEventMap>(\n    eventName: K,\n    handler: (event: DocumentEventMap[K]) => void,\n    target?: AnyEventTarget,\n    options?: AddEventListenerOptions | boolean,\n): void;\nexport function useEventListener<K extends keyof HTMLElementEventMap>(\n    eventName: K,\n    handler: (event: HTMLElementEventMap[K]) => void,\n    target?: AnyEventTarget,\n    options?: AddEventListenerOptions | boolean,\n): void;\nexport function useEventListener(\n    eventName: string,\n    handler: (event: Event) => void,\n    target?: AnyEventTarget,\n    options?: AddEventListenerOptions | boolean,\n): void {\n    const handlerRef = useLatestRef(handler);\n\n    useEffect(() => {\n        const resolvedTarget: EventTarget | null =\n            target === undefined\n                ? typeof window === \"undefined\"\n                    ? null\n                    : window\n                : \"current\" in (target as { current: unknown })\n                  ? ((target as { current: EventTarget | null }).current ?? null)\n                  : (target as EventTarget | null);\n\n        if (!resolvedTarget?.addEventListener) return;\n\n        const listener: EventListener = (event) => handlerRef.current(event);\n        resolvedTarget.addEventListener(eventName, listener, options);\n        return () => resolvedTarget.removeEventListener(eventName, listener, options);\n    }, [eventName, target, options, handlerRef]);\n}\n"],"mappings":"+DAqCA,SAAgB,EACZ,EACA,EACA,EACA,EACI,CACJ,IAAM,EAAa,EAAA,aAAa,CAAO,GAEvC,EAAA,EAAA,UAAA,KAAgB,CACZ,IAAM,EACF,IAAW,IAAA,GACL,OAAO,OAAW,IACd,KACA,OACJ,YAAc,EACV,EAA2C,SAAW,KACvD,EAEb,GAAI,CAAC,GAAgB,iBAAkB,OAEvC,IAAM,EAA2B,GAAU,EAAW,QAAQ,CAAK,EAEnE,OADA,EAAe,iBAAiB,EAAW,EAAU,CAAO,MAC/C,EAAe,oBAAoB,EAAW,EAAU,CAAO,CAChF,EAAG,CAAC,EAAW,EAAQ,EAAS,CAAU,CAAC,CAC/C"}