@r2wc/react-to-web-component 2.0.0-alpha.1 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
|
|
3
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
|
+
> Note: This package only works with the React 18. If you are using React 16 or 17, use version 1.0.
|
|
6
|
+
|
|
5
7
|
`@r2wc/react-to-web-component`:
|
|
6
8
|
|
|
7
9
|
- Works in all modern browsers. (Edge needs a [customElements polyfill](https://github.com/webcomponents/polyfills/tree/master/packages/custom-elements)).
|
|
8
|
-
- Is `1.
|
|
10
|
+
- Is `1.26KB` minified and gzipped.
|
|
9
11
|
|
|
10
12
|
## Need help or have questions?
|
|
11
13
|
|
|
@@ -46,8 +48,6 @@ Now we can use `<web-greeting>` like any other HTML element!
|
|
|
46
48
|
</body>
|
|
47
49
|
```
|
|
48
50
|
|
|
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
51
|
In the above case, the web-greeting custom element is not making use of the `name` property from our `Greeting` component.
|
|
52
52
|
|
|
53
53
|
## Working with Attributes
|
|
@@ -89,17 +89,12 @@ To install from npm:
|
|
|
89
89
|
npm install @r2wc/react-to-web-component
|
|
90
90
|
```
|
|
91
91
|
|
|
92
|
-
##
|
|
93
|
-
|
|
94
|
-
Greeting example in [CodeSandbox](https://codesandbox.io/s/sample-greeting-app-ts-qwidh9)
|
|
95
|
-
|
|
96
|
-
Hello, world example (React17) in [CodeSandbox](https://codesandbox.io/s/hello-world-react17-u4l3x1)
|
|
97
|
-
|
|
98
|
-
Example with all prop types in [CodeSandbox](https://codesandbox.io/p/sandbox/vite-example-with-numerous-types-gjf87o)
|
|
92
|
+
## Examples
|
|
99
93
|
|
|
100
|
-
|
|
94
|
+
* [Greeting](https://codesandbox.io/s/greeting-react-17-u4l3x1)
|
|
95
|
+
* [All the Props](https://codesandbox.io/s/all-the-props-react-17-x09rxo)
|
|
101
96
|
|
|
102
|
-
##
|
|
97
|
+
## Blog Posts
|
|
103
98
|
|
|
104
99
|
R2WC with Vite [View Post](https://www.bitovi.com/blog/react-everywhere-with-vite-and-react-to-webcomponent)
|
|
105
100
|
|
|
@@ -109,17 +104,6 @@ R2WC with Create React App (CRA) [View Post](https://www.bitovi.com/blog/how-to-
|
|
|
109
104
|
|
|
110
105
|
Check out our [full API documentation](../../docs/api.md).
|
|
111
106
|
|
|
112
|
-
Under the hood, `r2wc` creates a `CustomElementConstructor` with custom getters/setters and life cycle methods that keep track of the props that you have defined. When a property is set, its custom setter:
|
|
113
|
-
|
|
114
|
-
- re-renders the React component inside the custom element.
|
|
115
|
-
- creates an enumerable getter / setter on the instance
|
|
116
|
-
to save the set value and avoid hitting the proxy in the future.
|
|
117
|
-
|
|
118
|
-
Also:
|
|
119
|
-
|
|
120
|
-
- Enumerable properties and values on the custom element are used as the `props` passed to the React component.
|
|
121
|
-
- The React component is not rendered until the custom element is inserted into the page.
|
|
122
|
-
|
|
123
107
|
# We want to hear from you.
|
|
124
108
|
|
|
125
109
|
Come chat with us about open source in our Bitovi community [Discord](https://discord.gg/J7ejFsZnJ4).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const
|
|
1
|
+
"use strict";const c=require("react"),u=require("react-dom"),o=require("@r2wc/core");function s(e,t,n){const r=c.createElement(t,n);return u.render(r,e),{container:e,ReactComponent:t}}function i({container:e,ReactComponent:t},n){const r=c.createElement(t,n);u.render(r,e)}function m({container:e}){u.unmountComponentAtNode(e)}function d(e,t={}){return o(e,t,{mount:s,update:i,unmount:m})}module.exports=d;
|
|
@@ -1,36 +1,22 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
u = function(t, e) {
|
|
10
|
-
c.usingClientEntryPoint = !0;
|
|
11
|
-
try {
|
|
12
|
-
return n.createRoot(t, e);
|
|
13
|
-
} finally {
|
|
14
|
-
c.usingClientEntryPoint = !1;
|
|
15
|
-
}
|
|
1
|
+
import m from "react";
|
|
2
|
+
import o from "react-dom";
|
|
3
|
+
import u from "@r2wc/core";
|
|
4
|
+
function c(e, t, n) {
|
|
5
|
+
const r = m.createElement(t, n);
|
|
6
|
+
return o.render(r, e), {
|
|
7
|
+
container: e,
|
|
8
|
+
ReactComponent: t
|
|
16
9
|
};
|
|
17
10
|
}
|
|
18
|
-
function
|
|
19
|
-
const r =
|
|
20
|
-
|
|
21
|
-
root: r,
|
|
22
|
-
ReactComponent: e
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
function _({ root: t, ReactComponent: e }, o) {
|
|
26
|
-
const r = i.createElement(e, o);
|
|
27
|
-
t.render(r);
|
|
11
|
+
function f({ container: e, ReactComponent: t }, n) {
|
|
12
|
+
const r = m.createElement(t, n);
|
|
13
|
+
o.render(r, e);
|
|
28
14
|
}
|
|
29
|
-
function
|
|
30
|
-
|
|
15
|
+
function i({ container: e }) {
|
|
16
|
+
o.unmountComponentAtNode(e);
|
|
31
17
|
}
|
|
32
|
-
function p(
|
|
33
|
-
return
|
|
18
|
+
function p(e, t = {}) {
|
|
19
|
+
return u(e, t, { mount: c, update: f, unmount: i });
|
|
34
20
|
}
|
|
35
21
|
export {
|
|
36
22
|
p as default
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@r2wc/react-to-web-component",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Convert React components to native Web Components.",
|
|
5
5
|
"homepage": "https://www.bitovi.com/frontend-javascript-consulting/react-consulting",
|
|
6
6
|
"author": "Bitovi",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
],
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
14
|
-
"url": "git+https://github.com/bitovi/react-to-
|
|
14
|
+
"url": "git+https://github.com/bitovi/react-to-web-component.git"
|
|
15
15
|
},
|
|
16
16
|
"type": "module",
|
|
17
17
|
"main": "./dist/react-to-web-component.cjs",
|
|
@@ -42,15 +42,14 @@
|
|
|
42
42
|
"build": "vite build"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@r2wc/core": "^1.0.0
|
|
45
|
+
"@r2wc/core": "^1.0.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@types/react": "^
|
|
49
|
-
"@types/react-dom": "^
|
|
50
|
-
"prop-types": "^15.8.1"
|
|
48
|
+
"@types/react": "^17.0.0",
|
|
49
|
+
"@types/react-dom": "^17.0.0"
|
|
51
50
|
},
|
|
52
51
|
"peerDependencies": {
|
|
53
|
-
"react": "^
|
|
54
|
-
"react-dom": "^
|
|
52
|
+
"react": "^16.0.0 || ^17.0.0",
|
|
53
|
+
"react-dom": "^16.0.0 || ^17.0.0"
|
|
55
54
|
}
|
|
56
55
|
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
import { describe, it, expect, assert } from "vitest"
|
|
3
3
|
import matchers from "@testing-library/jest-dom/matchers"
|
|
4
|
-
import React from "react"
|
|
5
|
-
import PropTypes from "prop-types"
|
|
6
4
|
|
|
7
5
|
import r2wc from "./react-to-web-component"
|
|
8
6
|
|
|
@@ -16,7 +14,7 @@ const Greeting: React.FC<{ name: string }> = ({ name }) => (
|
|
|
16
14
|
<h1>Hello, {name}</h1>
|
|
17
15
|
)
|
|
18
16
|
|
|
19
|
-
describe("react-to-web-component
|
|
17
|
+
describe("react-to-web-component", () => {
|
|
20
18
|
it("basics with react", () => {
|
|
21
19
|
const MyWelcome = r2wc(Greeting)
|
|
22
20
|
customElements.define("my-welcome", MyWelcome)
|
|
@@ -28,71 +26,6 @@ describe("react-to-web-component 1", () => {
|
|
|
28
26
|
expect(myWelcome.nodeName).toEqual("MY-WELCOME")
|
|
29
27
|
})
|
|
30
28
|
|
|
31
|
-
it("works with props array", async () => {
|
|
32
|
-
function TestComponent({ name }: { name: string }) {
|
|
33
|
-
return <div>hello, {name}</div>
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const TestElement = r2wc(TestComponent, { props: ["name"] })
|
|
37
|
-
|
|
38
|
-
customElements.define("test-hello", TestElement)
|
|
39
|
-
|
|
40
|
-
const body = document.body
|
|
41
|
-
body.innerHTML = "<test-hello name='Bavin'></test-hello>"
|
|
42
|
-
|
|
43
|
-
await flushPromises()
|
|
44
|
-
|
|
45
|
-
const div = body.querySelector("div")
|
|
46
|
-
expect(div?.textContent).toBe("hello, Bavin")
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it("works with proptypes", async () => {
|
|
50
|
-
function WithProptypes({ name }: { name: string }) {
|
|
51
|
-
return <div>hello, {name}</div>
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
WithProptypes.propTypes = {
|
|
55
|
-
name: PropTypes.string.isRequired,
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const WithPropTypesElement = r2wc(WithProptypes)
|
|
59
|
-
|
|
60
|
-
customElements.define("with-proptypes", WithPropTypesElement)
|
|
61
|
-
|
|
62
|
-
const body = document.body
|
|
63
|
-
body.innerHTML = "<with-proptypes name='Bavin'></with-proptypes>"
|
|
64
|
-
|
|
65
|
-
await flushPromises()
|
|
66
|
-
|
|
67
|
-
const div = body.querySelector("div")
|
|
68
|
-
expect(div?.textContent).toBe("hello, Bavin")
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
it("works with class components", async () => {
|
|
72
|
-
class TestClassComponent extends React.Component<{ name: string }> {
|
|
73
|
-
render() {
|
|
74
|
-
return <div>hello, {this.props.name}</div>
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
class TestClassElement extends r2wc(TestClassComponent, {
|
|
79
|
-
props: ["name"],
|
|
80
|
-
}) {}
|
|
81
|
-
|
|
82
|
-
customElements.define("test-class", TestClassElement)
|
|
83
|
-
|
|
84
|
-
const body = document.body
|
|
85
|
-
body.innerHTML = "<test-class name='Bavin'></test-class>"
|
|
86
|
-
|
|
87
|
-
await flushPromises()
|
|
88
|
-
|
|
89
|
-
const div = body.querySelector("div")
|
|
90
|
-
const testClassEl = body.querySelector("test-class")
|
|
91
|
-
|
|
92
|
-
expect(testClassEl).toBeInstanceOf(TestClassElement)
|
|
93
|
-
expect(div?.textContent).toBe("hello, Bavin")
|
|
94
|
-
})
|
|
95
|
-
|
|
96
29
|
it("works with shadow DOM `options.shadow === 'open'`", async () => {
|
|
97
30
|
expect.assertions(5)
|
|
98
31
|
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import type { R2WCOptions } from "@r2wc/core"
|
|
2
|
-
import type { Root } from "react-dom/client"
|
|
3
2
|
|
|
4
3
|
import React from "react"
|
|
5
|
-
import
|
|
4
|
+
import ReactDOM from "react-dom"
|
|
6
5
|
|
|
7
6
|
import r2wcCore from "@r2wc/core"
|
|
8
7
|
|
|
9
8
|
interface Context<Props extends object> {
|
|
10
|
-
|
|
9
|
+
container: HTMLElement
|
|
11
10
|
ReactComponent: React.ComponentType<Props>
|
|
12
11
|
}
|
|
13
12
|
|
|
@@ -16,27 +15,27 @@ function mount<Props extends object>(
|
|
|
16
15
|
ReactComponent: React.ComponentType<Props>,
|
|
17
16
|
props: Props,
|
|
18
17
|
): Context<Props> {
|
|
19
|
-
const root = createRoot(container)
|
|
20
|
-
|
|
21
18
|
const element = React.createElement(ReactComponent, props)
|
|
22
|
-
|
|
19
|
+
|
|
20
|
+
ReactDOM.render(element, container)
|
|
23
21
|
|
|
24
22
|
return {
|
|
25
|
-
|
|
23
|
+
container,
|
|
26
24
|
ReactComponent,
|
|
27
25
|
}
|
|
28
26
|
}
|
|
29
27
|
|
|
30
28
|
function update<Props extends object>(
|
|
31
|
-
{
|
|
29
|
+
{ container, ReactComponent }: Context<Props>,
|
|
32
30
|
props: Props,
|
|
33
31
|
): void {
|
|
34
32
|
const element = React.createElement(ReactComponent, props)
|
|
35
|
-
|
|
33
|
+
|
|
34
|
+
ReactDOM.render(element, container)
|
|
36
35
|
}
|
|
37
36
|
|
|
38
|
-
function unmount<Props extends object>({
|
|
39
|
-
|
|
37
|
+
function unmount<Props extends object>({ container }: Context<Props>): void {
|
|
38
|
+
ReactDOM.unmountComponentAtNode(container)
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
export default function r2wc<Props extends object>(
|