YUI Library Home

Site Search (YDN & YUIBlog):

# 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.

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

#resizablepanel .bd { overflow:auto; 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", height: "150px", autofillheight: "body", constraintoviewport: true, context: ["showbtn", "tl", "bl"] }); panel.render();

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:

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

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; }

### 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).

resize.on('resize', function(args) { var panelHeight = args.height; this.cfg.setProperty("height", panelHeight + "px"); }, panel, true);

### 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.

// Setup startResize handler, to constrain the resize width/height // if the constraintoviewport configuration property is enabled. resize.on('startResize', function(args) { if (this.cfg.getProperty("constraintoviewport")) { var D = YAHOO.util.Dom; var clientRegion = D.getClientRegion(); var elRegion = D.getRegion(this.element); resize.set("maxWidth", clientRegion.right - elRegion.left - YAHOO.widget.Overlay.VIEWPORT_OFFSET); resize.set("maxHeight", clientRegion.bottom - elRegion.top - YAHOO.widget.Overlay.VIEWPORT_OFFSET); } else { resize.set("maxWidth", null); resize.set("maxHeight", null); } }, panel, 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:

- 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
- Using the Button ARIA Plugin (included with examples for Button Control)
- Using the Container ARIA Plugin
- Using A Menu Button To Replace A <select> Element (included with examples for Button Control)
- Replacing the content of a Button's Menu (included with examples for Button Control)
- 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)
- Editor in a Dialog Control (included with examples for Rich Text Editor)
- Menu Buttons (included with examples for Button Control)
- Popup Calendar - Advanced (included with examples for Calendar Control)
- Showing, Hiding, and Reordering Columns. (included with examples for DataTable Control)
- Split Buttons (included with examples for Button Control)
- Simple Calendar Menu Button (included with examples for Button Control)
- Calendar Menu Button with Date on Button Face (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)

### More Container Family Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2010 Yahoo! Inc. All rights reserved.

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