Yahoo! Developer Network Home - Help

Site Search (YDN & YUIBlog):

# 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 and Internet Explorer 8 with the YUI Menu Control.

View example in new window.

## Using ARIA roles and states in the Menu Control

Begin by defining an anonymous function in order to keep all variables out of the global scope. Inside the anonymous function, define some shortcuts to widgets and utils that will be used frequently as well as an array of MenuItem configuration properties that describe each item in the MenuBar.

(function () { var Event = YAHOO.util.Event, Dom = YAHOO.util.Dom, MenuBar = YAHOO.widget.MenuBar, /* Define an array of object literals, each containing the data necessary to create the items for a MenuBar. */ aItemData = [ { text: "File", submenu: { id: "filemenu", itemdata: [ { text: "New File", helptext: "Ctrl + N" }, "New Folder", { text: "Open", helptext: "Ctrl + O" }, { text: "Open With...", submenu: { id: "applications", itemdata: [ "Application 1", "Application 2", "Application 3", "Application 4" ] } }, { text: "Print", helptext: "Ctrl + P" } ] } }, { text: "Edit", submenu: { id: "editmenu", itemdata: [ [ { text: "Undo", helptext: "Ctrl + Z" }, { text: "Redo", helptext: "Ctrl + Y" } ], [ { text: "Cut", helptext: "Ctrl + X" }, { text: "Copy", helptext: "Ctrl + C" }, { text: "Paste", helptext: "Ctrl + V" }, { text: "Delete", helptext: "Del" } ], [ { text: "Select All", helptext: "Ctrl + A" } ], [ { text: "Find", helptext: "Ctrl + F" }, { text: "Find Again", helptext: "Ctrl + G" } ] ] } } ]; }());

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.

