Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material - Mat-Dialog - change background blur / darkening effect

Hello my beloved community,

Using angular with angular material.

With the default configuration when you open up a material dialog, it darkens the background a bit. Now I would like it to be a blurred background instead. I tried playing around with the css styles but I could not get the background of the window to change (couldn't get the right selector inside of component template).

I went through the documentation but there is nothing there. I can play a little bit more with the styles since I am sure there is probably some tricky way but considering the darkening effect is already there out of the box I would assume there should be a theming feature available out of the box as well. What you think?

enter image description here

like image 913
qubits Avatar asked Mar 11 '19 21:03

qubits


People also ask

How to use the angular material dialog?

In order to use the Angular Material Dialog, we will first need to import MatDialogModule: ... ... ... Notice also CourseDialogComponent, this component will be the body of our custom dialog.

How to add dark mode in angular material components?

The Angular Material components library comes bundled with a few themes which we can use for your app. But for adding a dark mode, you need to add a custom theme file instead. A sample custom theme looks like the following.

How to change the background color of the dialog component?

mat-dialog-container { background-color: greenyellow !important; } another way is changing Material theme globally in styles.scss. if you want to change background color different for dialogs, set background color of = transparency. then set background color for dialog component.

Is there a light or dark theme for angular?

This prebuilt theme is intentionally a dark theme. If you're not intending to use this theme, there are still 2 other light themes that you can choose from: indigo-pink.css or deeppurple-amber.css. Check out the theming guide for more info: material.angular.io/guide/theming#using-a-pre-built-theme


2 Answers

I guess you've missed the property MatDialogConfig - backdropClass in the docs.

Check this StackBlitz DEMO for a simple example

From this DEMO:

dialog-overview-example.ts:

openDialog(): void {
  const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
    width: '250px',
    data: {name: this.name, animal: this.animal},
    backdropClass: 'backdropBackground' // This is the "wanted" line
  });

  dialogRef.afterClosed().subscribe(result => {
    console.log('The dialog was closed');
    this.animal = result;
  });
}

styles.css:

.backdropBackground {
  /* your css needs */
}
like image 62
benshabatnoam Avatar answered Dec 18 '22 23:12

benshabatnoam


The given answer by @benshabatnoam is absolutely correct, but the documentation also has another option to disable the backdrop altogether.

hasBackdrop

Here is an example:

https://stackblitz.com/edit/angular-ei9hdv

like image 44
Suyash Gulati Avatar answered Dec 18 '22 22:12

Suyash Gulati