Skip to content

Commit 03012df

Browse files
authored
Merge pull request #428 from zjffun/doc/typescript
[doc] add documentation of using Draggable with TypeScript
2 parents 2619c81 + bbb7625 commit 03012df

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ You can find the documentation for each module within their respective directori
126126
- [Swappable](src/Swappable)
127127
- [SwappableEvent](src/Swappable/SwappableEvent)
128128

129+
## TypeScript
130+
131+
Draggable includes [TypeScript](http://typescriptlang.org) definitions.
132+
133+
[Documentation](doc/typescript.md)
134+
129135
## Running examples
130136

131137
To run the `examples` project locally, simply run the following from the `draggable` root:

doc/typescript.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)