@@ -29,10 +29,10 @@ const UseRegisterShortcutProvider = ({
29
29
shortcutTimeout,
30
30
children,
31
31
} : UseRegisterShortcutProviderType ) => {
32
- const disableShortcuts = useRef < boolean > ( false )
33
- const shortcuts = useRef < Record < string , ShortcutType > > ( { } )
34
- const keysDown = useRef < Set < string > > ( new Set ( ) )
35
- const keyDownTimeout = useRef < ReturnType < typeof setTimeout > > ( - 1 )
32
+ const disableShortcutsRef = useRef < boolean > ( false )
33
+ const shortcutsRef = useRef < Record < string , ShortcutType > > ( { } )
34
+ const keysDownRef = useRef < Set < string > > ( new Set ( ) )
35
+ const keyDownTimeoutRef = useRef < ReturnType < typeof setTimeout > > ( - 1 )
36
36
const ignoredTags = ignoreTags ?? IGNORE_TAGS_FALLBACK
37
37
38
38
const registerShortcut : UseRegisterShortcutContextType [ 'registerShortcut' ] = useCallback (
@@ -43,79 +43,81 @@ const UseRegisterShortcutProvider = ({
43
43
}
44
44
45
45
const match =
46
- shortcuts . current [ id ] && deepEquals ( shortcuts . current [ id ] . keys , keys ) ? shortcuts . current [ id ] : null
46
+ shortcutsRef . current [ id ] && deepEquals ( shortcutsRef . current [ id ] . keys , keys )
47
+ ? shortcutsRef . current [ id ]
48
+ : null
47
49
48
50
if ( match ) {
49
51
verifyCallbackStack ( match . callbackStack )
50
52
match . callbackStack . push ( callback )
51
53
return
52
54
}
53
55
54
- shortcuts . current [ id ] = { keys : processedKeys , callbackStack : [ callback ] , description }
56
+ shortcutsRef . current [ id ] = { keys : processedKeys , callbackStack : [ callback ] , description }
55
57
} ,
56
58
[ ] ,
57
59
)
58
60
59
61
const unregisterShortcut : UseRegisterShortcutContextType [ 'unregisterShortcut' ] = useCallback ( ( keys ) => {
60
62
const { id } = preprocessKeys ( keys )
61
63
62
- if ( ! shortcuts . current [ id ] ) {
64
+ if ( ! shortcutsRef . current [ id ] ) {
63
65
return
64
66
}
65
67
66
- const { callbackStack } = shortcuts . current [ id ]
68
+ const { callbackStack } = shortcutsRef . current [ id ]
67
69
verifyCallbackStack ( callbackStack )
68
70
callbackStack . pop ( )
69
71
70
72
if ( ! callbackStack . length ) {
71
73
// NOTE: delete the shortcut only if all registered callbacks are unregistered
72
74
// if 2 shortcuts are registered with the same keys then there needs to be 2 unregister calls
73
- delete shortcuts . current [ id ]
75
+ delete shortcutsRef . current [ id ]
74
76
}
75
77
} , [ ] )
76
78
77
79
const setDisableShortcuts : UseRegisterShortcutContextType [ 'setDisableShortcuts' ] = useCallback ( ( shouldDisable ) => {
78
- disableShortcuts . current = shouldDisable
80
+ disableShortcutsRef . current = shouldDisable
79
81
} , [ ] )
80
82
81
83
const triggerShortcut : UseRegisterShortcutContextType [ 'triggerShortcut' ] = useCallback ( ( keys ) => {
82
84
const { id } = preprocessKeys ( keys )
83
85
84
- if ( ! shortcuts . current [ id ] ) {
86
+ if ( ! shortcutsRef . current [ id ] ) {
85
87
return
86
88
}
87
89
88
- const { callbackStack } = shortcuts . current [ id ]
90
+ const { callbackStack } = shortcutsRef . current [ id ]
89
91
verifyCallbackStack ( callbackStack )
90
92
91
93
// NOTE: call the last callback in the callback stack
92
94
callbackStack [ callbackStack . length - 1 ] ( )
93
95
} , [ ] )
94
96
95
97
const handleKeyupEvent = useCallback ( ( ) => {
96
- if ( ! keysDown . current . size ) {
98
+ if ( ! keysDownRef . current . size ) {
97
99
return
98
100
}
99
101
100
- const { id } = preprocessKeys ( Array . from ( keysDown . current . values ( ) ) as ShortcutType [ 'keys' ] )
102
+ const { id } = preprocessKeys ( Array . from ( keysDownRef . current . values ( ) ) as ShortcutType [ 'keys' ] )
101
103
102
- if ( shortcuts . current [ id ] ) {
103
- const { callbackStack } = shortcuts . current [ id ]
104
+ if ( shortcutsRef . current [ id ] ) {
105
+ const { callbackStack } = shortcutsRef . current [ id ]
104
106
verifyCallbackStack ( callbackStack )
105
107
callbackStack [ callbackStack . length - 1 ] ( )
106
108
}
107
109
108
- keysDown . current . clear ( )
110
+ keysDownRef . current . clear ( )
109
111
110
- if ( keyDownTimeout . current > - 1 ) {
111
- clearTimeout ( keyDownTimeout . current )
112
- keyDownTimeout . current = - 1
112
+ if ( keyDownTimeoutRef . current > - 1 ) {
113
+ clearTimeout ( keyDownTimeoutRef . current )
114
+ keyDownTimeoutRef . current = - 1
113
115
}
114
116
} , [ ] )
115
117
116
118
const handleKeydownEvent = useCallback ( ( event : KeyboardEvent ) => {
117
- if ( keyDownTimeout . current === - 1 ) {
118
- keyDownTimeout . current = setTimeout ( ( ) => {
119
+ if ( keyDownTimeoutRef . current === - 1 ) {
120
+ keyDownTimeoutRef . current = setTimeout ( ( ) => {
119
121
handleKeyupEvent ( )
120
122
} , shortcutTimeout ?? DEFAULT_TIMEOUT )
121
123
}
@@ -131,26 +133,26 @@ const UseRegisterShortcutProvider = ({
131
133
return
132
134
}
133
135
134
- if ( ! disableShortcuts . current ) {
135
- keysDown . current . add ( event . key . toUpperCase ( ) )
136
+ if ( ! disableShortcutsRef . current ) {
137
+ keysDownRef . current . add ( event . key . toUpperCase ( ) )
136
138
137
139
if ( event . ctrlKey ) {
138
- keysDown . current . add ( 'CONTROL' )
140
+ keysDownRef . current . add ( 'CONTROL' )
139
141
}
140
142
if ( event . metaKey ) {
141
- keysDown . current . add ( 'META' )
143
+ keysDownRef . current . add ( 'META' )
142
144
}
143
145
if ( event . altKey ) {
144
- keysDown . current . add ( 'ALT' )
146
+ keysDownRef . current . add ( 'ALT' )
145
147
}
146
148
if ( event . shiftKey ) {
147
- keysDown . current . add ( 'SHIFT' )
149
+ keysDownRef . current . add ( 'SHIFT' )
148
150
}
149
151
}
150
152
} , [ ] )
151
153
152
154
const handleBlur = useCallback ( ( ) => {
153
- keysDown . current . clear ( )
155
+ keysDownRef . current . clear ( )
154
156
} , [ ] )
155
157
156
158
useEffect ( ( ) => {
@@ -162,6 +164,10 @@ const UseRegisterShortcutProvider = ({
162
164
window . removeEventListener ( 'keydown' , handleKeydownEvent )
163
165
window . removeEventListener ( 'keyup' , handleKeyupEvent )
164
166
window . removeEventListener ( 'blur' , handleBlur )
167
+
168
+ if ( keyDownTimeoutRef . current > - 1 ) {
169
+ clearTimeout ( keyDownTimeoutRef . current )
170
+ }
165
171
}
166
172
} , [ handleKeyupEvent , handleKeydownEvent , handleBlur ] )
167
173
0 commit comments