1- import { KeyboardEvent as KeyEvt , RefObject , useCallback } from "react" ;
1+ import { KeyboardEvent as KeyEvt , RefObject , useMemo } from "react" ;
22import { useEventListener } from "." ;
3+ import { hotKeyHandler } from "../utils" ;
34
45/**
5- * __`useHotKeys`__: Hook to listen for the keyboard press, support key combinations.
6+ * __`useHotKeys`__: Hook to listen for the keyboard press, support key combinations, built on [hotKeyHandler](#/hotKeyHandler) utility function .
67 * @param {Object } options
78 * @param {`${string}` | `${'alt' | 'ctrl' | 'meta' | 'shift' | 'ctrlCommand'}+${string}` | `${'alt' | 'ctrl' | 'meta' | 'shift' | 'ctrlCommand'}+${'alt' | 'ctrl' | 'meta' | 'shift' | 'ctrlCommand'}+${string}` } options.hotKey - hotKey string: _ctrlCommand_ indicates to listen __Ctrl__ (on Windows) or __Command__ (on Mac) keys.
89 * @param {"keydown"|"keyup" } [options.type="keydown"] - event type
@@ -12,46 +13,9 @@ import { useEventListener } from ".";
1213 * @returns {()=>void } remove - used to manually remove the eventListener, otherwise is removed when component is unmounted.
1314 */
1415export const useHotKeys = ( { hotKey, type = "keydown" , target = window , listener, listenerOpts } : { hotKey : `${string } ` | `${'alt' | 'ctrl' | 'meta' | 'shift' | 'ctrlCommand' } +${string } ` | `${'alt' | 'ctrl' | 'meta' | 'shift' | 'ctrlCommand' } +${'alt' | 'ctrl' | 'meta' | 'shift' | 'ctrlCommand' } +${string } `, type ?: "keydown" | "keyup" , target ?: RefObject < HTMLElement > | Window , listener : ( evt : KeyboardEvent | KeyEvt < HTMLElement > ) => void | Promise < void > , listenerOpts ?: boolean | AddEventListenerOptions } ) : ( ( ) => void ) => {
15- const handler = useCallback ( ( evt : KeyboardEvent | KeyEvt < HTMLElement > ) => {
16- let keys = hotKey . toLowerCase ( ) . split ( "+" ) . map ( el => el . trim ( ) ) ;
17- const modifiers = {
18- alt : keys . includes ( 'alt' ) ,
19- ctrl : keys . includes ( 'ctrl' ) ,
20- meta : keys . includes ( 'meta' ) ,
21- ctrlCommand : keys . includes ( 'ctrlcommand' ) ,
22- shift : keys . includes ( 'shift' ) ,
23- } ;
24- keys = keys . filter ( el => ! [ 'alt' , 'ctrl' , 'meta' , 'shift' , 'ctrlCommand' ] . includes ( el ) ) ;
25- for ( let i = 0 , size = keys . length ; i < size ; i ++ ) {
26- if ( keys [ i ] === "" && ( i + 1 ) < size && keys [ i + 1 ] === "" ) {
27- keys [ i + 1 ] = "+" ;
28- }
29- }
30- keys = keys . filter ( el => el !== "" )
31- const { altKey, ctrlKey, metaKey, shiftKey, key : pressedKey } = evt ;
32-
33- if ( modifiers . ctrlCommand ) {
34- if ( ! ctrlKey && ! metaKey ) {
35- return false ;
36- }
37- } else {
38- if ( ctrlKey !== modifiers . ctrl ) {
39- return false ;
40- }
41- if ( altKey !== modifiers . alt ) {
42- return false ;
43- }
44- if ( metaKey !== modifiers . meta ) {
45- return false ;
46- }
47- if ( shiftKey !== modifiers . shift ) {
48- return false ;
49- }
50- }
51- if ( ! ( keys . includes ( pressedKey . toLowerCase ( ) ) || keys . includes ( evt . code . replace ( "Key" , "" ) ) ) ) {
52- return false
53- }
54- listener && listener ( evt ) ;
16+ const handler = useMemo ( ( ) => {
17+ const handler = hotKeyHandler ( hotKey , listener ) ;
18+ return handler ;
5519 } , [ hotKey , listener ] ) ;
5620
5721 const remove = useEventListener < KeyboardEvent > ( {
0 commit comments