@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
108 lines (106 loc) • 3.52 kB
JavaScript
import { utils_exports } from "../../utils/index.js";
import { getColorSchemeVar, isColorScheme } from "../system/var.js";
//#region src/core/css/utils.ts
const globalValues = new Set([
"inherit",
"initial",
"none",
"revert",
"unset"
]);
function isCSSFunction(value) {
return (0, utils_exports.isString)(value) && value.includes("(") && value.includes(")");
}
function getCSSFunction(value) {
const [, type, values] = /(^[a-z-A-Z]+)\((.*)\)/g.exec(value) ?? [];
return {
type,
values
};
}
function splitValues(values, cb = (current) => current === ",", addSeparator = false) {
const result = [];
let value = "";
let depth = 0;
for (let i = 0; i < values.length; i++) {
const current = values[i];
const prev = values[i - 1];
const next = values[i + 1];
if (current === "(") depth++;
if (current === ")") depth--;
if (!(0, utils_exports.isUndefined)(current) && cb(current, prev, next) && depth === 0) {
if (value) result.push(value.trim());
if (addSeparator) result.push(current);
value = "";
} else value += current;
}
if (value) result.push(value.trim());
return result.filter(Boolean);
}
function isCSSVar(value) {
return /^var\(--.+\)$/.test(value);
}
function isCSSToken({ cssMap }) {
return function(value) {
return (0, utils_exports.isObject)(cssMap) && value in cssMap && !!cssMap[value]?.ref;
};
}
function isImportant(value) {
return (0, utils_exports.isString)(value) && (/\s*!important$/.test(value) || /\s*!$/.test(value));
}
function omitImportant(value) {
return (0, utils_exports.isString)(value) ? value.replace(/(!important|!)$/, "").trim() : value;
}
function insertImportant(value, style) {
if ((0, utils_exports.isString)(value)) return value + " !important";
else if ((0, utils_exports.isObject)(value)) if (style?.important) return Object.fromEntries(Object.entries(value).map(([key, value$1]) => [key, value$1 + " !important"]));
else {
if (!style?.properties) return value;
for (const property of style.properties) value[property] += " !important";
}
return value;
}
function analyzeCSSValue(value) {
let n = parseFloat(value.toString());
const unit = value.toString().replace(String(n), "");
return {
unit,
unitless: !unit,
value
};
}
function tokenToVar(system) {
return function(token, value, fallbackValue) {
if (isColorScheme(value)) return getColorSchemeVar(system)(value);
const resolvedToken = `${token}.${value}`;
if (isCSSToken(system)(resolvedToken)) return system.cssMap[resolvedToken].ref;
else return fallbackValue || value;
};
}
function varToValue(system) {
return function(variable) {
const value = system.cssVars[variable];
if (isCSSVar(value)) return varToValue(system)(value.replace(/^var\(/, "").replace(/\)$/, ""));
else return value;
};
}
function tokenToValue(system) {
return function(token, value, fallbackValue) {
const resolvedToken = `${token}.${value}`;
if (isCSSToken(system)(resolvedToken)) {
const variable = system.cssMap[resolvedToken].var;
return varToValue(system)(variable);
} else return fallbackValue || value;
};
}
function combineFunctions(a, b) {
return function(value, ...args) {
return b(a(value, ...args), ...args);
};
}
function pipe(...transformers) {
return transformers.reduce(combineFunctions);
}
//#endregion
export { analyzeCSSValue, getCSSFunction, globalValues, insertImportant, isCSSFunction, isCSSToken, isCSSVar, isImportant, omitImportant, pipe, splitValues, tokenToValue, tokenToVar, varToValue };
//# sourceMappingURL=utils.js.map