You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -25,6 +25,11 @@ A simple menu is usually closed, appearing when opened. It is appropriate for an
25
25
</ul>
26
26
</div>
27
27
```
28
+
> Note: adding a `tabindex` of `0` to the menu items places them in the tab order.
29
+
Adding a `tabindex` of `-1` to the root element makes it programmatically focusable, without
30
+
placing it in the tab order. This allows the menu to be focused on open, so that the next Tab
31
+
keypress moves to the first menu item. If you would like the first menu item to be automatically
32
+
focused instead, remove `tabindex="-1"` from the root element.
28
33
29
34
```js
30
35
let menu =newmdl.SimpleMenu(document.querySelector('.mdl-simple-menu'));
@@ -61,6 +66,26 @@ menu.open = true;
61
66
menu.open=false;
62
67
```
63
68
69
+
It also has two lower level methods, which control the menu directly, by showing (opening) and
70
+
hiding (closing) it:
71
+
72
+
```js
73
+
// Show (open) menu.
74
+
menu.show();
75
+
// Hide (close) menu.
76
+
menu.hide();
77
+
// Show (open) menu, and focus the menu item at index 1.
78
+
menu.show({focusIndex:1});
79
+
```
80
+
81
+
You can still use the `open` getter property even if showing and hiding directly:
82
+
83
+
```js
84
+
menu.show();
85
+
console.log(`Menu is ${menu.open?'open':'closed'}.`);
86
+
```
87
+
88
+
64
89
#### Including in code
65
90
66
91
##### ES2015
@@ -122,6 +147,8 @@ with the following `detail` data:
122
147
|`item`|`HTMLElement`| The DOM element for the selected item |
123
148
|`index`|`number`| The index of the selected item |
124
149
150
+
If the menu is closed with no selection made (for example, if the user hits `Escape` while it's open), a `MDLSimpleMenu:cancel` custom event is emitted instead, with no data attached.
151
+
125
152
### Using the Foundation Class
126
153
127
154
MDL Simple Menu ships with an `MDLSimpleMenuFoundation` class that external frameworks and libraries can use to
@@ -140,7 +167,16 @@ The adapter for temporary drawers must provide the following functions, with cor
140
167
|`getNumberOfItems() => numbers`| Returns the number of _item_ elements inside the items container. In our vanilla component, we determine this by counting the number of list items whose `role` attribute corresponds to the correct child role of the role present on the menu list element. For example, if the list element has a role of `menu` this queries for all elements that have a role of `menuitem`. |
141
168
|`registerInteractionHandler(type: string, handler: EventListener) => void`| Adds an event listener `handler` for event type `type`. |
142
169
|`deregisterInteractionHandler(type: string, handler: EventListener) => void`| Removes an event listener `handler` for event type `type`. |
170
+
|`registerDocumentClickHandler(handler: EventListener) => void`| Adds an event listener `handler` for event type 'click'. |
171
+
|`deregisterDocumentClickHandler(handler: EventListener) => void`| Removes an event listener `handler` for event type 'click'. |
143
172
|`getYParamsForItemAtIndex(index: number) => {top: number, height: number}`| Returns an object with the offset top and offset height values for the _item_ element inside the items container at the provided index. Note that this is an index into the list of _item_ elements, and not necessarily every child element of the list. |
144
173
|`setTransitionDelayForItemAtIndex(index: number, value: string) => void`| Sets the transition delay on the element inside the items container at the provided index to the provided value. The same notice for `index` applies here as above. |
145
174
|`getIndexForEventTarget(target: EventTarget) => number`| Checks to see if the `target` of an event pertains to one of the menu items, and if so returns the index of that item. Returns -1 if the target is not one of the menu items. The same notice for `index` applies here as above. |
146
175
|`notifySelected(evtData: {index: number}) => void`| Dispatches an event notifying listeners that a menu item has been selected. The function should accept an `evtData` parameter containing the an object with an `index` property representing the index of the selected item. Implementations may choose to supplement this data with additional data, such as the item itself. |
176
+
|`notifyCancel() => void`| Dispatches an event notifying listeners that the menu has been closed with no selection made. |
177
+
|`saveFocus() => void`| Stores the currently focused element on the document, for restoring with `restoreFocus`. |
178
+
|`restoreFocus() => void`| Restores the previously saved focus state, by making the previously focused element the active focus again. |
179
+
|`isFocused() => boolean`| Returns a boolean value indicating whether the root element of the simple menu is focused. |
180
+
|`focus() => void`| Focuses the root element of the simple menu. |
181
+
|`getFocusedItemIndex() => number`| Returns the index of the currently focused menu item (-1 if none). |
182
+
|`focusItemAtIndex(index: number) => void`| Focuses the menu item with the provided index. |
0 commit comments