react-hotkeys-hook 5.0.0-1 → 5.0.0-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +13 -85
- package/{dist → packages/react-hotkeys-hook/dist}/BoundHotkeysProxyProvider.d.ts +14 -14
- package/{dist → packages/react-hotkeys-hook/dist}/HotkeysProvider.d.ts +16 -16
- package/packages/react-hotkeys-hook/dist/deepEqual.d.ts +1 -0
- package/{dist → packages/react-hotkeys-hook/dist}/index.d.ts +6 -6
- package/packages/react-hotkeys-hook/dist/index.js +220 -0
- package/{dist → packages/react-hotkeys-hook/dist}/isHotkeyPressed.d.ts +4 -4
- package/{dist → packages/react-hotkeys-hook/dist}/parseHotkeys.d.ts +5 -5
- package/packages/react-hotkeys-hook/dist/types.d.ts +45 -0
- package/{dist → packages/react-hotkeys-hook/dist}/useDeepEqualMemo.d.ts +1 -1
- package/{dist → packages/react-hotkeys-hook/dist}/useHotkeys.d.ts +2 -3
- package/{dist → packages/react-hotkeys-hook/dist}/useRecordHotkeys.d.ts +6 -6
- package/{dist → packages/react-hotkeys-hook/dist}/validators.d.ts +8 -7
- package/dist/deepEqual.d.ts +0 -1
- package/dist/index.js +0 -8
- package/dist/react-hotkeys-hook.cjs.development.js +0 -525
- package/dist/react-hotkeys-hook.cjs.development.js.map +0 -1
- package/dist/react-hotkeys-hook.cjs.production.min.js +0 -2
- package/dist/react-hotkeys-hook.cjs.production.min.js.map +0 -1
- package/dist/react-hotkeys-hook.esm.js +0 -519
- package/dist/react-hotkeys-hook.esm.js.map +0 -1
- package/dist/setupTests.d.ts +0 -1
- package/dist/types.d.ts +0 -38
- package/src/BoundHotkeysProxyProvider.tsx +0 -27
- package/src/HotkeysProvider.tsx +0 -81
- package/src/deepEqual.ts +0 -8
- package/src/index.ts +0 -16
- package/src/isHotkeyPressed.ts +0 -71
- package/src/parseHotkeys.ts +0 -58
- package/src/types.ts +0 -61
- package/src/useDeepEqualMemo.ts +0 -12
- package/src/useHotkeys.ts +0 -176
- package/src/useRecordHotkeys.ts +0 -51
- package/src/validators.ts +0 -111
package/src/useRecordHotkeys.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { useCallback, useState } from 'react'
|
|
2
|
-
import { mapKey } from './parseHotkeys'
|
|
3
|
-
|
|
4
|
-
export default function useRecordHotkeys() {
|
|
5
|
-
const [keys, setKeys] = useState(new Set<string>())
|
|
6
|
-
const [isRecording, setIsRecording] = useState(false)
|
|
7
|
-
|
|
8
|
-
const handler = useCallback((event: KeyboardEvent) => {
|
|
9
|
-
if (event.code === undefined) {
|
|
10
|
-
// Synthetic event (e.g., Chrome autofill). Ignore.
|
|
11
|
-
return
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
event.preventDefault()
|
|
15
|
-
event.stopPropagation()
|
|
16
|
-
|
|
17
|
-
setKeys((prev) => {
|
|
18
|
-
const newKeys = new Set(prev)
|
|
19
|
-
|
|
20
|
-
newKeys.add(mapKey(event.code))
|
|
21
|
-
|
|
22
|
-
return newKeys
|
|
23
|
-
})
|
|
24
|
-
}, [])
|
|
25
|
-
|
|
26
|
-
const stop = useCallback(() => {
|
|
27
|
-
if (typeof document !== 'undefined') {
|
|
28
|
-
document.removeEventListener('keydown', handler)
|
|
29
|
-
|
|
30
|
-
setIsRecording(false)
|
|
31
|
-
}
|
|
32
|
-
}, [handler])
|
|
33
|
-
|
|
34
|
-
const start = useCallback(() => {
|
|
35
|
-
setKeys(new Set<string>())
|
|
36
|
-
|
|
37
|
-
if (typeof document !== 'undefined') {
|
|
38
|
-
stop()
|
|
39
|
-
|
|
40
|
-
document.addEventListener('keydown', handler)
|
|
41
|
-
|
|
42
|
-
setIsRecording(true)
|
|
43
|
-
}
|
|
44
|
-
}, [handler, stop])
|
|
45
|
-
|
|
46
|
-
const resetKeys = useCallback(() => {
|
|
47
|
-
setKeys(new Set<string>())
|
|
48
|
-
}, [])
|
|
49
|
-
|
|
50
|
-
return [keys, { start, stop, resetKeys, isRecording }] as const
|
|
51
|
-
}
|
package/src/validators.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { FormTags, Hotkey, Scopes, Trigger } from './types'
|
|
2
|
-
import { isHotkeyPressed, isReadonlyArray } from './isHotkeyPressed'
|
|
3
|
-
import { mapKey } from './parseHotkeys'
|
|
4
|
-
|
|
5
|
-
export function maybePreventDefault(e: KeyboardEvent, hotkey: Hotkey, preventDefault?: Trigger): void {
|
|
6
|
-
if ((typeof preventDefault === 'function' && preventDefault(e, hotkey)) || preventDefault === true) {
|
|
7
|
-
e.preventDefault()
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function isHotkeyEnabled(e: KeyboardEvent, hotkey: Hotkey, enabled?: Trigger): boolean {
|
|
12
|
-
if (typeof enabled === 'function') {
|
|
13
|
-
return enabled(e, hotkey)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return enabled === true || enabled === undefined
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function isKeyboardEventTriggeredByInput(ev: KeyboardEvent): boolean {
|
|
20
|
-
return isHotkeyEnabledOnTag(ev, ['input', 'textarea', 'select'])
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function isHotkeyEnabledOnTag(
|
|
24
|
-
{ target }: KeyboardEvent,
|
|
25
|
-
enabledOnTags: readonly FormTags[] | boolean = false
|
|
26
|
-
): boolean {
|
|
27
|
-
const targetTagName = target && (target as HTMLElement).tagName
|
|
28
|
-
|
|
29
|
-
if (isReadonlyArray(enabledOnTags)) {
|
|
30
|
-
return Boolean(
|
|
31
|
-
targetTagName && enabledOnTags && enabledOnTags.some((tag) => tag.toLowerCase() === targetTagName.toLowerCase())
|
|
32
|
-
)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return Boolean(targetTagName && enabledOnTags && enabledOnTags)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function isScopeActive(activeScopes: string[], scopes?: Scopes): boolean {
|
|
39
|
-
if (activeScopes.length === 0 && scopes) {
|
|
40
|
-
console.warn(
|
|
41
|
-
'A hotkey has the "scopes" option set, however no active scopes were found. If you want to use the global scopes feature, you need to wrap your app in a <HotkeysProvider>'
|
|
42
|
-
)
|
|
43
|
-
|
|
44
|
-
return true
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (!scopes) {
|
|
48
|
-
return true
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return activeScopes.some((scope) => scopes.includes(scope)) || activeScopes.includes('*')
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export const isHotkeyMatchingKeyboardEvent = (e: KeyboardEvent, hotkey: Hotkey, ignoreModifiers = false): boolean => {
|
|
55
|
-
const { alt, meta, mod, shift, ctrl, keys, useKey } = hotkey
|
|
56
|
-
const { code, key: producedKey, ctrlKey, metaKey, shiftKey, altKey } = e
|
|
57
|
-
|
|
58
|
-
const mappedCode = mapKey(code)
|
|
59
|
-
|
|
60
|
-
if (useKey && keys?.length === 1 && keys.includes(producedKey)) {
|
|
61
|
-
return true
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (
|
|
65
|
-
!keys?.includes(mappedCode) &&
|
|
66
|
-
!['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(mappedCode)
|
|
67
|
-
) {
|
|
68
|
-
return false
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (!ignoreModifiers) {
|
|
72
|
-
// We check the pressed keys for compatibility with the keyup event. In keyup events the modifier flags are not set.
|
|
73
|
-
if (alt !== altKey && mappedCode !== 'alt') {
|
|
74
|
-
return false
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (shift !== shiftKey && mappedCode !== 'shift') {
|
|
78
|
-
return false
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Mod is a special key name that is checking for meta on macOS and ctrl on other platforms
|
|
82
|
-
if (mod) {
|
|
83
|
-
if (!metaKey && !ctrlKey) {
|
|
84
|
-
return false
|
|
85
|
-
}
|
|
86
|
-
} else {
|
|
87
|
-
if (meta !== metaKey && mappedCode !== 'meta' && mappedCode !== 'os') {
|
|
88
|
-
return false
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (ctrl !== ctrlKey && mappedCode !== 'ctrl' && mappedCode !== 'control') {
|
|
92
|
-
return false
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// All modifiers are correct, now check the key
|
|
98
|
-
// If the key is set, we check for the key
|
|
99
|
-
if (keys && keys.length === 1 && keys.includes(mappedCode)) {
|
|
100
|
-
return true
|
|
101
|
-
} else if (keys) {
|
|
102
|
-
// Check if all keys are present in pressedDownKeys set
|
|
103
|
-
return isHotkeyPressed(keys)
|
|
104
|
-
} else if (!keys) {
|
|
105
|
-
// If the key is not set, we only listen for modifiers, that check went alright, so we return true
|
|
106
|
-
return true
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// There is nothing that matches.
|
|
110
|
-
return false
|
|
111
|
-
}
|