Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular material Snackbar with multiple actions

Is it possible to have multiple actions within an Angular Material snackbar?

I know it is possible to use your own component for the snackbar.

But for this code, the action is only one action

private snackBar: MatSnackBar;

this.snackBar._openedSnackBarRef.onAction().subscribe(
  () => {
    console.log('action has occurred, but which one?')
  }
);
like image 749
Dev Db Avatar asked Apr 11 '18 12:04

Dev Db


People also ask

What is snackbar in Angular 7?

Angular Material 7 - SnackBar. The <MatSnackBar>, an Angular Directive, is used to show a notification bar to show on mobile devices as an alternative of dialogs/popups. In this chapter, we will showcase the configuration required to show a snack bar using Angular Material.

What is angular material toast | snackbar?

Angular Material Snackbar or Angular material toast is useful for showing alert and notification messages. Exploring how to implement Angular material toast | snackbar and how to configure its color and position. How to apply style or customize the style to Angular material toast | snackbar.

How to apply angular material snackbar color?

Position the snackBar on page screen and it can have values of “start” or “left”, “center”, “end” or “right”. How to apply Angular Material Snackbar color? We can apply CSS classes to our Angular material toast, by using the CSS custom class name to panelClass attribute and we can add style and in our let change the background color.

How to create an angular material snack project?

Step 1: Let’s first create an Angular material snack project, we need to install the angular material library. Let’s create and install the angular material library. ng new snackMaterialApp cd snackMaterialApp ng add @angular/material While running the 3rd command will ask you the select theme, material typography, and animation, press yes on all.


2 Answers

I am using Angular9 and we have an option to use a custom component inside the SnackBar. Here is the sample code:

Inside callee component:

    openSnackBar(message) {
        this.snackBar.openFromComponent(CustomSnackBarComponent, {
            duration: 5 * 1000,
        });
    }

Custom component:

@Component({
    selector: 'snack-bar-component-example-snack',
    template: `<div class="example-pizza-party">
               <button mat-raised-button (click)="cancel()">Cancel </button>
               <button mat-raised-button (click)="doAction()">View Results </button>
               </div>`,
    styles: [`
      .example-pizza-party {
        color: green;
      }
    `],
  })
  export class CustomSnackBarComponent {

    constructor(private router: Router) { }

    doAction() {
        // you can do some action here
    }

    cancel() {
      // close the snackbar or wirte another action
    }

  }

Refer to the official docs here.

like image 116
Sandeep Kumar Avatar answered Sep 18 '22 03:09

Sandeep Kumar


Demo : SnackBar created using Bottom Sheet

I have not found 2 actions working in SnackBars, but implemented a snackBar with 2 actions using BottomSheet Component.

Scenario :
I had same requirement in the project.
Notifications were displayed using snackBar in the whole project, and for one action, its easy to handle using snackBar.

Problem :

  • But, there was a requirement of adding 2 or multiple actions in the snackBars as notifications were completely handled through snackBars in the Project.

Solution Achieved :

  • Created and styled the BottomSheet in such a manner to replicate the snackbar used in the project.
  • Demo Project : https://stackblitz.com/edit/angular-bottomsheet-as-snackbar?file=src%2Fapp%2Fapp.component.html

Click on any tab to open a SnackBar

Opened SnackBar :

Open SnackBar using BottomSheet

like image 36
Abhishek Kumar Avatar answered Sep 20 '22 03:09

Abhishek Kumar