Stylized elements
Benefit from ShadowDOM, CSS Nesting, @layer, @scope, in addition, browser compatibility has improved in recent years, and vendor private prefixes have also been cancelled, there is no longer a need for CSS Modules similar solutions.
Shared style
Use the css to create a shared style table, use @adoptedStyle to apply to the required elements.
Because the style cannot penetrate the ShadowDOM, the global style table cannot be used to achieve sharing styles. But you can use CSS variables to achieve the same effect.
CSS in JS
You can reference CSS selectors in JS:
NOTE
Use
$as a key to represent:host, :scopeselectors, allowing styles to apply to both ShadowDOM and LightDOM.
Stylized Instance
If you need to specify the style of a single instance through properties or states, you can:
This method is complex and troublesome to write; it is recommended to use createDecoratorTheme
Customize the style outside the element
Use ::part(only ShadowDOM) to export the internal content of the element, allowing external custom styles:
Also use ElementInternals.states to export element internal state, external styling this element for current state:
NOTE
Note the difference with
createState
TIP
Can also customize element styles using hack, for example:
GemLinkElement[Symbol.metadata].adoptedStyleSheets.push( css` * { font-style: italic; } `, );
Custom element external style
Tailwind CSS
Atomic CSS solutions such as Tailwind only support Light DOM