react-hotkeys-hook 4.0.0-5 → 4.0.0-7

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.
@@ -0,0 +1,3 @@
1
+ export declare function isHotkeyPressed(key: string | string[], splitKey?: string): boolean;
2
+ export declare function pushToCurrentlyPressedKeys(key: string | string[]): void;
3
+ export declare function removeFromCurrentlyPressedKeys(key: string | string[]): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-hotkeys-hook",
3
- "version": "4.0.0-5",
3
+ "version": "4.0.0-7",
4
4
  "repository": "https://JohannesKlauss@github.com/JohannesKlauss/react-keymap-hook.git",
5
5
  "homepage": "https://johannesklauss.github.io/react-hotkeys-hook/",
6
6
  "author": "Johannes Klauss",
@@ -4,19 +4,19 @@ import BoundHotkeysProxyProviderProvider from './BoundHotkeysProxyProvider'
4
4
 
5
5
  export type HotkeysContextType = {
6
6
  hotkeys: ReadonlyArray<Hotkey>
7
- activeScopes: string[]
7
+ enabledScopes: string[]
8
8
  toggleScope: (scope: string) => void
9
- activateScope: (scope: string) => void
10
- deactivateScope: (scope: string) => void
9
+ enableScope: (scope: string) => void
10
+ disableScope: (scope: string) => void
11
11
  }
12
12
 
13
13
  // The context is only needed for special features like global scoping, so we use a graceful default fallback
14
14
  const HotkeysContext = createContext<HotkeysContextType>({
15
15
  hotkeys: [],
16
- activeScopes: [], // This array has to be empty instead of containing '*' as default, to check if the provider is set or not
16
+ enabledScopes: [], // This array has to be empty instead of containing '*' as default, to check if the provider is set or not
17
17
  toggleScope: () => {},
18
- activateScope: () => {},
19
- deactivateScope: () => {},
18
+ enableScope: () => {},
19
+ disableScope: () => {},
20
20
  })
21
21
 
22
22
  export const useHotkeysContext = () => {
@@ -34,7 +34,7 @@ export const HotkeysProvider = ({initiallyActiveScopes = ['*'], children}: Props
34
34
 
35
35
  const isAllActive = useMemo(() => internalActiveScopes.includes('*'), [internalActiveScopes])
36
36
 
37
- const activateScope = (scope: string) => {
37
+ const enableScope = (scope: string) => {
38
38
  if (isAllActive) {
39
39
  setInternalActiveScopes([scope])
40
40
  } else {
@@ -42,7 +42,7 @@ export const HotkeysProvider = ({initiallyActiveScopes = ['*'], children}: Props
42
42
  }
43
43
  }
44
44
 
45
- const deactivateScope = (scope: string) => {
45
+ const disableScope = (scope: string) => {
46
46
  const scopes = internalActiveScopes.filter(s => s !== scope)
47
47
 
48
48
  if (scopes.length === 0) {
@@ -54,9 +54,9 @@ export const HotkeysProvider = ({initiallyActiveScopes = ['*'], children}: Props
54
54
 
55
55
  const toggleScope = (scope: string) => {
56
56
  if (internalActiveScopes.includes(scope)) {
57
- deactivateScope(scope)
57
+ disableScope(scope)
58
58
  } else {
59
- activateScope(scope)
59
+ enableScope(scope)
60
60
  }
61
61
  }
62
62
 
@@ -69,7 +69,7 @@ export const HotkeysProvider = ({initiallyActiveScopes = ['*'], children}: Props
69
69
  }
70
70
 
71
71
  return (
72
- <HotkeysContext.Provider value={{activeScopes: internalActiveScopes, hotkeys: boundHotkeys, activateScope, deactivateScope, toggleScope}}>
72
+ <HotkeysContext.Provider value={{enabledScopes: internalActiveScopes, hotkeys: boundHotkeys, enableScope, disableScope, toggleScope}}>
73
73
  <BoundHotkeysProxyProviderProvider addHotkey={addBoundHotkey} removeHotkey={removeBoundHotkey}>
74
74
  {children}
75
75
  </BoundHotkeysProxyProviderProvider>
@@ -2,6 +2,15 @@ import { Hotkey, KeyboardModifiers, Keys } from './types'
2
2
 
3
3
  const reservedModifierKeywords = ['ctrl', 'shift', 'alt', 'meta', 'mod']
4
4
 
5
+ const mappedKeys: Record<string, string> = {
6
+ esc: 'escape',
7
+ return: 'enter',
8
+ left: 'arrowleft',
9
+ up: 'arrowup',
10
+ right: 'arrowright',
11
+ down: 'arrowdown',
12
+ }
13
+
5
14
  export function parseKeysHookInput(keys: Keys, splitKey: string = ','): string[] {
6
15
  if (typeof keys === 'string') {
7
16
  return keys.split(splitKey)
@@ -15,8 +24,7 @@ export function parseHotkey(hotkey: string, combinationKey: string = '+'): Hotke
15
24
  .toLocaleLowerCase()
16
25
  .split(combinationKey)
17
26
  .map(k => k.trim())
18
- .map(k => k === 'esc' ? 'escape' : k)
19
- .map(k => k === 'return' ? 'enter' : k)
27
+ .map(k => mappedKeys[k] || k)
20
28
 
21
29
  const modifiers: KeyboardModifiers = {
22
30
  alt: keys.includes('alt'),
package/src/useHotkeys.ts CHANGED
@@ -34,11 +34,11 @@ export default function useHotkeys<T extends HTMLElement>(
34
34
  const cb = useCallback(callback, [..._deps])
35
35
  const memoisedOptions = useDeepEqualMemo(_options)
36
36
 
37
- const { activeScopes } = useHotkeysContext()
37
+ const { enabledScopes } = useHotkeysContext()
38
38
  const proxy = useBoundHotkeysProxy()
39
39
 
40
40
  useLayoutEffect(() => {
41
- if (memoisedOptions?.enabled === false || !isScopeActive(activeScopes, memoisedOptions?.scopes)) {
41
+ if (memoisedOptions?.enabled === false || !isScopeActive(enabledScopes, memoisedOptions?.scopes)) {
42
42
  return
43
43
  }
44
44
 
@@ -117,7 +117,7 @@ export default function useHotkeys<T extends HTMLElement>(
117
117
  parseKeysHookInput(keys, memoisedOptions?.splitKey).forEach((key) => proxy.removeHotkey(parseHotkey(key, memoisedOptions?.combinationKey)))
118
118
  }
119
119
  }
120
- }, [keys, cb, memoisedOptions, activeScopes])
120
+ }, [keys, cb, memoisedOptions, enabledScopes])
121
121
 
122
122
  return ref
123
123
  }