UNPKG

@chakra-ui/styled-system

Version:

Style function for css-in-js building component libraries

114 lines (92 loc) 3.35 kB
"use strict"; exports.__esModule = true; exports.calc = void 0; var _utils = require("@chakra-ui/utils"); /** * Thank you @markdalgleish for this piece of art! */ function resolveReference(operand) { if ((0, _utils.isObject)(operand) && operand.reference) { return operand.reference; } return String(operand); } var toExpression = function toExpression(operator) { for (var _len = arguments.length, operands = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { operands[_key - 1] = arguments[_key]; } return operands.map(resolveReference).join(" " + operator + " ").replace(/calc/g, ""); }; var _add = function add() { for (var _len2 = arguments.length, operands = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { operands[_key2] = arguments[_key2]; } return "calc(" + toExpression.apply(void 0, ["+"].concat(operands)) + ")"; }; var _subtract = function subtract() { for (var _len3 = arguments.length, operands = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { operands[_key3] = arguments[_key3]; } return "calc(" + toExpression.apply(void 0, ["-"].concat(operands)) + ")"; }; var _multiply = function multiply() { for (var _len4 = arguments.length, operands = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { operands[_key4] = arguments[_key4]; } return "calc(" + toExpression.apply(void 0, ["*"].concat(operands)) + ")"; }; var _divide = function divide() { for (var _len5 = arguments.length, operands = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { operands[_key5] = arguments[_key5]; } return "calc(" + toExpression.apply(void 0, ["/"].concat(operands)) + ")"; }; var _negate = function negate(x) { var value = resolveReference(x); if (value != null && !Number.isNaN(parseFloat(value))) { return String(value).startsWith("-") ? String(value).slice(1) : "-" + value; } return _multiply(value, -1); }; var calc = Object.assign(function (x) { return { add: function add() { for (var _len6 = arguments.length, operands = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) { operands[_key6] = arguments[_key6]; } return calc(_add.apply(void 0, [x].concat(operands))); }, subtract: function subtract() { for (var _len7 = arguments.length, operands = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) { operands[_key7] = arguments[_key7]; } return calc(_subtract.apply(void 0, [x].concat(operands))); }, multiply: function multiply() { for (var _len8 = arguments.length, operands = new Array(_len8), _key8 = 0; _key8 < _len8; _key8++) { operands[_key8] = arguments[_key8]; } return calc(_multiply.apply(void 0, [x].concat(operands))); }, divide: function divide() { for (var _len9 = arguments.length, operands = new Array(_len9), _key9 = 0; _key9 < _len9; _key9++) { operands[_key9] = arguments[_key9]; } return calc(_divide.apply(void 0, [x].concat(operands))); }, negate: function negate() { return calc(_negate(x)); }, toString: function toString() { return x.toString(); } }; }, { add: _add, subtract: _subtract, multiply: _multiply, divide: _divide, negate: _negate }); exports.calc = calc; //# sourceMappingURL=calc.js.map