YUI Library Home

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 implements resizability for the Panel control, 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 height configuration property of the Panel is set to reflect the new height of Panel's containing element.

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 to accomodate the resize handle.

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:

We set the autofillheight to "body", just to highlight the behavior for the example. It is set to this value by default, and will result in the Panel resizing the body element to fill out the height of the Panel's DIV element ('resizablepanel'), whenever the height configuration property is set.

Adding the Resize Support

Creating A Resize Instance

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.

Syncing Up Other Elements Of The Panel On Resize

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 Panel's height configuration property to match the new pixel height of the containing DIV. This will result in the body element, which we specified in the autofillheight property for the Panel, being resized to fill out the 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. Setting the height configuration property, will also result in the iframe shim and shadow being resized to match the new dimensions of the containing DIV if required for the browser (IE6 and IE7 quirks mode).

Setting Up Resize Constraints

We also setup a listener for the startResize event, which we use to setup the constraints for the height and width of the resized element, if the panel's constraintoviewport value is set to true.

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

Container Family Examples:

More Container Family Resources:

Copyright © 2009 Yahoo! Inc. All rights reserved.

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