Angular Material - Mat-Dialog - change background blur / darkening effect
Asked Answered
A

6

12

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

Allonym answered 11/3, 2019 at 21:28 Comment(0)
S
44

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 */
}
Scalise answered 11/3, 2019 at 21:47 Comment(4)
Thanks @benschabatnoam, that's exactly what I was looking for. I don't know how I could have missed it in the documentation, it's right at the top.Allonym
can we use this class for making background blur using opacityForepart
@Darshantheerth sure you can, I've updated the stackblitz sample to show you how to use the correct cssScalise
I had to set hasBackdrop: true in v 11Dinar
C
6

You can achieve a nice effect by combining opacity and blur. Do like this:

Add backdropClass to your dialog-options:

backdropClass: "bdrop"

And these rules to your stylesheet:

.bdrop {
    background-color: #bbbbbbf2;
    backdrop-filter: blur(4px);
}

Demo: https://angular-blurred-dialog-backdrop-zdyvpc.stackblitz.io/

Comedy answered 27/1, 2021 at 15:16 Comment(0)
S
4

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

Stable answered 12/3, 2020 at 14:14 Comment(1)
Thank you, looks like current angular material has backdrop disabled by default.Sassenach
H
1

Also you can just overide class .mat-dialog-container {} in your styles.scss

.mat-dialog-container {
  box-shadow: 0px 11px 15px -7px rgb(0 0 0 / 20%), 0px 24px 38px 3px rgb(0 0 0 / 14%), 0px 9px 46px 8px rgb(0 0 0 / 12%);
  background: #fff;
  color: black;
}
Homeomorphism answered 27/8, 2021 at 7:27 Comment(0)
P
0
const dialogConfig = new MatDialogConfig();**strong text**
dialogConfig.id="dialog"
this.dialog.open(CreatOrderComponent,dialogConfig)

// you can give it an ID then you can use that id to style it

Preindicate answered 4/7, 2022 at 11:42 Comment(0)
M
0

mat-dialog open and background blur

  const dialogRef = this.dialog.open(openComponent, {
            backdropClass: 'custom-dialog-overlay',  // mat-dialog css class
            disableClose: true  // If you click outside the mat-dialog box window, it will not close. 
        });

Global css

.custom-dialog-overlay {
  filter: blur(20px);
  background-color: #aba1a1;
  opacity: 0.7 !important;
}
Mesothorax answered 6/7, 2023 at 5:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.