DEV Community

John Peters
John Peters

Posted on • Edited on

1 2

Material Dialog Positioning

First pass in your 'MessageComponent' on Open

     // data you want to show is contained in the matDialogConfig
     let mc = this.getConfig(data);
     // Tell Matdialog which Angular component to use.
     let mdRef = this.md.open(MessageComponent, mc);
Enter fullscreen mode Exit fullscreen mode

Grab the data out of MessageComponent

    import { MAT_DIALOG_DATA } from "@angular/material/dialog";
    import { Component, OnInit, AfterViewInit, Inject } from "@angular/core";
    import { inject } from "@angular/core/testing";

    @Component({
      selector: "lib-message",
      templateUrl: "./message.component.html",
      styleUrls: ["./message.component.css"],
    })
    export class MessageComponent implements OnInit, AfterViewInit {
      constructor(@Inject(MAT_DIALOG_DATA) public data: any) {
        // get the injected dialog data
        this.data = data;
      }

      ngOnInit(): void {}
      ngAfterViewInit() {}
    }
Enter fullscreen mode Exit fullscreen mode

In HTML, this is only markup needed.


    {{data}}
Enter fullscreen mode Exit fullscreen mode

The CSS in the MessageComponent allows full control on position.

    :host {
        display: grid;
        justify-content: center;
        align-items: center;
        background-color: yellow;
        position: absolute;
        top: 10em;
        left: 20em;
        height: 10em;
        width: 20em;
        box-shadow: 0 0 5px rgb(0, 0, 0, 0.27);
    }
Enter fullscreen mode Exit fullscreen mode

Your message component takes precedence over the CDK Overlay! Very nice.

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

👋 The next DEV Challenge is live

Participate in the Agent.ai Challenge

Make your life easier and win some cash? Sounds like a plan!

We are so excited to team up with Agent.ai for our next community challenge – can you guess what we’ll be building?! 🤖😎

Read more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay