|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { EmbeddedViewRef, NgZone, TemplateRef } from '@angular/core'; |
| 5 | +import * as React from 'react'; |
| 6 | +import * as ReactDOM from 'react-dom'; |
| 7 | +import { Subscription } from 'rxjs'; |
| 8 | + |
| 9 | +const DEBUG = false; |
| 10 | + |
| 11 | +/** |
| 12 | + * @internal |
| 13 | + */ |
| 14 | +export interface ReactTemplateProps { |
| 15 | + /** |
| 16 | + * Experimental rendering mode. |
| 17 | + * Uses a similar approach to `router-outlet`, where the child elements are added to the parent, instead of this node, and this is hidden. |
| 18 | + * @default false |
| 19 | + */ |
| 20 | + legacyRenderMode?: boolean; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Creates a new `ReactTemplate` element. |
| 25 | + * @param templateRef The template to render. |
| 26 | + * @param context The context to inject the template. |
| 27 | + * @param ngZone A zone used for tracking changes in the template. |
| 28 | + * @param additionalProps _Optional_. @see `ReactTemplateProps`. |
| 29 | + */ |
| 30 | +export function createReactTemplateElement<TContext extends object | void>( |
| 31 | + templateRef: TemplateRef<TContext>, |
| 32 | + context: TContext, |
| 33 | + ngZone: NgZone, |
| 34 | + additionalProps?: ReactTemplateProps |
| 35 | +) { |
| 36 | + return React.createElement(ReactTemplate, { ngZone, templateRef, context, ...additionalProps }); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * @internal |
| 41 | + */ |
| 42 | +interface InternalReactTemplateProps<TContext extends object | void> extends ReactTemplateProps { |
| 43 | + ngZone: NgZone; |
| 44 | + templateRef: TemplateRef<TContext>; |
| 45 | + context: TContext; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Render an `ng-template` as a child of a React component. |
| 50 | + * Supports two rendering modes: |
| 51 | + * 1. `legacy` - append `<react-content>` as the root, and nest the `children-to-append` underneath it. |
| 52 | + * 2. `new` (**default**) - append the `children-to-append` to the parent of this component, and hide the `<react-content>` element. |
| 53 | + * (similar to how `router-outlet` behaves in Angular). |
| 54 | + */ |
| 55 | +export class ReactTemplate<TContext extends object | void> extends React.Component< |
| 56 | + InternalReactTemplateProps<TContext> |
| 57 | +> { |
| 58 | + private _embeddedViewRef: EmbeddedViewRef<TContext>; |
| 59 | + private _ngZoneSubscription: Subscription; |
| 60 | + |
| 61 | + componentDidUpdate() { |
| 62 | + // Context has changes, trigger change detection after pushing the new context in |
| 63 | + Object.assign(this._embeddedViewRef.context, this.props.context); |
| 64 | + this._embeddedViewRef.detectChanges(); |
| 65 | + } |
| 66 | + |
| 67 | + componentDidMount() { |
| 68 | + const { context, ngZone, templateRef } = this.props; |
| 69 | + |
| 70 | + this._embeddedViewRef = templateRef.createEmbeddedView(context); |
| 71 | + const element = ReactDOM.findDOMNode(this); |
| 72 | + if (DEBUG) { |
| 73 | + console.warn('ReactTemplate Component > componentDidMount > childrenToAppend:', { |
| 74 | + rootNodes: this._embeddedViewRef.rootNodes, |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + const hostElement = this.props.legacyRenderMode ? element : element.parentElement; |
| 79 | + |
| 80 | + this._embeddedViewRef.rootNodes.forEach(child => hostElement.appendChild(child)); |
| 81 | + |
| 82 | + // Detect the first cycle's changes, and then subscribe for subsequent ones |
| 83 | + this._embeddedViewRef.detectChanges(); |
| 84 | + this._ngZoneSubscription = ngZone.onUnstable.subscribe(() => { |
| 85 | + this._embeddedViewRef.detectChanges(); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + componentWillUnmount() { |
| 90 | + this._ngZoneSubscription.unsubscribe(); |
| 91 | + |
| 92 | + if (this._embeddedViewRef) { |
| 93 | + this._embeddedViewRef.destroy(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + render() { |
| 98 | + return React.createElement('react-template', !this.props.legacyRenderMode && { style: { display: 'none' } }); |
| 99 | + } |
| 100 | +} |
0 commit comments