YUI 3: Drag & Drop (DD)

The Drag & Drop Utility allows you to create a draggable interface efficiently, buffering you from browser-level abnormalities and enabling you to focus on the interesting logic surrounding your particular implementation. This component enables you to create a variety of standard draggable objects with just a few lines of code and then, using its extensive API, add your own specific implementation logic.

For more information about drag and drop as a design pattern, see the Yahoo! Design Pattern Library.

Getting Started

Include Dependencies

The easiest way to include the source files for Drag & Drop and its dependencies is to add the YUI seed file to your page, using the following script tag, and allow the YUI instance to download any additional files which may be required:

  1. <script src="http://yui.yahooapis.com/3.0.0/build/yui/yui-min.js"></script>
<script src="http://yui.yahooapis.com/3.0.0/build/yui/yui-min.js"></script>

The YUI instance will automatically pull down Drag & Drop's source files and any missing dependencies when the dd module is used. This helps you avoid having to manually manage the list of files needed on your page to support multiple components while also optimizing your initial page weight by loading files only when they are required.

If you do want to include file dependencies manually on your page, the YUI Dependency Configurator can be used to determine the list of files you need to include in order to use Drag & Drop.

The YUI Instance

Once you have the YUI seed file on your page (yui-min.js), you can create a new YUI instance for your application to use and populate it with the modules you need, specified as the first set of arguments to the use method:

  1. // Create new YUI instance, and populate it with the required modules
  2. YUI().use('dd', function(Y) {
  3.  
  4. // Drag & Drop available, and ready for use.
  5.  
  6. });
// Create new YUI instance, and populate it with the required modules
YUI().use('dd', function(Y) {
 
    // Drag & Drop available, and ready for use.
 
});

The last argument passed to use is a callback function. The callback function will be invoked as soon as the YUI instance is done downloading any required files missing from your page. Once those files are loaded, your local YUI instance will be supplemented with the classes which make up the dd module and any modules it depends on. A reference to the populated YUI instance (Y) is passed back to your callback function. Within your callback, then, you can start writing your application code based on your own custom instance of YUI.

For more information on creating instances of YUI and the use method, see the YUI Global object documentation.

Using the Drag & Drop Utility

This section describes how to use the Drag & Drop Utility in further detail. It contains these subsections:

Module Descriptions

Drag & Drop for YUI 3.0 has been broken up into several modules to allow you, as the implementer, to pick how you want it to work and what code you need on your page.

Module Name Description
dd-ddm-base Base DragDrop Manager, required to make something draggable.
dd-ddm Adds shim support, only needed when you need to drag over something that steals mouse events or you are targeting.
dd-ddm-drop Adds Drop support, only required when you have drop targets you need to interact with.
dd-drag Main drag class, this makes something draggable.
dd-proxy Adds proxy support to the main drag class.
dd-constrain Adds constrain support to the main drag class.
dd-scroll Adds node and window based scroll support.
dd-drop Drop Support, this is the support for all drop targets.
dd-plugin Node plugin for Drag
dd-drop-plugin Node plugin for Drop
dd All of the above modules rolled up into one file.

Simple Drag & Drop

Basic Drag

You create a simple Drag instance by including the dd-drag module and using the following code:

  1. var Y = new YUI().use('dd-drag', function(Y) {
  2. var dd = new Y.DD.Drag({
  3. node: '#drag'
  4. });
  5. });
var Y = new YUI().use('dd-drag', function(Y) {
    var dd = new Y.DD.Drag({
        node: '#drag'
    });
});
Basic Proxy Drag

You create a simple Proxy Drag instance by including the dd-drag and dd-proxy modules and using the following code:

  1. var Y = new YUI().use('dd-drag', 'dd-proxy', function(Y) {
  2. var dd = new Y.DD.Drag({
  3. node: '#drag'
  4. }).plug(Y.Plugin.DDProxy); //This config option makes the node a Proxy Drag
  5. });
var Y = new YUI().use('dd-drag', 'dd-proxy', function(Y) {
    var dd = new Y.DD.Drag({
        node: '#drag'
    }).plug(Y.Plugin.DDProxy); //This config option makes the node a Proxy Drag
});
Basic Drop Target

You create a simple Drop Target instance by including the dd-drop module and using the following code:

  1. var Y = new YUI().use('dd-drop', function(Y) {
  2. var drop = new Y.DD.Drop({
  3. node: '#drop'
  4. });
  5. });
var Y = new YUI().use('dd-drop', function(Y) {
    var drop = new Y.DD.Drop({
        node: '#drop'
    });
});

Events

Drag & Drop for YUI 3.0 has been been packed with useful events to allow the implementer to control the end user experience.

