1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import { inject, injectable, optional } from 'inversify';
|
18 | import * as React from 'react';
|
19 | import { createRoot, Root } from 'react-dom/client';
|
20 | import { Disposable, DisposableCollection } from '../../common';
|
21 |
|
22 | export type RendererHost = HTMLElement;
|
23 | export const RendererHost = Symbol('RendererHost');
|
24 |
|
25 | @injectable()
|
26 | export class ReactRenderer implements Disposable {
|
27 | protected readonly toDispose = new DisposableCollection();
|
28 | readonly host: HTMLElement;
|
29 | protected hostRoot: Root;
|
30 |
|
31 | constructor(
|
32 | @inject(RendererHost) @optional() host?: RendererHost
|
33 | ) {
|
34 | this.host = host || document.createElement('div');
|
35 | this.hostRoot = createRoot(this.host);
|
36 | this.toDispose.push(Disposable.create(() => this.hostRoot.unmount()));
|
37 | }
|
38 |
|
39 | dispose(): void {
|
40 | this.toDispose.dispose();
|
41 | }
|
42 |
|
43 | render(): void {
|
44 | this.hostRoot.render(<React.Fragment>{this.doRender()}</React.Fragment>);
|
45 | }
|
46 |
|
47 | protected doRender(): React.ReactNode {
|
48 | return undefined;
|
49 | }
|
50 | }
|