YUI 3.x Home -

YUI Library Examples: Overlay: Standard Module Support

Overlay: Standard Module Support

This example shows how you can work either the headerContent, bodyContent, footerContent attributes, to replace content in the Overlay's standard module sections, or use the setStdModContent(section, content, where) method to insert content before, or append it after existing content in the section.

Overlay Header
Overlay Body
Overlay Footer

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed tellus pede, aliquet vitae, faucibus quis, lobortis non, metus. Pellentesque at metus ac mi condimentum egestas. In vel neque a massa porttitor ultrices. Nunc lorem. Vivamus ullamcorper fringilla tortor. Etiam at nunc pellentesque lectus cursus pretium. Integer velit. In quis nunc eget leo rhoncus scelerisque. In in ante ac ante pharetra vestibulum. Praesent sit amet metus. Nam egestas ipsum. Nulla facilisi. Quisque rhoncus, eros sed convallis faucibus, erat felis pretium nisi, non bibendum magna mauris non metus. Integer mauris eros, volutpat non, pretium vitae, rutrum at, tellus.

Overlay's Standard Module Support

Setting Up The YUI Instance

To create an instance of an Overlay on you page, the only module you need to request is the overlay module. The overlay module will pull in the widget, widget-stack, widget-position, widget-position-ext and widget-stdmod dependencies it has.

  1. YUI({...}).use("overlay", function(Y) {
  2. // We'll write example code here, where we have a Y.Overlay class available.
  3. });
YUI({...}).use("overlay", function(Y) {
    // We'll write example code here, where we have a Y.Overlay class available.
});

Note, using the overlay module, will also pull down the default CSS required for overlay, on top of which we only need to add our required look/feel CSS for the example.

Creating The Overlay From Markup

For this example, we'll create the overlay instance from markup which already exists on the page, and is shown below:

  1. <div id="overlay">
  2. <div class="yui-widget-hd">Overlay Header</div>
  3. <div class="yui-widget-bd">Overlay Body</div>
  4. <div class="yui-widget-ft">Overlay Footer</div>
  5. </div>
<div id="overlay">
    <div class="yui-widget-hd">Overlay Header</div>
    <div class="yui-widget-bd">Overlay Body</div>
    <div class="yui-widget-ft">Overlay Footer</div>
</div>

Instantiating The Overlay

To create an overlay instance, we use the overlay constructor Y.Overlay, and pass it the contentBox node reference for the existing markup on the page:

  1. var overlay = new Y.Overlay({
  2. contentBox:"#overlay",
  3. width:"20em",
  4. align: {
  5. node:"#overlay-stdmod > .filler",
  6. points:["tl", "tl"]
  7. }
  8. });
  9. overlay.render();
var overlay = new Y.Overlay({
    contentBox:"#overlay",
    width:"20em",
    align: {
        node:"#overlay-stdmod > .filler",
        points:["tl", "tl"]
    }
});
overlay.render();

We also set it's width and align it to the filler paragraph in the example box ("#overlay-stdmod > .filler"). We don't pass any node references to the render method, so the Overlay is rendered in the location of the contentBox provided.

Setting Content

The example provides a set of input fields, allowing the user to set content in either of the 3 standard module sections which Overlay supports using Overlay's setStdModContent method. The content can either be inserted before, appended after, or replace existing content in the specified section.

Additionally the new content can be converted to a node instance before being added to the specified section. Although it has no impact on the example, if the new content is added as a string, innerHTML is used to insert before or append after the existing section content, removing any event listeners which may have been attached to elements inside the section. The ability to convert the content to a node instance is provided in the example to illustrate Overlay's ability to handle both content formats.

  1. // Hold onto input field references.
  2. var content = Y.one("#content");
  3. var where = Y.one("#where");
  4. var section = Y.one("#section");
  5. var asString = Y.one("#asString");
  6.  
  7. Y.on("click", function() {
  8.  
  9. // New content to be added.
  10. var newContent = content.get("value");
  11.  
  12. // If the "Set content as string" checkbox is checked, we pass new content into the
  13. // setStdModContent method as string (innerHTML will be used to set the new content).
  14.  
  15. // If it's not checked, we create a node reference from the string,
  16. // and pass the new content into the setStdModContent as a node reference.
  17.  
  18. if (! asString.get("checked") ) {
  19. newContent = Y.Node.create(newContent);
  20. }
  21.  
  22. overlay.setStdModContent(section.get("value"), newContent, where.get("value"));
  23.  
  24. }, "#setContent");
// Hold onto input field references.
var content = Y.one("#content");
var where = Y.one("#where");
var section = Y.one("#section");
var asString = Y.one("#asString");
 
Y.on("click", function() {
 
    // New content to be added.
    var newContent = content.get("value");
 
    // If the "Set content as string" checkbox is checked, we pass new content into the 
    // setStdModContent method as string (innerHTML will be used to set the new content).
 
    // If it's not checked, we create a node reference from the string,
    // and pass the new content into the setStdModContent as a node reference.
 
    if (! asString.get("checked") ) {
        newContent = Y.Node.create(newContent);
    }
 
    overlay.setStdModContent(section.get("value"), newContent, where.get("value"));
 
}, "#setContent");

CSS: Overlay Look/Feel

As with the other basic overlay examples, the overlay.css Sam Skin file (build/overlay/assets/skins/sam/overlay.css) provides the default functional CSS for the overlay. Namely the CSS rules to hide the overlay, and position it absolutely. However there's no default out-of-the-box look/feel applied to the Overlay widget.

The example provides it's own look and feel for the Overlay, by defining rules for the content box, header, body and footer sections:

  1. /* Overlay Look/Feel */
  2. .yui-overlay-content {
  3. padding:3px;
  4. border:1px solid #000;
  5. background-color:#aaa;
  6. }
  7.  
  8. .yui-overlay-content .yui-widget-hd {
  9. padding:5px;
  10. border:2px solid #aa0000;
  11. background-color:#fff;
  12. }
  13.  
  14. .yui-overlay-content .yui-widget-bd {
  15. padding:5px;
  16. border:2px solid #0000aa;
  17. background-color:#fff;
  18. }
  19.  
  20. .yui-overlay-content .yui-widget-ft {
  21. padding:5px;
  22. border:2px solid #00aa00;
  23. background-color:#fff;
  24. }
/* Overlay Look/Feel */
.yui-overlay-content {
    padding:3px;
    border:1px solid #000;
    background-color:#aaa;
}
 
.yui-overlay-content .yui-widget-hd {
    padding:5px;
    border:2px solid #aa0000;
    background-color:#fff;
}
 
.yui-overlay-content .yui-widget-bd {
    padding:5px;
    border:2px solid #0000aa;
    background-color:#fff;
}
 
.yui-overlay-content .yui-widget-ft {
    padding:5px;
    border:2px solid #00aa00;
    background-color:#fff;
}

Copyright © 2009 Yahoo! Inc. All rights reserved.

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