2020-01-29 11:10:25 +01:00
|
|
|
export default class ClassBuilder {
|
2020-02-03 19:38:09 +01:00
|
|
|
constructor(block, customDefaults) {
|
2020-01-29 11:10:25 +01:00
|
|
|
this.block = `mdc-${block}`;
|
2020-02-03 19:38:09 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
buildClass(base, classParams) {
|
|
|
|
let cls = base;
|
|
|
|
const { modifiers, customs, extras } = classParams;
|
|
|
|
if (!!modifiers) cls += modifiers.map(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.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();
|
2020-01-29 11:10:25 +01:00
|
|
|
}
|
|
|
|
}
|