Back to Draggable

README

src/Droppable/README.md

1.2.14.5 KB
Original Source

Droppable

Droppable is built on top of Draggable and allows you to declare draggable and droppable elements via options. Droppable fires four events on top of the draggable events: droppable:start, droppable:dropped, droppable:returned and droppable:stop. Droppable elements must begin in an occupied dropzone (see below, Classes and example), so they may returned if the drag is canceled or returned.

Usage

  • NPM:
js
import {Droppable} from '@shopify/draggable';
// Or
import Droppable from '@shopify/draggable/build/esm/Droppable/Droppable';

const droppable = new Droppable(document.querySelectorAll('.container'), {
  draggable: '.item',
  dropzone: '.dropzone',
});
  • Browser (as a module):
html
<script type="module">
  import Droppable from 'https://cdn.jsdelivr.net/npm/@shopify/draggable/build/esm/Droppable/Droppable.mjs';

  const droppable = new Droppable(document.querySelectorAll('.container'), {
    draggable: '.item',
    dropzone: '.dropzone',
  });
</script>
  • Browser (Standalone):
html
<script src="https://cdn.jsdelivr.net/npm/@shopify/draggable/build/umd/index.min.js"></script>
<script>
  const droppable = new Draggable.Droppable(
    document.querySelectorAll('.container'),
    {
      draggable: '.item',
      dropzone: '.dropzone',
    },
  );
</script>

API

Check out Draggable API for the base API

Options

Check out Draggable options for the base options

dropzone {String|HTMLElement[]|NodeList|Function} A css selector string, an array of elements, a NodeList or a function returning elements for dropzone elements within the containers.

Events

Check out Draggable events for the base events

NameDescriptionCancelableCancelable action
droppable:startGets fired before dropping the draggable element into a dropzonetruePrevents drag
droppable:droppedGets fired when dropping draggable element into a dropzonetruePrevents drop
droppable:returnedGets fired when draggable elements returns to original dropzonetruePrevents return
droppable:stopGets fired before dropping the draggable element into a dropzone elementfalse-

Classes

Check out Draggable class identifiers for the base class identifiers

NameDescriptionDefault
droppable:activeClass added to the unoccupied dropzone elements when drag startsdraggable-droppable--active
droppable:occupiedClass added to the dropzone element when it contains a draggable elementdraggable-droppable--occupied

Example

This sample HTML and JavaScript will make .item elements draggable and droppable among all .dropzone elements:

html
<div class="container">
  <div class="dropzone draggable-dropzone--occupied">
    <div class="item">A</div>
  </div>
  <div class="dropzone draggable-dropzone--occupied">
    <div class="item">B</div>
  </div>
  <div class="dropzone draggable-dropzone--occupied">
    <div class="item">C</div>
  </div>
</div>

<div class="container">
  <div class="dropzone"></div>
</div>

<style>
  .item {
    height: 100%;
  }
  .dropzone {
    outline: solid 1px;
    height: 50px;
  }
  .draggable-dropzone--occupied {
    background: lightgreen;
  }
</style>
js
import {Droppable} from '@shopify/draggable';

const droppable = new Droppable(document.querySelectorAll('.container'), {
  draggable: '.item',
  dropzone: '.dropzone',
});

droppable.on('droppable:dropped', () => console.log('droppable:dropped'));
droppable.on('droppable:returned', () => console.log('droppable:returned'));