Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-hotkeys-hook

Package Overview
Dependencies
Maintainers
1
Versions
113
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-hotkeys-hook - npm Package Compare versions

Comparing version 4.4.3 to 5.0.0-0

2

dist/HotkeysProvider.d.ts

@@ -5,3 +5,3 @@ import { Hotkey } from './types';

hotkeys: ReadonlyArray<Hotkey>;
enabledScopes: string[];
activeScopes: string[];
toggleScope: (scope: string) => void;

@@ -8,0 +8,0 @@ enableScope: (scope: string) => void;

@@ -25,9 +25,7 @@ 'use strict';

"return": 'enter',
'.': 'period',
',': 'comma',
'-': 'slash',
' ': 'space',
'`': 'backquote',
'#': 'backslash',
'+': 'bracketright',
left: 'arrowleft',
right: 'arrowright',
up: 'arrowup',
down: 'arrowdown',
space: ' ',
ShiftLeft: 'shift',

@@ -45,3 +43,3 @@ ShiftRight: 'shift',

function mapKey(key) {
return (mappedKeys[key] || key).trim().toLowerCase().replace(/key|digit|numpad|arrow/, '');
return (mappedKeys[key.trim()] || key.trim()).toLowerCase().replace(/key|digit|numpad/, '');
}

@@ -84,13 +82,14 @@ function isHotkeyModifier(key) {

if (e.key === undefined) {
// Synthetic event (e.g., Chrome autofill). Ignore.
// Synthetic event (e.g., Chrome autofill). Ignore.
return;
}
pushToCurrentlyPressedKeys([mapKey(e.key), mapKey(e.code)]);
console.log('keydown', e.key, mapKey(e.key), e.key.length);
pushToCurrentlyPressedKeys([mapKey(e.key)]);
});
document.addEventListener('keyup', function (e) {
if (e.key === undefined) {
// Synthetic event (e.g., Chrome autofill). Ignore.
// Synthetic event (e.g., Chrome autofill). Ignore.
return;
}
removeFromCurrentlyPressedKeys([mapKey(e.key), mapKey(e.code)]);
removeFromCurrentlyPressedKeys([mapKey(e.key)]);
});

@@ -200,3 +199,2 @@ }

var pressedKeyUppercase = e.key,
code = e.code,
ctrlKey = e.ctrlKey,

@@ -206,5 +204,4 @@ metaKey = e.metaKey,

