Skip to content

feat(cdk/portal): support projectableNodes in component portal #25185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cdk/portal/dom-portal-outlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class DomPortalOutlet extends BasePortalOutlet {
componentFactory,
portal.viewContainerRef.length,
portal.injector || portal.viewContainerRef.injector,
portal.projectableNodes || undefined,
);

this.setDisposeFn(() => componentRef.destroy());
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/portal/portal-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr

ngOnDestroy() {
super.dispose();
this._attachedPortal = null;
this._attachedRef = null;
this._attachedRef = this._attachedPortal = null;
}

/**
Expand All @@ -157,6 +156,7 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
componentFactory,
viewContainerRef.length,
portal.injector || viewContainerRef.injector,
portal.projectableNodes || undefined,
);

// If we're using a view container that's different from the injected one (e.g. when the portal
Expand Down
15 changes: 14 additions & 1 deletion src/cdk/portal/portal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,19 @@ describe('Portals', () => {

expect(hostContainer.textContent).toContain('Pizza');
});

it('should be able to pass projectable nodes to portal', () => {
// Set the selectedHost to be a ComponentPortal.
const testAppComponent = fixture.componentInstance;
const componentPortal = new ComponentPortal(PizzaMsg, undefined, undefined, undefined, [
[document.createTextNode('Projectable node')],
]);

testAppComponent.selectedPortal = componentPortal;
fixture.detectChanges();

expect(fixture.nativeElement.textContent).toContain('Projectable node');
});
});

describe('DomPortalOutlet', () => {
Expand Down Expand Up @@ -728,7 +741,7 @@ class ChocolateInjector {
/** Simple component for testing ComponentPortal. */
@Component({
selector: 'pizza-msg',
template: '<p>Pizza</p><p>{{snack}}</p>',
template: '<p>Pizza</p><p>{{snack}}</p><ng-content></ng-content>',
})
class PizzaMsg {
constructor(@Optional() public snack: Chocolate) {}
Expand Down
11 changes: 9 additions & 2 deletions src/cdk/portal/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ export class ComponentPortal<T> extends Portal<ComponentRef<T>> {
component: ComponentType<T>;

/**
* [Optional] Where the attached component should live in Angular's *logical* component tree.
* Where the attached component should live in Angular's *logical* component tree.
* This is different from where the component *renders*, which is determined by the PortalOutlet.
* The origin is necessary when the host is outside of the Angular application context.
*/
viewContainerRef?: ViewContainerRef | null;

/** [Optional] Injector used for the instantiation of the component. */
/** Injector used for the instantiation of the component. */
injector?: Injector | null;

/**
Expand All @@ -101,17 +101,24 @@ export class ComponentPortal<T> extends Portal<ComponentRef<T>> {
*/
componentFactoryResolver?: ComponentFactoryResolver | null;

/**
* List of DOM nodes that should be projected through `<ng-content>` of the attached component.
*/
projectableNodes?: Node[][] | null;

constructor(
component: ComponentType<T>,
viewContainerRef?: ViewContainerRef | null,
injector?: Injector | null,
componentFactoryResolver?: ComponentFactoryResolver | null,
projectableNodes?: Node[][] | null,
) {
super();
this.component = component;
this.viewContainerRef = viewContainerRef;
this.injector = injector;
this.componentFactoryResolver = componentFactoryResolver;
this.projectableNodes = projectableNodes;
}
}

Expand Down
3 changes: 2 additions & 1 deletion tools/public_api_guard/cdk/portal.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ export type CdkPortalOutletAttachedRef = ComponentRef<any> | EmbeddedViewRef<any

// @public
export class ComponentPortal<T> extends Portal<ComponentRef<T>> {
constructor(component: ComponentType<T>, viewContainerRef?: ViewContainerRef | null, injector?: Injector | null, componentFactoryResolver?: ComponentFactoryResolver | null);
constructor(component: ComponentType<T>, viewContainerRef?: ViewContainerRef | null, injector?: Injector | null, componentFactoryResolver?: ComponentFactoryResolver | null, projectableNodes?: Node[][] | null);
component: ComponentType<T>;
componentFactoryResolver?: ComponentFactoryResolver | null;
injector?: Injector | null;
projectableNodes?: Node[][] | null;
viewContainerRef?: ViewContainerRef | null;
}

Expand Down