@pyreon/dnd 0.22.0 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +135 -78
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,80 +1,94 @@
|
|
|
1
1
|
# @pyreon/dnd
|
|
2
2
|
|
|
3
|
-
Signal-driven drag and drop
|
|
3
|
+
Signal-driven drag and drop over `@atlaskit/pragmatic-drag-and-drop`.
|
|
4
|
+
|
|
5
|
+
A small Pyreon-native wrapper over Atlassian's `pragmatic-drag-and-drop` (the same library Trello / Jira ship with). pdnd handles the native-event lifecycle, hit-testing, and edge detection; `@pyreon/dnd` adapts every state field into a Pyreon signal (`isDragging` / `isOver` / `activeId` / `overEdge` / `dragData`) so consumers compose with `effect` / `computed` / JSX without re-bridging. Five hooks cover the common surfaces — single draggable, single drop target, sortable list with edge detection + auto-scroll + keyboard reordering, native-file drop with MIME / count filtering, and a global drag monitor for overlays / analytics.
|
|
4
6
|
|
|
5
7
|
## Install
|
|
6
8
|
|
|
7
9
|
```bash
|
|
8
|
-
bun add @pyreon/dnd
|
|
10
|
+
bun add @pyreon/dnd @pyreon/core @pyreon/reactivity
|
|
11
|
+
# pragmatic-drag-and-drop is a runtime dependency, installed automatically
|
|
9
12
|
```
|
|
10
13
|
|
|
11
|
-
##
|
|
14
|
+
## Quick start — single draggable + drop target
|
|
12
15
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
```tsx
|
|
17
|
+
import { useDraggable, useDroppable } from '@pyreon/dnd'
|
|
18
|
+
|
|
19
|
+
function Card(props: { card: { id: string; title: string } }) {
|
|
20
|
+
let el: HTMLElement | null = null
|
|
21
|
+
const { isDragging } = useDraggable({
|
|
22
|
+
element: () => el,
|
|
23
|
+
data: { id: props.card.id, type: 'card' },
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
<div ref={(node) => (el = node)} class={() => (isDragging() ? 'opacity-50' : '')}>
|
|
28
|
+
{props.card.title}
|
|
29
|
+
</div>
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function DropZone() {
|
|
34
|
+
let el: HTMLElement | null = null
|
|
35
|
+
const { isOver } = useDroppable({
|
|
36
|
+
element: () => el,
|
|
37
|
+
canDrop: (data) => data.type === 'card',
|
|
38
|
+
onDrop: (data) => acceptCard(data.id as string),
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<div ref={(node) => (el = node)} class={() => (isOver() ? 'bg-blue-50' : '')}>
|
|
43
|
+
Drop here
|
|
44
|
+
</div>
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
```
|
|
20
48
|
|
|
21
49
|
## Hooks
|
|
22
50
|
|
|
23
|
-
### useDraggable
|
|
51
|
+
### `useDraggable({ element, data, handle?, disabled?, onDragStart?, onDragEnd? })`
|
|
24
52
|
|
|
25
|
-
Make an element draggable
|
|
53
|
+
Make an element draggable. `data` may be an object OR a function for dynamic payloads. `disabled` is reactive (accepts a function). `handle` lets you scope drag initiation to a sub-element.
|
|
26
54
|
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
element: () => cardEl,
|
|
30
|
-
data: { id: card.id, type: "card" },
|
|
31
|
-
handle: () => handleEl, // optional drag handle
|
|
32
|
-
disabled: () => locked(), // reactive disable
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
<div ref={(el) => cardEl = el} class={isDragging() ? "opacity-50" : ""}>
|
|
36
|
-
<div ref={(el) => handleEl = el}>⠿</div>
|
|
37
|
-
{card.title}
|
|
38
|
-
</div>
|
|
55
|
+
```ts
|
|
56
|
+
type Result = { isDragging: () => boolean }
|
|
39
57
|
```
|
|
40
58
|
|
|
41
|
-
### useDroppable
|
|
59
|
+
### `useDroppable({ element, data?, canDrop?, onDragEnter?, onDragLeave?, onDrop? })`
|
|
42
60
|
|
|
43
|
-
Make an element a drop target
|
|
44
|
-
|
|
45
|
-
```tsx
|
|
46
|
-
const { isOver } = useDroppable({
|
|
47
|
-
element: () => zoneEl,
|
|
48
|
-
canDrop: (data) => data.type === "card",
|
|
49
|
-
onDrop: (data) => addCard(data.id),
|
|
50
|
-
onDragEnter: () => highlight(),
|
|
51
|
-
onDragLeave: () => unhighlight(),
|
|
52
|
-
})
|
|
61
|
+
Make an element a drop target. `canDrop(sourceData)` filters; return `false` to reject. `data` is attached to the drop event so handlers can read target metadata.
|
|
53
62
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
</div>
|
|
63
|
+
```ts
|
|
64
|
+
type Result = { isOver: () => boolean }
|
|
57
65
|
```
|
|
58
66
|
|
|
59
|
-
### useSortable
|
|
67
|
+
### `useSortable({ items, by, onReorder, axis? })`
|
|
60
68
|
|
|
61
|
-
Full
|
|
69
|
+
Full sortable list with edge detection, auto-scroll, and keyboard reordering. `by` matches Pyreon's `<For by={...}>` pattern so the same key extractor flows through.
|
|
62
70
|
|
|
63
71
|
```tsx
|
|
72
|
+
const cols = signal<Column[]>([])
|
|
73
|
+
|
|
64
74
|
const { containerRef, itemRef, activeId, overId, overEdge } = useSortable({
|
|
65
|
-
items:
|
|
66
|
-
by: (
|
|
67
|
-
onReorder: (
|
|
68
|
-
axis:
|
|
75
|
+
items: () => cols(),
|
|
76
|
+
by: (c) => c.id,
|
|
77
|
+
onReorder: (next) => cols.set(next),
|
|
78
|
+
axis: 'vertical', // or 'horizontal'
|
|
69
79
|
})
|
|
70
80
|
|
|
71
|
-
|
|
72
|
-
<For each={
|
|
73
|
-
{col => (
|
|
81
|
+
;<ul ref={containerRef}>
|
|
82
|
+
<For each={cols()} by={(c) => c.id}>
|
|
83
|
+
{(col) => (
|
|
74
84
|
<li
|
|
75
85
|
ref={itemRef(col.id)}
|
|
76
|
-
class={activeId() === col.id ?
|
|
77
|
-
style={
|
|
86
|
+
class={() => (activeId() === col.id ? 'dragging' : '')}
|
|
87
|
+
style={() =>
|
|
88
|
+
overId() === col.id && overEdge() === 'top'
|
|
89
|
+
? 'border-top: 2px solid blue'
|
|
90
|
+
: ''
|
|
91
|
+
}
|
|
78
92
|
>
|
|
79
93
|
{col.name}
|
|
80
94
|
</li>
|
|
@@ -83,52 +97,95 @@ const { containerRef, itemRef, activeId, overId, overEdge } = useSortable({
|
|
|
83
97
|
</ul>
|
|
84
98
|
```
|
|
85
99
|
|
|
86
|
-
|
|
100
|
+
Behaviour:
|
|
87
101
|
|
|
88
102
|
- Auto-scroll when dragging near container edges
|
|
89
|
-
- `overEdge` signal
|
|
103
|
+
- `overEdge` signal — `'top'`/`'bottom'` (vertical) or `'left'`/`'right'` (horizontal)
|
|
90
104
|
- Keyboard reordering with Alt+Arrow keys
|
|
91
|
-
-
|
|
92
|
-
|
|
93
|
-
### useFileDrop
|
|
105
|
+
- ARIA: `role="listitem"`, `aria-roledescription`, `tabindex`
|
|
94
106
|
|
|
95
|
-
|
|
107
|
+
### `useFileDrop({ element, onDrop, accept?, maxFiles?, disabled? })`
|
|
96
108
|
|
|
97
|
-
|
|
98
|
-
const { isOver, isDraggingFiles } = useFileDrop({
|
|
99
|
-
element: () => dropZone,
|
|
100
|
-
accept: ["image/*", ".pdf"],
|
|
101
|
-
maxFiles: 5,
|
|
102
|
-
onDrop: (files) => upload(files),
|
|
103
|
-
disabled: () => uploading(),
|
|
104
|
-
})
|
|
109
|
+
Native file-drop zone. `accept` mirrors `<input accept>` syntax (`['image/*', '.pdf']`); `maxFiles` enforces an upper bound; both filter the array passed to `onDrop`.
|
|
105
110
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
</div>
|
|
111
|
+
```ts
|
|
112
|
+
type Result = {
|
|
113
|
+
isOver: () => boolean // files dragged over THIS zone
|
|
114
|
+
isDraggingFiles: () => boolean // files dragged anywhere on the page
|
|
115
|
+
}
|
|
112
116
|
```
|
|
113
117
|
|
|
114
|
-
|
|
118
|
+
`isDraggingFiles` is useful for showing a "drop here" affordance the moment files enter the window — not just when they hover the specific zone.
|
|
115
119
|
|
|
116
|
-
|
|
120
|
+
### `useDragMonitor({ canMonitor?, onDragStart?, onDrop? })`
|
|
121
|
+
|
|
122
|
+
Page-global drag state — for overlays, analytics, or coordinating multiple drag-and-drop areas.
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
type Result = {
|
|
126
|
+
isDragging: () => boolean
|
|
127
|
+
dragData: () => DragData | null
|
|
128
|
+
}
|
|
129
|
+
```
|
|
117
130
|
|
|
118
131
|
```tsx
|
|
119
132
|
const { isDragging, dragData } = useDragMonitor({
|
|
120
|
-
canMonitor: (data) => data.type ===
|
|
121
|
-
|
|
122
|
-
onDrop: (source, target) => logAnalytics(source, target),
|
|
133
|
+
canMonitor: (data) => data.type === 'card',
|
|
134
|
+
onDrop: (source, target) => track('reorder', { source, target }),
|
|
123
135
|
})
|
|
124
136
|
|
|
125
|
-
|
|
126
|
-
<div class="global-drag-overlay">
|
|
127
|
-
Dragging: {() => dragData()?.name}
|
|
128
|
-
</div>
|
|
137
|
+
;<Show when={isDragging()}>
|
|
138
|
+
<div class="global-drag-overlay">Dragging: {() => dragData()?.name}</div>
|
|
129
139
|
</Show>
|
|
130
140
|
```
|
|
131
141
|
|
|
142
|
+
## Types
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
type DragData = Record<string, unknown>
|
|
146
|
+
type DropEdge = 'top' | 'bottom' | 'left' | 'right'
|
|
147
|
+
type DropLocation = { edge: DropEdge | null; data: DragData }
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Common patterns
|
|
151
|
+
|
|
152
|
+
### Cross-list sortable (kanban columns)
|
|
153
|
+
|
|
154
|
+
Multiple `useSortable` instances pointing at different column signals — combine with `useDragMonitor` for cross-list logic.
|
|
155
|
+
|
|
156
|
+
### Disable while saving
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
useDraggable({
|
|
160
|
+
element: () => el,
|
|
161
|
+
data: { id },
|
|
162
|
+
disabled: () => isSaving(), // reactive — re-evaluates on signal change
|
|
163
|
+
})
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Dynamic data
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
useDraggable({
|
|
170
|
+
element: () => el,
|
|
171
|
+
data: () => ({ id: item.id(), position: position() }),
|
|
172
|
+
})
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Gotchas
|
|
176
|
+
|
|
177
|
+
- **Hooks are SSR-safe** — they return zero-state accessors when `document` is undefined. Real registration happens at first browser tick.
|
|
178
|
+
- **`element: () => el` must return the SAME element across reads** until the component unmounts. Reassigning `el` to a new node mid-life re-registers but stale closures from `onDragStart` callbacks fire against the OLD node.
|
|
179
|
+
- **`useSortable` requires `items` to be reactive** (a getter or signal call) — the hook needs to re-derive on insert / remove. Passing a captured array snapshot breaks reordering.
|
|
180
|
+
- **`canMonitor` / `canDrop` run on every drag event** — keep them cheap. For expensive checks, derive a flag in a `computed` upstream.
|
|
181
|
+
- **`useFileDrop` only fires on REAL file drags from the OS** — not from `useDraggable` (those go through pdnd's element adapter). The two adapters are isolated.
|
|
182
|
+
- **`onDrop` receives accepted files only** — files rejected by `accept` / `maxFiles` are silently filtered. Pair with `onDragEnter` / `isOver` if you need user feedback on rejection.
|
|
183
|
+
- **`@pyreon/dnd` does NOT bundle pdnd** — the pragmatic-drag-and-drop chunks come from your app's bundle graph. ~6KB minified for the element adapter (the common case).
|
|
184
|
+
|
|
185
|
+
## Documentation
|
|
186
|
+
|
|
187
|
+
Full docs: [docs.pyreon.dev/docs/dnd](https://docs.pyreon.dev/docs/dnd) (or `docs/docs/dnd.md` in this repo).
|
|
188
|
+
|
|
132
189
|
## License
|
|
133
190
|
|
|
134
191
|
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pyreon/dnd",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.24.0",
|
|
4
4
|
"description": "Signal-driven drag and drop for Pyreon — wraps @atlaskit/pragmatic-drag-and-drop",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/pyreon/pyreon/issues"
|
|
@@ -42,12 +42,12 @@
|
|
|
42
42
|
"@atlaskit/pragmatic-drag-and-drop": "^1.7.10",
|
|
43
43
|
"@atlaskit/pragmatic-drag-and-drop-auto-scroll": "^2.0.0",
|
|
44
44
|
"@atlaskit/pragmatic-drag-and-drop-hitbox": "^1.0.0",
|
|
45
|
-
"@pyreon/core": "^0.
|
|
46
|
-
"@pyreon/reactivity": "^0.
|
|
45
|
+
"@pyreon/core": "^0.24.0",
|
|
46
|
+
"@pyreon/reactivity": "^0.24.0"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@pyreon/test-utils": "^0.13.
|
|
50
|
-
"@pyreon/typescript": "^0.
|
|
49
|
+
"@pyreon/test-utils": "^0.13.11",
|
|
50
|
+
"@pyreon/typescript": "^0.24.0",
|
|
51
51
|
"@vitest/browser-playwright": "^4.1.4"
|
|
52
52
|
}
|
|
53
53
|
}
|