forked from tsai/budibase
committed by
GitHub
5 changed files with 111 additions and 82 deletions
@ -1,43 +1,70 @@ |
|||||
export default class ClassBuilder { |
export default class ClassBuilder { |
||||
constructor(block, customDefaults) { |
constructor(block, defaultIgnoreList) { |
||||
this.block = `mdc-${block}`; |
this.block = `mdc-${block}`; |
||||
this.customDefaults = customDefaults; //will be ignored when building custom classes
|
this.defaultIgnoreList = defaultIgnoreList; //will be ignored when building custom classes
|
||||
} |
} |
||||
|
|
||||
// classParams: {modifiers:[] (mdc), custom:[] (bbmd), extra:[] (any)}
|
/* |
||||
blocks(classParams) { |
handles both blocks and elementss (BEM MD Notation) |
||||
let base = this.block; |
params = {elementName: string, props: {modifiers{}, customs:{}, extras: []}} |
||||
if (classParams == undefined) return base; |
All are optional |
||||
return this.buildClass(base, classParams); |
*/ |
||||
|
build(params) { |
||||
|
if (!params) return this.block; //return block if nothing passed
|
||||
|
const { props, elementName } = params; |
||||
|
let base = !!elementName ? `${this.block}__${elementName}` : this.block; |
||||
|
if (!props) return base; |
||||
|
return this._handleProps(base, props); |
||||
} |
} |
||||
|
|
||||
//elementName: string, classParams: {}
|
//Easily grab a simple element class
|
||||
elements(elementName, classParams) { |
elem(elementName) { |
||||
let base = `${this.block}__${elementName}`; |
return this.build({ 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
|
//use if a different base is needed than whats defined by this.block
|
||||
buildClass(base, classParams) { |
debase(base, elementProps) { |
||||
|
if (!elementProps) return base; |
||||
|
return this._handleProps(base, elementProps); |
||||
|
} |
||||
|
|
||||
|
//proxies bindProps and checks for which elementProps exist before binding
|
||||
|
_handleProps(base, elementProps) { |
||||
let cls = base; |
let cls = base; |
||||
const { modifiers, customs, extras } = classParams; |
const { modifiers, customs, extras } = elementProps; |
||||
if (!!modifiers) |
if (!!modifiers) cls += this._bindProps(modifiers, base); |
||||
cls += modifiers.map(m => (!!m ? ` ${base}--${m}` : "")).join(" "); |
if (!!customs) cls += this._bindProps(customs, base, true); |
||||
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(" ")}`; |
if (!!extras) cls += ` ${extras.join(" ")}`; |
||||
return cls.trim(); |
return cls.trim(); |
||||
} |
} |
||||
|
|
||||
|
/* |
||||
|
Handles both modifiers and customs. Use property, value or both depending |
||||
|
on whether it is passsed props for custom or modifiers |
||||
|
if custom uses the following convention for scss mixins: |
||||
|
bbmd-{this.block}--{property}-{value} |
||||
|
bbmd-mdc-button--size-large |
||||
|
*/ |
||||
|
_bindProps(elementProps, base, isCustom = false) { |
||||
|
return Object.entries(elementProps) |
||||
|
.map(([property, value]) => { |
||||
|
//disregard falsy and values set by defaultIgnoreList constructor param
|
||||
|
if ( |
||||
|
!!value && |
||||
|
(!this.defaultIgnoreList || !this.defaultIgnoreList.includes(value)) |
||||
|
) { |
||||
|
let classBase = isCustom ? `bbmd-${base}` : `${base}`; |
||||
|
let valueType = typeof value; |
||||
|
|
||||
|
if (valueType == "string" || valueType == "number") { |
||||
|
return isCustom |
||||
|
? ` ${classBase}--${property}-${value}` |
||||
|
: ` ${classBase}--${value}`; |
||||
|
} else if (valueType == "boolean") { |
||||
|
return ` ${classBase}--${property}`; |
||||
|
} |
||||
|
} |
||||
|
}) |
||||
|
.join(""); |
||||
|
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue