@marcoms/make-element
Advanced tools
Comparing version 4.0.3 to 4.1.0
{ | ||
"name": "@marcoms/make-element", | ||
"version": "4.0.3", | ||
"version": "4.1.0", | ||
"description": "Create custom elements without boilerplate", | ||
@@ -5,0 +5,0 @@ "main": "build/make-element.js", |
@@ -99,2 +99,8 @@ # make-element | ||
##### `boolAttr` | ||
Whether the attribute is a boolean attribute | ||
Boolean attributes automatically have defined a `toAttr` function that returns `''` (making the attribute true) on a truthy value and `undefined` (removing the attribute) on a falsy value. | ||
##### `get(val)` | ||
@@ -141,2 +147,4 @@ | ||
Return values of `undefined` will remove the attribute | ||
##### `fromAttr(val)` | ||
@@ -143,0 +151,0 @@ |
@@ -17,2 +17,3 @@ export interface ElementDef { | ||
attr?: string; | ||
boolAttr?: boolean; | ||
set?: SetFn; | ||
@@ -52,2 +53,9 @@ get?: GetFn; | ||
interface RegisteredProp extends PropDef { | ||
boolAttr: boolean; | ||
set: SetFn; | ||
get: GetFn; | ||
toAttr: ToAttrFn; | ||
fromAttr: FromAttrFn; | ||
coerce: CoerceFn; | ||
val: any; | ||
@@ -86,2 +94,10 @@ hasSet: boolean; | ||
function convertToBoolAttr(val) { | ||
if (Boolean(val)) { | ||
return ''; | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
function makeElement(def: ElementDef = {}): CustomElementClass { | ||
@@ -136,5 +152,11 @@ const props: PropDefs = def.props || {}; | ||
let boolAttr = false; | ||
if (typeof propDef.boolAttr === 'boolean') { | ||
boolAttr = propDef.boolAttr; | ||
} | ||
let toAttr = identity; | ||
if (typeof propDef.toAttr === 'function') { | ||
// this === element instance | ||
if (boolAttr) { | ||
toAttr = convertToBoolAttr; | ||
} else if (typeof propDef.toAttr === 'function') { | ||
toAttr = propDef.toAttr; | ||
@@ -170,2 +192,4 @@ } | ||
boolAttr, | ||
// function used to produce an attribute value from a | ||
@@ -248,4 +272,8 @@ // property value | ||
// invoke attributeChangedCallback | ||
this.setAttribute(attrName, attrVal); | ||
if (attrVal !== undefined) { | ||
// invoke attributeChangedCallback | ||
this.setAttribute(attrName, attrVal); | ||
} else { | ||
this.removeAttribute(attrName); | ||
} | ||
} | ||
@@ -252,0 +280,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
132673
1172
220