-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathuseKeyboardShortcut.ts
68 lines (61 loc) · 2.62 KB
/
useKeyboardShortcut.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import {useEffect} from 'react';
import type {GestureResponderEvent} from 'react-native';
import type {ValueOf} from 'type-fest';
import KeyboardShortcut from '@libs/KeyboardShortcut';
import CONST from '@src/CONST';
type Shortcut = ValueOf<typeof CONST.KEYBOARD_SHORTCUTS>;
type KeyboardShortcutConfig = {
/* Should we capture the event on inputs too? */
captureOnInputs?: boolean;
/* Should we bubble the event? */
shouldBubble?: boolean;
/* The position the callback should take in the stack. 0 means top priority, and 1 means less priority than the most recently added. */
priority?: number;
/* Should call event.preventDefault after callback? */
shouldPreventDefault?: boolean;
/* Do not capture key events targeting excluded nodes (i.e. do not prevent default and let the event bubble) */
excludedNodes?: string[];
/* Is keyboard shortcut is already active */
isActive?: boolean;
/* Shuld stop propagation? */
shouldStopPropagation?: boolean;
};
/**
* Register a keyboard shortcut handler.
* Recommendation: To ensure stability, wrap the `callback` function with the useCallback hook before using it with this hook.
*/
export default function useKeyboardShortcut(shortcut: Shortcut, callback: (e?: GestureResponderEvent | KeyboardEvent) => void, config: KeyboardShortcutConfig | Record<string, never> = {}) {
const {
captureOnInputs = true,
shouldBubble = false,
priority = 0,
shouldPreventDefault = true,
// The "excludedNodes" array needs to be stable to prevent the "useEffect" hook from being recreated unnecessarily.
// Hence the use of CONST.EMPTY_ARRAY.
excludedNodes = CONST.EMPTY_ARRAY,
isActive = true,
// This flag is used to prevent auto submit form when press enter key on selection modal.
shouldStopPropagation = false,
} = config;
useEffect(() => {
if (!isActive) {
return () => {};
}
const unsubscribe = KeyboardShortcut.subscribe(
shortcut.shortcutKey,
callback,
shortcut.descriptionKey ?? '',
shortcut.modifiers,
captureOnInputs,
shouldBubble,
priority,
shouldPreventDefault,
excludedNodes as string[],
shouldStopPropagation,
);
return () => {
unsubscribe();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isActive, callback, captureOnInputs, excludedNodes, priority, shortcut.descriptionKey, shortcut.modifiers.join(), shortcut.shortcutKey, shouldBubble, shouldPreventDefault]);
}