UNPKG

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