@bigbinary/neetoui
Version:
neetoUI drives the experience at all neeto products
209 lines (205 loc) • 9.19 kB
JavaScript
;
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
require('@babel/runtime/helpers/defineProperty');
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var neetoCist = require('@bigbinary/neeto-cist');
var qs = require('qs');
var ramda = require('ramda');
var en = require('./en-BfFI1Si2.js');
var index = require('./index-D0rucYPS.js');
var i18next = require('i18next');
var getEnTranslationValue = function getEnTranslationValue(translationKey) {
return translationKey.split(".").reduce(function (acc, key) {
return acc[key];
}, en.enTranslations);
};
var getScrollbarWidth = function getScrollbarWidth() {
var _parentDiv$parentNode;
var parentDiv = document.createElement("div");
parentDiv.style.visibility = "hidden";
parentDiv.style.overflow = "scroll";
document.body.appendChild(parentDiv);
var childDiv = document.createElement("div");
parentDiv.appendChild(childDiv);
var scrollbarWidth = parentDiv.offsetWidth - childDiv.offsetWidth;
parentDiv === null || parentDiv === void 0 ? void 0 : (_parentDiv$parentNode = parentDiv.parentNode) === null || _parentDiv$parentNode === void 0 ? void 0 : _parentDiv$parentNode.removeChild(parentDiv);
return scrollbarWidth;
};
var getTimezoneAppliedDateTime = function getTimezoneAppliedDateTime(inputDateTime) {
var timezoneAppliedDateTime = function timezoneAppliedDateTime(date) {
return date && typeof date.format === "function" ? index.dayjs(date.format("YYYY-MM-DD HH:mm:ss")) : null;
};
return Array.isArray(inputDateTime) ? inputDateTime.map(timezoneAppliedDateTime) : timezoneAppliedDateTime(inputDateTime);
};
var noop = function noop() {};
var hyphenize = function hyphenize(input) {
var fallbackString = "nui";
if (typeof input === "number") return String(input);
if (input && typeof input === "string" && input.replace) {
return input.replace(/[\s_]/g, "-").replace(/([a-z])([A-Z])/g, "$1-$2").replace(/-+/g, "-").toLowerCase();
}
return fallbackString;
};
var getValidDayjsValue = function getValidDayjsValue(value) {
if (Array.isArray(value)) {
return value.map(function (date) {
if (!date) return null;
var parsed = index.dayjs(date);
return parsed.isValid() ? parsed : null;
});
}
if (!value) return value;
var parsed = index.dayjs(value);
return parsed.isValid() ? parsed : null;
};
var UniqueArray = /*#__PURE__*/function () {
function UniqueArray() {
_classCallCheck(this, UniqueArray);
this.array = [];
}
return _createClass(UniqueArray, [{
key: "add",
value: function add(item) {
if (this.array.some(ramda.equals(item))) return false;
this.array.push(item);
return true;
}
}, {
key: "remove",
value: function remove(item) {
this.array = this.array.filter(ramda.complement(ramda.equals(item)));
}
}]);
}();
var trapFocusOnFocusableElements = function trapFocusOnFocusableElements(ref) {
var _ref$current, _ref$current2;
var focusableElements = 'button,[href], input, select, textarea, [tabindex]:not([tabindex="-1"])';
var firstFocusableElement = ref === null || ref === void 0 ? void 0 : (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.querySelectorAll(focusableElements)[0];
var focusableContent = ref === null || ref === void 0 ? void 0 : (_ref$current2 = ref.current) === null || _ref$current2 === void 0 ? void 0 : _ref$current2.querySelectorAll(focusableElements);
var lastFocusableElement = focusableContent[(focusableContent === null || focusableContent === void 0 ? void 0 : focusableContent.length) - 1];
var onKeyDown = function onKeyDown(e) {
var isTabPressed = e.key === "Tab" || e.keyCode === 9;
if (!isTabPressed) {
return;
}
if (e.shiftKey) {
if (document.activeElement === firstFocusableElement) {
lastFocusableElement.focus();
e.preventDefault();
}
} else {
if (document.activeElement === lastFocusableElement) {
firstFocusableElement.focus();
e.preventDefault();
}
}
};
document.addEventListener("keydown", onKeyDown);
return function () {
return document.removeEventListener("keydown", onKeyDown);
};
};
var focusFirstFocusableElement = function focusFirstFocusableElement(ref) {
var _ref$current3;
var focusableElements = 'button,[href], input, select, textarea, [tabindex]:not([tabindex="-1"])';
var firstFocusableElement = ref === null || ref === void 0 ? void 0 : (_ref$current3 = ref.current) === null || _ref$current3 === void 0 ? void 0 : _ref$current3.querySelectorAll(focusableElements)[0];
firstFocusableElement === null || firstFocusableElement === void 0 ? void 0 : firstFocusableElement.focus();
};
var hideScrollAndAddMargin = function hideScrollAndAddMargin() {
if (!document.body) return;
var scrollbarWidth = getScrollbarWidth();
document.body.style.overflow = "hidden";
document.body.style.marginRight = "".concat(scrollbarWidth, "px");
};
var showScrollAndRemoveMargin = function showScrollAndRemoveMargin() {
if (!document.body) return;
document.body.style.overflow = "auto";
document.body.style.marginRight = "0px";
};
var ANT_DESIGN_GLOBAL_TOKEN_OVERRIDES = {
colorBgContainer: "rgb(var(--neeto-ui-white))",
colorBorder: "rgb(var(--neeto-ui-gray-300))",
colorBorderSecondary: "rgb(var(--neeto-ui-gray-200))",
colorFillAlter: "rgb(var(--neeto-ui-gray-100))",
colorFillContent: "rgb(var(--neeto-ui-gray-100))",
colorFillSecondary: "rgb(var(--neeto-ui-gray-100))",
colorIcon: "rgb(var(--neeto-ui-gray-700))",
colorIconHover: "rgb(var(--neeto-ui-gray-800))",
colorLink: "rgb(var(--neeto-ui-primary-500))",
colorLinkActive: "rgb(var(--neeto-ui-primary-800))",
colorLinkHover: "rgb(var(--neeto-ui-primary-600))",
colorPrimary: "rgb(var(--neeto-ui-primary-500))",
colorSplit: "rgb(var(--neeto-ui-gray-100))",
colorText: "rgb(var(--neeto-ui-gray-800))",
colorTextDescription: "rgb(var(--neeto-ui-gray-700))",
colorTextDisabled: "rgb(var(--neeto-ui-gray-600))",
colorTextHeading: "rgb(var(--neeto-ui-black))",
colorTextPlaceholder: "rgb(var(--neeto-ui-gray-500))",
controlItemBgActive: "rgb(var(--neeto-ui-primary-100))",
controlItemBgActiveHover: "rgb(var(--neeto-ui-pastel-purple))",
controlItemBgHover: "rgb(var(--neeto-ui-gray-100))"
};
var buildUrl = function buildUrl(route, params) {
var placeHolders = [];
ramda.toPairs(params).forEach(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
key = _ref2[0],
value = _ref2[1];
if (!route.includes(":".concat(key))) return;
placeHolders.push(key);
route = route.replace(":".concat(key), encodeURIComponent(value));
});
var queryParams = ramda.pipe(ramda.omit(placeHolders), neetoCist.preprocessForSerialization, qs.stringify)(params);
return ramda.isEmpty(queryParams) ? route : "".concat(route, "?").concat(queryParams);
};
var getLocale = function getLocale(i18n, t, translationKey) {
if (ramda.isEmpty(i18n)) return getEnTranslationValue(translationKey);
return i18n.exists(translationKey) ? t(translationKey) : getEnTranslationValue(translationKey);
};
var setToLocalStorage = function setToLocalStorage(key, value) {
try {
// eslint-disable-next-line @bigbinary/neeto/no-local-storage
localStorage.setItem(key, JSON.stringify(value));
} catch (_unused) {
// localStorage access can fail due to private browsing mode or storage restrictions
}
};
var removeFromLocalStorage = function removeFromLocalStorage(key) {
try {
// eslint-disable-next-line @bigbinary/neeto/no-local-storage
localStorage.removeItem(key);
} catch (_unused2) {
// localStorage access can fail due to private browsing mode or storage restrictions
}
};
var getFromLocalStorage = function getFromLocalStorage(key, defaultValue) {
try {
// eslint-disable-next-line @bigbinary/neeto/no-local-storage
var storedValue = localStorage.getItem(key);
return storedValue ? JSON.parse(storedValue) : defaultValue;
} catch (_unused3) {
// localStorage access can fail due to private browsing mode or storage restrictions
return defaultValue;
}
};
var getContentDir = function getContentDir() {
return i18next.language ? i18next.dir() : "ltr";
};
exports.ANT_DESIGN_GLOBAL_TOKEN_OVERRIDES = ANT_DESIGN_GLOBAL_TOKEN_OVERRIDES;
exports.UniqueArray = UniqueArray;
exports.buildUrl = buildUrl;
exports.focusFirstFocusableElement = focusFirstFocusableElement;
exports.getContentDir = getContentDir;
exports.getFromLocalStorage = getFromLocalStorage;
exports.getLocale = getLocale;
exports.getTimezoneAppliedDateTime = getTimezoneAppliedDateTime;
exports.getValidDayjsValue = getValidDayjsValue;
exports.hideScrollAndAddMargin = hideScrollAndAddMargin;
exports.hyphenize = hyphenize;
exports.noop = noop;
exports.removeFromLocalStorage = removeFromLocalStorage;
exports.setToLocalStorage = setToLocalStorage;
exports.showScrollAndRemoveMargin = showScrollAndRemoveMargin;
exports.trapFocusOnFocusableElements = trapFocusOnFocusableElements;
//# sourceMappingURL=index-DFhZ7eXY.js.map