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.

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:

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.

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:

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:

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.

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.

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.

More Container Family Resources:

Copyright © 2008 Yahoo! Inc. All rights reserved.

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