YUI 3.x Home -

YUI Library Examples: MenuNav Node Plugin: Adding Submenus On The Fly

Note: This is YUI 3.x. Looking for YUI 2.x?

MenuNav Node Plugin: Adding Submenus On The Fly

This example demonstrates how to use the IO Utility to add submenus to a menu built using the MenuNav Node Plugin.

Design Goal

This menu will be created using the Progressive Enhancement design pattern, so that the accessibility of the menu can be tailored based on the capabilities of the user's browser. The goal is to design a menu that satisfies each of the following use cases:

Browser Grade Technologies User Experience
C HTML The user is using a browser for which CSS and JavaScript are being withheld.
A HTML + CSS The user is using an A-Grade browser, but has chosen to disable JavaScript.
A HTML + CSS + JavaScript The user is using an A-Grade browser with CSS and JavaScript enabled.
A HTML + CSS + JavaScript + ARIA The user is using an ARIA-capable, A-Grade browser with CSS and JavaScript enabled.

The MenuNav Node Plugin helps support most of the these use cases out of the box. By using an established, semantic, list-based pattern for markup, the core, C-grade experience is easily cemented using the MenuNav Node Plugin. Using JavaScript, the MenuNav Node Plugin implements established mouse and keyboard interaction patterns to deliver a user experience that is both familiar and easy to use, as well as support for the WAI-ARIA Roles and States, making it easy to satisfy the last two use cases. The second is the only use case that is not handled out of the box when using the MenuNav Node Plugin.

One common solution to making a menuing system work when CSS is enabled, but JavaScript is disabled is to leverage the :hover and :focus pseudo classes to provide support for both the mouse and the keyboard. However, there are a couple of problems with this approach:

Inconsistent Browser Support
IE 6 only supports the :hover and :focus pseudo classes on <a> elements. And while IE 7 supports :hover on all elements, it only supports :focus pseudo class on <a> elements. This solution won't work if the goal is to provide a consistent user experience across all of the A-grade browsers when JavaScript is disabled.
Poor User Experience
Even if the :hover and :focus pseudo classes were supported consistently across all A-grade browsers, it would be a solution that would work, but wouldn't work well. Use of the :focus pseudo class to enable keyboard support for a menu results in an unfamiliar, potentially cumbersome experience for keyboard users. Having a menu appear in response to its label simply being focused isn't an established interaction pattern for menus on the desktop, and implementing that pattern could result in menus that popup unexpectedly, and as a result, have the potential to get in the user's way. While use of the :hover pseudo class can be used to show submenus in response to a mouseover event, it doesn't allow the user to move diagonally from a label to its corresponding submenu — an established interaction pattern that greatly improves a menu's usability.
Bloats Code
Relying on :hover and :focus as an intermediate solution when JavaScript is disabled adds bloat to a menu's CSS. And relying on these pseudo classes would also likely mean additional code on the server to detect IE, so that submenu HTML that is inaccessible to IE users with JavaScript disabled is not delivered to the browser.

As the functionality for displaying submenus cannot be implemented in CSS to work consistently and well in all A-grade browsers, then that functionality is better implemented using JavaScript. And if submenus are only accessible if JavaScript is enabled, then it is best to only add the HTML for submenus via JavaScript. Adding submenus via JavaScript has the additional advantage of speeding up the initial load time of a page.

Approach

The approach for this menu will be to create horizontal top navigation that, when JavaScript is enabled, is enhanced into split buttons. The content of each submenu is functionality that is accessible via the page linked from the anchor of each submenu's label. Each submenu is purely sugar — a faster means of accessing functionality that is accessible via another path.

Setting Up the HTML

Start by providing the markup for the root horizontal menu, following the pattern outlined in the Split Button Top Nav example, minus the application of the yui-splitbuttonnav class to the menu's bounding box, the markup for the submenus, and the <a href="…" class="yui-menu-toggle"> elements inside each label that toggle each submenu's display. Include the MenuNav Node Plugin CSS in the <head> so that menu is styled even if JS is disabled. The following illustrates what the initial menu markup:

Setting Up the script

With the core markup for the menu in place, JavaScript will be responsible for transforming the simple horizontal menu into top navigation rendered like split buttons. The script will appended a submenu toggle to each menu label as well as add the yui-splitbuttonnav class to the menu's bounding box. Each submenu's label will be responsible for creating its corresponding submenu the first time its display is requested by the user. The content of each submenu is fetched asynchronously using Y.io.

Note: In keeping with the Exceptional Performance team's recommendation, the script block used to instantiate the menu will be placed at the bottom of the page. This not only improves performance, it helps ensure that the DOM subtree of the element representing the root menu (<div id="productsandservices">) is ready to be scripted.

Copyright © 2009 Yahoo! Inc. All rights reserved.

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