Panel is a widget that mimics the functionality of a regular OS window. It is similar to Overlay, with added functionality to support modality, event listeners on which to autohide and autofocus, header/footer button support and a custom stylesheet. Panel does not have any implementation code of it's own. It implements a set of extensions that provide certain sets of functionality. The "Creating Custom Widget Classes" example shows how you can use these extensions to build classes which mix and match some of the above features.
Getting Started
To include the source files for Panel and its dependencies, first load the YUI seed file if you haven't already loaded it.
<script src="http://yui.yahooapis.com/3.4.0/build/yui/yui-min.js"></script>
Next, create a new YUI instance for your application and populate it with the
modules you need by specifying them as arguments to the YUI().use()
method.
YUI will automatically load any dependencies required by the modules you
specify.
// Create a new YUI instance and populate it with the required modules. YUI().use('panel', function (Y) { // Panel is available and ready for use. Add implementation // code here. });
For more information on creating YUI instances and on the
use()
method, see the
documentation for the YUI Global object.
Creating a Panel
This simple example will create a Panel with default functionality. By default, a Panel is rendered with a "Close" button added to the header, with modality enabled, and will be hidden if the "esc" key or "Close" button is pressed. Clicking anywhere outside the panel will bring back focus to the panel.
YUI().use('panel', function(Y) { var panel = new Y.Panel({ srcNode: "#myPanelContent", width: 400, centered: true }); panel.render(); });
Modal Panel
A Panel is not modal by default. This functionality can be changed through the "modal" attribute, either during instantiation or later through the set() method.
YUI().use('panel', function(Y) { var panel = new Y.Panel({ srcNode: "#myPanelContent", width: 400, modal: true //make a modal panel }); panel.render(); //optionally, we could have written panel.set('modal', true); });
Panels can be nested in one another, and have different modal behavior. For instance, a modal panel may launch a non-modal panel on top of it. The WidgetModality
extension takes care of nesting behavior so no extra code is required for the implementer. Refer to the examples for more information.
Choosing when to focus and hide
By default, a Panel will return focus to itself if anything else on the page receives focus or is clicked. On the other hand, clicking the "close" button, or pressing the "esc" key will hide it. Both of these options can be configured as needed through the "hideOn" and "focusOn" attributes.
The following code snippet shows how to change the default "hide" behavior. Instead of hiding when the "esc" key is pressed, the Panel hides whenever something outside it's bounding box is pressed, or when a certain element on the page (with an id of anotherNode
) is clicked.
YUI().use('panel', function(Y) { var panel = new Y.Panel({ srcNode: "#myPanelContent", width: 400, centered: true, modal:false, //The hideOn Attribute takes an array of objects, with a required property "eventName" //and two optional properties "node", and "keyCode" hideOn: [ { //When we don't specify a node, it defaults to the boundingbox of this panel instance eventName: "clickoutside" }, { //This listens to click events on the node that was specified node: Y.one("#anotherNode"), eventName:"click" } ] }); panel.render(); });
Similarly, the "focusOn" attribute can be changed to configure the default focus behavior
var panel = new Y.Panel({ srcNode: "#myPanelContent", width: 400, centered: true, modal:false, //The focusOn Attribute takes an array of objects, with a required property "eventName" //and two optional properties "node", and "keyCode" focusOn: [ { //When we don't specify a node, it defaults to the boundingbox of this panel instance eventName: "clickoutside" }, { //This listens to click events on the node that was specified node: Y.one("#anotherNode"), eventName:"click" } ] }); panel.render(); });
To simply get rid of the default behavior, we could just set the "focusOn" and "hideOn" attributes to empty arrays.
Header/Footer Button Support
Panel supports header/footer buttons through the WidgetButtons
and Widget-StdMod
extensions. By default, it comes with a "close" button represented by the "x" in the top-right corner of the header. As a developer, you can easily add/remove buttons to the header or the footer, change the style of existing buttons, or change the markup that is used to render the buttons.
YUI().use('panel', function(Y) { var doSomethingElse = function() { ... }; var panel = new Y.Panel({ srcNode: "#myPanelContent", width: 400, centered: true, //make changes to the buttons through the "buttons" attribute, which takes an array of objects buttons: [ { //each object has a "value" property, which can be text or an HTML string, value: "Okay", // "defaultFn" which takes the function that should be executed on a click event defaultFn: function(e) { e.preventDefault(); panel.hide(); doSomethingElse(); }, // "section" which tells where to render the button and should be Y.WidgetStdMod.HEADER, or Y.WidgetStdMod.FOOTER section: Y.WidgetStdMod.FOOTER //plus an optional "href" property if you are linking to an URL } ] }); panel.render(); });
If you want to append buttons to the ones that are already present within the panel, you can use the addButton()
method.
var cancelButton = { value: "Cancel", defaultFn: function(e) { e.preventDefault(); cancelActions(); }, section: Y.WidgetStdMod.FOOTER //we could also write "header", "footer" or Y.WidgetStdMod.HEADER here. }; panel.addButton(cancelButton);
Note regarding older browsers
Panel is tested across the A-grade browser set according to the GBS Browser Test Baseline as of July 2011.
However, developers implementing Panel and other components which rely on z-index support in IE6 and IE7 should be aware of the concept of stacking context. Essentially, when setting the z-index of the widget, you should ensure that the widget's parent does not have a lower z-index.