@bianic-ui/utils
Version:
Common utilties and types for Bianic UI
60 lines (51 loc) • 1.95 kB
JavaScript
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.
*/
export var 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
*/
function checkIsBrowser() {
var _window = getWindow();
return Boolean(typeof _window !== "undefined" && _window.document && _window.document.createElement);
}
export var isBrowser = checkIsBrowser();
/**
* Get the normalized event key across all browsers
* @param event keyboard event
*/
export function normalizeEventKey(event) {
var {
key,
keyCode
} = event;
var isArrowKey = keyCode >= 37 && keyCode <= 40 && key.indexOf("Arrow") !== 0;
return isArrowKey ? "Arrow" + key : key;
}
export var dataAttr = condition => condition ? "" : undefined;
export var ariaAttr = condition => condition ? true : undefined;
export var getOwnerDocument = node => (node == null ? void 0 : node.ownerDocument) || document;
export 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(" ");
};
//# sourceMappingURL=dom.js.map