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.
To create an instance of an Overlay on your 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-align
, widget-position-constrain
and widget-stdmod
extensions it uses.
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.
Note: be sure to add the yui3-skin-sam
classname to the
page's <body>
element or to a parent element of the widget in order to apply
the default CSS skin. See Understanding Skinning.
<body class="yui3-skin-sam"> <!-- You need this skin class -->
For this example, we'll create the overlay instance from markup which already exists on the page, and is shown below. We mark the existing markup with the yui3-overlay-loading
class, so that we can hide it while the rich control is being instantiated:
<div id="overlay" class="yui3-overlay-loading"> <div class="yui3-widget-hd">Overlay Header</div> <div class="yui3-widget-bd">Overlay Body</div> <div class="yui3-widget-ft">Overlay Footer</div> </div>
To create an overlay instance, we use the overlay constructor Y.Overlay
, and pass it the srcNode
reference for the existing markup on the page:
var overlay = new Y.Overlay({ srcNode:"#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.
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.
// Hold onto input field references. var content = Y.one("#content"); var where = Y.one("#where"); var section = Y.one("#section"); Y.on("click", function() { // New content to be added. For this example, we escape the HTML since it's // coming from an unknown source, however Overlay accepts HTML strings as // content (you should ensure it's coming from a trusted source). var newContent = Y.Escape.html(content.get("value")); overlay.setStdModContent(section.get("value"), newContent, where.get("value")); }, "#setHTML");
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:
/* Hide overlay markup while loading, if js is enabled */ .yui3-js-enabled .yui3-overlay-loading { top:-1000em; left:-1000em; position:absolute; } /* Overlay Look/Feel */ .yui3-overlay-content { background-color: #ECEFFB; border: 1px solid #9EA8C6; border-radius: 3px; box-shadow: 3px 3px 5px rgba(0, 0, 0, 0.25); } .yui3-overlay-content .yui3-widget-hd { background-color: #B6BFDA; color: #30418C; font-size: 120%; font-weight: bold; padding: 0.2em 0.5em 0.3em; border-radius: 2px 2px 0 0; } .yui3-overlay-content .yui3-widget-bd { padding: 0.4em 0.6em 0.5em; } .yui3-overlay-content .yui3-widget-ft { background-color:#DFE3F5; padding: 0.4em 0.6em 0.5em; border-radius: 0 0 2px 2px; }
<div class="overlay-example" id="overlay-stdmod"> <div id="overlay" class="yui3-overlay-loading"> <div class="yui3-widget-hd">Overlay Header</div> <div class="yui3-widget-bd">Overlay Body</div> <div class="yui3-widget-ft">Overlay Footer</div> </div> <div class="fields"> <div> <label for="content">New content:</label> <textarea name="content" id="content"></textarea> <p> <strong>Note:</strong> while <a href="#setting-content">Overlay accepts HTML strings and Nodes</a> as content, for this example, all HTML will be escaped. </p> </div> <p> <label for="section">Section to add content to:</label> <select name="section" id="section"> <option value="header">Header</option> <option value="body">Body</option> <option value="footer">Footer</option> </select> </p> <p> <label for="where">Replace, insert before or append after existing content:</label> <select name="where" id="where"> <option value="before">Before</option> <option value="after">After</option> <option value="replace">Replace</option> </select> </p> <button type="button" id="setHTML">Set Content</button> </div> <div class="filler"> 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. </div> </div> <script type="text/javascript"> YUI().use("overlay", "escape", function(Y) { var overlay = new Y.Overlay({ srcNode:"#overlay", width:"20em", align: { node:"#overlay-stdmod > .filler", points:["tl", "tl"] } }); overlay.render(); var content = Y.one("#content"); var where = Y.one("#where"); var section = Y.one("#section"); Y.on("click", function() { var newContent = Y.Escape.html(content.get("value")); overlay.setStdModContent(section.get("value"), newContent, where.get("value")); }, "#setHTML"); }); </script>