@r2wc/react-to-web-component 1.0.0-alpha.4 → 2.0.0-alpha.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
|
@@ -91,15 +91,13 @@ npm install @r2wc/react-to-web-component
|
|
|
91
91
|
|
|
92
92
|
## External Examples
|
|
93
93
|
|
|
94
|
-
Greeting example in a [CodePen](https://codepen.io/bavinedwards/pen/jOveaGm)
|
|
95
|
-
|
|
96
94
|
Greeting example in [CodeSandbox](https://codesandbox.io/s/sample-greeting-app-ts-qwidh9)
|
|
97
95
|
|
|
98
96
|
Hello, world example (React17) in [CodeSandbox](https://codesandbox.io/s/hello-world-react17-u4l3x1)
|
|
99
97
|
|
|
100
98
|
Example with all prop types in [CodeSandbox](https://codesandbox.io/p/sandbox/vite-example-with-numerous-types-gjf87o)
|
|
101
99
|
|
|
102
|
-
R2WC With Vite Header Example in [CodeSandbox](https://codesandbox.io/p/sandbox/header-example-
|
|
100
|
+
R2WC With Vite Header Example in [CodeSandbox](https://codesandbox.io/p/sandbox/r2wc-header-example-vqzfgo)
|
|
103
101
|
|
|
104
102
|
## External Blog Posts
|
|
105
103
|
|
|
@@ -111,7 +109,7 @@ R2WC with Create React App (CRA) [View Post](https://www.bitovi.com/blog/how-to-
|
|
|
111
109
|
|
|
112
110
|
Check out our [full API documentation](../../docs/api.md).
|
|
113
111
|
|
|
114
|
-
`r2wc` creates a
|
|
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:
|
|
115
113
|
|
|
116
114
|
- re-renders the React component inside the custom element.
|
|
117
115
|
- creates an enumerable getter / setter on the instance
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const
|
|
1
|
+
"use strict";const i=require("react"),a=require("react-dom"),E=require("@r2wc/core");var u,r=a;if(process.env.NODE_ENV==="production")u=r.createRoot,r.hydrateRoot;else{var c=r.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;u=function(e,t){c.usingClientEntryPoint=!0;try{return r.createRoot(e,t)}finally{c.usingClientEntryPoint=!1}}}function _(e,t,o){const n=u(e),s=i.createElement(t,o);return n.render(s),{root:n,ReactComponent:t}}function l({root:e,ReactComponent:t},o){const n=i.createElement(t,o);e.render(n)}function f({root:e}){e.unmount()}function R(e,t={}){return E(e,t,{mount:_,update:l,unmount:f})}module.exports=R;
|
|
@@ -1,22 +1,36 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import i from "react";
|
|
2
|
+
import m from "react-dom";
|
|
3
|
+
import f from "@r2wc/core";
|
|
4
|
+
var u, n = m;
|
|
5
|
+
if (process.env.NODE_ENV === "production")
|
|
6
|
+
u = n.createRoot, n.hydrateRoot;
|
|
7
|
+
else {
|
|
8
|
+
var c = n.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
|
|
9
|
+
u = function(t, e) {
|
|
10
|
+
c.usingClientEntryPoint = !0;
|
|
11
|
+
try {
|
|
12
|
+
return n.createRoot(t, e);
|
|
13
|
+
} finally {
|
|
14
|
+
c.usingClientEntryPoint = !1;
|
|
15
|
+
}
|
|
9
16
|
};
|
|
10
17
|
}
|
|
11
|
-
function
|
|
12
|
-
const r =
|
|
13
|
-
|
|
18
|
+
function E(t, e, o) {
|
|
19
|
+
const r = u(t), a = i.createElement(e, o);
|
|
20
|
+
return r.render(a), {
|
|
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);
|
|
14
28
|
}
|
|
15
|
-
function
|
|
16
|
-
|
|
29
|
+
function l({ root: t }) {
|
|
30
|
+
t.unmount();
|
|
17
31
|
}
|
|
18
|
-
function p(
|
|
19
|
-
return
|
|
32
|
+
function p(t, e = {}) {
|
|
33
|
+
return f(t, e, { mount: E, update: _, unmount: l });
|
|
20
34
|
}
|
|
21
35
|
export {
|
|
22
36
|
p as default
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@r2wc/react-to-web-component",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-alpha.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",
|
|
@@ -45,11 +45,12 @@
|
|
|
45
45
|
"@r2wc/core": "^1.0.0-alpha.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>(
|