Budibase is an open-source low-code platform for creating internal apps in minutes. Supports PostgreSQL, MySQL, MSSQL, MongoDB, Rest API, Docker, K8s 🚀
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

43 lines
1.6 KiB

export default class ClassBuilder {
constructor(block, customDefaults) {
this.block = `mdc-${block}`;
this.customDefaults = customDefaults; //will be ignored when building custom classes
}
// classParams: {modifiers:[] (mdc), custom:[] (bbmd), extra:[] (any)}
blocks(classParams) {
let base = this.block;
if (classParams == undefined) return base;
return this.buildClass(base, classParams);
}
//elementName: string, classParams: {}
elements(elementName, classParams) {
let base = `${this.block}__${elementName}`;
if (classParams == undefined) return base;
return this.buildClass(base, classParams);
}
//TODO: Classparams modifier and custom could take an object. Booleans or numbers use key value for classes, strings use property value for classes. Extras to stay as is
buildClass(base, classParams) {
let cls = base;
const { modifiers, customs, extras } = classParams;
if (!!modifiers)
cls += modifiers.map(m => (!!m ? ` ${base}--${m}` : "")).join(" ");
if (!!customs)
cls += Object.entries(customs)
.map(([property, value]) => {
//disregard falsy and values set by customDefaults constructor param
if (
!!value &&
(!this.customDefaults || !this.customDefaults.includes(value))
) {
//custom scss name convention = bbmd-[block | element]--[property]-[value]
return ` bbmd-${base}--${property}-${value}`;
}
})
.join("");
if (!!extras) cls += ` ${extras.join(" ")}`;
return cls.trim();
}
}