GemElement more
Features other than Attribute/Property/Store/State.
Template syntax extension
Gem has made many modifications to lit-html and built some common functions without passing directive.
Reference DOM
If you want to manipulate the DOM content within an element, such as reading the value of <input>, you can use querySelector to get the element you want, in order to get the type support of TypeScript, GemElement provides createRef to complete this work:
Rest properties
Sometimes, when passing properties to an element through parameters, a lot of repetitive code needs to be written, for example:
Gem supports rest properties, similar to React:
Conditional rendering
In development, conditional rendering is often encountered, and it is usually written like this:
This has low readability, so Gem supports v-if, resembling Vue:
NOTE
Compared to the original approach, using v-if incurs a slight performance loss during element initialization, as those elements that will not render will still have their parameters parsed and created.
Custom event
Custom event is a method of transferring data. It can be easily done by using dispatch(new CustomEvent('event')). To obtain TypeScript type support, GemElement allows you to quickly define methods to emit custom events:
Add custom event handler:
Effect
In many cases, elements need to perform some side effects based on certain attributes, such as network requests, and finally update the document. This is where @effect comes in handy. It can check dependencies every time the element is render and execute a callback if the dependencies change.
Below is an example of effect that depends on child elements(Other implementations):
Memo
In order to avoid performing some complex calculations when rerender, @memo can execution of the callback when specified dependencies change, unlike the effect, memo execution before the render.
NOTE
@memosupportsgetter, but decorators spec does not currently support private names, this restriction can be release usingSWC plugin .- The decorators
@effectand@memoare based onGemElement.effectandGemElement.memo. If necessary,effectandmemocan be dynamically added usingGemElement.effectandGemElement.memo