UNPKG

@instawork/design-system

Version:

The design system for Instawork's web apps

41 lines 1.36 kB
export var KeyboardKey; (function (KeyboardKey) { KeyboardKey["arrowDown"] = "ArrowDown"; KeyboardKey["arrowLeft"] = "ArrowLeft"; KeyboardKey["arrowRight"] = "ArrowRight"; KeyboardKey["arrowUp"] = "ArrowUp"; })(KeyboardKey || (KeyboardKey = {})); /** * A map of non-standard keyboard key names used by IE to the standard key names. * @type {Object.<string, string>} */ export const COMPAT_MAP = { Down: KeyboardKey.arrowDown, Left: KeyboardKey.arrowLeft, Right: KeyboardKey.arrowRight, Up: KeyboardKey.arrowUp, }; /** * Utility class for dealing with browser compatibility issues related to keyboard input */ export class KeyboardInput { static fromEvent(e) { return COMPAT_MAP[e.key] || e.key; } /** * Used to wrap a keyboard event handler so that {KeyboardEvent.key} uses the standard value. * * This can be used to avoid having to check for multiple values to facilitate IE compatibility. * * JQuery Example: * $('selector').on('keydown', KeyboardInput.wrapEvent(function (e) { return e.key === 'ArrowUp'; }); */ static wrapEvent(handler) { const self = this; return function wrappedHandler(e) { Object.assign(e, { key: self.fromEvent(e) }); return handler(e); }; } } //# sourceMappingURL=keyboard-input.js.map