@r2wc/react-to-web-component 2.0.1 → 2.0.3
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
|
@@ -91,8 +91,11 @@ npm install @r2wc/react-to-web-component
|
|
|
91
91
|
|
|
92
92
|
## Examples
|
|
93
93
|
|
|
94
|
-
* [Greeting](https://codesandbox.io/s/greeting-
|
|
95
|
-
* [All the Props](https://codesandbox.io/s/all-the-props-
|
|
94
|
+
* [Greeting](https://codesandbox.io/s/greeting-md5oih)
|
|
95
|
+
* [All the Props](https://codesandbox.io/s/all-the-props-n8z5hv)
|
|
96
|
+
* [Header Demo](https://codesandbox.io/s/example-header-blog-7k313l)
|
|
97
|
+
* [MUI Button](https://codesandbox.io/s/example-mui-button-qwidh9)
|
|
98
|
+
* [Checklist Demo](https://codesandbox.io/s/example-checklist-blog-y3nqwx)
|
|
96
99
|
|
|
97
100
|
## Blog Posts
|
|
98
101
|
|
|
@@ -102,7 +105,17 @@ R2WC with Create React App (CRA) [View Post](https://www.bitovi.com/blog/how-to-
|
|
|
102
105
|
|
|
103
106
|
## How it works
|
|
104
107
|
|
|
105
|
-
Check out our [full API documentation](
|
|
108
|
+
Check out our [full API documentation](https://github.com/bitovi/react-to-web-component/blob/main/docs/api.md).
|
|
109
|
+
|
|
110
|
+
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:
|
|
111
|
+
|
|
112
|
+
- re-renders the React component inside the custom element.
|
|
113
|
+
- creates an enumerable getter / setter on the instance to save the set value and avoid hitting the proxy in the future.
|
|
114
|
+
|
|
115
|
+
Also:
|
|
116
|
+
|
|
117
|
+
- Enumerable properties and values on the custom element are used as the `props` passed to the React component.
|
|
118
|
+
- The React component is not rendered until the custom element is inserted into the page.
|
|
106
119
|
|
|
107
120
|
# We want to hear from you.
|
|
108
121
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const c=require("react"),u=require("react-dom"),
|
|
1
|
+
"use strict";const c=require("react"),u=require("react-dom/client"),i=require("@r2wc/core");function s(e,t,r){const n=u.createRoot(e),o=c.createElement(t,r);return n.render(o),{root:n,ReactComponent:t}}function m({root:e,ReactComponent:t},r){const n=c.createElement(t,r);e.render(n)}function l({root:e}){e.unmount()}function a(e,t={}){return i(e,t,{mount:s,update:m,unmount:l})}module.exports=a;
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
function
|
|
5
|
-
const
|
|
6
|
-
return
|
|
7
|
-
|
|
8
|
-
ReactComponent:
|
|
1
|
+
import o from "react";
|
|
2
|
+
import { createRoot as c } from "react-dom/client";
|
|
3
|
+
import m from "@r2wc/core";
|
|
4
|
+
function f(t, e, r) {
|
|
5
|
+
const n = c(t), u = o.createElement(e, r);
|
|
6
|
+
return n.render(u), {
|
|
7
|
+
root: n,
|
|
8
|
+
ReactComponent: e
|
|
9
9
|
};
|
|
10
10
|
}
|
|
11
|
-
function
|
|
12
|
-
const
|
|
13
|
-
|
|
11
|
+
function i({ root: t, ReactComponent: e }, r) {
|
|
12
|
+
const n = o.createElement(e, r);
|
|
13
|
+
t.render(n);
|
|
14
14
|
}
|
|
15
|
-
function
|
|
16
|
-
|
|
15
|
+
function a({ root: t }) {
|
|
16
|
+
t.unmount();
|
|
17
17
|
}
|
|
18
|
-
function
|
|
19
|
-
return
|
|
18
|
+
function s(t, e = {}) {
|
|
19
|
+
return m(t, e, { mount: f, update: i, unmount: a });
|
|
20
20
|
}
|
|
21
21
|
export {
|
|
22
|
-
|
|
22
|
+
s as default
|
|
23
23
|
};
|
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.3",
|
|
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",
|
|
@@ -45,11 +45,12 @@
|
|
|
45
45
|
"@r2wc/core": "^1.0.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@types/react": "^
|
|
49
|
-
"@types/react-dom": "^
|
|
48
|
+
"@types/react": "^18.0.0",
|
|
49
|
+
"@types/react-dom": "^18.0.0",
|
|
50
|
+
"prop-types": "^15.8.1"
|
|
50
51
|
},
|
|
51
52
|
"peerDependencies": {
|
|
52
|
-
"react": "^
|
|
53
|
-
"react-dom": "^
|
|
53
|
+
"react": "^18.0.0",
|
|
54
|
+
"react-dom": "^18.0.0"
|
|
54
55
|
}
|
|
55
56
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
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"
|
|
4
6
|
|
|
5
7
|
import r2wc from "./react-to-web-component"
|
|
6
8
|
|
|
@@ -14,7 +16,7 @@ const Greeting: React.FC<{ name: string }> = ({ name }) => (
|
|
|
14
16
|
<h1>Hello, {name}</h1>
|
|
15
17
|
)
|
|
16
18
|
|
|
17
|
-
describe("react-to-web-component", () => {
|
|
19
|
+
describe("react-to-web-component 1", () => {
|
|
18
20
|
it("basics with react", () => {
|
|
19
21
|
const MyWelcome = r2wc(Greeting)
|
|
20
22
|
customElements.define("my-welcome", MyWelcome)
|
|
@@ -26,6 +28,71 @@ describe("react-to-web-component", () => {
|
|
|
26
28
|
expect(myWelcome.nodeName).toEqual("MY-WELCOME")
|
|
27
29
|
})
|
|
28
30
|
|
|
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
|
+
|
|
29
96
|
it("works with shadow DOM `options.shadow === 'open'`", async () => {
|
|
30
97
|
expect.assertions(5)
|
|
31
98
|
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type { R2WCOptions } from "@r2wc/core"
|
|
2
|
+
import type { Root } from "react-dom/client"
|
|
2
3
|
|
|
3
4
|
import React from "react"
|
|
4
|
-
import
|
|
5
|
+
import { createRoot } from "react-dom/client"
|
|
5
6
|
|
|
6
7
|
import r2wcCore from "@r2wc/core"
|
|
7
8
|
|
|
8
9
|
interface Context<Props extends object> {
|
|
9
|
-
|
|
10
|
+
root: Root
|
|
10
11
|
ReactComponent: React.ComponentType<Props>
|
|
11
12
|
}
|
|
12
13
|
|
|
@@ -15,27 +16,27 @@ function mount<Props extends object>(
|
|
|
15
16
|
ReactComponent: React.ComponentType<Props>,
|
|
16
17
|
props: Props,
|
|
17
18
|
): Context<Props> {
|
|
18
|
-
const
|
|
19
|
+
const root = createRoot(container)
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
const element = React.createElement(ReactComponent, props)
|
|
22
|
+
root.render(element)
|
|
21
23
|
|
|
22
24
|
return {
|
|
23
|
-
|
|
25
|
+
root,
|
|
24
26
|
ReactComponent,
|
|
25
27
|
}
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
function update<Props extends object>(
|
|
29
|
-
{
|
|
31
|
+
{ root, ReactComponent }: Context<Props>,
|
|
30
32
|
props: Props,
|
|
31
33
|
): void {
|
|
32
34
|
const element = React.createElement(ReactComponent, props)
|
|
33
|
-
|
|
34
|
-
ReactDOM.render(element, container)
|
|
35
|
+
root.render(element)
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
function unmount<Props extends object>({
|
|
38
|
-
|
|
38
|
+
function unmount<Props extends object>({ root }: Context<Props>): void {
|
|
39
|
+
root.unmount()
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
export default function r2wc<Props extends object>(
|