@pyreon/dnd 0.51.0 → 0.53.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 +36 -22
- package/lib/index.js +297 -134
- package/lib/index.js.map +1 -1
- package/lib/native-plugin.js +269 -0
- package/lib/native-plugin.js.map +1 -0
- package/lib/types/index.d.ts +88 -41
- package/lib/types/native-plugin.d.ts +13 -0
- package/native/kotlin/com/pyreon/runtime/PyreonSortable.kt +198 -0
- package/native/kotlin/com/pyreon/runtime/PyreonSortableModifier.kt +59 -0
- package/native/swift/PyreonSortable.swift +256 -0
- package/package.json +46 -14
- package/lib/analysis/index.js.html +0 -5406
package/README.md
CHANGED
|
@@ -17,29 +17,25 @@ bun add @pyreon/dnd @pyreon/core @pyreon/reactivity
|
|
|
17
17
|
import { useDraggable, useDroppable } from '@pyreon/dnd'
|
|
18
18
|
|
|
19
19
|
function Card(props: { card: { id: string; title: string } }) {
|
|
20
|
-
|
|
21
|
-
const { isDragging } = useDraggable({
|
|
22
|
-
element: () => el,
|
|
20
|
+
const { ref, isDragging } = useDraggable({
|
|
23
21
|
data: { id: props.card.id, type: 'card' },
|
|
24
22
|
})
|
|
25
23
|
|
|
26
24
|
return (
|
|
27
|
-
<div ref={
|
|
25
|
+
<div ref={ref} class={() => (isDragging() ? 'opacity-50' : '')}>
|
|
28
26
|
{props.card.title}
|
|
29
27
|
</div>
|
|
30
28
|
)
|
|
31
29
|
}
|
|
32
30
|
|
|
33
31
|
function DropZone() {
|
|
34
|
-
|
|
35
|
-
const { isOver } = useDroppable({
|
|
36
|
-
element: () => el,
|
|
32
|
+
const { ref, isOver } = useDroppable({
|
|
37
33
|
canDrop: (data) => data.type === 'card',
|
|
38
|
-
onDrop: (data) => acceptCard(data.id as string),
|
|
34
|
+
onDrop: (data, { edge }) => acceptCard(data.id as string, edge),
|
|
39
35
|
})
|
|
40
36
|
|
|
41
37
|
return (
|
|
42
|
-
<div ref={
|
|
38
|
+
<div ref={ref} class={() => (isOver() ? 'bg-blue-50' : '')}>
|
|
43
39
|
Drop here
|
|
44
40
|
</div>
|
|
45
41
|
)
|
|
@@ -48,23 +44,31 @@ function DropZone() {
|
|
|
48
44
|
|
|
49
45
|
## Hooks
|
|
50
46
|
|
|
51
|
-
###
|
|
47
|
+
### Binding the element: `ref` or `element`
|
|
48
|
+
|
|
49
|
+
`useDraggable` / `useDroppable` / `useFileDrop` return a `ref` callback — attach it (`<div ref={ref}>`) and the registration follows the element: it registers when the element mounts (even if that is long after the hook ran — behind `<Show>`, a lazy branch), moves when the element is swapped, and is disposed on unmount. The `element: () => el` getter still works: it is resolved on a microtask after the hook runs, and a getter that reads a **signal** re-registers when the signal changes. A getter that is still `null` at that point (with no `ref` attached) prints a `[Pyreon]` dev warning instead of silently registering nothing.
|
|
50
|
+
|
|
51
|
+
### `useDraggable({ element?, data, handle?, disabled?, preview?, onDragStart?, onDragEnd? })`
|
|
52
52
|
|
|
53
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.
|
|
54
54
|
|
|
55
55
|
```ts
|
|
56
|
-
type Result = { isDragging: () => boolean }
|
|
56
|
+
type Result = { isDragging: () => boolean; ref: (el: HTMLElement | null) => void }
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
-
### `useDroppable({ element
|
|
59
|
+
### `useDroppable<TTarget, TSource>({ element?, data?, canDrop?, onDragEnter?, onDragLeave?, onDrop?, edges?, sticky? })`
|
|
60
60
|
|
|
61
|
-
Make an element a drop target. `canDrop(sourceData)` filters; return `false` to reject. `data`
|
|
61
|
+
Make an element a drop target. `canDrop(sourceData)` filters; return `false` to reject — type the SOURCE data with the second generic. `onDrop(sourceData, { edge, data })` receives where it landed: the closest configured edge (`null` without `edges`) and this target's own `data`, captured before `isOver` / `overEdge` reset.
|
|
62
62
|
|
|
63
63
|
```ts
|
|
64
|
-
type Result = {
|
|
64
|
+
type Result = {
|
|
65
|
+
isOver: () => boolean
|
|
66
|
+
overEdge: () => DropEdge | null // live closest edge while hovered (needs `edges`)
|
|
67
|
+
ref: (el: HTMLElement | null) => void
|
|
68
|
+
}
|
|
65
69
|
```
|
|
66
70
|
|
|
67
|
-
### `useSortable({ items, by, onReorder, axis? })`
|
|
71
|
+
### `useSortable({ items, by, onReorder, axis?, label?, disabled?, groupId?, onCrossListDrop?, onCrossListReceive? })`
|
|
68
72
|
|
|
69
73
|
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.
|
|
70
74
|
|
|
@@ -101,18 +105,24 @@ Behaviour:
|
|
|
101
105
|
|
|
102
106
|
- Auto-scroll when dragging near container edges
|
|
103
107
|
- `overEdge` signal — `'top'`/`'bottom'` (vertical) or `'left'`/`'right'` (horizontal)
|
|
104
|
-
- Keyboard reordering
|
|
105
|
-
- ARIA: `role="listitem"
|
|
108
|
+
- Keyboard reordering — Space/Enter picks the focused item up, the axis arrows move it, Space/Enter drops it, Escape cancels and restores its position; Alt+Arrow moves directly
|
|
109
|
+
- ARIA: `role="listitem"` (only when the item has no role of its own), `role="list"` on a non-`<ul>`/`<ol>` container, `aria-roledescription`, `tabindex`
|
|
110
|
+
- `label: (item) => string` — human names in screen-reader announcements
|
|
111
|
+
- `disabled` (boolean or accessor) — blocks pointer drags and keyboard reordering
|
|
112
|
+
- `groupId` + `onCrossListDrop` / `onCrossListReceive` — drag between sortables sharing a group (kanban)
|
|
113
|
+
- `itemHandleRef(key)` — scope drag initiation to a grip element inside the row
|
|
114
|
+
- `overEdge()`, `activeId()`, `overId()`, plus the O(2) `isActive(key)` / `isOverKey(key)` selectors
|
|
106
115
|
- Fine-grained teardown: each **item** registration is disposed the moment its `itemRef` fires with `null` (or re-registers), and the **container** registration (auto-scroll + reorder drop-target + keyboard handler) is disposed on `containerRef(null)` / re-register — so a churning `<For>` list _and_ a `<Show>`-toggled container never leak listeners on detached elements.
|
|
107
116
|
|
|
108
|
-
### `useFileDrop({ element
|
|
117
|
+
### `useFileDrop({ element?, onDrop, onReject?, accept?, maxFiles?, disabled? })`
|
|
109
118
|
|
|
110
|
-
Native file-drop zone. `accept` mirrors `<input accept>` syntax (`['image/*', '.pdf']`); `maxFiles` enforces an upper bound; both filter the array passed to `onDrop`.
|
|
119
|
+
Native file-drop zone. `accept` mirrors `<input accept>` syntax (`['image/*', '.pdf']`, and `'*'` / `'*/*'` for anything); `maxFiles` enforces an upper bound; both filter the array passed to `onDrop`. `onReject(files, reason)` receives what was filtered out (`reason`: `'accept'` or `'maxFiles'`).
|
|
111
120
|
|
|
112
121
|
```ts
|
|
113
122
|
type Result = {
|
|
114
123
|
isOver: () => boolean // files dragged over THIS zone
|
|
115
124
|
isDraggingFiles: () => boolean // files dragged anywhere on the page
|
|
125
|
+
ref: (el: HTMLElement | null) => void
|
|
116
126
|
}
|
|
117
127
|
```
|
|
118
128
|
|
|
@@ -175,7 +185,7 @@ useDraggable({
|
|
|
175
185
|
|
|
176
186
|
## Accessibility (built in)
|
|
177
187
|
|
|
178
|
-
`useSortable` announces drags through `@pyreon/a11y`'s live region — "Picked up Alice", "Moved Alice to position 2 of 3", "Dropped …" — and auto-creates a visually-hidden instructions node
|
|
188
|
+
`useSortable` announces drags through `@pyreon/a11y`'s live region — "Picked up Alice", "Moved Alice to position 2 of 3", "Dropped …" — and auto-creates a visually-hidden instructions node linked to every item via `aria-describedby` (it lives in a shared host on `document.body`, never inside your list). Pass `label: (item) => string` so announcements use human names instead of raw keys. Keyboard pickup mode (Space/Enter, arrows, Escape) and Alt+Arrow direct moves ship by default.
|
|
179
189
|
|
|
180
190
|
## Custom drag previews, edges, handles
|
|
181
191
|
|
|
@@ -186,17 +196,21 @@ useDraggable({
|
|
|
186
196
|
## Gotchas
|
|
187
197
|
|
|
188
198
|
- **Hooks are SSR-safe** — they return zero-state accessors when `document` is undefined. Real registration happens at first browser tick.
|
|
189
|
-
-
|
|
199
|
+
- **Prefer the returned `ref` over `element: () => el`.** A getter reading a plain `let` is resolved once (on a microtask); it can only re-resolve when it reads a signal. The `ref` follows mounts, swaps and unmounts by construction.
|
|
190
200
|
- **`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.
|
|
191
201
|
- **`canMonitor` / `canDrop` run on every drag event** — keep them cheap. For expensive checks, derive a flag in a `computed` upstream.
|
|
192
202
|
- **`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.
|
|
193
|
-
- **`onDrop` receives accepted files only** — files rejected by `accept` / `maxFiles`
|
|
203
|
+
- **`onDrop` receives accepted files only** — files rejected by `accept` / `maxFiles` go to `onReject(files, reason)`.
|
|
194
204
|
- **`@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).
|
|
195
205
|
|
|
196
206
|
## Performance
|
|
197
207
|
|
|
198
208
|
The signal-driven layer adds **near-zero overhead** over calling pdnd directly. A package-level benchmark (`bun run bench`) measures the _wrapper tax_ — the JS the ergonomic hook adds over a hand-rolled Pyreon+pdnd integration wiring the same reactive state — against the **real** pdnd build under happy-dom, with per-`(op × impl)` process isolation and a bootstrap CI95. `useDraggable` / `useDroppable` / `useSortable`-item mount→unmount lifecycles all tie the hand-rolled baseline (CI overlap); `useDragMonitor` adds ~one closure allocation per mount; per drag-event dispatch adds one optional-callback hop. Reactive DnD at ~raw-pdnd cost. The bench also measures the ROW-BINDING idiom at scale: at N=1000 sortable rows, one row-enter costs ~26µs with the naive `overId() === key` equality idiom (every row subscribes — O(N) notifies) vs ~1.5µs with the `isOverKey(key)` selector idiom (**~18× / 94% faster**, `createSelector`-backed O(2) notifies — use the selectors, they exist for exactly this). (Real pointer-gesture timing is browser-dependent and out of scope for the micro-bench; author-judge disclosed.)
|
|
199
209
|
|
|
210
|
+
## Native (iOS / Android)
|
|
211
|
+
|
|
212
|
+
`useSortable({ items, by, onReorder })` lowers to the `PyreonSortableState` engine on SwiftUI and Compose, and `ref={s.containerRef}` / `ref={s.itemRef(key)}` to the drag view modifiers (list reorder within one list — cross-list boards and the element-getter hooks have no native lowering). The compile-time half ships in this package (`src/native-plugin/`, declared in `package.json` as `pyreon.native.plugin` and discovered by `pyreon native`), not in `@pyreon/native-compiler`. `--no-plugins` builds treat the package like any unclaimed library.
|
|
213
|
+
|
|
200
214
|
## Documentation
|
|
201
215
|
|
|
202
216
|
Full docs: [pyreon.dev/docs/dnd](https://pyreon.dev/docs/dnd) (or `docs/src/content/docs/dnd.md` in this repo).
|