Skip to content

Commit fb98c0b

Browse files
committed
feat: add compatibility with data-turbo-permanent
1 parent c9bf5ee commit fb98c0b

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

src/React/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 2.21.0
4+
5+
- Add Turbo `data-turbo-permanent` support. Unmounting the React component now uses
6+
a delay and can be prevented if the Stimulus controller is `deconnected` and
7+
immediately `connected` again.
8+
39
## 2.13.2
410

511
- Revert "Change JavaScript package to `type: module`"

src/React/assets/dist/render_controller.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ export default class extends Controller {
77
component: StringConstructor;
88
props: ObjectConstructor;
99
};
10+
private unmountTimeoutId;
1011
connect(): void;
1112
disconnect(): void;
1213
_renderReactElement(reactElement: ReactElement): void;
14+
_unmountReactElement(): void;
1315
private dispatchEvent;
1416
}

src/React/assets/dist/render_controller.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var clientExports = requireClient();
4040

4141
class default_1 extends Controller {
4242
connect() {
43+
clearTimeout(this.unmountTimeoutId);
4344
const props = this.propsValue ? this.propsValue : null;
4445
this.dispatchEvent('connect', { component: this.componentValue, props: props });
4546
if (!this.componentValue) {
@@ -54,11 +55,8 @@ class default_1 extends Controller {
5455
});
5556
}
5657
disconnect() {
57-
this.element.root.unmount();
58-
this.dispatchEvent('unmount', {
59-
component: this.componentValue,
60-
props: this.propsValue ? this.propsValue : null,
61-
});
58+
clearTimeout(this.unmountTimeoutId);
59+
this.unmountTimeoutId = setTimeout(this._unmountReactElement.bind(this), 50);
6260
}
6361
_renderReactElement(reactElement) {
6462
const element = this.element;
@@ -67,6 +65,13 @@ class default_1 extends Controller {
6765
}
6866
element.root.render(reactElement);
6967
}
68+
_unmountReactElement() {
69+
this.element.root.unmount();
70+
this.dispatchEvent('unmount', {
71+
component: this.componentValue,
72+
props: this.propsValue ? this.propsValue : null,
73+
});
74+
}
7075
dispatchEvent(name, payload) {
7176
this.dispatch(name, { detail: payload, prefix: 'react' });
7277
}

src/React/assets/src/render_controller.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ export default class extends Controller {
1919
component: String,
2020
props: Object,
2121
};
22-
22+
private unmountTimeoutId: any;
23+
2324
connect() {
25+
clearTimeout(this.unmountTimeoutId);
26+
2427
const props = this.propsValue ? this.propsValue : null;
2528

2629
this.dispatchEvent('connect', { component: this.componentValue, props: props });
@@ -40,11 +43,8 @@ export default class extends Controller {
4043
}
4144

4245
disconnect() {
43-
(this.element as any).root.unmount();
44-
this.dispatchEvent('unmount', {
45-
component: this.componentValue,
46-
props: this.propsValue ? this.propsValue : null,
47-
});
46+
clearTimeout(this.unmountTimeoutId);
47+
this.unmountTimeoutId = setTimeout(this._unmountReactElement.bind(this), 50);
4848
}
4949

5050
_renderReactElement(reactElement: ReactElement) {
@@ -57,6 +57,14 @@ export default class extends Controller {
5757

5858
element.root.render(reactElement);
5959
}
60+
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+
}
6068

6169
private dispatchEvent(name: string, payload: any) {
6270
this.dispatch(name, { detail: payload, prefix: 'react' });

0 commit comments

Comments
 (0)