@@ -18,41 +18,100 @@ Find information on how to perform common tasks in [this guide](https://github.c
18
18
19
19
## Recreating this project from scratch
20
20
21
- Execute the following commands:
22
- ``` bash
21
+ ### Initialize using Create React App
22
+
23
+ ``` sh
23
24
$ yarn global add create-react-app
24
25
25
26
$ create-react-app hello-react
26
27
$ cd hello-react
27
28
28
29
$ yarn install
30
+ ```
31
+
32
+ ### Upgrade to latest react-scripts
33
+
34
+ This is needed to get Babel 7 and compile Vaadin components properly.
35
+
36
+ ``` sh
37
+ $ yarn upgrade react-scripts@next
38
+ ```
39
+
40
+ Note: Vaadin components support modern browsers and IE11, so the ` browserslist ` section
41
+ in ` package.json ` , which is added when upgrading, should be updated to look like this:
42
+
43
+ ``` js
44
+ " browserslist" : [
45
+ " last 2 versions" ,
46
+ " ie 11"
47
+ ]
48
+ ```
49
+
50
+ ### Install dependencies
29
51
30
- $ yarn add @polymer/polymer
52
+ ``` sh
31
53
$ yarn add @vaadin/vaadin-button
32
54
$ yarn add @vaadin/vaadin-text-field
33
55
$ yarn add @webcomponents/webcomponentsjs
34
56
```
35
57
36
- Open ` src/index.js ` .
58
+ ### Add Web Components polyfill
59
+
60
+ 1 . Install the utility to copy polyfills:
61
+
62
+ ``` sh
63
+ $ yarn add vendor-copy
64
+ ```
65
+
66
+ 2 . Update ` scripts ` section in ` package.json ` and add the line:
67
+
68
+ ``` js
69
+ " postinstall" : " vendor-copy"
70
+ ```
71
+
72
+ 3 . Add the following section to ` package.json ` :
73
+
74
+ ``` js
75
+ " vendorCopy" : [
76
+ {
77
+ " from" : " node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js" ,
78
+ " to" : " public/vendor/custom-elements-es5-adapter.js"
79
+ },
80
+ {
81
+ " from" : " node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js" ,
82
+ " to" : " public/vendor/webcomponents-bundle.js"
83
+ }
84
+ ],
85
+ ```
86
+
87
+ 4 . Open ` public/index.html ` and add the following lines:
88
+ ``` html
89
+ <script src =" %PUBLIC_URL%/vendor/webcomponents-bundle.js" ></script >
90
+ <script >if (! window .customElements ) { document .write (' <!--' ); } </script >
91
+ <script src =" %PUBLIC_URL%/vendor/custom-elements-es5-adapter.js" ></script >
92
+ <!-- ! DO NOT REMOVE THIS COMMENT, WE NEED ITS CLOSING MARKER -->
93
+ ```
37
94
38
- In the ` import ` section before the app importing, add :
95
+ 5 . Make sure the scripts are copied :
39
96
40
- ``` typescript
41
- import ' @webcomponents/webcomponentsjs/bundles/webcomponents-sd-ce.js ' ;
97
+ ``` sh
98
+ $ yarn run postinstall
42
99
```
43
100
44
- Open ` src/App.js ` .
101
+ ### Making Vaadin components work
102
+
103
+ Open ` src/App.js ` and do the following modifications.
45
104
46
- In the ` import ` section, add :
105
+ 1 . Import Vaadin components :
47
106
48
- ``` typescript
107
+ ``` js
49
108
import ' @vaadin/vaadin-button/vaadin-button.js' ;
50
109
import ' @vaadin/vaadin-text-field/vaadin-text-field.js' ;
51
110
```
52
111
53
- Define a constructor with a simple property :
112
+ 2 . Define a constructor for the App component :
54
113
55
- ``` javascript
114
+ ``` js
56
115
constructor (props ) {
57
116
super (props);
58
117
this .state = {greeting: " React App" };
@@ -61,22 +120,24 @@ constructor(props) {
61
120
}
62
121
```
63
122
64
- Replace all the HTML in the ` return ` of ` render ` method with :
123
+ 3 . Update the ` render ` method of the App component to return the HTML :
65
124
66
125
``` html
67
- <div >
126
+ <div className = " App " >
68
127
<vaadin-text-field ref ={this.textField} placeholder =" Type Something" ></vaadin-text-field >
69
128
<vaadin-button onClick ={this.clicked} >Click Me!</vaadin-button >
70
129
<h2 >Hello {this.state.greeting}!</h2 >
71
130
</div >
72
131
```
73
132
74
- Define the click event
133
+ 4 . Define the click event handler
75
134
76
- ``` javascript
135
+ ``` js
77
136
clicked () {
78
137
this .setState ({greeting: this .textField .current .value })
79
138
}
80
139
```
81
140
141
+ ### Starting the application
142
+
82
143
Run the app with ` yarn start `
0 commit comments