HTML Element Plus
The bits of Custom Elements I keep reimplementing in one handy class.
I have extended HTMLElement as HTMLElementPlus
to contain the bits of functionality I keep reimplementing.
Example
;; { super; } // Set the HTML for the template static { return html` `; } { if !thisshadowRoot this; thisshadowRoot; } static { if name === 'lang' return null; } static { return 'lang'; } customElements;
Example Using Mixins
/* Pick one or more of the following features: allAttributesChanged:* Adds a callback for when all the attributes have been parsed.* Also provides optional methods for Attribute parsing and providing default values static get observedAttributes() {} static defaultAttributeValue (name) {} static parseAttributeValue (name, value) {} emitEvent* Provides el.emitEvent(name, details) for quick event firing templateHelper* Provides management for templates and automatically running ShadyCSS* Set `static get templateHTML` to define your html, use el.templateContent to get it out refs* Access defined elements in the shadow dom by using the `ref="foo"` attribute.* Read it later using el.refs.foo */; { super; } { if !thisshadowRoot this; thisshadowRootinnerHTML = '<div ref="test" id="test-div"></div>'; } static { return 'one' 'two'; } static { if name === 'one' return this; if name === 'two' return this; } static { return ; }
Features
These are the current features.
Collected Attribute Change Callbacks
Provides a callback when all attributes have been parsed, rather than one-by-one. allAttributesChangedCallback
useful for waiting to handle all at once.
allAttributesChangedCallback
gets called with an object with parsed attributes.
The parser can be set by setting the function static parseAttributeValue(name, value)
in the class.
Default attribute values can be provided by setting the static defaultAttributeValue(name)
function, so you can provide sensible fallback values.
This also gets called during the constructor if there are no attributes listed.
Query the shadow dom by reference
E.g. an element in the shadow dom: <span ref="foobar"></span>
can be queried using this.refs.foobar
;
Easy event firing.
Fire an event using this.emitEvent('event-name', {foo: 'bar'});
This can be listed for using, el.addEventListener
;
Automatic Template Creation
Setting templateHTML in your class to return a string of you template contents will build and populate a template. It will automatically run shadyDOM if you are using the polyfill. E.g.