react-hotkeys-hook
Advanced tools
Comparing version 4.4.0 to 4.4.1
@@ -13,3 +13,3 @@ import { ReactNode } from 'react'; | ||
} | ||
export default function BoundHotkeysProxyProviderProvider({ addHotkey, removeHotkey, children }: Props): JSX.Element; | ||
export default function BoundHotkeysProxyProviderProvider({ addHotkey, removeHotkey, children }: Props): import("react").JSX.Element; | ||
export {}; |
@@ -15,3 +15,3 @@ import { Hotkey } from './types'; | ||
} | ||
export declare const HotkeysProvider: ({ initiallyActiveScopes, children }: Props) => JSX.Element; | ||
export declare const HotkeysProvider: ({ initiallyActiveScopes, children }: Props) => import("react").JSX.Element; | ||
export {}; |
import useHotkeys from './useHotkeys'; | ||
import type { Options } from './types'; | ||
import type { Options, Keys, HotkeyCallback } from './types'; | ||
import { HotkeysProvider, useHotkeysContext } from './HotkeysProvider'; | ||
import { isHotkeyPressed } from './isHotkeyPressed'; | ||
import useRecordHotkeys from './useRecordHotkeys'; | ||
export { useHotkeys, useRecordHotkeys, useHotkeysContext, isHotkeyPressed, HotkeysProvider, Options }; | ||
export { useHotkeys, useRecordHotkeys, useHotkeysContext, isHotkeyPressed, HotkeysProvider, Options, Keys, HotkeyCallback, }; |
@@ -1,3 +0,4 @@ | ||
export declare function isHotkeyPressed(key: string | string[], splitKey?: string): boolean; | ||
export declare function isReadonlyArray(value: unknown): value is readonly unknown[]; | ||
export declare function isHotkeyPressed(key: string | readonly string[], splitKey?: string): boolean; | ||
export declare function pushToCurrentlyPressedKeys(key: string | string[]): void; | ||
export declare function removeFromCurrentlyPressedKeys(key: string | string[]): void; |
@@ -102,2 +102,6 @@ 'use strict'; | ||
var currentlyPressedKeys = /*#__PURE__*/new Set(); | ||
// https://github.com/microsoft/TypeScript/issues/17002 | ||
function isReadonlyArray(value) { | ||
return Array.isArray(value); | ||
} | ||
function isHotkeyPressed(key, splitKey) { | ||
@@ -107,3 +111,3 @@ if (splitKey === void 0) { | ||
} | ||
var hotkeyArray = Array.isArray(key) ? key : key.split(splitKey); | ||
var hotkeyArray = isReadonlyArray(key) ? key : key.split(splitKey); | ||
return hotkeyArray.every(function (hotkey) { | ||
@@ -165,3 +169,3 @@ return currentlyPressedKeys.has(hotkey.trim().toLowerCase()); | ||
var targetTagName = target && target.tagName; | ||
if (enabledOnTags instanceof Array) { | ||
if (isReadonlyArray(enabledOnTags)) { | ||
return Boolean(targetTagName && enabledOnTags && enabledOnTags.some(function (tag) { | ||
@@ -373,3 +377,3 @@ return tag.toLowerCase() === targetTagName.toLowerCase(); | ||
var _options = !(options instanceof Array) ? options : !(dependencies instanceof Array) ? dependencies : undefined; | ||
var _keys = keys instanceof Array ? keys.join(_options == null ? void 0 : _options.splitKey) : keys; | ||
var _keys = isReadonlyArray(keys) ? keys.join(_options == null ? void 0 : _options.splitKey) : keys; | ||
var _deps = options instanceof Array ? options : dependencies instanceof Array ? dependencies : undefined; | ||
@@ -376,0 +380,0 @@ var memoisedCB = react.useCallback(callback, _deps != null ? _deps : []); |
@@ -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&&s([i(e.key),i(e.code)])})),document.addEventListener("keyup",(function(e){void 0!==e.key&&d([i(e.key),i(e.code)])}))),"undefined"!=typeof window&&window.addEventListener("blur",(function(){a.clear()}));var a=new Set;function l(e,t){return void 0===t&&(t=","),(Array.isArray(e)?e:e.split(t)).every((function(e){return a.has(e.trim().toLowerCase())}))}function s(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 f(e,t){var n=e.target;void 0===t&&(t=!1);var o=n&&n.tagName;return t instanceof Array?Boolean(o&&t&&t.some((function(e){return e.toLowerCase()===o.toLowerCase()}))):Boolean(o&&t&&!0===t)}var v=e.createContext(void 0);function y(e){return t.jsx(v.Provider,{value:{addHotkey:e.addHotkey,removeHotkey:e.removeHotkey},children:e.children})}function p(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&&p(e[o],t[o])}),!0):e===t}var k=e.createContext({hotkeys:[],enabledScopes:[],toggleScope:function(){},enableScope:function(){},disableScope:function(){}}),m=function(){return e.useContext(k)},h=function(e){e.stopPropagation(),e.preventDefault(),e.stopImmediatePropagation()},b="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}))}))}),[]),m=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!p(t,e)}))}))}),[]);return t.jsx(k.Provider,{value:{enabledScopes:c,hotkeys:s,enableScope:f,disableScope:v,toggleScope:m},children:t.jsx(y,{addHotkey:h,removeHotkey:b,children:i})})},exports.isHotkeyPressed=l,exports.useHotkeys=function(t,n,o,r){var a=e.useRef(null),y=e.useRef(!1),k=o instanceof Array?r instanceof Array?void 0:r:o,w=t instanceof Array?t.join(null==k?void 0:k.splitKey):t,g=o instanceof Array?o:r instanceof Array?r:void 0,C=e.useCallback(n,null!=g?g:[]),L=e.useRef(C);L.current=g?C:n;var S=function(t){var n=e.useRef(void 0);return p(n.current,t)||(n.current=t),n.current}(k),E=m().enabledScopes,A=e.useContext(v);return b((function(){if(!1!==(null==S?void 0:S.enabled)&&(t=null==S?void 0:S.scopes,0===(e=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>'),1):!t||e.some((function(e){return t.includes(e)}))||e.includes("*"))){var e,t,n=function(e,t){var n;void 0===t&&(t=!1),(!f(e,["input","textarea","select"])||f(e,null==S?void 0:S.enableOnFormTags))&&(null!=S&&null!=S.ignoreEventWhen&&S.ignoreEventWhen(e)||(null===a.current||document.activeElement===a.current||a.current.contains(document.activeElement)?(null==(n=e.target)||!n.isContentEditable||null!=S&&S.enableOnContentEditable)&&u(w,null==S?void 0:S.splitKey).forEach((function(n){var o,r=c(n,null==S?void 0:S.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,s=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(!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!(!s||1!==s.length||!s.includes(m)&&!s.includes(k))||(s?l(s):!s)}(e,r,null==S?void 0:S.ignoreModifiers)||null!=(o=r.keys)&&o.includes("*")){if(t&&y.current)return;if(function(e,t,n){("function"==typeof n&&n(e,t)||!0===n)&&e.preventDefault()}(e,r,null==S?void 0:S.preventDefault),!function(e,t,n){return"function"==typeof n?n(e,t):!0===n||void 0===n}(e,r,null==S?void 0:S.enabled))return void h(e);L.current(e,r),t||(y.current=!0)}})):h(e)))},o=function(e){void 0!==e.key&&(s(i(e.code)),(void 0===(null==S?void 0:S.keydown)&&!0!==(null==S?void 0:S.keyup)||null!=S&&S.keydown)&&n(e))},r=function(e){void 0!==e.key&&(d(i(e.code)),y.current=!1,null!=S&&S.keyup&&n(e,!0))},v=a.current||(null==k?void 0:k.document)||document;return v.addEventListener("keyup",r),v.addEventListener("keydown",o),A&&u(w,null==S?void 0:S.splitKey).forEach((function(e){return A.addHotkey(c(e,null==S?void 0:S.combinationKey,null==S?void 0:S.description))})),function(){v.removeEventListener("keyup",r),v.removeEventListener("keydown",o),A&&u(w,null==S?void 0:S.splitKey).forEach((function(e){return A.removeHotkey(c(e,null==S?void 0:S.combinationKey,null==S?void 0:S.description))}))}}}),[w,S,E]),a},exports.useHotkeysContext=m,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",".":"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;void 0===t&&(t=!1),(!v(e,["input","textarea","select"])||v(e,null==E?void 0:E.enableOnFormTags))&&(null!=E&&null!=E.ignoreEventWhen&&E.ignoreEventWhen(e)||(null===a.current||document.activeElement===a.current||a.current.contains(document.activeElement)?(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(!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(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)}})):b(e)))},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}]}; | ||
//# sourceMappingURL=react-hotkeys-hook.cjs.production.min.js.map |
@@ -100,2 +100,6 @@ import { useContext, createContext, useState, useCallback, useRef, useLayoutEffect, useEffect } from 'react'; | ||
var currentlyPressedKeys = /*#__PURE__*/new Set(); | ||
// https://github.com/microsoft/TypeScript/issues/17002 | ||
function isReadonlyArray(value) { | ||
return Array.isArray(value); | ||
} | ||
function isHotkeyPressed(key, splitKey) { | ||
@@ -105,3 +109,3 @@ if (splitKey === void 0) { | ||
} | ||
var hotkeyArray = Array.isArray(key) ? key : key.split(splitKey); | ||
var hotkeyArray = isReadonlyArray(key) ? key : key.split(splitKey); | ||
return hotkeyArray.every(function (hotkey) { | ||
@@ -163,3 +167,3 @@ return currentlyPressedKeys.has(hotkey.trim().toLowerCase()); | ||
var targetTagName = target && target.tagName; | ||
if (enabledOnTags instanceof Array) { | ||
if (isReadonlyArray(enabledOnTags)) { | ||
return Boolean(targetTagName && enabledOnTags && enabledOnTags.some(function (tag) { | ||
@@ -371,3 +375,3 @@ return tag.toLowerCase() === targetTagName.toLowerCase(); | ||
var _options = !(options instanceof Array) ? options : !(dependencies instanceof Array) ? dependencies : undefined; | ||
var _keys = keys instanceof Array ? keys.join(_options == null ? void 0 : _options.splitKey) : keys; | ||
var _keys = isReadonlyArray(keys) ? keys.join(_options == null ? void 0 : _options.splitKey) : keys; | ||
var _deps = options instanceof Array ? options : dependencies instanceof Array ? dependencies : undefined; | ||
@@ -374,0 +378,0 @@ var memoisedCB = useCallback(callback, _deps != null ? _deps : []); |
import type { DependencyList } from 'react'; | ||
export declare type FormTags = 'input' | 'textarea' | 'select' | 'INPUT' | 'TEXTAREA' | 'SELECT'; | ||
export declare type Keys = string | string[]; | ||
export declare type Scopes = string | string[]; | ||
export declare type Keys = string | readonly string[]; | ||
export declare type Scopes = string | readonly string[]; | ||
export declare type RefType<T> = T | null; | ||
@@ -14,3 +14,3 @@ export declare type KeyboardModifiers = { | ||
export declare type Hotkey = KeyboardModifiers & { | ||
keys?: string[]; | ||
keys?: readonly string[]; | ||
scopes?: Scopes; | ||
@@ -24,3 +24,3 @@ description?: string; | ||
enabled?: Trigger; | ||
enableOnFormTags?: FormTags[] | boolean; | ||
enableOnFormTags?: readonly FormTags[] | boolean; | ||
enableOnContentEditable?: boolean; | ||
@@ -27,0 +27,0 @@ ignoreEventWhen?: (e: KeyboardEvent) => boolean; |
@@ -5,4 +5,4 @@ import { FormTags, Hotkey, Scopes, Trigger } from './types'; | ||
export declare function isKeyboardEventTriggeredByInput(ev: KeyboardEvent): boolean; | ||
export declare function isHotkeyEnabledOnTag({ target }: KeyboardEvent, enabledOnTags?: FormTags[] | boolean): boolean; | ||
export declare function isHotkeyEnabledOnTag({ target }: KeyboardEvent, enabledOnTags?: readonly FormTags[] | boolean): boolean; | ||
export declare function isScopeActive(activeScopes: string[], scopes?: Scopes): boolean; | ||
export declare const isHotkeyMatchingKeyboardEvent: (e: KeyboardEvent, hotkey: Hotkey, ignoreModifiers?: boolean) => boolean; |
{ | ||
"name": "react-hotkeys-hook", | ||
"description": "React hook for handling keyboard shortcuts", | ||
"version": "4.4.0", | ||
"version": "4.4.1", | ||
"repository": "https://JohannesKlauss@github.com/JohannesKlauss/react-keymap-hook.git", | ||
@@ -78,16 +78,16 @@ "homepage": "https://johannesklauss.github.io/react-hotkeys-hook/", | ||
"devDependencies": { | ||
"@babel/core": "7.21.4", | ||
"@babel/core": "7.21.8", | ||
"@babel/plugin-proposal-class-properties": "7.18.6", | ||
"@babel/plugin-transform-react-jsx": "7.21.0", | ||
"@babel/preset-env": "7.21.4", | ||
"@babel/plugin-transform-react-jsx": "7.21.5", | ||
"@babel/preset-env": "7.21.5", | ||
"@babel/preset-react": "7.18.6", | ||
"@babel/preset-typescript": "7.21.4", | ||
"@babel/preset-typescript": "7.21.5", | ||
"@testing-library/jest-dom": "5.16.5", | ||
"@testing-library/react": "14.0.0", | ||
"@testing-library/user-event": "14.4.3", | ||
"@types/jest": "29.5.0", | ||
"@types/react": "18.0.35", | ||
"@types/react-dom": "18.0.11", | ||
"@typescript-eslint/eslint-plugin": "5.58.0", | ||
"@typescript-eslint/parser": "5.58.0", | ||
"@types/jest": "29.5.2", | ||
"@types/react": "18.2.14", | ||
"@types/react-dom": "18.2.6", | ||
"@typescript-eslint/eslint-plugin": "5.60.0", | ||
"@typescript-eslint/parser": "5.60.0", | ||
"eslint": "^8.34.0", | ||
@@ -98,3 +98,3 @@ "eslint-plugin-prettier": "4.2.1", | ||
"jest-environment-jsdom": "29.5.0", | ||
"prettier": "2.8.7", | ||
"prettier": "2.8.8", | ||
"react": "18.2.0", | ||
@@ -104,3 +104,3 @@ "react-dom": "18.2.0", | ||
"tsdx": "0.14.1", | ||
"tslib": "2.5.0", | ||
"tslib": "2.5.3", | ||
"typescript": "5.0.4" | ||
@@ -107,0 +107,0 @@ }, |
import useHotkeys from './useHotkeys' | ||
import type { Options } from './types' | ||
import type { Options, Keys, HotkeyCallback } from './types' | ||
import { HotkeysProvider, useHotkeysContext } from './HotkeysProvider' | ||
@@ -7,2 +7,11 @@ import { isHotkeyPressed } from './isHotkeyPressed' | ||
export { useHotkeys, useRecordHotkeys, useHotkeysContext, isHotkeyPressed, HotkeysProvider, Options } | ||
export { | ||
useHotkeys, | ||
useRecordHotkeys, | ||
useHotkeysContext, | ||
isHotkeyPressed, | ||
HotkeysProvider, | ||
Options, | ||
Keys, | ||
HotkeyCallback, | ||
} |
@@ -32,5 +32,10 @@ import { isHotkeyModifier, mapKey } from './parseHotkeys' | ||
export function isHotkeyPressed(key: string | string[], splitKey = ','): boolean { | ||
const hotkeyArray = Array.isArray(key) ? key : key.split(splitKey) | ||
// https://github.com/microsoft/TypeScript/issues/17002 | ||
export function isReadonlyArray(value: unknown): value is readonly unknown[] { | ||
return Array.isArray(value) | ||
} | ||
export function isHotkeyPressed(key: string | readonly string[], splitKey = ','): boolean { | ||
const hotkeyArray = isReadonlyArray(key) ? key : key.split(splitKey) | ||
return hotkeyArray.every((hotkey) => currentlyPressedKeys.has(hotkey.trim().toLowerCase())) | ||
@@ -37,0 +42,0 @@ } |
import type { DependencyList } from 'react' | ||
export type FormTags = 'input' | 'textarea' | 'select' | 'INPUT' | 'TEXTAREA' | 'SELECT' | ||
export type Keys = string | string[] | ||
export type Scopes = string | string[] | ||
export type Keys = string | readonly string[] | ||
export type Scopes = string | readonly string[] | ||
@@ -18,3 +18,3 @@ export type RefType<T> = T | null | ||
export type Hotkey = KeyboardModifiers & { | ||
keys?: string[] | ||
keys?: readonly string[] | ||
scopes?: Scopes | ||
@@ -32,3 +32,3 @@ description?: string | ||
enabled?: Trigger // Main setting that determines if the hotkey is enabled or not. (Default: true) | ||
enableOnFormTags?: FormTags[] | boolean // Enable hotkeys on a list of tags. (Default: false) | ||
enableOnFormTags?: readonly FormTags[] | boolean // Enable hotkeys on a list of tags. (Default: false) | ||
enableOnContentEditable?: boolean // Enable hotkeys on tags with contentEditable props. (Default: false) | ||
@@ -35,0 +35,0 @@ ignoreEventWhen?: (e: KeyboardEvent) => boolean // Ignore evenets based on a condition (Default: undefined) |
@@ -15,3 +15,3 @@ import { HotkeyCallback, Keys, Options, OptionsOrDependencyArray, RefType } from './types' | ||
import useDeepEqualMemo from './useDeepEqualMemo' | ||
import { pushToCurrentlyPressedKeys, removeFromCurrentlyPressedKeys } from './isHotkeyPressed' | ||
import { isReadonlyArray, pushToCurrentlyPressedKeys, removeFromCurrentlyPressedKeys } from './isHotkeyPressed' | ||
@@ -40,3 +40,3 @@ const stopPropagation = (e: KeyboardEvent): void => { | ||
: undefined | ||
const _keys: string = keys instanceof Array ? keys.join(_options?.splitKey) : keys | ||
const _keys: string = isReadonlyArray(keys) ? keys.join(_options?.splitKey) : keys | ||
const _deps: DependencyList | undefined = | ||
@@ -43,0 +43,0 @@ options instanceof Array ? options : dependencies instanceof Array ? dependencies : undefined |
import { FormTags, Hotkey, Scopes, Trigger } from './types' | ||
import { isHotkeyPressed } from './isHotkeyPressed' | ||
import { isHotkeyPressed, isReadonlyArray } from './isHotkeyPressed' | ||
import { mapKey } from './parseHotkeys' | ||
@@ -23,6 +23,9 @@ | ||
export function isHotkeyEnabledOnTag({ target }: KeyboardEvent, enabledOnTags: FormTags[] | boolean = false): boolean { | ||
export function isHotkeyEnabledOnTag( | ||
{ target }: KeyboardEvent, | ||
enabledOnTags: readonly FormTags[] | boolean = false | ||
): boolean { | ||
const targetTagName = target && (target as HTMLElement).tagName | ||
if (enabledOnTags instanceof Array) { | ||
if (isReadonlyArray(enabledOnTags)) { | ||
return Boolean( | ||
@@ -29,0 +32,0 @@ targetTagName && enabledOnTags && enabledOnTags.some((tag) => tag.toLowerCase() === targetTagName.toLowerCase()) |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
192011
1684