react-hotkeys-hook 5.0.0-1 → 5.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.
Files changed (35) hide show
  1. package/README.md +1 -1
  2. package/package.json +15 -83
  3. package/{dist → packages/react-hotkeys-hook/dist}/BoundHotkeysProxyProvider.d.ts +14 -14
  4. package/{dist → packages/react-hotkeys-hook/dist}/HotkeysProvider.d.ts +16 -16
  5. package/packages/react-hotkeys-hook/dist/deepEqual.d.ts +1 -0
  6. package/{dist → packages/react-hotkeys-hook/dist}/index.d.ts +6 -6
  7. package/packages/react-hotkeys-hook/dist/index.js +220 -0
  8. package/{dist → packages/react-hotkeys-hook/dist}/isHotkeyPressed.d.ts +4 -4
  9. package/{dist → packages/react-hotkeys-hook/dist}/parseHotkeys.d.ts +5 -5
  10. package/packages/react-hotkeys-hook/dist/types.d.ts +45 -0
  11. package/{dist → packages/react-hotkeys-hook/dist}/useDeepEqualMemo.d.ts +1 -1
  12. package/{dist → packages/react-hotkeys-hook/dist}/useHotkeys.d.ts +2 -3
  13. package/{dist → packages/react-hotkeys-hook/dist}/useRecordHotkeys.d.ts +6 -6
  14. package/{dist → packages/react-hotkeys-hook/dist}/validators.d.ts +8 -7
  15. package/dist/deepEqual.d.ts +0 -1
  16. package/dist/index.js +0 -8
  17. package/dist/react-hotkeys-hook.cjs.development.js +0 -525
  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 -519
  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 -38
  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 -71
  30. package/src/parseHotkeys.ts +0 -58
  31. package/src/types.ts +0 -61
  32. package/src/useDeepEqualMemo.ts +0 -12
  33. package/src/useHotkeys.ts +0 -176
  34. package/src/useRecordHotkeys.ts +0 -51
  35. package/src/validators.ts +0 -111
