How do you style TabContainer and Tabs
- [ ] Documentation is unclear
- [ ] Documentation is missing
Generally, I have been struggling with styling in UI5 web components. The documentation doesn't explicitly detail which CSS attributes should be changed to style the icons that are used in tabs/tab containers or the text for the tabs. I have tried about 6 different ways, but would like to know what is the bigger picture regarding styling. How does one change exactly the component they are targeting and is there any documentation explaining this in more detail. I did about a 4-hour google search to no avail. I really appreciate any help on this, because it's such a wonderful set of tools.
I have seen some information on shadow parts but noticed that these aren't generally exposed to be changed. I have also seen an addcsstyling function that did not work for me. I am generally confused on how to do this. Adding a styles to index.html or inline styles did not work on any attempts.
Hi @lix2k3
One of the core principles of Web Components is encapsulation. In our project, this encapsulation is being used in order to implement the Fiori design guidelines. This means that using UI5 Web Components, you follow the Fiori guidelines.
From technical POV, restyling Web Components is not impossible(although it can be very ugly).
Native way of restyling
If we assume that we have the following HTML(two tabcontainers with different ID's):
<ui5-tabcontainer id="tabContainer1" fixed collapsed show-overflow>
<ui5-tab text="Products" additional-text="125">
</ui5-tab>
<ui5-tab-separator></ui5-tab-separator>
<ui5-tab icon="sap-icon://menu2" text="Laptops" semantic-color="Positive" additional-text="25">
</ui5-tab>
<ui5-tab icon="sap-icon://menu" text="Monitors" selected semantic-color="Critical" additional-text="45">
</ui5-tab>
<ui5-tab icon="sap-icon://add" text="Keyboards" semantic-color="Negative" additional-text="15">
</ui5-tab>
</ui5-tabcontainer>
<ui5-tabcontainer id="tabContainer2" fixed collapsed show-overflow>
<ui5-tab text="Products" additional-text="125">
</ui5-tab>
<ui5-tab-separator></ui5-tab-separator>
<ui5-tab icon="sap-icon://menu2" text="Laptops" semantic-color="Positive" additional-text="25">
</ui5-tab>
<ui5-tab icon="sap-icon://menu" text="Monitors" selected semantic-color="Critical" additional-text="45">
</ui5-tab>
<ui5-tab icon="sap-icon://add" text="Keyboards" semantic-color="Negative" additional-text="15">
</ui5-tab>
</ui5-tabcontainer>
Now let's imagine that we want to change the add icon colour to red:
document.querySelector("#tabContainer1").shadowRoot.querySelector("ui5-icon[name='add']").style.color = "red".
If we want to change all the add icons' colour to red, in all of our tabcontainers, we can just use querySelectorAll and cycle through the results.
This is the native way of restyling web components(although we brake their encapsulation).
UI5 Web Components API for inserting a style
In our project, we have added a special method that can insert CSS for any of our web components. You can import & use it like this:
import { addCustomCSS } from "@ui5/webcomponents-base/dist/Theming";
addCustomCSS("ui5-select", ".ui5-select-root { background-color: red; } ");
Note: This method doesn't work on the instance and adds the styles to all of the components with this tag(in this case to all ui5-select's on the page). Use this method with caution, because it might be changed/removed in the future.
Hello @lix2k3 these are some general patterns/ideas to style web components:
-
Styles on tag level: we designed more simple components such as Title, Label, Badge and few more in such way, that it is possible to set styles/classes on the custom elements like “ui5-badge { background-color: red}” that will take effect. You can try this yourself here: https://sap.github.io/ui5-webcomponents/master/playground/main/pages/Badge/
-
Shadow Parts : For more complex components, the styling on tag level is not possible, therefore we introduced shadow parts on specific elements in the components’ Shadow DOM, that the user can restyle, using standard CSS syntax for shadow parts For example: you can change the appearance of the Card’s heading, subheading and status with the CSS shadow parts syntax:
#myCard::part(status) {
color: red;
}
-
Custom Theme schema: The UI5 WebComponents use CSS variables, to change the entire color scheme, the users can create their own theme. More details on how to do that, you can find here: https://sap.github.io/ui5-webcomponents/playground/docs/customtheming/
-
JS API: addCustomCSS - already explained above.
Best, ilhan
Thank you. This is very helpful and more than enough to get started!