5
<title>Example: Accessible Toolbar</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: Accessible Toolbar</h1>
17
<a href="#toc" class="jump">Jump to Table of Contents</a>
21
<div id="main" class="yui3-u">
22
<div class="content"><div class="intro">
24
This example illustrates how to create an accessible toolbar using the
25
Focus Manager Node Plugin and Node's support for the
26
<a href="http://www.w3.org/TR/wai-aria/">WAI-ARIA Roles and States</a>.
33
border: solid 1px #999;
34
background-color: #ccc;
39
.yui3-toolbar-button {
40
display: inline-block;
43
border-color: #808080;
44
background-color: #dfdfdf;
46
font-size: 85%; /* 11px */
53
.yui3-toolbar-button span {
54
display: inline-block;
57
border-color: #808080;
59
*position: relative; /* Necessary to get negative margins working in IE */
63
.yui3-toolbar-button span span {
64
display: inline-block;
65
border: solid 1px #b6b6b6;
70
.yui3-toolbar-button input {
73
padding: 4px 4px 4px 24px;
74
*overflow: visible; /* Remove superfluous padding for IE */
75
background: transparent url(../assets/node-focusmanager/icons.png) no-repeat;
79
background-position: 4px -102px;
80
*background-position: 4px -100px;
84
background-position: 4px -78px;
85
*background-position: 4px -76px;
89
background-position: 4px -54px;
90
*background-position: 4px -52px;
94
background-position: 4px -30px;
95
*background-position: 4px -28px;
99
background-position: 4px -126px;
100
*background-position: 4px -124px;
104
background-position: 4px -6px;
105
*background-position: 4px -4px;
109
/* Augment the browser's default styling of the focus state by changing the
110
background color of the button when it is focused. */
112
.yui3-toolbar-button input.focus {
113
background-color: #B3D4FF;
117
<div id="toolbar-1" class="yui3-toolbar">
118
<span id="add-btn" class="yui3-toolbar-button first-child"><span><span><input type="button" name="btn-add" value="Add"></span></span></span>
119
<span id="edit-btn" class="yui3-toolbar-button"><span><span><input type="button" name="btn-edit" value="Edit"></span></span></span>
120
<span id="print-btn" class="yui3-toolbar-button"><span><span><input type="button" name="btn-print" value="Print"></span></span></span>
121
<span id="delete-btn" class="yui3-toolbar-button"><span><span><input type="button" name="btn-delete" value="Delete"></span></span></span>
122
<span id="open-btn" class="yui3-toolbar-button"><span><span><input type="button" name="btn-open" value="Open"></span></span></span>
123
<span id="save-btn" class="yui3-toolbar-button"><span><span><input type="button" name="btn-save" value="Save"></span></span></span>
128
YUI().use("node-focusmanager", function (Y) {
130
// Retrieve the Node instance representing the toolbar
131
// (<div id="toolbar">) and call the "plug" method
132
// passing in a reference to the Focus Manager Node Plugin.
134
var toolbar = Y.one("#toolbar-1");
136
toolbar.plug(Y.Plugin.NodeFocusManager, {
138
descendants: "input",
139
keys: { next: "down:39", // Right arrow
140
previous: "down:37" }, // Left arrow
147
// Set the ARIA "role" attribute of the Node instance representing the
148
// toolbar to "toolbar" to improve the semantics of the markup for
149
// users of screen readers.
151
toolbar.set("role", "toolbar");
154
// Listen for the click event on each button via the use of the
157
toolbar.delegate("click", function (event) {
159
alert("You clicked " + this.one("input").get("value"));
161
}, ".yui3-toolbar-button");
169
<h2 id="setting-up-the-html">Setting Up the HTML</h2>
172
Start with a set of <code><input></code> elements. For the
173
purpose of this example, the <code>type</code> attribute of each
174
button will be set to a value of "button" since they won't be responsible for
175
submitting a form. Each <code><input></code> is wrapped by two
176
<code><span></code>s that serve as decorator elements used to style
177
each button with rounded corners.
180
<pre class="code prettyprint"><div id="toolbar-1" class="yui3-toolbar">
181
<span id="add-btn" class="yui3-toolbar-button first-child"><span><span><input type="button" name="btn-add" value="Add"></span></span></span>
182
<span id="edit-btn" class="yui3-toolbar-button"><span><span><input type="button" name="btn-edit" value="Edit"></span></span></span>
183
<span id="print-btn" class="yui3-toolbar-button"><span><span><input type="button" name="btn-print" value="Print"></span></span></span>
184
<span id="delete-btn" class="yui3-toolbar-button"><span><span><input type="button" name="btn-delete" value="Delete"></span></span></span>
185
<span id="open-btn" class="yui3-toolbar-button"><span><span><input type="button" name="btn-open" value="Open"></span></span></span>
186
<span id="save-btn" class="yui3-toolbar-button"><span><span><input type="button" name="btn-save" value="Save"></span></span></span>
187
</div></pre>
190
<h2 id="setting-up-the-css">Setting Up the CSS</h2>
193
Next, each button in the toolbar is styled with rounded corners using a
194
combination of the CSS <code>border</code> property along with the use of
195
negative margins. An icon is added to each button using a background image.
196
Following the advice of the
197
<a href="http://developer.yahoo.com/performance/rules.html">Exceptional Performance team</a>,
198
this example uses the technique of
199
<a href="http://www.alistapart.com/articles/sprites">CSS Sprites</a>,
200
combining all of the icons for each button into a single image to reduce
204
<pre class="code prettyprint">.yui3-toolbar {
205
border: solid 1px #999;
206
background-color: #ccc;
211
.yui3-toolbar-button {
212
display: inline-block;
215
border-color: #808080;
216
background-color: #dfdfdf;
218
font-size: 85%; /* 11px */
225
.yui3-toolbar-button span {
226
display: inline-block;
229
border-color: #808080;
231
*position: relative; /* Necessary to get negative margins working in IE */
235
.yui3-toolbar-button span span {
236
display: inline-block;
237
border: solid 1px #b6b6b6;
242
.yui3-toolbar-button input {
245
padding: 4px 4px 4px 24px;
246
*overflow: visible; /* Remove superfluous padding for IE */
247
background: transparent url(../assets/node-focusmanager/icons.png) no-repeat;
251
background-position: 4px -102px;
252
*background-position: 4px -100px;
256
background-position: 4px -78px;
257
*background-position: 4px -76px;
261
background-position: 4px -54px;
262
*background-position: 4px -52px;
266
background-position: 4px -30px;
267
*background-position: 4px -28px;
271
background-position: 4px -126px;
272
*background-position: 4px -124px;
276
background-position: 4px -6px;
277
*background-position: 4px -4px;
281
<h2 id="initializing-the-focus-manager">Initializing the Focus Manager</h2>
284
With the toolbar markup and CSS in place, retrieve the Node instance
285
representing the toolbar (<code><div id="toolbar-1"></code>)
286
and call the <a href="http://yuilibrary.com/yui/docs/api/classes/Node.html#method_plug"><code>plug</code></a>
287
passing in a reference to the Focus Manager Node Plugin as the first argument,
288
and a collection of configuration attributes as the second argument.
293
<a href="http://yuilibrary.com/yui/docs/api/classes/plugin.NodeFocusManager.html#config_descendants"><code>descendants</code></a>
294
attribute is set to a value of "input", so that only one button in the toolbar
295
is in the browser's default tab flow. This allows users navigating via the
296
keyboard to use the tab key to quickly move into and out of the toolbar. Once
297
the toolbar has focus, the user can move focus among each button using the left
298
and right arrows keys, as defined by the value of the
299
<a href="http://yuilibrary.com/yui/docs/api/classes/plugin.NodeFocusManager.html#config_keys"><code>keys</code></a>
300
attribute. Lastly, the
301
<a href="http://yuilibrary.com/yui/docs/api/classes/plugin.NodeFocusManager.html#config_focusClass"><code>focusClass</code></a>
302
attribute is used to apply a class of <code>focus</code> to each
303
<code><input></code> when it is focused, making it easy to style the
304
focused state in all browsers.
307
<pre class="code prettyprint">YUI().use("node-focusmanager", function (Y) {
309
// Retrieve the Node instance representing the toolbar
310
// (<div id="toolbar">) and call the "plug" method
311
// passing in a reference to the Focus Manager Node Plugin.
313
var toolbar = Y.one("#toolbar-1");
315
toolbar.plug(Y.Plugin.NodeFocusManager, {
317
descendants: "input",
318
keys: { next: "down:39", // Right arrow
319
previous: "down:37" }, // Left arrow
320
focusClass: "focus",
326
// Set the ARIA "role" attribute of the Node instance representing the
327
// toolbar to "toolbar" to improve the semantics of the markup for
328
// users of screen readers.
330
toolbar.set("role", "toolbar");
333
// Listen for the click event on each button via the use of the
334
// "delegate" method
336
toolbar.delegate("click", function (event) {
338
alert("You clicked " + this.one("input").get("value"));
340
}, ".yui3-toolbar-button");
345
<h2 id="styling-focus">Styling Focus</h2>
348
To augment the browser's default styling of the focused state define a CSS
349
selector that adds a background color to the
352
<pre class="code prettyprint">/* Augment the browser's default styling of the focus state by changing the
353
background color of the button when it is focused. */
355
.yui3-toolbar-button input.focus {
356
background-color: #B3D4FF;
360
<h2 id="complete-example-source">Complete Example Source</h2>
362
<pre class="code prettyprint"><div id="toolbar-1" class="yui3-toolbar">
363
<span id="add-btn" class="yui3-toolbar-button first-child"><span><span><input type="button" name="btn-add" value="Add"></span></span></span>
364
<span id="edit-btn" class="yui3-toolbar-button"><span><span><input type="button" name="btn-edit" value="Edit"></span></span></span>
365
<span id="print-btn" class="yui3-toolbar-button"><span><span><input type="button" name="btn-print" value="Print"></span></span></span>
366
<span id="delete-btn" class="yui3-toolbar-button"><span><span><input type="button" name="btn-delete" value="Delete"></span></span></span>
367
<span id="open-btn" class="yui3-toolbar-button"><span><span><input type="button" name="btn-open" value="Open"></span></span></span>
368
<span id="save-btn" class="yui3-toolbar-button"><span><span><input type="button" name="btn-save" value="Save"></span></span></span>
373
YUI().use("node-focusmanager", function (Y) {
375
// Retrieve the Node instance representing the toolbar
376
// (<div id="toolbar">) and call the "plug" method
377
// passing in a reference to the Focus Manager Node Plugin.
379
var toolbar = Y.one("#toolbar-1");
381
toolbar.plug(Y.Plugin.NodeFocusManager, {
383
descendants: "input",
384
keys: { next: "down:39", // Right arrow
385
previous: "down:37" }, // Left arrow
386
focusClass: "focus",
392
// Set the ARIA "role" attribute of the Node instance representing the
393
// toolbar to "toolbar" to improve the semantics of the markup for
394
// users of screen readers.
396
toolbar.set("role", "toolbar");
399
// Listen for the click event on each button via the use of the
400
// "delegate" method
402
toolbar.delegate("click", function (event) {
404
alert("You clicked " + this.one("input").get("value"));
406
}, ".yui3-toolbar-button");
410
</script></pre>
415
<div id="sidebar" class="yui3-u">
417
<div id="toc" class="sidebox">
419
<h2 class="no-toc">Table of Contents</h2>
425
<a href="#setting-up-the-html">Setting Up the HTML</a>
428
<a href="#setting-up-the-css">Setting Up the CSS</a>
431
<a href="#initializing-the-focus-manager">Initializing the Focus Manager</a>
434
<a href="#styling-focus">Styling Focus</a>
437
<a href="#complete-example-source">Complete Example Source</a>
445
<div class="sidebox">
447
<h2 class="no-toc">Examples</h2>
451
<ul class="examples">
454
<li data-description="Creating an accessible toolbar using the Focus Manager Node Plugin and Node's support for the WAI-ARIA Roles and States.">
455
<a href="node-focusmanager-1.html">Accessible Toolbar</a>
460
<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.">
461
<a href="node-focusmanager-3.html">Accessible Menu Button</a>
475
<script src="../assets/vendor/prettify/prettify-min.js"></script>
476
<script>prettyPrint();</script>