UNPKG

@kiwicom/orbit-components

Version:

Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.

111 lines (89 loc) 4.01 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); var _consts = require("./consts"); var _consts2 = require("../utils/layout/consts"); var _common = require("../utils/common"); var _layout = require("../utils/layout"); var normalizeSpacing = function normalizeSpacing(el, property, theme) { if ((0, _typeof2.default)(el) === "object") { return Object.entries(el).map(function (_ref) { var _ref2 = (0, _slicedToArray2.default)(_ref, 2), key = _ref2[0], val = _ref2[1]; if (val === "none") return (0, _layout.formatCSS)("".concat(property, "-").concat(key), "0"); return (0, _layout.formatCSS)("".concat(property, "-").concat(key), (0, _consts2.TOKENS)(theme)[val]); }); } if (el !== "none") { return [(0, _layout.formatCSS)(property, (0, _consts2.TOKENS)(theme)[el])]; } return []; }; var normalizeToken = function normalizeToken(token, property, prefix, theme) { return (0, _layout.formatCSS)(property, theme.orbit[prefix + (0, _common.firstToUpper)(token)]); }; var normalizeSize = function normalizeSize(val, property) { if (val === _consts.WIDTH_AND_HEIGHT.FULL) { return (0, _layout.formatCSS)(property, "100%"); } return (0, _layout.formatCSS)(property, val); }; var normalizeElevation = function normalizeElevation(val, theme) { return (0, _layout.formatCSS)("box-shadow", theme.orbit["boxShadow".concat((0, _common.firstToUpper)(val))]); }; var norm = function norm(_ref3) { var val = _ref3.val, key = _ref3.key, theme = _ref3.theme; if (!(0, _layout.isDefined)(val) || !(0, _layout.isDefined)(key)) return undefined; var all = { padding: normalizeSpacing(val, key, theme), margin: normalizeSpacing(val, key, theme), color: key === "color" && normalizeToken(val, key, "palette", theme), background: key === "background" && normalizeToken(val, key, "palette", theme), height: normalizeSize(val, key), width: normalizeSize(val, key), borderRadius: key === "borderRadius" && normalizeToken(val, "border-radius", "borderRadius", theme), elevation: key === "elevation" && normalizeElevation(val, theme), justify: (0, _layout.formatCSS)("justify-content", (0, _layout.getJustify)(val)), align: (0, _layout.formatCSS)("align-items", (0, _layout.getAlign)(val)), direction: (0, _layout.formatCSS)("flex-direction", (0, _layout.getDirection)(val)), grow: (0, _layout.formatCSS)("flex-grow", (0, _layout.getGrow)(val)), shrink: (0, _layout.formatCSS)("flex-shrink", (0, _layout.getShrink)(val)), wrap: (0, _layout.formatCSS)("flex-wrap", (0, _layout.getWrap)(val)), textAlign: (0, _layout.formatCSS)("text-align", val), minWidth: (0, _layout.formatCSS)("min-width", val), maxWidth: (0, _layout.formatCSS)("max-width", val), maxHeight: (0, _layout.formatCSS)("max-height", val) }; if (!all[key]) return (0, _layout.formatCSS)(key, val); return all[key]; }; var normalize = function normalize(object) { return function (_ref4) { var theme = _ref4.theme; if (!object) return null; return Object.entries(object).reduce(function (acc, _ref5) { var _ref6 = (0, _slicedToArray2.default)(_ref5, 2), key = _ref6[0], val = _ref6[1]; var accFn = function accFn(additional) { return [].concat((0, _toConsumableArray2.default)(acc), (0, _toConsumableArray2.default)(additional)); }; return accFn([norm({ val: val, key: key, theme: theme })]); }, []); }; }; var _default = normalize; exports.default = _default;