react-hotkeys-hook 5.0.0-0 → 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.
Files changed (35) hide show
  1. package/package.json +13 -85
  2. package/{dist → packages/react-hotkeys-hook/dist}/BoundHotkeysProxyProvider.d.ts +14 -14
  3. package/{dist → packages/react-hotkeys-hook/dist}/HotkeysProvider.d.ts +16 -16
  4. package/packages/react-hotkeys-hook/dist/deepEqual.d.ts +1 -0
  5. package/{dist → packages/react-hotkeys-hook/dist}/index.d.ts +6 -6
  6. package/packages/react-hotkeys-hook/dist/index.js +220 -0
  7. package/{dist → packages/react-hotkeys-hook/dist}/isHotkeyPressed.d.ts +4 -4
  8. package/packages/react-hotkeys-hook/dist/parseHotkeys.d.ts +5 -0
  9. package/packages/react-hotkeys-hook/dist/types.d.ts +45 -0
  10. package/{dist → packages/react-hotkeys-hook/dist}/useDeepEqualMemo.d.ts +1 -1
  11. package/{dist → packages/react-hotkeys-hook/dist}/useHotkeys.d.ts +2 -3
  12. package/{dist → packages/react-hotkeys-hook/dist}/useRecordHotkeys.d.ts +6 -5
  13. package/{dist → packages/react-hotkeys-hook/dist}/validators.d.ts +8 -7
  14. package/dist/deepEqual.d.ts +0 -1
  15. package/dist/index.js +0 -8
  16. package/dist/parseHotkeys.d.ts +0 -5
  17. package/dist/react-hotkeys-hook.cjs.development.js +0 -514
  18. package/dist/react-hotkeys-hook.cjs.development.js.map +0 -1
  19. package/dist/react-hotkeys-hook.cjs.production.min.js +0 -2
  20. package/dist/react-hotkeys-hook.cjs.production.min.js.map +0 -1
  21. package/dist/react-hotkeys-hook.esm.js +0 -508
  22. package/dist/react-hotkeys-hook.esm.js.map +0 -1
  23. package/dist/setupTests.d.ts +0 -1
  24. package/dist/types.d.ts +0 -36
  25. package/src/BoundHotkeysProxyProvider.tsx +0 -27
  26. package/src/HotkeysProvider.tsx +0 -81
  27. package/src/deepEqual.ts +0 -8
  28. package/src/index.ts +0 -16
  29. package/src/isHotkeyPressed.ts +0 -73
  30. package/src/parseHotkeys.ts +0 -58
  31. package/src/types.ts +0 -45
  32. package/src/useDeepEqualMemo.ts +0 -12
  33. package/src/useHotkeys.ts +0 -172
  34. package/src/useRecordHotkeys.ts +0 -47
  35. package/src/validators.ts +0 -106
@@ -1,47 +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.key === 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.key))
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
- return [keys, { start, stop, isRecording }] as const
47
- }
package/src/validators.ts DELETED
@@ -1,106 +0,0 @@
1
- import { FormTags, Hotkey, Scopes, Trigger } from './types'
2
- import { isHotkeyPressed, isReadonlyArray } from './isHotkeyPressed'
3
-
4
- export function maybePreventDefault(e: KeyboardEvent, hotkey: Hotkey, preventDefault?: Trigger): void {
5
- if ((typeof preventDefault === 'function' && preventDefault(e, hotkey)) || preventDefault === true) {
6
- e.preventDefault()
7
- }
8
- }
9
-
10
- export function isHotkeyEnabled(e: KeyboardEvent, hotkey: Hotkey, enabled?: Trigger): boolean {
11
- if (typeof enabled === 'function') {
12
- return enabled(e, hotkey)
13
- }
14
-
15
- return enabled === true || enabled === undefined
16
- }
17
-
18
- export function isKeyboardEventTriggeredByInput(ev: KeyboardEvent): boolean {
19
- return isHotkeyEnabledOnTag(ev, ['input', 'textarea', 'select'])
20
- }
21
-
22
- export function isHotkeyEnabledOnTag(
23
- { target }: KeyboardEvent,
24
- enabledOnTags: readonly FormTags[] | boolean = false
25
- ): boolean {
26
- const targetTagName = target && (target as HTMLElement).tagName
27
-
28
- if (isReadonlyArray(enabledOnTags)) {
29
- return Boolean(
30
- targetTagName && enabledOnTags && enabledOnTags.some((tag) => tag.toLowerCase() === targetTagName.toLowerCase())
31
- )
32
- }
33
-
34
- return Boolean(targetTagName && enabledOnTags && enabledOnTags === true)
35
- }
36
-
37
- export function isScopeActive(activeScopes: string[], scopes?: Scopes): boolean {
38
- if (activeScopes.length === 0 && scopes) {
39
- console.warn(
40
- '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>'
41
- )
42
-
43
- return true
44
- }
45
-
46
- if (!scopes) {
47
- return true
48
- }
49
-
50
- return activeScopes.some((scope) => scopes.includes(scope)) || activeScopes.includes('*')
51
- }
52
-
53
- export const isHotkeyMatchingKeyboardEvent = (e: KeyboardEvent, hotkey: Hotkey, ignoreModifiers = false): boolean => {
54
- const { alt, meta, mod, shift, ctrl, keys } = hotkey
55
- const { key: pressedKeyUppercase, ctrlKey, metaKey, shiftKey, altKey } = e
56
-
57
- const pressedKey = pressedKeyUppercase.toLowerCase()
58
-
59
- if (
60
- !keys?.includes(pressedKey) &&
61
- !['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(pressedKey)
62
- ) {
63
- return false
64
- }
65
-
66
- if (!ignoreModifiers) {
67
- // We check the pressed keys for compatibility with the keyup event. In keyup events the modifier flags are not set.
68
- if (alt === !altKey && pressedKey !== 'alt') {
69
- return false
70
- }
71
-
72
- if (shift === !shiftKey && pressedKey !== 'shift') {
73
- return false
74
- }
75
-
76
- // Mod is a special key name that is checking for meta on macOS and ctrl on other platforms
77
- if (mod) {
78
- if (!metaKey && !ctrlKey) {
79
- return false
80
- }
81
- } else {
82
- if (meta === !metaKey && pressedKey !== 'meta' && pressedKey !== 'os') {
83
- return false
84
- }
85
-
86
- if (ctrl === !ctrlKey && pressedKey !== 'ctrl' && pressedKey !== 'control') {
87
- return false
88
- }
89
- }
90
- }
91
-
92
- // All modifiers are correct, now check the key
93
- // If the key is set, we check for the key
94
- if (keys && keys.length === 1 && keys.includes(pressedKey)) {
95
- return true
96
- } else if (keys) {
97
- // Check if all keys are present in pressedDownKeys set
98
- return isHotkeyPressed(keys)
99
- } else if (!keys) {
100
- // If the key is not set, we only listen for modifiers, that check went alright, so we return true
101
- return true
102
- }
103
-
104
- // There is nothing that matches.
105
- return false
106
- }