react-hotkeys-hook 4.0.0-6 → 4.0.0
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/README.md +6 -11
- package/dist/HotkeysProvider.d.ts +3 -3
- package/dist/index.js +5 -5
- package/dist/index.js.map +2 -2
- package/package.json +1 -1
- package/src/HotkeysProvider.tsx +11 -11
- package/src/useHotkeys.ts +3 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-hotkeys-hook",
|
|
3
|
-
"version": "4.0.0
|
|
3
|
+
"version": "4.0.0",
|
|
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",
|
package/src/HotkeysProvider.tsx
CHANGED
|
@@ -4,19 +4,19 @@ import BoundHotkeysProxyProviderProvider from './BoundHotkeysProxyProvider'
|
|
|
4
4
|
|
|
5
5
|
export type HotkeysContextType = {
|
|
6
6
|
hotkeys: ReadonlyArray<Hotkey>
|
|
7
|
-
|
|
7
|
+
enabledScopes: string[]
|
|
8
8
|
toggleScope: (scope: string) => void
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
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
|
-
|
|
19
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
57
|
+
disableScope(scope)
|
|
58
58
|
} else {
|
|
59
|
-
|
|
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={{
|
|
72
|
+
<HotkeysContext.Provider value={{enabledScopes: internalActiveScopes, hotkeys: boundHotkeys, enableScope, disableScope, toggleScope}}>
|
|
73
73
|
<BoundHotkeysProxyProviderProvider addHotkey={addBoundHotkey} removeHotkey={removeBoundHotkey}>
|
|
74
74
|
{children}
|
|
75
75
|
</BoundHotkeysProxyProviderProvider>
|
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 {
|
|
37
|
+
const { enabledScopes } = useHotkeysContext()
|
|
38
38
|
const proxy = useBoundHotkeysProxy()
|
|
39
39
|
|
|
40
40
|
useLayoutEffect(() => {
|
|
41
|
-
if (memoisedOptions?.enabled === false || !isScopeActive(
|
|
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,
|
|
120
|
+
}, [keys, cb, memoisedOptions, enabledScopes])
|
|
121
121
|
|
|
122
122
|
return ref
|
|
123
123
|
}
|