react-hotkeys-hook 5.0.0-0 → 5.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/package.json +13 -85
- package/{dist → packages/react-hotkeys-hook/dist}/BoundHotkeysProxyProvider.d.ts +14 -14
- package/{dist → packages/react-hotkeys-hook/dist}/HotkeysProvider.d.ts +16 -16
- package/packages/react-hotkeys-hook/dist/deepEqual.d.ts +1 -0
- package/{dist → packages/react-hotkeys-hook/dist}/index.d.ts +6 -6
- package/packages/react-hotkeys-hook/dist/index.js +220 -0
- package/{dist → packages/react-hotkeys-hook/dist}/isHotkeyPressed.d.ts +4 -4
- package/packages/react-hotkeys-hook/dist/parseHotkeys.d.ts +5 -0
- package/packages/react-hotkeys-hook/dist/types.d.ts +45 -0
- package/{dist → packages/react-hotkeys-hook/dist}/useDeepEqualMemo.d.ts +1 -1
- package/{dist → packages/react-hotkeys-hook/dist}/useHotkeys.d.ts +2 -3
- package/{dist → packages/react-hotkeys-hook/dist}/useRecordHotkeys.d.ts +6 -5
- package/{dist → packages/react-hotkeys-hook/dist}/validators.d.ts +8 -7
- package/dist/deepEqual.d.ts +0 -1
- package/dist/index.js +0 -8
- package/dist/parseHotkeys.d.ts +0 -5
- package/dist/react-hotkeys-hook.cjs.development.js +0 -514
- 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 -508
- package/dist/react-hotkeys-hook.esm.js.map +0 -1
- package/dist/setupTests.d.ts +0 -1
- package/dist/types.d.ts +0 -36
- package/src/BoundHotkeysProxyProvider.tsx +0 -27
- package/src/HotkeysProvider.tsx +0 -81
- package/src/deepEqual.ts +0 -8
- package/src/index.ts +0 -16
- package/src/isHotkeyPressed.ts +0 -73
- package/src/parseHotkeys.ts +0 -58
- package/src/types.ts +0 -45
- package/src/useDeepEqualMemo.ts +0 -12
- package/src/useHotkeys.ts +0 -172
- package/src/useRecordHotkeys.ts +0 -47
- package/src/validators.ts +0 -106
|
@@ -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']\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 space: ' ',\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, splitKey = ','): string[] {\n return keys.split(splitKey)\n}\n\nexport function parseHotkey(hotkey: string, combinationKey = '+', description?: string): Hotkey {\n const keys = hotkey\n .toLocaleLowerCase()\n .split(combinationKey)\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 }\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.key === undefined) {\n // Synthetic event (e.g., Chrome autofill). Ignore.\n return\n }\n\n console.log('keydown', e.key, mapKey(e.key), e.key.length)\n\n pushToCurrentlyPressedKeys([mapKey(e.key)])\n })\n\n document.addEventListener('keyup', (e) => {\n if (e.key === undefined) {\n // Synthetic event (e.g., Chrome autofill). Ignore.\n return\n }\n\n removeFromCurrentlyPressedKeys([mapKey(e.key)])\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[], splitKey = ','): boolean {\n const hotkeyArray = isReadonlyArray(key) ? key : key.split(splitKey)\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'\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 === true)\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 } = hotkey\n const { key: pressedKeyUppercase, ctrlKey, metaKey, shiftKey, altKey } = e\n\n const pressedKey = pressedKeyUppercase.toLowerCase()\n\n if (\n !keys?.includes(pressedKey) &&\n !['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(pressedKey)\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 && pressedKey !== 'alt') {\n return false\n }\n\n if (shift === !shiftKey && pressedKey !== '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 && pressedKey !== 'meta' && pressedKey !== 'os') {\n return false\n }\n\n if (ctrl === !ctrlKey && pressedKey !== 'ctrl' && pressedKey !== '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(pressedKey)) {\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?.splitKey) : 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?.splitKey).forEach((key) => {\n const hotkey = parseHotkey(key, memoisedOptions?.combinationKey)\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.key === undefined) {\n // Synthetic event (e.g., Chrome autofill). Ignore.\n return\n }\n\n pushToCurrentlyPressedKeys(mapKey(event.key))\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.key === undefined) {\n // Synthetic event (e.g., Chrome autofill). Ignore.\n return\n }\n\n removeFromCurrentlyPressedKeys(mapKey(event.key))\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?.splitKey).forEach((key) =>\n proxy.addHotkey(parseHotkey(key, memoisedOptions?.combinationKey, memoisedOptions?.description))\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?.splitKey).forEach((key) =>\n proxy.removeHotkey(parseHotkey(key, memoisedOptions?.combinationKey, memoisedOptions?.description))\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.key === 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.key))\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 return [keys, { start, stop, isRecording }] as const\n}\n"],"names":["reservedModifierKeywords","mappedKeys","esc","left","right","up","down","space","ShiftLeft","ShiftRight","AltLeft","AltRight","MetaLeft","MetaRight","OSLeft","OSRight","ControlLeft","ControlRight","mapKey","key","trim","toLowerCase","replace","isHotkeyModifier","includes","parseKeysHookInput","keys","splitKey","split","parseHotkey","hotkey","combinationKey","description","toLocaleLowerCase","map","k","modifiers","alt","ctrl","shift","meta","mod","singleCharKeys","filter","_extends","document","addEventListener","e","undefined","console","log","length","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","warn","scope","isHotkeyMatchingKeyboardEvent","ignoreModifiers","pressedKeyUppercase","ctrlKey","metaKey","shiftKey","altKey","pressedKey","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"],"mappings":";;;;;;;;;;;;;;;;;;AAEA,IAAMA,wBAAwB,GAAG,CAAC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC;AAExE,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,KAAK,EAAE,GAAG;EACVC,SAAS,EAAE,OAAO;EAClBC,UAAU,EAAE,OAAO;EACnBC,OAAO,EAAE,KAAK;EACdC,QAAQ,EAAE,KAAK;EACfC,QAAQ,EAAE,MAAM;EAChBC,SAAS,EAAE,MAAM;EACjBC,MAAM,EAAE,MAAM;EACdC,OAAO,EAAE,MAAM;EACfC,WAAW,EAAE,MAAM;EACnBC,YAAY,EAAE;CACf;SAEeC,MAAMA,CAACC,GAAW;EAChC,OAAO,CAAClB,UAAU,CAACkB,GAAG,CAACC,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AAO,CAACtH,GAAG,CAACjD,MAAM,CAAC4J,KAAK,CAAC3J,GAAG,CAAC,CAAC;MAE9B,OAAOsK,OAAO;KACf,CAAC;GACH,EAAE,EAAE,CAAC;EAEN,IAAMC,IAAI,GAAG1D,WAAW,CAAC;IACvB,IAAI,OAAOnF,QAAQ,KAAK,WAAW,EAAE;MACnCA,QAAQ,CAACsI,mBAAmB,CAAC,SAAS,EAAEK,OAAO,CAAC;MAEhDD,cAAc,CAAC,KAAK,CAAC;;GAExB,EAAE,CAACC,OAAO,CAAC,CAAC;EAEb,IAAMG,KAAK,GAAG3D,WAAW,CAAC;IACxBqD,OAAO,CAAC,IAAI5H,GAAG,EAAU,CAAC;IAE1B,IAAI,OAAOZ,QAAQ,KAAK,WAAW,EAAE;MACnC6I,IAAI,EAAE;MAEN7I,QAAQ,CAACC,gBAAgB,CAAC,SAAS,EAAE0I,OAAO,CAAC;MAE7CD,cAAc,CAAC,IAAI,CAAC;;GAEvB,EAAE,CAACC,OAAO,EAAEE,IAAI,CAAC,CAAC;EAEnB,OAAO,CAAChK,IAAI,EAAE;IAAEiK,KAAK,EAALA,KAAK;IAAED,IAAI,EAAJA,IAAI;IAAEJ,WAAW,EAAXA;GAAa,CAAU;AACtD;;;;"}
|
package/dist/setupTests.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import '@testing-library/jest-dom';
|
package/dist/types.d.ts
DELETED
|
@@ -1,36 +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
|
-
};
|
|
13
|
-
export declare type Hotkey = KeyboardModifiers & {
|
|
14
|
-
keys?: readonly string[];
|
|
15
|
-
scopes?: Scopes;
|
|
16
|
-
description?: string;
|
|
17
|
-
};
|
|
18
|
-
export declare type HotkeysEvent = Hotkey;
|
|
19
|
-
export declare type HotkeyCallback = (keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => void;
|
|
20
|
-
export declare type Trigger = boolean | ((keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => boolean);
|
|
21
|
-
export declare type Options = {
|
|
22
|
-
enabled?: Trigger;
|
|
23
|
-
enableOnFormTags?: readonly FormTags[] | boolean;
|
|
24
|
-
enableOnContentEditable?: boolean;
|
|
25
|
-
ignoreEventWhen?: (e: KeyboardEvent) => boolean;
|
|
26
|
-
combinationKey?: string;
|
|
27
|
-
splitKey?: string;
|
|
28
|
-
scopes?: Scopes;
|
|
29
|
-
keyup?: boolean;
|
|
30
|
-
keydown?: boolean;
|
|
31
|
-
preventDefault?: Trigger;
|
|
32
|
-
description?: string;
|
|
33
|
-
document?: Document;
|
|
34
|
-
ignoreModifiers?: boolean;
|
|
35
|
-
};
|
|
36
|
-
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
|
-
}
|
package/src/HotkeysProvider.tsx
DELETED
|
@@ -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
|
-
}
|
package/src/isHotkeyPressed.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { isHotkeyModifier, mapKey } from './parseHotkeys'
|
|
2
|
-
;(() => {
|
|
3
|
-
if (typeof document !== 'undefined') {
|
|
4
|
-
document.addEventListener('keydown', (e) => {
|
|
5
|
-
if (e.key === undefined) {
|
|
6
|
-
// Synthetic event (e.g., Chrome autofill). Ignore.
|
|
7
|
-
return
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
console.log('keydown', e.key, mapKey(e.key), e.key.length)
|
|
11
|
-
|
|
12
|
-
pushToCurrentlyPressedKeys([mapKey(e.key)])
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
document.addEventListener('keyup', (e) => {
|
|
16
|
-
if (e.key === undefined) {
|
|
17
|
-
// Synthetic event (e.g., Chrome autofill). Ignore.
|
|
18
|
-
return
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
removeFromCurrentlyPressedKeys([mapKey(e.key)])
|
|
22
|
-
})
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (typeof window !== 'undefined') {
|
|
26
|
-
window.addEventListener('blur', () => {
|
|
27
|
-
currentlyPressedKeys.clear()
|
|
28
|
-
})
|
|
29
|
-
}
|
|
30
|
-
})()
|
|
31
|
-
|
|
32
|
-
const currentlyPressedKeys: Set<string> = new Set<string>()
|
|
33
|
-
|
|
34
|
-
// https://github.com/microsoft/TypeScript/issues/17002
|
|
35
|
-
export function isReadonlyArray(value: unknown): value is readonly unknown[] {
|
|
36
|
-
return Array.isArray(value)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function isHotkeyPressed(key: string | readonly string[], splitKey = ','): boolean {
|
|
40
|
-
const hotkeyArray = isReadonlyArray(key) ? key : key.split(splitKey)
|
|
41
|
-
|
|
42
|
-
return hotkeyArray.every((hotkey) => currentlyPressedKeys.has(hotkey.trim().toLowerCase()))
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function pushToCurrentlyPressedKeys(key: string | string[]): void {
|
|
46
|
-
const hotkeyArray = Array.isArray(key) ? key : [key]
|
|
47
|
-
|
|
48
|
-
/*
|
|
49
|
-
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.
|
|
50
|
-
https://stackoverflow.com/questions/11818637/why-does-javascript-drop-keyup-events-when-the-metakey-is-pressed-on-mac-browser
|
|
51
|
-
Otherwise the set will hold all ever pressed keys while the meta key is down which leads to wrong results.
|
|
52
|
-
*/
|
|
53
|
-
if (currentlyPressedKeys.has('meta')) {
|
|
54
|
-
currentlyPressedKeys.forEach((key) => !isHotkeyModifier(key) && currentlyPressedKeys.delete(key.toLowerCase()))
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
hotkeyArray.forEach((hotkey) => currentlyPressedKeys.add(hotkey.toLowerCase()))
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function removeFromCurrentlyPressedKeys(key: string | string[]): void {
|
|
61
|
-
const hotkeyArray = Array.isArray(key) ? key : [key]
|
|
62
|
-
|
|
63
|
-
/*
|
|
64
|
-
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.
|
|
65
|
-
https://stackoverflow.com/questions/11818637/why-does-javascript-drop-keyup-events-when-the-metakey-is-pressed-on-mac-browser
|
|
66
|
-
Otherwise the set will hold all ever pressed keys while the meta key is down which leads to wrong results.
|
|
67
|
-
*/
|
|
68
|
-
if (key === 'meta') {
|
|
69
|
-
currentlyPressedKeys.clear()
|
|
70
|
-
} else {
|
|
71
|
-
hotkeyArray.forEach((hotkey) => currentlyPressedKeys.delete(hotkey.toLowerCase()))
|
|
72
|
-
}
|
|
73
|
-
}
|
package/src/parseHotkeys.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { Hotkey, KeyboardModifiers } from './types'
|
|
2
|
-
|
|
3
|
-
const reservedModifierKeywords = ['shift', 'alt', 'meta', 'mod', 'ctrl']
|
|
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
|
-
space: ' ',
|
|
13
|
-
ShiftLeft: 'shift',
|
|
14
|
-
ShiftRight: 'shift',
|
|
15
|
-
AltLeft: 'alt',
|
|
16
|
-
AltRight: 'alt',
|
|
17
|
-
MetaLeft: 'meta',
|
|
18
|
-
MetaRight: 'meta',
|
|
19
|
-
OSLeft: 'meta',
|
|
20
|
-
OSRight: 'meta',
|
|
21
|
-
ControlLeft: 'ctrl',
|
|
22
|
-
ControlRight: 'ctrl',
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function mapKey(key: string): string {
|
|
26
|
-
return (mappedKeys[key.trim()] || key.trim()).toLowerCase().replace(/key|digit|numpad/, '')
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function isHotkeyModifier(key: string) {
|
|
30
|
-
return reservedModifierKeywords.includes(key)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function parseKeysHookInput(keys: string, splitKey = ','): string[] {
|
|
34
|
-
return keys.split(splitKey)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function parseHotkey(hotkey: string, combinationKey = '+', description?: string): Hotkey {
|
|
38
|
-
const keys = hotkey
|
|
39
|
-
.toLocaleLowerCase()
|
|
40
|
-
.split(combinationKey)
|
|
41
|
-
.map((k) => mapKey(k))
|
|
42
|
-
|
|
43
|
-
const modifiers: KeyboardModifiers = {
|
|
44
|
-
alt: keys.includes('alt'),
|
|
45
|
-
ctrl: keys.includes('ctrl') || keys.includes('control'),
|
|
46
|
-
shift: keys.includes('shift'),
|
|
47
|
-
meta: keys.includes('meta'),
|
|
48
|
-
mod: keys.includes('mod'),
|
|
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,45 +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
|
-
}
|
|
16
|
-
|
|
17
|
-
export type Hotkey = KeyboardModifiers & {
|
|
18
|
-
keys?: readonly string[]
|
|
19
|
-
scopes?: Scopes
|
|
20
|
-
description?: string
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export type HotkeysEvent = Hotkey
|
|
24
|
-
|
|
25
|
-
export type HotkeyCallback = (keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => void
|
|
26
|
-
|
|
27
|
-
export type Trigger = boolean | ((keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => boolean)
|
|
28
|
-
|
|
29
|
-
export type Options = {
|
|
30
|
-
enabled?: Trigger // Main setting that determines if the hotkey is enabled or not. (Default: true)
|
|
31
|
-
enableOnFormTags?: readonly FormTags[] | boolean // Enable hotkeys on a list of tags. (Default: false)
|
|
32
|
-
enableOnContentEditable?: boolean // Enable hotkeys on tags with contentEditable props. (Default: false)
|
|
33
|
-
ignoreEventWhen?: (e: KeyboardEvent) => boolean // Ignore evenets based on a condition (Default: undefined)
|
|
34
|
-
combinationKey?: string // Character to split keys in hotkeys combinations. (Default: +)
|
|
35
|
-
splitKey?: string // Character to separate different hotkeys. (Default: ,)
|
|
36
|
-
scopes?: Scopes // Scope
|
|
37
|
-
keyup?: boolean // Trigger on keyup event? (Default: undefined)
|
|
38
|
-
keydown?: boolean // Trigger on keydown event? (Default: true)
|
|
39
|
-
preventDefault?: Trigger // Prevent default browser behavior? (Default: false)
|
|
40
|
-
description?: string // Use this option to describe what the hotkey does. (Default: undefined)
|
|
41
|
-
document?: Document // Listen to events on the document instead of the window. (Default: false)
|
|
42
|
-
ignoreModifiers?: boolean // Ignore modifiers when matching hotkeys. (Default: false)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export type OptionsOrDependencyArray = Options | DependencyList
|
package/src/useDeepEqualMemo.ts
DELETED
|
@@ -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,172 +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?.splitKey) : 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?.splitKey).forEach((key) => {
|
|
87
|
-
const hotkey = parseHotkey(key, memoisedOptions?.combinationKey)
|
|
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.key === undefined) {
|
|
118
|
-
// Synthetic event (e.g., Chrome autofill). Ignore.
|
|
119
|
-
return
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
pushToCurrentlyPressedKeys(mapKey(event.key))
|
|
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.key === undefined) {
|
|
131
|
-
// Synthetic event (e.g., Chrome autofill). Ignore.
|
|
132
|
-
return
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
removeFromCurrentlyPressedKeys(mapKey(event.key))
|
|
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?.splitKey).forEach((key) =>
|
|
153
|
-
proxy.addHotkey(parseHotkey(key, memoisedOptions?.combinationKey, memoisedOptions?.description))
|
|
154
|
-
)
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return () => {
|
|
158
|
-
// @ts-ignore
|
|
159
|
-
domNode.removeEventListener('keyup', handleKeyUp)
|
|
160
|
-
// @ts-ignore
|
|
161
|
-
domNode.removeEventListener('keydown', handleKeyDown)
|
|
162
|
-
|
|
163
|
-
if (proxy) {
|
|
164
|
-
parseKeysHookInput(_keys, memoisedOptions?.splitKey).forEach((key) =>
|
|
165
|
-
proxy.removeHotkey(parseHotkey(key, memoisedOptions?.combinationKey, memoisedOptions?.description))
|
|
166
|
-
)
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}, [_keys, memoisedOptions, activeScopes])
|
|
170
|
-
|
|
171
|
-
return ref
|
|
172
|
-
}
|