Angular Material Tutorial - 11 - Sidenav

Codevolution
4 Mar 201909:49

Summary

TLDRThis video tutorial introduces the implementation of side navigation in Angular Material. It outlines the process of importing necessary modules, setting up the sitemap container, and utilizing components like mat-sidenav-container, mat-sidenav, and mat-sidenav-content. The video demonstrates various methods to show and hide the sidenav, including using the opened attribute, two-way binding, and invoking open/close methods. It also explains how to apply different modes to the sidenav, such as 'over', 'push', and 'side'. Additionally, it covers event handling for opened and closed events to perform actions based on sidenav state changes.

Takeaways

  • πŸ“Œ Import the necessary Angular Material modules for sidenav, navigation, and other components.
  • πŸ–ΌοΈ Use the MatSidenavContainer as a wrapper for the main content and sidenav to ensure proper layout and behavior.
  • πŸšͺ The MatSidenav component represents the sidenav content and is hidden by default.
  • πŸ“ Add padding, height, and styling to the MatSidenavContainer and MatSidenavContent for better visual presentation.
  • 🎨 Apply a background color and set a width for the sidenav to enhance its appearance.
  • πŸ”„ Use two-way data binding to control the visibility of the sidenav with a property and a toggle button.
  • πŸ”„ Implement the (click) event on a button to toggle the sidenav's open state.
  • πŸ”„ Utilize the mode attribute on the MatSidenav component to set different sidenav modes: over, push, or side.
  • πŸ”„ Apply the open(), close(), and toggle() methods on the sidenav to control its visibility programmatically.
  • πŸ“£ Listen to the opened and closed events of the sidenav to perform actions when the sidenav state changes.
  • πŸ“ˆ Use template reference variables to invoke methods on components directly from the template.

Q & A

  • What is the first step in using Angular Material's side navigation?

    -The first step is to import the necessary modules, specifically 'mat side' and 'nav module', from the Angular Material library.

  • How do you add side navigation to your application?

    -You add side navigation by using three components: 'mat-sidenav-container', 'mat-sidenav', and 'mat-sidenav-content'. The 'mat-sidenav-container' acts as a container for both the side navigation and the main content.

  • Why is the side navigation not visible by default?

    -The side navigation is hidden by default, and you need to use methods such as adding the 'opened' attribute or binding it to a property to make it visible.

  • How can you apply basic styling to the side navigation?

    -You can apply basic styling by adding CSS rules to 'mat-sidenav-container', 'mat-sidenav', and 'mat-sidenav-content', such as setting a height, padding, background color, and width.

  • What is two-way data binding and how is it used in the context of the side navigation?

    -Two-way data binding is a technique that allows data to be both read and updated from and to the UI. In the context of side navigation, it is used to bind the 'opened' attribute to a property that can be controlled by a button click.

  • What are the different modes available for the side navigation?

    -The side navigation can have three modes: 'over', 'push', and 'side'. 'Over' mode covers the main content with a backdrop, 'push' mode pushes the main content aside, and 'side' mode places the side navigation next to the main content.

  • How can you toggle the side navigation open and closed?

    -You can toggle the side navigation by invoking the 'toggle' method on the 'mat-sidenav' component or by using a button that calls the 'open' and 'close' methods on the side navigation's template reference variable.

  • What are the 'opened' and 'closed' events and how can they be utilized?

    -The 'opened' and 'closed' events are emitted when the side navigation is opened or closed, respectively. You can listen to these events and perform actions, such as logging a message or executing a function, by calling methods in response to these events.

  • How can you ensure that certain elements are not affected by the side navigation container?

    -If you don't want certain elements, like headers or footers, to be affected by the side navigation container, you can place them outside of the 'mat-sidenav-container'.

  • What is a template reference variable and how is it used in this context?

    -A template reference variable is a way to get a reference to a DOM element in Angular templates. In this context, it is used to reference the 'mat-sidenav' component so that you can invoke methods like 'open', 'close', and 'toggle' on it.

  • What are the three methods to show or hide the side navigation?

    -The three methods to show or hide the side navigation are: 1) using the 'opened' attribute, 2) invoking the 'open' or 'close' methods on the side navigation itself, and 3) calling the 'toggle' method.

Outlines

00:00

πŸ“š Introduction to Angular Material SideNav

This paragraph introduces the process of implementing a side navigation (side nav) in an Angular application using Angular Material. It begins by explaining the initial step of importing the necessary modules from the Angular Material library. The paragraph then describes the use of three main components: the MatSidenavContainer, MatSidenav, and MatSidenavContent, which collectively form the structure of the side nav. It also touches on the default behavior of the side nav being hidden and the various methods to show or hide it, such as adding the 'opened' attribute or using two-way data binding with a button. Additionally, the paragraph briefly mentions the importance of applying basic styling to improve the user interface.

05:03

πŸ”§ Customizing SideNav Behavior and Appearance

This paragraph delves into the different modes available for the side nav and how to apply them using the 'mode' attribute on the MatSidenav component. The default 'over' mode is explained, where the side nav floats over the main content with a backdrop. The 'push' mode is introduced, which pushes the main content aside to make space for the side nav, and the 'side' mode is mentioned, where the side nav appears alongside the main content. The paragraph also covers methods for controlling the side nav, such as invoking the open, close, and toggle methods on the side nav component itself. Lastly, it discusses the possibility of performing actions based on the side nav's opened and closed events, providing an example of logging the state to the console.

Mindmap

Keywords

πŸ’‘Angular Material

Angular Material is a UI framework developed by the Angular team that provides a set of pre-built, well-designed, and accessible components for building web applications. In the video, Angular Material is used to create a side navigation system, showcasing how to import modules and use components like mat sidenav and mat-sidenav-container.

πŸ’‘Module Import

In the context of Angular applications, module import refers to the process of bringing in external libraries or components into the current project. This is essential for using functionalities provided by these libraries. In the video, the script begins with importing the Material module to access components like mat sidenav.

πŸ’‘mat sidenav-container

The mat sidenav-container is a component in Angular Material that serves as a container for the side navigation and the main content of the application. It ensures that the sidenav and the main content are correctly positioned and interact with each other. In the video, it is used to create a structured layout where the sidenav and main content can coexist.

πŸ’‘mat sidenav

The mat sidenav component represents the side navigation part of an application in Angular Material. It is used to create a sidebar that can contain links, buttons, or other navigation elements. In the video, the mat sidenav is configured to be initially hidden and can be shown using different methods.

πŸ’‘Two-way Binding

Two-way binding is a technique in Angular that allows data to be synchronized between a component's property and an element in the template. This means that any changes made to the property or the bound element will be reflected in both places. In the video, two-way binding is used to control the visibility of the sidenav based on the opened property.

πŸ’‘Template Reference Variable

A template reference variable in Angular is a way to get a reference to a DOM element or a component instance in the template. It allows developers to interact with the element or component directly from the component's class. In the video, a template reference variable is used to invoke methods on the mat sidenav component.

πŸ’‘Side Nav Modes

Side Nav Modes refer to the different behaviors or styles of side navigation in Angular Material. The three modes are 'over', 'push', and 'side'. Each mode determines how the sidenav interacts with the main content when it is opened. 'Over' mode allows the sidenav to cover the main content, 'push' mode pushes the main content aside, and 'side' mode places the sidenav alongside the main content.

πŸ’‘Open and Close Methods

The open and close methods are functions that can be called on the mat sidenav component to programmatically control its visibility. The open method is used to show the sidenav, while the close method is used to hide it. These methods provide an alternative to using the opened attribute for managing the sidenav state.

πŸ’‘Toggle Method

The toggle method is a function available on the mat sidenav component that switches its visibility state. If the sidenav is closed, calling the toggle method will open it, and if it is open, calling the toggle method will close it. This method is often used in response to user actions, such as clicking a hamburger icon.

πŸ’‘Events

