-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29226 from kubabutkiewicz/ts-migration/useKeyboar…
…dShortcut/hook [No QA] [TS migration] Migrate 'useKeyboardShortcut.js' hook to TypeScript
- Loading branch information
Showing
4 changed files
with
70 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import {useEffect} from 'react'; | ||
import {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; | ||
}; | ||
|
||
/** | ||
* 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: () => 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, | ||
} = config; | ||
|
||
useEffect(() => { | ||
if (!isActive) { | ||
return () => {}; | ||
} | ||
|
||
const unsubscribe = KeyboardShortcut.subscribe( | ||
shortcut.shortcutKey, | ||
callback, | ||
shortcut.descriptionKey ?? '', | ||
shortcut.modifiers, | ||
captureOnInputs, | ||
shouldBubble, | ||
priority, | ||
shouldPreventDefault, | ||
excludedNodes as string[], | ||
); | ||
|
||
return () => { | ||
unsubscribe(); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [isActive, callback, captureOnInputs, excludedNodes, priority, shortcut.descriptionKey, shortcut.modifiers.join(), shortcut.shortcutKey, shouldBubble, shouldPreventDefault]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters