Yahoo! Developer Network Home - Help

YDN Site Search

# 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" }, "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", disabled: true } ], [ { text: "Cut", helptext: "Ctrl + X", disabled: true }, { text: "Copy", helptext: "Ctrl + C", disabled: true }, { text: "Paste", helptext: "Ctrl + V" }, { text: "Delete", helptext: "Del", disabled: true } ], [ { text: "Select All", helptext: "Ctrl + A" } ], [ { text: "Find", helptext: "Ctrl + F" }, { text: "Find Again", helptext: "Ctrl + G" } ] ] } }, "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 () { /* 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 that keeps the left-most submenu against the left edge of the browser viewport. */ function onSubmenuShow() { if (this.id == "yahoo") { this.cfg.setProperty("x", 0); } } // 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. Lastly, it is necessary to add a "show" event handler to keep the position of the first, left-most submenu positioned flush to the edge of the browser's viewport. This is because by default Menu instances try to keep themselves positioned 10 pixels away from the edge of the browser viewport.

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 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://us.i1.yimg.com/us.yimg.com/i/us/nt/b/purpley.1.0.gif) center center no-repeat; width: 2em; overflow: hidden; }

### 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