altKey = e.altKey;
var keyCode = mapKey(code);
var pressedKey = pressedKeyUppercase.toLowerCase();
if (!(keys != null && keys.includes(keyCode)) && !['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(keyCode)) {
if (!(keys != null && keys.includes(pressedKey)) && !['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(pressedKey)) {
return false;

@@ -236,3 +233,3 @@ }

// If the key is set, we check for the key
if (keys && keys.length === 1 && (keys.includes(pressedKey) || keys.includes(keyCode))) {
if (keys && keys.length === 1 && keys.includes(pressedKey)) {
return true;

@@ -278,3 +275,3 @@ } else if (keys) {

hotkeys: [],
enabledScopes: [],
activeScopes: [],
toggleScope: function toggleScope() {},

@@ -291,3 +288,3 @@ enableScope: function enableScope() {},

children = _ref.children;
var _useState = react.useState((initiallyActiveScopes == null ? void 0 : initiallyActiveScopes.length) > 0 ? initiallyActiveScopes : ['*']),
var _useState = react.useState(initiallyActiveScopes),
internalActiveScopes = _useState[0],

@@ -308,11 +305,5 @@ setInternalActiveScopes = _useState[1];

setInternalActiveScopes(function (prev) {
if (prev.filter(function (s) {
return prev.filter(function (s) {
return s !== scope;
}).length === 0) {
return ['*'];
} else {
return prev.filter(function (s) {
return s !== scope;
});
}
});
});

@@ -323,11 +314,5 @@ }, []);

if (prev.includes(scope)) {
if (prev.filter(function (s) {
return prev.filter(function (s) {
return s !== scope;
}).length === 0) {
return ['*'];
} else {
return prev.filter(function (s) {
return s !== scope;
});
}
});
} else {

@@ -355,3 +340,3 @@ if (prev.includes('*')) {

value: {
enabledScopes: internalActiveScopes,
activeScopes: internalActiveScopes,
hotkeys: boundHotkeys,

@@ -399,6 +384,6 @@ enableScope: enableScope,

var _useHotkeysContext = useHotkeysContext(),
enabledScopes = _useHotkeysContext.enabledScopes;
activeScopes = _useHotkeysContext.activeScopes;
var proxy = useBoundHotkeysProxy();
useSafeLayoutEffect(function () {
if ((memoisedOptions == null ? void 0 : memoisedOptions.enabled) === false || !isScopeActive(enabledScopes, memoisedOptions == null ? void 0 : memoisedOptions.scopes)) {
if ((memoisedOptions == null ? void 0 : memoisedOptions.enabled) === false || !isScopeActive(activeScopes, memoisedOptions == null ? void 0 : memoisedOptions.scopes)) {
return;

@@ -454,3 +439,3 @@ }

}
pushToCurrentlyPressedKeys(mapKey(event.code));
pushToCurrentlyPressedKeys(mapKey(event.key));
if ((memoisedOptions == null ? void 0 : memoisedOptions.keydown) === undefined && (memoisedOptions == null ? void 0 : memoisedOptions.keyup) !== true || memoisedOptions != null && memoisedOptions.keydown) {

@@ -465,3 +450,3 @@ listener(event);

}
removeFromCurrentlyPressedKeys(mapKey(event.code));
removeFromCurrentlyPressedKeys(mapKey(event.key));
hasTriggeredRef.current = false;

@@ -493,3 +478,3 @@ if (memoisedOptions != null && memoisedOptions.keyup) {

};
}, [_keys, memoisedOptions, enabledScopes]);
}, [_keys, memoisedOptions, activeScopes]);
return ref;

@@ -514,3 +499,3 @@ }

var newKeys = new Set(prev);
newKeys.add(mapKey(event.code));
newKeys.add(mapKey(event.key));
return newKeys;

@@ -517,0 +502,0 @@ });

@@ -1,2 +0,2 @@

"use strict";var e=require("react"),t=require("react/jsx-runtime");function n(){return(n=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e}).apply(this,arguments)}var o=["shift","alt","meta","mod","ctrl"],r={esc:"escape",return:"enter",".":"period",",":"comma","-":"slash"," ":"space","`":"backquote","#":"backslash","+":"bracketright",ShiftLeft:"shift",ShiftRight:"shift",AltLeft:"alt",AltRight:"alt",MetaLeft:"meta",MetaRight:"meta",OSLeft:"meta",OSRight:"meta",ControlLeft:"ctrl",ControlRight:"ctrl"};function i(e){return(r[e]||e).trim().toLowerCase().replace(/key|digit|numpad|arrow/,"")}function u(e,t){return void 0===t&&(t=","),e.split(t)}function c(e,t,r){void 0===t&&(t="+");var u=e.toLocaleLowerCase().split(t).map((function(e){return i(e)}));return n({},{alt:u.includes("alt"),ctrl:u.includes("ctrl")||u.includes("control"),shift:u.includes("shift"),meta:u.includes("meta"),mod:u.includes("mod")},{keys:u.filter((function(e){return!o.includes(e)})),description:r})}"undefined"!=typeof document&&(document.addEventListener("keydown",(function(e){void 0!==e.key&&d([i(e.key),i(e.code)])})),document.addEventListener("keyup",(function(e){void 0!==e.key&&f([i(e.key),i(e.code)])}))),"undefined"!=typeof window&&window.addEventListener("blur",(function(){a.clear()}));var a=new Set;function l(e){return Array.isArray(e)}function s(e,t){return void 0===t&&(t=","),(l(e)?e:e.split(t)).every((function(e){return a.has(e.trim().toLowerCase())}))}function d(e){var t=Array.isArray(e)?e:[e];a.has("meta")&&a.forEach((function(e){return!function(e){return o.includes(e)}(e)&&a.delete(e.toLowerCase())})),t.forEach((function(e){return a.add(e.toLowerCase())}))}function f(e){var t=Array.isArray(e)?e:[e];"meta"===e?a.clear():t.forEach((function(e){return a.delete(e.toLowerCase())}))}function v(e,t){var n=e.target;void 0===t&&(t=!1);var o=n&&n.tagName;return l(t)?Boolean(o&&t&&t.some((function(e){return e.toLowerCase()===o.toLowerCase()}))):Boolean(o&&t&&!0===t)}var y=e.createContext(void 0);function p(e){return t.jsx(y.Provider,{value:{addHotkey:e.addHotkey,removeHotkey:e.removeHotkey},children:e.children})}function k(e,t){return e&&t&&"object"==typeof e&&"object"==typeof t?Object.keys(e).length===Object.keys(t).length&&Object.keys(e).reduce((function(n,o){return n&&k(e[o],t[o])}),!0):e===t}var m=e.createContext({hotkeys:[],enabledScopes:[],toggleScope:function(){},enableScope:function(){},disableScope:function(){}}),h=function(){return e.useContext(m)},b=function(e){e.stopPropagation(),e.preventDefault(),e.stopImmediatePropagation()},w="undefined"!=typeof window?e.useLayoutEffect:e.useEffect;exports.HotkeysProvider=function(n){var o=n.initiallyActiveScopes,r=void 0===o?["*"]:o,i=n.children,u=e.useState((null==r?void 0:r.length)>0?r:["*"]),c=u[0],a=u[1],l=e.useState([]),s=l[0],d=l[1],f=e.useCallback((function(e){a((function(t){return t.includes("*")?[e]:Array.from(new Set([].concat(t,[e])))}))}),[]),v=e.useCallback((function(e){a((function(t){return 0===t.filter((function(t){return t!==e})).length?["*"]:t.filter((function(t){return t!==e}))}))}),[]),y=e.useCallback((function(e){a((function(t){return t.includes(e)?0===t.filter((function(t){return t!==e})).length?["*"]:t.filter((function(t){return t!==e})):t.includes("*")?[e]:Array.from(new Set([].concat(t,[e])))}))}),[]),h=e.useCallback((function(e){d((function(t){return[].concat(t,[e])}))}),[]),b=e.useCallback((function(e){d((function(t){return t.filter((function(t){return!k(t,e)}))}))}),[]);return t.jsx(m.Provider,{value:{enabledScopes:c,hotkeys:s,enableScope:f,disableScope:v,toggleScope:y},children:t.jsx(p,{addHotkey:h,removeHotkey:b,children:i})})},exports.isHotkeyPressed=s,exports.useHotkeys=function(t,n,o,r){var a=e.useRef(null),p=e.useRef(!1),m=o instanceof Array?r instanceof Array?void 0:r:o,g=l(t)?t.join(null==m?void 0:m.splitKey):t,C=o instanceof Array?o:r instanceof Array?r:void 0,L=e.useCallback(n,null!=C?C:[]),S=e.useRef(L);S.current=C?L:n;var E=function(t){var n=e.useRef(void 0);return k(n.current,t)||(n.current=t),n.current}(m),A=h().enabledScopes,x=e.useContext(y);return w((function(){if(!1!==(null==E?void 0:E.enabled)&&(t=null==E?void 0:E.scopes,0===(e=A).length&&t?(console.warn('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>'),1):!t||e.some((function(e){return t.includes(e)}))||e.includes("*"))){var e,t,n=function(e,t){var n;if(void 0===t&&(t=!1),!v(e,["input","textarea","select"])||v(e,null==E?void 0:E.enableOnFormTags)){if(null!==a.current){var o=a.current.getRootNode();if((o instanceof Document||o instanceof ShadowRoot)&&o.activeElement!==a.current&&!a.current.contains(o.activeElement))return void b(e)}(null==(n=e.target)||!n.isContentEditable||null!=E&&E.enableOnContentEditable)&&u(g,null==E?void 0:E.splitKey).forEach((function(n){var o,r=c(n,null==E?void 0:E.combinationKey);if(function(e,t,n){void 0===n&&(n=!1);var o=t.alt,r=t.meta,u=t.mod,c=t.shift,a=t.ctrl,l=t.keys,d=e.key,f=e.ctrlKey,v=e.metaKey,y=e.shiftKey,p=e.altKey,k=i(e.code),m=d.toLowerCase();if(!(null!=l&&l.includes(k)||["ctrl","control","unknown","meta","alt","shift","os"].includes(k)))return!1;if(!n){if(o===!p&&"alt"!==m)return!1;if(c===!y&&"shift"!==m)return!1;if(u){if(!v&&!f)return!1}else{if(r===!v&&"meta"!==m&&"os"!==m)return!1;if(a===!f&&"ctrl"!==m&&"control"!==m)return!1}}return!(!l||1!==l.length||!l.includes(m)&&!l.includes(k))||(l?s(l):!l)}(e,r,null==E?void 0:E.ignoreModifiers)||null!=(o=r.keys)&&o.includes("*")){if(null!=E&&null!=E.ignoreEventWhen&&E.ignoreEventWhen(e))return;if(t&&p.current)return;if(function(e,t,n){("function"==typeof n&&n(e,t)||!0===n)&&e.preventDefault()}(e,r,null==E?void 0:E.preventDefault),!function(e,t,n){return"function"==typeof n?n(e,t):!0===n||void 0===n}(e,r,null==E?void 0:E.enabled))return void b(e);S.current(e,r),t||(p.current=!0)}}))}},o=function(e){void 0!==e.key&&(d(i(e.code)),(void 0===(null==E?void 0:E.keydown)&&!0!==(null==E?void 0:E.keyup)||null!=E&&E.keydown)&&n(e))},r=function(e){void 0!==e.key&&(f(i(e.code)),p.current=!1,null!=E&&E.keyup&&n(e,!0))},l=a.current||(null==m?void 0:m.document)||document;return l.addEventListener("keyup",r),l.addEventListener("keydown",o),x&&u(g,null==E?void 0:E.splitKey).forEach((function(e){return x.addHotkey(c(e,null==E?void 0:E.combinationKey,null==E?void 0:E.description))})),function(){l.removeEventListener("keyup",r),l.removeEventListener("keydown",o),x&&u(g,null==E?void 0:E.splitKey).forEach((function(e){return x.removeHotkey(c(e,null==E?void 0:E.combinationKey,null==E?void 0:E.description))}))}}}),[g,E,A]),a},exports.useHotkeysContext=h,exports.useRecordHotkeys=function(){var t=e.useState(new Set),n=t[0],o=t[1],r=e.useState(!1),u=r[0],c=r[1],a=e.useCallback((function(e){void 0!==e.key&&(e.preventDefault(),e.stopPropagation(),o((function(t){var n=new Set(t);return n.add(i(e.code)),n})))}),[]),l=e.useCallback((function(){"undefined"!=typeof document&&(document.removeEventListener("keydown",a),c(!1))}),[a]);return[n,{start:e.useCallback((function(){o(new Set),"undefined"!=typeof document&&(l(),document.addEventListener("keydown",a),c(!0))}),[a,l]),stop:l,isRecording:u}]};
"use strict";var e=require("react"),t=require("react/jsx-runtime");function n(){return(n=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e}).apply(this,arguments)}var o=["shift","alt","meta","mod","ctrl"],r={esc:"escape",return:"enter",left:"arrowleft",right:"arrowright",up:"arrowup",down:"arrowdown",space:" ",ShiftLeft:"shift",ShiftRight:"shift",AltLeft:"alt",AltRight:"alt",MetaLeft:"meta",MetaRight:"meta",OSLeft:"meta",OSRight:"meta",ControlLeft:"ctrl",ControlRight:"ctrl"};function i(e){return(r[e.trim()]||e.trim()).toLowerCase().replace(/key|digit|numpad/,"")}function u(e,t){return void 0===t&&(t=","),e.split(t)}function c(e,t,r){void 0===t&&(t="+");var u=e.toLocaleLowerCase().split(t).map((function(e){return i(e)}));return n({},{alt:u.includes("alt"),ctrl:u.includes("ctrl")||u.includes("control"),shift:u.includes("shift"),meta:u.includes("meta"),mod:u.includes("mod")},{keys:u.filter((function(e){return!o.includes(e)})),description:r})}"undefined"!=typeof document&&(document.addEventListener("keydown",(function(e){void 0!==e.key&&(console.log("keydown",e.key,i(e.key),e.key.length),f([i(e.key)]))})),document.addEventListener("keyup",(function(e){void 0!==e.key&&d([i(e.key)])}))),"undefined"!=typeof window&&window.addEventListener("blur",(function(){a.clear()}));var a=new Set;function l(e){return Array.isArray(e)}function s(e,t){return void 0===t&&(t=","),(l(e)?e:e.split(t)).every((function(e){return a.has(e.trim().toLowerCase())}))}function f(e){var t=Array.isArray(e)?e:[e];a.has("meta")&&a.forEach((function(e){return!function(e){return o.includes(e)}(e)&&a.delete(e.toLowerCase())})),t.forEach((function(e){return a.add(e.toLowerCase())}))}function d(e){var t=Array.isArray(e)?e:[e];"meta"===e?a.clear():t.forEach((function(e){return a.delete(e.toLowerCase())}))}function v(e,t){var n=e.target;void 0===t&&(t=!1);var o=n&&n.tagName;return l(t)?Boolean(o&&t&&t.some((function(e){return e.toLowerCase()===o.toLowerCase()}))):Boolean(o&&t&&!0===t)}var y=e.createContext(void 0);function p(e){return t.jsx(y.Provider,{value:{addHotkey:e.addHotkey,removeHotkey:e.removeHotkey},children:e.children})}function k(e,t){return e&&t&&"object"==typeof e&&"object"==typeof t?Object.keys(e).length===Object.keys(t).length&&Object.keys(e).reduce((function(n,o){return n&&k(e[o],t[o])}),!0):e===t}var m=e.createContext({hotkeys:[],activeScopes:[],toggleScope:function(){},enableScope:function(){},disableScope:function(){}}),h=function(){return e.useContext(m)},w=function(e){e.stopPropagation(),e.preventDefault(),e.stopImmediatePropagation()},g="undefined"!=typeof window?e.useLayoutEffect:e.useEffect;exports.HotkeysProvider=function(n){var o=n.initiallyActiveScopes,r=n.children,i=e.useState(void 0===o?["*"]:o),u=i[0],c=i[1],a=e.useState([]),l=a[0],s=a[1],f=e.useCallback((function(e){c((function(t){return t.includes("*")?[e]:Array.from(new Set([].concat(t,[e])))}))}),[]),d=e.useCallback((function(e){c((function(t){return t.filter((function(t){return t!==e}))}))}),[]),v=e.useCallback((function(e){c((function(t){return t.includes(e)?t.filter((function(t){return t!==e})):t.includes("*")?[e]:Array.from(new Set([].concat(t,[e])))}))}),[]),y=e.useCallback((function(e){s((function(t){return[].concat(t,[e])}))}),[]),h=e.useCallback((function(e){s((function(t){return t.filter((function(t){return!k(t,e)}))}))}),[]);return t.jsx(m.Provider,{value:{activeScopes:u,hotkeys:l,enableScope:f,disableScope:d,toggleScope:v},children:t.jsx(p,{addHotkey:y,removeHotkey:h,children:r})})},exports.isHotkeyPressed=s,exports.useHotkeys=function(t,n,o,r){var a=e.useRef(null),p=e.useRef(!1),m=o instanceof Array?r instanceof Array?void 0:r:o,b=l(t)?t.join(null==m?void 0:m.splitKey):t,C=o instanceof Array?o:r instanceof Array?r:void 0,L=e.useCallback(n,null!=C?C:[]),S=e.useRef(L);S.current=C?L:n;var E=function(t){var n=e.useRef(void 0);return k(n.current,t)||(n.current=t),n.current}(m),A=h().activeScopes,x=e.useContext(y);return g((function(){if(!1!==(null==E?void 0:E.enabled)&&function(e,t){return 0===e.length&&t?(console.warn('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>'),!0):!t||e.some((function(e){return t.includes(e)}))||e.includes("*")}(A,null==E?void 0:E.scopes)){var e=function(e,t){var n;if(void 0===t&&(t=!1),!v(e,["input","textarea","select"])||v(e,null==E?void 0:E.enableOnFormTags)){if(null!==a.current){var o=a.current.getRootNode();if((o instanceof Document||o instanceof ShadowRoot)&&o.activeElement!==a.current&&!a.current.contains(o.activeElement))return void w(e)}(null==(n=e.target)||!n.isContentEditable||null!=E&&E.enableOnContentEditable)&&u(b,null==E?void 0:E.splitKey).forEach((function(n){var o,r=c(n,null==E?void 0:E.combinationKey);if(function(e,t,n){void 0===n&&(n=!1);var o=t.alt,r=t.meta,i=t.mod,u=t.shift,c=t.ctrl,a=t.keys,l=e.ctrlKey,f=e.metaKey,d=e.shiftKey,v=e.altKey,y=e.key.toLowerCase();if(!(null!=a&&a.includes(y)||["ctrl","control","unknown","meta","alt","shift","os"].includes(y)))return!1;if(!n){if(o===!v&&"alt"!==y)return!1;if(u===!d&&"shift"!==y)return!1;if(i){if(!f&&!l)return!1}else{if(r===!f&&"meta"!==y&&"os"!==y)return!1;if(c===!l&&"ctrl"!==y&&"control"!==y)return!1}}return!(!a||1!==a.length||!a.includes(y))||(a?s(a):!a)}(e,r,null==E?void 0:E.ignoreModifiers)||null!=(o=r.keys)&&o.includes("*")){if(null!=E&&null!=E.ignoreEventWhen&&E.ignoreEventWhen(e))return;if(t&&p.current)return;if(function(e,t,n){("function"==typeof n&&n(e,t)||!0===n)&&e.preventDefault()}(e,r,null==E?void 0:E.preventDefault),!function(e,t,n){return"function"==typeof n?n(e,t):!0===n||void 0===n}(e,r,null==E?void 0:E.enabled))return void w(e);S.current(e,r),t||(p.current=!0)}}))}},t=function(t){void 0!==t.key&&(f(i(t.key)),(void 0===(null==E?void 0:E.keydown)&&!0!==(null==E?void 0:E.keyup)||null!=E&&E.keydown)&&e(t))},n=function(t){void 0!==t.key&&(d(i(t.key)),p.current=!1,null!=E&&E.keyup&&e(t,!0))},o=a.current||(null==m?void 0:m.document)||document;return o.addEventListener("keyup",n),o.addEventListener("keydown",t),x&&u(b,null==E?void 0:E.splitKey).forEach((function(e){return x.addHotkey(c(e,null==E?void 0:E.combinationKey,null==E?void 0:E.description))})),function(){o.removeEventListener("keyup",n),o.removeEventListener("keydown",t),x&&u(b,null==E?void 0:E.splitKey).forEach((function(e){return x.removeHotkey(c(e,null==E?void 0:E.combinationKey,null==E?void 0:E.description))}))}}}),[b,E,A]),a},exports.useHotkeysContext=h,exports.useRecordHotkeys=function(){var t=e.useState(new Set),n=t[0],o=t[1],r=e.useState(!1),u=r[0],c=r[1],a=e.useCallback((function(e){void 0!==e.key&&(e.preventDefault(),e.stopPropagation(),o((function(t){var n=new Set(t);return n.add(i(e.key)),n})))}),[]),l=e.useCallback((function(){"undefined"!=typeof document&&(document.removeEventListener("keydown",a),c(!1))}),[a]);return[n,{start:e.useCallback((function(){o(new Set),"undefined"!=typeof document&&(l(),document.addEventListener("keydown",a),c(!0))}),[a,l]),stop:l,isRecording:u}]};
//# sourceMappingURL=react-hotkeys-hook.cjs.production.min.js.map