@@ -1 +0,0 @@
1
- {"version":3,"file":"react-hotkeys-hook.esm.js","sources":["../src/parseHotkeys.ts","../src/isHotkeyPressed.ts","../src/validators.ts","../src/BoundHotkeysProxyProvider.tsx","../src/deepEqual.ts","../src/HotkeysProvider.tsx","../src/useDeepEqualMemo.ts","../src/useHotkeys.ts","../src/useRecordHotkeys.ts"],"sourcesContent":["import { Hotkey, KeyboardModifiers } from './types'\n\nconst reservedModifierKeywords = ['shift', 'alt', 'meta', 'mod', 'ctrl', 'control']\n\nconst mappedKeys: Record<string, string> = {\n esc: 'escape',\n return: 'enter',\n left: 'arrowleft',\n right: 'arrowright',\n up: 'arrowup',\n down: 'arrowdown',\n ShiftLeft: 'shift',\n ShiftRight: 'shift',\n AltLeft: 'alt',\n AltRight: 'alt',\n MetaLeft: 'meta',\n MetaRight: 'meta',\n OSLeft: 'meta',\n OSRight: 'meta',\n ControlLeft: 'ctrl',\n ControlRight: 'ctrl',\n}\n\nexport function mapKey(key: string): string {\n return (mappedKeys[key.trim()] || key.trim()).toLowerCase().replace(/key|digit|numpad/, '')\n}\n\nexport function isHotkeyModifier(key: string) {\n return reservedModifierKeywords.includes(key)\n}\n\nexport function parseKeysHookInput(keys: string, delimiter = ','): string[] {\n return keys.toLowerCase().split(delimiter)\n}\n\nexport function parseHotkey(hotkey: string, splitKey = '+', useKey = false, description?: string): Hotkey {\n const keys = hotkey\n .toLocaleLowerCase()\n .split(splitKey)\n .map((k) => mapKey(k))\n\n const modifiers: KeyboardModifiers = {\n alt: keys.includes('alt'),\n ctrl: keys.includes('ctrl') || keys.includes('control'),\n shift: keys.includes('shift'),\n meta: keys.includes('meta'),\n mod: keys.includes('mod'),\n useKey,\n }\n\n const singleCharKeys = keys.filter((k) => !reservedModifierKeywords.includes(k))\n\n return {\n ...modifiers,\n keys: singleCharKeys,\n description,\n }\n}\n","import { isHotkeyModifier, mapKey } from './parseHotkeys'\n;(() => {\n if (typeof document !== 'undefined') {\n document.addEventListener('keydown', (e) => {\n if (e.code === undefined) {\n // Synthetic event (e.g., Chrome autofill). Ignore.\n return\n }\n\n pushToCurrentlyPressedKeys([mapKey(e.code)])\n })\n\n document.addEventListener('keyup', (e) => {\n if (e.code === undefined) {\n // Synthetic event (e.g., Chrome autofill). Ignore.\n return\n }\n\n removeFromCurrentlyPressedKeys([mapKey(e.code)])\n })\n }\n\n if (typeof window !== 'undefined') {\n window.addEventListener('blur', () => {\n currentlyPressedKeys.clear()\n })\n }\n})()\n\nconst currentlyPressedKeys: Set<string> = new Set<string>()\n\n// https://github.com/microsoft/TypeScript/issues/17002\nexport function isReadonlyArray(value: unknown): value is readonly unknown[] {\n return Array.isArray(value)\n}\n\nexport function isHotkeyPressed(key: string | readonly string[], delimiter = ','): boolean {\n const hotkeyArray = isReadonlyArray(key) ? key : key.split(delimiter)\n\n return hotkeyArray.every((hotkey) => currentlyPressedKeys.has(hotkey.trim().toLowerCase()))\n}\n\nexport function pushToCurrentlyPressedKeys(key: string | string[]): void {\n const hotkeyArray = Array.isArray(key) ? key : [key]\n\n /*\n Due to a weird behavior on macOS we need to clear the set if the user pressed down the meta key and presses another key.\n https://stackoverflow.com/questions/11818637/why-does-javascript-drop-keyup-events-when-the-metakey-is-pressed-on-mac-browser\n Otherwise the set will hold all ever pressed keys while the meta key is down which leads to wrong results.\n */\n if (currentlyPressedKeys.has('meta')) {\n currentlyPressedKeys.forEach((key) => !isHotkeyModifier(key) && currentlyPressedKeys.delete(key.toLowerCase()))\n }\n\n hotkeyArray.forEach((hotkey) => currentlyPressedKeys.add(hotkey.toLowerCase()))\n}\n\nexport function removeFromCurrentlyPressedKeys(key: string | string[]): void {\n const hotkeyArray = Array.isArray(key) ? key : [key]\n\n /*\n Due to a weird behavior on macOS we need to clear the set if the user pressed down the meta key and presses another key.\n https://stackoverflow.com/questions/11818637/why-does-javascript-drop-keyup-events-when-the-metakey-is-pressed-on-mac-browser\n Otherwise the set will hold all ever pressed keys while the meta key is down which leads to wrong results.\n */\n if (key === 'meta') {\n currentlyPressedKeys.clear()\n } else {\n hotkeyArray.forEach((hotkey) => currentlyPressedKeys.delete(hotkey.toLowerCase()))\n }\n}\n","import { FormTags, Hotkey, Scopes, Trigger } from './types'\nimport { isHotkeyPressed, isReadonlyArray } from './isHotkeyPressed'\nimport { mapKey } from './parseHotkeys'\n\nexport function maybePreventDefault(e: KeyboardEvent, hotkey: Hotkey, preventDefault?: Trigger): void {\n if ((typeof preventDefault === 'function' && preventDefault(e, hotkey)) || preventDefault === true) {\n e.preventDefault()\n }\n}\n\nexport function isHotkeyEnabled(e: KeyboardEvent, hotkey: Hotkey, enabled?: Trigger): boolean {\n if (typeof enabled === 'function') {\n return enabled(e, hotkey)\n }\n\n return enabled === true || enabled === undefined\n}\n\nexport function isKeyboardEventTriggeredByInput(ev: KeyboardEvent): boolean {\n return isHotkeyEnabledOnTag(ev, ['input', 'textarea', 'select'])\n}\n\nexport function isHotkeyEnabledOnTag(\n { target }: KeyboardEvent,\n enabledOnTags: readonly FormTags[] | boolean = false\n): boolean {\n const targetTagName = target && (target as HTMLElement).tagName\n\n if (isReadonlyArray(enabledOnTags)) {\n return Boolean(\n targetTagName && enabledOnTags && enabledOnTags.some((tag) => tag.toLowerCase() === targetTagName.toLowerCase())\n )\n }\n\n return Boolean(targetTagName && enabledOnTags && enabledOnTags)\n}\n\nexport function isScopeActive(activeScopes: string[], scopes?: Scopes): boolean {\n if (activeScopes.length === 0 && scopes) {\n console.warn(\n '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>'\n )\n\n return true\n }\n\n if (!scopes) {\n return true\n }\n\n return activeScopes.some((scope) => scopes.includes(scope)) || activeScopes.includes('*')\n}\n\nexport const isHotkeyMatchingKeyboardEvent = (e: KeyboardEvent, hotkey: Hotkey, ignoreModifiers = false): boolean => {\n const { alt, meta, mod, shift, ctrl, keys, useKey } = hotkey\n const { code, key: producedKey, ctrlKey, metaKey, shiftKey, altKey } = e\n\n const mappedCode = mapKey(code)\n\n if (useKey && keys?.length === 1 && keys.includes(producedKey)) {\n return true\n }\n\n if (\n !keys?.includes(mappedCode) &&\n !['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(mappedCode)\n ) {\n return false\n }\n\n if (!ignoreModifiers) {\n // We check the pressed keys for compatibility with the keyup event. In keyup events the modifier flags are not set.\n if (alt !== altKey && mappedCode !== 'alt') {\n return false\n }\n\n if (shift !== shiftKey && mappedCode !== 'shift') {\n return false\n }\n\n // Mod is a special key name that is checking for meta on macOS and ctrl on other platforms\n if (mod) {\n if (!metaKey && !ctrlKey) {\n return false\n }\n } else {\n if (meta !== metaKey && mappedCode !== 'meta' && mappedCode !== 'os') {\n return false\n }\n\n if (ctrl !== ctrlKey && mappedCode !== 'ctrl' && mappedCode !== 'control') {\n return false\n }\n }\n }\n\n // All modifiers are correct, now check the key\n // If the key is set, we check for the key\n if (keys && keys.length === 1 && keys.includes(mappedCode)) {\n return true\n } else if (keys) {\n // Check if all keys are present in pressedDownKeys set\n return isHotkeyPressed(keys)\n } else if (!keys) {\n // If the key is not set, we only listen for modifiers, that check went alright, so we return true\n return true\n }\n\n // There is nothing that matches.\n return false\n}\n","import { createContext, ReactNode, useContext } from 'react'\nimport { Hotkey } from './types'\n\ntype BoundHotkeysProxyProviderType = {\n addHotkey: (hotkey: Hotkey) => void\n removeHotkey: (hotkey: Hotkey) => void\n}\n\nconst BoundHotkeysProxyProvider = createContext<BoundHotkeysProxyProviderType | undefined>(undefined)\n\nexport const useBoundHotkeysProxy = () => {\n return useContext(BoundHotkeysProxyProvider)\n}\n\ninterface Props {\n children: ReactNode\n addHotkey: (hotkey: Hotkey) => void\n removeHotkey: (hotkey: Hotkey) => void\n}\n\nexport default function BoundHotkeysProxyProviderProvider({ addHotkey, removeHotkey, children }: Props) {\n return (\n <BoundHotkeysProxyProvider.Provider value={{ addHotkey, removeHotkey }}>\n {children}\n </BoundHotkeysProxyProvider.Provider>\n )\n}\n","export default function deepEqual(x: any, y: any): boolean {\n //@ts-ignore\n return x && y && typeof x === 'object' && typeof y === 'object'\n ? Object.keys(x).length === Object.keys(y).length &&\n //@ts-ignore\n Object.keys(x).reduce((isEqual, key) => isEqual && deepEqual(x[key], y[key]), true)\n : x === y\n}\n","import { Hotkey } from './types'\nimport { createContext, ReactNode, useState, useContext, useCallback } from 'react'\nimport BoundHotkeysProxyProviderProvider from './BoundHotkeysProxyProvider'\nimport deepEqual from './deepEqual'\n\nexport type HotkeysContextType = {\n hotkeys: ReadonlyArray<Hotkey>\n activeScopes: string[]\n toggleScope: (scope: string) => void\n enableScope: (scope: string) => void\n disableScope: (scope: string) => void\n}\n\n// The context is only needed for special features like global scoping, so we use a graceful default fallback\nconst HotkeysContext = createContext<HotkeysContextType>({\n hotkeys: [],\n activeScopes: [], // This array has to be empty instead of containing '*' as default, to check if the provider is set or not\n toggleScope: () => {},\n enableScope: () => {},\n disableScope: () => {},\n})\n\nexport const useHotkeysContext = () => {\n return useContext(HotkeysContext)\n}\n\ninterface Props {\n initiallyActiveScopes?: string[]\n children: ReactNode\n}\n\nexport const HotkeysProvider = ({ initiallyActiveScopes = ['*'], children }: Props) => {\n const [internalActiveScopes, setInternalActiveScopes] = useState(initiallyActiveScopes)\n const [boundHotkeys, setBoundHotkeys] = useState<Hotkey[]>([])\n\n const enableScope = useCallback((scope: string) => {\n setInternalActiveScopes((prev) => {\n if (prev.includes('*')) {\n return [scope]\n }\n return Array.from(new Set([...prev, scope]))\n })\n }, [])\n\n const disableScope = useCallback((scope: string) => {\n setInternalActiveScopes((prev) => {\n return prev.filter((s) => s !== scope)\n })\n }, [])\n\n const toggleScope = useCallback((scope: string) => {\n setInternalActiveScopes((prev) => {\n if (prev.includes(scope)) {\n return prev.filter((s) => s !== scope)\n } else {\n if (prev.includes('*')) {\n return [scope]\n }\n return Array.from(new Set([...prev, scope]))\n }\n })\n }, [])\n\n const addBoundHotkey = useCallback((hotkey: Hotkey) => {\n setBoundHotkeys((prev) => [...prev, hotkey])\n }, [])\n\n const removeBoundHotkey = useCallback((hotkey: Hotkey) => {\n setBoundHotkeys((prev) => prev.filter((h) => !deepEqual(h, hotkey)))\n }, [])\n\n return (\n <HotkeysContext.Provider\n value={{ activeScopes: internalActiveScopes, hotkeys: boundHotkeys, enableScope, disableScope, toggleScope }}\n >\n <BoundHotkeysProxyProviderProvider addHotkey={addBoundHotkey} removeHotkey={removeBoundHotkey}>\n {children}\n </BoundHotkeysProxyProviderProvider>\n </HotkeysContext.Provider>\n )\n}\n","import { useRef } from 'react'\nimport deepEqual from './deepEqual'\n\nexport default function useDeepEqualMemo<T>(value: T) {\n const ref = useRef<T | undefined>(undefined)\n\n if (!deepEqual(ref.current, value)) {\n ref.current = value\n }\n\n return ref.current\n}\n","import { HotkeyCallback, Keys, Options, OptionsOrDependencyArray, RefType } from './types'\nimport { DependencyList, useCallback, useEffect, useLayoutEffect, useRef } from 'react'\nimport { mapKey, parseHotkey, parseKeysHookInput } from './parseHotkeys'\nimport {\n isHotkeyEnabled,\n isHotkeyEnabledOnTag,\n isHotkeyMatchingKeyboardEvent,\n isKeyboardEventTriggeredByInput,\n isScopeActive,\n maybePreventDefault,\n} from './validators'\nimport { useHotkeysContext } from './HotkeysProvider'\nimport { useBoundHotkeysProxy } from './BoundHotkeysProxyProvider'\nimport useDeepEqualMemo from './useDeepEqualMemo'\nimport { isReadonlyArray, pushToCurrentlyPressedKeys, removeFromCurrentlyPressedKeys } from './isHotkeyPressed'\n\nconst stopPropagation = (e: KeyboardEvent): void => {\n e.stopPropagation()\n e.preventDefault()\n e.stopImmediatePropagation()\n}\n\nconst useSafeLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect\n\nexport default function useHotkeys<T extends HTMLElement>(\n keys: Keys,\n callback: HotkeyCallback,\n options?: OptionsOrDependencyArray,\n dependencies?: OptionsOrDependencyArray\n) {\n const ref = useRef<RefType<T>>(null)\n const hasTriggeredRef = useRef(false)\n\n const _options: Options | undefined = !(options instanceof Array)\n ? (options as Options)\n : !(dependencies instanceof Array)\n ? (dependencies as Options)\n : undefined\n const _keys: string = isReadonlyArray(keys) ? keys.join(_options?.delimiter) : keys\n const _deps: DependencyList | undefined =\n options instanceof Array ? options : dependencies instanceof Array ? dependencies : undefined\n\n const memoisedCB = useCallback(callback, _deps ?? [])\n const cbRef = useRef<HotkeyCallback>(memoisedCB)\n\n if (_deps) {\n cbRef.current = memoisedCB\n } else {\n cbRef.current = callback\n }\n\n const memoisedOptions = useDeepEqualMemo(_options)\n\n const { activeScopes } = useHotkeysContext()\n const proxy = useBoundHotkeysProxy()\n\n useSafeLayoutEffect(() => {\n if (memoisedOptions?.enabled === false || !isScopeActive(activeScopes, memoisedOptions?.scopes)) {\n return\n }\n\n const listener = (e: KeyboardEvent, isKeyUp = false) => {\n if (isKeyboardEventTriggeredByInput(e) && !isHotkeyEnabledOnTag(e, memoisedOptions?.enableOnFormTags)) {\n return\n }\n\n // TODO: SINCE THE EVENT IS NOW ATTACHED TO THE REF, THE ACTIVE ELEMENT CAN NEVER BE INSIDE THE REF. THE HOTKEY ONLY TRIGGERS IF THE\n // REF IS THE ACTIVE ELEMENT. THIS IS A PROBLEM SINCE FOCUSED SUB COMPONENTS WON'T TRIGGER THE HOTKEY.\n if (ref.current !== null) {\n const rootNode = ref.current.getRootNode()\n\n if (\n (rootNode instanceof Document || rootNode instanceof ShadowRoot) &&\n rootNode.activeElement !== ref.current &&\n !ref.current.contains(rootNode.activeElement)\n ) {\n stopPropagation(e)\n return\n }\n }\n\n if ((e.target as HTMLElement)?.isContentEditable && !memoisedOptions?.enableOnContentEditable) {\n return\n }\n\n parseKeysHookInput(_keys, memoisedOptions?.delimiter).forEach((key) => {\n const hotkey = parseHotkey(key, memoisedOptions?.splitKey, memoisedOptions?.useKey)\n\n if (isHotkeyMatchingKeyboardEvent(e, hotkey, memoisedOptions?.ignoreModifiers) || hotkey.keys?.includes('*')) {\n if (memoisedOptions?.ignoreEventWhen?.(e)) {\n return\n }\n\n if (isKeyUp && hasTriggeredRef.current) {\n return\n }\n\n maybePreventDefault(e, hotkey, memoisedOptions?.preventDefault)\n\n if (!isHotkeyEnabled(e, hotkey, memoisedOptions?.enabled)) {\n stopPropagation(e)\n\n return\n }\n\n // Execute the user callback for that hotkey\n cbRef.current(e, hotkey)\n\n if (!isKeyUp) {\n hasTriggeredRef.current = true\n }\n }\n })\n }\n\n const handleKeyDown = (event: KeyboardEvent) => {\n if (event.code === undefined) {\n // Synthetic event (e.g., Chrome autofill). Ignore.\n return\n }\n\n pushToCurrentlyPressedKeys(mapKey(event.code))\n\n if ((memoisedOptions?.keydown === undefined && memoisedOptions?.keyup !== true) || memoisedOptions?.keydown) {\n listener(event)\n }\n }\n\n const handleKeyUp = (event: KeyboardEvent) => {\n if (event.code === undefined) {\n // Synthetic event (e.g., Chrome autofill). Ignore.\n return\n }\n\n removeFromCurrentlyPressedKeys(mapKey(event.code))\n\n hasTriggeredRef.current = false\n\n if (memoisedOptions?.keyup) {\n listener(event, true)\n }\n }\n\n const domNode = ref.current || _options?.document || document\n\n // @ts-ignore\n domNode.addEventListener('keyup', handleKeyUp)\n // @ts-ignore\n domNode.addEventListener('keydown', handleKeyDown)\n\n if (proxy) {\n parseKeysHookInput(_keys, memoisedOptions?.delimiter).forEach((key) =>\n proxy.addHotkey(\n parseHotkey(key, memoisedOptions?.splitKey, memoisedOptions?.useKey, memoisedOptions?.description)\n )\n )\n }\n\n return () => {\n // @ts-ignore\n domNode.removeEventListener('keyup', handleKeyUp)\n // @ts-ignore\n domNode.removeEventListener('keydown', handleKeyDown)\n\n if (proxy) {\n parseKeysHookInput(_keys, memoisedOptions?.delimiter).forEach((key) =>\n proxy.removeHotkey(\n parseHotkey(key, memoisedOptions?.splitKey, memoisedOptions?.useKey, memoisedOptions?.description)\n )\n )\n }\n }\n }, [_keys, memoisedOptions, activeScopes])\n\n return ref\n}\n","import { useCallback, useState } from 'react'\nimport { mapKey } from './parseHotkeys'\n\nexport default function useRecordHotkeys() {\n const [keys, setKeys] = useState(new Set<string>())\n const [isRecording, setIsRecording] = useState(false)\n\n const handler = useCallback((event: KeyboardEvent) => {\n if (event.code === undefined) {\n // Synthetic event (e.g., Chrome autofill). Ignore.\n return\n }\n\n event.preventDefault()\n event.stopPropagation()\n\n setKeys((prev) => {\n const newKeys = new Set(prev)\n\n newKeys.add(mapKey(event.code))\n\n return newKeys\n })\n }, [])\n\n const stop = useCallback(() => {\n if (typeof document !== 'undefined') {\n document.removeEventListener('keydown', handler)\n\n setIsRecording(false)\n }\n }, [handler])\n\n const start = useCallback(() => {\n setKeys(new Set<string>())\n\n if (typeof document !== 'undefined') {\n stop()\n\n document.addEventListener('keydown', handler)\n\n setIsRecording(true)\n }\n }, [handler, stop])\n\n const resetKeys = useCallback(() => {\n setKeys(new Set<string>())\n }, [])\n\n return [keys, { start, stop, resetKeys, isRecording }] as const\n}\n"],"names":["reservedModifierKeywords","mappedKeys","esc","left","right","up","down","ShiftLeft","ShiftRight","AltLeft","AltRight","MetaLeft","MetaRight","OSLeft","OSRight","ControlLeft","ControlRight","mapKey","key","trim","toLowerCase","replace","isHotkeyModifier","includes","parseKeysHookInput","keys","delimiter","split","parseHotkey","hotkey","splitKey","useKey","description","toLocaleLowerCase","map","k","modifiers","alt","ctrl","shift","meta","mod","singleCharKeys","filter","_extends","document","addEventListener","e","code","undefined","pushToCurrentlyPressedKeys","removeFromCurrentlyPressedKeys","window","currentlyPressedKeys","clear","Set","isReadonlyArray","value","Array","isArray","isHotkeyPressed","hotkeyArray","every","has","forEach","add","maybePreventDefault","preventDefault","isHotkeyEnabled","enabled","isKeyboardEventTriggeredByInput","ev","isHotkeyEnabledOnTag","_ref","enabledOnTags","target","targetTagName","tagName","Boolean","some","tag","isScopeActive","activeScopes","scopes","length","console","warn","scope","isHotkeyMatchingKeyboardEvent","ignoreModifiers","producedKey","ctrlKey","metaKey","shiftKey","altKey","mappedCode","BoundHotkeysProxyProvider","createContext","useBoundHotkeysProxy","useContext","BoundHotkeysProxyProviderProvider","addHotkey","removeHotkey","children","_jsx","Provider","deepEqual","x","y","Object","reduce","isEqual","HotkeysContext","hotkeys","toggleScope","enableScope","disableScope","useHotkeysContext","HotkeysProvider","initiallyActiveScopes","_ref$initiallyActiveS","_useState","useState","internalActiveScopes","setInternalActiveScopes","_useState2","boundHotkeys","setBoundHotkeys","useCallback","prev","from","concat","s","addBoundHotkey","removeBoundHotkey","h","useDeepEqualMemo","ref","useRef","current","stopPropagation","stopImmediatePropagation","useSafeLayoutEffect","useLayoutEffect","useEffect","useHotkeys","callback","options","dependencies","hasTriggeredRef","_options","_keys","join","_deps","memoisedCB","cbRef","memoisedOptions","_useHotkeysContext","proxy","listener","isKeyUp","enableOnFormTags","rootNode","getRootNode","Document","ShadowRoot","activeElement","contains","_e$target","isContentEditable","enableOnContentEditable","_hotkey$keys","ignoreEventWhen","handleKeyDown","event","keydown","keyup","handleKeyUp","domNode","removeEventListener","useRecordHotkeys","setKeys","isRecording","setIsRecording","handler","newKeys","stop","start","resetKeys"],"mappings":";;;;;;;;;;;;;;;;;;AAEA,IAAMA,wBAAwB,GAAG,CAAC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC;AAEnF,IAAMC,UAAU,GAA2B;EACzCC,GAAG,EAAE,QAAQ;EACb,UAAQ,OAAO;EACfC,IAAI,EAAE,WAAW;EACjBC,KAAK,EAAE,YAAY;EACnBC,EAAE,EAAE,SAAS;EACbC,IAAI,EAAE,WAAW;EACjBC,SAAS,EAAE,OAAO;EAClBC,UAAU,EAAE,OAAO;EACnBC,OAAO,EAAE,KAAK;EACdC,QAAQ,EAAE,KAAK;EACfC,QAAQ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KAAK;IAAED,IAAI,EAAJA,IAAI;IAAEE,SAAS,EAATA,SAAS;IAAEN,WAAW,EAAXA;GAAa,CAAU;AACjE;;;;"}
@@ -1 +0,0 @@
1
- import '@testing-library/jest-dom';
package/dist/types.d.ts DELETED
@@ -1,38 +0,0 @@
1
- import type { DependencyList } from 'react';
2
- export declare type FormTags = 'input' | 'textarea' | 'select' | 'INPUT' | 'TEXTAREA' | 'SELECT';
3
- export declare type Keys = string | readonly string[];
4
- export declare type Scopes = string | readonly string[];
5
- export declare type RefType<T> = T | null;
6
- export declare type KeyboardModifiers = {
7
- alt?: boolean;
8
- ctrl?: boolean;
9
- meta?: boolean;
10
- shift?: boolean;
11
- mod?: boolean;
12
- useKey?: boolean;
13
- };
14
- export declare type Hotkey = KeyboardModifiers & {
15
- keys?: readonly string[];
16
- scopes?: Scopes;
17
- description?: string;
18
- };
19
- export declare type HotkeysEvent = Hotkey;
20
- export declare type HotkeyCallback = (keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => void;
21
- export declare type Trigger = boolean | ((keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => boolean);
22
- export declare type Options = {
23
- enabled?: Trigger;
24
- enableOnFormTags?: readonly FormTags[] | boolean;
25
- enableOnContentEditable?: boolean;
26
- ignoreEventWhen?: (e: KeyboardEvent) => boolean;
27
- splitKey?: string;
28
- delimiter?: string;
29
- scopes?: Scopes;
30
- keyup?: boolean;
31
- keydown?: boolean;
32
- preventDefault?: Trigger;
33
- description?: string;
34
- document?: Document;
35
- ignoreModifiers?: boolean;
36
- useKey?: boolean;
37
- };
38
- export declare type OptionsOrDependencyArray = Options | DependencyList;
@@ -1,27 +0,0 @@
1
- import { createContext, ReactNode, useContext } from 'react'
2
- import { Hotkey } from './types'
3
-
4
- type BoundHotkeysProxyProviderType = {
5
- addHotkey: (hotkey: Hotkey) => void
6
- removeHotkey: (hotkey: Hotkey) => void
7
- }
8
-
9
- const BoundHotkeysProxyProvider = createContext<BoundHotkeysProxyProviderType | undefined>(undefined)
10
-
11
- export const useBoundHotkeysProxy = () => {
12
- return useContext(BoundHotkeysProxyProvider)
13
- }
14
-
15
- interface Props {
16
- children: ReactNode
17
- addHotkey: (hotkey: Hotkey) => void
18
- removeHotkey: (hotkey: Hotkey) => void
19
- }
20
-
21
- export default function BoundHotkeysProxyProviderProvider({ addHotkey, removeHotkey, children }: Props) {
22
- return (
23
- <BoundHotkeysProxyProvider.Provider value={{ addHotkey, removeHotkey }}>
24
- {children}
25
- </BoundHotkeysProxyProvider.Provider>
26
- )
27
- }
@@ -1,81 +0,0 @@
1
- import { Hotkey } from './types'
2
- import { createContext, ReactNode, useState, useContext, useCallback } from 'react'
3
- import BoundHotkeysProxyProviderProvider from './BoundHotkeysProxyProvider'
4
- import deepEqual from './deepEqual'
5
-
6
- export type HotkeysContextType = {
7
- hotkeys: ReadonlyArray<Hotkey>
8
- activeScopes: string[]
9
- toggleScope: (scope: string) => void
10
- enableScope: (scope: string) => void
11
- disableScope: (scope: string) => void
12
- }
13
-
14
- // The context is only needed for special features like global scoping, so we use a graceful default fallback
15
- const HotkeysContext = createContext<HotkeysContextType>({
16
- hotkeys: [],
17
- activeScopes: [], // This array has to be empty instead of containing '*' as default, to check if the provider is set or not
18
- toggleScope: () => {},
19
- enableScope: () => {},
20
- disableScope: () => {},
21
- })
22
-
23
- export const useHotkeysContext = () => {
24
- return useContext(HotkeysContext)
25
- }
26
-
27
- interface Props {
28
- initiallyActiveScopes?: string[]
29
- children: ReactNode
30
- }
31
-
32
- export const HotkeysProvider = ({ initiallyActiveScopes = ['*'], children }: Props) => {
33
- const [internalActiveScopes, setInternalActiveScopes] = useState(initiallyActiveScopes)
34
- const [boundHotkeys, setBoundHotkeys] = useState<Hotkey[]>([])
35
-
36
- const enableScope = useCallback((scope: string) => {
37
- setInternalActiveScopes((prev) => {
38
- if (prev.includes('*')) {
39
- return [scope]
40
- }
41
- return Array.from(new Set([...prev, scope]))
42
- })
43
- }, [])
44
-
45
- const disableScope = useCallback((scope: string) => {
46
- setInternalActiveScopes((prev) => {
47
- return prev.filter((s) => s !== scope)
48
- })
49
- }, [])
50
-
51
- const toggleScope = useCallback((scope: string) => {
52
- setInternalActiveScopes((prev) => {
53
- if (prev.includes(scope)) {
54
- return prev.filter((s) => s !== scope)
55
- } else {
56
- if (prev.includes('*')) {
57
- return [scope]
58
- }
59
- return Array.from(new Set([...prev, scope]))
60
- }
61
- })
62
- }, [])
63
-
64
- const addBoundHotkey = useCallback((hotkey: Hotkey) => {
65
- setBoundHotkeys((prev) => [...prev, hotkey])
66
- }, [])
67
-
68
- const removeBoundHotkey = useCallback((hotkey: Hotkey) => {
69
- setBoundHotkeys((prev) => prev.filter((h) => !deepEqual(h, hotkey)))
70
- }, [])
71
-
72
- return (
73
- <HotkeysContext.Provider
74
- value={{ activeScopes: internalActiveScopes, hotkeys: boundHotkeys, enableScope, disableScope, toggleScope }}
75
- >
76
- <BoundHotkeysProxyProviderProvider addHotkey={addBoundHotkey} removeHotkey={removeBoundHotkey}>
77
- {children}
78
- </BoundHotkeysProxyProviderProvider>
79
- </HotkeysContext.Provider>
80
- )
81
- }
package/src/deepEqual.ts DELETED
@@ -1,8 +0,0 @@
1
- export default function deepEqual(x: any, y: any): boolean {
2
- //@ts-ignore
3
- return x && y && typeof x === 'object' && typeof y === 'object'
4
- ? Object.keys(x).length === Object.keys(y).length &&
5
- //@ts-ignore
6
- Object.keys(x).reduce((isEqual, key) => isEqual && deepEqual(x[key], y[key]), true)
7
- : x === y
8
- }
package/src/index.ts DELETED
@@ -1,16 +0,0 @@
1
- import useHotkeys from './useHotkeys'
2
- import type { Options, Keys, HotkeyCallback } from './types'
3
- import { HotkeysProvider, useHotkeysContext } from './HotkeysProvider'
4
- import { isHotkeyPressed } from './isHotkeyPressed'
5
- import useRecordHotkeys from './useRecordHotkeys'
6
-
7
- export {
8
- useHotkeys,
9
- useRecordHotkeys,
10
- useHotkeysContext,
11
- isHotkeyPressed,
12
- HotkeysProvider,
13
- Options,
14
- Keys,
15
- HotkeyCallback,
16
- }
@@ -1,71 +0,0 @@
1
- import { isHotkeyModifier, mapKey } from './parseHotkeys'
2
- ;(() => {
3
- if (typeof document !== 'undefined') {
4
- document.addEventListener('keydown', (e) => {
5
- if (e.code === undefined) {
6
- // Synthetic event (e.g., Chrome autofill). Ignore.
7
- return
8
- }
9
-
10
- pushToCurrentlyPressedKeys([mapKey(e.code)])
11
- })
12
-
13
- document.addEventListener('keyup', (e) => {
14
- if (e.code === undefined) {
15
- // Synthetic event (e.g., Chrome autofill). Ignore.
16
- return
17
- }
18
-
19
- removeFromCurrentlyPressedKeys([mapKey(e.code)])
20
- })
21
- }
22
-
23
- if (typeof window !== 'undefined') {
24
- window.addEventListener('blur', () => {
25
- currentlyPressedKeys.clear()
26
- })
27
- }
28
- })()
29
-
30
- const currentlyPressedKeys: Set<string> = new Set<string>()
31
-
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[], delimiter = ','): boolean {
38
- const hotkeyArray = isReadonlyArray(key) ? key : key.split(delimiter)
39
-
40
- return hotkeyArray.every((hotkey) => currentlyPressedKeys.has(hotkey.trim().toLowerCase()))
41
- }
42
-
43
- export function pushToCurrentlyPressedKeys(key: string | string[]): void {
44
- const hotkeyArray = Array.isArray(key) ? key : [key]
45
-
46
- /*
47
- Due to a weird behavior on macOS we need to clear the set if the user pressed down the meta key and presses another key.
48
- https://stackoverflow.com/questions/11818637/why-does-javascript-drop-keyup-events-when-the-metakey-is-pressed-on-mac-browser
49
- Otherwise the set will hold all ever pressed keys while the meta key is down which leads to wrong results.
50
- */
51
- if (currentlyPressedKeys.has('meta')) {
52
- currentlyPressedKeys.forEach((key) => !isHotkeyModifier(key) && currentlyPressedKeys.delete(key.toLowerCase()))
53
- }
54
-
55
- hotkeyArray.forEach((hotkey) => currentlyPressedKeys.add(hotkey.toLowerCase()))
56
- }
57
-
58
- export function removeFromCurrentlyPressedKeys(key: string | string[]): void {
59
- const hotkeyArray = Array.isArray(key) ? key : [key]
60
-
61
- /*
62
- Due to a weird behavior on macOS we need to clear the set if the user pressed down the meta key and presses another key.
63
- https://stackoverflow.com/questions/11818637/why-does-javascript-drop-keyup-events-when-the-metakey-is-pressed-on-mac-browser
64
- Otherwise the set will hold all ever pressed keys while the meta key is down which leads to wrong results.
65
- */
66
- if (key === 'meta') {
67
- currentlyPressedKeys.clear()
68
- } else {
69
- hotkeyArray.forEach((hotkey) => currentlyPressedKeys.delete(hotkey.toLowerCase()))
70
- }
71
- }
@@ -1,58 +0,0 @@
1
- import { Hotkey, KeyboardModifiers } from './types'
2
-
3
- const reservedModifierKeywords = ['shift', 'alt', 'meta', 'mod', 'ctrl', 'control']
4
-
5
- const mappedKeys: Record<string, string> = {
6
- esc: 'escape',
7
- return: 'enter',
8
- left: 'arrowleft',
9
- right: 'arrowright',
10
- up: 'arrowup',
11
- down: 'arrowdown',
12
- ShiftLeft: 'shift',
13
- ShiftRight: 'shift',
14
- AltLeft: 'alt',
15
- AltRight: 'alt',
16
- MetaLeft: 'meta',
17
- MetaRight: 'meta',
18
- OSLeft: 'meta',
19
- OSRight: 'meta',
20
- ControlLeft: 'ctrl',
21
- ControlRight: 'ctrl',
22
- }
23
-
24
- export function mapKey(key: string): string {
25
- return (mappedKeys[key.trim()] || key.trim()).toLowerCase().replace(/key|digit|numpad/, '')
26
- }
27
-
28
- export function isHotkeyModifier(key: string) {
29
- return reservedModifierKeywords.includes(key)
30
- }
31
-
32
- export function parseKeysHookInput(keys: string, delimiter = ','): string[] {
33
- return keys.toLowerCase().split(delimiter)
34
- }
35
-
36
- export function parseHotkey(hotkey: string, splitKey = '+', useKey = false, description?: string): Hotkey {
37
- const keys = hotkey
38
- .toLocaleLowerCase()
39
- .split(splitKey)
40
- .map((k) => mapKey(k))
41
-
42
- const modifiers: KeyboardModifiers = {
43
- alt: keys.includes('alt'),
44
- ctrl: keys.includes('ctrl') || keys.includes('control'),
45
- shift: keys.includes('shift'),
46
- meta: keys.includes('meta'),
47
- mod: keys.includes('mod'),
48
- useKey,
49
- }
50
-
51
- const singleCharKeys = keys.filter((k) => !reservedModifierKeywords.includes(k))
52
-
53
- return {
54
- ...modifiers,
55
- keys: singleCharKeys,
56
- description,
57
- }
58
- }
package/src/types.ts DELETED
@@ -1,61 +0,0 @@
1
- import type { DependencyList } from 'react'
2
-
3
- export type FormTags = 'input' | 'textarea' | 'select' | 'INPUT' | 'TEXTAREA' | 'SELECT'
4
- export type Keys = string | readonly string[]
5
- export type Scopes = string | readonly 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
- useKey?: boolean // Custom modifier to listen to the produced key instead of the code
16
- }
17
-
18
- export type Hotkey = KeyboardModifiers & {
19
- keys?: readonly string[]
20
- scopes?: Scopes
21
- description?: string
22
- }
23
-
24
- export type HotkeysEvent = Hotkey
25
-
26
- export type HotkeyCallback = (keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => void
27
-
28
- export type Trigger = boolean | ((keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => boolean)
29
-
30
- export type Options = {
31
- // Main setting that determines if the hotkey is enabled or not. (Default: true)
32
- enabled?: Trigger
33
- // Enable hotkeys on a list of tags. (Default: false)
34
- enableOnFormTags?: readonly FormTags[] | boolean
35
- // Enable hotkeys on tags with contentEditable props. (Default: false)
36
- enableOnContentEditable?: boolean
37
- // Ignore evenets based on a condition (Default: undefined)
38
- ignoreEventWhen?: (e: KeyboardEvent) => boolean
39
- // Character to split keys in hotkeys combinations. (Default: +)
40
- splitKey?: string
41
- // Character to separate different hotkeys. (Default: ,)
42
- delimiter?: string
43
- // Scope of the hotkey. (Default: undefined)
44
- scopes?: Scopes
45
- // Trigger on keyup event? (Default: undefined)
46
- keyup?: boolean
47
- // Trigger on keydown event? (Default: true)
48
- keydown?: boolean
49
- // Prevent default browser behavior? (Default: false)
50
- preventDefault?: Trigger
51
- // Use this option to describe what the hotkey does. (Default: undefined)
52
- description?: string
53
- // Listen to events on the document instead of the window. (Default: false)
54
- document?: Document
55
- // Ignore modifiers when matching hotkeys. (Default: false)
56
- ignoreModifiers?: boolean
57
- // Listen to the produced key instead of the code. (Default: false)
58
- useKey?: boolean
59
- }
60
-
61
- export type OptionsOrDependencyArray = Options | DependencyList
@@ -1,12 +0,0 @@
1
- import { useRef } from 'react'
2
- import deepEqual from './deepEqual'
3
-
4
- export default function useDeepEqualMemo<T>(value: T) {
5
- const ref = useRef<T | undefined>(undefined)
6
-
7
- if (!deepEqual(ref.current, value)) {
8
- ref.current = value
9
- }
10
-
11
- return ref.current
12
- }
package/src/useHotkeys.ts DELETED
@@ -1,176 +0,0 @@
1
- import { HotkeyCallback, Keys, Options, OptionsOrDependencyArray, RefType } from './types'
2
- import { DependencyList, useCallback, useEffect, useLayoutEffect, useRef } from 'react'
3
- import { mapKey, 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
- import useDeepEqualMemo from './useDeepEqualMemo'
15
- import { isReadonlyArray, pushToCurrentlyPressedKeys, removeFromCurrentlyPressedKeys } from './isHotkeyPressed'
16
-
17
- const stopPropagation = (e: KeyboardEvent): void => {
18
- e.stopPropagation()
19
- e.preventDefault()
20
- e.stopImmediatePropagation()
21
- }
22
-
23
- const useSafeLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect
24
-
25
- export default function useHotkeys<T extends HTMLElement>(
26
- keys: Keys,
27
- callback: HotkeyCallback,
28
- options?: OptionsOrDependencyArray,
29
- dependencies?: OptionsOrDependencyArray
30
- ) {
31
- const ref = useRef<RefType<T>>(null)
32
- const hasTriggeredRef = useRef(false)
33
-
34
- const _options: Options | undefined = !(options instanceof Array)
35
- ? (options as Options)
36
- : !(dependencies instanceof Array)
37
- ? (dependencies as Options)
38
- : undefined
39
- const _keys: string = isReadonlyArray(keys) ? keys.join(_options?.delimiter) : keys
40
- const _deps: DependencyList | undefined =
41
- options instanceof Array ? options : dependencies instanceof Array ? dependencies : undefined
42
-
43
- const memoisedCB = useCallback(callback, _deps ?? [])
44
- const cbRef = useRef<HotkeyCallback>(memoisedCB)
45
-
46
- if (_deps) {
47
- cbRef.current = memoisedCB
48
- } else {
49
- cbRef.current = callback
50
- }
51
-
52
- const memoisedOptions = useDeepEqualMemo(_options)
53
-
54
- const { activeScopes } = useHotkeysContext()
55
- const proxy = useBoundHotkeysProxy()
56
-
57
- useSafeLayoutEffect(() => {
58
- if (memoisedOptions?.enabled === false || !isScopeActive(activeScopes, memoisedOptions?.scopes)) {
59
- return
60
- }
61
-
62
- const listener = (e: KeyboardEvent, isKeyUp = false) => {
63
- if (isKeyboardEventTriggeredByInput(e) && !isHotkeyEnabledOnTag(e, memoisedOptions?.enableOnFormTags)) {
64
- return
65
- }
66
-
67
- // TODO: SINCE THE EVENT IS NOW ATTACHED TO THE REF, THE ACTIVE ELEMENT CAN NEVER BE INSIDE THE REF. THE HOTKEY ONLY TRIGGERS IF THE
68
- // REF IS THE ACTIVE ELEMENT. THIS IS A PROBLEM SINCE FOCUSED SUB COMPONENTS WON'T TRIGGER THE HOTKEY.
69
- if (ref.current !== null) {
70
- const rootNode = ref.current.getRootNode()
71
-
72
- if (
73
- (rootNode instanceof Document || rootNode instanceof ShadowRoot) &&
74
- rootNode.activeElement !== ref.current &&
75
- !ref.current.contains(rootNode.activeElement)
76
- ) {
77
- stopPropagation(e)
78
- return
79
- }
80
- }
81
-
82
- if ((e.target as HTMLElement)?.isContentEditable && !memoisedOptions?.enableOnContentEditable) {
83
- return
84
- }
85
-
86
- parseKeysHookInput(_keys, memoisedOptions?.delimiter).forEach((key) => {
87
- const hotkey = parseHotkey(key, memoisedOptions?.splitKey, memoisedOptions?.useKey)
88
-
89
- if (isHotkeyMatchingKeyboardEvent(e, hotkey, memoisedOptions?.ignoreModifiers) || hotkey.keys?.includes('*')) {
90
- if (memoisedOptions?.ignoreEventWhen?.(e)) {
91
- return
92
- }
93
-
94
- if (isKeyUp && hasTriggeredRef.current) {
95
- return
96
- }
97
-
98
- maybePreventDefault(e, hotkey, memoisedOptions?.preventDefault)
99
-
100
- if (!isHotkeyEnabled(e, hotkey, memoisedOptions?.enabled)) {
101
- stopPropagation(e)
102
-
103
- return
104
- }
105
-
106
- // Execute the user callback for that hotkey
107
- cbRef.current(e, hotkey)
108
-
109
- if (!isKeyUp) {
110
- hasTriggeredRef.current = true
111
- }
112
- }
113
- })
114
- }
115
-
116
- const handleKeyDown = (event: KeyboardEvent) => {
117
- if (event.code === undefined) {
118
- // Synthetic event (e.g., Chrome autofill). Ignore.
119
- return
120
- }
121
-
122
- pushToCurrentlyPressedKeys(mapKey(event.code))
123
-
124
- if ((memoisedOptions?.keydown === undefined && memoisedOptions?.keyup !== true) || memoisedOptions?.keydown) {
125
- listener(event)
126
- }
127
- }
128
-
129
- const handleKeyUp = (event: KeyboardEvent) => {
130
- if (event.code === undefined) {
131
- // Synthetic event (e.g., Chrome autofill). Ignore.
132
- return
133
- }
134
-
135
- removeFromCurrentlyPressedKeys(mapKey(event.code))
136
-
137
- hasTriggeredRef.current = false
138
-
139
- if (memoisedOptions?.keyup) {
140
- listener(event, true)
141
- }
142
- }
143
-
144
- const domNode = ref.current || _options?.document || document
145
-
146
- // @ts-ignore
147
- domNode.addEventListener('keyup', handleKeyUp)
148
- // @ts-ignore
149
- domNode.addEventListener('keydown', handleKeyDown)
150
-
151
- if (proxy) {
152
- parseKeysHookInput(_keys, memoisedOptions?.delimiter).forEach((key) =>
153
- proxy.addHotkey(
154
- parseHotkey(key, memoisedOptions?.splitKey, memoisedOptions?.useKey, memoisedOptions?.description)
155
- )
156
- )
157
- }
158
-
159
- return () => {
160
- // @ts-ignore
161
- domNode.removeEventListener('keyup', handleKeyUp)
162
- // @ts-ignore
163
- domNode.removeEventListener('keydown', handleKeyDown)
164
-
165
- if (proxy) {
166
- parseKeysHookInput(_keys, memoisedOptions?.delimiter).forEach((key) =>
167
- proxy.removeHotkey(
168
- parseHotkey(key, memoisedOptions?.splitKey, memoisedOptions?.useKey, memoisedOptions?.description)
169
- )
170
- )
171
- }
172
- }
173
- }, [_keys, memoisedOptions, activeScopes])
174
-
175
- return ref
176
- }