Nguyễn Bá Hưng 4750048c38 Update usage docs
2020-08-15 19:09:01 +07:00
..
2018-03-02 16:51:27 -05:00
2018-03-22 21:25:48 -04:00
2018-03-02 16:51:27 -05:00
2020-08-15 19:09:01 +07:00

Collidable

When you use the collidable plugin you can specify which elements you can't drag over and it will freeze the mirror movement for you. These currently only work with Sortable, Swappable and Droppable.

This plugin is not included by default, so make sure to import it before using.

Usage

  • ES6:
import { Draggable, Plugins } from "@shopify/draggable";
// Or
// import Draggable from '@shopify/draggable/lib/draggable';
// import Collidable from '@shopify/draggable/lib/plugins/collidable';

const draggable = new Draggable(document.querySelectorAll("ul"), {
  draggable: "li",
  collidables: ".other-list",
  plugins: [Plugins.Collidable], // Or [Collidable]
});
  • Browser (All bundle):
<script src="https://cdn.jsdelivr.net/npm/@shopify/draggable@1.0.0-beta.11/lib/draggable.bundle.js"></script>
<script>
    const draggable = new Draggable.Draggable(document.querySelectorAll('ul'), {
      draggable: 'li',
      collidables: '.other-list',
      plugins: [Draggable.Plugins.Collidable]
    });
</script>
  • Browser (Standalone):
<script src="https://cdn.jsdelivr.net/npm/@shopify/draggable@1.0.0-beta.11/lib/draggable.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@shopify/draggable@1.0.0-beta.11/lib/plugins/collidable.js"></script>
<script>
    const draggable = new Draggable.default(document.querySelectorAll('ul'), {
      draggable: 'li',
      collidables: '.other-list',
      plugins: [Collidable.default]
    });
</script>

Options

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

Events

Name Description Cancelable Cancelable action
collidable:in Gets fired when dragging near a collidable element false -
collidable:out Gets fired when dragging away from a collidable element false -

Example

import { Sortable, Plugins } from '@shopify/draggable';

const sortable = new Sortable(document.querySelectorAll('ul'), {
  draggable: 'li',
  collidables: '.other-list',
  plugins: [Plugins.Collidable]
});

sortable.on('collidable:in', () => console.log('collidable:in'));
sortable.on('collidable:out', () => console.log('collidable:out'));

Plans

  • Improving collision detection for mirror

Caveats

  • Currently only bases collision based on mouse cursor and not mirror element