In Angular, events are occurrences that happen in the application, such as user interactions, that can be listened for and responded to by the application. The script mentions 'opened' and 'closed' events that are emitted by the mat sidenav component when it is opened or closed, allowing developers to perform actions based on these events.

πŸ’‘Styling

Styling in web development refers to the process of applying visual characteristics to elements in the application. This includes colors, fonts, layout properties, and more. In the video, styling is used to enhance the appearance of the sidenav and the main content, such as adding padding, setting background colors, and defining widths.

Highlights

The introduction of Angular Material's side, nav, and angular modules.

The process of importing the module and adding it to the material array.

Setting up a site map using three components: Matt site, nav container, mat side nav, and map site map content.

The default behavior of the side nav being hidden and the method to make it visible using the 'opened' attribute.

Adding basic styling to improve the user interface, including padding, background color, and width adjustments.

Two-way binding to control the side nav's visibility with a button, requiring the forms module.

Exploring different modes for the side nav: over, push, and side.

The 'over' mode, where the side nav floats over the main content with a backdrop.

The 'push' mode, where the side nav pushes the main content aside.

The 'side' mode, where the side nav appears alongside the main content.

Using template reference variables to invoke open or close methods on the side nav component.

The toggle method for the side nav, activated by a button in the main content.

Listening to 'opened' and 'closed' events for the side nav to perform actions upon opening or closing.

Creating a log method to record console messages for side nav events.

The practical application of side nav in Angular Material for building interactive user interfaces.

The conclusion of the video, summarizing the side nav functionalities and encouraging viewers to subscribe for more content.

Transcripts

00:00

in this video let's take a look at side

00:03

nav and angular material the first step

00:06

as always is to import the module so in

00:10

material module dot es import mat side

00:14

nav module and add it to the material

00:17

array now we can add the site map to our

00:21

application

00:23

to set up a site now we use three

00:26

components the first one is Matt site

00:30

nap container which acts as a container

00:34

to the site nav as well as the main

00:37

content within the container we have mat

00:41

side nav

00:44

which represents the site content

00:48

and then we have map site map content

00:53

which represents the main content it is

00:57

important that the site content and the

01:00

main content are placed within the

01:02

container if you don't want some

01:06

elements to be affected by this

01:07

container for example header and footer

01:10

you can place them outside the sitemap

01:13

container all right if we now save the

01:16

file and take a look at the browser the

01:19

output is not quite what we expect

01:23

we can see the main content but the site

01:26

now is not seen let me tell you that

01:30

this is the expected behavior site now

01:34

is always hidden by default there are a

01:38

couple of ways to open or show the side

01:40

nav the simplest way is to add the

01:43

opened attribute on the mat side nav

01:46

component

01:49

if you now say the file and take a look

01:51

at the browser you should be able to see

01:54

the site now as well the UI though is

01:57

not clear so let me add some basic

02:00

styling back in vs code and AB dot

02:04

component dot CSS I'm going to add mat

02:09

side nap container has a height of a

02:12

hundred percent mat side nav and mat

02:17

side nav content let's add some padding

02:20

and finally just for the side nav I'm

02:26

going to add a background color light

02:30

coral and a width of 200 pixels

02:37

if we now take a look at the browser it

02:39

is much more clear the site now appears

02:43

like an overlay on the main content and

02:45

when I click outside the side nav it

02:49

closes and we can see the main content

02:52

now once we close it though there is no

02:55

way to open it again

02:57

so rather than setting the opened

03:00

attribute like here let's bind it to a

03:04

property which we can then control using

03:07

a button we will be using two-way

03:10

binding so the first step is to import

03:13

the forms module in app module so open

03:17

up dot module dot ES

03:21

in Port forms module and add it to the

03:25

imports array

03:27

now let's go back to two-way binding in

03:31

AB dot component dot es which is the

03:34

component class I'm going to create a

03:36

new property called opened and set it

03:40

defaults next in the HTML bind the

03:46

opened attribute to the opened property

03:48

and create a button that will toggle the

03:52

opened property value so for two-way

03:55

binding we use banana in a box in tax so

03:59

square brackets parenthesis and close it

04:02

now this is going to be equal to the

04:05

property we just created opened next as

04:09

part of the main content I'm going to

04:11

add a button the text is going to be

04:15

toggle opened and on click of this

04:19

button so click handler we simply toggle

04:23

the open value

04:25

opened is equal to not off opened if you

04:30

now say the files and take a look at the

04:31

browser the site now should be initially

04:35

hidden because we have initialized it to

04:37

false I click on the button and the side

04:41

nav is shown click outside the side nav

04:44

and it automatically hides so this is

04:47

the first method to show or hide the

04:50

side nav before we take a look at the

04:53

next method I want to quickly show you

04:56

the different modes that can be applied

04:58

to a side nav the mode for a side nav

05:02

can be specified using the mode

05:05

attribute on the mat side nav component

05:08

the default mode is over mode is equal

05:13

to over and this is the mode we have

05:17

seen so far if you go back to the

05:20

browser you can see that there is no

05:23

change from what we have already seen

05:25

the site now floats over the main

05:27

content which is covered by a backdrop

05:31

the second possible value for the mold

05:34

attribute is push so I'm going to change

05:37

over to push let's see how this works in

05:41

the browser I clicked on the button the

05:46

site map appears but this time the site

05:49

map pushes the primary content out of

05:51

its way covering it with a backdrop the

05:56

final possible mode is side so mode is

05:59

equal to side

06:02

in this mode the side nav appears

06:05

side-by-side with the main content

06:08

shrinking the main contents width to

06:11

make space for the side nav so the three

06:15

possible modes are over push and side

06:19

I'm going to stick with site for the

06:21

rest of this video now back to opening

06:25

and closing the side nav we have seen

06:28

the first method which is using the

06:30

opened attribute the second way to open

06:33

or close the side nav is by invoking the

06:36

open or close methods on the side nav

06:40

itself for this method I will create a

06:43

template reference variable on the mat

06:46

side nav component hash symbol and side

06:50

nav now I can create two buttons that

06:54

will call the open and close methods on

06:57

this template reference variable button

07:01

open on click it is going to call side

07:05

nav dot open

07:08

similarly button close on click side nav

07:16

dot close let's save this file and go

07:20

back to the browser I click on open and

07:23

the side nav opens I click on close and

07:26

the side nav closes so this is the

07:30

second method the third method is to

07:34

simply toggle the side nav by calling

07:37

the toggle method this is also the

07:40

approach you might want to take when you

07:43

have a hamburger icon for example I will

07:46

create another button in the main

07:49

content and the text is going to be

07:52

toggle

07:54

on click of this button I simply call

07:57

the toggle method on the side nav

08:00

reference variable side nav dot toggle

08:04

if I now go back to the browser click on

08:08

the toggle button it opens I click again

08:12

it closes so these are the three methods

08:16

and if at all you want to perform some

08:19

action on open or close off the side nav

08:22

you can listen to the opened and closed

08:26

events now back in vs code in AB

08:30

component Diaz I am going to create a

08:33

method called log which accepts a

08:37

parameter called state and simply logs

08:41

that to the console

08:44

now in the HTML we can listen to opened

08:49

and closed events and call our log

08:52

method passing in the appropriate value

08:55

so on mat side nav listen to opened

08:59

event

09:01

and call the log method passing in the

09:05

string opened similarly on the closed

09:09

event called the method passing in the

09:14

string closed if you now head back to

09:17

the browser open dev tools click on

09:22

toggle you should see the message opened

09:25

toggle again you should see the message

09:28

closed now I am simply logging a message

09:33

but you can perform any action that

09:35

seems fit for your application so that

09:40

is about the side nav in angular

09:42

material thank you guys for watching

09:44

feel free to subscribe I'll see you guys

09:46

in the next video

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Angular MaterialSide NavigationWeb DevelopmentInteractive TutorialUI/UX DesignFrontend FrameworkComponent LibraryResponsive DesignCoding TutorialWeb Accessibility