Angular Material Tutorial - 25 - Snackbar

Codevolution
15 Apr 201909:09

Summary

TLDRThis video tutorial introduces snack bars in Angular Material, which are notification tools used to inform users of completed actions. It covers how to import the necessary modules, create a button to trigger a snack bar, and define methods for opening it with customizable messages and actions. The video also explains handling user interactions with observables, setting durations for automatic dismissal, and creating custom snack bar content with components. The comprehensive guide is aimed at developers looking to enhance their Angular applications with user-friendly notifications.

Takeaways

  • 📌 Snack bars in Angular Material are used as notifications to inform users of performed operations.
  • 🔴 They appear at the bottom of the screen and disappear automatically without requiring user input.
  • 🚫 Only one snack bar notification can be displayed at a time.
  • 🛠️ To implement a snack bar, import the MatSnackBarModule and add it to the NgModule's imports array.
  • 🔄 Add a button with the mat-button attribute and handle the click event to trigger the opening of a snack bar.
  • 📄 In the component class, inject MatSnackBar through the constructor and define an openSnackBar method.
  • 🔧 Pass a message and an optional action to the open method of the MatSnackBar service to display the snack bar with an action button.
  • 🔄 Use observables like afterDismissed() and onAction() to execute code when the snack bar is dismissed or the action is clicked.
  • ⏱️ The duration of a snack bar's display can be set using the duration property in milliseconds.
  • 🎨 Custom components can be used as content for a snack bar with the openFromComponent() method.
  • 👀 Snack bars can be utilized for actions such as undoing an operation, enhancing user experience.

Q & A

  • What are snack bars in Angular Material?

    -Snack bars in Angular Material are notifications that inform users of some operation that has been performed. They appear for a short duration at the bottom of the screen and do not require user input to disappear.

  • How to import the MatSnackBarModule in Angular?

    -To import the MatSnackBarModule, you need to include it from 'material.module.es' and add it to the 'imports' array in your module file.

  • How do you add a button to trigger a snack bar in Angular?

    -You can add a button with the 'mat-button' attribute and handle the 'click' event to call a method that opens the snack bar, passing a message as an argument.

  • What service is used to open a snack bar in Angular?

    -The 'MatSnackBar' service from 'angular/material' is used to open a snack bar in Angular applications.

  • How can you add an action to a snack bar?

    -An action can be added to a snack bar as the second argument to the 'open' method on the service instance. The action is displayed next to the message and can be clicked to execute a function.

  • What are observables in the context of snack bars in Angular?

    -Observables related to snack bars are used to execute code when the snack bar is dismissed or when an action is clicked. 'After dismissed' and 'on action' are two such observables.

  • How do you handle the action click on a snack bar?

    -To handle the action click, you subscribe to the 'on action' observable of the snack bar instance, which allows you to execute code when the action is clicked.

  • What is the purpose of the 'duration' property in snack bar configuration?

    -The 'duration' property in snack bar configuration indicates the length of time in milliseconds before the snack bar is automatically dismissed.

  • How can you create a custom snack bar with a component in Angular?

    -To create a custom snack bar, you can use the 'open from component' method of the MatSnackBar service, passing in the custom component as its first argument along with a duration for how long the snack bar should be displayed.

  • What is the typical use case for an action in a snack bar?

    -A typical use case for an action in a snack bar is to provide an 'undo' option for an operation, allowing users to revert the action that was performed.

  • How do you structure the component class to handle snack bars?

    -In the component class, you would inject the MatSnackBar service into the constructor and define methods to open snack bars, handle actions, and possibly execute code after the snack bar is dismissed or an action is clicked using observables.

Outlines

00:00

📢 Introduction to Angular Material Snackbars

This paragraph introduces the concept of snack bars in Angular Material, which are notification-like elements that inform users about an operation's completion. It explains that snack bars appear briefly at the screen's bottom and do not require user interaction to disappear. The video begins by demonstrating how to import the MatSnackbarModule and integrate it into the application. It then shows how to create a button in the HTML that, when clicked, triggers the display of a snack bar with a message 'Item deleted'. The paragraph further discusses how to add an action to the snack bar, allowing users to perform an opposite action, such as 'undo'. It also touches on how to handle the snack bar's dismissal and action events using observables, providing a way to execute code when the snack bar is closed or the action is taken.

05:01

🛠 Configuring and Customizing Angular Material Snackbars

The second paragraph delves into the configuration options available for Angular Material snack bars, such as the duration property that controls the display time before automatic dismissal. It describes how to set a duration of 2000 milliseconds (2 seconds) for the snack bar. The paragraph then moves on to discuss the 'openFromComponent' method, which allows for the creation and display of a snack bar with custom component content. An example is provided where a custom component with an orange-colored notification is created, added to the app module, and displayed when a button is clicked. The paragraph concludes by summarizing the key points about snack bars in Angular Material and encourages viewers to explore further.

