As alternative solution for https://github.com/vaadin/web-components/issues/5214 we now have styled some css parts in the shadow DOM of the avatar(-group).
What I am wondering is:
To what extend can/should we style css (parts) in Vaadin (but also in general) and not break backwards compatibilty?
What are/where are best practices for building good webcomponents so API consumers will not break backwards compatility too fast.
For example: building a webcomponent with a root flex container will break when an API consumer changes the css display property, so in this case moving the flex container to the shadow DOM could make the component less fragile. But stil, the consumer can break a lot of stuff...
The same applies for css parts.
Product Owner of Vaadin components here. You are asking the very same questions we have been asking ourselves for years:
etc.
Our initial approach, in Vaadin 10 where the Web Component based components first shipped, was to hide as much as possible of the components' internal structure in Shadow DOM, and to define only elements with part names (and the root elements) to be part of the "public styling API".
This approach had many drawbacks however:
Today, my take on the matter is that it's simply futile to try to prevent any of this. We still have shadow DOM in the components, but the style encapsulation aspect is not really something we actively try to utilize. We move any elements to light DOM that benefit from it (while keeping other parts in the shadow DOM for different technical reasons), and we're ready to expose any element as a named part for which there is a clear use case for custom CSS.
What I'm saying here is of course about a very generic component library, that is not meant for internal use for a particular product or company, but for thousands of companies to use in any way they like, which often includes tweaking the look and feel, and in some cases even layout/structural CSS, to fit their needs.
If I were to build a set of components for a design system tailored for a particular company or product, I would probably be much more inclined to limit customizability, but I'd still need to balance that with accessibility etc.
So, in an attempt to answer your questions:
For example: building a webcomponent with a root flex container will break when an API consumer changes the css display property, so in this case moving the flex container to the shadow DOM could make the component less fragile.
For this specific case, one option is to use !important
in the shadow DOM styles, which prevent light DOM styles from overriding them:
:host {
display: flex !important;
}
Another useful tool is to reset all properties on the host, so that there aren't any surprises coming from the light DOM (inherited properties mainly). Then set any properties with !important
that are required for the component to work correctly:
:host {
all: initial;
display: flex !important;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.