|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Accessible Menu Button</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
<a href="#toc" class="jump">Jump to Table of Contents</a> |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
<h1>Example: Accessible Menu Button</h1> |
|
|
27 |
<div class="yui3-g"> |
|
|
28 |
<div class="yui3-u-3-4"> |
|
|
29 |
<div id="main"> |
|
|
30 |
<div class="content"><div class="intro"> |
|
|
31 |
<p> |
|
|
32 |
This example illustrates how to use the Focus Manager Node Plugin, |
|
|
33 |
Event's <a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#event_delegate">delegation support</a> and |
|
|
34 |
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#event_mouseenter">mouseenter</a> event, along with |
|
|
35 |
the <a href="../overlay/index.html">Overlay widget</a> and Node's support for the |
|
|
36 |
<a href="http://www.w3.org/TR/wai-aria/">WAI-ARIA Roles and States</a> to |
|
|
37 |
create an accessible menu button. |
|
|
38 |
</p> |
|
|
39 |
</div> |
|
|
40 |
|
|
|
41 |
<div class="example"> |
|
|
42 |
<style scoped> |
|
|
43 |
/* The following two styles are necessary to override style rules in the |
|
|
44 |
YUI CSS file. */ |
|
|
45 |
|
|
|
46 |
.example ul { |
|
|
47 |
margin: 0; |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
.example a:hover { |
|
|
51 |
text-decoration: none; |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
|
|
|
55 |
/* Hide the button and list while it is being transformed into a menu button. */ |
|
|
56 |
.yui3-js-enabled .yui3-menubutton-loading #menu-1, |
|
|
57 |
.yui3-js-enabled .yui3-menubutton-loading #button-1 { |
|
|
58 |
display: none; |
|
|
59 |
} |
|
|
60 |
|
|
|
61 |
</style> |
|
|
62 |
|
|
|
63 |
<link rel="stylesheet" href="../assets/node-focusmanager/menubutton.css"> |
|
|
64 |
|
|
|
65 |
<div class="yui3-menubutton-loading"> |
|
|
66 |
<a id="button-1" href="#menu-1"><span><span>Move To</span></span></a> |
|
|
67 |
<div id="menu-1"> |
|
|
68 |
<ul> |
|
|
69 |
<li><input type="button" name="button-1" value="Inbox"></li> |
|
|
70 |
<li><input type="button" name="button-2" value="Archive"></li> |
|
|
71 |
<li><input type="button" name="button-3" value="Trash"></li> |
|
|
72 |
</ul> |
|
|
73 |
</div> |
|
|
74 |
</div> |
|
|
75 |
<div id="out">(Accessible Menu Button results here)</div> |
|
|
76 |
|
|
|
77 |
<script> |
|
|
78 |
YUI().use("node-focusmanager", "node-event-simulate", "overlay", function(Y){ |
|
|
79 |
|
|
|
80 |
var menuButton = Y.one("#button-1"), |
|
|
81 |
out = Y.one("#out"), |
|
|
82 |
menu; |
|
|
83 |
|
|
|
84 |
var initMenu = function () { |
|
|
85 |
|
|
|
86 |
menu = new Y.Overlay({ |
|
|
87 |
contentBox: "#menu-1", |
|
|
88 |
visible: false, |
|
|
89 |
tabIndex: null |
|
|
90 |
}); |
|
|
91 |
|
|
|
92 |
menu.render(); |
|
|
93 |
|
|
|
94 |
|
|
|
95 |
Y.one("#menu-1").setStyle("display", ""); |
|
|
96 |
|
|
|
97 |
var boundingBox = menu.get("boundingBox"), |
|
|
98 |
contentBox = menu.get("contentBox"); |
|
|
99 |
|
|
|
100 |
boundingBox.addClass("yui3-buttonmenu"); |
|
|
101 |
contentBox.addClass("yui3-buttonmenu-content"); |
|
|
102 |
|
|
|
103 |
|
|
|
104 |
// Append a decorator element to the bounding box to render the shadow. |
|
|
105 |
|
|
|
106 |
boundingBox.append('<div class="yui3-menu-shadow"></div>'); |
|
|
107 |
|
|
|
108 |
|
|
|
109 |
// Apply the ARIA roles, states and properties to the menu. |
|
|
110 |
|
|
|
111 |
boundingBox.setAttrs({ |
|
|
112 |
role: "menu", |
|
|
113 |
"aria-labelledby": menuLabelID |
|
|
114 |
}); |
|
|
115 |
|
|
|
116 |
boundingBox.all("input").set("role", "menuitem"); |
|
|
117 |
|
|
|
118 |
|
|
|
119 |
// For NVDA: Add the role of "presentation" to each LI |
|
|
120 |
// element to prevent NVDA from announcing the |
|
|
121 |
// "listitem" role. |
|
|
122 |
|
|
|
123 |
boundingBox.all("div,ul,li").set("role", "presentation"); |
|
|
124 |
|
|
|
125 |
|
|
|
126 |
// Use the FocusManager Node Plugin to manage the focusability |
|
|
127 |
// of each menuitem in the menu. |
|
|
128 |
|
|
|
129 |
contentBox.plug(Y.Plugin.NodeFocusManager, { |
|
|
130 |
|
|
|
131 |
descendants: "input", |
|
|
132 |
keys: { next: "down:40", // Down arrow |
|
|
133 |
previous: "down:38" }, // Up arrow |
|
|
134 |
focusClass: { |
|
|
135 |
className: "yui3-menuitem-active", |
|
|
136 |
fn: function (node) { |
|
|
137 |
return node.get("parentNode"); |
|
|
138 |
} |
|
|
139 |
}, |
|
|
140 |
circular: true |
|
|
141 |
|
|
|
142 |
}); |
|
|
143 |
|
|
|
144 |
|
|
|
145 |
// Subscribe to the change event for the "focused" attribute |
|
|
146 |
// to listen for when the menu initially gains focus, and |
|
|
147 |
// when the menu has lost focus completely. |
|
|
148 |
|
|
|
149 |
contentBox.focusManager.after("focusedChange", function (event) { |
|
|
150 |
|
|
|
151 |
if (!event.newVal) { // The menu has lost focus |
|
|
152 |
|
|
|
153 |
// Set the "activeDescendant" attribute to 0 when the |
|
|
154 |
// menu is hidden so that the user can tab from the |
|
|
155 |
// button to the first item in the menu the next time |
|
|
156 |
// the menu is made visible. |
|
|
157 |
|
|
|
158 |
this.set("activeDescendant", 0); |
|
|
159 |
|
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
}); |
|
|
163 |
|
|
|
164 |
|
|
|
165 |
// Hide the button's menu if the user presses the escape key |
|
|
166 |
// while focused either on the button or its menu. |
|
|
167 |
|
|
|
168 |
Y.on("key", function () { |
|
|
169 |
|
|
|
170 |
menu.hide(); |
|
|
171 |
menuButton.focus(); |
|
|
172 |
|
|
|
173 |
}, [menuButton, boundingBox] ,"down:27"); |
|
|
174 |
|
|
|
175 |
|
|
|
176 |
if (Y.UA.ie === 6) { |
|
|
177 |
|
|
|
178 |
// Set the width and height of the menu's bounding box - |
|
|
179 |
// this is necessary for IE 6 so that the CSS for the |
|
|
180 |
// shadow element can simply set the shadow's width and |
|
|
181 |
// height to 100% to ensure that dimensions of the shadow |
|
|
182 |
// are always sync'd to the that of its parent menu. |
|
|
183 |
|
|
|
184 |
menu.on("visibleChange", function (event) { |
|
|
185 |
|
|
|
186 |
if (event.newVal) { |
|
|
187 |
|
|
|
188 |
boundingBox.setStyles({ height: "", width: "" }); |
|
|
189 |
|
|
|
190 |
boundingBox.setStyles({ |
|
|
191 |
height: (boundingBox.get("offsetHeight") + "px"), |
|
|
192 |
width: (boundingBox.get("offsetWidth") + "px") }); |
|
|
193 |
|
|
|
194 |
} |
|
|
195 |
|
|
|
196 |
}); |
|
|
197 |
|
|
|
198 |
} |
|
|
199 |
|
|
|
200 |
|
|
|
201 |
menu.after("visibleChange", function (event) { |
|
|
202 |
|
|
|
203 |
var bVisible = event.newVal; |
|
|
204 |
|
|
|
205 |
// Focus the first item when the menu is made visible |
|
|
206 |
// to allow users to navigate the menu via the keyboard |
|
|
207 |
|
|
|
208 |
if (bVisible) { |
|
|
209 |
|
|
|
210 |
// Need to set focus via a timer for Webkit and Opera |
|
|
211 |
Y.Lang.later(0, contentBox.focusManager, contentBox.focusManager.focus); |
|
|
212 |
|
|
|
213 |
} |
|
|
214 |
|
|
|
215 |
boundingBox.set("aria-hidden", (!bVisible)); |
|
|
216 |
|
|
|
217 |
}); |
|
|
218 |
|
|
|
219 |
|
|
|
220 |
// Hide the menu when one of menu items is clicked. |
|
|
221 |
|
|
|
222 |
boundingBox.delegate("click", function (event) { |
|
|
223 |
|
|
|
224 |
out.setHTML("You clicked " + this.one("input").get("value")); |
|
|
225 |
|
|
|
226 |
contentBox.focusManager.blur(); |
|
|
227 |
menu.hide(); |
|
|
228 |
|
|
|
229 |
}, "li"); |
|
|
230 |
|
|
|
231 |
|
|
|
232 |
// Focus each menuitem as the user moves the mouse over |
|
|
233 |
// the menu. |
|
|
234 |
|
|
|
235 |
boundingBox.delegate("mouseenter", function (event) { |
|
|
236 |
|
|
|
237 |
var focusManager = contentBox.focusManager; |
|
|
238 |
|
|
|
239 |
if (focusManager.get("focused")) { |
|
|
240 |
focusManager.focus(this.one("input")); |
|
|
241 |
} |
|
|
242 |
|
|
|
243 |
}, "li"); |
|
|
244 |
|
|
|
245 |
|
|
|
246 |
// Hide the menu if the user clicks outside of it or if the |
|
|
247 |
// user doesn't click on the button |
|
|
248 |
|
|
|
249 |
boundingBox.get("ownerDocument").on("mousedown", function (event) { |
|
|
250 |
|
|
|
251 |
var oTarget = event.target; |
|
|
252 |
|
|
|
253 |
if (!oTarget.compareTo(menuButton) && |
|
|
254 |
!menuButton.contains(oTarget) && |
|
|
255 |
!oTarget.compareTo(boundingBox) && |
|
|
256 |
!boundingBox.contains(oTarget)) { |
|
|
257 |
|
|
|
258 |
menu.hide(); |
|
|
259 |
|
|
|
260 |
} |
|
|
261 |
|
|
|
262 |
}); |
|
|
263 |
|
|
|
264 |
}; |
|
|
265 |
|
|
|
266 |
|
|
|
267 |
menuButton.addClass("yui3-menubutton"); |
|
|
268 |
|
|
|
269 |
|
|
|
270 |
// Hide the list until it is transformed into a menu |
|
|
271 |
|
|
|
272 |
Y.one("#menu-1").setStyle("display", "none"); |
|
|
273 |
|
|
|
274 |
|
|
|
275 |
// Remove the "yui3-menubutton-loading" class from the parent container |
|
|
276 |
// now that the necessary YUI dependencies are loaded and the |
|
|
277 |
// menu button has been skinned. |
|
|
278 |
|
|
|
279 |
menuButton.ancestor(".yui3-menubutton-loading").removeClass("yui3-menubutton-loading"); |
|
|
280 |
|
|
|
281 |
|
|
|
282 |
// Apply the ARIA roles, states and properties to the anchor. |
|
|
283 |
|
|
|
284 |
menuButton.setAttrs({ |
|
|
285 |
role: "button", |
|
|
286 |
"aria-haspopup": true |
|
|
287 |
}); |
|
|
288 |
|
|
|
289 |
|
|
|
290 |
// Remove the "href" attribute from the anchor element to |
|
|
291 |
// prevent JAWS and NVDA from reading the value of the "href" |
|
|
292 |
// attribute when the anchor is focused. |
|
|
293 |
|
|
|
294 |
if ((Y.UA.gecko || Y.UA.ie) && navigator.userAgent.indexOf("Windows") > -1) { |
|
|
295 |
|
|
|
296 |
menuButton.removeAttribute("href"); |
|
|
297 |
|
|
|
298 |
// Since the anchor's "href" attribute has been removed, the |
|
|
299 |
// element will not fire the click event in Firefox when the |
|
|
300 |
// user presses the enter key. To fix this, dispatch the |
|
|
301 |
// "click" event to the anchor when the user presses the |
|
|
302 |
// enter key. |
|
|
303 |
|
|
|
304 |
Y.on("key", function (event) { |
|
|
305 |
|
|
|
306 |
menuButton.simulate("click"); |
|
|
307 |
|
|
|
308 |
}, menuButton, "down:13"); |
|
|
309 |
|
|
|
310 |
} |
|
|
311 |
|
|
|
312 |
|
|
|
313 |
// Set the "tabIndex" attribute of the anchor element to 0 to |
|
|
314 |
// place it in the browser's default tab flow. This is |
|
|
315 |
// necessary since 1) anchor elements are not in the default |
|
|
316 |
// tab flow in Opera and 2) removing the "href" attribute |
|
|
317 |
// prevents the anchor from firing its "click" event |
|
|
318 |
// in Firefox. |
|
|
319 |
|
|
|
320 |
menuButton.set("tabIndex", 0); |
|
|
321 |
|
|
|
322 |
// Since there is some intermediary markup (<span>s) between the anchor element with the role |
|
|
323 |
// of "button" applied and the text label for the anchor - we need to use the |
|
|
324 |
// "aria-labelledby" attribute to ensure that screen readers announce the text label for the |
|
|
325 |
// button. |
|
|
326 |
|
|
|
327 |
var menuLabel = menuButton.one("span span"), |
|
|
328 |
menuLabelID = Y.stamp(menuLabel); |
|
|
329 |
|
|
|
330 |
menuLabel.set("id", menuLabelID); |
|
|
331 |
menuButton.set("aria-labelledby", menuLabelID); |
|
|
332 |
|
|
|
333 |
var showMenu = function (event) { |
|
|
334 |
|
|
|
335 |
// For performance: Defer the creation of the menu until |
|
|
336 |
// the first time the button is clicked. |
|
|
337 |
|
|
|
338 |
if (!menu) { |
|
|
339 |
initMenu(); |
|
|
340 |
} |
|
|
341 |
|
|
|
342 |
|
|
|
343 |
if (!menu.get("visible")) { |
|
|
344 |
|
|
|
345 |
menu.set("align", { |
|
|
346 |
node: menuButton, |
|
|
347 |
points: [Y.WidgetPositionAlign.TL, Y.WidgetPositionAlign.BL] |
|
|
348 |
}); |
|
|
349 |
|
|
|
350 |
menu.show(); |
|
|
351 |
|
|
|
352 |
} |
|
|
353 |
|
|
|
354 |
// Prevent the anchor element from being focused |
|
|
355 |
// when the users mouses down on it. |
|
|
356 |
event.preventDefault(); |
|
|
357 |
|
|
|
358 |
}; |
|
|
359 |
|
|
|
360 |
|
|
|
361 |
// Bind both a "mousedown" and "click" event listener to |
|
|
362 |
// ensure the button's menu can be invoked using both the |
|
|
363 |
// mouse and the keyboard. |
|
|
364 |
|
|
|
365 |
menuButton.on(["mousedown", "click"], showMenu); |
|
|
366 |
|
|
|
367 |
}); |
|
|
368 |
</script> |
|
|
369 |
|
|
|
370 |
</div> |
|
|
371 |
|
|
|
372 |
<h2 id="setting-up-the-html">Setting Up the HTML</h2> |
|
|
373 |
|
|
|
374 |
<p> |
|
|
375 |
For a menu button, start with an <code><a></code> element whose |
|
|
376 |
<code>href</code> attribute is set to the id of an <code><div></code> |
|
|
377 |
that wraps a list of <code><input></code> elements. |
|
|
378 |
Therefore, without JavaScript and CSS, the menu button is simply an in-page |
|
|
379 |
link to a set of additional buttons. |
|
|
380 |
</p> |
|
|
381 |
|
|
|
382 |
<pre class="code prettyprint"><div class="yui3-menubutton-loading"> |
|
|
383 |
<a id="button-1" href="#menu-1"><span><span>Move To</span></span></a> |
|
|
384 |
<div id="menu-1"> |
|
|
385 |
<ul> |
|
|
386 |
<li><input type="button" name="button-1" value="Inbox"></li> |
|
|
387 |
<li><input type="button" name="button-2" value="Archive"></li> |
|
|
388 |
<li><input type="button" name="button-3" value="Trash"></li> |
|
|
389 |
</ul> |
|
|
390 |
</div> |
|
|
391 |
</div></pre> |
|
|
392 |
|
|
|
393 |
|
|
|
394 |
<h2 id="progressive-enhancement">Progressive Enhancement</h2> |
|
|
395 |
|
|
|
396 |
<p> |
|
|
397 |
To account for the scenario where the user has CSS enabled in their browser but JavaScript |
|
|
398 |
is disabled, the CSS used to style the menu button will be loaded via JavaScript |
|
|
399 |
using the YUI instance's <a href="../yui/index.html#loader">built-in Loader</a>. |
|
|
400 |
</p> |
|
|
401 |
|
|
|
402 |
<pre class="code prettyprint">YUI({ |
|
|
403 |
modules: { |
|
|
404 |
"menubuttoncss": { |
|
|
405 |
type: "css", |
|
|
406 |
fullpath: "../assets/node-focusmanager/menubutton.css" |
|
|
407 |
}, |
|
|
408 |
"menubuttonjs": { |
|
|
409 |
type: "js", |
|
|
410 |
fullpath: "../assets/node-focusmanager/menubutton.js", |
|
|
411 |
requires: ["menubuttoncss", "node-focusmanager", "node-event-simulate", "overlay"] |
|
|
412 |
} |
|
|
413 |
} |
|
|
414 |
}).use("menubuttonjs");</pre> |
|
|
415 |
|
|
|
416 |
|
|
|
417 |
<p> |
|
|
418 |
To prevent the user from seeing a flash unstyled content when JavaScript is enabled, |
|
|
419 |
a style rule is created using YUI's <code>yui3-js-enabled</code> class name that will temporarily |
|
|
420 |
hide the markup while the JavaScript and CSS are in the process of loading. For more on using the |
|
|
421 |
<code>yui3-js-enabled</code> class name, see the |
|
|
422 |
<a href="../widget/index.html#progressive">Hiding Progressively Enhanced Markup</a> section of the |
|
|
423 |
<a href="../widget/index.html">YUI Widget landing page</a>. |
|
|
424 |
</p> |
|
|
425 |
|
|
|
426 |
<pre class="code prettyprint">/* Hide the button and list while it is being transformed into a menu button. */ |
|
|
427 |
|
|
|
428 |
.yui3-js-enabled .yui3-menubutton-loading #menu-1, |
|
|
429 |
.yui3-js-enabled .yui3-menubutton-loading #button-1 { |
|
|
430 |
display: none; |
|
|
431 |
}</pre> |
|
|
432 |
|
|
|
433 |
|
|
|
434 |
<h2 id="aria-support">ARIA Support</h2> |
|
|
435 |
|
|
|
436 |
<p> |
|
|
437 |
Through the use of CSS and JavaScript the HTML for the menu button can be |
|
|
438 |
transformed into something that looks and behaves like a desktop menu button, |
|
|
439 |
but users of screen readers won't perceive it as an atomic widget, but rather |
|
|
440 |
simply as a set of HTML elements. However, through the application |
|
|
441 |
of the |
|
|
442 |
<a href="http://www.w3.org/TR/wai-aria/">WAI-ARIA Roles and States</a>, it is |
|
|
443 |
possible to improve the semantics of the markup such that users of screen |
|
|
444 |
readers perceive it as a menu button control. |
|
|
445 |
</p> |
|
|
446 |
|
|
|
447 |
<h2 id="keyboard-functionality">Keyboard Functionality</h2> |
|
|
448 |
|
|
|
449 |
<p> |
|
|
450 |
The keyboard functionality for the button's menu will be provided by the Focus |
|
|
451 |
Manager Node Plugin. The Focus Manager's |
|
|
452 |
<a href="http://yuilibrary.com/yui/docs/api/classes/plugin.NodeFocusManager.html#attr_descendants"><code>descendants</code></a> |
|
|
453 |
attribute is set to a value of "input", so that only one menuitem in the |
|
|
454 |
button's menu is in the browser's default tab flow. This allows users |
|
|
455 |
navigating via the keyboard to use the tab key to quickly move into and out of |
|
|
456 |
the menu. Once the menu has focus, the user can move focus among each menuitem |
|
|
457 |
using the up and down arrows keys, as defined by the value of the |
|
|
458 |
<a href="http://yuilibrary.com/yui/docs/api/classes/plugin.NodeFocusManager.html#attr_keys"><code>keys</code></a> |
|
|
459 |
attribute. The |
|
|
460 |
<a href="http://yuilibrary.com/yui/docs/api/classes/plugin.NodeFocusManager.html#attr_focusClass"><code>focusClass</code></a> |
|
|
461 |
attribute is used to apply a class of <code>yui-menuitem-active</code> to |
|
|
462 |
the parent <code><li></code> of each |
|
|
463 |
<code><input></code> when it is focused, making it easy to style the |
|
|
464 |
menuitem's focused state in all browsers. |
|
|
465 |
|
|
|
466 |
<pre class="code prettyprint">YUI().use("*", function (Y) { |
|
|
467 |
|
|
|
468 |
var menuButton = Y.one("#button-1"), |
|
|
469 |
menu; |
|
|
470 |
|
|
|
471 |
|
|
|
472 |
var initMenu = function () { |
|
|
473 |
|
|
|
474 |
menu = new Y.Overlay({ |
|
|
475 |
contentBox: "#menu-1", |
|
|
476 |
visible: false, |
|
|
477 |
tabIndex: null |
|
|
478 |
}); |
|
|
479 |
|
|
|
480 |
menu.render(); |
|
|
481 |
|
|
|
482 |
|
|
|
483 |
Y.one("#menu-1").setStyle("display", ""); |
|
|
484 |
|
|
|
485 |
var boundingBox = menu.get("boundingBox"), |
|
|
486 |
contentBox = menu.get("contentBox"); |
|
|
487 |
|
|
|
488 |
boundingBox.addClass("yui3-buttonmenu"); |
|
|
489 |
contentBox.addClass("yui3-buttonmenu-content"); |
|
|
490 |
|
|
|
491 |
|
|
|
492 |
// Append a decorator element to the bounding box to render the shadow. |
|
|
493 |
|
|
|
494 |
boundingBox.append('<div class="yui3-menu-shadow"></div>'); |
|
|
495 |
|
|
|
496 |
|
|
|
497 |
// Apply the ARIA roles, states and properties to the menu. |
|
|
498 |
|
|
|
499 |
boundingBox.setAttrs({ |
|
|
500 |
role: "menu", |
|
|
501 |
"aria-labelledby": menuLabelID |
|
|
502 |
}); |
|
|
503 |
|
|
|
504 |
boundingBox.all("input").set("role", "menuitem"); |
|
|
505 |
|
|
|
506 |
|
|
|
507 |
// For NVDA: Add the role of "presentation" to each LI |
|
|
508 |
// element to prevent NVDA from announcing the |
|
|
509 |
// "listitem" role. |
|
|
510 |
|
|
|
511 |
boundingBox.all("div,ul,li").set("role", "presentation"); |
|
|
512 |
|
|
|
513 |
|
|
|
514 |
// Use the FocusManager Node Plugin to manage the focusability |
|
|
515 |
// of each menuitem in the menu. |
|
|
516 |
|
|
|
517 |
contentBox.plug(Y.Plugin.NodeFocusManager, { |
|
|
518 |
|
|
|
519 |
descendants: "input", |
|
|
520 |
keys: { next: "down:40", // Down arrow |
|
|
521 |
previous: "down:38" }, // Up arrow |
|
|
522 |
focusClass: { |
|
|
523 |
className: "yui3-menuitem-active", |
|
|
524 |
fn: function (node) { |
|
|
525 |
return node.get("parentNode"); |
|
|
526 |
} |
|
|
527 |
}, |
|
|
528 |
circular: true |
|
|
529 |
|
|
|
530 |
}); |
|
|
531 |
|
|
|
532 |
|
|
|
533 |
// Subscribe to the change event for the "focused" attribute |
|
|
534 |
// to listen for when the menu initially gains focus, and |
|
|
535 |
// when the menu has lost focus completely. |
|
|
536 |
|
|
|
537 |
contentBox.focusManager.after("focusedChange", function (event) { |
|
|
538 |
|
|
|
539 |
if (!event.newVal) { // The menu has lost focus |
|
|
540 |
|
|
|
541 |
// Set the "activeDescendant" attribute to 0 when the |
|
|
542 |
// menu is hidden so that the user can tab from the |
|
|
543 |
// button to the first item in the menu the next time |
|
|
544 |
// the menu is made visible. |
|
|
545 |
|
|
|
546 |
this.set("activeDescendant", 0); |
|
|
547 |
|
|
|
548 |
} |
|
|
549 |
|
|
|
550 |
}); |
|
|
551 |
|
|
|
552 |
|
|
|
553 |
// Hide the button's menu if the user presses the escape key |
|
|
554 |
// while focused either on the button or its menu. |
|
|
555 |
|
|
|
556 |
Y.on("key", function () { |
|
|
557 |
|
|
|
558 |
menu.hide(); |
|
|
559 |
menuButton.focus(); |
|
|
560 |
|
|
|
561 |
}, [menuButton, boundingBox] ,"down:27"); |
|
|
562 |
|
|
|
563 |
|
|
|
564 |
if (Y.UA.ie === 6) { |
|
|
565 |
|
|
|
566 |
// Set the width and height of the menu's bounding box - |
|
|
567 |
// this is necessary for IE 6 so that the CSS for the |
|
|
568 |
// shadow element can simply set the shadow's width and |
|
|
569 |
// height to 100% to ensure that dimensions of the shadow |
|
|
570 |
// are always sync'd to the that of its parent menu. |
|
|
571 |
|
|
|
572 |
menu.on("visibleChange", function (event) { |
|
|
573 |
|
|
|
574 |
if (event.newVal) { |
|
|
575 |
|
|
|
576 |
boundingBox.setStyles({ height: "", width: "" }); |
|
|
577 |
|
|
|
578 |
boundingBox.setStyles({ |
|
|
579 |
height: (boundingBox.get("offsetHeight") + "px"), |
|
|
580 |
width: (boundingBox.get("offsetWidth") + "px") }); |
|
|
581 |
|
|
|
582 |
} |
|
|
583 |
|
|
|
584 |
}); |
|
|
585 |
|
|
|
586 |
} |
|
|
587 |
|
|
|
588 |
|
|
|
589 |
menu.after("visibleChange", function (event) { |
|
|
590 |
|
|
|
591 |
var bVisible = event.newVal; |
|
|
592 |
|
|
|
593 |
// Focus the first item when the menu is made visible |
|
|
594 |
// to allow users to navigate the menu via the keyboard |
|
|
595 |
|
|
|
596 |
if (bVisible) { |
|
|
597 |
|
|
|
598 |
// Need to set focus via a timer for Webkit and Opera |
|
|
599 |
Y.Lang.later(0, contentBox.focusManager, contentBox.focusManager.focus); |
|
|
600 |
|
|
|
601 |
} |
|
|
602 |
|
|
|
603 |
boundingBox.set("aria-hidden", (!bVisible)); |
|
|
604 |
|
|
|
605 |
}); |
|
|
606 |
|
|
|
607 |
|
|
|
608 |
// Hide the menu when one of menu items is clicked. |
|
|
609 |
|
|
|
610 |
boundingBox.delegate("click", function (event) { |
|
|
611 |
|
|
|
612 |
alert("You clicked " + this.one("input").get("value")); |
|
|
613 |
|
|
|
614 |
contentBox.focusManager.blur(); |
|
|
615 |
menu.hide(); |
|
|
616 |
|
|
|
617 |
}, "li"); |
|
|
618 |
|
|
|
619 |
|
|
|
620 |
// Focus each menuitem as the user moves the mouse over |
|
|
621 |
// the menu. |
|
|
622 |
|
|
|
623 |
boundingBox.delegate("mouseenter", function (event) { |
|
|
624 |
|
|
|
625 |
var focusManager = contentBox.focusManager; |
|
|
626 |
|
|
|
627 |
if (focusManager.get("focused")) { |
|
|
628 |
focusManager.focus(this.one("input")); |
|
|
629 |
} |
|
|
630 |
|
|
|
631 |
}, "li"); |
|
|
632 |
|
|
|
633 |
|
|
|
634 |
// Hide the menu if the user clicks outside of it or if the |
|
|
635 |
// user doesn't click on the button |
|
|
636 |
|
|
|
637 |
boundingBox.get("ownerDocument").on("mousedown", function (event) { |
|
|
638 |
|
|
|
639 |
var oTarget = event.target; |
|
|
640 |
|
|
|
641 |
if (!oTarget.compareTo(menuButton) && |
|
|
642 |
!menuButton.contains(oTarget) && |
|
|
643 |
!oTarget.compareTo(boundingBox) && |
|
|
644 |
!boundingBox.contains(oTarget)) { |
|
|
645 |
|
|
|
646 |
menu.hide(); |
|
|
647 |
|
|
|
648 |
} |
|
|
649 |
|
|
|
650 |
}); |
|
|
651 |
|
|
|
652 |
}; |
|
|
653 |
|
|
|
654 |
|
|
|
655 |
menuButton.addClass("yui3-menubutton"); |
|
|
656 |
|
|
|
657 |
|
|
|
658 |
// Hide the list until it is transformed into a menu |
|
|
659 |
|
|
|
660 |
Y.one("#menu-1").setStyle("display", "none"); |
|
|
661 |
|
|
|
662 |
|
|
|
663 |
// Remove the "yui3-menubutton-loading" class from the parent container |
|
|
664 |
// now that the necessary YUI dependencies are loaded and the |
|
|
665 |
// menu button has been skinned. |
|
|
666 |
|
|
|
667 |
menuButton.ancestor(".yui3-menubutton-loading").removeClass("yui3-menubutton-loading"); |
|
|
668 |
|
|
|
669 |
|
|
|
670 |
// Apply the ARIA roles, states and properties to the anchor. |
|
|
671 |
|
|
|
672 |
menuButton.setAttrs({ |
|
|
673 |
role: "button", |
|
|
674 |
"aria-haspopup": true |
|
|
675 |
}); |
|
|
676 |
|
|
|
677 |
|
|
|
678 |
// Remove the "href" attribute from the anchor element to |
|
|
679 |
// prevent JAWS and NVDA from reading the value of the "href" |
|
|
680 |
// attribute when the anchor is focused. |
|
|
681 |
|
|
|
682 |
if ((Y.UA.gecko || Y.UA.ie) && navigator.userAgent.indexOf("Windows") > -1) { |
|
|
683 |
|
|
|
684 |
menuButton.removeAttribute("href"); |
|
|
685 |
|
|
|
686 |
// Since the anchor's "href" attribute has been removed, the |
|
|
687 |
// element will not fire the click event in Firefox when the |
|
|
688 |
// user presses the enter key. To fix this, dispatch the |
|
|
689 |
// "click" event to the anchor when the user presses the |
|
|
690 |
// enter key. |
|
|
691 |
|
|
|
692 |
Y.on("key", function (event) { |
|
|
693 |
|
|
|
694 |
menuButton.simulate("click"); |
|
|
695 |
|
|
|
696 |
}, menuButton, "down:13"); |
|
|
697 |
|
|
|
698 |
} |
|
|
699 |
|
|
|
700 |
|
|
|
701 |
// Set the "tabIndex" attribute of the anchor element to 0 to |
|
|
702 |
// place it in the browser's default tab flow. This is |
|
|
703 |
// necessary since 1) anchor elements are not in the default |
|
|
704 |
// tab flow in Opera and 2) removing the "href" attribute |
|
|
705 |
// prevents the anchor from firing its "click" event |
|
|
706 |
// in Firefox. |
|
|
707 |
|
|
|
708 |
menuButton.set("tabIndex", 0); |
|
|
709 |
|
|
|
710 |
// Since there is some intermediary markup (<span>s) between the anchor element with the role |
|
|
711 |
// of "button" applied and the text label for the anchor - we need to use the |
|
|
712 |
// "aria-labelledby" attribute to ensure that screen readers announce the text label for the |
|
|
713 |
// button. |
|
|
714 |
|
|
|
715 |
var menuLabel = menuButton.one("span span"), |
|
|
716 |
menuLabelID = Y.stamp(menuLabel); |
|
|
717 |
|
|
|
718 |
menuLabel.set("id", menuLabelID); |
|
|
719 |
menuButton.set("aria-labelledby", menuLabelID); |
|
|
720 |
|
|
|
721 |
var showMenu = function (event) { |
|
|
722 |
|
|
|
723 |
// For performance: Defer the creation of the menu until |
|
|
724 |
// the first time the button is clicked. |
|
|
725 |
|
|
|
726 |
if (!menu) { |
|
|
727 |
initMenu(); |
|
|
728 |
} |
|
|
729 |
|
|
|
730 |
|
|
|
731 |
if (!menu.get("visible")) { |
|
|
732 |
|
|
|
733 |
menu.set("align", { |
|
|
734 |
node: menuButton, |
|
|
735 |
points: [Y.WidgetPositionAlign.TL, Y.WidgetPositionAlign.BL] |
|
|
736 |
}); |
|
|
737 |
|
|
|
738 |
menu.show(); |
|
|
739 |
|
|
|
740 |
} |
|
|
741 |
|
|
|
742 |
// Prevent the anchor element from being focused |
|
|
743 |
// when the users mouses down on it. |
|
|
744 |
event.preventDefault(); |
|
|
745 |
|
|
|
746 |
}; |
|
|
747 |
|
|
|
748 |
|
|
|
749 |
// Bind both a "mousedown" and "click" event listener to |
|
|
750 |
// ensure the button's menu can be invoked using both the |
|
|
751 |
// mouse and the keyboard. |
|
|
752 |
|
|
|
753 |
menuButton.on("mousedown", showMenu); |
|
|
754 |
menuButton.on("click", showMenu); |
|
|
755 |
|
|
|
756 |
});</pre> |
|
|
757 |
|
|
|
758 |
</div> |
|
|
759 |
</div> |
|
|
760 |
</div> |
|
|
761 |
|
|
|
762 |
<div class="yui3-u-1-4"> |
|
|
763 |
<div class="sidebar"> |
|
|
764 |
|
|
|
765 |
<div id="toc" class="sidebox"> |
|
|
766 |
<div class="hd"> |
|
|
767 |
<h2 class="no-toc">Table of Contents</h2> |
|
|
768 |
</div> |
|
|
769 |
|
|
|
770 |
<div class="bd"> |
|
|
771 |
<ul class="toc"> |
|
|
772 |
<li> |
|
|
773 |
<a href="#setting-up-the-html">Setting Up the HTML</a> |
|
|
774 |
</li> |
|
|
775 |
<li> |
|
|
776 |
<a href="#progressive-enhancement">Progressive Enhancement</a> |
|
|
777 |
</li> |
|
|
778 |
<li> |
|
|
779 |
<a href="#aria-support">ARIA Support</a> |
|
|
780 |
</li> |
|
|
781 |
<li> |
|
|
782 |
<a href="#keyboard-functionality">Keyboard Functionality</a> |
|
|
783 |
</li> |
|
|
784 |
</ul> |
|
|
785 |
</div> |
|
|
786 |
</div> |
|
|
787 |
|
|
|
788 |
|
|
|
789 |
|
|
|
790 |
<div class="sidebox"> |
|
|
791 |
<div class="hd"> |
|
|
792 |
<h2 class="no-toc">Examples</h2> |
|
|
793 |
</div> |
|
|
794 |
|
|
|
795 |
<div class="bd"> |
|
|
796 |
<ul class="examples"> |
|
|
797 |
|
|
|
798 |
|
|
|
799 |
<li data-description="Creating an accessible toolbar using the Focus Manager Node Plugin and Node's support for the WAI-ARIA Roles and States."> |
|
|
800 |
<a href="node-focusmanager-toolbar.html">Accessible Toolbar</a> |
|
|
801 |
</li> |
|
|
802 |
|
|
|
803 |
|
|
|
804 |
|
|
|
805 |
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States."> |
|
|
806 |
<a href="node-focusmanager-button.html">Accessible Menu Button</a> |
|
|
807 |
</li> |
|
|
808 |
|
|
|
809 |
|
|
|
810 |
</ul> |
|
|
811 |
</div> |
|
|
812 |
</div> |
|
|
813 |
|
|
|
814 |
|
|
|
815 |
|
|
|
816 |
</div> |
|
|
817 |
</div> |
|
|
818 |
</div> |
|
|
819 |
</div> |
|
|
820 |
|
|
|
821 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
822 |
<script>prettyPrint();</script> |
|
|
823 |
|
|
|
824 |
<script> |
|
|
825 |
YUI.Env.Tests = { |
|
|
826 |
examples: [], |
|
|
827 |
project: '../assets', |
|
|
828 |
assets: '../assets/node-focusmanager', |
|
|
829 |
name: 'node-focusmanager-button', |
|
|
830 |
title: 'Accessible Menu Button', |
|
|
831 |
newWindow: '', |
|
|
832 |
auto: false |
|
|
833 |
}; |
|
|
834 |
YUI.Env.Tests.examples.push('node-focusmanager-toolbar'); |
|
|
835 |
YUI.Env.Tests.examples.push('node-focusmanager-button'); |
|
|
836 |
|
|
|
837 |
</script> |
|
|
838 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
839 |
|
|
|
840 |
|
|
|
841 |
|
|
|
842 |
</body> |
|
|
843 |
</html> |