2020-01-29 11:10:25 +01:00
|
|
|
export default class ClassBuilder {
|
2020-02-10 16:51:09 +01:00
|
|
|
constructor(block, customDefaults) {
|
|
|
|
this.block = `mdc-${block}`
|
|
|
|
this.customDefaults = customDefaults //will be ignored when building custom classes
|
2020-02-03 19:38:09 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 16:51:09 +01:00
|
|
|
// classParams: {modifiers:[] (mdc), custom:[] (bbmd), extra:[] (any)}
|
|
|
|
blocks(classParams) {
|
|
|
|
let base = this.block
|
|
|
|
if (classParams == undefined) return base
|
|
|
|
return this.buildClass(base, classParams)
|
2020-02-03 19:38:09 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 16:51:09 +01:00
|
|
|
//elementName: string, classParams: {}
|
|
|
|
elements(elementName, classParams) {
|
|
|
|
let base = `${this.block}__${elementName}`
|
|
|
|
if (classParams == undefined) return base
|
|
|
|
return this.buildClass(base, classParams)
|
2020-02-03 19:38:09 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 16:51:09 +01:00
|
|
|
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}`
|
2020-02-10 11:04:20 +01:00
|
|
|
}
|
2020-02-10 16:51:09 +01:00
|
|
|
})
|
|
|
|
.join("")
|
|
|
|
if (extras) cls += ` ${extras.join(" ")}`
|
|
|
|
return cls.trim()
|
2020-02-10 11:04:20 +01:00
|
|
|
}
|
2020-01-29 11:10:25 +01:00
|
|
|
}
|