@guruhotel/aura-hooks
Version:
🪝 Hooks library designed by the Guruhotel team for Aura UI
1 lines • 829 B
JavaScript
export function parseHotkey(e){const t=e.toLowerCase().split("+").map((e=>e.trim())),r={alt:t.includes("alt"),ctrl:t.includes("ctrl"),meta:t.includes("meta"),mod:t.includes("mod"),shift:t.includes("shift")},n=["alt","ctrl","meta","shift","mod"];return{...r,key:t.find((e=>!n.includes(e)))}}export function getHotkeyMatcher(e){return t=>function(e,t){const{alt:r,ctrl:n,meta:o,mod:a,shift:i,key:c}=e,{altKey:s,ctrlKey:l,metaKey:u,shiftKey:f,key:d}=t;if(r!==s)return!1;if(a){if(!l&&!u)return!1}else{if(n!==l)return!1;if(o!==u)return!1}return i===f&&!(!c||d.toLowerCase()!==c.toLowerCase()&&t.code.replace("Key","").toLowerCase()!==c.toLowerCase())}(parseHotkey(e),t)}export function getHotkeyHandler(e){return t=>{const r="nativeEvent"in t?t.nativeEvent:t;e.forEach((([e,n])=>{getHotkeyMatcher(e)(r)&&(t.preventDefault(),n(r))}))}}