Skip to content

Commit

Permalink
feat(useEventListener): Add null type in first parameter (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
immois authored Dec 9, 2023
1 parent c705e87 commit 5445202
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-socks-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@raddix/use-event-listener': minor
---

Add null type in first parameter of useEventListener
8 changes: 4 additions & 4 deletions packages/hooks/use-event-listener/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { type RefObject, useEffect, useRef } from 'react';

export const _window = typeof window !== 'undefined' ? window : global.window;
export const _document =
typeof document !== 'undefined' ? document : global.document;
export const _window = typeof window !== 'undefined' ? window : null;
export const _document = typeof document !== 'undefined' ? document : null;

export const useEventListener = <T extends keyof HTMLElementEventMap>(
target: RefObject<EventTarget> | EventTarget,
target: RefObject<EventTarget> | EventTarget | null,
eventType: T,
callback: (event: HTMLElementEventMap[T]) => void,
options?: boolean | AddEventListenerOptions
Expand All @@ -17,6 +16,7 @@ export const useEventListener = <T extends keyof HTMLElementEventMap>(
}, [callback]);

useEffect(() => {
if (target === null) return;
const targetElement = 'current' in target ? target.current : target;

if (!targetElement?.addEventListener) return;
Expand Down

0 comments on commit 5445202

Please sign in to comment.