'Style Web Components via Attributes or Custom Styles
i have a Web Component my-icon. The component wraps an icon-font and some "bussines related" ui-logic to choose the correct icon by the given name:
The user can also decide which size and color the icon should have. What is the right way to provide this functonality?
- Custom Styles --my-icon-color and --my-icon-size
- Properties + Attributes for color and size
- Both
Currently i provide both, but i'm not shure if it's a good practice to "style" a component via attributes and properties. On the other hand it may ease the use of the component (e.g. change the color via Data-Binding).
Thank You
Solution 1:[1]
There is no best practice, there are multiple ways to style shadowDOM, which one you use is up to you.
ShadowDOM is styled by:
<style>within shadowDOMInheritable styles
https://lamplightdev.com/blog/2019/03/26/why-is-my-web-component-inheriting-styles/(cascading) CSS properties
shadowParts (and Themes)
https://meowni.ca/posts/part-theme-explainer/<slot>are reflected, they are NOT styled by the shadowDOM, but by its container.
See: ::slotted content(feb 2022) Constructible StyleSheets is still a Chromium only party
https://caniuse.com/mdn-api_cssstylesheet_cssstylesheetyour custom directive
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Danny '365CSI' Engelman |