Mindmap

Keywords

💡Angular Material

Angular Material is a UI framework by the Angular team that provides a collection of reusable, well-tested, and accessible UI components based on Material Design. In the video, Angular Material is used to create and manage snack bars, which are a part of the user interface.

💡Snack Bars

Snack bars are a type of UI component used to display brief notifications or messages to users. They typically appear at the bottom of the screen and disappear automatically after a set duration or after user interaction. In the context of the video, snack bars are used to inform users about operations performed, such as an item being deleted.

💡Notifications

Notifications are alerts or messages that inform users about events or updates. In the video, snack bars serve as a form of notification, providing feedback on actions performed within the application without requiring user input to acknowledge or dismiss them.

💡User Interaction

User interaction refers to the actions taken by a user to engage with a system or application. In the context of the video, user interaction is demonstrated when a user clicks a button to trigger the appearance of a snack bar.

💡Observables

Observables are a programming concept used to manage data streams and the propagation of changes. They allow components to subscribe to data sources and get notified when the data changes. In the video, observables are used to handle actions when a user interacts with the snack bar, such as dismissing it or triggering an undo action.

💡Component

In the context of Angular, a component is a self-contained piece of code that encapsulates functionality and markup. Components can be reused throughout an application and can have their own properties and methods. In the video, a custom component is created to display as the content of a snack bar, demonstrating how to use components in conjunction with snack bars.

💡Duration

Duration in the context of the video refers to the length of time a snack bar is displayed on the screen before it is automatically dismissed. It is an important property to control the user's experience by ensuring notifications do not stay on the screen for longer than necessary.

💡Action

In the context of the video, an action refers to a button or a user-initiated event within a snack bar that allows users to perform a related task, such as undoing an operation. Actions provide a way to directly respond to notifications and are an integral part of the user experience.

💡Custom Content

Custom content refers to the unique and tailored information or components that are designed specifically for a particular purpose or user interface element. In the video, custom content is demonstrated by using a custom component to display a different design and message in a snack bar.

💡Click Event

A click event is a user interaction that occurs when a user clicks on an element on a webpage or application. In the context of the video, click events are used to trigger the opening of a snack bar and to execute functions associated with the snack bar's action button.

💡Service

In the context of Angular applications, a service is a class that provides a set of functions or data that can be used across different components. Services are used to handle specific tasks and can be injected into components to provide their functionality. The MatSnackBar service is an example from Angular Material used to create and manage snack bars within the application.

Highlights

Introduction to snack bars in Angular Material as a notification system.

Snack bars are temporary notifications displayed at the bottom of the screen.

Importing the MatSnackBarModule and adding it to the material array is the first step in using snack bars.

A button with the mat-button attribute and an event handler for clicks is used to trigger a snack bar.

The openSnackBar method is defined in the component class to handle displaying snack bars.

Injecting the MatSnackBar service into the component's constructor enables the use of snack bars.

An action can be specified for the snack bar as a second argument in the open method.

The action parameter allows for a more generic openSnackBar method to handle different actions.

Observables are used to execute code when the snack bar is dismissed or an action is clicked.

The afterDismissed() and onAction() observables are subscribed to for different actions.

The duration property in the configuration object sets the automatic dismissal time for the snack bar.

The openFromComponent method creates and opens a snack bar with custom component content.

Custom components can be added to the app module's declarations and entryComponents arrays.

An example of a custom snack bar with an orange-colored notification is provided.

The video provides a comprehensive guide on implementing and customizing snack bars in Angular Material.

The use of snack bars enhances user experience by providing clear feedback on operations performed.

The video concludes with an invitation to subscribe for more content on Angular Material.

Transcripts

00:00

in this video let's take a look at snack

00:03

bars and angular material snack bars are

00:06

basically like notifications that inform

00:09

users of some operation that has been

00:11

performed they appear for a short

00:14

duration at the bottom of the screen and

00:17

they don't require user input to

00:19

disappear and at any point in time only

00:23

one snack bar notification may be

00:25

displayed let's begin by importing the

00:29

module so in material dot module dot es

00:31

import mat snack bar module and added to

00:36

the material array now in our HTML let's

00:41

add a button that will open the snack

00:43

bar so button add the mat button

00:47

attribute and handle the click event on

00:54

click we will call a method called

00:57

open snack bar passing in a string item

01:02

deleted as argument the button text is

01:07

going to be show snack bar so we now

01:12

have a button that called the open snack

01:15

bar method let's go to the class

01:17

component and define that method to open

01:21

a snack bar we need to make use of a

01:24

service and that service is mat snack

01:27

bar from angular slash material so go

01:31

ahead and import it and once you import

01:34

it inject it in the constructor

01:36

constructor private

01:41

snack bar of type mad snack bar now we

01:46

can define the open snack bar method

01:48

which will actually open the snack bar

01:51

so open snack bar accepts a message and

