@@ -9,42 +9,39 @@ const useShortcut = (combination, onKeyDownFn, onKeyUpFn) => {
99 import ( 'keyboardjs' ) . then ( setKeyboardJs )
1010 } , [ ] )
1111
12- useEffect (
13- ( ) => {
14- if ( ! keyboardJs ) {
15- return
16- }
17-
18- const down = event => {
19- set ( [ true , event ] )
20- if ( onKeyDownFn ) onKeyDownFn ( )
21- }
22- const up = event => {
23- set ( [ false , event ] )
24- if ( onKeyUpFn ) onKeyUpFn ( )
12+ useEffect ( ( ) => {
13+ if ( ! keyboardJs ) {
14+ return
15+ }
16+
17+ const down = event => {
18+ set ( [ true , event ] )
19+ if ( onKeyDownFn ) onKeyDownFn ( )
20+ }
21+ const up = event => {
22+ set ( [ false , event ] )
23+ if ( onKeyUpFn ) onKeyUpFn ( )
24+ }
25+
26+ // support multi combination in array style
27+ if ( Array . isArray ( combination ) ) {
28+ for ( let i = 0 ; i < combination . length ; i += 1 ) {
29+ keyboardJs . bind ( combination [ i ] , down , up )
2530 }
31+ } else {
32+ keyboardJs . bind ( combination , down , up )
33+ }
2634
27- // support multi combination in array style
35+ return ( ) => {
2836 if ( Array . isArray ( combination ) ) {
2937 for ( let i = 0 ; i < combination . length ; i += 1 ) {
30- keyboardJs . bind ( combination [ i ] , down , up )
38+ keyboardJs . unbind ( combination [ i ] , down , up )
3139 }
3240 } else {
33- keyboardJs . bind ( combination , down , up )
34- }
35-
36- return ( ) => {
37- if ( Array . isArray ( combination ) ) {
38- for ( let i = 0 ; i < combination . length ; i += 1 ) {
39- keyboardJs . unbind ( combination [ i ] , down , up )
40- }
41- } else {
42- keyboardJs . unbind ( combination , down , up )
43- }
41+ keyboardJs . unbind ( combination , down , up )
4442 }
45- } ,
46- [ combination , keyboardJs , onKeyDownFn , onKeyUpFn ]
47- )
43+ }
44+ } , [ combination , keyboardJs , onKeyDownFn , onKeyUpFn ] )
4845
4946 return state
5047}
0 commit comments