1- import { version as reactVersion } from 'react' ;
2- import ReactDOM from 'react-dom' ;
3- import type { Root , createRoot as createRootOriginal } from 'react-dom/ client' ;
1+ import type ReactDOM from 'react-dom ' ;
2+ import type { Root } from 'react-dom/client ' ;
3+ import { getCreateRoot } from './ react-dom- client' ;
44
55type Renderable = Parameters < ReactDOM . Renderer > [ 0 ] [ number ] ;
6-
7- const [ , major ] = / ^ ( \d + ) \. \d + \. \d + $ / . exec ( reactVersion ) || [ undefined , '16' ] ;
8- const reactMajor = Number ( major ) ;
9-
10- const isPreEighteen = reactMajor < 18 ;
11- const REACT_DOM_CLIENT_IMPORT = isPreEighteen
12- ? './react-dom-client-polyfill'
13- : 'react-dom/client' ;
6+ type ReactHTMLElementDOMRoot = Pick < Root , 'render' | 'unmount' > ;
7+
8+ const awaitValue = < T > ( awaiter : ( ) => T ) : Promise < T > => new Promise ( ( resolve ) => {
9+ const result = awaiter ( ) ;
10+ if ( result ) {
11+ resolve ( result ) ;
12+ } else {
13+ setTimeout ( ( ) => resolve ( awaitValue ( awaiter ) ) , 100 ) ;
14+ }
15+ } ) ;
1416
1517class ReactHTMLElement extends HTMLElement {
1618 private _initialized ?: boolean ;
1719
1820 private _mountPoint ?: Element ;
1921
20- private _root ?: Root ;
22+ private _root ?: ReactHTMLElementDOMRoot ;
23+
24+ private _awaitingRoot = false ;
2125
2226 private getShadowRoot ( ) : ShadowRoot {
2327 return this . shadowRoot || this . attachShadow ( { mode : 'open' } ) ;
@@ -49,27 +53,21 @@ class ReactHTMLElement extends HTMLElement {
4953 this . _mountPoint = mount ;
5054 }
5155
52- async root ( ) : Promise < Root > {
56+ async root ( ) : Promise < ReactHTMLElementDOMRoot > {
57+ if ( this . _awaitingRoot ) {
58+ await awaitValue ( ( ) => this . _root ) ;
59+ }
5360 if ( this . _root ) return this . _root ;
5461
55- const { createRoot } = ( await import (
56- /* webpackExports: ['createRoot'] */
57- `${ REACT_DOM_CLIENT_IMPORT } `
58- ) ) as {
59- createRoot : typeof createRootOriginal ;
60- } ;
61- this . _root = createRoot ( this . mountPoint ) ;
62+ this . _awaitingRoot = true ;
63+ this . _root = ( await getCreateRoot ( ) ) ( this . mountPoint ) ;
64+ this . _awaitingRoot = false ;
6265 return this . _root ;
6366 }
6467
6568 render ( app : Renderable ) : void {
6669 if ( ! this . isConnected ) return ;
6770
68- if ( isPreEighteen ) {
69- ReactDOM . render ( app , this . mountPoint ) ;
70- return ;
71- }
72-
7371 void this . renderRoot ( app ) ;
7472 }
7573
@@ -81,11 +79,6 @@ class ReactHTMLElement extends HTMLElement {
8179 disconnectedCallback ( ) : void {
8280 if ( ! this . _mountPoint ) return ;
8381
84- if ( isPreEighteen ) {
85- ReactDOM . unmountComponentAtNode ( this . _mountPoint ) ;
86- return ;
87- }
88-
8982 this . _root ?. unmount ( ) ;
9083 }
9184
0 commit comments