Attr Types
Convert simple types to and from HTML node attributes. This is useful when building WebComponents.
Note that this only allows simple types (as opposed to complex types), which are typically used in HTML attributes. See this on how to pass complex data to WebComponents.
Parsing
; AttrTypes // => 'asdf'AttrTypes // => 3AttrTypes // => ['w', 'a', 's', 'd']AttrTypes // => trueAttrTypes // => false const aOfN = AttrTypes; // => [1, 2, 3] // => null const lOrR = AttrTypes;AttrTypes // => 'left'AttrTypes // => null
Stringifying
; AttrTypesnumber // => '3'AttrTypesarray // => 'w,a,s,d'AttrTypesbool // => ''AttrTypesbool // => null const aOfN = AttrTypes;aOfN // => '1,2,3' const lOrR = AttrTypes;lOrR // => 'left'lOrR // => null
DOM Interaction
To get the value of an attribute (e.g. inside attributeChangedCallback
):
// You need to remember the typeconst attrType = ;const value = ;
To reflect a changed property in the node attributes, use:
// You need to remember the typeconst attrType = ; const attr = attrType; if attr == null el; else el;
AttrTypes is used in the wild by hy-drawer and hy-push-state.