package.dist.react-hotkeys-hook.cjs.production.min.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of react-hotkeys-hook Show documentation
Show all versions of react-hotkeys-hook Show documentation
React hook for handling keyboard shortcuts
The newest version!
"use strict";var e=require("react"),n=require("react/jsx-runtime");function t(){return(t=Object.assign?Object.assign.bind():function(e){for(var n=1;n0?r:["*"]),c=u[0],a=u[1],l=e.useState([]),s=l[0],d=l[1],f=e.useCallback((function(e){a((function(n){return n.includes("*")?[e]:Array.from(new Set([].concat(n,[e])))}))}),[]),v=e.useCallback((function(e){a((function(n){return 0===n.filter((function(n){return n!==e})).length?["*"]:n.filter((function(n){return n!==e}))}))}),[]),y=e.useCallback((function(e){a((function(n){return n.includes(e)?0===n.filter((function(n){return n!==e})).length?["*"]:n.filter((function(n){return n!==e})):n.includes("*")?[e]:Array.from(new Set([].concat(n,[e])))}))}),[]),h=e.useCallback((function(e){d((function(n){return[].concat(n,[e])}))}),[]),b=e.useCallback((function(e){d((function(n){return n.filter((function(n){return!k(n,e)}))}))}),[]);return n.jsx(m.Provider,{value:{enabledScopes:c,hotkeys:s,enableScope:f,disableScope:v,toggleScope:y},children:n.jsx(p,{addHotkey:h,removeHotkey:b,children:i})})},exports.isHotkeyPressed=s,exports.useHotkeys=function(n,t,o,r){var a=e.useState(null),p=a[0],m=a[1],g=e.useRef(!1),C=o instanceof Array?r instanceof Array?void 0:r:o,S=l(n)?n.join(null==C?void 0:C.splitKey):n,L=o instanceof Array?o:r instanceof Array?r:void 0,E=e.useCallback(t,null!=L?L:[]),A=e.useRef(E);A.current=L?E:t;var x=function(n){var t=e.useRef(void 0);return k(t.current,n)||(t.current=n),t.current}(C),H=h().enabledScopes,j=e.useContext(y);return w((function(){if(!1!==(null==x?void 0:x.enabled)&&(n=null==x?void 0:x.scopes,0===(e=H).length&&n?(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 '),1):!n||e.some((function(e){return n.includes(e)}))||e.includes("*"))){var e,n,t=function(e,n){var t;if(void 0===n&&(n=!1),!v(e,["input","textarea","select"])||v(e,null==x?void 0:x.enableOnFormTags)){if(null!==p){var o=p.getRootNode();if((o instanceof Document||o instanceof ShadowRoot)&&o.activeElement!==p&&!p.contains(o.activeElement))return void b(e)}(null==(t=e.target)||!t.isContentEditable||null!=x&&x.enableOnContentEditable)&&u(S,null==x?void 0:x.splitKey).forEach((function(t){var o,r=c(t,null==x?void 0:x.combinationKey);if(function(e,n,t){void 0===t&&(t=!1);var o=n.alt,r=n.meta,u=n.mod,c=n.shift,a=n.ctrl,l=n.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)||null!=l&&l.includes(m)||["ctrl","control","unknown","meta","alt","shift","os"].includes(k)))return!1;if(!t){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==x?void 0:x.ignoreModifiers)||null!=(o=r.keys)&&o.includes("*")){if(null!=x&&null!=x.ignoreEventWhen&&x.ignoreEventWhen(e))return;if(n&&g.current)return;if(function(e,n,t){("function"==typeof t&&t(e,n)||!0===t)&&e.preventDefault()}(e,r,null==x?void 0:x.preventDefault),!function(e,n,t){return"function"==typeof t?t(e,n):!0===t||void 0===t}(e,r,null==x?void 0:x.enabled))return void b(e);A.current(e,r),n||(g.current=!0)}}))}},o=function(e){void 0!==e.key&&(d(i(e.code)),(void 0===(null==x?void 0:x.keydown)&&!0!==(null==x?void 0:x.keyup)||null!=x&&x.keydown)&&t(e))},r=function(e){void 0!==e.key&&(f(i(e.code)),g.current=!1,null!=x&&x.keyup&&t(e,!0))},a=p||(null==C?void 0:C.document)||document;return a.addEventListener("keyup",r),a.addEventListener("keydown",o),j&&u(S,null==x?void 0:x.splitKey).forEach((function(e){return j.addHotkey(c(e,null==x?void 0:x.combinationKey,null==x?void 0:x.description))})),function(){a.removeEventListener("keyup",r),a.removeEventListener("keydown",o),j&&u(S,null==x?void 0:x.splitKey).forEach((function(e){return j.removeHotkey(c(e,null==x?void 0:x.combinationKey,null==x?void 0:x.description))}))}}}),[p,S,x,H]),m},exports.useHotkeysContext=h,exports.useRecordHotkeys=function(){var n=e.useState(new Set),t=n[0],o=n[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(n){var t=new Set(n);return t.add(i(e.code)),t})))}),[]),l=e.useCallback((function(){"undefined"!=typeof document&&(document.removeEventListener("keydown",a),c(!1))}),[a]),s=e.useCallback((function(){o(new Set),"undefined"!=typeof document&&(l(),document.addEventListener("keydown",a),c(!0))}),[a,l]),d=e.useCallback((function(){o(new Set)}),[]);return[t,{start:s,stop:l,resetKeys:d,isRecording:u}]};
//# sourceMappingURL=react-hotkeys-hook.cjs.production.min.js.map