<style type="text/css" scoped>
{{>overlay-css}}
/* Example Layout CSS */
.example .overlay-example {
position: relative;
padding: 5px;
height: 34.5em;
}
.example #overlay-align {
border: solid 1px #fff;
}
.example .overlay-example button {
margin-left: 1px;
}
.example .overlay-example .yui3-g {
color: #999;
}
.example .align-box {
background-color: #ddd;
border: solid 1px #ddd;
margin: 0px;
text-align: center;
width: 100%;
}
.example .overlay-example .content {
margin: 0 1em 0 0;
padding: 0;
}
.example .overlay-example .example-head {
border-bottom: solid 1px #ddd;
margin-bottom: 2em;
padding-bottom: 1em;
height: 8.6em;
}
.example .overlay-example .example-head pre{
margin: 0;
}
.example #alignment {
position: absolute;
right: 5px;
text-align: left;
top: 0;
height: 9em;
width: 36em;
}
.example .nav{
list-style-type: none;
-moz-padding-start: 0;
-webkit-padding-start: 0;
padding-start: 0;
margin: 0;
padding: 0;
}
.example .nav li {
border: solid 1px #ddd;
width: 72px;
height: 2em;
line-height: 2em;
text-indent: 0.3em;
}
.example .align-box .title {
color: #000;
padding: 2px;
}
.example #align2 {
width: 383px;
height: 164px;
}
.example #align3 {
height: 3em;
line-height: 3em;
}
.example #step {
font-size: 55%;
margin-left: 22px;
font-weight: normal;
}
#main .aligned-to{ /* highlight the node that overlay is currently aligned to */
border: solid 1px #f00;
}
</style>
<div class="intro">
<p>This example shows how you can use Overlay's extended positioning support to align or center the overlay either in the viewport or relative to another node on the page. You can specify any one of 9 points (top-left, top-right, bottom-left, bottom-right, top-center, bottom-center, left-center, right-center, center) to align on both the Overlay and the node/viewport it is being aligned to.</p>
</div>
<div class="example">
{{>overlay-align-source}}
</div>
<h2>Aligned XY Overlay Positioning - align and center Support</h2>
<h3>Setting Up The YUI Instance</h3>
<p>As with the <a href="overlay-xy.html">"Basic XY Positioning"</a> example, 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.</p>
```
YUI({...}).use("overlay", function(Y) {
// We'll write example code here, where we have a Y.Overlay class available.
});
```
<p>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.</p>
<h3>Instantiating The Overlay</h3>
<p>For this example, we'll instantiate an Overlay, as we did for the <a href="overlay-xy.html">"Basic XY Positioning"</a> example, however we'll set the content for the Overlay sections using the `headerContent` and `bodyContent` attributes. We won't create a footer section for this overlay:</p>
```
/* Create Overlay from script, this time. With no footer */
var overlay = new Y.Overlay({
width:"16em",
headerContent: 'Aligned Overlay<span id="step"></span>',
bodyContent: '<div class="descr"></div><div id="alignment"></div>',
zIndex:2
});
/* Render it as a child of the #overlay-align element */
overlay.render("#overlay-align");
```
<p>Since the Overlay is created from script, and doesn't currently exist in the document, we pass the `overlay.render()` method a selector query (`"#overlay-align"`) for the node under which we want the Overlay to be rendered in the DOM. If we leave out this argument to render (or if the selector query doesn't bring back a node), the Overlay will be rendered to the current document's body element.</p>
<h3>Aligning the overlay</h3>
<p>The <a href="{{apiDocs}}/WidgetPositionAlign.html">`WidgetPositionAlign`</a> extension used to create the overlay class adds the `align` and `centered` attributes to the Overlay, which can be used to align or center the Overlay relative to another element on the page (or the viewport).</p>
<p>The `align` attribute accepts as a value an object literal with the following properties:</p>
<dl>
<dt>node</dt>
<dd>
The node to which the Widget is to be aligned. If set to null, or not provided, the Overlay is aligned to the viewport
</dd>
<dt>points</dt>
<dd>
<p>
A two element array, defining the two points on the Overlay and node which are to be aligned. The first element is the point on the Overlay, and the second element is the point on the node (or viewport).
Supported alignment points are defined as static properties on the `WidgetPositionAlign` extension.
</p>
<p>
e.g. `[Y.WidgetPositionAlign.TR, Y.WidgetPositionAlign.TL]` aligns the Top-Right corner of the Overlay with the
Top-Left corner of the node/viewport, and `[Y.WidgetPositionAlign.CC, Y.WidgetPositionAlign.TC]` aligns the Center of the
Overlay with the Top-Center edge of the node/viewport.
</p>
</dd>
</dl>
<p>The `centered` property can either by set to `true` to center the Overlay in the viewport, or set to a selector string or node reference to center the Overlay in a particular node.</p>
<p>The example loops around a list of 6 alignment configurations, as the "Align Next" button is clicked:</p>
```
/* Setup local variable for Y.WidgetPositionAlign, since we use it multiple times */
var a = Y.WidgetPositionAlign;
...
/* Align top-left corner of overlay, with top-right corner of #align1 */
overlay.set("align", {node:"#align1",
points:[a.TL, a.TR]});
/* Center overlay in #align2 */
overlay.set("centered", "#align2");
/* Align top-center edge of overlay with bottom-center edge of #align3 */
overlay.set("align", {node:"#align3",
points:[a.TC, a.BC]});
/* Align right-center edge of overlay with right-center edge of viewport */
overlay.set("align", {points:[a.RC, a.RC]});
/* Center overlay in viewport */
overlay.set("centered", true);
/* Center in #overlay-align (example box) */
overlay.set("align", {node:"#overlay-align",
points:[a.CC, a.CC]});
```
<h3>CSS: Overlay Look/Feel</h3>
<p>As mentioned in the <a href="overlay-xy.html">"Basic XY Positioning"</a> example, 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.</p>
<p>The example provides it's own look and feel for the Overlay, by defining rules for the content box, header and body sections:</p>
```
{{>overlay-css}}
```
<h2>Complete Example Source</h2>
```
{{>overlay-align-source}}
```