react-hotkeys-hook 3.4.7 → 4.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/README.md +113 -40
- package/dist/BoundHotkeysProxyProvider.d.ts +14 -0
- package/dist/HotkeysProvider.d.ts +16 -0
- package/dist/index.d.ts +4 -7
- package/dist/index.js +2 -8
- package/dist/index.js.map +7 -0
- package/dist/parseHotkeys.d.ts +3 -0
- package/dist/types.d.ts +32 -0
- package/dist/useHotkeys.d.ts +2 -18
- package/dist/validators.d.ts +7 -0
- package/package.json +15 -24
- package/src/BoundHotkeysProxyProvider.tsx +23 -0
- package/src/HotkeysProvider.tsx +78 -0
- package/src/index.ts +9 -6
- package/src/isHotkeyPressed.ts +51 -0
- package/src/parseHotkeys.ts +33 -0
- package/src/types.ts +41 -0
- package/src/useHotkeys.ts +99 -77
- package/src/validators.ts +92 -0
- package/dist/react-hotkeys-hook.cjs.development.js +0 -93
- 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 -87
- package/dist/react-hotkeys-hook.esm.js.map +0 -1
- package/dist/useIsHotkeyPressed.d.ts +0 -7
- package/src/index.test.tsx +0 -230
- package/src/useIsHotkeyPressed.ts +0 -8
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Hotkey } from './types'
|
|
2
|
+
import { parseHotkey } from './parseHotkeys'
|
|
3
|
+
import isEqual from 'lodash/isEqual'
|
|
4
|
+
|
|
5
|
+
const currentlyPressedKeys: Set<Hotkey> = new Set<Hotkey>()
|
|
6
|
+
|
|
7
|
+
export function isHotkeyPressed(key: string | string[], splitKey: string = ','): boolean {
|
|
8
|
+
const hotkeyArray = Array.isArray(key) ? key : key.split(splitKey)
|
|
9
|
+
|
|
10
|
+
return hotkeyArray.every((hotkey) => {
|
|
11
|
+
const parsedHotkey = parseHotkey(hotkey)
|
|
12
|
+
|
|
13
|
+
for (const pressedHotkey of currentlyPressedKeys) {
|
|
14
|
+
if (isEqual(parsedHotkey, pressedHotkey)) {
|
|
15
|
+
return true
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function pushToCurrentlyPressedKeys(key: string | string[]): void {
|
|
22
|
+
const hotkeyArray = Array.isArray(key) ? key : [key]
|
|
23
|
+
|
|
24
|
+
hotkeyArray.forEach(hotkey => currentlyPressedKeys.add(parseHotkey(hotkey)))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function removeFromCurrentlyPressedKeys(key: string | string[]): void {
|
|
28
|
+
const hotkeyArray = Array.isArray(key) ? key : [key]
|
|
29
|
+
|
|
30
|
+
hotkeyArray.forEach((hotkey) => {
|
|
31
|
+
const parsedHotkey = parseHotkey(hotkey)
|
|
32
|
+
|
|
33
|
+
for (const pressedHotkey of currentlyPressedKeys) {
|
|
34
|
+
if (pressedHotkey.keys?.every((key) => parsedHotkey.keys?.includes(key))) {
|
|
35
|
+
currentlyPressedKeys.delete(pressedHotkey)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
(() => {
|
|
42
|
+
window.addEventListener('DOMContentLoaded', () => {
|
|
43
|
+
document.addEventListener('keydown', e => {
|
|
44
|
+
pushToCurrentlyPressedKeys(e.key)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
document.addEventListener('keyup', e => {
|
|
48
|
+
removeFromCurrentlyPressedKeys(e.key)
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
})()
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Hotkey, KeyboardModifiers, Keys } from './types'
|
|
2
|
+
|
|
3
|
+
const reservedModifierKeywords = ['ctrl', 'shift', 'alt', 'meta', 'mod']
|
|
4
|
+
|
|
5
|
+
export function parseKeysHookInput(keys: Keys, splitKey: string = ','): string[] {
|
|
6
|
+
if (typeof keys === 'string') {
|
|
7
|
+
return keys.split(splitKey)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return keys
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function parseHotkey(hotkey: string, combinationKey: string = '+'): Hotkey {
|
|
14
|
+
const keys = hotkey
|
|
15
|
+
.toLocaleLowerCase()
|
|
16
|
+
.split(combinationKey)
|
|
17
|
+
.map((k) => k.trim())
|
|
18
|
+
|
|
19
|
+
const modifiers: KeyboardModifiers = {
|
|
20
|
+
alt: keys.includes('alt'),
|
|
21
|
+
ctrl: keys.includes('ctrl'),
|
|
22
|
+
shift: keys.includes('shift'),
|
|
23
|
+
meta: keys.includes('meta'),
|
|
24
|
+
mod: keys.includes('mod'),
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const singleCharKeys = keys.filter((k) => !reservedModifierKeywords.includes(k))
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
...modifiers,
|
|
31
|
+
keys: singleCharKeys,
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { DependencyList } from 'react'
|
|
2
|
+
|
|
3
|
+
export type FormTags = 'input' | 'textarea' | 'select' | 'INPUT' | 'TEXTAREA' | 'SELECT'
|
|
4
|
+
export type Keys = string | string[]
|
|
5
|
+
export type Scopes = string | string[]
|
|
6
|
+
|
|
7
|
+
export type RefType<T> = T | null
|
|
8
|
+
|
|
9
|
+
export type KeyboardModifiers = {
|
|
10
|
+
alt?: boolean
|
|
11
|
+
ctrl?: boolean
|
|
12
|
+
meta?: boolean
|
|
13
|
+
shift?: boolean
|
|
14
|
+
mod?: boolean
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type Hotkey = KeyboardModifiers & {
|
|
18
|
+
keys?: string[]
|
|
19
|
+
scopes?: Scopes
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type HotkeysEvent = Hotkey & {}
|
|
23
|
+
|
|
24
|
+
export type HotkeyCallback = (keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => void
|
|
25
|
+
|
|
26
|
+
export type Trigger = boolean | ((keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => boolean)
|
|
27
|
+
|
|
28
|
+
export type Options = {
|
|
29
|
+
enabled?: Trigger // Main setting that determines if the hotkey is enabled or not. (Default: true)
|
|
30
|
+
enableOnFormTags?: FormTags[] | boolean // Enable hotkeys on a list of tags. (Default: false)
|
|
31
|
+
enableOnContentEditable?: boolean // Enable hotkeys on tags with contentEditable props. (Default: false)
|
|
32
|
+
combinationKey?: string // Character to split keys in hotkeys combinations. (Default: +)
|
|
33
|
+
splitKey?: string // Character to separate different hotkeys. (Default: ,)
|
|
34
|
+
scopes?: Scopes // Scope
|
|
35
|
+
keyup?: boolean // Trigger on keyup event? (Default: undefined)
|
|
36
|
+
keydown?: boolean // Trigger on keydown event? (Default: true)
|
|
37
|
+
preventDefault?: Trigger // Prevent default browser behavior? (Default: false)
|
|
38
|
+
description?: string // Use this option to describe what the hotkey does. (Default: undefined)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type OptionsOrDependencyArray = Options | DependencyList
|
package/src/useHotkeys.ts
CHANGED
|
@@ -1,91 +1,113 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (options instanceof Array) {
|
|
36
|
-
deps = options;
|
|
37
|
-
options = undefined;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const {
|
|
41
|
-
enableOnTags,
|
|
42
|
-
filter,
|
|
43
|
-
keyup,
|
|
44
|
-
keydown,
|
|
45
|
-
filterPreventDefault = true,
|
|
46
|
-
enabled = true,
|
|
47
|
-
enableOnContentEditable = false,
|
|
48
|
-
} = options as Options || {};
|
|
49
|
-
const ref = useRef<T | null>(null);
|
|
50
|
-
|
|
51
|
-
// The return value of this callback determines if the browsers default behavior is prevented.
|
|
52
|
-
const memoisedCallback = useCallback((keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => {
|
|
53
|
-
if (filter && !filter(keyboardEvent)) {
|
|
54
|
-
return !filterPreventDefault;
|
|
55
|
-
}
|
|
1
|
+
import { HotkeyCallback, Keys, OptionsOrDependencyArray, RefType } from './types'
|
|
2
|
+
import { useCallback, useLayoutEffect, useRef } from 'react'
|
|
3
|
+
import { parseHotkey, parseKeysHookInput } from './parseHotkeys'
|
|
4
|
+
import {
|
|
5
|
+
isHotkeyEnabled,
|
|
6
|
+
isHotkeyEnabledOnTag,
|
|
7
|
+
isHotkeyMatchingKeyboardEvent,
|
|
8
|
+
isKeyboardEventTriggeredByInput,
|
|
9
|
+
isScopeActive,
|
|
10
|
+
maybePreventDefault,
|
|
11
|
+
} from './validators'
|
|
12
|
+
import { useHotkeysContext } from './HotkeysProvider'
|
|
13
|
+
import { useBoundHotkeysProxy } from './BoundHotkeysProxyProvider'
|
|
14
|
+
|
|
15
|
+
const stopPropagation = (e: KeyboardEvent): void => {
|
|
16
|
+
e.stopPropagation()
|
|
17
|
+
e.preventDefault()
|
|
18
|
+
e.stopImmediatePropagation()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default function useHotkeys<T extends HTMLElement>(
|
|
22
|
+
keys: Keys,
|
|
23
|
+
callback: HotkeyCallback,
|
|
24
|
+
options?: OptionsOrDependencyArray,
|
|
25
|
+
dependencies?: OptionsOrDependencyArray,
|
|
26
|
+
) {
|
|
27
|
+
const ref = useRef<RefType<T>>(null)
|
|
28
|
+
const { current: pressedDownKeys } = useRef<Set<string>>(new Set())
|
|
29
|
+
|
|
30
|
+
const _options = !(options instanceof Array) ? options : !(dependencies instanceof Array) ? dependencies : undefined
|
|
31
|
+
const _deps = options instanceof Array ? options : dependencies instanceof Array ? dependencies : []
|
|
32
|
+
|
|
33
|
+
const cb = useCallback(callback, [..._deps])
|
|
34
|
+
const ctx = useHotkeysContext()
|
|
56
35
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return true;
|
|
36
|
+
const proxy = useBoundHotkeysProxy()
|
|
37
|
+
|
|
38
|
+
useLayoutEffect(() => {
|
|
39
|
+
if (_options?.enabled === false || !isScopeActive(ctx.activeScopes, _options?.scopes)) {
|
|
40
|
+
return
|
|
63
41
|
}
|
|
64
42
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
43
|
+
const listener = (e: KeyboardEvent) => {
|
|
44
|
+
if (isKeyboardEventTriggeredByInput(e) && !isHotkeyEnabledOnTag(e, _options?.enableOnFormTags)) {
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (ref.current !== null && document.activeElement !== ref.current && !ref.current.contains(document.activeElement)) {
|
|
49
|
+
stopPropagation(e)
|
|
50
|
+
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (((e.target as HTMLElement)?.isContentEditable && !_options?.enableOnContentEditable)) {
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
parseKeysHookInput(keys, _options?.splitKey).forEach((key) => {
|
|
59
|
+
const hotkey = parseHotkey(key, _options?.combinationKey)
|
|
60
|
+
|
|
61
|
+
if (isHotkeyMatchingKeyboardEvent(e, hotkey, pressedDownKeys) || hotkey.keys?.includes('*')) {
|
|
62
|
+
maybePreventDefault(e, hotkey, _options?.preventDefault)
|
|
63
|
+
|
|
64
|
+
if (!isHotkeyEnabled(e, hotkey, _options?.enabled)) {
|
|
65
|
+
stopPropagation(e)
|
|
66
|
+
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
cb(e, hotkey)
|
|
71
|
+
}
|
|
72
|
+
})
|
|
68
73
|
}
|
|
69
74
|
|
|
70
|
-
|
|
71
|
-
|
|
75
|
+
const handleKeyDown = (event: KeyboardEvent) => {
|
|
76
|
+
pressedDownKeys.add(event.key.toLowerCase())
|
|
72
77
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
78
|
+
if ((_options?.keydown === undefined && _options?.keyup !== true) || _options?.keydown) {
|
|
79
|
+
listener(event)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const handleKeyUp = (event: KeyboardEvent) => {
|
|
84
|
+
pressedDownKeys.delete(event.key.toLowerCase())
|
|
76
85
|
|
|
77
|
-
|
|
86
|
+
if (_options?.keyup) {
|
|
87
|
+
listener(event)
|
|
88
|
+
}
|
|
78
89
|
}
|
|
79
90
|
|
|
80
|
-
//
|
|
81
|
-
|
|
82
|
-
|
|
91
|
+
// @ts-ignore
|
|
92
|
+
(ref.current || document).addEventListener('keyup', handleKeyUp);
|
|
93
|
+
// @ts-ignore
|
|
94
|
+
(ref.current || document).addEventListener('keydown', handleKeyDown)
|
|
95
|
+
|
|
96
|
+
if (proxy) {
|
|
97
|
+
parseKeysHookInput(keys, _options?.splitKey).forEach((key) => proxy.addHotkey(parseHotkey(key, _options?.combinationKey)))
|
|
83
98
|
}
|
|
84
99
|
|
|
85
|
-
|
|
100
|
+
return () => {
|
|
101
|
+
// @ts-ignore
|
|
102
|
+
(ref.current || document).removeEventListener('keyup', handleKeyUp);
|
|
103
|
+
// @ts-ignore
|
|
104
|
+
(ref.current || document).removeEventListener('keydown', handleKeyDown)
|
|
86
105
|
|
|
87
|
-
|
|
88
|
-
|
|
106
|
+
if (proxy) {
|
|
107
|
+
parseKeysHookInput(keys, _options?.splitKey).forEach((key) => proxy.removeHotkey(parseHotkey(key, _options?.combinationKey)))
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}, [keys, cb, _options])
|
|
89
111
|
|
|
90
|
-
return ref
|
|
112
|
+
return ref
|
|
91
113
|
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { FormTags, Hotkey, Scopes, Trigger } from './types'
|
|
2
|
+
|
|
3
|
+
export function maybePreventDefault(e: KeyboardEvent, hotkey: Hotkey, preventDefault?: Trigger): void {
|
|
4
|
+
if ((typeof preventDefault === 'function' && preventDefault(e, hotkey)) || preventDefault === true) {
|
|
5
|
+
e.preventDefault()
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function isHotkeyEnabled(e: KeyboardEvent, hotkey: Hotkey, enabled?: Trigger): boolean {
|
|
10
|
+
if (typeof enabled === 'function') {
|
|
11
|
+
return enabled(e, hotkey)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return enabled === true || enabled === undefined
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function isKeyboardEventTriggeredByInput(ev: KeyboardEvent): boolean {
|
|
18
|
+
return isHotkeyEnabledOnTag(ev, ['input', 'textarea', 'select'])
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isHotkeyEnabledOnTag({ target }: KeyboardEvent, enabledOnTags: FormTags[] | boolean = false): boolean {
|
|
22
|
+
const targetTagName = target && (target as HTMLElement).tagName
|
|
23
|
+
|
|
24
|
+
if (enabledOnTags instanceof Array) {
|
|
25
|
+
return Boolean(targetTagName && enabledOnTags && enabledOnTags.some(tag => tag.toLowerCase() === targetTagName.toLowerCase()))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return Boolean(targetTagName && enabledOnTags && enabledOnTags === true)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function isScopeActive(activeScopes: string[], scopes?: Scopes): boolean {
|
|
32
|
+
if (activeScopes.length === 0 && scopes) {
|
|
33
|
+
console.warn(
|
|
34
|
+
'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>'
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
return true
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!scopes) {
|
|
41
|
+
return true
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return activeScopes.some(scope => scopes.includes(scope)) || activeScopes.includes('*')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const isHotkeyMatchingKeyboardEvent = (e: KeyboardEvent, hotkey: Hotkey, pressedDownKeys: Set<string>): boolean => {
|
|
48
|
+
const { alt, ctrl, meta, mod, shift, keys } = hotkey
|
|
49
|
+
const { altKey, ctrlKey, metaKey, shiftKey, key: pressedKeyUppercase, code } = e
|
|
50
|
+
|
|
51
|
+
const keyCode = code.toLowerCase().replace('key', '')
|
|
52
|
+
const pressedKey = pressedKeyUppercase.toLowerCase()
|
|
53
|
+
|
|
54
|
+
if (altKey !== alt && pressedKey !== 'alt') {
|
|
55
|
+
return false
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (shiftKey !== shift && pressedKey !== 'shift') {
|
|
59
|
+
return false
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Mod is a special key name that is checking for meta on macOS and ctrl on other platforms
|
|
63
|
+
if (mod) {
|
|
64
|
+
if (!metaKey && !ctrlKey) {
|
|
65
|
+
return false
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
if (metaKey !== meta && keyCode !== 'meta') {
|
|
69
|
+
return false
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (ctrlKey !== ctrl && keyCode !== 'ctrl') {
|
|
73
|
+
return false
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// All modifiers are correct, now check the key
|
|
78
|
+
// If the key is set we check for the key
|
|
79
|
+
if (keys && keys.length === 1 && (keys.includes(pressedKey) || keys.includes(keyCode))) {
|
|
80
|
+
return true
|
|
81
|
+
} else if (keys) {
|
|
82
|
+
// Check if all keys are present in pressedDownKeys set
|
|
83
|
+
return keys.every(key => pressedDownKeys.has(key))
|
|
84
|
+
}
|
|
85
|
+
else if (!keys) {
|
|
86
|
+
// If the key is not set, we only listen for modifiers, that check went alright, so we return true
|
|
87
|
+
return true
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// There is nothing that matches.
|
|
91
|
+
return false
|
|
92
|
+
}
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
4
|
-
|
|
5
|
-
var hotkeys = _interopDefault(require('hotkeys-js'));
|
|
6
|
-
var react = require('react');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @deprecated Use isHotkeyPressed instead. Will be removed version 4.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
function useIsHotkeyPressed() {
|
|
13
|
-
return hotkeys.isPressed;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
hotkeys.filter = function () {
|
|
17
|
-
return true;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
var tagFilter = function tagFilter(_ref, enableOnTags) {
|
|
21
|
-
var target = _ref.target;
|
|
22
|
-
var targetTagName = target && target.tagName;
|
|
23
|
-
return Boolean(targetTagName && enableOnTags && enableOnTags.includes(targetTagName));
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
var isKeyboardEventTriggeredByInput = function isKeyboardEventTriggeredByInput(ev) {
|
|
27
|
-
return tagFilter(ev, ['INPUT', 'TEXTAREA', 'SELECT']);
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
function useHotkeys(keys, callback, options, deps) {
|
|
31
|
-
if (options instanceof Array) {
|
|
32
|
-
deps = options;
|
|
33
|
-
options = undefined;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
var _ref2 = options || {},
|
|
37
|
-
enableOnTags = _ref2.enableOnTags,
|
|
38
|
-
filter = _ref2.filter,
|
|
39
|
-
keyup = _ref2.keyup,
|
|
40
|
-
keydown = _ref2.keydown,
|
|
41
|
-
_ref2$filterPreventDe = _ref2.filterPreventDefault,
|
|
42
|
-
filterPreventDefault = _ref2$filterPreventDe === void 0 ? true : _ref2$filterPreventDe,
|
|
43
|
-
_ref2$enabled = _ref2.enabled,
|
|
44
|
-
enabled = _ref2$enabled === void 0 ? true : _ref2$enabled,
|
|
45
|
-
_ref2$enableOnContent = _ref2.enableOnContentEditable,
|
|
46
|
-
enableOnContentEditable = _ref2$enableOnContent === void 0 ? false : _ref2$enableOnContent;
|
|
47
|
-
|
|
48
|
-
var ref = react.useRef(null); // The return value of this callback determines if the browsers default behavior is prevented.
|
|
49
|
-
|
|
50
|
-
var memoisedCallback = react.useCallback(function (keyboardEvent, hotkeysEvent) {
|
|
51
|
-
var _keyboardEvent$target, _ref$current;
|
|
52
|
-
|
|
53
|
-
if (filter && !filter(keyboardEvent)) {
|
|
54
|
-
return !filterPreventDefault;
|
|
55
|
-
} // Check whether the hotkeys was triggered inside an input and that input is enabled or if it was triggered by a content editable tag and it is enabled.
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (isKeyboardEventTriggeredByInput(keyboardEvent) && !tagFilter(keyboardEvent, enableOnTags) || (_keyboardEvent$target = keyboardEvent.target) != null && _keyboardEvent$target.isContentEditable && !enableOnContentEditable) {
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (ref.current === null || document.activeElement === ref.current || (_ref$current = ref.current) != null && _ref$current.contains(document.activeElement)) {
|
|
63
|
-
callback(keyboardEvent, hotkeysEvent);
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return false;
|
|
68
|
-
}, deps ? [ref, enableOnTags, filter].concat(deps) : [ref, enableOnTags, filter]);
|
|
69
|
-
react.useEffect(function () {
|
|
70
|
-
if (!enabled) {
|
|
71
|
-
hotkeys.unbind(keys, memoisedCallback);
|
|
72
|
-
return;
|
|
73
|
-
} // In this case keydown is likely undefined, so we set it to false, since hotkeys needs the `keydown` key to have a value.
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (keyup && keydown !== true) {
|
|
77
|
-
options.keydown = false;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
hotkeys(keys, options || {}, memoisedCallback);
|
|
81
|
-
return function () {
|
|
82
|
-
return hotkeys.unbind(keys, memoisedCallback);
|
|
83
|
-
};
|
|
84
|
-
}, [memoisedCallback, keys, enabled]);
|
|
85
|
-
return ref;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
var isHotkeyPressed = hotkeys.isPressed;
|
|
89
|
-
|
|
90
|
-
exports.isHotkeyPressed = isHotkeyPressed;
|
|
91
|
-
exports.useHotkeys = useHotkeys;
|
|
92
|
-
exports.useIsHotkeyPressed = useIsHotkeyPressed;
|
|
93
|
-
//# sourceMappingURL=react-hotkeys-hook.cjs.development.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"react-hotkeys-hook.cjs.development.js","sources":["../src/useIsHotkeyPressed.ts","../src/useHotkeys.ts","../src/index.ts"],"sourcesContent":["import hotkeys from 'hotkeys-js';\n\n/**\n * @deprecated Use isHotkeyPressed instead. Will be removed version 4.\n */\nexport function useIsHotkeyPressed() {\n return hotkeys.isPressed;\n}","import hotkeys, { HotkeysEvent, KeyHandler } from 'hotkeys-js';\nimport React, { useCallback, useEffect, useRef } from 'react';\n\ntype AvailableTags = 'INPUT' | 'TEXTAREA' | 'SELECT';\n\n// We implement our own custom filter system.\nhotkeys.filter = () => true;\n\nconst tagFilter = ({ target }: KeyboardEvent, enableOnTags?: AvailableTags[]) => {\n const targetTagName = target && (target as HTMLElement).tagName;\n\n return Boolean((targetTagName && enableOnTags && enableOnTags.includes(targetTagName as AvailableTags)));\n};\n\nconst isKeyboardEventTriggeredByInput = (ev: KeyboardEvent) => {\n return tagFilter(ev, ['INPUT', 'TEXTAREA', 'SELECT']);\n};\n\nexport type Options = {\n enabled?: boolean; // Main setting that determines if the hotkey is enabled or not. (Default: true)\n filter?: typeof hotkeys.filter; // A filter function returning whether the callback should get triggered or not. (Default: undefined)\n filterPreventDefault?: boolean; // Prevent default browser behavior if the filter function returns false. (Default: true)\n enableOnTags?: AvailableTags[]; // Enable hotkeys on a list of tags. (Default: [])\n enableOnContentEditable?: boolean; // Enable hotkeys on tags with contentEditable props. (Default: false)\n splitKey?: string; // Character to split keys in hotkeys combinations. (Default +)\n scope?: string; // Scope. Currently not doing anything.\n keyup?: boolean; // Trigger on keyup event? (Default: undefined)\n keydown?: boolean; // Trigger on keydown event? (Default: true)\n};\n\nexport function useHotkeys<T extends Element>(keys: string, callback: KeyHandler, options?: Options): React.MutableRefObject<T | null>;\nexport function useHotkeys<T extends Element>(keys: string, callback: KeyHandler, deps?: any[]): React.MutableRefObject<T | null>;\nexport function useHotkeys<T extends Element>(keys: string, callback: KeyHandler, options?: Options, deps?: any[]): React.MutableRefObject<T | null>;\nexport function useHotkeys<T extends Element>(keys: string, callback: KeyHandler, options?: any[] | Options, deps?: any[]): React.MutableRefObject<T | null> {\n if (options instanceof Array) {\n deps = options;\n options = undefined;\n }\n\n const {\n enableOnTags,\n filter,\n keyup,\n keydown,\n filterPreventDefault = true,\n enabled = true,\n enableOnContentEditable = false,\n } = options as Options || {};\n const ref = useRef<T | null>(null);\n\n // The return value of this callback determines if the browsers default behavior is prevented.\n const memoisedCallback = useCallback((keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => {\n if (filter && !filter(keyboardEvent)) {\n return !filterPreventDefault;\n }\n\n // Check whether the hotkeys was triggered inside an input and that input is enabled or if it was triggered by a content editable tag and it is enabled.\n if (\n (isKeyboardEventTriggeredByInput(keyboardEvent) && !tagFilter(keyboardEvent, enableOnTags))\n || ((keyboardEvent.target as HTMLElement)?.isContentEditable && !enableOnContentEditable)\n ) {\n return true;\n }\n\n if (ref.current === null || document.activeElement === ref.current || ref.current?.contains(document.activeElement)) {\n callback(keyboardEvent, hotkeysEvent);\n return true;\n }\n\n return false;\n }, deps ? [ref, enableOnTags, filter, ...deps] : [ref, enableOnTags, filter]);\n\n useEffect(() => {\n if (!enabled) {\n hotkeys.unbind(keys, memoisedCallback);\n\n return;\n }\n\n // In this case keydown is likely undefined, so we set it to false, since hotkeys needs the `keydown` key to have a value.\n if (keyup && keydown !== true) {\n (options as Options).keydown = false;\n }\n\n hotkeys(keys, (options as Options) || {}, memoisedCallback);\n\n return () => hotkeys.unbind(keys, memoisedCallback);\n }, [memoisedCallback, keys, enabled]);\n\n return ref;\n}\n","import { useIsHotkeyPressed } from './useIsHotkeyPressed';\nimport { useHotkeys, Options } from './useHotkeys';\nimport hotkeys from 'hotkeys-js';\n\nconst isHotkeyPressed = hotkeys.isPressed;\n\nexport { useHotkeys, useIsHotkeyPressed, isHotkeyPressed, Options };"],"names":["useIsHotkeyPressed","hotkeys","isPressed","filter","tagFilter","enableOnTags","target","targetTagName","tagName","Boolean","includes","isKeyboardEventTriggeredByInput","ev","useHotkeys","keys","callback","options","deps","Array","undefined","keyup","keydown","filterPreventDefault","enabled","enableOnContentEditable","ref","useRef","memoisedCallback","useCallback","keyboardEvent","hotkeysEvent","isContentEditable","current","document","activeElement","contains","useEffect","unbind","isHotkeyPressed"],"mappings":";;;;;;;AAEA;;;;SAGgBA;AACd,SAAOC,OAAO,CAACC,SAAf;AACD;;ACDDD,OAAO,CAACE,MAAR,GAAiB;AAAA,SAAM,IAAN;AAAA,CAAjB;;AAEA,IAAMC,SAAS,GAAG,SAAZA,SAAY,OAA4BC,YAA5B;MAAGC,cAAAA;AACnB,MAAMC,aAAa,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IC,GAAG,CAACO,OAAJ,KAAgB,IAAhB,IAAwBC,QAAQ,CAACC,aAAT,KAA2BT,GAAG,CAACO,OAAvD,oBAAkEP,GAAG,CAACO,OAAtE,aAAkE,aAAaG,QAAb,CAAsBF,QAAQ,CAACC,aAA/B,CAAtE,EAAqH;AACnHnB,MAAAA,QAAQ,CAACc,aAAD,EAAgBC,YAAhB,CAAR;AACA,aAAO,IAAP;AACD;;AAED,WAAO,KAAP;AACD,GAnBmC,EAmBjCb,IAAI,IAAIQ,GAAJ,EAASpB,YAAT,EAAuBF,MAAvB,SAAkCc,IAAlC,IAA0C,CAACQ,GAAD,EAAMpB,YAAN,EAAoBF,MAApB,CAnBb,CAApC;AAqBAiC,EAAAA,eAAS,CAAC;AACR,QAAI,CAACb,OAAL,EAAc;AACZtB,MAAAA,OAAO,CAACoC,MAAR,CAAevB,IAAf,EAAqBa,gBAArB;AAEA;AACD;;;AAGD,QAAIP,KAAK,IAAIC,OAAO,KAAK,IAAzB,EAA+B;AAC5BL,MAAAA,OAAmB,CAACK,OAApB,GAA8B,KAA9B;AACF;;AAEDpB,IAAAA,OAAO,CAACa,IAAD,EAAQE,OAAmB,IAAI,EAA/B,EAAmCW,gBAAnC,CAAP;AAEA,WAAO;AAAA,aAAM1B,OAAO,CAACoC,MAAR,CAAevB,IAAf,EAAqBa,gBAArB,CAAN;AAAA,KAAP;AACD,GAfQ,EAeN,CAACA,gBAAD,EAAmBb,IAAnB,EAAyBS,OAAzB,CAfM,CAAT;AAiBA,SAAOE,GAAP;AACD;;ICtFKa,eAAe,GAAGrC,OAAO,CAACC,SAAhC;;;;;;"}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";var e,t=(e=require("hotkeys-js"))&&"object"==typeof e&&"default"in e?e.default:e,n=require("react");t.filter=function(){return!0};var r=function(e,t){var n=e.target,r=n&&n.tagName;return Boolean(r&&t&&t.includes(r))};exports.isHotkeyPressed=t.isPressed,exports.useHotkeys=function(e,u,i,o){i instanceof Array&&(o=i,i=void 0);var s=i||{},a=s.enableOnTags,l=s.filter,c=s.keyup,d=s.keydown,f=s.filterPreventDefault,v=void 0===f||f,b=s.enabled,y=void 0===b||b,E=s.enableOnContentEditable,k=void 0!==E&&E,P=n.useRef(null),m=n.useCallback((function(e,t){var n,i;return l&&!l(e)?!v:!!(r(e,["INPUT","TEXTAREA","SELECT"])&&!r(e,a)||null!=(n=e.target)&&n.isContentEditable&&!k)||!!(null===P.current||document.activeElement===P.current||null!=(i=P.current)&&i.contains(document.activeElement))&&(u(e,t),!0)}),o?[P,a,l].concat(o):[P,a,l]);return n.useEffect((function(){if(y)return c&&!0!==d&&(i.keydown=!1),t(e,i||{},m),function(){return t.unbind(e,m)};t.unbind(e,m)}),[m,e,y]),P},exports.useIsHotkeyPressed=function(){return t.isPressed};
|
|
2
|
-
//# sourceMappingURL=react-hotkeys-hook.cjs.production.min.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"react-hotkeys-hook.cjs.production.min.js","sources":["../src/useHotkeys.ts","../src/index.ts","../src/useIsHotkeyPressed.ts"],"sourcesContent":["import hotkeys, { HotkeysEvent, KeyHandler } from 'hotkeys-js';\nimport React, { useCallback, useEffect, useRef } from 'react';\n\ntype AvailableTags = 'INPUT' | 'TEXTAREA' | 'SELECT';\n\n// We implement our own custom filter system.\nhotkeys.filter = () => true;\n\nconst tagFilter = ({ target }: KeyboardEvent, enableOnTags?: AvailableTags[]) => {\n const targetTagName = target && (target as HTMLElement).tagName;\n\n return Boolean((targetTagName && enableOnTags && enableOnTags.includes(targetTagName as AvailableTags)));\n};\n\nconst isKeyboardEventTriggeredByInput = (ev: KeyboardEvent) => {\n return tagFilter(ev, ['INPUT', 'TEXTAREA', 'SELECT']);\n};\n\nexport type Options = {\n enabled?: boolean; // Main setting that determines if the hotkey is enabled or not. (Default: true)\n filter?: typeof hotkeys.filter; // A filter function returning whether the callback should get triggered or not. (Default: undefined)\n filterPreventDefault?: boolean; // Prevent default browser behavior if the filter function returns false. (Default: true)\n enableOnTags?: AvailableTags[]; // Enable hotkeys on a list of tags. (Default: [])\n enableOnContentEditable?: boolean; // Enable hotkeys on tags with contentEditable props. (Default: false)\n splitKey?: string; // Character to split keys in hotkeys combinations. (Default +)\n scope?: string; // Scope. Currently not doing anything.\n keyup?: boolean; // Trigger on keyup event? (Default: undefined)\n keydown?: boolean; // Trigger on keydown event? (Default: true)\n};\n\nexport function useHotkeys<T extends Element>(keys: string, callback: KeyHandler, options?: Options): React.MutableRefObject<T | null>;\nexport function useHotkeys<T extends Element>(keys: string, callback: KeyHandler, deps?: any[]): React.MutableRefObject<T | null>;\nexport function useHotkeys<T extends Element>(keys: string, callback: KeyHandler, options?: Options, deps?: any[]): React.MutableRefObject<T | null>;\nexport function useHotkeys<T extends Element>(keys: string, callback: KeyHandler, options?: any[] | Options, deps?: any[]): React.MutableRefObject<T | null> {\n if (options instanceof Array) {\n deps = options;\n options = undefined;\n }\n\n const {\n enableOnTags,\n filter,\n keyup,\n keydown,\n filterPreventDefault = true,\n enabled = true,\n enableOnContentEditable = false,\n } = options as Options || {};\n const ref = useRef<T | null>(null);\n\n // The return value of this callback determines if the browsers default behavior is prevented.\n const memoisedCallback = useCallback((keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => {\n if (filter && !filter(keyboardEvent)) {\n return !filterPreventDefault;\n }\n\n // Check whether the hotkeys was triggered inside an input and that input is enabled or if it was triggered by a content editable tag and it is enabled.\n if (\n (isKeyboardEventTriggeredByInput(keyboardEvent) && !tagFilter(keyboardEvent, enableOnTags))\n || ((keyboardEvent.target as HTMLElement)?.isContentEditable && !enableOnContentEditable)\n ) {\n return true;\n }\n\n if (ref.current === null || document.activeElement === ref.current || ref.current?.contains(document.activeElement)) {\n callback(keyboardEvent, hotkeysEvent);\n return true;\n }\n\n return false;\n }, deps ? [ref, enableOnTags, filter, ...deps] : [ref, enableOnTags, filter]);\n\n useEffect(() => {\n if (!enabled) {\n hotkeys.unbind(keys, memoisedCallback);\n\n return;\n }\n\n // In this case keydown is likely undefined, so we set it to false, since hotkeys needs the `keydown` key to have a value.\n if (keyup && keydown !== true) {\n (options as Options).keydown = false;\n }\n\n hotkeys(keys, (options as Options) || {}, memoisedCallback);\n\n return () => hotkeys.unbind(keys, memoisedCallback);\n }, [memoisedCallback, keys, enabled]);\n\n return ref;\n}\n","import { useIsHotkeyPressed } from './useIsHotkeyPressed';\nimport { useHotkeys, Options } from './useHotkeys';\nimport hotkeys from 'hotkeys-js';\n\nconst isHotkeyPressed = hotkeys.isPressed;\n\nexport { useHotkeys, useIsHotkeyPressed, isHotkeyPressed, Options };","import hotkeys from 'hotkeys-js';\n\n/**\n * @deprecated Use isHotkeyPressed instead. Will be removed version 4.\n */\nexport function useIsHotkeyPressed() {\n return hotkeys.isPressed;\n}"],"names":["hotkeys","filter","tagFilter","enableOnTags","target","targetTagName","tagName","Boolean","includes","isPressed","keys","callback","options","deps","Array","undefined","keyup","keydown","filterPreventDefault","enabled","enableOnContentEditable","ref","useRef","memoisedCallback","useCallback","keyboardEvent","hotkeysEvent","isContentEditable","current","document","activeElement","_ref$current","contains","useEffect","unbind"],"mappings":"iHAMAA,EAAQC,OAAS,kBAAM,GAEvB,IAAMC,EAAY,WAA4BC,OAAzBC,IAAAA,OACbC,EAAgBD,GAAWA,EAAuBE,eAEjDC,QAASF,GAAiBF,GAAgBA,EAAaK,SAASH,6BCPjDL,EAAQS,6BD6BhC,SAA8CC,EAAcC,EAAsBC,EAA2BC,GACvGD,aAAmBE,QACrBD,EAAOD,EACPA,OAAUG,SAWRH,GAAsB,GAPxBT,IAAAA,aACAF,IAAAA,OACAe,IAAAA,MACAC,IAAAA,YACAC,qBAAAA,oBACAC,QAAAA,oBACAC,wBAAAA,gBAEIC,EAAMC,SAAiB,MAGvBC,EAAmBC,eAAY,SAACC,EAA8BC,kBAC9DzB,IAAWA,EAAOwB,IACZP,KAtCLhB,EA2C8BuB,EA3ChB,CAAC,QAAS,WAAY,aA2CavB,EAAUuB,EAAetB,aACxEsB,EAAcrB,WAAwBuB,oBAAsBP,OAK/C,OAAhBC,EAAIO,SAAoBC,SAASC,gBAAkBT,EAAIO,kBAAWP,EAAIO,UAAJG,EAAaC,SAASH,SAASC,kBACnGnB,EAASc,EAAeC,IACjB,KAIRb,GAAQQ,EAAKlB,EAAcF,UAAWY,GAAQ,CAACQ,EAAKlB,EAAcF,WAErEgC,aAAU,cACHd,SAODH,IAAqB,IAAZC,IACVL,EAAoBK,SAAU,GAGjCjB,EAAQU,EAAOE,GAAuB,GAAIW,GAEnC,kBAAMvB,EAAQkC,OAAOxB,EAAMa,IAZhCvB,EAAQkC,OAAOxB,EAAMa,KAatB,CAACA,EAAkBb,EAAMS,IAErBE,gDEnFArB,EAAQS"}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import hotkeys from 'hotkeys-js';
|
|
2
|
-
import { useRef, useCallback, useEffect } from 'react';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @deprecated Use isHotkeyPressed instead. Will be removed version 4.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
function useIsHotkeyPressed() {
|
|
9
|
-
return hotkeys.isPressed;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
hotkeys.filter = function () {
|
|
13
|
-
return true;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
var tagFilter = function tagFilter(_ref, enableOnTags) {
|
|
17
|
-
var target = _ref.target;
|
|
18
|
-
var targetTagName = target && target.tagName;
|
|
19
|
-
return Boolean(targetTagName && enableOnTags && enableOnTags.includes(targetTagName));
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
var isKeyboardEventTriggeredByInput = function isKeyboardEventTriggeredByInput(ev) {
|
|
23
|
-
return tagFilter(ev, ['INPUT', 'TEXTAREA', 'SELECT']);
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
function useHotkeys(keys, callback, options, deps) {
|
|
27
|
-
if (options instanceof Array) {
|
|
28
|
-
deps = options;
|
|
29
|
-
options = undefined;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
var _ref2 = options || {},
|
|
33
|
-
enableOnTags = _ref2.enableOnTags,
|
|
34
|
-
filter = _ref2.filter,
|
|
35
|
-
keyup = _ref2.keyup,
|
|
36
|
-
keydown = _ref2.keydown,
|
|
37
|
-
_ref2$filterPreventDe = _ref2.filterPreventDefault,
|
|
38
|
-
filterPreventDefault = _ref2$filterPreventDe === void 0 ? true : _ref2$filterPreventDe,
|
|
39
|
-
_ref2$enabled = _ref2.enabled,
|
|
40
|
-
enabled = _ref2$enabled === void 0 ? true : _ref2$enabled,
|
|
41
|
-
_ref2$enableOnContent = _ref2.enableOnContentEditable,
|
|
42
|
-
enableOnContentEditable = _ref2$enableOnContent === void 0 ? false : _ref2$enableOnContent;
|
|
43
|
-
|
|
44
|
-
var ref = useRef(null); // The return value of this callback determines if the browsers default behavior is prevented.
|
|
45
|
-
|
|
46
|
-
var memoisedCallback = useCallback(function (keyboardEvent, hotkeysEvent) {
|
|
47
|
-
var _keyboardEvent$target, _ref$current;
|
|
48
|
-
|
|
49
|
-
if (filter && !filter(keyboardEvent)) {
|
|
50
|
-
return !filterPreventDefault;
|
|
51
|
-
} // Check whether the hotkeys was triggered inside an input and that input is enabled or if it was triggered by a content editable tag and it is enabled.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (isKeyboardEventTriggeredByInput(keyboardEvent) && !tagFilter(keyboardEvent, enableOnTags) || (_keyboardEvent$target = keyboardEvent.target) != null && _keyboardEvent$target.isContentEditable && !enableOnContentEditable) {
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (ref.current === null || document.activeElement === ref.current || (_ref$current = ref.current) != null && _ref$current.contains(document.activeElement)) {
|
|
59
|
-
callback(keyboardEvent, hotkeysEvent);
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return false;
|
|
64
|
-
}, deps ? [ref, enableOnTags, filter].concat(deps) : [ref, enableOnTags, filter]);
|
|
65
|
-
useEffect(function () {
|
|
66
|
-
if (!enabled) {
|
|
67
|
-
hotkeys.unbind(keys, memoisedCallback);
|
|
68
|
-
return;
|
|
69
|
-
} // In this case keydown is likely undefined, so we set it to false, since hotkeys needs the `keydown` key to have a value.
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (keyup && keydown !== true) {
|
|
73
|
-
options.keydown = false;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
hotkeys(keys, options || {}, memoisedCallback);
|
|
77
|
-
return function () {
|
|
78
|
-
return hotkeys.unbind(keys, memoisedCallback);
|
|
79
|
-
};
|
|
80
|
-
}, [memoisedCallback, keys, enabled]);
|
|
81
|
-
return ref;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
var isHotkeyPressed = hotkeys.isPressed;
|
|
85
|
-
|
|
86
|
-
export { isHotkeyPressed, useHotkeys, useIsHotkeyPressed };
|
|
87
|
-
//# sourceMappingURL=react-hotkeys-hook.esm.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"react-hotkeys-hook.esm.js","sources":["../src/useIsHotkeyPressed.ts","../src/useHotkeys.ts","../src/index.ts"],"sourcesContent":["import hotkeys from 'hotkeys-js';\n\n/**\n * @deprecated Use isHotkeyPressed instead. Will be removed version 4.\n */\nexport function useIsHotkeyPressed() {\n return hotkeys.isPressed;\n}","import hotkeys, { HotkeysEvent, KeyHandler } from 'hotkeys-js';\nimport React, { useCallback, useEffect, useRef } from 'react';\n\ntype AvailableTags = 'INPUT' | 'TEXTAREA' | 'SELECT';\n\n// We implement our own custom filter system.\nhotkeys.filter = () => true;\n\nconst tagFilter = ({ target }: KeyboardEvent, enableOnTags?: AvailableTags[]) => {\n const targetTagName = target && (target as HTMLElement).tagName;\n\n return Boolean((targetTagName && enableOnTags && enableOnTags.includes(targetTagName as AvailableTags)));\n};\n\nconst isKeyboardEventTriggeredByInput = (ev: KeyboardEvent) => {\n return tagFilter(ev, ['INPUT', 'TEXTAREA', 'SELECT']);\n};\n\nexport type Options = {\n enabled?: boolean; // Main setting that determines if the hotkey is enabled or not. (Default: true)\n filter?: typeof hotkeys.filter; // A filter function returning whether the callback should get triggered or not. (Default: undefined)\n filterPreventDefault?: boolean; // Prevent default browser behavior if the filter function returns false. (Default: true)\n enableOnTags?: AvailableTags[]; // Enable hotkeys on a list of tags. (Default: [])\n enableOnContentEditable?: boolean; // Enable hotkeys on tags with contentEditable props. (Default: false)\n splitKey?: string; // Character to split keys in hotkeys combinations. (Default +)\n scope?: string; // Scope. Currently not doing anything.\n keyup?: boolean; // Trigger on keyup event? (Default: undefined)\n keydown?: boolean; // Trigger on keydown event? (Default: true)\n};\n\nexport function useHotkeys<T extends Element>(keys: string, callback: KeyHandler, options?: Options): React.MutableRefObject<T | null>;\nexport function useHotkeys<T extends Element>(keys: string, callback: KeyHandler, deps?: any[]): React.MutableRefObject<T | null>;\nexport function useHotkeys<T extends Element>(keys: string, callback: KeyHandler, options?: Options, deps?: any[]): React.MutableRefObject<T | null>;\nexport function useHotkeys<T extends Element>(keys: string, callback: KeyHandler, options?: any[] | Options, deps?: any[]): React.MutableRefObject<T | null> {\n if (options instanceof Array) {\n deps = options;\n options = undefined;\n }\n\n const {\n enableOnTags,\n filter,\n keyup,\n keydown,\n filterPreventDefault = true,\n enabled = true,\n enableOnContentEditable = false,\n } = options as Options || {};\n const ref = useRef<T | null>(null);\n\n // The return value of this callback determines if the browsers default behavior is prevented.\n const memoisedCallback = useCallback((keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => {\n if (filter && !filter(keyboardEvent)) {\n return !filterPreventDefault;\n }\n\n // Check whether the hotkeys was triggered inside an input and that input is enabled or if it was triggered by a content editable tag and it is enabled.\n if (\n (isKeyboardEventTriggeredByInput(keyboardEvent) && !tagFilter(keyboardEvent, enableOnTags))\n || ((keyboardEvent.target as HTMLElement)?.isContentEditable && !enableOnContentEditable)\n ) {\n return true;\n }\n\n if (ref.current === null || document.activeElement === ref.current || ref.current?.contains(document.activeElement)) {\n callback(keyboardEvent, hotkeysEvent);\n return true;\n }\n\n return false;\n }, deps ? [ref, enableOnTags, filter, ...deps] : [ref, enableOnTags, filter]);\n\n useEffect(() => {\n if (!enabled) {\n hotkeys.unbind(keys, memoisedCallback);\n\n return;\n }\n\n // In this case keydown is likely undefined, so we set it to false, since hotkeys needs the `keydown` key to have a value.\n if (keyup && keydown !== true) {\n (options as Options).keydown = false;\n }\n\n hotkeys(keys, (options as Options) || {}, memoisedCallback);\n\n return () => hotkeys.unbind(keys, memoisedCallback);\n }, [memoisedCallback, keys, enabled]);\n\n return ref;\n}\n","import { useIsHotkeyPressed } from './useIsHotkeyPressed';\nimport { useHotkeys, Options } from './useHotkeys';\nimport hotkeys from 'hotkeys-js';\n\nconst isHotkeyPressed = hotkeys.isPressed;\n\nexport { useHotkeys, useIsHotkeyPressed, isHotkeyPressed, Options };"],"names":["useIsHotkeyPressed","hotkeys","isPressed","filter","tagFilter","enableOnTags","target","targetTagName","tagName","Boolean","includes","isKeyboardEventTriggeredByInput","ev","useHotkeys","keys","callback","options","deps","Array","undefined","keyup","keydown","filterPreventDefault","enabled","enableOnContentEditable","ref","useRef","memoisedCallback","useCallback","keyboardEvent","hotkeysEvent","isContentEditable","current","document","activeElement","contains","useEffect","unbind","isHotkeyPressed"],"mappings":";;;AAEA;;;;SAGgBA;AACd,SAAOC,OAAO,CAACC,SAAf;AACD;;ACDDD,OAAO,CAACE,MAAR,GAAiB;AAAA,SAAM,IAAN;AAAA,CAAjB;;AAEA,IAAMC,SAAS,GAAG,SAAZA,SAAY,OAA4BC,YAA5B;MAAGC,cAAAA;AACnB,MAAMC,aAAa,GAAGD,MAAM,IAAKA,MAAsB,CAACE,OAAxD;AAEA,SAAOC,OAAO,CAAEF,aAAa,IAAIF,YAAjB,IAAiCA,YAAY,CAACK,QAAb,CAAsBH,aAAtB,CAAnC,CAAd;AACD,CAJD;;A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