|
| 1 | +# Default usage |
| 2 | + |
| 3 | +```typescript |
| 4 | +import {Sortable} from '@shopify/draggable'; |
| 5 | + |
| 6 | +const sortable = new Sortable(document.querySelectorAll('ul'), { |
| 7 | + draggable: 'li', |
| 8 | +}); |
| 9 | + |
| 10 | +// The type of the first argument is SortableEventNames |
| 11 | +sortable.on('sortable:sort', (evt) => { |
| 12 | + // The type of evt is SortableSortEvent |
| 13 | +}); |
| 14 | + |
| 15 | +// The type of the first argument is SortableEventNames |
| 16 | +sortable.on('drag:out:container', (evt) => { |
| 17 | + // The type of evt is DragOutContainerEvent |
| 18 | +}); |
| 19 | +``` |
| 20 | + |
| 21 | +# Using plugins |
| 22 | + |
| 23 | +When creating an instance with plugins with events, you need to manually specify the event names. |
| 24 | + |
| 25 | +```typescript |
| 26 | +import {Droppable, Plugins} from '@shopify/draggable'; |
| 27 | + |
| 28 | +// 1. import the event names you need |
| 29 | +import type { |
| 30 | + DroppableEventNames, |
| 31 | + CollidableEventNames, |
| 32 | +} from "@shopify/draggable"; |
| 33 | + |
| 34 | +// 2. Specify the event names when create the instance |
| 35 | +const droppable = new Droppable<DroppableEventNames | CollidableEventNames>(document.querySelectorAll('.container'), { |
| 36 | + draggable: '.item', |
| 37 | + dropzone: '.dropzone', |
| 38 | + collidables: '.other-list', |
| 39 | + plugins: [Plugins.Collidable], |
| 40 | +}); |
| 41 | + |
| 42 | +// The type of the first argument can be DroppableEventNames or CollidableEventNames |
| 43 | +droppable.on('droppable:dropped', (evt) => { |
| 44 | + // The type of evt is DroppableDroppedEvent |
| 45 | +}); |
| 46 | + |
| 47 | +// The type of the first argument can be DroppableEventNames or CollidableEventNames |
| 48 | +droppable.on('collidable:in', (evt) => { |
| 49 | + // The type of evt is CollidableInEvent |
| 50 | +}); |
| 51 | +``` |
0 commit comments