v2 Introduction
After a long development process, Gem has finally entered v2, focusing on allowing users to write custom elements in a simple way. Below are some major updates in v2.
Decorators
v2 uses ES decorators GemElement.constructor with decorators:
Using decorators provides better scalability and reduces code complexity. For the same purpose, decorators like @effect and @memo have been added to help you write more concise custom elements:
WARNING
In the future, Gem may deprecate lifecycle callback functions and fully use decorators instead.
Internal State and DOM Ref
v1 used a specific field state to represent the internal state of the element and used this.setState to update the state. In v2, any field can be used, as defining the state also defines the update method:
Similar to createState, use createRef to replace v1's @refobject:
Default Use of Light DOM
One reason Gem uses Shadow DOM is for style isolation, allowing users to write "modular" CSS directly. However, there are some drawbacks to using Shadow DOM for WebApp development:
- Cannot use SVG symbols
- URL Hash is ineffective
document.activeElementis ineffective- Inconvenient to integrate React/Vue components
- Poorer performance
If you are not writing highly encapsulated custom elements (such as UI libraries), using Light DOM is a more suitable choice. Now, the CSS specification has introduced @scope, so Gem takes full advantage of @scope and defaults to using Light DOM, while also supporting "modularity" (v1 does not support Light DOM style "modularity"). In the example below, the div selector will only apply to the content of <my-element>:
NOTE
Just like in the initial example, if you want to use Shadow DOM, you need to add
@shadowand replace:scopewith:host.
Theme enhancement
v1 only supports global themes, while v2 supports scoped themes and theme overrides: Additionally, thanks to relative color syntax, colors ending with Color in the theme directly support using "weights" (similar to font weight) to adjust brightness: theme.textColor500, which is a color slightly brighter than the original textColor.
Creating better Gem together
We hope Gem can become the preferred solution for creating custom elements with excellent design. If you have any suggestions or ideas, please create an issue.