@@ -3,6 +3,9 @@ const retargetEvents = require('react-shadow-dom-retarget-events');
33const getStyleElementsFromReactWebComponentStyleLoader = require ( './getStyleElementsFromReactWebComponentStyleLoader' ) ;
44const extractAttributes = require ( './extractAttributes' ) ;
55
6+ require ( '@webcomponents/shadydom' ) ;
7+ require ( '@webcomponents/custom-elements' ) ;
8+
69module . exports = {
710 /**
811 * @param {JSX.Element } app
@@ -35,62 +38,48 @@ module.exports = {
3538 }
3639 }
3740
38- const proto = Object . create ( HTMLElement . prototype , {
39- attachedCallback : {
40- value : function ( ) {
41- let webComponentInstance = this ;
42- let mountPoint = webComponentInstance ;
43-
44- if ( useShadowDom ) {
45- // Re-assign the webComponentInstance (this) to the newly created shadowRoot
46- webComponentInstance = webComponentInstance . createShadowRoot ( ) ;
41+ const proto = class extends HTMLElement {
42+ connectedCallback ( ) {
43+ const webComponentInstance = this ;
44+ let mountPoint = webComponentInstance ;
4745
48- // Re-assign the mountPoint to the newly created "div" element
49- mountPoint = document . createElement ( 'div' ) ;
46+ if ( useShadowDom ) {
47+ // Re-assign the webComponentInstance (this) to the newly created shadowRoot
48+ const shadowRoot = webComponentInstance . attachShadow ( { mode : 'open' } ) ;
49+ // Re-assign the mountPoint to the newly created "div" element
50+ mountPoint = document . createElement ( 'div' ) ;
5051
51- // Move all of the styles assigned to the react component inside of the shadowRoot.
52- // By default this is not used, only if the library is explicitly installed
53- const styles = getStyleElementsFromReactWebComponentStyleLoader ( ) ;
54- styles . forEach ( ( style ) => {
55- webComponentInstance . appendChild ( style . cloneNode ( webComponentInstance ) ) ;
56- } ) ;
52+ // Move all of the styles assigned to the react component inside of the shadowRoot.
53+ // By default this is not used, only if the library is explicitly installed
54+ const styles = getStyleElementsFromReactWebComponentStyleLoader ( ) ;
55+ styles . forEach ( ( style ) => {
56+ shadowRoot . appendChild ( style . cloneNode ( shadowRoot ) ) ;
57+ } ) ;
5758
58- webComponentInstance . appendChild ( mountPoint ) ;
59+ shadowRoot . appendChild ( mountPoint ) ;
5960
60- retargetEvents ( webComponentInstance ) ;
61- }
61+ retargetEvents ( shadowRoot ) ;
62+ }
6263
63- ReactDOM . render ( app , mountPoint , function ( ) {
64- appInstance = this ;
65- appInstance . props = extractAttributes ( webComponentInstance ) ;
64+ ReactDOM . render ( app , mountPoint , function ( ) {
65+ appInstance = this ;
66+ appInstance . props = Object . assign ( extractAttributes ( webComponentInstance ) , appInstance . props ) ;
6667
67- callConstructorHook ( webComponentInstance ) ;
68- callLifeCycleHook ( 'attachedCallback' ) ;
69- } ) ;
70- } ,
71- } ,
72- connectedCallback : {
73- value : ( ) => {
68+ callConstructorHook ( webComponentInstance ) ;
7469 callLifeCycleHook ( 'connectedCallback' ) ;
75- } ,
76- } ,
77- disconnectedCallback : {
78- value : ( ) => {
70+ } ) ;
71+ }
72+ disconnectedCallback ( ) {
7973 callLifeCycleHook ( 'disconnectedCallback' ) ;
80- } ,
81- } ,
82- attributeChangedCallback : {
83- value : ( attributeName , oldValue , newValue , namespace ) => {
84- callLifeCycleHook ( 'attributeChangedCallback' , [ attributeName , oldValue , newValue , namespace ] ) ;
85- } ,
86- } ,
87- adoptedCallback : {
88- value : ( oldDocument , newDocument ) => {
89- callLifeCycleHook ( 'adoptedCallback' , [ oldDocument , newDocument ] ) ;
90- } ,
91- } ,
92- } ) ;
74+ }
75+ attributeChangedCallback ( attributeName , oldValue , newValue , namespace ) {
76+ callLifeCycleHook ( 'attributeChangedCallback' , [ attributeName , oldValue , newValue , namespace ] ) ;
77+ }
78+ adoptedCallback ( oldDocument , newDocument ) {
79+ callLifeCycleHook ( 'adoptedCallback' , [ oldDocument , newDocument ] ) ;
80+ }
81+ }
9382
94- document . registerElement ( tagName , { prototype : proto } ) ;
83+ customElements . define ( tagName , proto ) ;
9584 } ,
9685} ;
0 commit comments