How to display the Angular Material Progress Spinner as a somewhat transparent overlay of the current view (a page or a modal dialog)?
I was inspired by: Overriding Angular Material Size and Styling of md-dialog-container
I solved it like this:
Create a new component ProgressSpinnerDialogComponent
The content of progress-spinner-dialog.component.html:
<mat-spinner></mat-spinner>
The content of progress-spinner-dialog.component.ts:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-progress-spinner-dialog',
templateUrl: './progress-spinner-dialog.component.html',
styleUrls: ['./progress-spinner-dialog.component.css']
})
export class ProgressSpinnerDialogComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
In styles.css add for @angular/material version 17.x onwards:
.transparent .mat-mdc-dialog-container {
--mdc-dialog-container-color: rgba(0, 0, 0, 0.0);
}
.transparent .mat-mdc-dialog-surface {
box-shadow: none !important;
}
for previous versions of @angular/material:
.transparent .mat-dialog-container {
box-shadow: none;
background: rgba(0, 0, 0, 0.0);
}
Here an example usage of the progress spinner:
import { Component, OnInit } from '@angular/core';
import { MatDialog, MatDialogRef } from "@angular/material/dialog";
import { Observable } from "rxjs";
import { ProgressSpinnerDialogComponent } from "/path/to/progress-spinner-dialog.component";
@Component({
selector: 'app-use-progress-spinner-component',
templateUrl: './use-progress-spinner-component.html',
styleUrls: ['./use-progress-spinner-component.css']
})
export class UseProgressSpinnerComponent implements OnInit {
constructor(
private dialog: MatDialog
) {
let observable = new Observable(this.myObservable);
this.showProgressSpinnerUntilExecuted(observable);
}
ngOnInit() {
}
myObservable(observer) {
setTimeout(() => {
observer.next("done waiting for 5 sec");
observer.complete();
}, 5000);
}
showProgressSpinnerUntilExecuted(observable: Observable<Object>) {
let dialogRef: MatDialogRef<ProgressSpinnerDialogComponent> = this.dialog.open(ProgressSpinnerDialogComponent, {
panelClass: 'transparent',
disableClose: true
});
let subscription = observable.subscribe(
(response: any) => {
subscription.unsubscribe();
//handle response
console.log(response);
dialogRef.close();
},
(error) => {
subscription.unsubscribe();
//handle error
dialogRef.close();
}
);
}
}
Add it to the app.module
declarations: [...,ProgressSpinnerDialogComponent,...],
entryComponents: [ProgressSpinnerDialogComponent],
Use the below code to achieve the opaque:
HTML
<div style="height: 800px" [class.hide]="show">
<button class="btn btn-success" (click)="showSpinner()">Show spinner</button>
</div>
<app-spinner [show]="show" [size]="150"></app-spinner>
COMPONENT
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-spinner',
template: `
<i aria-hidden="true" class="fa fa-spinner fa-spin" [style.font-size.px]="size" *ngIf="show"></i>
`
})
export class SpinnerComponent {
@Input() size = 50;
@Input() show = false;
showSpinner() {
this.show = true;
}
}
CSS
.hide {
opacity: 0;
}
LIVE DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With