Skip to content

Commit 511e6f5

Browse files
rolfsmedsedler-san
andauthored
Docs/custom flow component cssimport (vaadin#3779)
* Clarified use of cssimport in custom Flow components * Fixed incorrect example of applying cssimport to custom flow component --------- Co-authored-by: edler-san <19165931+edler-san@users.noreply.github.com>
1 parent fddefda commit 511e6f5

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

articles/flow/create-ui/creating-components/extending-component.adoc

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ image:images/vaadin-number-field-server-side-extension.png[Number Field server s
3535

3636
[source,java]
3737
----
38-
@CssImport("./styles/numeric-field-styles.css")
38+
@CssImport(value = "./styles/numeric-field-styles.css")
3939
public class NumericField extends TextField {
4040
4141
private Button substractBtn;
@@ -81,19 +81,16 @@ public class NumericField extends TextField {
8181
incrementValue);
8282
});
8383
84-
getElement().setAttribute("theme", "numeric");
84+
addClassName("numeric");
8585
styleBtns();
8686
8787
addToPrefix(substractBtn);
8888
addToSuffix(addBtn);
8989
}
9090
9191
private void styleBtns() {
92-
// Note: The same as addThemeVariants
93-
substractBtn.getElement()
94-
.setAttribute("theme", "icon");
95-
addBtn.getElement()
96-
.setAttribute("theme", "icon");
92+
substractBtn.addThemeName("icon");
93+
addBtn.addThemeName("icon");
9794
}
9895
9996
public void setNumericValue(int value) {
@@ -115,20 +112,20 @@ Every component has a [methodname]`getElement()` method that allows you to acces
115112
See <<many-elements#,Creating a Component Using Multiple Elements>> for more.
116113

117114
Import additional styles for the component using the `@CssImport` annotation.
118-
These styles apply only to the `NumericField` component, and not to all `TextField` components.
115+
Note that CSS selectors (based on the `numeric` classname applied to the component) must be used to scope the styles to this component.
119116

120-
*Example*: Creating [filename]`numeric-field-styles.css` to customize the appearance of the `vaadin-text-field` component.
117+
*Example*: Creating [filename]`numeric-field-styles.css` to customize the appearance of the custom `vaadin-text-field` component.
121118

122119
.`styles/numeric-field-styles.css`
123120
[source,css]
124121
----
125-
:host([theme~="numeric"]) [part="input-field"] {
122+
vaadin-text-field.numeric::part(input-field) {
126123
background-color: var(--lumo-base-color);
127124
border: 1px solid var(--lumo-contrast-30pct);
128125
box-sizing: border-box;
129126
}
130127
131-
:host([theme~="numeric"]) [part="value"]{
128+
vaadin-text-field.numeric::part(value) {
132129
text-align: center;
133130
}
134131
----

articles/styling/legacy/css-import.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ public class MyUI extends Div {
1717
}
1818
----
1919

20-
Although this mechanism still works, the <<../application-theme#, application theme folder>> is now the recommended approach for loading application styles. It also supports <<../advanced/shadow-dom-styling#, injection of Shadow DOM styles>> into Vaadin components through the `components` sub-folder. `@CssImport` is still useful for loading stylesheets into custom standalone components, however.
20+
Although this mechanism still works, the <<../application-theme#, application theme folder>> is now the recommended primary approach for loading application styles. It also supports <<../advanced/shadow-dom-styling#, injection of Shadow DOM styles>> into Vaadin components through the `components` sub-folder.
21+
22+
`@CssImport` is still a valid mechanism for loading stylesheets into <<{articles}/flow/create-ui/creating-components#, custom Flow-based components>>, in cases where these need to be separate from the application theme.

0 commit comments

Comments
 (0)