YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Menu Family: Application Menubar

# Menu Family: Application Menubar

This example demonstrates how to use the MenuBar widget to create a menu bar entirely from JavaScript that functions like one found in a traditional desktop application.

View example in new window.

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

/* Define an array of object literals, each containing the data necessary to create the items for a MenuBar. */ var aItemData = [ { text: "<em id=\"yahoolabel\">Yahoo!</em>", submenu: { id: "yahoo", itemdata: [ "About Yahoo!", "YUI Team Info", "Preferences" ] } }, { text: "File", submenu: { id: "filemenu", itemdata: [ { text: "New File", helptext: "Ctrl + N", onclick: { fn: onMenuItemClick }, keylistener: { ctrl: true, keys: 78 } }, "New Folder", { text: "Open", helptext: "Ctrl + O", onclick: { fn: onMenuItemClick }, keylistener: { ctrl: true, keys: 79 } }, { text: "Open With...", submenu: { id: "applications", itemdata: [ "Application 1", "Application 2", "Application 3", "Application 4" ] } }, { text: "Print", helptext: "Ctrl + P", onclick: { fn: onMenuItemClick }, keylistener: { ctrl: true, keys: 80 } } ] } }, { text: "Edit", submenu: { id: "editmenu", itemdata: [ [ { text: "Undo", helptext: "Ctrl + Z", onclick: { fn: onMenuItemClick }, keylistener: { ctrl: true, keys: 90 } }, { text: "Redo", helptext: "Ctrl + Y", disabled: true } ], [ { text: "Cut", helptext: "Ctrl + X", disabled: true }, { text: "Copy", helptext: "Ctrl + C", disabled: true }, { text: "Paste", helptext: "Ctrl + V", onclick: { fn: onMenuItemClick }, keylistener: { ctrl: true, keys: 86 } }, { text: "Delete", helptext: "Del", disabled: true } ], [ { text: "Select All", helptext: "Ctrl + A", onclick: { fn: onMenuItemClick }, keylistener: { ctrl: true, keys: 65 } } ], [ { text: "Find", helptext: "Ctrl + F", onclick: { fn: onMenuItemClick }, keylistener: { ctrl: true, keys: 70 } }, { text: "Find Again", helptext: "Ctrl + G", onclick: { fn: onMenuItemClick }, keylistener: { ctrl: true, keys: 71 } } ] ] } }, "View", "Favorites", "Tools", "Help" ];

Next use the `onDOMReady` method of the 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. */ YAHOO.util.Event.onDOMReady(function () { var onMenuItemClick = function () { alert("Callback for MenuItem: " + this.cfg.getProperty("text")); }; /* Instantiate a MenuBar: The first argument passed to the constructor is the id of the element to be created; the second is an object literal of configuration properties. */ var oMenuBar = new YAHOO.widget.MenuBar("mymenubar", { lazyload: true, itemdata: aItemData }); /* 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); // Add a "show" event listener for each submenu. function onSubmenuShow() { var oIFrame, oElement, nOffsetWidth; // Keep the left-most submenu against the left edge of the browser viewport if (this.id == "yahoo") { YAHOO.util.Dom.setX(this.element, 0); oIFrame = this.iframe; if (oIFrame) { YAHOO.util.Dom.setX(oIFrame, 0); } this.cfg.setProperty("x", 0, true); } /* Need to set the width for submenus of submenus in IE to prevent the mouseout event from firing prematurely when the user mouses off of a MenuItem's text node. */ if ((this.id == "filemenu" || this.id == "editmenu") && YAHOO.env.ua.ie) { oElement = this.element; nOffsetWidth = oElement.offsetWidth; /* Measuring the difference of the offsetWidth before and after setting the "width" style attribute allows us to compute the about of padding and borders applied to the element, which in turn allows us to set the "width" property correctly. */ oElement.style.width = nOffsetWidth + "px"; oElement.style.width = (nOffsetWidth - (oElement.offsetWidth - nOffsetWidth)) + "px"; } } // Subscribe to the "show" event for each submenu oMenuBar.subscribe("show", onSubmenuShow); });

The "lazyload" property is set to "true" to help speed up the initial load time of the MenuBar instance by deferring the initialization and rendering of each submenu until just before it is initially made visible. The "itemdata" property is set to the array of MenuItem configuration properties; each item in the array will be used to add a new item to the MenuBar when it is rendered.

Often the first item in a menu bar has an icon as its label, but no text. It is easy to achieve this using CSS, while still ensuring the text of the MenuItem is available to users of a screen reader.

Start by wrapping the MenuItem's text label in an `<em>` element. Next, give the `<em>` a fixed width, and set the "text-indent" property to a value that pushes the text beyond the boundaries of element as defined by the width. Use the "overflow" property to hide the text. Lastly, apply an image to the MenuItem instance via the "background" property.

em#yahoolabel { text-indent: -6em; display: block; background: url(http://l.yimg.com/a/i/us/nt/b/purpley.1.0.gif) center center no-repeat; width: 2em; overflow: hidden; }

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### 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
- Using the Menu ARIA Plugin
- Replacing the content of a Button's Menu (included with examples for Button Control)
- Using A Menu Button To Replace A <select> Element (included with examples for Button Control)
- Using the Button ARIA Plugin (included with examples for Button Control)
- Context Menu Integration (included with examples for DataTable Control)
- 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)
- Layout with Menu Controls (included with examples for Layout Manager)
- Using the ProfilerViewer API while Profiling the YUI Menu Control (included with examples for ProfilerViewer Control)

### More Menu Family Resources:

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

Copyright © 2010 Yahoo! Inc. All rights reserved.

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