UNPKG

reakit-utils

Version:

Reakit utils

40 lines (30 loc) 1.19 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var getDocument = require('./getDocument.js'); var getWindow = require('./getWindow.js'); function createKeyboardEvent(element, type, eventInit) { if (eventInit === void 0) { eventInit = {}; } if (typeof KeyboardEvent === "function") { return new KeyboardEvent(type, eventInit); } // IE 11 doesn't support Event constructors var event = getDocument.getDocument(element).createEvent("KeyboardEvent"); event.initKeyboardEvent(type, eventInit.bubbles, eventInit.cancelable, getWindow.getWindow(element), eventInit.key, eventInit.location, eventInit.ctrlKey, eventInit.altKey, eventInit.shiftKey, eventInit.metaKey); return event; } /** * Creates and dispatches `KeyboardEvent` in a way that also works on IE 11. * * @example * import { fireKeyboardEvent } from "reakit-utils"; * * fireKeyboardEvent(document.getElementById("id"), "keydown", { * key: "ArrowDown", * shiftKey: true, * }); */ function fireKeyboardEvent(element, type, eventInit) { return element.dispatchEvent(createKeyboardEvent(element, type, eventInit)); } exports.fireKeyboardEvent = fireKeyboardEvent;