Skip to content

Commit

Permalink
fix: useEventListeners hook
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jun 26, 2023
1 parent dad27dc commit 623031e
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
useEffect,
useState,
Dispatch,
SetStateAction
SetStateAction,
DependencyList
} from 'react';

export function useFreshworksWidget(display: 'open' | 'hide'): void {
Expand Down Expand Up @@ -97,3 +98,26 @@ export function useCountdown(

return [_seconds, _setSeconds];
}

export function useEventListener<
EventType extends keyof HTMLElementEventMap
>(
element: HTMLElement,
type: EventType,
listener: (this: HTMLElement, ev: HTMLElementEventMap[EventType]) => any,
{
options,
deps = []
}: {
options?: boolean | AddEventListenerOptions,
deps?: DependencyList
}
): void {
useEffect(() => {
element.addEventListener(type, listener, options);

return () => {
element.removeEventListener(type, listener, options);
};
}, deps);
}

0 comments on commit 623031e

Please sign in to comment.