I am trying to position the MatSnackbar
module to appear at the top of my page.
I have tried using the config
to add customclass
. Here is my code:
component.ts
let config = new MatSnackBarConfig();
config.duration = 50000;
config.panelClass = ['red-snackbar']
this.snackBar.open("Please fill all the details before proceeding.", null, config);
.css
.red-snackbar{
position: absolute !important;
top: 54px;
}
But it is not working. Is it possible to re-position the MatSnackbar?
MatSnackBar is used to display information or text from bottom of the screen when performing any action. First, install the angular material using the above-mentioned command. After completing the installation, Import ‘MatSnackBarModule’ from ‘@angular/material/snack-bar’ in the app.module.ts file.
1) MatSnackBarModule: This is the module or we can say in the build module which is provided by the material library this has to be present inside the root module or any other child module in which we are using it. For reference please find the below code and use it in our application,
(MatSnackBarConfig<any>) - Additional configuration options for the snackbar. horizontalPosition: MatSnackBarHorizontalPosition - The horizontal position to place the snack bar. verticalPosition: MatSnackBarVerticalPosition - The vertical position to place the snack bar.
The angular module for this component is as above. This section covers basic components with message, action, and duration This is an example of showing a simple notification offline message on clicking the button provided click event for showing simple snack bar notification popup
You can determine the verticalPosition: MatSnackBarVerticalPosition
as specified by the docs
ts:
openSnackBar(message: string, action: string) {
this.snackBar.open(message, action, {
duration: 2000,
// here specify the position
verticalPosition: 'top'
});
}
Demo
To position the snackbar use position values
this.snackBar.open(message.text, action, {
duration: this.timeOut,
verticalPosition: 'bottom', // 'top' | 'bottom'
horizontalPosition: 'end', //'start' | 'center' | 'end' | 'left' | 'right'
panelClass: ['red-snackbar'],
});
and to change color, in your style.css define the red-snackbar:
.red-snackbar{
background-color: rgb(153, 50, 50);
color:lightgoldenrodyellow;
}
https://www.coditty.com/code/angular-material-display-multiple-snackbars-messages-in-sequence
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