You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+12-18Lines changed: 12 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
# React to Web Component
2
2
3
-
`react-to-webcomponent` converts [React](https://reactjs.org/) components to [custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements)! It lets you share React components as native elements that **don't** require mounted being through React. The custom element acts as a wrapper for the underlying React component. Use these custom elements with any project that uses HTML even in any framework (vue, svelte, angular, ember, canjs) the same way you would use standard HTML elements.
3
+
`@r2wc/react-to-web-component` converts [React](https://reactjs.org/) components to [custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements)! It lets you share React components as native elements that **don't** require mounted being through React. The custom element acts as a wrapper for the underlying React component. Use these custom elements with any project that uses HTML even in any framework (vue, svelte, angular, ember, canjs) the same way you would use standard HTML elements.
4
4
5
-
`react-to-webcomponent`:
5
+
`@r2wc/react-to-web-component`:
6
6
7
7
- Works in all modern browsers. (Edge needs a [customElements polyfill](https://github.com/webcomponents/polyfills/tree/master/packages/custom-elements)).
8
8
- Is `1.11KB` minified and gzipped.
@@ -16,8 +16,6 @@ This project is supported by [Bitovi, a React consultancy](https://www.bitovi.co
16
16
17
17
Or, you can hire us for training, consulting, or development. [Set up a free consultation.](https://www.bitovi.com/frontend-javascript-consulting/react-consulting)
18
18
19
-
> This is the documentation for the upcoming version 2 of react-to-webcomponent. It will have a very similar but backwards incompatible API. For production deployments, please see the [document for version 1](https://github.com/bitovi/react-to-webcomponent/tree/main).
20
-
21
19
## Basic Use
22
20
23
21
For basic usage, we will use this simple React component:
@@ -28,12 +26,12 @@ const Greeting = () => {
28
26
}
29
27
```
30
28
31
-
With our React component complete, all we have to do is call `reactToWebComponent` and [customElements.define](https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define) to create and define our custom element:
29
+
With our React component complete, all we have to do is call `r2wc` and [customElements.define](https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define) to create and define our custom element:
@@ -48,27 +46,27 @@ Now we can use `<web-greeting>` like any other HTML element!
48
46
</body>
49
47
```
50
48
51
-
Note that by using React 18, `reactToWebComponent` will use the new root API. If your application needs the legacy API, please use React 17
49
+
Note that by using React 18, `r2wc` will use the new root API. If your application needs the legacy API, please use React 17
52
50
53
51
In the above case, the web-greeting custom element is not making use of the `name` property from our `Greeting` component.
54
52
55
53
## Working with Attributes
56
54
57
-
By default, custom elements created by `reactToWebComponent` only pass properties to the underlying React component. To make attributes work, you must specify your component's props.
55
+
By default, custom elements created by `r2wc` only pass properties to the underlying React component. To make attributes work, you must specify your component's props.
58
56
59
57
```js
60
58
constGreeting= ({ name }) => {
61
59
return<h1>Hello, {name}!</h1>
62
60
}
63
61
64
-
constWebGreeting=reactToWebComponent(Greeting, {
62
+
constWebGreeting=r2wc(Greeting, {
65
63
props: {
66
64
name:"string",
67
65
},
68
66
})
69
67
```
70
68
71
-
Now `reactToWebComponent` will know to look for `name` attributes
69
+
Now `r2wc` will know to look for `name` attributes
72
70
as follows:
73
71
74
72
```html
@@ -88,7 +86,7 @@ We also have a [complete example using a third party library](docs/complete-exam
88
86
To install from npm:
89
87
90
88
```
91
-
npm i react-to-webcomponent
89
+
npm install @r2wc/react-to-web-component
92
90
```
93
91
94
92
## External Examples
@@ -109,15 +107,11 @@ R2WC with Vite [View Post](https://www.bitovi.com/blog/react-everywhere-with-vit
109
107
110
108
R2WC with Create React App (CRA) [View Post](https://www.bitovi.com/blog/how-to-create-a-web-component-with-create-react-app)
Check out our [full API documentation](docs/api.md).
112
+
Check out our [full API documentation](../../docs/api.md).
119
113
120
-
`reactToWebComponent` creates a constructor function whose prototype is a [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy). This acts as a trap for any property set on instances of the custom element. When a property is set, the proxy:
114
+
`r2wc` creates a constructor function whose prototype is a [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy). This acts as a trap for any property set on instances of the custom element. When a property is set, the proxy:
121
115
122
116
- re-renders the React component inside the custom element.
123
117
- creates an enumerable getter / setter on the instance
`react-to-webcomponent` converts [React](https://reactjs.org/) components to [custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements)! It lets you share React components as native elements that **don't** require mounted being through React. The custom element acts as a wrapper for the underlying React component. Use these custom elements with any project that uses HTML even in any framework (vue, svelte, angular, ember, canjs) the same way you would use standard HTML elements.
4
+
5
+
`react-to-webcomponent`:
6
+
7
+
- Works in all modern browsers. (Edge needs a [customElements polyfill](https://github.com/webcomponents/polyfills/tree/master/packages/custom-elements)).
8
+
- Is `1.11KB` minified and gzipped.
9
+
10
+
## Need help or have questions?
11
+
12
+
This project is supported by [Bitovi, a React consultancy](https://www.bitovi.com/frontend-javascript-consulting/react-consulting). You can get help or ask questions on our:
Or, you can hire us for training, consulting, or development. [Set up a free consultation.](https://www.bitovi.com/frontend-javascript-consulting/react-consulting)
18
+
19
+
## Basic Use
20
+
21
+
For basic usage, we will use this simple React component:
22
+
23
+
```js
24
+
constGreeting= () => {
25
+
return<h1>Hello, World!</h1>
26
+
}
27
+
```
28
+
29
+
With our React component complete, all we have to do is call `r2wc` and [customElements.define](https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define) to create and define our custom element:
30
+
31
+
```js
32
+
importReactfrom"react"
33
+
import*asReactDOMfrom"react-dom/client"// if using React 18
34
+
// import * as ReactDOM from "react-dom" // if using React 17
Now we can use `<web-greeting>` like any other HTML element!
43
+
44
+
```html
45
+
<body>
46
+
<h1>Greeting Demo</h1>
47
+
48
+
<web-greeting></web-greeting>
49
+
</body>
50
+
```
51
+
52
+
In the above case, the web-greeting custom element is not making use of the `name` property from our `Greeting` component.
53
+
54
+
## Working with Attributes
55
+
56
+
By default, custom elements created by `r2wc` only pass properties to the underlying React component. To make attributes work, you must specify your component's props.
Now `r2wc` will know to look for `name` attributes
71
+
as follows:
72
+
73
+
```html
74
+
<body>
75
+
<h1>Greeting Demo</h1>
76
+
77
+
<web-greetingname="Justin"></web-greeting>
78
+
</body>
79
+
```
80
+
81
+
For projects needing more advanced usage of the web components, see our [programatic usage and declarative demos](docs/programatic-usage.md).
82
+
83
+
We also have a [complete example using a third party library](docs/complete-example.md).
84
+
85
+
## Setup
86
+
87
+
To install from npm:
88
+
89
+
```
90
+
npm i react-to-webcomponent
91
+
```
92
+
93
+
## External Examples
94
+
95
+
Greeting example in a [CodePen](https://codepen.io/bavinedwards/pen/jOveaGm)
96
+
97
+
Greeting example in [CodeSandbox](https://codesandbox.io/s/sample-greeting-app-ts-qwidh9)
98
+
99
+
Hello, world example (React17) in [CodeSandbox](https://codesandbox.io/s/hello-world-react17-u4l3x1)
100
+
101
+
Example with all prop types in [CodeSandbox](https://codesandbox.io/p/sandbox/vite-example-with-numerous-types-gjf87o)
102
+
103
+
R2WC With Vite Header Example in [CodeSandbox](https://codesandbox.io/p/sandbox/header-example-e4x25q)
104
+
105
+
## External Blog Posts
106
+
107
+
R2WC with Vite [View Post](https://www.bitovi.com/blog/react-everywhere-with-vite-and-react-to-webcomponent)
108
+
109
+
R2WC with Create React App (CRA) [View Post](https://www.bitovi.com/blog/how-to-create-a-web-component-with-create-react-app)
110
+
111
+
## How it works
112
+
113
+
Check out our [full API documentation](../../docs/api.md).
114
+
115
+
`r2wc` creates a constructor function whose prototype is a [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy). This acts as a trap for any property set on instances of the custom element. When a property is set, the proxy:
116
+
117
+
- re-renders the React component inside the custom element.
118
+
- creates an enumerable getter / setter on the instance
119
+
to save the set value and avoid hitting the proxy in the future.
120
+
121
+
Also:
122
+
123
+
- Enumerable properties and values on the custom element are used as the `props` passed to the React component.
124
+
- The React component is not rendered until the custom element is inserted into the page.
125
+
126
+
# We want to hear from you.
127
+
128
+
Come chat with us about open source in our Bitovi community [Discord](https://discord.gg/J7ejFsZnJ4).
129
+
130
+
See what we're up to by following us on [Twitter](https://twitter.com/bitovi).
`@r2wc/react-to-web-component` converts [React](https://reactjs.org/) components to [custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements)! It lets you share React components as native elements that **don't** require mounted being through React. The custom element acts as a wrapper for the underlying React component. Use these custom elements with any project that uses HTML even in any framework (vue, svelte, angular, ember, canjs) the same way you would use standard HTML elements.
4
+
5
+
`@r2wc/react-to-web-component`:
6
+
7
+
- Works in all modern browsers. (Edge needs a [customElements polyfill](https://github.com/webcomponents/polyfills/tree/master/packages/custom-elements)).
8
+
- Is `1.11KB` minified and gzipped.
9
+
10
+
## Need help or have questions?
11
+
12
+
This project is supported by [Bitovi, a React consultancy](https://www.bitovi.com/frontend-javascript-consulting/react-consulting). You can get help or ask questions on our:
Or, you can hire us for training, consulting, or development. [Set up a free consultation.](https://www.bitovi.com/frontend-javascript-consulting/react-consulting)
18
+
19
+
## Basic Use
20
+
21
+
For basic usage, we will use this simple React component:
22
+
23
+
```js
24
+
constGreeting= () => {
25
+
return<h1>Hello, World!</h1>
26
+
}
27
+
```
28
+
29
+
With our React component complete, all we have to do is call `r2wc` and [customElements.define](https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define) to create and define our custom element:
Now we can use `<web-greeting>` like any other HTML element!
40
+
41
+
```html
42
+
<body>
43
+
<h1>Greeting Demo</h1>
44
+
45
+
<web-greeting></web-greeting>
46
+
</body>
47
+
```
48
+
49
+
Note that by using React 18, `r2wc` will use the new root API. If your application needs the legacy API, please use React 17
50
+
51
+
In the above case, the web-greeting custom element is not making use of the `name` property from our `Greeting` component.
52
+
53
+
## Working with Attributes
54
+
55
+
By default, custom elements created by `r2wc` only pass properties to the underlying React component. To make attributes work, you must specify your component's props.
56
+
57
+
```js
58
+
constGreeting= ({ name }) => {
59
+
return<h1>Hello, {name}!</h1>
60
+
}
61
+
62
+
constWebGreeting=r2wc(Greeting, {
63
+
props: {
64
+
name:"string",
65
+
},
66
+
})
67
+
```
68
+
69
+
Now `r2wc` will know to look for `name` attributes
70
+
as follows:
71
+
72
+
```html
73
+
<body>
74
+
<h1>Greeting Demo</h1>
75
+
76
+
<web-greetingname="Justin"></web-greeting>
77
+
</body>
78
+
```
79
+
80
+
For projects needing more advanced usage of the web components, see our [programatic usage and declarative demos](docs/programatic-usage.md).
81
+
82
+
We also have a [complete example using a third party library](docs/complete-example.md).
83
+
84
+
## Setup
85
+
86
+
To install from npm:
87
+
88
+
```
89
+
npm install @r2wc/react-to-web-component
90
+
```
91
+
92
+
## External Examples
93
+
94
+
Greeting example in a [CodePen](https://codepen.io/bavinedwards/pen/jOveaGm)
95
+
96
+
Greeting example in [CodeSandbox](https://codesandbox.io/s/sample-greeting-app-ts-qwidh9)
97
+
98
+
Hello, world example (React17) in [CodeSandbox](https://codesandbox.io/s/hello-world-react17-u4l3x1)
99
+
100
+
Example with all prop types in [CodeSandbox](https://codesandbox.io/p/sandbox/vite-example-with-numerous-types-gjf87o)
101
+
102
+
R2WC With Vite Header Example in [CodeSandbox](https://codesandbox.io/p/sandbox/header-example-e4x25q)
103
+
104
+
## External Blog Posts
105
+
106
+
R2WC with Vite [View Post](https://www.bitovi.com/blog/react-everywhere-with-vite-and-react-to-webcomponent)
107
+
108
+
R2WC with Create React App (CRA) [View Post](https://www.bitovi.com/blog/how-to-create-a-web-component-with-create-react-app)
109
+
110
+
## How it works
111
+
112
+
Check out our [full API documentation](../../docs/api.md).
113
+
114
+
`r2wc` creates a constructor function whose prototype is a [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy). This acts as a trap for any property set on instances of the custom element. When a property is set, the proxy:
115
+
116
+
- re-renders the React component inside the custom element.
117
+
- creates an enumerable getter / setter on the instance
118
+
to save the set value and avoid hitting the proxy in the future.
119
+
120
+
Also:
121
+
122
+
- Enumerable properties and values on the custom element are used as the `props` passed to the React component.
123
+
- The React component is not rendered until the custom element is inserted into the page.
124
+
125
+
# We want to hear from you.
126
+
127
+
Come chat with us about open source in our Bitovi community [Discord](https://discord.gg/J7ejFsZnJ4).
128
+
129
+
See what we're up to by following us on [Twitter](https://twitter.com/bitovi).
0 commit comments