Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,020 --> 00:00:08,520
In this video we're really going to star and polish off the header by adding a cool feature that I think
2
00:00:08,520 --> 00:00:11,940
is pretty common in a lot of headers.
3
00:00:11,960 --> 00:00:20,390
This is the feature where when you hover over a certain tab in our case such as the services tab it
4
00:00:20,480 --> 00:00:30,320
then will open up a menu displaying the further pages within that material UI provides a menu component
5
00:00:30,320 --> 00:00:38,510
for us to help with a lot of the functionality behind this such as showing and hiding the menu styling
6
00:00:38,510 --> 00:00:47,130
the menu items and keeping track of which menu item is selected so just like before we're going to go
7
00:00:47,130 --> 00:00:54,930
ahead and jump in to the documentation to see how they set this up and how this provided functionality
8
00:00:54,930 --> 00:00:55,650
works.
9
00:00:55,650 --> 00:01:02,490
And then in the next video we're going to jump in to implementing it in our application.
10
00:01:02,530 --> 00:01:02,960
All right.
11
00:01:02,960 --> 00:01:12,550
And here we are at the material UI documentation page under components navigation and menus.
12
00:01:12,620 --> 00:01:22,320
And here you can see their definition of a menu is that it displays a list of choices on temporary surfaces.
13
00:01:22,340 --> 00:01:28,430
So that's the technical way of basically saying it appears for a little bit and lets you do something
14
00:01:28,430 --> 00:01:30,950
and then it disappears.
15
00:01:30,960 --> 00:01:38,940
This is a very common component and task not only in headers like we'll be using it for but all over
16
00:01:38,970 --> 00:01:46,040
all sorts of different applications if we start and take a look at their simple menu we can see that
17
00:01:46,090 --> 00:01:52,120
if we press this button they have a little just text button here for us and we go ahead and press that
18
00:01:52,330 --> 00:01:54,230
bam we've got a little menu.
19
00:01:54,310 --> 00:02:01,210
And so this gives us a little styling for our hovering over each option as well as when you select an
20
00:02:01,210 --> 00:02:04,490
option it then it closes the menu.
21
00:02:04,570 --> 00:02:07,000
This is probably extremely familiar to you.
22
00:02:07,000 --> 00:02:12,600
I'm sure you can think of a billion different times that you've seen this or interacted with menus.
23
00:02:12,700 --> 00:02:19,840
And so obviously it's pretty useful to have if we come and take a look at the source code required for
24
00:02:19,840 --> 00:02:20,440
this.
25
00:02:20,440 --> 00:02:29,980
We can see at the top they are main important imports are the menu and the menu item and then they just
26
00:02:29,980 --> 00:02:37,940
use a button down here to actually trigger opening the menu so you can see here the implementation is
27
00:02:37,940 --> 00:02:39,100
very simple.
28
00:02:39,160 --> 00:02:50,110
It is just a menu component here wrapping all of our menu items each menu item has in on click prop
29
00:02:50,410 --> 00:02:53,980
which then is pass the handle closed function.
30
00:02:54,130 --> 00:03:02,140
And if we look up here we can see that all that does is it takes and uses are set anchor element hook
31
00:03:02,380 --> 00:03:06,210
to Noel So what's going on there.
32
00:03:06,210 --> 00:03:14,250
As you can see up here at the top we set an anchor element and a set anchor element react hook and default
33
00:03:14,250 --> 00:03:23,910
that state to know and the anchor element is then passed to the menu as a prop the anchor element essentially
34
00:03:23,910 --> 00:03:33,630
tells the menu where to show itself on the screen and which component to latch on to on the DOM whether
35
00:03:33,630 --> 00:03:41,760
or not the menu is being displayed is controlled by the open prop and that creates a boolean out of
36
00:03:41,760 --> 00:03:43,230
the anchor element.
37
00:03:43,230 --> 00:03:51,620
So if there is an element there that it is attached to the boolean call will result in true.
38
00:03:52,170 --> 00:04:00,410
Whereas if the anchor element is null the boolean function will result in false when the anchor element
39
00:04:00,500 --> 00:04:08,990
is present in the boolean function returns a true creating a true value for our open prop the menu is
40
00:04:08,990 --> 00:04:17,240
displayed and you can see here on close set to the handle closed function which is setting our anchor
41
00:04:17,240 --> 00:04:26,030
element to normal which then creates this boolean function to return a false value setting of the Open
42
00:04:26,180 --> 00:04:30,620
Value to false and then closing the menu.
43
00:04:30,700 --> 00:04:40,480
You see we have the handle close for the on close prop on menu as well as for the on click prop on menu
44
00:04:40,510 --> 00:04:41,240
items.
45
00:04:41,380 --> 00:04:49,030
Because whether we click on a menu item and navigate to a new page or select a new option we then want
46
00:04:49,030 --> 00:04:55,480
the menu to be closed or if we simply closed the menu by clicking on the button again which triggers
47
00:04:55,480 --> 00:05:03,190
the on clothes handler on the menu we also just want the menu to close this other prop here.
48
00:05:03,210 --> 00:05:11,820
Keep mounted essentially tells the children elements of the menu in this case all of our menu items
49
00:05:12,210 --> 00:05:20,270
to stay mounted on the DRM whether or not it's actually visible on the screen.
50
00:05:20,300 --> 00:05:26,600
This could be useful for search engine optimization or other situations where you don't actually want
51
00:05:26,660 --> 00:05:30,030
that information or data to disappear.
52
00:05:30,050 --> 00:05:36,900
The other prop on menu is this idea and that is provided the simple menu string.
53
00:05:37,280 --> 00:05:44,120
And the reason we pass that I.D. is so there on our button up here which actually triggers the showing
54
00:05:44,120 --> 00:05:52,730
of our menu on the button we set some Aria controls which again are for web accessibility and people
55
00:05:52,730 --> 00:06:00,680
who are browsing the internet on specialized devices need to be told if there's a menu popping up on
56
00:06:00,680 --> 00:06:01,870
the screen or not.
57
00:06:01,970 --> 00:06:05,930
So that it can read off the options to that effect.
58
00:06:05,940 --> 00:06:15,530
We set the RBA controls equal to the menu item that is managing or that the button is triggering.
59
00:06:15,540 --> 00:06:17,310
In other words.
60
00:06:17,310 --> 00:06:25,740
So that is why we set the same string of simple dash menu both for the RBA controls on the button as
61
00:06:25,740 --> 00:06:31,820
well as for the idea on the menu linking the two together.
62
00:06:31,970 --> 00:06:40,430
The second part in that process is the RBA Dash has pop up equals true prop which again we just set
63
00:06:40,430 --> 00:06:48,410
to make sure that any screen reader or other specialized devices are aware of the fact that this button
64
00:06:48,590 --> 00:06:56,680
is triggering a pop up menu the last step to this of course is the actual on click handler where we're
65
00:06:56,700 --> 00:06:59,520
calling the handle click function.
66
00:06:59,580 --> 00:07:07,410
This is where the Click event is being passed through to our function and we use set anchor element
67
00:07:07,710 --> 00:07:15,690
to set the event dot current target which specifies the element that we have selected and sets that
68
00:07:15,690 --> 00:07:24,320
to our anger elements state which is then updates the menu to be anchored to the selected element.
69
00:07:24,330 --> 00:07:26,420
In this case our button.
70
00:07:26,580 --> 00:07:29,820
So I think that that makes pretty good sense.
71
00:07:29,830 --> 00:07:31,420
It's not too crazy.
72
00:07:31,530 --> 00:07:39,120
We're really just specifying on the button the on click handler to set our anchor element to the button
73
00:07:39,150 --> 00:07:47,040
when we click it which then opens our menu over that element and we can then select an element which
74
00:07:47,130 --> 00:07:52,860
triggers the on click handler on the menu items to close the menu.
75
00:07:52,870 --> 00:07:57,080
So this is just simply opening and showing the menu.
76
00:07:57,160 --> 00:08:00,940
But if we want to add a little bit of extra functionality to it.
77
00:08:01,000 --> 00:08:05,170
We'll actually be using the selected menus.
78
00:08:05,340 --> 00:08:13,150
We are going to be using this in our application and it allows you to keep track of which option is
79
00:08:13,150 --> 00:08:14,730
currently selected.
80
00:08:14,800 --> 00:08:22,090
You can see it not only do we have the hover effect for the other options but then we have this darker
81
00:08:22,150 --> 00:08:26,080
styling for the one that is already selected.
82
00:08:26,080 --> 00:08:33,530
So if we go we can choose another one and you see it is keeping track let's take a look at how they're
83
00:08:33,530 --> 00:08:43,670
doing this with the source code and you can see if we scroll down here and if we keep going down to
84
00:08:43,700 --> 00:08:48,560
where they have the menu here at the bottom you can see what they're doing.
85
00:08:48,560 --> 00:08:56,930
So here is our opening menu component with all of the familiar props from the last example but now instead
86
00:08:56,930 --> 00:09:07,390
of rendering each menu item explicitly they're actually mapping over a list of the different menu options.
87
00:09:07,410 --> 00:09:15,870
This allows them to keep track of the index of which option they are currently iterating over and managing
88
00:09:15,900 --> 00:09:25,440
a selected index piece of state to compare to the index of the currently iterated menu item and determine
89
00:09:25,470 --> 00:09:35,000
whether or not it is the item selected let's actually come up and take a look at the functions up here
90
00:09:35,030 --> 00:09:38,610
and hopefully this will make a little more sense than just talking about it.
91
00:09:38,690 --> 00:09:46,610
And so you can see that we have our anchor element and set anchor element state like we did in the previous
92
00:09:46,610 --> 00:09:51,160
example just telling the menu where to render itself.
93
00:09:51,200 --> 00:09:58,970
But now we also have the selected index and the set selected index state for managing which item we
94
00:09:58,970 --> 00:10:01,150
have clicked on.
95
00:10:01,160 --> 00:10:10,790
So now we see our handle menu item click function that takes an event in an index and sets the selected
96
00:10:10,790 --> 00:10:20,300
index to whichever index is provided while also then emptying the anchor element and clearing the menu
97
00:10:20,300 --> 00:10:21,290
off the screen.
98
00:10:22,610 --> 00:10:30,590
So if we come down here and we take another look at this you can see that when a menu item is clicked
99
00:10:30,710 --> 00:10:41,370
so on click it takes that event back click event and it passes it to our handle menu item click hook.
100
00:10:41,690 --> 00:10:49,010
And like I said earlier since we are now iterating through a list of menu items we also have access
101
00:10:49,190 --> 00:10:56,480
to the current index of each menu item and we can pass that as well to determine whether or not that
102
00:10:56,480 --> 00:11:05,060
is the item we've clicked on before that index past to the on click handler is then used to set our
103
00:11:05,060 --> 00:11:07,550
selected index at the state.
104
00:11:07,550 --> 00:11:16,580
And once that updates then the selected prop on the menu item will update to determine whether or not
105
00:11:16,610 --> 00:11:25,370
the selected index piece of state is equal to the currently iterated index of the menu item to enable
106
00:11:25,370 --> 00:11:26,420
this approach.
107
00:11:26,420 --> 00:11:34,430
They had to come up here and define an array of options with each element in the array or being the
108
00:11:34,430 --> 00:11:39,020
string that they want to have displayed for the menu item.
109
00:11:39,050 --> 00:11:41,530
I think that that makes a lot more sense now.
110
00:11:41,570 --> 00:11:49,340
When you see this knowing the each of these menu items is actually being iterated over and having the
111
00:11:49,400 --> 00:11:56,170
seeing elected option being managed we are going to be implementing this in our application.
112
00:11:56,170 --> 00:11:59,620
So if that still seems a little confusing don't worry.
113
00:11:59,740 --> 00:12:03,430
I think it'll make a lot more sense once we go through and hook it up.
13161
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.