@@ -23,9 +23,7 @@ import { useContext, createContext, useState, useCallback, useRef, useLayoutEffect, useEffect } from 'react';

"return": 'enter',
'.': 'period',
',': 'comma',
'-': 'slash',
' ': 'space',
'`': 'backquote',
'#': 'backslash',
'+': 'bracketright',
left: 'arrowleft',
right: 'arrowright',
up: 'arrowup',
down: 'arrowdown',
space: ' ',
ShiftLeft: 'shift',

@@ -43,3 +41,3 @@ ShiftRight: 'shift',

function mapKey(key) {
return (mappedKeys[key] || key).trim().toLowerCase().replace(/key|digit|numpad|arrow/, '');
return (mappedKeys[key.trim()] || key.trim()).toLowerCase().replace(/key|digit|numpad/, '');
}

@@ -82,13 +80,14 @@ function isHotkeyModifier(key) {

if (e.key === undefined) {
// Synthetic event (e.g., Chrome autofill). Ignore.
// Synthetic event (e.g., Chrome autofill). Ignore.
return;
}
pushToCurrentlyPressedKeys([mapKey(e.key), mapKey(e.code)]);
console.log('keydown', e.key, mapKey(e.key), e.key.length);
pushToCurrentlyPressedKeys([mapKey(e.key)]);
});
document.addEventListener('keyup', function (e) {
if (e.key === undefined) {
// Synthetic event (e.g., Chrome autofill). Ignore.
// Synthetic event (e.g., Chrome autofill). Ignore.
return;
}
removeFromCurrentlyPressedKeys([mapKey(e.key), mapKey(e.code)]);
removeFromCurrentlyPressedKeys([mapKey(e.key)]);
});

@@ -198,3 +197,2 @@ }

var pressedKeyUppercase = e.key,
code = e.code,
ctrlKey = e.ctrlKey,

@@ -204,5 +202,4 @@ metaKey = e.metaKey,

altKey = e.altKey;
var keyCode = mapKey(code);
var pressedKey = pressedKeyUppercase.toLowerCase();
if (!(keys != null && keys.includes(keyCode)) && !['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(keyCode)) {
if (!(keys != null && keys.includes(pressedKey)) && !['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(pressedKey)) {
return false;

@@ -234,3 +231,3 @@ }

// If the key is set, we check for the key
if (keys && keys.length === 1 && (keys.includes(pressedKey) || keys.includes(keyCode))) {
if (keys && keys.length === 1 && keys.includes(pressedKey)) {
return true;

@@ -276,3 +273,3 @@ } else if (keys) {

hotkeys: [],
enabledScopes: [],
activeScopes: [],
toggleScope: function toggleScope() {},

@@ -289,3 +286,3 @@ enableScope: function enableScope() {},

children = _ref.children;
var _useState = useState((initiallyActiveScopes == null ? void 0 : initiallyActiveScopes.length) > 0 ? initiallyActiveScopes : ['*']),
var _useState = useState(initiallyActiveScopes),
internalActiveScopes = _useState[0],

@@ -306,11 +303,5 @@ setInternalActiveScopes = _useState[1];

setInternalActiveScopes(function (prev) {
if (prev.filter(function (s) {
return prev.filter(function (s) {
return s !== scope;
}).length === 0) {
return ['*'];
} else {
return prev.filter(function (s) {
return s !== scope;
});
}
});
});

@@ -321,11 +312,5 @@ }, []);

if (prev.includes(scope)) {
if (prev.filter(function (s) {
return prev.filter(function (s) {
return s !== scope;
}).length === 0) {
return ['*'];
} else {
return prev.filter(function (s) {
return s !== scope;
});
}
});
} else {

@@ -353,3 +338,3 @@ if (prev.includes('*')) {

value: {
enabledScopes: internalActiveScopes,
activeScopes: internalActiveScopes,
hotkeys: boundHotkeys,

@@ -397,6 +382,6 @@ enableScope: enableScope,

var _useHotkeysContext = useHotkeysContext(),
enabledScopes = _useHotkeysContext.enabledScopes;
activeScopes = _useHotkeysContext.activeScopes;
var proxy = useBoundHotkeysProxy();
useSafeLayoutEffect(function () {
if ((memoisedOptions == null ? void 0 : memoisedOptions.enabled) === false || !isScopeActive(enabledScopes, memoisedOptions == null ? void 0 : memoisedOptions.scopes)) {
if ((memoisedOptions == null ? void 0 : memoisedOptions.enabled) === false || !isScopeActive(activeScopes, memoisedOptions == null ? void 0 : memoisedOptions.scopes)) {
return;

@@ -452,3 +437,3 @@ }

}
pushToCurrentlyPressedKeys(mapKey(event.code));
pushToCurrentlyPressedKeys(mapKey(event.key));
if ((memoisedOptions == null ? void 0 : memoisedOptions.keydown) === undefined && (memoisedOptions == null ? void 0 : memoisedOptions.keyup) !== true || memoisedOptions != null && memoisedOptions.keydown) {

@@ -463,3 +448,3 @@ listener(event);

}
removeFromCurrentlyPressedKeys(mapKey(event.code));
removeFromCurrentlyPressedKeys(mapKey(event.key));
hasTriggeredRef.current = false;

@@ -491,3 +476,3 @@ if (memoisedOptions != null && memoisedOptions.keyup) {

};
}, [_keys, memoisedOptions, enabledScopes]);
}, [_keys, memoisedOptions, activeScopes]);
return ref;

@@ -512,3 +497,3 @@ }

var newKeys = new Set(prev);
newKeys.add(mapKey(event.code));
newKeys.add(mapKey(event.key));
return newKeys;

@@ -515,0 +500,0 @@ });

{
"name": "react-hotkeys-hook",
"description": "React hook for handling keyboard shortcuts",
"version": "4.4.3",
"version": "5.0.0-0",
"repository": {

@@ -6,0 +6,0 @@ "type": "git",

@@ -6,7 +6,9 @@ import { isHotkeyModifier, mapKey } from './parseHotkeys'

if (e.key === undefined) {
// Synthetic event (e.g., Chrome autofill). Ignore.
// Synthetic event (e.g., Chrome autofill). Ignore.
return
}
pushToCurrentlyPressedKeys([mapKey(e.key), mapKey(e.code)])
console.log('keydown', e.key, mapKey(e.key), e.key.length)
pushToCurrentlyPressedKeys([mapKey(e.key)])
})

@@ -16,7 +18,7 @@

if (e.key === undefined) {
// Synthetic event (e.g., Chrome autofill). Ignore.
// Synthetic event (e.g., Chrome autofill). Ignore.
return
}
removeFromCurrentlyPressedKeys([mapKey(e.key), mapKey(e.code)])
removeFromCurrentlyPressedKeys([mapKey(e.key)])
})

@@ -23,0 +25,0 @@ }

@@ -8,9 +8,7 @@ import { Hotkey, KeyboardModifiers } from './types'

return: 'enter',
'.': 'period',
',': 'comma',
'-': 'slash',
' ': 'space',
'`': 'backquote',
'#': 'backslash',
'+': 'bracketright',
left: 'arrowleft',
right: 'arrowright',
up: 'arrowup',
down: 'arrowdown',
space: ' ',
ShiftLeft: 'shift',

@@ -29,6 +27,3 @@ ShiftRight: 'shift',

export function mapKey(key: string): string {
return (mappedKeys[key] || key)
.trim()
.toLowerCase()
.replace(/key|digit|numpad|arrow/, '')
return (mappedKeys[key.trim()] || key.trim()).toLowerCase().replace(/key|digit|numpad/, '')
}

@@ -35,0 +30,0 @@

@@ -54,7 +54,7 @@ import { HotkeyCallback, Keys, Options, OptionsOrDependencyArray, RefType } from './types'

const { enabledScopes } = useHotkeysContext()
const { activeScopes } = useHotkeysContext()
const proxy = useBoundHotkeysProxy()
useSafeLayoutEffect(() => {
if (memoisedOptions?.enabled === false || !isScopeActive(enabledScopes, memoisedOptions?.scopes)) {
if (memoisedOptions?.enabled === false || !isScopeActive(activeScopes, memoisedOptions?.scopes)) {
return

@@ -72,2 +72,3 @@ }

const rootNode = ref.current.getRootNode()
if (

@@ -123,3 +124,3 @@ (rootNode instanceof Document || rootNode instanceof ShadowRoot) &&

pushToCurrentlyPressedKeys(mapKey(event.code))
pushToCurrentlyPressedKeys(mapKey(event.key))

@@ -137,3 +138,3 @@ if ((memoisedOptions?.keydown === undefined && memoisedOptions?.keyup !== true) || memoisedOptions?.keydown) {

removeFromCurrentlyPressedKeys(mapKey(event.code))
removeFromCurrentlyPressedKeys(mapKey(event.key))

@@ -172,5 +173,5 @@ hasTriggeredRef.current = false

}
}, [_keys, memoisedOptions, enabledScopes])
}, [_keys, memoisedOptions, activeScopes])
return ref
}

