|
| 1 | +import { RefObject, useEffect } from 'react'; |
| 2 | + |
| 3 | +import { usePreservedCallback } from '../usePreservedCallback/index.ts'; |
| 4 | + |
| 5 | +/** |
| 6 | + * @description |
| 7 | + * `useEventListener` is a React hook that simplifies adding and cleaning up event listeners on various targets |
| 8 | + * such as `window`, `document`, HTML elements, or SVG elements. |
| 9 | + * It automatically updates the handler without needing to reattach the event listener on every render, |
| 10 | + * ensuring stable performance and correct behavior. |
| 11 | + * |
| 12 | + * @template KW - The type of event for window events. |
| 13 | + * @template KD - The type of event for document events. |
| 14 | + * @template KH - The type of event for HTML or SVG element events. |
| 15 | + * @template T - The type of the DOM element (default is `HTMLElement`). |
| 16 | + * @param {KW | KD | KH} eventName - The name of the event to listen for. |
| 17 | + * @param {(event: WindowEventMap[KW] | DocumentEventMap[KD] | HTMLElementEventMap[KH] | SVGElementEventMap[KH]) => void} handler - The callback function to execute when the event is triggered. |
| 18 | + * @param {RefObject<T | null> | Document} [element] - A React ref object targeting the element to attach the event listener to, or a `Document` object to attach directly to the document. Defaults to `window` if not provided. |
| 19 | + * @param {boolean | AddEventListenerOptions} [options] - Optional parameters for the event listener, such as `capture`, `once`, or `passive`. |
| 20 | + * |
| 21 | + * @returns {void} This hook does not return anything. |
| 22 | + * |
| 23 | + * @example |
| 24 | + * function WindowResize() { |
| 25 | + * useEventListener('resize', (event) => { |
| 26 | + * console.log('Window resized', event); |
| 27 | + * }); |
| 28 | + * |
| 29 | + * return <div>Resize the window and check the console.</div>; |
| 30 | + * } |
| 31 | + * |
| 32 | + * @example |
| 33 | + * function ClickButton() { |
| 34 | + * const buttonRef = useRef<HTMLButtonElement>(null); |
| 35 | + * |
| 36 | + * useEventListener('click', (event) => { |
| 37 | + * console.log('Button clicked', event); |
| 38 | + * }, buttonRef); |
| 39 | + * |
| 40 | + * return <button ref={buttonRef}>Click me</button>; |
| 41 | + * } |
| 42 | + * |
| 43 | + * @example |
| 44 | + * function Document() { |
| 45 | + * useEventListener('click', (event) => { |
| 46 | + * console.log('Document clicked at coordinates', event.clientX, event.clientY); |
| 47 | + * }, document); |
| 48 | + * |
| 49 | + * return <div>Click anywhere on the document and check the console for coordinates.</div>; |
| 50 | + * } |
| 51 | + */ |
| 52 | +export function useEventListener< |
| 53 | + K extends keyof HTMLElementEventMap & keyof SVGElementEventMap, |
| 54 | + T extends Element = K extends keyof HTMLElementEventMap ? HTMLElement : SVGElement, |
| 55 | +>( |
| 56 | + eventName: K, |
| 57 | + handler: ((event: HTMLElementEventMap[K]) => void) | ((event: SVGElementEventMap[K]) => void), |
| 58 | + element: RefObject<T | null>, |
| 59 | + options?: boolean | AddEventListenerOptions |
| 60 | +): void; |
| 61 | +export function useEventListener<K extends keyof DocumentEventMap>( |
| 62 | + eventName: K, |
| 63 | + handler: (event: DocumentEventMap[K]) => void, |
| 64 | + element: Document, |
| 65 | + options?: boolean | AddEventListenerOptions |
| 66 | +): void; |
| 67 | +export function useEventListener<K extends keyof WindowEventMap>( |
| 68 | + eventName: K, |
| 69 | + handler: (event: WindowEventMap[K]) => void, |
| 70 | + element?: undefined, |
| 71 | + options?: boolean | AddEventListenerOptions |
| 72 | +): void; |
| 73 | +export function useEventListener< |
| 74 | + KW extends keyof WindowEventMap, |
| 75 | + KD extends keyof DocumentEventMap, |
| 76 | + KH extends keyof HTMLElementEventMap & keyof SVGElementEventMap, |
| 77 | + T extends HTMLElement | SVGAElement = HTMLElement, |
| 78 | +>( |
| 79 | + eventName: KW | KD | KH, |
| 80 | + handler: (event: WindowEventMap[KW] | HTMLElementEventMap[KH] | DocumentEventMap[KD] | Event) => void, |
| 81 | + element?: RefObject<T | null> | Document, |
| 82 | + options?: boolean | AddEventListenerOptions |
| 83 | +) { |
| 84 | + const preservedHandler = usePreservedCallback(handler); |
| 85 | + |
| 86 | + useEffect(() => { |
| 87 | + const targetElement = |
| 88 | + element instanceof Document ? document : (element?.current ?? (element === undefined ? window : undefined)); |
| 89 | + |
| 90 | + if (!targetElement?.addEventListener) return; |
| 91 | + |
| 92 | + const listener: typeof handler = event => preservedHandler(event); |
| 93 | + |
| 94 | + targetElement.addEventListener(eventName, listener, options); |
| 95 | + |
| 96 | + return () => targetElement.removeEventListener(eventName, listener, options); |
| 97 | + }, [eventName, element, options, preservedHandler]); |
| 98 | +} |
0 commit comments