Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useEventListener): Add null type in first parameter #79

Merged
merged 2 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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