UNPKG

656 BJavaScriptView Raw
1import useEventListener from './useEventListener';
2import { useCallback } from 'react';
3/**
4 * Attaches an event handler outside directly to the `document`,
5 * bypassing the react synthetic event system.
6 *
7 * ```ts
8 * useGlobalListener('keydown', (event) => {
9 * console.log(event.key)
10 * })
11 * ```
12 *
13 * @param event The DOM event name
14 * @param handler An event handler
15 * @param capture Whether or not to listen during the capture event phase
16 */
17export default function useGlobalListener(event, handler, capture = false) {
18 const documentTarget = useCallback(() => document, []);
19 return useEventListener(documentTarget, event, handler, capture);
20}
\No newline at end of file