YUI Library Home

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.

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    {  
9        text: "<em id=\"yahoolabel\">Yahoo!</em>",  
10        submenu: {  
11            id: "yahoo",  
12            itemdata: [ 
13                "About Yahoo!"
14                "YUI Team Info"
15                "Preferences" 
16            ] 
17        } 
18         
19    }, 
20 
21    {  
22        text: "File",  
23        submenu: {   
24            id: "filemenu",  
25            itemdata: [ 
26 
27                { text: "New File", helptext: "Ctrl + N" }, 
28                "New Folder"
29                { text: "Open", helptext: "Ctrl + O" }, 
30                {  
31                    text: "Open With...",  
32                    submenu: {  
33                        id: "applications",  
34                        itemdata: [ 
35                            "Application 1",  
36                            "Application 2",  
37                            "Application 3",  
38                            "Application 4" 
39                        ]  
40                    }  
41                }, 
42                { text: "Print", helptext: "Ctrl + P" } 
43 
44            ]  
45        } 
46     
47    }, 
48     
49    { 
50        text: "Edit",  
51        submenu: {  
52            id: "editmenu",  
53            itemdata: [ 
54 
55                [  
56                    { text: "Undo", helptext: "Ctrl + Z" }, 
57                    { text: "Redo", helptext: "Ctrl + Y", disabled: true } 
58                ], 
59                 
60                [ 
61                    { text: "Cut", helptext: "Ctrl + X", disabled: true }, 
62                    { text: "Copy", helptext: "Ctrl + C", disabled: true }, 
63                    { text: "Paste", helptext: "Ctrl + V" }, 
64                    { text: "Delete", helptext: "Del", disabled: true } 
65                ], 
66                 
67                [ { text: "Select All", helptext: "Ctrl + A" } ], 
68 
69                [ 
70                    { text: "Find", helptext: "Ctrl + F" }, 
71                    { text: "Find Again", helptext: "Ctrl + G" } 
72                ] 
73     
74        ] } 
75 
76    }, 
77 
78    "View"
79 
80    "Favorites"
81 
82    "Tools"
83 
84    "Help" 
85]; 
view plain | print | ?

Next use the onDOMReady method of the 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 
10         constructor is the id of the element to be created; the 
11         second is an 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         Since this MenuBar instance is built completely from 
22         script, call the "render" method passing in a node 
23         reference for the DOM element that its should be 
24         appended to.
25    */ 
26 
27    oMenuBar.render(document.body); 
28     
29 
30    // Add a "show" event listener for each submenu. 
31     
32    function onSubmenuShow() { 
33 
34        var oIFrame, 
35            oElement, 
36            nOffsetWidth; 
37 
38 
39        // Keep the left-most submenu against the left edge of the browser viewport 
40 
41        if (this.id == "yahoo") { 
42             
43            YAHOO.util.Dom.setX(this.element, 0); 
44 
45            oIFrame = this.iframe;             
46 
47 
48            if (oIFrame) { 
49     
50                YAHOO.util.Dom.setX(oIFrame, 0); 
51     
52            } 
53             
54            this.cfg.setProperty("x", 0, true); 
55         
56        } 
57 
58 
59        /*
60            Need to set the width for submenus of submenus in IE to prevent the mouseout 
61            event from firing prematurely when the user mouses off of a MenuItem's 
62            text node.
63        */ 
64 
65        if ((this.id == "filemenu" || this.id == "editmenu") && YAHOO.env.ua.ie) { 
66 
67            oElement = this.element; 
68            nOffsetWidth = oElement.offsetWidth; 
69     
70            /*
71                Measuring the difference of the offsetWidth before and after
72                setting the "width" style attribute allows us to compute the 
73                about of padding and borders applied to the element, which in 
74                turn allows us to set the "width" property correctly.
75            */ 
76             
77            oElement.style.width = nOffsetWidth + "px"
78            oElement.style.width = (nOffsetWidth - (oElement.offsetWidth - nOffsetWidth)) + "px"
79         
80        } 
81 
82    } 
83     
84 
85    // Subscribe to the "show" event for each submenu 
86     
87    oMenuBar.subscribe("show", onSubmenuShow); 
88 
89}); 
view plain | print | ?

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.

1em#yahoolabel { 
2 
3    text-indent-6em
4    displayblock
5    backgroundurl(http://us.i1.yimg.com/us.yimg.com/i/us/nt/b/purpley.1.0.gif) center center no-repeat; 
6    width2em
7    overflowhidden
8 
9} 
view plain | print | ?

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:

More Menu Family Resources:

Copyright © 2009 Yahoo! Inc. All rights reserved.

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