37 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-08-20 23:25:37 +04:00
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
export var token2CSSVar = function token2CSSVar(token) {
var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
return "--".concat(prefix ? "".concat(prefix, "-") : '').concat(token).replace(/([a-z0-9])([A-Z])/g, '$1-$2').replace(/([A-Z]+)([A-Z][a-z0-9]+)/g, '$1-$2').replace(/([a-z])([A-Z0-9])/g, '$1-$2').toLowerCase();
};
export var serializeCSSVar = function serializeCSSVar(cssVars, hashId, options) {
if (!Object.keys(cssVars).length) {
return '';
}
return ".".concat(hashId).concat(options !== null && options !== void 0 && options.scope ? ".".concat(options.scope) : '', "{").concat(Object.entries(cssVars).map(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
key = _ref2[0],
value = _ref2[1];
return "".concat(key, ":").concat(value, ";");
}).join(''), "}");
};
export var transformToken = function transformToken(token, themeKey, config) {
var cssVars = {};
var result = {};
Object.entries(token).forEach(function (_ref3) {
var _config$preserve, _config$ignore;
var _ref4 = _slicedToArray(_ref3, 2),
key = _ref4[0],
value = _ref4[1];
if (config !== null && config !== void 0 && (_config$preserve = config.preserve) !== null && _config$preserve !== void 0 && _config$preserve[key]) {
result[key] = value;
} else if ((typeof value === 'string' || typeof value === 'number') && !(config !== null && config !== void 0 && (_config$ignore = config.ignore) !== null && _config$ignore !== void 0 && _config$ignore[key])) {
var _config$unitless;
var cssVar = token2CSSVar(key, config === null || config === void 0 ? void 0 : config.prefix);
cssVars[cssVar] = typeof value === 'number' && !(config !== null && config !== void 0 && (_config$unitless = config.unitless) !== null && _config$unitless !== void 0 && _config$unitless[key]) ? "".concat(value, "px") : String(value);
result[key] = "var(".concat(cssVar, ")");
}
});
return [result, serializeCSSVar(cssVars, themeKey, {
scope: config === null || config === void 0 ? void 0 : config.scope
})];
};