angular / angular/components

bug(CdkPortalOutlet): Component can be rendered outside CdkPortalOutlet

Open
#26,187 0 comments 8 reactions 0 assignees View on GitHub
area: cdk/portal P3
Dominant language
TypeScript
Stars
25k
Forks
6.8k
Avg merge
1d 8h
Merged PRs (30d)
91

Description

### Is this a regression?

- [ ] Yes, this behavior used to work in the previous version

### The previous version in which this bug was not present was

_No response_

### Description

If you have ComponentPortal with provided ViewContainerRef NgForOf can rerender component outside CdkPortalOutlet.
Looks like [this code](https://github.com/angular/components/blob/7a24e95bafbdeb697f74a48e275c2442bcbefc74/src/cdk/portal/portal-directives.ts#L166) is executed only once and after NgForOf dom manipulations there is nothing that can move ComponentPortalExample to expected position

### Reproduction

[Demo](https://stackblitz.com/edit/angular-ivy-lthq8f?file=src%2Fapp%2Fportal.component.ts,src%2Fapp%2Fapp.module.ts,src%2Fapp%2Fapp.component.html)

Steps to reproduce:
1. Wait until component outlet will render component
2. Drag Item 1 down
3. Drag Item 1 up
4. ComponentPortalExample is rendered inside item list instead of inside component outlet
instead

### Expected Behavior

ComponentPortalExample should stay inside CdkPortalOutlet

### Actual Behavior

ComponentPortalExample is rendered inside item list instead of inside CdkPortalOutlet

### Environment

![image](https://user-images.githubusercontent.com/21180501/205888660-85a14f18-8501-4d2f-ba01-6c561bfb8500.png)

Contributor guide

Open the contributing guide

Research direction

Start with src/cdk/portal/portal-directives.ts at the linked code and reproduce the issue using the provided StackBlitz demo. Investigate the interaction between ComponentPortal, the provided ViewContainerRef, and NgForOf DOM reordering; done means ComponentPortalExample remains inside CdkPortalOutlet after dragging Item 1 down and back up.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.