Skip to content

Commit 585a852

Browse files
author
Ben Grynhaus
committed
Simplify creation of ReactContent element
1 parent 8092577 commit 585a852

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

libs/core/src/lib/renderer/react-content.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import * as React from 'react';
55
import * as ReactDOM from 'react-dom';
6-
import { Omit } from '../declarations/omit';
76
import * as dom from '../utils/dom';
87

98
const DEBUG = false;
@@ -12,14 +11,7 @@ export const CHILDREN_TO_APPEND_PROP = 'children-to-append';
1211
/**
1312
* Props that can be passed to `ReactContent` from users.
1413
*/
15-
export type ReactContentProps = Omit<AllReactContentProps, typeof CHILDREN_TO_APPEND_PROP>;
16-
17-
/**
18-
* @internal
19-
*/
20-
export interface AllReactContentProps {
21-
readonly [CHILDREN_TO_APPEND_PROP]: HTMLElement[];
22-
14+
export interface ReactContentProps {
2315
/**
2416
* Experimental rendering mode.
2517
* Uses a similar approach to `router-outlet`, where the child elements are added to the parent, instead of this node, and this is hidden.
@@ -28,14 +20,34 @@ export interface AllReactContentProps {
2820
legacyRenderMode?: boolean;
2921
}
3022

23+
/**
24+
* Creates a new `ReactContent` element.
25+
* @param children The children to append to the `ReactContent` element.
26+
* @param additionalProps _Optional_. @see `ReactContentProps`
27+
* @returns
28+
*/
29+
export function createReactContentElement(children: ReadonlyArray<HTMLElement>, additionalProps?: ReactContentProps) {
30+
return React.createElement(ReactContent, {
31+
...additionalProps,
32+
[CHILDREN_TO_APPEND_PROP]: children,
33+
});
34+
}
35+
36+
/**
37+
* @internal
38+
*/
39+
export interface InternalReactContentProps extends ReactContentProps {
40+
readonly [CHILDREN_TO_APPEND_PROP]: ReadonlyArray<HTMLElement>;
41+
}
42+
3143
/**
3244
* Render any `HTMLElement`s (including Angular components) as a child of React components.
3345
* Supports two rendering modes:
3446
* 1. `legacy` - append `<react-content>` as the root, and nest the `children-to-append` underneath it.
3547
* 2. `new` (**default**) - append the `children-to-append` to the parent of this component, and hide the `<react-content>` element.
3648
* (similar to how `router-outlet` behaves in Angular).
3749
*/
38-
export class ReactContent extends React.PureComponent<AllReactContentProps> {
50+
export class ReactContent extends React.PureComponent<InternalReactContentProps> {
3951
componentDidMount() {
4052
const element = ReactDOM.findDOMNode(this);
4153
if (this.props[CHILDREN_TO_APPEND_PROP]) {

libs/core/src/lib/renderer/renderprop-helpers.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33

44
import { ComponentRef, NgZone, TemplateRef } from '@angular/core';
55
import * as React from 'react';
6-
import { CHILDREN_TO_APPEND_PROP, ReactContent, ReactContentProps } from '../renderer/react-content';
6+
import { createReactContentElement, ReactContentProps } from '../renderer/react-content';
77
import { ReactTemplate } from './react-template';
88

99
export interface RenderPropContext<TContext extends object> {
1010
readonly render: (context: TContext) => JSX.Element;
1111
}
1212

1313
function renderReactContent(rootNodes: HTMLElement[], additionalProps?: ReactContentProps): JSX.Element {
14-
return React.createElement(ReactContent, {
15-
...additionalProps,
16-
[CHILDREN_TO_APPEND_PROP]: rootNodes,
17-
});
14+
return createReactContentElement(rootNodes, additionalProps);
1815
}
1916

2017
/**

0 commit comments

Comments
 (0)