@@ -14,20 +14,17 @@ import { Controller } from '@hotwired/stimulus';
1414export default class extends Controller {
1515 declare readonly componentValue ?: string ;
1616 declare readonly propsValue ?: object ;
17+ declare readonly permanentValue : boolean ;
1718
1819 static values = {
1920 component : String ,
2021 props : Object ,
22+ permanent : { type : Boolean , default : false } ,
2123 } ;
22- private unmountTimeoutId : any ;
2324
2425 connect ( ) {
25- clearTimeout ( this . unmountTimeoutId ) ;
26-
2726 const props = this . propsValue ? this . propsValue : null ;
28-
2927 this . dispatchEvent ( 'connect' , { component : this . componentValue , props : props } ) ;
30-
3128 if ( ! this . componentValue ) {
3229 throw new Error ( 'No component specified.' ) ;
3330 }
@@ -43,8 +40,17 @@ export default class extends Controller {
4340 }
4441
4542 disconnect ( ) {
46- clearTimeout ( this . unmountTimeoutId ) ;
47- this . unmountTimeoutId = setTimeout ( this . _unmountReactElement . bind ( this ) , 50 ) ;
43+ if ( this . permanentValue ) {
44+ // Prevent unmounting the component if the controller is permanent
45+ // (no render is allowed after unmounting)
46+ return ;
47+ }
48+
49+ ( this . element as any ) . root . unmount ( ) ;
50+ this . dispatchEvent ( 'unmount' , {
51+ component : this . componentValue ,
52+ props : this . propsValue ? this . propsValue : null ,
53+ } ) ;
4854 }
4955
5056 _renderReactElement ( reactElement : ReactElement ) {
@@ -58,14 +64,6 @@ export default class extends Controller {
5864 element . root . render ( reactElement ) ;
5965 }
6066
61- _unmountReactElement ( ) {
62- ( this . element as any ) . root . unmount ( ) ;
63- this . dispatchEvent ( 'unmount' , {
64- component : this . componentValue ,
65- props : this . propsValue ? this . propsValue : null ,
66- } ) ;
67- }
68-
6967 private dispatchEvent ( name : string , payload : any ) {
7068 this . dispatch ( name , { detail : payload , prefix : 'react' } ) ;
7169 }
0 commit comments