You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* describing why this is useful with React 17 ([c12ca21](https://github.com/WTW-IM/react-html-element/commit/c12ca21c9d0cff0c8636593d15889c641034205a))
15
+
16
+
### Fix
17
+
18
+
* ensuring we only load the HTML 5 Adapter when we need it ([871755f](https://github.com/WTW-IM/react-html-element/commit/871755fc4b78fc557e77bf3b7bd6048480b3a34d))
* ensuring we push package.json with semantic-release commit ([f0531bc](https://github.com/WTW-IM/react-html-element/commit/f0531bca4998708fe7e37117e28a4e5ab213cf8c))
30
+
* reconfiguring .travis.yml to use on conditions ([f587a7c](https://github.com/WTW-IM/react-html-element/commit/f587a7cc127ed61413c74cbcfc9f26bc7b015755))
31
+
* running release build on react-16 and react-17 branches ([4abdde9](https://github.com/WTW-IM/react-html-element/commit/4abdde98d68464bd32b0f52acebee9701dde6815))
32
+
* setting up release for react-17 ([9d9d4ad](https://github.com/WTW-IM/react-html-element/commit/9d9d4add5f77e7b6089195a357893abc1764a6fe))
33
+
34
+
### Update
35
+
36
+
* adding a base-level render function ([9cb8a6b](https://github.com/WTW-IM/react-html-element/commit/9cb8a6b5a5a66eafa827dfebe2e83577f7a70b68))
37
+
* adding this.shadow for easy style placement ([1a5c275](https://github.com/WTW-IM/react-html-element/commit/1a5c2758acaf3850d67852efe61fa95899f00259))
38
+
* ensuring we can accommodate lower versions of React 17 ([2cac97b](https://github.com/WTW-IM/react-html-element/commit/2cac97b4eea0305b018f87f65eca049d79520878))
39
+
40
+
### Upgrade
41
+
42
+
* updating all dependencies ([5737167](https://github.com/WTW-IM/react-html-element/commit/5737167f47ffb4b3e99fc7f15e4bd1f987663eb7))
The [React documentation around using React in Web Components](https://reactjs.org/docs/web-components.html#using-react-in-your-web-components) presents a case where you can create Web Components using React, but when explored, utilizing React in Web Components presents some significant functionality issues, as detailed in [this issue](https://github.com/facebook/react/issues/9242). Namely, complex React apps rendered in Web Components lose their functionality.
8
+
This package works with React at version 17. For version 16, [see the react-16
9
+
branch of this repo](https://github.com/WTW-IM/react-html-element/tree/react-16).
9
10
10
-
## The Solution
11
+
## What is it?
11
12
12
-
`react-html-element` seamlessly creates the glue needed to utilize React in your Web Components without losing functionality.
13
+
`react-html-element` gives a few quality of life improvements over using React
14
+
in Web Components [as described in the React documentation](https://reactjs.org/docs/web-components.html).
15
+
Read on to find out what you can get by using it!
13
16
14
17
## Installation
15
18
@@ -31,7 +34,7 @@ function Incrementer(): React.ReactElement {
The key pieces of code are `... extends ReactHTMLElement` and `this.mountPoint`.
63
+
The key pieces of code are `... extends ReactHTMLElement` and `this.render`,
64
+
which mounts our app to its designated `mountPoint`, [as described below](#thismountpoint-and-using-custom-templates).
61
65
62
66
> ### Polyfills
63
-
> One thing to remember is that you will need to load [the webcomponentsjs polyfills](https://www.webcomponents.org/polyfills) for `ReactHTMLElement` to work in all browsers. Be sure to include [the ES5 adapter](https://github.com/webcomponents/polyfills/tree/master/packages/webcomponentsjs#custom-elements-es5-adapterjs), as we currently transpile `ReactHTMLElement` down to ES5. The polyfills should be in the `<head>`, and should look something like this:
67
+
>
68
+
> One thing to remember is that you will need to load [the webcomponentsjs polyfills](https://www.webcomponents.org/polyfills) for `ReactHTMLElement` to work in all browsers.
> <!--- We use the closing bracket of this comment to close off the above opening comment, if it gets written -->
74
72
> ```
75
73
>
76
74
> There are many ways to implement these polyfills, and you can explore them in the [webpcomponentsjs README](https://github.com/webcomponents/polyfills/tree/master/packages/webcomponentsjs#how-to-use).
@@ -94,9 +92,7 @@ This will allow us to utilize our Web Component as an element in any HTML:
94
92
<scriptsrc="./path/to/incrementer.js"></script>
95
93
</head>
96
94
<body>
97
-
<h1>
98
-
Behold: An Incrementer
99
-
</h1>
95
+
<h1>Behold: An Incrementer</h1>
100
96
<!-- put your web component in your html -->
101
97
<incrementer></incrementer>
102
98
</body>
@@ -114,7 +110,7 @@ import ReactHTMLElement from 'react-html-element';
Using styled-components with ReactHTMLElement seems tricky, but there's actually a very simple way to implement it: the [`StyleSheetManager`](https://styled-components.com/docs/api#stylesheetmanager). An app rendered with `StyleSheetManager` might look like this:
We use `this.mountPoint.parentNode` for the styles instead of simply using `this.mountPoint` for the case of unmounting. If stylesheets are a child of `this.mountPoint`, ReactDOM will throw an error when you try to unmount. (`unmountComponentAtNode(): The node you're attempting to unmount was rendered by another copy of React.`) This error is a little cryptic, but the bottom line is that ReactDOM expects that everything inside the mounted node was generated by React itself. When we use the same node to place our styles, it breaks that expectation. Using the `parentNode` will cause the styles to be placed within the Shadow DOM, but not inside the same component where our app is mounted.
145
+
`this.shadow` is a getter that will initialize your Web Component, attaching a Shadow
146
+
Root with `{mode: 'open'}`, and setting the Shadow Root's innerHTML to your
147
+
template or `<div></div>`. If this initialization has already occurred, it will
148
+
simply return the previously created Shadow Root. `this.mountPoint` utilizes
149
+
`this.shadow` as part of its work to generate the Shadow Root.
150
+
151
+
We use `this.shadow` for the styles instead of simply using `this.mountPoint` because of unmounting. If stylesheets are a child of `this.mountPoint`, ReactDOM will throw an error when you try to unmount. (`unmountComponentAtNode(): The node you're attempting to unmount was rendered by another copy of React.`) This error is a little cryptic, but the bottom line is that ReactDOM expects that everything inside the mounted node was generated by React itself. When we use the same node to place our styles, it breaks that expectation. Using the `this.shadow` will cause the styles to be placed as a first-child of the Shadow DOM, but not inside the same component where our app is mounted.
150
152
151
-
If you're using a [custom template](#thismountpoint-and-using-custom-templates), you can find a node for your `StyleSheetManager`target by searching through the `shadowRoot` in the same way you might search through `document.body`. Often, simply using `this.mountPoint.parentNode` will still work as expected, even with custom templates.
153
+
If you're using a [custom template](#thismountpoint-and-using-custom-templates), you may need to set the target for your `StyleSheetManager`differently. Often, simply using `this.mountPoint.parentNode` will work as expected, even with custom templates, but this will depend on your template. (You may run into competing styles or have a very unusual use-case where placing a `style` tag as a sibling of your application causes some other issue.)
0 commit comments