Back to Draggable

README

src/Swappable/README.md

1.2.12.8 KB
Original Source

Swappable

Swappable is built on top of Draggable and allows you to swap elements by dragging over them. No order will be maintained (unlike Sortable), so any draggable element that gets dragged over will be swapped with the source element.

Usage

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

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

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

API

Check out Draggables API for the base API

Options

Check out Draggables options for the base options

Events

Check out Draggables events for base events

NameDescriptionCancelableCancelable action
swappable:startGets fired when starting to dragtruePrevents drag
swappable:swapGets fired before the source gets swappedtruePrevents swap
swappable:swappedGets fired before the source gets swappedfalse-
swappable:stopGets fired when dragging out of a droppable elementfalse-

Classes

Check out Draggables class identifiers

Example

This sample code will make list items draggable and allows you to swap them with other draggable elements:

js
import {Swappable} from '@shopify/draggable';

const swappable = new Swappable(document.querySelectorAll('ul'), {
  draggable: 'li',
});

swappable.on('swappable:start', () => console.log('swappable:start'));
swappable.on('swappable:swapped', () => console.log('swappable:swapped'));
swappable.on('swappable:stop', () => console.log('swappable:stop'));