Event Target Preventable Stoppable Bubbles Description
drag:mouseDown Drag yes yes yes Handles the mousedown DOM event, checks to see if you have a valid handle then starts the drag timers.
drag:afterMouseDown Drag no no yes Fires after the mousedown event has been cleared.
drag:align Drag yes yes yes Fires when this node is aligned.
drag:removeHandle Drag no no yes Fires after a handle is removed.
drag:addHandle Drag no no yes Fires after a handle is added.
drag:removeInvalid Drag no no yes Fires after an invalid selector is removed.
drag:addInvalid Drag no no yes Fires after an invalid selector is added.
drag:start Drag no no yes Fires at the start of a drag operation.
drag:end Drag yes yes yes Fires at the end of a drag operation.
drag:drag Drag yes yes yes Fires every mousemove during a drag operation.
drag:over Drag no no yes Fires when this node is over a Drop Target. (Fired from dd-drop)
drag:enter Drag no no yes Fires when this node enters a Drop Target. (Fired from dd-drop)
drag:exit Drag no no yes Fires when this node exits a Drop Target. (Fired from dd-drop)
drag:drophit Drag no no yes Fires when this node is dropped on a valid Drop Target. (Fired from dd-ddm-drop)
drag:dropmiss Drag no no yes Fires when this node is dropped on an invalid Drop Target. (Fired from dd-ddm-drop)
drop:over Drop no no yes Fires when a drag element is over this target.
drop:enter Drop no no yes Fires when a drag element enters this target.
drop:exit Drop no no yes Fires when a drag element exits this target.
drop:hit Drop no no yes Fires when a draggable node is dropped on this Drop Target. (Fired from dd-ddm-drop)

Event Bubbling

To allow for a truly Event driven application, all Drag & Drop related events are bubbled to the DragDropMgr.

Per YUI instance, you can listen for all Drag & Drop events from a central location.

So instead of doing this:

  1. var doSomething = function() {
  2. Y.log('Do Something Here');
  3. };
  4. dd1.on('drag:drag', doSomething);
  5. dd2.on('drag:drag', doSomething);
  6. dd3.on('drag:drag', doSomething);
  7. dd4.on('drag:drag', doSomething);
  8. dd5.on('drag:drag', doSomething);
  9. dd6.on('drag:drag', doSomething);
  10. dd7.on('drag:drag', doSomething);
  11. dd8.on('drag:drag', doSomething);
  12. dd9.on('drag:drag', doSomething);
var doSomething = function() {
    Y.log('Do Something Here');
};
dd1.on('drag:drag', doSomething);
dd2.on('drag:drag', doSomething);
dd3.on('drag:drag', doSomething);
dd4.on('drag:drag', doSomething);
dd5.on('drag:drag', doSomething);
dd6.on('drag:drag', doSomething);
dd7.on('drag:drag', doSomething);
dd8.on('drag:drag', doSomething);
dd9.on('drag:drag', doSomething);

You can now do this:

  1. var doSomething = function() {
  2. Y.log('Do Something Here');
  3. };
  4. Y.DD.DDM.on('drag:drag', doSomething);
var doSomething = function() {
    Y.log('Do Something Here');
};
Y.DD.DDM.on('drag:drag', doSomething);

CSS Class Names

The Drag & Drop Utility adds CSS class names for important moments in the drag and drop operation. Below you will find the list of these class names.

The Drag & Drop Utility doesn't ship with a default skin, so no style rules are attached to these class names. That is completely left up to the implementer.

Class Name Target State
yui-dd-draggable Drag Given to all Drag Nodes
yui-dd-locked Drag Added when a Drag instance is locked
yui-dd-dragging Drag Added while a Drag instance is active
yui-dd-proxy Proxy Given to the Proxy Node
yui-dd-drop Drop Given to all Drop Targets
yui-dd-drop-locked Drop Added when a Drop instance is locked
yui-dd-drop-active-valid Drop Added to a Drop when it is an valid target for the current drag operation
yui-dd-drop-active-invalid Drop Added to a Drop when it is an invalid target for the current drag operation
yui-dd-drop-over Drop Added when a Drag instance is over this Drop Target

Support & Community

Forums & Blog

YUI 3 discussion forums are hosted on YUILibrary.com.

In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.

Filing Bugs & Feature Requests

The YUI Library's public bug tracking and feature request repositories are located on the YUILibrary.com site. Before filing new feature requests or bug reports, please review our reporting guidelines.

Copyright © 2010 Yahoo! Inc. All rights reserved. Copyright | Privacy Policy | Terms of Use | Job Openings