1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<div class="intro">
<p>
This example illustrates how to use the Focus Manager Node Plugin,
Event's <a href="{{apiDocs}}/classes/YUI.html#event_delegate">delegation support</a> and
<a href="{{apiDocs}}/classes/YUI.html#event_mouseenter">mouseenter</a> event, along with
the <a href="../overlay/index.html">Overlay widget</a> and Node's support for the
<a href="http://www.w3.org/TR/wai-aria/">WAI-ARIA Roles and States</a> to
create an accessible menu button.
</p>
</div>
<div class="example">
<style scoped>
/* The following two styles are necessary to override style rules in the
YUI CSS file. */
.example ul {
margin: 0;
}
.example a:hover {
text-decoration: none;
}
/* Hide the button and list while it is being transformed into a menu button. */
.yui3-js-enabled .yui3-menubutton-loading #menu-1,
.yui3-js-enabled .yui3-menubutton-loading #button-1 {
display: none;
}
</style>
{{>node-focusmanager-3-source}}
</div>
<h2>Setting Up the HTML</h2>
<p>
For a menu button, start with an `<a>` element whose
`href` attribute is set to the id of an `<div>`
that wraps a list of `<input>` elements.
Therefore, without JavaScript and CSS, the menu button is simply an in-page
link to a set of additional buttons.
</p>
```
<div class="yui3-menubutton-loading">
<a id="button-1" href="#menu-1"><span><span>Move To</span></span></a>
<div id="menu-1">
<ul>
<li><input type="button" name="button-1" value="Inbox"></li>
<li><input type="button" name="button-2" value="Archive"></li>
<li><input type="button" name="button-3" value="Trash"></li>
</ul>
</div>
</div>
```
<h2>Progressive Enhancement</h2>
<p>
To account for the scenario where the user has CSS enabled in their browser but JavaScript
is disabled, the CSS used to style the menu button will be loaded via JavaScript
using the YUI instance's <a href="../yui/index.html#loader">built-in Loader</a>.
</p>
```
YUI({
modules: {
"menubuttoncss": {
type: "css",
fullpath: "{{componentAssets}}/menubutton.css"
},
"menubuttonjs": {
type: "js",
fullpath: "{{componentAssets}}/menubutton.js",
requires: ["menubuttoncss", "node-focusmanager", "node-event-simulate", "overlay"]
}
}
}).use("menubuttonjs");
```
<p>
To prevent the user from seeing a flash unstyled content when JavaScript is enabled,
a style rule is created using YUI's `yui3-js-enabled` class name that will temporarily
hide the markup while the JavaScript and CSS are in the process of loading. For more on using the
`yui3-js-enabled` class name, see the
<a href="../widget/index.html#progressive">Hiding Progressively Enhanced Markup</a> section of the
<a href="../widget/index.html">YUI Widget landing page</a>.
</p>
```
/* Hide the button and list while it is being transformed into a menu button. */
.yui3-js-enabled .yui3-menubutton-loading #menu-1,
.yui3-js-enabled .yui3-menubutton-loading #button-1 {
display: none;
}
```
<h2>ARIA Support</h2>
<p>
Through the use of CSS and JavaScript the HTML for the menu button can be
transformed into something that looks and behaves like a desktop menu button,
but users of screen readers won't perceive it as an atomic widget, but rather
simply as a set of HTML elements. However, through the application
of the
<a href="http://www.w3.org/TR/wai-aria/">WAI-ARIA Roles and States</a>, it is
possible to improve the semantics of the markup such that users of screen
readers perceive it as a menu button control.
</p>
<h2>Keyboard Functionality</h2>
<p>
The keyboard functionality for the button's menu will be provided by the Focus
Manager Node Plugin. The Focus Manager's
<a href="{{apiDocs}}/classes/plugin.NodeFocusManager.html#attr_descendants">`descendants`</a>
attribute is set to a value of "input", so that only one menuitem in the
button's menu is in the browser's default tab flow. This allows users
navigating via the keyboard to use the tab key to quickly move into and out of
the menu. Once the menu has focus, the user can move focus among each menuitem
using the up and down arrows keys, as defined by the value of the
<a href="{{apiDocs}}/classes/plugin.NodeFocusManager.html#attr_keys">`keys`</a>
attribute. The
<a href="{{apiDocs}}/classes/plugin.NodeFocusManager.html#attr_focusClass">`focusClass`</a>
attribute is used to apply a class of `yui-menuitem-active` to
the parent `<li>` of each
`<input>` when it is focused, making it easy to style the
menuitem's focused state in all browsers.
```
YUI().use("*", function (Y) {
var menuButton = Y.one("#button-1"),
menu;
var initMenu = function () {
menu = new Y.Overlay({
contentBox: "#menu-1",
visible: false,
tabIndex: null
});
menu.render();
Y.one("#menu-1").setStyle("display", "");
var boundingBox = menu.get("boundingBox"),
contentBox = menu.get("contentBox");
boundingBox.addClass("yui3-buttonmenu");
contentBox.addClass("yui3-buttonmenu-content");
// Append a decorator element to the bounding box to render the shadow.
boundingBox.append('<div class="yui3-menu-shadow"></div>');
// Apply the ARIA roles, states and properties to the menu.
role: "menu",
"aria-labelledby": menuLabelID
});
boundingBox.all("input").set("role", "menuitem");
// For NVDA: Add the role of "presentation" to each LI
// element to prevent NVDA from announcing the
// "listitem" role.
boundingBox.all("div,ul,li").set("role", "presentation");
// Use the FocusManager Node Plugin to manage the focusability
// of each menuitem in the menu.
descendants: "input",
keys: { next: "down:40", // Down arrow
previous: "down:38" }, // Up arrow
focusClass: {
className: "yui3-menuitem-active",
fn: function (node) {
return node.get("parentNode");
}
},
circular: true
});
// Subscribe to the change event for the "focused" attribute
// to listen for when the menu initially gains focus, and
// when the menu has lost focus completely.
contentBox.focusManager.after("focusedChange", function (event) {
if (!event.newVal) { // The menu has lost focus
// Set the "activeDescendant" attribute to 0 when the
// menu is hidden so that the user can tab from the
// button to the first item in the menu the next time
// the menu is made visible.
this.set("activeDescendant", 0);
}
});
// Hide the button's menu if the user presses the escape key
// while focused either on the button or its menu.
Y.on("key", function () {
menu.hide();
}, [menuButton, boundingBox] ,"down:27");
if (Y.UA.ie === 6) {
// Set the width and height of the menu's bounding box -
// this is necessary for IE 6 so that the CSS for the
// shadow element can simply set the shadow's width and
// height to 100% to ensure that dimensions of the shadow
// are always sync'd to the that of its parent menu.
menu.on("visibleChange", function (event) {
if (event.newVal) {
boundingBox.setStyles({ height: "", width: "" });
height: (boundingBox.get("offsetHeight") + "px"),
width: (boundingBox.get("offsetWidth") + "px") });
}
});
}
menu.after("visibleChange", function (event) {
var bVisible = event.newVal;
// Focus the first item when the menu is made visible
// to allow users to navigate the menu via the keyboard
if (bVisible) {
// Need to set focus via a timer for Webkit and Opera
}
boundingBox.set("aria-hidden", (!bVisible));
});
// Hide the menu when one of menu items is clicked.
boundingBox.delegate("click", function (event) {
alert("You clicked " + this.one("input").get("value"));
menu.hide();
}, "li");
// Focus each menuitem as the user moves the mouse over
// the menu.
boundingBox.delegate("mouseenter", function (event) {
var focusManager = contentBox.focusManager;
if (focusManager.get("focused")) {
focusManager.focus(this.one("input"));
}
}, "li");
// Hide the menu if the user clicks outside of it or if the
// user doesn't click on the button
boundingBox.get("ownerDocument").on("mousedown", function (event) {
var oTarget = event.target;
if (!oTarget.compareTo(menuButton) &&
!menuButton.contains(oTarget) &&
!oTarget.compareTo(boundingBox) &&
!boundingBox.contains(oTarget)) {
menu.hide();
}
});
};
menuButton.addClass("yui3-menubutton");
// Hide the list until it is transformed into a menu
Y.one("#menu-1").setStyle("display", "none");
// Remove the "yui3-menubutton-loading" class from the parent container
// now that the necessary YUI dependencies are loaded and the
// menu button has been skinned.
menuButton.ancestor(".yui3-menubutton-loading").removeClass("yui3-menubutton-loading");
// Apply the ARIA roles, states and properties to the anchor.
role: "button",
"aria-haspopup": true
});
// Remove the "href" attribute from the anchor element to
// prevent JAWS and NVDA from reading the value of the "href"
// attribute when the anchor is focused.
menuButton.removeAttribute("href");
// Since the anchor's "href" attribute has been removed, the
// element will not fire the click event in Firefox when the
// user presses the enter key. To fix this, dispatch the
// "click" event to the anchor when the user presses the
// enter key.
Y.on("key", function (event) {
menuButton.simulate("click");
}, menuButton, "down:13");
}
// Set the "tabIndex" attribute of the anchor element to 0 to
// place it in the browser's default tab flow. This is
// necessary since 1) anchor elements are not in the default
// tab flow in Opera and 2) removing the "href" attribute
// prevents the anchor from firing its "click" event
// in Firefox.
menuButton.set("tabIndex", 0);
// Since there is some intermediary markup (<span>s) between the anchor element with the role
// of "button" applied and the text label for the anchor - we need to use the
// "aria-labelledby" attribute to ensure that screen readers announce the text label for the
// button.
var menuLabel = menuButton.one("span span"),
menuLabelID = Y.stamp(menuLabel);
menuLabel.set("id", menuLabelID);
menuButton.set("aria-labelledby", menuLabelID);
var showMenu = function (event) {
// For performance: Defer the creation of the menu until
// the first time the button is clicked.
if (!menu) {
initMenu();
}
if (!menu.get("visible")) {
menu.set("align", {
node: menuButton,
points: [Y.WidgetPositionAlign.TL, Y.WidgetPositionAlign.BL]
});
menu.show();
}
// Prevent the anchor element from being focused
// when the users mouses down on it.
};
// Bind both a "mousedown" and "click" event listener to
// ensure the button's menu can be invoked using both the
// mouse and the keyboard.
menuButton.on("mousedown", showMenu);
menuButton.on("click", showMenu);
});
```