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.

95 lines (82 loc) 3.33 kB
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _typeof from "@babel/runtime/helpers/esm/typeof"; import { WIDTH_AND_HEIGHT } from "./consts"; import { TOKENS } from "../utils/layout/consts"; import { firstToUpper } from "../utils/common"; import { getJustify, getAlign, formatCSS, getDirection, getShrink, getWrap, getGrow, isDefined } from "../utils/layout"; var normalizeSpacing = function normalizeSpacing(el, property, theme) { if (_typeof(el) === "object") { return Object.entries(el).map(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), key = _ref2[0], val = _ref2[1]; if (val === "none") return formatCSS("".concat(property, "-").concat(key), "0"); return formatCSS("".concat(property, "-").concat(key), TOKENS(theme)[val]); }); } if (el !== "none") { return [formatCSS(property, TOKENS(theme)[el])]; } return []; }; var normalizeToken = function normalizeToken(token, property, prefix, theme) { return formatCSS(property, theme.orbit[prefix + firstToUpper(token)]); }; var normalizeSize = function normalizeSize(val, property) { if (val === WIDTH_AND_HEIGHT.FULL) { return formatCSS(property, "100%"); } return formatCSS(property, val); }; var normalizeElevation = function normalizeElevation(val, theme) { return formatCSS("box-shadow", theme.orbit["boxShadow".concat(firstToUpper(val))]); }; var norm = function norm(_ref3) { var val = _ref3.val, key = _ref3.key, theme = _ref3.theme; if (!isDefined(val) || !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: formatCSS("justify-content", getJustify(val)), align: formatCSS("align-items", getAlign(val)), direction: formatCSS("flex-direction", getDirection(val)), grow: formatCSS("flex-grow", getGrow(val)), shrink: formatCSS("flex-shrink", getShrink(val)), wrap: formatCSS("flex-wrap", getWrap(val)), textAlign: formatCSS("text-align", val), minWidth: formatCSS("min-width", val), maxWidth: formatCSS("max-width", val), maxHeight: formatCSS("max-height", val) }; if (!all[key]) return 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 = _slicedToArray(_ref5, 2), key = _ref6[0], val = _ref6[1]; var accFn = function accFn(additional) { return [].concat(_toConsumableArray(acc), _toConsumableArray(additional)); }; return accFn([norm({ val: val, key: key, theme: theme })]); }, []); }; }; export default normalize;