angular / angular/components

bug(OverlayContainer, MatDialog): MatDialog not rendering inside custom OverlayContainer in angular 15

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

Description

In Angular 15, if a component is provided with OverlayContainer and overridden to use it as a custom container element, individual overlay elements are not rendering inside the container (), instead is appended to the document body(default behavior).

```@Injectable()
export class OverlayService extends OverlayContainer{
constructor(
@Inject(DOCUMENT) _document: any,
platform: Platform,
private problem: ElementRef
) {
super(_document, platform);
}

protected override _createContainer(): void {
const containerClass = "cdk-overlay-container";
const container = this._document.createElement("div");
container.classList.add(containerClass);

this.problem.nativeElement.appendChild(container);
this._containerElement = container;
}

}

@Component({
selector: 'app-problem',
templateUrl: './problem.component.html',
styleUrls: ['./problem.component.css'],
providers: [
DialogServiceService,
MatDialog,
{ provide: OverlayContainer, useClass: OverlayService },
]
})
```

Expected behavior:

```


....




```

I was expecting the MatDialog to render inside the custom container as in previous angular versions.

### What troubleshooting steps have you tried?

Current behavior(Angular 15):

```




```

### Reproduction

https://stackblitz.com/edit/angular-pfwikb?file=src%2Fapp%2Fproblem%2Fproblem.component.ts

### Environment

- Angular: 15
- CDK/Material: 15.1.2
- Browser(s): Chrome
- Operating System (e.g. Windows, macOS, Ubuntu): Mac

Contributor guide

Open the contributing guide

Research direction

Start with the StackBlitz reproduction at src/app/problem/problem.component.ts and inspect the OverlayService._createContainer implementation and the component-level OverlayContainer provider. Compare the rendered DOM with the expected custom-container structure, then verify that MatDialog uses that container under Angular 15. Done means the dialog is appended inside the custom cdk-overlay-container rather than directly under body.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.