react-hotkeys-hook 4.4.0 → 4.4.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/README.md CHANGED
@@ -80,7 +80,7 @@ export const ExampleComponent = () => {
80
80
 
81
81
  #### Changing a scope's active state
82
82
 
83
- You can change the active state of a scope using the `deactivateScope`, `activateScope` and `toggleScope` functions
83
+ You can change the active state of a scope using the `disableScope`, `enableScope` and `toggleScope` functions
84
84
  returned by the `useHotkeysContext()` hook. Note that you have to have your app wrapped in a `<HotkeysProvider>` component.
85
85
 
86
86
  ```jsx harmony
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "react-hotkeys-hook",
3
3
  "description": "React hook for handling keyboard shortcuts",
4
- "version": "4.4.0",
5
- "repository": "https://JohannesKlauss@github.com/JohannesKlauss/react-keymap-hook.git",
4
+ "version": "4.4.2",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/JohannesKlauss/react-keymap-hook.git"
8
+ },
6
9
  "homepage": "https://johannesklauss.github.io/react-hotkeys-hook/",
7
10
  "author": "Johannes Klauss",
8
11
  "main": "dist/index.js",
@@ -75,31 +78,31 @@
75
78
  }
76
79
  },
77
80
  "devDependencies": {
78
- "@babel/core": "7.21.4",
81
+ "@babel/core": "7.23.2",
79
82
  "@babel/plugin-proposal-class-properties": "7.18.6",
80
- "@babel/plugin-transform-react-jsx": "7.21.0",
81
- "@babel/preset-env": "7.21.4",
82
- "@babel/preset-react": "7.18.6",
83
- "@babel/preset-typescript": "7.21.4",
84
- "@testing-library/jest-dom": "5.16.5",
83
+ "@babel/plugin-transform-react-jsx": "7.22.15",
84
+ "@babel/preset-env": "7.23.2",
85
+ "@babel/preset-react": "7.22.15",
86
+ "@babel/preset-typescript": "7.23.2",
87
+ "@testing-library/jest-dom": "5.17.0",
85
88
  "@testing-library/react": "14.0.0",
86
89
  "@testing-library/user-event": "14.4.3",
87
- "@types/jest": "29.5.0",
88
- "@types/react": "18.0.35",
89
- "@types/react-dom": "18.0.11",
90
- "@typescript-eslint/eslint-plugin": "5.58.0",
91
- "@typescript-eslint/parser": "5.58.0",
92
- "eslint": "^8.34.0",
90
+ "@types/jest": "29.5.6",
91
+ "@types/react": "18.2.33",
92
+ "@types/react-dom": "18.2.14",
93
+ "@typescript-eslint/eslint-plugin": "5.60.0",
94
+ "@typescript-eslint/parser": "5.60.0",
95
+ "eslint": "8.56.0",
93
96
  "eslint-plugin-prettier": "4.2.1",
94
- "eslint-plugin-react": "^7.32.2",
95
- "jest": "29.5.0",
96
- "jest-environment-jsdom": "29.5.0",
97
- "prettier": "2.8.7",
97
+ "eslint-plugin-react": "7.33.2",
98
+ "jest": "29.7.0",
99
+ "jest-environment-jsdom": "29.7.0",
100
+ "prettier": "2.8.8",
98
101
  "react": "18.2.0",
99
102
  "react-dom": "18.2.0",
100
103
  "react-test-renderer": "18.2.0",
101
104
  "tsdx": "0.14.1",
102
- "tslib": "2.5.0",
105
+ "tslib": "2.5.3",
103
106
  "typescript": "5.0.4"
104
107
  },
105
108
  "peerDependencies": {
package/src/index.ts CHANGED
@@ -1,7 +1,16 @@
1
1
  import useHotkeys from './useHotkeys'
2
- import type { Options } from './types'
2
+ import type { Options, Keys, HotkeyCallback } from './types'
3
3
  import { HotkeysProvider, useHotkeysContext } from './HotkeysProvider'
4
4
  import { isHotkeyPressed } from './isHotkeyPressed'
5
5
  import useRecordHotkeys from './useRecordHotkeys'
6
6
 
7
- export { useHotkeys, useRecordHotkeys, useHotkeysContext, isHotkeyPressed, HotkeysProvider, Options }
7
+ export {
8
+ useHotkeys,
9
+ useRecordHotkeys,
10
+ useHotkeysContext,
11
+ isHotkeyPressed,
12
+ HotkeysProvider,
13
+ Options,
14
+ Keys,
15
+ HotkeyCallback,
16
+ }
@@ -29,8 +29,13 @@ import { isHotkeyModifier, mapKey } from './parseHotkeys'
29
29
 
30
30
  const currentlyPressedKeys: Set<string> = new Set<string>()
31
31
 
32
- export function isHotkeyPressed(key: string | string[], splitKey = ','): boolean {
33
- const hotkeyArray = Array.isArray(key) ? key : key.split(splitKey)
32
+ // https://github.com/microsoft/TypeScript/issues/17002
33
+ export function isReadonlyArray(value: unknown): value is readonly unknown[] {
34
+ return Array.isArray(value)
35
+ }
36
+
37
+ export function isHotkeyPressed(key: string | readonly string[], splitKey = ','): boolean {
38
+ const hotkeyArray = isReadonlyArray(key) ? key : key.split(splitKey)
34
39
 
35
40
  return hotkeyArray.every((hotkey) => currentlyPressedKeys.has(hotkey.trim().toLowerCase()))
36
41
  }
package/src/types.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import type { DependencyList } from 'react'
2
2
 
3
3
  export type FormTags = 'input' | 'textarea' | 'select' | 'INPUT' | 'TEXTAREA' | 'SELECT'
4
- export type Keys = string | string[]
5
- export type Scopes = string | string[]
4
+ export type Keys = string | readonly string[]
5
+ export type Scopes = string | readonly string[]
6
6
 
7
7
  export type RefType<T> = T | null
8
8
 
@@ -15,7 +15,7 @@ export type KeyboardModifiers = {
15
15
  }
16
16
 
17
17
  export type Hotkey = KeyboardModifiers & {
18
- keys?: string[]
18
+ keys?: readonly string[]
19
19
  scopes?: Scopes
20
20
  description?: string
21
21
  }
@@ -28,7 +28,7 @@ export type Trigger = boolean | ((keyboardEvent: KeyboardEvent, hotkeysEvent: Ho
28
28
 
29
29
  export type Options = {
30
30
  enabled?: Trigger // Main setting that determines if the hotkey is enabled or not. (Default: true)
31
- enableOnFormTags?: FormTags[] | boolean // Enable hotkeys on a list of tags. (Default: false)
31
+ enableOnFormTags?: readonly FormTags[] | boolean // Enable hotkeys on a list of tags. (Default: false)
32
32
  enableOnContentEditable?: boolean // Enable hotkeys on tags with contentEditable props. (Default: false)
33
33
  ignoreEventWhen?: (e: KeyboardEvent) => boolean // Ignore evenets based on a condition (Default: undefined)
34
34
  combinationKey?: string // Character to split keys in hotkeys combinations. (Default: +)
package/src/useHotkeys.ts CHANGED
@@ -12,7 +12,7 @@ import {
12
12
  import { useHotkeysContext } from './HotkeysProvider'
13
13
  import { useBoundHotkeysProxy } from './BoundHotkeysProxyProvider'
14
14
  import useDeepEqualMemo from './useDeepEqualMemo'
15
- import { pushToCurrentlyPressedKeys, removeFromCurrentlyPressedKeys } from './isHotkeyPressed'
15
+ import { isReadonlyArray, pushToCurrentlyPressedKeys, removeFromCurrentlyPressedKeys } from './isHotkeyPressed'
16
16
 
17
17
  const stopPropagation = (e: KeyboardEvent): void => {
18
18
  e.stopPropagation()
@@ -36,7 +36,7 @@ export default function useHotkeys<T extends HTMLElement>(
36
36
  : !(dependencies instanceof Array)
37
37
  ? (dependencies as Options)
38
38
  : undefined
39
- const _keys: string = keys instanceof Array ? keys.join(_options?.splitKey) : keys
39
+ const _keys: string = isReadonlyArray(keys) ? keys.join(_options?.splitKey) : keys
40
40
  const _deps: DependencyList | undefined =
41
41
  options instanceof Array ? options : dependencies instanceof Array ? dependencies : undefined
42
42
 
package/src/validators.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { FormTags, Hotkey, Scopes, Trigger } from './types'
2
- import { isHotkeyPressed } from './isHotkeyPressed'
2
+ import { isHotkeyPressed, isReadonlyArray } from './isHotkeyPressed'
3
3
  import { mapKey } from './parseHotkeys'
4
4
 
5
5
  export function maybePreventDefault(e: KeyboardEvent, hotkey: Hotkey, preventDefault?: Trigger): void {
@@ -20,10 +20,13 @@ export function isKeyboardEventTriggeredByInput(ev: KeyboardEvent): boolean {
20
20
  return isHotkeyEnabledOnTag(ev, ['input', 'textarea', 'select'])
21
21
  }
22
22
 
23
- export function isHotkeyEnabledOnTag({ target }: KeyboardEvent, enabledOnTags: FormTags[] | boolean = false): boolean {
23
+ export function isHotkeyEnabledOnTag(
24
+ { target }: KeyboardEvent,
25
+ enabledOnTags: readonly FormTags[] | boolean = false
26
+ ): boolean {
24
27
  const targetTagName = target && (target as HTMLElement).tagName
25
28
 
26
- if (enabledOnTags instanceof Array) {
29
+ if (isReadonlyArray(enabledOnTags)) {
27
30
  return Boolean(
28
31
  targetTagName && enabledOnTags && enabledOnTags.some((tag) => tag.toLowerCase() === targetTagName.toLowerCase())
29
32
  )
@@ -55,6 +58,10 @@ export const isHotkeyMatchingKeyboardEvent = (e: KeyboardEvent, hotkey: Hotkey,
55
58
  const keyCode = mapKey(code)
56
59
  const pressedKey = pressedKeyUppercase.toLowerCase()
57
60
 
61
+ if (!keys?.includes(keyCode) && !['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(keyCode)) {
62
+ return false;
63
+ }
64
+
58
65
  if (!ignoreModifiers) {
59
66
  // We check the pressed keys for compatibility with the keyup event. In keyup events the modifier flags are not set.
60
67
  if (alt === !altKey && pressedKey !== 'alt') {
@@ -1,14 +0,0 @@
1
- import { ReactNode } from 'react';
2
- import { Hotkey } from './types';
3
- declare type BoundHotkeysProxyProviderType = {
4
- addHotkey: (hotkey: Hotkey) => void;
5
- removeHotkey: (hotkey: Hotkey) => void;
6
- };
7
- export declare const useBoundHotkeysProxy: () => BoundHotkeysProxyProviderType | undefined;
8
- interface Props {
9
- children: ReactNode;
10
- addHotkey: (hotkey: Hotkey) => void;
11
- removeHotkey: (hotkey: Hotkey) => void;
12
- }
13
- export default function BoundHotkeysProxyProviderProvider({ addHotkey, removeHotkey, children }: Props): JSX.Element;
14
- export {};
@@ -1,16 +0,0 @@
1
- import { Hotkey } from './types';
2
- import { ReactNode } from 'react';
3
- export declare type HotkeysContextType = {
4
- hotkeys: ReadonlyArray<Hotkey>;
5
- enabledScopes: string[];
6
- toggleScope: (scope: string) => void;
7
- enableScope: (scope: string) => void;
8
- disableScope: (scope: string) => void;
9
- };
10
- export declare const useHotkeysContext: () => HotkeysContextType;
11
- interface Props {
12
- initiallyActiveScopes?: string[];
13
- children: ReactNode;
14
- }
15
- export declare const HotkeysProvider: ({ initiallyActiveScopes, children }: Props) => JSX.Element;
16
- export {};
@@ -1 +0,0 @@
1
- export default function deepEqual(x: any, y: any): boolean;
package/dist/index.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import useHotkeys from './useHotkeys';
2
- import type { Options } from './types';
3
- import { HotkeysProvider, useHotkeysContext } from './HotkeysProvider';
4
- import { isHotkeyPressed } from './isHotkeyPressed';
5
- import useRecordHotkeys from './useRecordHotkeys';
6
- export { useHotkeys, useRecordHotkeys, useHotkeysContext, isHotkeyPressed, HotkeysProvider, Options };
package/dist/index.js DELETED
@@ -1,8 +0,0 @@
1
-
2
- 'use strict'
3
-
4
- if (process.env.NODE_ENV === 'production') {
5
- module.exports = require('./react-hotkeys-hook.cjs.production.min.js')
6
- } else {
7
- module.exports = require('./react-hotkeys-hook.cjs.development.js')
8
- }
@@ -1,3 +0,0 @@
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;
@@ -1,5 +0,0 @@
1
- import { Hotkey } from './types';
2
- export declare function mapKey(key: string): string;
3
- export declare function isHotkeyModifier(key: string): boolean;
4
- export declare function parseKeysHookInput(keys: string, splitKey?: string): string[];
5
- export declare function parseHotkey(hotkey: string, combinationKey?: string, description?: string): Hotkey;