/* Initialize and render the MenuBar when the page's DOM is ready to be scripted. */ Event.onDOMReady(function () { /* Instantiate a MenuBar: The first argument passed to the constructor is the id of the HTML to be created that will represent the MenuBar; the second is an object literal of configuration properties. */ var oMenuBar = new MenuBar("mymenubar", { lazyload: true, itemdata: aItemData }); /* Add a "show" event listener that keeps the left-most submenu against the left edge of the browser viewport. */ function onSubmenuShow() { var oIFrame; if (this.id == "filemenu") { Dom.setX(this.element, 0); oIFrame = this.iframe; if (oIFrame) { Dom.setX(oIFrame, 0); } this.cfg.setProperty("x", 0, true); } } // Subscribe to the "show" event for each submenu oMenuBar.subscribe("show", onSubmenuShow); });

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.

/* Create a variable to store a reference to the currently focused MenuItem label, or the MenuItem label that CAN be focused by the user. */ var oCurrentItemLabel; /* Initialize and render the MenuBar when the page's DOM is ready to be scripted. */ Event.onDOMReady(function () { /* Instantiate a MenuBar: The first argument passed to the constructor is the id of the HTML to be created that will represent the MenuBar; the second is an object literal of configuration properties. */ var oMenuBar = new MenuBar("mymenubar", { lazyload: true, itemdata: aItemData }); /* Add a "show" event listener that keeps the left-most submenu against the left edge of the browser viewport. */ function onSubmenuShow() { var oIFrame; if (this.id == "filemenu") { Dom.setX(this.element, 0); oIFrame = this.iframe; if (oIFrame) { Dom.setX(oIFrame, 0); } this.cfg.setProperty("x", 0, true); } } // Subscribe to the "show" event for each submenu oMenuBar.subscribe("show", onSubmenuShow); /* Create a variable to store a reference to the currently focused MenuItem label, or the MenuItem label that CAN be focused by the user. */ var oCurrentItemLabel; /* Only apply the WAI-ARIA Roles and States for FF 3 and IE 8 since those are the only browsers that support them. */ if ((UA.gecko && UA.gecko >= 1.9) || (UA.ie && UA.ie >= 8)) { /* Add the WAI-ARIA Roles and States to the MenuBar's DOM elements once it is rendered. */ oMenuBar.subscribe("render", function () { /* Apply the "role" attribute of "menu" or "menubar" depending on the type of the Menu control being rendered. */ this.element.setAttribute("role", (this instanceof MenuBar ? "menubar" : "menu")); /* Apply the appropriate "role" and "aria-[state]" attributes to the label of each MenuItem instance. */ var aMenuItems = this.getItems(), i = aMenuItems.length - 1, oMenuItem, oMenuItemLabel; do { oMenuItem = aMenuItems[i]; /* Retrieve a reference to the anchor element that serves as the label for each MenuItem. */ oMenuItemLabel = oMenuItem.element.firstChild; // Set the "role" attribute of the label to "menuitem" oMenuItemLabel.setAttribute("role", "menuitem"); // Remove the label from the browser's default tab order oMenuItemLabel.tabIndex = -1; /* Optional: JAWS announces the value of each anchor element's "href" attribute when it recieves focus. If the MenuItem instance's "url" attribute is set to the default, remove the attribute so that JAWS doesn't announce its value. */ if (oMenuItem.cfg.getProperty("url") == "#") { oMenuItemLabel.removeAttribute("href"); } /* If the MenuItem has a submenu, set the "aria-haspopup" attribute to true so that the screen reader can announce */ if (oMenuItem.cfg.getProperty("submenu")) { oMenuItemLabel.setAttribute("aria-haspopup", true); } } while (i--); /* Set the "tabindex" of the first MenuItem's label to 0 so the user can easily tab into and out of the control. */ if (this.getRoot() == this) { oCurrentItemLabel = this.getItem(0).element.firstChild; oCurrentItemLabel.tabIndex = 0; } }); function onMenuItemFocus(p_oEvent) { // The currently focused element var oTarget = Event.getTarget(p_oEvent); if (Dom.isAncestor(oMenuBar.element, oTarget)) { /* Modify value of the tabIndex attribute so that the currently focused MenuItem label is in the browser's default tab order. */ if (oCurrentItemLabel) { oCurrentItemLabel.tabIndex = -1; } oCurrentItemLabel = oTarget; oCurrentItemLabel.tabIndex = 0; } else { /* If the focus has moved to an element on the page that is not a part of the MenuBar, restore the MenuBar to its original state so that the first item is in the browser's default tab index. */ oCurrentItemLabel.tabIndex = -1; oCurrentItemLabel = oMenuBar.getItem(0).element.firstChild; oCurrentItemLabel.tabIndex = 0; } } // Subscribe to the focus event. if (UA.ie) { /* Since the "focus" event doesn't bubble, and IE doesn't support listening for the capture phase of events, subscribe to the "focusin" event, which DOES bubble. */ Event.on(document, "focusin", onMenuItemFocus); } else { /* Since the "focus" event doesn't bubble, we'll use event capture for Gecko, Webkit and Opera. */ document.addEventListener("focus", onMenuItemFocus, true); } } /* Since this MenuBar instance is built completely from script, call the "render" method passing in a node reference for the DOM element that its should be appended to. */ oMenuBar.render(document.body);

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:

- Basic Menu From Markup
- Basic Menu From JavaScript
- Grouped Menu Items Using Markup
- Grouped Menu Items Using JavaScript
- Grouped Menu Items With Titles From Markup
- Grouped Menu Items With Titles From JavaScript
- Multi-tiered Menu From Markup
- Multi-tiered Menu From JavaScript
- Handling Menu Click Events
- Listening For Menu Events
- MenuItem Configuration Properties
- Setting Menu Configuration Properties At Runtime
- Website Left Nav With Submenus Built From Markup
- Website Left Nav With Submenus From JavaScript
- Website Top Nav With Submenus Built From Markup
- Website Top Nav With Submenus From JavaScript
- Website Left Nav Using Animation With Submenus Built From Markup
- Website Left Nav Using Animation With Submenus From JavaScript
- Website Top Nav Using Animation With Submenus Built From Markup
- Website Top Nav Using Animation With Submenus From JavaScript
- Context Menu
- Adding A Context Menu To A Table
- Adding A Context Menu To A TreeView
- OS-Style Programs Menu
- Application Menubar
- Implementing ARIA Roles and States with Menu
- Context Menu Integration (included with examples for DataTable Control (beta))
- Menu Buttons (included with examples for Button Control)
- Split Buttons (included with examples for Button Control)
- Color Picker Button (included with examples for Button Control)
- Fixed Width Menu Button (included with examples for Button Control)
- Slider Button (included with examples for Button Control)
- Using the ProfilerViewer API while Profiling the YUI Menu Control (included with examples for ProfilerViewer Control (beta))

### More Menu Family Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2008 Yahoo! Inc. All rights reserved.

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