@r2wc/react-to-web-component 2.0.2 → 2.0.4
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
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
# React to Web Component
|
|
2
2
|
|
|
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
|
|
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 being mounted 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:
|
|
5
|
+
> Note: The latest version of this package only works with the React 18. If you are using React 16 or 17, please use version 1.
|
|
6
6
|
|
|
7
7
|
`@r2wc/react-to-web-component`:
|
|
8
8
|
|
|
9
9
|
- Works in all modern browsers. (Edge needs a [customElements polyfill](https://github.com/webcomponents/polyfills/tree/master/packages/custom-elements)).
|
|
10
10
|
- Is `1.26KB` minified and gzipped.
|
|
11
11
|
|
|
12
|
+
## Setup
|
|
13
|
+
|
|
14
|
+
To install from npm:
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
npm install @r2wc/react-to-web-component
|
|
18
|
+
```
|
|
19
|
+
|
|
12
20
|
## Need help or have questions?
|
|
13
21
|
|
|
14
22
|
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:
|
|
@@ -81,21 +89,13 @@ For projects needing more advanced usage of the web components, see our [program
|
|
|
81
89
|
|
|
82
90
|
We also have a [complete example using a third party library](docs/complete-example.md).
|
|
83
91
|
|
|
84
|
-
## Setup
|
|
85
|
-
|
|
86
|
-
To install from npm:
|
|
87
|
-
|
|
88
|
-
```
|
|
89
|
-
npm install @r2wc/react-to-web-component
|
|
90
|
-
```
|
|
91
|
-
|
|
92
92
|
## Examples
|
|
93
93
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
94
|
+
- [Hello World](https://codesandbox.io/s/hello-world-md5oih) - The quintessential software demo!
|
|
95
|
+
- [All the Props](https://codesandbox.io/s/all-the-props-n8z5hv) - A demo of all the prop transform types that R2WC supports.
|
|
96
|
+
- [Header Example](https://codesandbox.io/s/example-header-blog-7k313l) - An example reusable Header component.
|
|
97
|
+
- [MUI Button](https://codesandbox.io/s/example-mui-button-qwidh9) - An example application using an MUI button with theme customization.
|
|
98
|
+
- [Checklist Demo](https://codesandbox.io/s/example-checklist-blog-y3nqwx) - An example Checklist application.
|
|
99
99
|
|
|
100
100
|
## Blog Posts
|
|
101
101
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const
|
|
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,3 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
import { default as default_2 } from 'react';
|
|
2
|
+
import { R2WCOptions } from '@r2wc/core';
|
|
3
|
+
|
|
4
|
+
declare function r2wc<Props extends object>(ReactComponent: default_2.ComponentType<Props>, options?: R2WCOptions<Props>): CustomElementConstructor;
|
|
5
|
+
export default r2wc;
|
|
6
|
+
|
|
7
|
+
export { }
|
|
@@ -1,37 +1,23 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
}
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
function E(t, e, o) {
|
|
19
|
-
const r = u(t), a = i.createElement(e, o);
|
|
20
|
-
return r.render(a), {
|
|
21
|
-
root: r,
|
|
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,
|
|
22
8
|
ReactComponent: e
|
|
23
9
|
};
|
|
24
10
|
}
|
|
25
|
-
function
|
|
26
|
-
const
|
|
27
|
-
t.render(
|
|
11
|
+
function i({ root: t, ReactComponent: e }, r) {
|
|
12
|
+
const n = o.createElement(e, r);
|
|
13
|
+
t.render(n);
|
|
28
14
|
}
|
|
29
|
-
function
|
|
15
|
+
function a({ root: t }) {
|
|
30
16
|
t.unmount();
|
|
31
17
|
}
|
|
32
|
-
function
|
|
33
|
-
return
|
|
18
|
+
function s(t, e = {}) {
|
|
19
|
+
return m(t, e, { mount: f, update: i, unmount: a });
|
|
34
20
|
}
|
|
35
21
|
export {
|
|
36
|
-
|
|
22
|
+
s as default
|
|
37
23
|
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@r2wc/react-to-web-component",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"description": "Convert React components to native Web Components.",
|
|
5
|
-
"homepage": "https://www.bitovi.com/
|
|
5
|
+
"homepage": "https://www.bitovi.com/open-source/react-to-web-component",
|
|
6
6
|
"author": "Bitovi",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"keywords": [
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
],
|
|
32
32
|
"scripts": {
|
|
33
33
|
"typecheck": "tsc --noEmit",
|
|
34
|
-
"eslint": "eslint
|
|
35
|
-
"prettier": "prettier --check
|
|
34
|
+
"eslint": "eslint .",
|
|
35
|
+
"prettier": "prettier --check .",
|
|
36
36
|
"depcheck": "depcheck .",
|
|
37
37
|
"dev": "vite",
|
|
38
38
|
"test": "vitest",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"prop-types": "^15.8.1"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"react": "^18.0.0",
|
|
54
|
-
"react-dom": "^18.0.0"
|
|
53
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
54
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
55
55
|
}
|
|
56
56
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { describe, it, expect, assert } from "vitest"
|
|
3
2
|
import matchers from "@testing-library/jest-dom/matchers"
|
|
4
|
-
import React from "react"
|
|
5
3
|
import PropTypes from "prop-types"
|
|
4
|
+
import React from "react"
|
|
5
|
+
import { describe, it, expect, assert } from "vitest"
|
|
6
6
|
|
|
7
7
|
import r2wc from "./react-to-web-component"
|
|
8
8
|
|
|
@@ -325,4 +325,42 @@ describe("react-to-web-component 1", () => {
|
|
|
325
325
|
}, 0)
|
|
326
326
|
})
|
|
327
327
|
})
|
|
328
|
+
|
|
329
|
+
it("Props typed as Function are dispatching events when events are enables via options", async () => {
|
|
330
|
+
expect.assertions(2)
|
|
331
|
+
|
|
332
|
+
function ThemeSelect({ onSelect }: { onSelect: (arg: string) => void }) {
|
|
333
|
+
return (
|
|
334
|
+
<div>
|
|
335
|
+
<button onClick={() => onSelect("V")}>V</button>
|
|
336
|
+
<button onClick={() => onSelect("Johnny")}>Johnny</button>
|
|
337
|
+
<button onClick={() => onSelect("Jane")}>Jane</button>
|
|
338
|
+
</div>
|
|
339
|
+
)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const WebThemeSelect = r2wc(ThemeSelect, {
|
|
343
|
+
events: { onSelect: { bubbles: true } },
|
|
344
|
+
})
|
|
345
|
+
customElements.define("theme-select-events", WebThemeSelect)
|
|
346
|
+
document.body.innerHTML = "<theme-select-events></theme-select-events>"
|
|
347
|
+
|
|
348
|
+
await new Promise((resolve, reject) => {
|
|
349
|
+
const failUnlessCleared = setTimeout(() => {
|
|
350
|
+
reject("event listener was not called to clear the failure timeout")
|
|
351
|
+
}, 1000)
|
|
352
|
+
|
|
353
|
+
const element = document.querySelector("theme-select-events")
|
|
354
|
+
element?.addEventListener("select", (event) => {
|
|
355
|
+
clearTimeout(failUnlessCleared)
|
|
356
|
+
expect((event as CustomEvent).detail).toEqual("Jane")
|
|
357
|
+
expect(event.target).toEqual(element)
|
|
358
|
+
resolve(true)
|
|
359
|
+
})
|
|
360
|
+
const button = element?.querySelector(
|
|
361
|
+
"button:last-child",
|
|
362
|
+
) as HTMLButtonElement
|
|
363
|
+
button.click()
|
|
364
|
+
})
|
|
365
|
+
})
|
|
328
366
|
})
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import type { R2WCOptions } from "@r2wc/core"
|
|
2
|
-
import type { Root } from "react-dom/client"
|
|
3
|
-
|
|
4
1
|
import React from "react"
|
|
5
|
-
import { createRoot } from "react-dom/client"
|
|
2
|
+
import { Root, createRoot } from "react-dom/client"
|
|
6
3
|
|
|
7
|
-
import r2wcCore from "@r2wc/core"
|
|
4
|
+
import r2wcCore, { R2WCOptions } from "@r2wc/core"
|
|
8
5
|
|
|
9
6
|
interface Context<Props extends object> {
|
|
10
7
|
root: Root
|