YUI Library Examples: Menu Family: Implementing ARIA Roles and States with Menu

Menu Family: Implementing ARIA Roles and States with Menu

This example demonstrates how use the WAI-ARIA Roles and States as implemented for Firefox 3 with the YUI Menu Control.

Using ARIA roles and states in the Menu Control

Begin by defining an array of MenuItem configuration properties that describe each item in the MenuBar.

1/*
2     Define an array of object literals, each containing 
3     the data necessary to create the items for a MenuBar.
4*/ 
5 
6var aItemData = [ 
7 
8    { text: "File", submenu: {  id: "filemenu", itemdata: [ 
9 
10            { text: "New File", helptext: "Ctrl + N" }, 
11            "New Folder"
12            { text: "Open", helptext: "Ctrl + O" }, 
13            { text: "Open With...", submenu: { id: "applications", itemdata: [ 
14                    "Application 1",  
15                    "Application 2",  
16                    "Application 3",  
17                    "Application 4" 
18                ] }  
19            }, 
20            { text: "Print", helptext: "Ctrl + P" } 
21 
22        ] } 
23     
24    }, 
25     
26    { text: "Edit", submenu: { id: "editmenu", itemdata: [ 
27 
28            [  
29                { text: "Undo", helptext: "Ctrl + Z" }, 
30                { text: "Redo", helptext: "Ctrl + Y" } 
31            ], 
32             
33            [ 
34                { text: "Cut", helptext: "Ctrl + X" }, 
35                { text: "Copy", helptext: "Ctrl + C" }, 
36                { text: "Paste", helptext: "Ctrl + V" }, 
37                { text: "Delete", helptext: "Del" } 
38            ], 
39             
40            [ { text: "Select All", helptext: "Ctrl + A" } ], 
41 
42            [ 
43                { text: "Find", helptext: "Ctrl + F" }, 
44                { text: "Find Again", helptext: "Ctrl + G" } 
45            ] 
46     
47        ] } 
48 
49    } 
50 
51]; 
view plain | print | ?

Next, use the onDOMReady method of the YUI Event Utility to instantiate the MenuBar as soon as the page's DOM is available for scripting.

1/*
2     Initialize and render the MenuBar when the page's DOM is ready 
3     to be scripted.
4*/ 
5 
6YAHOO.util.Event.onDOMReady(function () { 
7 
8    /*
9         Instantiate a MenuBar:  The first argument passed to the constructor is the id 
10         of the HTML to be created that will represent the MenuBar; the second is an 
11         object literal of configuration properties.
12    */ 
13 
14    var oMenuBar = new YAHOO.widget.MenuBar("mymenubar", {  
15                                                            lazyload: true,  
16                                                            itemdata: aItemData  
17                                                            }); 
18 
19 
20    /*
21        Add a "show" event listener that keeps the left-most
22        submenu against the left edge of the browser viewport.
23    */ 
24     
25    function onSubmenuShow() { 
26     
27        if (this.id == "yahoo") { 
28     
29            this.cfg.setProperty("x", 0); 
30         
31        } 
32     
33    } 
34     
35 
36    // Subscribe to the "show" event for each submenu 
37     
38    oMenuBar.subscribe("show", onSubmenuShow); 
39     
40}); 
view plain | print | ?

Lastly, apply the WAI-ARIA Roles and States to a Menu widget via a "render" event listener. Waiting for a Menu's "render" event to fire ensures that all of its DOM elements have been appended to the document and are available to be scripted. Roles and states are added to a Menu's DOM elements via the DOM setAttribute method.

1/*
2    Add the WAI-ARIA Roles and States to the MenuBar's DOM elements once it 
3    is rendered.
4*/ 
5 
6oMenuBar.subscribe("render"function () { 
7 
8    /*
9         Apply the "role" attribute of "menu" or "menubar" depending on the type of 
10         the Menu control being rendered.
11    */ 
12 
13    this.element.setAttribute("role",  
14                    (this instanceof YAHOO.widget.MenuBar ? "menubar" : "menu")); 
15 
16 
17    /*
18         Apply the appropriate "role" and "aria-[state]" attributes to the label of
19         each MenuItem instance.
20    */ 
21 
22    var aMenuItems = this.getItems(), 
23        i = aMenuItems.length - 1, 
24        oMenuItem, 
25        oMenuItemLabel; 
26     
27 
28    do { 
29 
30        oMenuItem = aMenuItems[i]; 
31 
32 
33        /*
34            Retrieve a reference to the anchor element that serves as the label for 
35            each MenuItem.
36        */ 
37 
38        oMenuItemLabel = oMenuItem.element.firstChild; 
39 
40 
41        // Set the "role" attribute of the label to "menuitem" 
42 
43        oMenuItemLabel.setAttribute("role""menuitem"); 
44 
45 
46        // Remove the label from the browser's default tab order 
47 
48        oMenuItemLabel.setAttribute("tabindex", -1); 
49 
50 
51        /*
52            Optional: JAWS announces the value of each anchor element's "href"
53            attribute when it recieves focus.  If the MenuItem instance's "url" 
54            attribute is set to the default, remove the attribute so that JAWS 
55            does announce its value.
56        */ 
57 
58        if (oMenuItem.cfg.getProperty("url") == "#") { 
59 
60            oMenuItemLabel.removeAttribute("href"); 
61         
62        } 
63 
64 
65        /*
66            If the MenuItem has a submenu, set the "aria-haspopup" attribute to 
67            true so that the screen reader can announce 
68        */ 
69 
70        if (oMenuItem.cfg.getProperty("submenu")) { 
71         
72            oMenuItemLabel.setAttribute("aria-haspopup"true); 
73         
74        } 
75 
76    } 
77    while (i--); 
78     
79 
80    /*
81         Set the "tabindex" of the first MenuItem's label to 0 so the user can 
82         easily tab into and out of the control.
83    */ 
84 
85    if (this.getRoot() == this) { 
86     
87        this.getItem(0).element.firstChild.setAttribute("tabindex", 0); 
88     
89    } 
90 
91}); 
92 
93 
94/*
95     Since this MenuBar instance is built completely from 
96     script, call the "render" method passing in a node 
97     reference for the DOM element that its should be 
98     appended to.
99*/ 
100 
101oMenuBar.render(document.body); 
view plain | print | ?

Once the WAI-ARIA Roles and States are applied, there are a few tweaks that can be made to the Menu's DOM elements to further improve the user experience. For Menu, the label of each MenuItem instance is represented in HTML as an anchor element (i.e. <a class="yuimenuitemlabel">), and in IE and Firefox, anchor elements are automatically part of the tab order. Having MenuItem labels in the tab order by default is important when JavaScript is disabled to ensure that the user can navigate a Menu via the keyboard with the tab key.

Since the Menu code provides its own, desktop-like keyboard functionality when JavaScript is enabled, having every MenuItem label in the browser's default tab order can be a nuisance to users of screen readers. When navigating the document with the tab key, users of screen readers have to tab through every single MenuItem label in a Menu, regardless of whether or not they want to use the Menu Control. This problem can be solved by setting the "tabindex" attribute of every MenuItem label but the first to a value of -1. Setting an element's "tabindex" attribute to a value of -1 removes it from the browser's default tab order, while maintaining its focusability via JavaScript. Since the YUI Menu keyboard functionality is activated when any MenuItem label has focus, with just one MenuItem label in the browser's default tab order the Menu's keyboard functionality will be preserved, while at the same time giving the user the ability to quickly tab into and out of the control.

Menu Family Examples:

More Menu Family Resources:

Copyright © 2008 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings