Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I position Position MatSnackBar module in Angular?

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?

like image 247
helloworld Avatar asked Aug 20 '18 19:08

helloworld


People also ask

How to use matsnackbar in Angular Material?

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.

What is matsnackbarmodule in material library?

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,

What are the different configuration options for the snackbar?

(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.

What is the angular module for snack bar notification popup?

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


2 Answers

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

like image 51
kboul Avatar answered Sep 23 '22 16:09

kboul


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

like image 33
Igor Simic Avatar answered Sep 22 '22 16:09

Igor Simic