UNPKG

@bianic-ui/utils

Version:

Common utilties and types for Bianic UI

86 lines (65 loc) 2.43 kB
"use strict"; exports.__esModule = true; exports.normalizeEventKey = normalizeEventKey; exports.cx = exports.getOwnerDocument = exports.ariaAttr = exports.dataAttr = exports.isBrowser = exports.getWindow = void 0; var _window = undefined; /** * Note: Accessing "window" in IE11 is somewhat expensive, and calling "typeof window" * hits a memory leak, whereas aliasing it and calling "typeof _window" does not. * Caching the window value at the file scope lets us minimize the impact. * * @see IE11 Memory Leak Issue https://github.com/microsoft/fluentui/pull/9010#issuecomment-490768427 */ try { _window = window; } catch (e) { /* no-op */ } /** * Helper to get the window object. The helper will make sure to use a cached variable * of "window", to avoid overhead and memory leaks in IE11. */ var getWindow = function getWindow(node) { var _node$ownerDocument$d, _node$ownerDocument; return (_node$ownerDocument$d = node == null ? void 0 : (_node$ownerDocument = node.ownerDocument) == null ? void 0 : _node$ownerDocument.defaultView) != null ? _node$ownerDocument$d : _window; }; /** * Check if we can use the DOM. Useful for SSR purposes */ exports.getWindow = getWindow; function checkIsBrowser() { var _window = getWindow(); return Boolean(typeof _window !== "undefined" && _window.document && _window.document.createElement); } var isBrowser = checkIsBrowser(); /** * Get the normalized event key across all browsers * @param event keyboard event */ exports.isBrowser = isBrowser; function normalizeEventKey(event) { var key = event.key, keyCode = event.keyCode; var isArrowKey = keyCode >= 37 && keyCode <= 40 && key.indexOf("Arrow") !== 0; return isArrowKey ? "Arrow" + key : key; } var dataAttr = function dataAttr(condition) { return condition ? "" : undefined; }; exports.dataAttr = dataAttr; var ariaAttr = function ariaAttr(condition) { return condition ? true : undefined; }; exports.ariaAttr = ariaAttr; var getOwnerDocument = function getOwnerDocument(node) { return (node == null ? void 0 : node.ownerDocument) || document; }; exports.getOwnerDocument = getOwnerDocument; var cx = function cx() { for (var _len = arguments.length, classNames = new Array(_len), _key = 0; _key < _len; _key++) { classNames[_key] = arguments[_key]; } return classNames.filter(Boolean).join(" "); }; exports.cx = cx; //# sourceMappingURL=dom.js.map