@@ -20,3 +20,3 @@ import { useCallback, useState } from 'react'

newKeys.add(mapKey(event.code))
newKeys.add(mapKey(event.key))

@@ -23,0 +23,0 @@ return newKeys

import { FormTags, Hotkey, Scopes, Trigger } from './types'
import { isHotkeyPressed, isReadonlyArray } from './isHotkeyPressed'
import { mapKey } from './parseHotkeys'

@@ -56,8 +55,10 @@ export function maybePreventDefault(e: KeyboardEvent, hotkey: Hotkey, preventDefault?: Trigger): void {

const { alt, meta, mod, shift, ctrl, keys } = hotkey
const { key: pressedKeyUppercase, code, ctrlKey, metaKey, shiftKey, altKey } = e
const { key: pressedKeyUppercase, ctrlKey, metaKey, shiftKey, altKey } = e
const keyCode = mapKey(code)
const pressedKey = pressedKeyUppercase.toLowerCase()
if (!keys?.includes(keyCode) && !['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(keyCode)) {
if (
!keys?.includes(pressedKey) &&
!['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(pressedKey)
) {
return false

@@ -94,3 +95,3 @@ }

// If the key is set, we check for the key
if (keys && keys.length === 1 && (keys.includes(pressedKey) || keys.includes(keyCode))) {
if (keys && keys.length === 1 && keys.includes(pressedKey)) {
return true

@@ -97,0 +98,0 @@ } else if (keys) {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc