The element
helper automates many utility features that
often have to be added to every new custom element, like dispatching
to different handler methods depending on the name of a changed attribute
or adding getters and setters for these attributes.
The static attributes
property, when present,
is used to automatically add the static observedAttributes
property for the custom element API, as well as define getters and setters
for the listed attributes.
A pair of filters can be given to modify the value after getting or before setting it. This can be used to convert between the string values that attributes restrict us to and a more sensible representation as well as some validation (attributes can be changed externally, after all).
Object keys in camelCase will be converted to kebab-case before being
used as attribute names in the auto-generated observedAttributes
.
The rules are:
get
property, it will be incorporated as a filter into the getter.
set
property, it will be incorporated as a filter into the setter.
false
no setter will be added (read only attribute).
The attributeChangedCallback
method is added automatically to the class.
This auto-generated callback will first look up a ${attributeName}Changed
method and call it, if found, with the old and new values as arguments.
Then it will look for a changed
method and call it with the same arguments
as attributeChangedCallback
normally receives.
Attribute names will be converted from camelCase to kebab-case when needed before
being used to form a method name to look up.
Changing an attribute foo-bar
will look for a fooBarChanged
method.
Certain methods, like re-rendering the content of a component should often happen in response to events that can happen repeatedly, or in response to many different events that can all happen at once.
To avoid having to repeatedly add complicated checks to handle these
event bursts, element
introduces the concept of dollar-methods.
Any method with a name starting with $
will automatically
have a sibling-method defined, with the dollar removed. Each time this
auto-generated method is called, its argument list will be pushed to an array
and a call of the original dollar-method will be scheduled as a micro-task.
The original method will then be called with the argument-lists of the individual calls to its dollar-less counterpart all at once.
It is of course still possible to manually call the dollar-method when immediate execution is wanted.
Note how in the following example, all three change methods would be called
in rapid succession if the browser applies the custom element class to an element
it found on the page with all three attributes foo
, bar
and baz
defined.
Here, this.render()
will not instantly re-render the whole component
but instead schedule the re-render in a micro-task, potentially avoiding lots of
repeated work.