Skip to content

Commit e52787b

Browse files
vaadin-botcaaladortltvjounimshabarov
authored
feat: document NpmPackage npm assets (#4513) (#4648)
* feat: document NpmPackage npm assets Add documentation on copying npm assets for NpmPackage. part of vaadin/flow#21806 * minor chages/clarifications update example * Add info on npm stylesheet with cssimport * Update articles/styling/advanced/npm-packages.adoc * Remove mentions to old feature fron new feature. # Conflicts: # articles/styling/advanced/npm-packages.adoc * edits * Update npm-packages.adoc --------- Co-authored-by: caalador <[email protected]> Co-authored-by: Tomi Virtanen <[email protected]> Co-authored-by: Jouni Koivuviita <[email protected]> Co-authored-by: Mikhail Shabarov <[email protected]>
1 parent ee72b9a commit e52787b

File tree

1 file changed

+53
-79
lines changed

1 file changed

+53
-79
lines changed
Lines changed: 53 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,85 @@
11
---
2-
title: Loading Theme Resources from npm Packages
3-
page-title: Using advanced npm packages in Vaadin styling
4-
description: Describes how to load theme resources from npm packages.
5-
meta-description: Learn how to integrate and use advanced npm packages for custom styling in Vaadin applications.
2+
title: Importing Resources from npm Packages
3+
page-title: Using static resources like fonts, icons, and stylesheets from npm packages in Vaadin
4+
description: Describes how to use resources like fonts, icons, and stylesheets from npm packages.
5+
meta-description: Learn how to use static resources like fonts, icons, and stylesheets from npm packages in Vaadin applications.
66
order: 50
77
---
88

99

1010
= Loading Styles, Fonts & Images from npm Packages
1111

12-
Stylesheets and other theme-related resources like font and image files can be loaded from npm packages through the theme configuration file `theme.json`.
12+
Before you can import stylesheets and other styling-related resources like fonts and icons from npm packages, you need to add the package dependency with the `@NpmPackage` annotation. Define both the package name and version number:
1313

14-
15-
[#styles-from-npm]
16-
== Loading Stylesheets from npm Packages
17-
18-
You can configure a custom theme to import style sheets from npm packages included as dependencies in the project by defining them in an `importCss` array in [filename]`theme.json`. Below is an example of a [filename]`theme.json` for importing CSS from npm packages:
19-
20-
[source,json]
14+
.Adding an npm package dependency
15+
[source,java]
2116
----
22-
{
23-
"importCss": [
24-
"@fortawesome/fontawesome-free/css/regular.css",
25-
"@fortawesome/fontawesome-free/css/all.min.css"
26-
]
17+
// tag::package[]
18+
@NpmPackage(value = "@fortawesome/fontawesome-free", version = "7.1.0")
19+
// end::package[]
20+
public class AppShell implements AppShellConfigurator {
2721
}
2822
----
2923

30-
This loads the external style sheets as if they were imported as local style sheets through [filename]`styles.css`.
31-
32-
[NOTE]
33-
The npm packages must be added to the project. The `importCss` configuration doesn't import the npm packages themselves to the project. You need to do that by using the [annotationname]`@NpmPackage` annotation.
34-
35-
Similar to the document root style sheet, style sheets can be forced to the document root for <<{articles}/flow/integrations/embedding#, embedded Flow application or component>> use cases through a corresponding `documentCss` array, which can be useful for font-face declarations in npm packages:
36-
37-
The following example [filename]`theme.json` defines importing of CSS from npm packages into doc root:
24+
Once you have added an npm package dependency, you can import resources from that package.
3825

39-
.[filename]`theme.json`
40-
[source,json]
41-
----
42-
{
43-
"documentCss": ["@fortawesome/fontawesome-free/css/all.min.css"]
44-
}
45-
----
46-
47-
48-
[#fonts-and-images-from-npm]
49-
== Loading Fonts and Images from npm Packages
50-
51-
In addition to style sheets, other assets like fonts, images, and icons can also be included from npm packages added to the project, by mapping them from the dependency to local URIs in an assets block in [filename]`theme.json`.
26+
[#styles-from-npm]
27+
== Importing Stylesheets from npm Packages
5228

53-
Syntax for mapping assets from npm package to local URIs goes as follows:
29+
Use the [annotation]`@CssImport` annotation to import stylesheets from an npm package. For example, to load `all.min.css` from `@fortawesome/fontawesome-free`:
5430

55-
.[filename]`theme.json`
56-
[source,json]
31+
.Importing a stylesheet from an npm package
32+
[source,java]
5733
----
58-
{
59-
"assets": {
60-
"package-name": {
61-
"asset/glob/pattern": "local/target/path"
62-
}
63-
}
34+
@NpmPackage(value = "@fortawesome/fontawesome-free", version = "7.1.0")
35+
// tag::import[]
36+
@CssImport("@fortawesome/fontawesome-free/css/all.min.css")
37+
// end::import[]
38+
public class AppShell implements AppShellConfigurator {
6439
}
65-
----
66-
67-
For example, to use SVG icons from `@fortawesome/fontawesome-free` npm package, the SVG files should be mapped to some local path as follows:
6840
69-
.Sample [filename]`theme.json` for mapping assets from npm packages.
70-
[source,json]
71-
----
72-
{
73-
"assets": {
74-
"@fortawesome/fontawesome-free": {
75-
"svgs/regular/**": "fontawesome/icons"
41+
@Tag("my-component")
42+
public class MyComponent implements Component {
43+
public MyComponent() {
44+
Span userIcon = new Span();
45+
userIcon.addClassNames("fa-sharp", "fa-solid", "fa-user");
46+
userIcon.getStyle().set("font-family", "'Font Awesome 7 Free'");
47+
add(userIcon);
7648
}
77-
}
7849
}
7950
----
8051

8152

82-
[NOTE]
83-
The npm packages must be added to the project. The assets configuration doesn't import the npm packages themselves to the project. You need to do that by using the [annotationname]`@NpmPackage` annotation.
84-
85-
The SVG images mapped by the example above are now available on the path `fontawesome/icons` relative to the theme's root folder, so they can be referenced in [filename]`styles.css` as follows:
53+
[since:com.vaadin:[email protected]]
54+
[#fonts-and-images-from-npm]
55+
== Using Static Resources from npm Packages
8656

87-
[source,css]
88-
----
89-
.icon-snowflake {
90-
background-image: url('./fontawesome/icons/snowflake.svg');
91-
}
92-
----
57+
The `assets` field in the [annotationname]`@NpmPackage` annotation allows you to copy assets from npm packages into the static resource folder (i.e., the `VAADIN/static` folder), allowing you to references them from Java source code and from the browser.
9358

94-
The assets block supports multiple packages and multiple mappings per package. Below is an example of [filename]`theme.json` mapping multiple packages and assets per package:
59+
The `assets` field syntax looks like this in general: `"asset/glob/pattern:local/target/path"`. The first part defines which files from the npm package should be copied, and it can be a “glob” pattern.
60+
The second part, after the colon, defines where matching resources are copied under the resources folder. You can define multiple asset mappings for a single package annotation.
9561

96-
[source,json]
62+
.Using icons from an npm package
63+
[source,java]
9764
----
98-
{
99-
"assets": {
100-
"@fortawesome/fontawesome-free": {
101-
"svgs/regular/**": "fontawesome/icons",
102-
"webfonts/**": "webfonts"
103-
},
104-
"@fortawesome/free-solid-svg-icons": {
105-
"*.js": "solids"
65+
// tag::annotation[]
66+
@NpmPackage(value = "@fortawesome/fontawesome-free", version = "7.1.0",
67+
assets = {
68+
"svgs/regular/**:fontawesome/icons"
69+
})
70+
// end::annotation[]
71+
72+
@Tag("my-component")
73+
public class MyComponent implements Component {
74+
public MyComponent() {
75+
// tag::usage[]
76+
add(new Image("VAADIN/static/fontawesome/icons/snowflake.svg",
77+
"snowflake"));
78+
// end::usage[]
10679
}
107-
}
10880
}
10981
----
11082

83+
In the previous example, the `fortawesome` file `svgs/regular/snowflake.svg` is copied to `VAADIN/static/fontawesome/icons/snowflake.svg` and can be accessed with that path in the application code and CSS stylesheets.
84+
11185
[discussion-id]`3e46fe3b-00d6-4cf7-908c-342a364210db`

0 commit comments

Comments
 (0)