Skip to content

Commit c786f79

Browse files
authored
Documentation update for v2 (#99)
* initial commit * more code sandbox examples * misc change * revert prop typings to string
1 parent 5480e38 commit c786f79

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ npm i react-to-webcomponent
9595

9696
Greeting example in a [CodePen](https://codepen.io/bavinedwards/pen/jOveaGm)
9797

98-
Greeting example in [CodeSandbox](https://codesandbox.io/s/sample-greeting-app-m6fc6l)
98+
Greeting example in [CodeSandbox](https://codesandbox.io/s/sample-greeting-app-ts-qwidh9)
99+
100+
Hello, world example (React17) in [CodeSandbox](https://codesandbox.io/s/hello-world-react17-u4l3x1)
101+
102+
Example with all prop types in [CodeSandbox](https://codesandbox.io/p/sandbox/vite-example-with-numerous-types-gjf87o)
103+
104+
R2WC With Vite Header Example in [CodeSandbox](https://codesandbox.io/p/sandbox/header-example-e4x25q)
99105

100106
## External Blog Posts
101107

docs/api.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@
44

55
- `ReactComponent` - A React component that you want to
66
convert to a Web Component.
7-
- `React` - A version of React (or [preact-compat](https://preactjs.com/guide/v10/switching-to-preact)) the
8-
component works with.
9-
- `ReactDOM` - A version of ReactDOM (or preact-compat) that the component works with.
10-
- `options` - An optional set of parameters.
7+
- `options` - An set of parameters.
118

129
- `options.shadow` - Use shadow DOM rather than light DOM.
1310
- `options.props` - Array of camelCasedProps to watch as String values or { [camelCasedProps]: String | Number | Boolean | Function | Object | Array | "ref" }
1411

1512
- When specifying Array or Object as the type, the string passed into the attribute must pass `JSON.parse()` requirements.
1613
- When specifying Boolean as the type, "true", "1", "yes", "TRUE", and "t" are mapped to `true`. All strings NOT begining with t, T, 1, y, or Y will be `false`.
1714
- When specifying Function as the type, the string passed into the attribute must be the name of a function on `window` (or `global`). The `this` context of the function will be the instance of the WebComponent / HTMLElement when called.
15+
- If PropTypes are defined on the React component, the `options.props` will be ignored and the PropTypes will be used instead.
16+
However, we strongly recommend using `options.props` instead of PropTypes as it is usually not a good idea to use PropTypes in production.
17+
- If `options.props` is an array of string (prop names), the type of those props will be `String`.
1818

1919
A new class inheriting from `HTMLElement` is
20-
returned. This class can be directly passed to `customElements.define` as follows:
20+
returned. This class is of type CustomElementConstructor can be directly passed to `customElements.define` as follows:
2121

2222
```js
2323
customElements.define(
2424
"web-greeting",
25-
reactToWebComponent(Greeting, React, ReactDOM),
25+
reactToWebComponent(Greeting),
2626
)
2727
```
2828

2929
Or the class can be defined and used later:
3030

3131
```js
32-
const WebGreeting = reactToWebComponent(Greeting, React, ReactDOM)
32+
const WebGreeting = reactToWebComponent(Greeting)
3333

3434
customElements.define("web-greeting", WebGreeting)
3535

@@ -40,7 +40,7 @@ document.body.appendChild(myGreeting)
4040
Or the class can be extended:
4141

4242
```js
43-
class WebGreeting extends reactToWebComponent(Greeting, React, ReactDOM) {
43+
class WebGreeting extends reactToWebComponent(Greeting) {
4444
disconnectedCallback() {
4545
super.disconnectedCallback()
4646
// special stuff
@@ -52,7 +52,7 @@ customElements.define("web-greeting", WebGreeting)
5252
Components can also be implemented using [shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM) with either `open` or `closed` mode.
5353

5454
```js
55-
const WebGreeting = reactToWebComponent(Greeting, React, ReactDOM, {
55+
const WebGreeting = reactToWebComponent(Greeting, {
5656
shadow: "open",
5757
})
5858

@@ -76,7 +76,7 @@ Greeting.propTypes = {
7676

7777
customElements.define(
7878
"my-dashed-style-greeting",
79-
reactToWebComponent(Greeting, React, ReactDOM, {}),
79+
reactToWebComponent(Greeting, {}),
8080
)
8181

8282
document.body.innerHTML =
@@ -94,7 +94,7 @@ function Greeting({ camelCaseName }) {
9494

9595
customElements.define(
9696
"my-dashed-style-greeting",
97-
reactToWebComponent(Greeting, React, ReactDOM, {
97+
reactToWebComponent(Greeting, {
9898
props: ["camelCaseName"],
9999
}),
100100
)
@@ -118,12 +118,12 @@ If `options.props` is an object, the keys are the camelCased React props and the
118118
```js
119119
function AttrPropTypeCasting(props) {
120120
console.log(props) // Note
121-
return <h1>Hello, {props.camelCaseName}</h1>
121+
return <h1>Hello, {props.stringProp}</h1>
122122
}
123123

124124
customElements.define(
125125
"attr-prop-type-casting",
126-
reactToWebComponent(AttrPropTypeCasting, React, ReactDOM, {
126+
reactToWebComponent(AttrPropTypeCasting, {
127127
props: {
128128
stringProp: "string",
129129
numProp: "number",
@@ -177,7 +177,7 @@ function ThemeSelect({ handleClick }) {
177177
)
178178
}
179179

180-
const WebThemeSelect = reactToWebComponent(ThemeSelect, React, ReactDOM, {
180+
const WebThemeSelect = reactToWebComponent(ThemeSelect, {
181181
props: {
182182
handleClick: "function",
183183
},
@@ -238,7 +238,7 @@ const ComRef = React.forwardRef(function ComRef(props, ref) {
238238
Then `React.createRef()` will automatically happen behind the scenes then attach the reference to the webcomponent instance if it has the corresponding attribute.
239239

240240
```js
241-
class WebComRef extends reactToWebComponent(ComRef, React, ReactDOM, {
241+
class WebComRef extends reactToWebComponent(ComRef, {
242242
props: {
243243
ref: "ref",
244244
h1Ref: "ref",

0 commit comments

Comments
 (0)