01:56

within the body we use the service

01:58

instance and call the open method

02:01

passing in the message so the service

02:04

instance is this dot snag bar and we

02:08

call open passing in the message

02:12

if we now save all the files and take a

02:15

look at the browser we should have a

02:17

button and when I click on the button

02:19

the snack bar should pop up at the

02:22

bottom of the screen with the message

02:24

item deleted so we have a basic snack

02:28

bar working as expected

02:30

apart from the message we can also

02:33

specify an action for a snack bar the

02:36

action is specified as the second

02:39

argument to the open method on the

02:41

service instance to keep the open snack

02:44

bar method more generic I will pass the

02:47

action parameter as well from the click

02:50

handler so in the HTML the second

02:53

parameter is going to be the string

02:55

dismiss in the component class the

02:59

method will now take a second parameter

03:01

which is action and the same is passed

03:06

to the open method

03:09

if you now take a look at the browser

03:11

click on the button a snack bar opens

03:14

and you can see the message item deleted

03:17

and the action dismiss when I click on

03:21

this action by default it closes the

03:24

snack bar a typical use case for this

03:28

type of snack bar is usually to undo an

03:31

operation for example let me rename the

03:35

button to delete

03:39

and the action to undo but when the user

03:45

does click on this action we want to

03:48

execute some code that will undo the

03:51

operation the way we are alerted when

03:54

the snack bar is dismissed or the action

03:57

is clicked is by using observables so in

04:02

the component class we are going to

04:05

start off by storing a reference to the

04:08

snack bar that has been opened select

04:12

snack bar F is equal to this dot snack

04:17

bar dot open now using this reference we

04:21

can subscribe to the after dismissed or

04:24

on action observables so the first one

04:29

is after dismissed it's a snack bar

04:32

sorry

04:33

snack bar F dot after dismissed dot

04:38

subscribe and we are going to basically

04:43

log the statement the snack bar

04:48

was dismissed similarly we also have

04:53

snack bar F dot on action dot subscribe

05:00

and over here we are going to log the

05:04

statement the slack bar action was

05:08

triggered if we now go back to the

05:12

browser and open dev tools click on the

05:17

delete button and click on the action we

05:22

can see the log statements in the

05:24

console action was clicked and the snack

05:28

bar was dismissed I just have console

05:32

log statements but you can have the code

05:34

to undo the deletion so we have the

05:38

message and the action which are passed

05:41

to the open method there is also a third

05:44

parameter which accepts some

05:47

configuration the one which would be

05:50

used mostly is the duration property the

05:54

Burien property indicates the length of

05:56

time in milliseconds to wait before

05:59

automatically dismissing the snack bar I

06:02

will add an object with the property

06:05

duration and set it to 2000 which is 2

06:09

seconds say comma and object duration

06:14

set to 2 seconds if you now go back to

06:19

the browser click on the button the

06:23

snack bar pops up but

06:25

after two seconds it automatically

06:27

dismisses and in the console you can see

06:30

the log statement so the after dismissed

06:34

part of the code was executed when the

06:38

snack bar was dismissed so when you have

06:41

an action you can use the on action

06:43

observable and when you don't have an

06:45

action but want to execute some code

06:47

when the snack bar is dismissed place

06:50

that code and the after dismissed

06:52

observable the last point to discuss is

06:56

about the open from component method as

07:00

the name indicates the open from

07:02

component method creates and opens a

07:05

snag bar with a custom component for the

07:09

content let's take a look at an example

07:11

in the existing component class which is

07:14

app component dot es I'm going to

07:17

quickly copy paste another component

07:22

now you could create the component in a

07:25

separate folder but for simplicity I

07:27

have it right here as you can see the

07:30

template is a span tag with color set to

07:34

orange now in the app module I will add

07:39

the component in the declarations array

07:41

and add to another property called entry

07:46

components

07:51

all right now that we have a component

07:53

to display as custom content let's add a

07:57

button and a click handler to open the

08:00

snack bar so in the HTML

08:04

button Matt button click handler is

08:10

going to be open custom snag bar the

08:13

text is going to be show custom slag bar

08:16

and in the component class we are going

08:21

to define open custom snack bar and this

08:28

is basically going to be this dot snag

08:31

bar dot open from component we pass in

08:35

the custom component as its first

08:38

argument and then we pass in duration of

08:43

two seconds if we now go back to the

08:47

browser and click on show custom snack

08:51

bar we should have our orange colored

08:54

notification custom snack bar all right

08:59

that is pretty much about snack bars in

09:02

angular material thank you guys for

09:04

watching feel free to subscribe I'll see

09:06

you guys in the next video

Rate This

5.0 / 5 (0 votes)

Related Tags
Angular MaterialSnack BarsUser NotificationsFrontend DevelopmentUI ComponentsCustom ContentAction HandlingCoding TutorialWeb DevelopmentNotification Duration