@@ -3,6 +3,9 @@ const retargetEvents = require('react-shadow-dom-retarget-events');
3
3
const getStyleElementsFromReactWebComponentStyleLoader = require ( './getStyleElementsFromReactWebComponentStyleLoader' ) ;
4
4
const extractAttributes = require ( './extractAttributes' ) ;
5
5
6
+ require ( '@webcomponents/shadydom' ) ;
7
+ require ( '@webcomponents/custom-elements' ) ;
8
+
6
9
module . exports = {
7
10
/**
8
11
* @param {JSX.Element } app
@@ -35,62 +38,48 @@ module.exports = {
35
38
}
36
39
}
37
40
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 ;
47
45
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' ) ;
50
51
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
+ } ) ;
57
58
58
- webComponentInstance . appendChild ( mountPoint ) ;
59
+ shadowRoot . appendChild ( mountPoint ) ;
59
60
60
- retargetEvents ( webComponentInstance ) ;
61
- }
61
+ retargetEvents ( shadowRoot ) ;
62
+ }
62
63
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 ) ;
66
67
67
- callConstructorHook ( webComponentInstance ) ;
68
- callLifeCycleHook ( 'attachedCallback' ) ;
69
- } ) ;
70
- } ,
71
- } ,
72
- connectedCallback : {
73
- value : ( ) => {
68
+ callConstructorHook ( webComponentInstance ) ;
74
69
callLifeCycleHook ( 'connectedCallback' ) ;
75
- } ,
76
- } ,
77
- disconnectedCallback : {
78
- value : ( ) => {
70
+ } ) ;
71
+ }
72
+ disconnectedCallback ( ) {
79
73
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
+ }
93
82
94
- document . registerElement ( tagName , { prototype : proto } ) ;
83
+ customElements . define ( tagName , proto ) ;
95
84
} ,
96
85
} ;
0 commit comments