Yahoo! Developer Network Home - Help

YDN Site Search

# YUI Library Examples: Container Family: Creating a Resizable Panel

# Container Family: Creating a Resizable Panel

Operating systems offer windows that can be resized, often by dragging from the lower right-hand corner (and, on Microsoft Windows, from the window edges). This example of the Panel Control implements resizability by leveraging YAHOO.util.Resize to introduce a resize handle to the bottom-right corner of the footer. Resize events are listened for, and the size of the body element is modified to fill out the new dimensions of the Panel.

View example in new window.

## Leveraging the Resize Utility

Prior to 2.5.0, we needed to add a large amount of code to the Panel control to create a resizable panel. With the Resize utility added in 2.5.0, we're able to achieve the same results with a lot less code.

The Resize utility encapsulates the code required to:

1. Create the resize drag handle
2. Monitor drag related events, and
3. Resize the element it's bound to

All we need to do is monitor when the panel is resized and resize it's contents relative to the new dimensions.

## Creating the Panel instance

We start off by creating a Panel instance as we normally do. In this case we'll create it from markup:

<div id="resizablepanel"> <div class="hd">Resizable Panel</div> <div class="bd"> <p>Lorem Ipsum...</p> </div> <div class="ft"></div> </div>

CSS is used to set `overflow:auto` on the body of the panel so that scrollbars are displayed if the panel ends up being too small to display it's contents. We also provide a default height for the footer and body.

#resizablepanel .bd { overflow:auto; height:10em; background-color:fff; padding:10px; } #resizablepanel .ft { height:15px; padding:0; }

NOTE: We also add three more CSS rules which, although not a core part of the design, are used to prevent the body overflow scrollbar from remaining visible in Gecko browsers on MacOS when the Panel is hidden. A detailed discussion of these rules can be found in the source of the example.

The JavaScript used to instantiate the Panel is shown below:

var panel = new YAHOO.widget.Panel('resizablepanel', { draggable: true, width: '500px', context: ["showbtn", "tl", "bl"] }); panel.render();

## Adding the Resize Support

After we've rendered the panel we can attach the Resize utility to its containing DIV element. This will add the resize handle to the DIV and change it's size when the handle is dragged:

var resize = new YAHOO.util.Resize('resizablepanel', { handles: ['br'], autoRatio: false, minWidth: 300, minHeight: 100, status: true });

The Resize constructor is given the id of the HTML element we want to resize. In this case it's the containing DIV for the panel instance (with id `resizablepanel`). We also tell the resize utility we need a bottom-right handle and set `minWidth` and `minHeight` properties to limit how small the panel can get.

Some custom CSS is applied to the resize handle to align it with the bottom of the panel and increase the default size.

#resizablepanel .yui-resize-handle-br { right:0; bottom:0; height: 8px; width: 8px; position:absolute; }

Dragging the handle will now resize the outer containing DIV of the panel. Since we want to keep the contents of the panel in sync with the new dimensions of the containing DIV, we listen for the `resize` event fired by the Resize instance.

In the listener, we set the height of the panel's body element to fill out the new height of the containing DIV. The width is handled automatically by the browser, with the header, body and footer DIVs filling out their containing element.

resize.on('resize', function(args) { var panelHeight = args.height; var headerHeight = this.header.offsetHeight; // Content + Padding + Border var footerHeight = this.footer.offsetHeight; // Content + Padding + Border var bodyHeight = (panelHeight - headerHeight - footerHeight); var bodyContentHeight = (IE_QUIRKS) ? bodyHeight : bodyHeight - PANEL_BODY_PADDING; YAHOO.util.Dom.setStyle(this.body, 'height', bodyContentHeight + 'px'); if (IE_SYNC) { // Keep the underlay and iframe size in sync. // You could also set the width property, to achieve the // same results, if you wanted to keep the panel's internal // width property in sync with the DOM width. this.sizeUnderlay(); // Syncing the iframe can be expensive. Disable iframe if you // don't need it. this.syncIframe(); } }, panel, true);

Note that when calculating the height of the body element, we need to account for padding and border widths and also differences in the box model for IE in quirks mode.

For IE (6, and 7 in quirks mode), we also need to invoke the `sizeUnderlay` and `syncIframe` methods on the panel, to ensure both these elements are resized to match the new size of the containing DIV element. For the other browsers this is handled automatically through CSS.

### Container Family Examples:

- The Module Control
- Creating and Positioning an Overlay
- Simple Tooltip Example
- One Tooltip, Many Context Elements
- Simple Panel Example
- Skinning a Panel with Custom CSS: Introduction
- Skinning a Panel with Custom CSS: Advanced
- Creating a Modal "Loading" Panel
- Creating a Resizable Panel
- Dialog Quickstart Example
- SimpleDialog Quickstart Example
- Using ContainerEffect Transitions
- Using the Overlay Manager to Manage Multiple Panels
- Implementing Container Keyboard Shortcuts with KeyListener
- Example of Color Picker Built in a Dialog via JavaScript (included with examples for Color Picker Control)
- Layout inside a resizable Panel (included with examples for Layout Manager (beta))
- Calendar inside a Container (included with examples for Calendar Control)
- Menu Buttons (included with examples for Button Control)
- Showing, Hiding, and Reordering Columns. (included with examples for DataTable Control (beta))
- Split Buttons (included with examples for Button Control)
- Calendar Menu Button (included with examples for Button Control)
- Calendar Menu Button (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)
- OS-Style Programs Menu (included with examples for Menu Family)
- Application Menubar (included with examples for Menu Family)
- Slider Button (included with examples for Button Control)
- Complex Application (included with examples for Layout Manager (beta))

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