Skip to content

Commit c313495

Browse files
author
Ben Grynhaus
committed
Simplify creation of ReactContent component and its public & internal API
1 parent 309d323 commit c313495

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22
// Licensed under the MIT License.
33

44
import { ComponentRef, EmbeddedViewRef, TemplateRef } from '@angular/core';
5-
import * as React from 'react';
6-
import { CHILDREN_TO_APPEND_PROP, ReactContent, ReactContentProps } from '../renderer/react-content';
5+
import { createReactContentElement, ReactContentProps } from '../renderer/react-content';
76

87
export interface RenderPropContext<TContext extends object> {
98
readonly render: (context: TContext) => JSX.Element;
109
}
1110

1211
function renderReactContent(rootNodes: HTMLElement[], additionalProps?: ReactContentProps): JSX.Element {
13-
return React.createElement(ReactContent, {
14-
...additionalProps,
15-
[CHILDREN_TO_APPEND_PROP]: rootNodes,
16-
});
12+
return createReactContentElement(rootNodes, additionalProps);
1713
}
1814

1915
/**

0 commit comments

Comments
 (0)