Skip to content

Commit 5d1bc1f

Browse files
author
Christopher J Baker
authored
misc cleanup (#146)
* cleanup TypeScript * Update READMEs
1 parent 75f2adc commit 5d1bc1f

17 files changed

+123
-101
lines changed

README.md

+19-38
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22

33
`@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.
44

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+
57
`@r2wc/react-to-web-component`:
68

79
- 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.
10+
- Is `1.26KB` minified and gzipped.
11+
12+
## Setup
13+
14+
To install from npm:
15+
16+
```
17+
npm install @r2wc/react-to-web-component
18+
```
919

1020
## Need help or have questions?
1121

@@ -46,8 +56,6 @@ Now we can use `<web-greeting>` like any other HTML element!
4656
</body>
4757
```
4858

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-
5159
In the above case, the web-greeting custom element is not making use of the `name` property from our `Greeting` component.
5260

5361
## Working with Attributes
@@ -81,25 +89,15 @@ For projects needing more advanced usage of the web components, see our [program
8189

8290
We also have a [complete example using a third party library](docs/complete-example.md).
8391

84-
## Setup
85-
86-
To install from npm:
92+
## Examples
8793

88-
```
89-
npm install @r2wc/react-to-web-component
90-
```
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.
9199

92-
## External Examples
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)
99-
100-
R2WC With Vite Header Example in [CodeSandbox](https://codesandbox.io/p/sandbox/r2wc-header-example-vqzfgo)
101-
102-
## External Blog Posts
100+
## Blog Posts
103101

104102
R2WC with Vite [View Post](https://www.bitovi.com/blog/react-everywhere-with-vite-and-react-to-webcomponent)
105103

@@ -112,30 +110,13 @@ Check out our [full API documentation](https://github.com/bitovi/react-to-web-co
112110
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:
113111

114112
- 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.
113+
- creates an enumerable getter / setter on the instance to save the set value and avoid hitting the proxy in the future.
117114

118115
Also:
119116

120117
- Enumerable properties and values on the custom element are used as the `props` passed to the React component.
121118
- The React component is not rendered until the custom element is inserted into the page.
122119

123-
# Tests
124-
125-
To run tests, first run:
126-
127-
```
128-
npm run buildtests
129-
```
130-
131-
This copies the root test file into each of the `/tests/react*` versioned folders, modifies the ReactDOM import for older versions, and installs the corresponding version of react in that directory.
132-
133-
Then run:
134-
135-
```
136-
npm run test
137-
```
138-
139120
# We want to hear from you.
140121

141122
Come chat with us about open source in our Bitovi community [Discord](https://discord.gg/J7ejFsZnJ4).

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
2+
"private": true,
3+
"homepage": "https://www.bitovi.com/open-source/react-to-web-component",
24
"namespace": "r2wc",
35
"workspaces": [
46
"packages/*"
57
],
6-
"private": true,
78
"scripts": {
89
"check-packages": "ts-node ./scripts/check-packages"
910
},

packages/core/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# React to Web Component
2+
3+
`@r2wc/core` is the heart of our system that 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+
5+
This project is an internal library that should not need to be used directly outside of renderers designed for this system, such as [@r2wc/react-to-web-component](../react-to-web-component).
6+
7+
## Need help or have questions?
8+
9+
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:
10+
11+
- [Discord Community](https://discord.gg/J7ejFsZnJ4)
12+
- [Twitter](https://twitter.com/bitovi)
13+
14+
Or, you can hire us for training, consulting, or development. [Set up a free consultation.](https://www.bitovi.com/frontend-javascript-consulting/react-consulting)
15+
16+
17+
## How it works
18+
19+
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:
20+
21+
- re-renders the React component inside the custom element.
22+
- creates an enumerable getter / setter on the instance to save the set value and avoid hitting the proxy in the future.
23+
24+
Also:
25+
26+
- Enumerable properties and values on the custom element are used as the `props` passed to the React component.
27+
- The React component is not rendered until the custom element is inserted into the page.
28+
29+
# We want to hear from you.
30+
31+
Come chat with us about open source in our Bitovi community [Discord](https://discord.gg/J7ejFsZnJ4).
32+
33+
See what we're up to by following us on [Twitter](https://twitter.com/bitovi).

packages/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@r2wc/core",
33
"version": "1.0.1",
44
"description": "Convert framework components to native Web Components.",
5-
"homepage": "https://www.bitovi.com/frontend-javascript-consulting/react-consulting",
5+
"homepage": "https://www.bitovi.com/open-source/react-to-web-component",
66
"author": "Bitovi",
77
"license": "MIT",
88
"keywords": [

packages/core/src/core.ts

+16-21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { R2WCType } from "./transforms"
22

33
import transforms from "./transforms"
4+
import { toDashedCase } from "./utils"
45

5-
type PropName<Props> = Extract<keyof Props, string>
6+
type PropName<Props> = Exclude<Extract<keyof Props, string>, "container">
67
type PropNames<Props> = Array<PropName<Props>>
78

89
export interface R2WCOptions<Props> {
@@ -20,6 +21,10 @@ export interface R2WCRenderer<Props, Context> {
2021
unmount: (context: Context) => void
2122
}
2223

24+
export interface R2WCBaseProps {
25+
container?: HTMLElement
26+
}
27+
2328
const renderSymbol = Symbol.for("r2wc.render")
2429
const connectedSymbol = Symbol.for("r2wc.connected")
2530
const contextSymbol = Symbol.for("r2wc.context")
@@ -32,7 +37,7 @@ const propsSymbol = Symbol.for("r2wc.props")
3237
* @param {String?} options.shadow - Shadow DOM mode as either open or closed.
3338
* @param {Object|Array?} options.props - Array of camelCasedProps to watch as Strings or { [camelCasedProp]: "string" | "number" | "boolean" | "function" | "json" }
3439
*/
35-
export default function r2wc<Props, Context>(
40+
export default function r2wc<Props extends R2WCBaseProps, Context>(
3641
ReactComponent: React.ComponentType<Props>,
3742
options: R2WCOptions<Props>,
3843
renderer: R2WCRenderer<Props, Context>,
@@ -43,14 +48,9 @@ export default function r2wc<Props, Context>(
4348
: []
4449
}
4550

46-
const propNames = (
47-
Array.isArray(options.props)
48-
? options.props.slice()
49-
: (Object.keys(options.props) as PropNames<Props>)
50-
).filter((prop) => {
51-
//@ts-ignore
52-
return prop !== "container"
53-
})
51+
const propNames = Array.isArray(options.props)
52+
? options.props.slice()
53+
: (Object.keys(options.props) as PropNames<Props>)
5454

5555
const propTypes = {} as Record<PropName<Props>, R2WCType>
5656
const mapPropAttribute = {} as Record<PropName<Props>, string>
@@ -60,7 +60,7 @@ export default function r2wc<Props, Context>(
6060
? "string"
6161
: options.props[prop]
6262

63-
const attribute = toDashedStyle(prop)
63+
const attribute = toDashedCase(prop)
6464

6565
mapPropAttribute[prop] = attribute
6666
mapAttributeProp[attribute] = prop
@@ -87,7 +87,6 @@ export default function r2wc<Props, Context>(
8787
this.container = this
8888
}
8989

90-
// @ts-ignore: There won't always be a container in the definition
9190
this[propsSymbol].container = this.container
9291

9392
for (const prop of propNames) {
@@ -96,9 +95,9 @@ export default function r2wc<Props, Context>(
9695
const type = propTypes[prop]
9796
const transform = transforms[type]
9897

99-
if (value && transform?.parse) {
98+
if (transform?.parse && value) {
10099
//@ts-ignore
101-
this[propsSymbol][prop] = transform.parse(value, this)
100+
this[propsSymbol][prop] = transform.parse(value, attribute, this)
102101
}
103102
}
104103
}
@@ -126,9 +125,9 @@ export default function r2wc<Props, Context>(
126125
const type = propTypes[prop]
127126
const transform = transforms[type]
128127

129-
if (prop in propTypes && transform?.parse) {
128+
if (prop in propTypes && transform?.parse && value) {
130129
//@ts-ignore
131-
this[propsSymbol][prop] = transform.parse(value, this)
130+
this[propsSymbol][prop] = transform.parse(value, attribute, this)
132131

133132
this[renderSymbol]()
134133
}
@@ -165,7 +164,7 @@ export default function r2wc<Props, Context>(
165164
const transform = transforms[type]
166165
if (transform?.stringify) {
167166
//@ts-ignore
168-
const attributeValue = transform.stringify(value)
167+
const attributeValue = transform.stringify(value, attribute, this)
169168
const oldAttributeValue = this.getAttribute(attribute)
170169

171170
if (oldAttributeValue !== attributeValue) {
@@ -178,7 +177,3 @@ export default function r2wc<Props, Context>(
178177

179178
return ReactWebComponent
180179
}
181-
182-
function toDashedStyle(camelCase = "") {
183-
return camelCase.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase()
184-
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Transform } from "./index"
22

3-
const string: Transform<boolean> = {
3+
const boolean: Transform<boolean> = {
44
stringify: (value) => (value ? "true" : "false"),
55
parse: (value) => /^[ty1-9]/i.test(value),
66
}
77

8-
export default string
8+
export default boolean

packages/core/src/transforms/function.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Transform } from "./index"
22

3-
const string: Transform<(...args: unknown[]) => unknown> = {
3+
const function_: Transform<(...args: unknown[]) => unknown> = {
44
stringify: (value) => value.name,
5-
parse: (value, element) => {
5+
parse: (value, attribute, element) => {
66
const fn = (() => {
77
if (typeof window !== "undefined" && value in window) {
88
// @ts-expect-error
@@ -19,4 +19,4 @@ const string: Transform<(...args: unknown[]) => unknown> = {
1919
},
2020
}
2121

22-
export default string
22+
export default function_

packages/core/src/transforms/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import function_ from "./function"
55
import json from "./json"
66

77
export interface Transform<Type> {
8-
stringify?: (value: Type) => string
9-
parse: (value: string, element: HTMLElement) => Type
8+
stringify?: (value: Type, attribute: string, element: HTMLElement) => string
9+
parse: (value: string, attribute: string, element: HTMLElement) => Type
1010
}
1111

1212
const transforms = {

packages/core/src/transforms/json.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Transform } from "./index"
22

3-
const string: Transform<string> = {
3+
const json: Transform<string> = {
44
stringify: (value) => JSON.stringify(value),
55
parse: (value) => JSON.parse(value),
66
}
77

8-
export default string
8+
export default json
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Transform } from "./index"
22

3-
const string: Transform<number> = {
3+
const number: Transform<number> = {
44
stringify: (value) => `${value}`,
55
parse: (value) => parseFloat(value),
66
}
77

8-
export default string
8+
export default number

packages/core/src/utils.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function toDashedCase(camelCase: string): string {
2+
return camelCase.replace(
3+
/([a-z0-9])([A-Z])/g,
4+
(_, a, b) => `${a}-${b.toLowerCase()}`,
5+
)
6+
}
7+
8+
export function toCamelCase(dashedCase: string): string {
9+
return dashedCase.replace(/[-:]([a-z])/g, (_, b) => `${b.toUpperCase()}`)
10+
}

packages/legacy/README.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
# React to Web Component
22

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+
`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 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.
44

5-
> Note: This is a compatibility wrapper around our new, simpler API. We highly reccomend using the new [@r2wc/react-to-web-component](https://github.com/bitovi/react-to-web-component) package.
5+
> Note: This is a compatibility wrapper around our new, simpler API. We highly reccomend using the new [@r2wc/react-to-web-component](https://www.npmjs.com/package/@r2wc/react-to-web-component) package.
66
77
`react-to-webcomponent`:
88

99
- Works in all modern browsers. (Edge needs a [customElements polyfill](https://github.com/webcomponents/polyfills/tree/master/packages/custom-elements)).
1010
- Is `1.37KB` minified and gzipped.
1111

12+
## Setup
13+
14+
To install from npm:
15+
16+
```
17+
npm install react-to-webcomponent
18+
```
19+
1220
## Need help or have questions?
1321

1422
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:
@@ -85,18 +93,11 @@ For projects needing more advanced usage of the web components, see our [program
8593

8694
We also have a [complete example using a third party library](docs/complete-example.md).
8795

88-
## Setup
89-
90-
To install from npm:
91-
92-
```
93-
npm install react-to-webcomponent
94-
```
9596

9697
## Examples
9798

98-
* [Greeting](https://codesandbox.io/s/greeting-legacy-8oopz3)
99-
* [All the Props](https://codesandbox.io/s/all-the-props-legacy-0zh6iv)
99+
* [Hello World](https://codesandbox.io/s/hello-world-legacy-8oopz3) - The quintessential software demo!
100+
* [All the Props](https://codesandbox.io/s/all-the-props-legacy-0zh6iv) - A demo of all the prop transform types that R2WC supports.
100101

101102
## Blog Posts
102103

packages/legacy/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "react-to-webcomponent",
33
"version": "2.0.0",
44
"description": "Convert React components to native Web Components.",
5-
"homepage": "https://www.bitovi.com/frontend-javascript-consulting/react-consulting",
5+
"homepage": "https://www.bitovi.com/open-source/react-to-web-component",
66
"author": "Bitovi",
77
"license": "MIT",
88
"keywords": [

packages/legacy/src/react-to-webcomponent.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ interface Context<Props> {
4242
* @param {Object|Array?} options.props - Array of camelCasedProps to watch as Strings or { [camelCasedProp]: String | Number | Boolean | Function | Object | Array }
4343
*/
4444
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
45-
export default function r2wc<Props>(
45+
export default function r2wc<Props extends object>(
4646
ReactComponent: React.ComponentType<Props>,
4747
React: ReactType,
4848
ReactDOM: ReactDOMRootType | ReactDOMRenderType,

0 commit comments

Comments
 (0)