Skip to content

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

Open
@lijojoy123

Description

@lijojoy123

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).

export class OverlayService extends OverlayContainer{
    constructor(
      @Inject(DOCUMENT) _document: any,
      platform: Platform,
      private problem: ElementRef<HTMLElement>
    ) {
      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:

<body>
 <app-problem>
   <div>....</div>
   <div class="cdk-overlay-container">
      <mat-dialog-container></mat-dialog-container>
   </div>
 </app-problem>
</body>

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):

<body>
  <app-problem></app-problem>
</body>
<div class="cdk-overlay-container">
  <mat-dialog-container></mat-dialog-container>
</div>

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: cdk/overlayarea: material/dialogtroubleshootingThis issue is not reporting an issue, but just asking for help

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions