@@ -10,68 +10,65 @@ directives apply their associated ARIA roles to their host element.
10
10
The directives in ` @angular/cdk/menu ` set the appropriate roles on their host element.
11
11
12
12
| Directive | ARIA Role |
13
- | ------------------- | ---------------- |
13
+ | --------------------- | ------------------ |
14
14
| CdkMenuBar | menubar |
15
15
| CdkMenu | menu |
16
16
| CdkMenuGroup | group |
17
17
| CdkMenuItem | menuitem |
18
18
| CdkMenuItemRadio | menuitemradio |
19
19
| CdkMenuItemCheckbox | menuitemcheckbox |
20
+ | CdkMenuTrigger | button |
20
21
21
22
### Getting started
22
23
23
24
Import the ` CdkMenuModule ` into the ` NgModule ` in which you want to create menus. You can then apply
24
25
menu directives to build your custom menu. A typical menu consists of the following directives:
25
26
26
- - ` cdkMenuTriggerFor ` - links a trigger button to a menu you intend to open
27
- - ` cdkMenuPanel ` - wraps the menu and provides a link between the ` cdkMenuTriggerFor ` and the
28
- ` cdkMenu `
29
- - ` cdkMenu ` - the actual menu you want to open
30
- - ` cdkMenuItem ` - added to each button
27
+ - ` cdkMenuTriggerFor ` - links a trigger element to an ` ng-template ` containing the menu the trigger opens
28
+ - ` cdkMenu ` - creates the actual menu content that the trigger will open
29
+ - ` cdkMenuItem ` - added to each item in the menu
31
30
32
31
<!-- example({
33
- "example": "cdk-menu-standalone-menu ",
34
- "file": "cdk-menu-standalone-menu -example.html"
32
+ "example": "cdk-menu-standalone-trigger ",
33
+ "file": "cdk-menu-standalone-trigger -example.html"
35
34
}) -->
36
35
37
36
Most menu interactions consist of two parts: a trigger and a menu panel.
38
37
39
38
#### Triggers
40
39
41
- You must add the ` cdkMenuItem ` and ` cdkMenuTriggerFor ` directives to triggers like so,
40
+ ` cdkMenuTriggerFor ` can be added to any button to make it a trigger for the given menu, or any menu
41
+ item to make it a trigger for a submenu. When adding this directive, be sure to pass a reference to
42
+ the template containing the menu it should open. You can toggle the associated menu using a mouse
43
+ or keyboard.
42
44
43
- ``` html
44
- < button cdkMenuItem [cdkMenuTriggerFor] = " menu" >Click me!</ button >
45
- ```
45
+ <!-- example({"example":"cdk-menu-standalone-trigger",
46
+ "file":"cdk- menu-standalone-trigger-example.html",
47
+ "region":"trigger"}) -->
46
48
47
- Adding ` cdkMenuItem ` gives you keyboard navigation and focus management. Associating a trigger with
48
- a menu is done through the ` cdkMenuTriggerFor ` directive and you must provide a template reference
49
- variable to it. Once both of these directives are set, you can toggle the associated menu
50
- programmatically, using a mouse or using a keyboard.
49
+ When creating a submenu trigger, add both ` cdkMenuItem ` and ` cdkMenuTriggerFor ` like so,
51
50
52
- #### Menu panels
51
+ <!-- example({"example":"cdk-menu-menubar",
52
+ "file":"cdk-menu-menubar-example.html",
53
+ "region":"file-trigger"}) -->
53
54
54
- You must wrap pop-up menus with an ` ng-template ` with the ` cdkMenuPanel ` directive and a reference
55
- variable which must be of type ` cdkMenuPanel ` . Further, the ` cdkMenu ` must also reference the
56
- ` cdkMenuPanel ` .
55
+ #### Menu content
57
56
58
- ``` html
59
- <ng-template cdkMenuPanel #panel =" cdkMenuPanel" >
60
- <div cdkMenu [cdkMenuPanel] =" panel" >
61
- <!-- some content -->
62
- </div >
63
- </ng-template >
64
- ```
57
+ Menu content is created using the ` cdkMenu ` or ` cdkMenuBar ` components. Menus can be always present
58
+ on the page (referred to as an inline menu), or shown on trigger activation (referred to as a popup
59
+ menu). Popup menus must be wrapped in an ` ng-template ` , as a reference to the ng-template is needed
60
+ to pass to the trigger directive.
61
+
62
+ Menus should exclusively contain elements with role ` menuitem ` , ` menuitemcheckbox ` ,
63
+ ` menuitemradio ` , or ` group ` . Supporting directives that automatically apply these roles are
64
+ discussed below.
65
65
66
66
Note that Angular CDK provides no styles; you must add styles as part of building your custom menu.
67
67
68
68
### Menu Bars
69
69
70
70
The ` CdkMenuBar ` directive follows the [ ARIA menubar] [ menubar ] spec and behaves similar to a desktop
71
- app menubar. It consists of at least one ` CdkMenuItem ` which triggers a submenu. A menubar can be
72
- layed out horizontally or vertically (defaulting to horizontal). If the layout changes, you must set
73
- the ` orientation ` attribute to match in order for the keyboard navigation to work properly and for
74
- menus to open up in the correct location.
71
+ app menubar. It consists of at least one ` CdkMenuItem ` which triggers a submenu.
75
72
76
73
<!-- example({
77
74
"example": "cdk-menu-menubar",
@@ -93,22 +90,21 @@ the cursor, similarly to native context menus.
93
90
You can nest context menu container elements. Upon right-click, the menu associated with the closest
94
91
container element will open.
95
92
96
- ``` html
97
- <div [cdkContextMenuTriggerFor] =" outer" >
98
- My outer context
99
- <div [cdkContextMenuTriggerFor] =" inner" >My inner context</div >
100
- </div >
101
- ```
93
+ <!-- example({
94
+ "example": "cdk-menu-nested-context",
95
+ "file": "cdk-menu-nested-context-example.html",
96
+ "region": "triggers"
97
+ }) -->
102
98
103
- In the example above, right clicking on "My inner context" will open up the "inner" menu and right
104
- clicking inside "My outer context" will open up the "outer" menu.
99
+ In the example above, right- clicking on "Inner context menu " will open up the "inner" menu and
100
+ right- clicking inside "Outer context menu " will open up the "outer" menu.
105
101
106
102
### Inline Menus
107
103
108
104
An _ inline menu_ is a menu that lives directly on the page rather than a pop-up associated with a
109
105
trigger. You can use an inline menu when you want a persistent menu interaction on a page. Menu
110
- items within an inline menus are logically grouped together and you can navigate through them using
111
- your keyboard.
106
+ items within an inline menus are logically grouped together, and you can navigate through them
107
+ using your keyboard.
112
108
113
109
<!-- example({
114
110
"example": "cdk-menu-inline",
@@ -117,45 +113,47 @@ your keyboard.
117
113
118
114
### Menu Items
119
115
120
- Both menu and menubar elements should exclusively contain menuitem elements. This directive allows
121
- the items to be navigated to via keyboard interaction .
116
+ The ` cdkMenuItem ` directive allows the items to be navigated to via keyboard interaction.
117
+ A menuitem can provide some user defined action by hooking into the ` cdkMenuItemTriggered ` output .
122
118
123
- A menuitem by itself can provide some user defined action by hooking into the ` cdkMenuItemTriggered `
124
- output. An example may be a close button which performs some closing logic.
125
-
126
- ``` html
127
- <ng-template cdkMenuPanel #panel =" cdkMenuPanel" >
128
- <div cdkMenu [cdkMenuPanel] =" panel" >
129
- <button cdkMenuItem (cdkMenuItemTriggered) =" closeApp()" >Close</button >
130
- </div >
131
- </ng-template >
132
- ```
119
+ <!-- example({"example":"cdk-menu-standalone-stateful-menu",
120
+ "file":"cdk-menu-standalone-stateful-menu-example.html",
121
+ "region":"reset-item"}) -->
133
122
134
123
You can create nested menus by using a menuitem as the trigger for another menu.
135
124
136
- ``` html
137
- <ng-template cdkMenuPanel #panel =" cdkMenuPanel" >
138
- <div cdkMenu [cdkMenuPanel] =" panel" >
139
- <button cdkMenuItem [cdkMenuTriggerFor] =" submenu" >Open Submenu</button >
140
- </div >
141
- </ng-template >
142
- ```
143
-
144
- A menuitem also has two sub-types, neither of which should trigger a menu: CdkMenuItemCheckbox and
145
- CdkMenuItemRadio
125
+ <!-- example({"example":"cdk-menu-menubar",
126
+ "file":"cdk-menu-menubar-example.html",
127
+ "region":"file-trigger"}) -->
146
128
147
129
#### Menu Item Checkboxes
148
130
149
131
A ` cdkMenuItemCheckbox ` is a special type of menuitem that behaves as a checkbox. You can use this
150
- type of menuitem to toggle items on and off. An element with the ` cdkMenuItemCheckbox ` directive
132
+ type of menuitem to toggle items on and off. An element with ` cdkMenuItemCheckbox ` directive
151
133
does not need the additional ` cdkMenuItem ` directive.
152
134
135
+ Checkbox items do not track their own state. You must bind the checked state using the
136
+ ` cdkMenuItemChecked ` input and listen to ` cdkMenuItemTriggered ` to know when it is toggled. If you
137
+ don't bind the state it will reset when the menu is closed and re-opened.
138
+
139
+ <!-- example({"example":"cdk-menu-standalone-stateful-menu",
140
+ "file":"cdk-menu-standalone-stateful-menu-example.html",
141
+ "region":"bold-item"}) -->
142
+
153
143
#### Menu Item Radios
154
144
155
145
A ` cdkMenuItemRadio ` is a special type of menuitem that behaves as a radio button. You can use this
156
146
type of menuitem for menus with exclusively selectable items. An element with the ` cdkMenuItemRadio `
157
147
directive does not need the additional ` cdkMenuItem ` directive.
158
148
149
+ As with checkbox items, radio items do not track their own state, but you can track it by binding
150
+ ` cdkMenuItemChecked ` and listening for ` cdkMenuItemTriggered ` . If you do not bind the state the
151
+ selection will reset when the menu is closed and reopened.
152
+
153
+ <!-- example({"example":"cdk-menu-standalone-stateful-menu",
154
+ "file":"cdk-menu-standalone-stateful-menu-example.html",
155
+ "region":"size-items"}) -->
156
+
159
157
#### Groups
160
158
161
159
By default ` cdkMenu ` acts as a group for ` cdkMenuItemRadio ` elements. Elements with
@@ -165,30 +163,6 @@ can have the checked state.
165
163
If you would like to have unrelated groups of radio buttons within a single menu you should use the
166
164
` cdkMenuGroup ` directive.
167
165
168
- ``` html
169
- <ng-template cdkMenuPanel #panel =" cdkMenuPanel" >
170
- <div cdkMenu [cdkMenuPanel] =" panel" >
171
- <!-- Font size -->
172
- <div cdkMenuGroup >
173
- <button cdkMenuItemRadio >Small</button >
174
- <button cdkMenuItemRadio >Medium</button >
175
- <button cdkMenuItemRadio >Large</button >
176
- </div >
177
- <hr />
178
- <!-- Paragraph alignment -->
179
- <div cdkMenuGroup >
180
- <button cdkMenuItemRadio >Left</button >
181
- <button cdkMenuItemRadio >Center</button >
182
- <button cdkMenuItemRadio >Right</button >
183
- </div >
184
- </div >
185
- </ng-template >
186
- ```
187
-
188
- Note however that when the menu is closed and reopened any state is lost. You must subscribe to the
189
- groups ` change ` output, or to ` cdkMenuItemToggled ` on each radio item and track changes your self.
190
- Finally, you can provide state for each item using the ` checked ` attribute.
191
-
192
166
<!-- example({
193
167
"example": "cdk-menu-standalone-stateful-menu",
194
168
"file": "cdk-menu-standalone-stateful-menu-example.html"
@@ -208,8 +182,8 @@ service if it can perform its close actions. In order to determine if the curren
208
182
closed out, the Menu Aim service calculates the slope between a selected target coordinate in the
209
183
submenu and the previous mouse point, and the slope between the target and the current mouse point.
210
184
If the slope of the current mouse point is greater than the slope of the previous that means the
211
- user is moving towards the submenu and we shouldn't close out. Users however may sometimes stop
212
- short in a sibling item after moving towards the submenu. The service is intelligent enough the
185
+ user is moving towards the submenu, so we shouldn't close out. Users however may sometimes stop
186
+ short in a sibling item after moving towards the submenu. The service is intelligent enough to
213
187
detect this intention and will trigger the next menu.
214
188
215
189
### Accessibility
@@ -227,4 +201,4 @@ Finally, keyboard interaction is supported as defined in the [ARIA menubar keybo
227
201
[ keyboard] :
228
202
https://www.w3.org/TR/wai-aria-practices-1.1/#keyboard-interaction-12
229
203
'ARIA Menubar Keyboard Interaction'
230
- [ diagram ] : menuaim.png ' Menu Aim Diagram '
204
+ [ diagram ] : https://material.angular.io/assets/img/ menuaim.png ' Menu Aim Diagram '
0 commit comments