UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

77 lines (75 loc) 2.56 kB
import { utils_exports } from "../../utils/index.js"; import { getCSSFunction, globalValues, isCSSFunction, isCSSToken, splitValues } from "./utils.js"; //#region src/core/css/calc.ts const OPERATORS = [ "+", "-", "*", "/" ]; function getValue(value, fallbackValue = "") { return function(token, { system,...rest }) { if (!value) return fallbackValue; if (isCSSFunction(value)) return generateCalc(token)(value, { system, ...rest }); else { if ((0, utils_exports.isNumeric)(value)) return value; const resolvedToken = `${token}.${value}`; if (isCSSToken(system)(resolvedToken)) return system.cssMap[resolvedToken].ref; else return value; } }; } function isOperator(value) { return (/* @__PURE__ */ new RegExp(`\\s[${OPERATORS.join("\\")}]\\s`)).test(value); } function generateCalc(token) { return function(value, options) { if (value == null || globalValues.has(value)) return value; if (!isCSSFunction(value)) return value; let { type, values } = getCSSFunction(value); if (!type || !values) return value; switch (type) { case "calc": return `calc(${splitValues(values, (char, prev, next) => isOperator(`${prev}${char}${next}`), true).map((value$1) => { if (value$1 && OPERATORS.includes(value$1)) return value$1; return getValue(value$1)(token, options); }).join(" ")})`; case "min": case "max": { let [firstValue, secondValue, ...otherValues] = splitValues(values); firstValue = getValue(firstValue, "100%")(token, options); secondValue = getValue(secondValue, "100%")(token, options); otherValues = otherValues.map((value$1) => getValue(value$1)(token, options)); return `${type}(${firstValue}, ${secondValue}` + (otherValues.length ? `, ${otherValues.join(", ")}` : "") + ")"; } case "clamp": { let [min, preferred, max] = splitValues(values); if (!max) { max = preferred; preferred = ""; } min = getValue(min)(token, options); preferred = getValue(preferred, "100%")(token, options); max = getValue(max)(token, options); return `clamp(${min}, ${preferred}, ${max})`; } case "minmax": { let [min, max] = splitValues(values); min = getValue(min)(token, options); max = getValue(max)(token, options); return `minmax(${min}, ${max})`; } case "fit-content": { let [value$1] = splitValues(values); value$1 = getValue(value$1)(token, options); return `fit-content(${value$1})`; } default: return value; } }; } //#endregion export { generateCalc }; //# sourceMappingURL=calc.js.map