UNPKG

@mantine/hooks

Version:

A collection of 50+ hooks for state and UI management

70 lines (67 loc) 2.02 kB
'use client'; 'use strict'; function parseHotkey(hotkey) { const keys = hotkey.toLowerCase().split("+").map((part) => part.trim()); const modifiers = { alt: keys.includes("alt"), ctrl: keys.includes("ctrl"), meta: keys.includes("meta"), mod: keys.includes("mod"), shift: keys.includes("shift"), plus: keys.includes("[plus]") }; const reservedKeys = ["alt", "ctrl", "meta", "shift", "mod"]; const freeKey = keys.find((key) => !reservedKeys.includes(key)); return { ...modifiers, key: freeKey === "[plus]" ? "+" : freeKey }; } function isExactHotkey(hotkey, event, usePhysicalKeys) { const { alt, ctrl, meta, mod, shift, key } = hotkey; const { altKey, ctrlKey, metaKey, shiftKey, key: pressedKey, code: pressedCode } = event; if (alt !== altKey) { return false; } if (mod) { if (!ctrlKey && !metaKey) { return false; } } else { if (ctrl !== ctrlKey) { return false; } if (meta !== metaKey) { return false; } } if (shift !== shiftKey) { return false; } if (key && (!usePhysicalKeys && pressedKey.toLowerCase() === key.toLowerCase() || pressedCode.replace("Key", "").toLowerCase() === key.toLowerCase())) { return true; } return false; } function getHotkeyMatcher(hotkey, usePhysicalKeys) { return (event) => isExactHotkey(parseHotkey(hotkey), event, usePhysicalKeys); } function getHotkeyHandler(hotkeys) { return (event) => { const _event = "nativeEvent" in event ? event.nativeEvent : event; hotkeys.forEach( ([hotkey, handler, options = { preventDefault: true, usePhysicalKeys: false }]) => { if (getHotkeyMatcher(hotkey, options.usePhysicalKeys)(_event)) { if (options.preventDefault) { event.preventDefault(); } handler(_event); } } ); }; } exports.getHotkeyHandler = getHotkeyHandler; exports.getHotkeyMatcher = getHotkeyMatcher; exports.parseHotkey = parseHotkey; //# sourceMappingURL=parse-hotkey.cjs.map