File tree Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -81,7 +81,7 @@ export class CustomElement extends HTMLElement {
81
81
let rootEl = this ;
82
82
switch ( this . constructor . renderRoot ) {
83
83
case "container" :
84
- rootEl = document . createElement ( "div" ) ;
84
+ rootEl = this . rootDiv = document . createElement ( "div" ) ;
85
85
this . appendChild ( rootEl ) ;
86
86
break ;
87
87
case "shadowRoot" :
@@ -110,7 +110,11 @@ export class CustomElement extends HTMLElement {
110
110
}
111
111
112
112
disconnectedCallback ( ) {
113
- ReactDOM . unmountComponentAtNode ( _rootShadows . get ( this ) ) ;
113
+ ReactDOM . unmountComponentAtNode ( _rootShadows . get ( this ) ) ;
114
+ if ( this . rootDiv ) {
115
+ this . removeChild ( this . rootDiv ) ;
116
+ delete this . rootDiv ;
117
+ }
114
118
let model = _models . get ( this ) ;
115
119
if ( model ) {
116
120
model . destroy ( ) ;
Original file line number Diff line number Diff line change @@ -111,4 +111,25 @@ describe("WebComponent", () => {
111
111
} ) ;
112
112
} ) ;
113
113
} ) ;
114
+
115
+ it ( "should clean up when unmounted" , ( ) => {
116
+ customElements . whenDefined ( 'custom-component' ) . then ( ( ) => {
117
+ expect ( element . querySelector ( 'div' ) ) . to . be . null ;
118
+
119
+ // CustomElement should create a container div
120
+ document . body . appendChild ( element ) ;
121
+ expect ( element . querySelector ( 'div' ) ) . to . exist ;
122
+
123
+ // CustomElement clean up the container div
124
+ document . body . removeChild ( element ) ;
125
+ expect ( element . querySelector ( 'div' ) ) . to . be . null ;
126
+
127
+ // Make sure that remounting it works
128
+ document . body . appendChild ( element ) ;
129
+ expect ( element . querySelector ( 'div' ) ) . to . exist ;
130
+
131
+ document . body . removeChild ( element ) ;
132
+ expect ( element . querySelector ( 'div' ) ) . to . be . null ;
133
+ } ) ;
134
+ } ) ;
114
135
} ) ;
You can’t perform that action at this time.
0 commit comments