@builder.io/mitosis
Version:
Write components once, run everywhere. Compiles to Vue, React, Solid, and Liquid. Import code from Figma and Builder.io
42 lines (41 loc) • 1.81 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.collectClassString = void 0;
// This should really be a preprocessor mapping the `class` attribute binding based on what other values have
// to make this more pluggable
function collectClassString(json, bindingOpenChar = '{', bindingCloseChar = '}') {
var _a, _b;
const staticClasses = [];
const hasStaticClasses = Boolean(staticClasses.length);
if (json.properties.class) {
staticClasses.push(json.properties.class);
delete json.properties.class;
}
if (json.properties.className) {
staticClasses.push(json.properties.className);
delete json.properties.className;
}
const dynamicClasses = [];
if (typeof ((_a = json.bindings.class) === null || _a === void 0 ? void 0 : _a.code) === 'string') {
dynamicClasses.push(json.bindings.class.code);
delete json.bindings.class;
}
if (typeof ((_b = json.bindings.className) === null || _b === void 0 ? void 0 : _b.code) === 'string') {
dynamicClasses.push(json.bindings.className.code);
delete json.bindings.className;
}
const staticClassesString = staticClasses.join(' ');
const dynamicClassesString = dynamicClasses.join(" + ' ' + ");
const hasDynamicClasses = Boolean(dynamicClasses.length);
if (hasStaticClasses && !hasDynamicClasses) {
return `"${staticClassesString}"`;
}
if (hasDynamicClasses && !hasStaticClasses) {
return `${bindingOpenChar}${dynamicClassesString}${bindingCloseChar}`;
}
if (hasDynamicClasses && hasStaticClasses) {
return `${bindingOpenChar}"${staticClassesString} " + ${dynamicClassesString}${bindingCloseChar}`;
}
return null;
}
exports.collectClassString = collectClassString;
;