@editora/ui-sortable 0.1.13 → 0.2.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.
@@ -0,0 +1,426 @@
1
+ const P = {
2
+ instructions: "Press Space to pick up. While it is picked up, use the arrow keys to move it, Space to drop it, Escape to cancel.",
3
+ lifted: ({ itemLabel: i, position: t, total: e }) => `Picked up ${i}. Position ${t} of ${e}.`,
4
+ moved: ({ itemLabel: i, position: t, total: e }) => `${i}: position ${t} of ${e}.`,
5
+ movedToList: ({ itemLabel: i, listLabel: t, position: e, total: n }) => `${i}: ${t}, position ${e} of ${n}.`,
6
+ dropped: ({ itemLabel: i, listLabel: t, position: e, total: n }) => `Dropped ${i}${t ? ` in ${t}` : ""}, position ${e} of ${n}.`,
7
+ cancelled: ({ itemLabel: i }) => `Cancelled. ${i} is back where it was.`
8
+ }, w = /* @__PURE__ */ new Map();
9
+ let y = null;
10
+ const T = /* @__PURE__ */ new Map();
11
+ function M(i) {
12
+ if (typeof document > "u" || !i) return;
13
+ (!y || !y.isConnected) && (y = document.createElement("div"), y.setAttribute("aria-live", "assertive"), y.setAttribute("aria-atomic", "true"), y.setAttribute("data-sortable-announcer", ""), $(y), document.body.appendChild(y)), y.textContent = "";
14
+ const t = y;
15
+ setTimeout(() => {
16
+ t.textContent = i;
17
+ }, 20);
18
+ }
19
+ function R(i) {
20
+ let t = T.get(i);
21
+ return (!t || !t.isConnected) && (t = document.createElement("div"), t.id = `sortable-instructions-${T.size + 1}`, t.textContent = i, $(t), document.body.appendChild(t), T.set(i, t)), t.id;
22
+ }
23
+ function $(i) {
24
+ Object.assign(i.style, {
25
+ position: "absolute",
26
+ width: "1px",
27
+ height: "1px",
28
+ margin: "-1px",
29
+ padding: "0",
30
+ overflow: "hidden",
31
+ clip: "rect(0 0 0 0)",
32
+ whiteSpace: "nowrap",
33
+ border: "0"
34
+ });
35
+ }
36
+ const k = () => typeof matchMedia == "function" && matchMedia("(prefers-reduced-motion: reduce)").matches, S = 'input, textarea, select, option, [contenteditable=""], [contenteditable="true"], [data-sortable-ignore]', D = 'a[href], button, input, select, textarea, [tabindex], [contenteditable=""], [contenteditable="true"]';
37
+ function F(i, t, e) {
38
+ const n = i.slice();
39
+ if (t < 0 || t >= n.length) return n;
40
+ const [o] = n.splice(t, 1);
41
+ return n.splice(Math.max(0, Math.min(e, n.length)), 0, o), n;
42
+ }
43
+ function z(i, t, e, n) {
44
+ const o = i.slice(), l = t.slice(), [c] = o.splice(e, 1);
45
+ return c !== void 0 && l.splice(Math.max(0, Math.min(n, l.length)), 0, c), [o, l];
46
+ }
47
+ function K(i, t) {
48
+ const e = {};
49
+ for (const l of Object.keys(i)) e[l] = i[l].slice();
50
+ if (t.type === "remove") return e;
51
+ const n = t.fromId ?? "", o = t.toId ?? "";
52
+ return t.type === "update" ? e[n] = F(i[n] || [], t.oldIndex, t.newIndex) : [e[n], e[o]] = z(i[n] || [], i[o] || [], t.oldIndex, t.newIndex), e;
53
+ }
54
+ let s = null;
55
+ class H {
56
+ constructor(t, e) {
57
+ this.observer = null, this.added = /* @__PURE__ */ new WeakMap(), this.pending = null, this.listeners = [], this.onPointerDown = (n) => {
58
+ if (this.options.disabled || s || this.pending || n.pointerType === "mouse" && n.button !== 0 || n.__sortable) return;
59
+ const o = n.target, l = this.itemOf(o);
60
+ if (l) {
61
+ if (n.__sortable = !0, this.options.handle) {
62
+ const c = o.closest(this.options.handle);
63
+ if (!c || !l.contains(c)) return;
64
+ } else if (o.closest(S))
65
+ return;
66
+ this.pending = { pointerId: n.pointerId, item: l, target: o, x: n.clientX, y: n.clientY, type: n.pointerType }, n.pointerType === "touch" && (this.pending.timer = window.setTimeout(() => {
67
+ this.pending && this.begin(this.pending.item, "pointer", this.pending.x, this.pending.y);
68
+ }, this.options.touchDelay ?? 200)), this.listen(window, "pointermove", this.onPointerMove, { passive: !1 }), this.listen(window, "pointerup", this.onPointerUp), this.listen(window, "pointercancel", this.onPointerCancel), this.listen(window, "keydown", this.onDragKeyDown);
69
+ }
70
+ }, this.onDragKeyDown = (n) => {
71
+ n.key !== "Escape" || !s || s.source !== "pointer" || s.from !== this || (n.preventDefault(), this.cancelPending(), x(!0));
72
+ }, this.onTouchMove = (n) => {
73
+ s && s.source === "pointer" && n.cancelable && n.preventDefault();
74
+ }, this.onPointerMove = (n) => {
75
+ const o = this.pending;
76
+ if (!(o && n.pointerId !== o.pointerId)) {
77
+ if (!s && o) {
78
+ const l = Math.hypot(n.clientX - o.x, n.clientY - o.y);
79
+ if (o.type === "touch") {
80
+ l > 8 && this.cancelPending();
81
+ return;
82
+ }
83
+ if (l < (this.options.threshold ?? 4)) return;
84
+ this.begin(o.item, "pointer", o.x, o.y);
85
+ }
86
+ s && s.source === "pointer" && s.from === this && (n.preventDefault(), L(n.clientX, n.clientY));
87
+ }
88
+ }, this.onPointerUp = () => {
89
+ const n = !!(s && s.source === "pointer" && s.from === this);
90
+ this.cancelPending(), n && (x(!1), j());
91
+ }, this.onPointerCancel = () => {
92
+ const n = !!(s && s.source === "pointer" && s.from === this);
93
+ this.cancelPending(), n && x(!0);
94
+ }, this.onKeyDown = (n) => {
95
+ if (this.options.keyboard === !1 || this.options.disabled) return;
96
+ const o = n.target, l = this.itemOf(o);
97
+ if (!l || o !== this.focusTargetOf(l) && o.closest(S)) return;
98
+ if (!(s && s.source === "keyboard" && s.item === l)) {
99
+ (n.key === " " || n.key === "Enter") && o === this.focusTargetOf(l) && !s && (n.preventDefault(), this.begin(l, "keyboard"));
100
+ return;
101
+ }
102
+ const r = n.key;
103
+ r === " " || r === "Enter" ? (n.preventDefault(), x(!1)) : r === "Escape" ? (n.preventDefault(), x(!0)) : r === "Tab" ? x(!1) : ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Home", "End"].includes(r) && (n.preventDefault(), X(r));
104
+ }, this.element = t, this.options = e, this.listen(t, "pointerdown", this.onPointerDown), this.listen(t, "keydown", this.onKeyDown), this.listen(t, "touchmove", this.onTouchMove, { passive: !1 }), this.listen(t, "dragstart", ((n) => {
105
+ this.itemOf(n.target) && n.preventDefault();
106
+ })), this.listen(t, "focusout", ((n) => {
107
+ if (!s || s.source !== "keyboard" || s.from !== this) return;
108
+ const o = n.relatedTarget;
109
+ o && s.item.contains(o) || setTimeout(() => {
110
+ s && s.source === "keyboard" && !s.item.contains(document.activeElement) && x(!1);
111
+ }, 0);
112
+ })), this.prepareItems(), typeof MutationObserver < "u" && (this.observer = new MutationObserver(() => this.prepareItems()), this.observer.observe(t, { childList: !0 }));
113
+ }
114
+ // -- items --------------------------------------------------------------------------------------------------------
115
+ items() {
116
+ const t = this.options.itemSelector;
117
+ return Array.from(this.element.children).filter(
118
+ (e) => e instanceof HTMLElement && !e.hasAttribute("data-sortable-ghost") && (!t || e.matches(t))
119
+ );
120
+ }
121
+ itemOf(t) {
122
+ let e = t;
123
+ for (; e && e !== this.element; ) {
124
+ if (e.parentNode === this.element && e instanceof HTMLElement)
125
+ return this.items().includes(e) ? e : null;
126
+ e = e.parentNode;
127
+ }
128
+ return null;
129
+ }
130
+ idOf(t) {
131
+ return this.options.getId ? this.options.getId(t) : t.getAttribute("data-id") || t.id || null;
132
+ }
133
+ listId() {
134
+ return this.options.id ?? (this.element.getAttribute("data-sortable-id") || this.element.id || null);
135
+ }
136
+ listLabel() {
137
+ const t = this.element.getAttribute("aria-labelledby"), e = t ? document.getElementById(t.split(" ")[0]) : null;
138
+ return this.element.getAttribute("aria-label") || e?.textContent?.trim() || this.listId() || "";
139
+ }
140
+ announcements() {
141
+ return { ...P, ...this.options.announcements };
142
+ }
143
+ // Keyboard users need something to focus: the handle if there is one, else the item.
144
+ prepareItems() {
145
+ if (this.options.keyboard === !1) return;
146
+ const t = R(this.announcements().instructions);
147
+ for (const e of this.items()) {
148
+ const n = this.focusTargetOf(e), o = this.added.get(n) || [];
149
+ n.matches(D) || (n.setAttribute("tabindex", "0"), o.includes("tabindex") || o.push("tabindex")), n.hasAttribute("aria-describedby") || (n.setAttribute("aria-describedby", t), o.push("aria-describedby")), o.length && this.added.set(n, o);
150
+ }
151
+ }
152
+ focusTargetOf(t) {
153
+ return (this.options.handle ? t.querySelector(this.options.handle) : null) || t;
154
+ }
155
+ context(t) {
156
+ const e = this.items(), n = t.getAttribute("aria-label") || (t.textContent || "").trim().replace(/\s+/g, " ").slice(0, 80);
157
+ return { item: t, itemLabel: n || "Item", listLabel: this.listLabel(), position: e.indexOf(t) + 1, total: e.length };
158
+ }
159
+ /** The lists an item of this one can go to: itself, and those of the same group. */
160
+ peers() {
161
+ const t = this.options.group;
162
+ return t ? Array.from(w.values()).filter((e) => e === this || e.options.group === t && !e.options.disabled) : [this];
163
+ }
164
+ cancelPending() {
165
+ this.pending?.timer && clearTimeout(this.pending.timer), this.pending = null, (!s || s.from !== this) && this.unlistenWindow();
166
+ }
167
+ unlistenWindow() {
168
+ this.listeners = this.listeners.filter(([t, e, n, o]) => t !== window ? !0 : (t.removeEventListener(e, n, o), !1));
169
+ }
170
+ listen(t, e, n, o) {
171
+ t.addEventListener(e, n, o), this.listeners.push([t, e, n, o]);
172
+ }
173
+ destroy() {
174
+ s && s.from === this && x(!0), this.observer?.disconnect(), this.listeners.forEach(([t, e, n, o]) => t.removeEventListener(e, n, o)), this.listeners = [];
175
+ for (const t of this.items())
176
+ for (const e of [t, ...Array.from(t.querySelectorAll("[tabindex], [aria-describedby]"))])
177
+ this.added.get(e)?.forEach((n) => e.removeAttribute(n)), this.added.delete(e);
178
+ w.get(this.element) === this && w.delete(this.element);
179
+ }
180
+ // -- dragging -----------------------------------------------------------------------------------------------------
181
+ begin(t, e, n = 0, o = 0) {
182
+ if (s) return;
183
+ const l = this.items().indexOf(t);
184
+ if (s = {
185
+ item: t,
186
+ source: e,
187
+ from: this,
188
+ oldIndex: l,
189
+ home: { container: this.element, next: t.nextSibling },
190
+ focusTarget: e === "keyboard" ? document.activeElement : null
191
+ }, t.setAttribute("data-sortable-dragging", e), document.documentElement.setAttribute("data-sortable-active", ""), e === "pointer") {
192
+ const c = t.getBoundingClientRect(), r = t.cloneNode(!0);
193
+ r.removeAttribute("id"), r.querySelectorAll("[id]").forEach((a) => a.removeAttribute("id")), r.setAttribute("data-sortable-ghost", ""), r.removeAttribute("data-sortable-dragging"), r.setAttribute("aria-hidden", "true"), Object.assign(r.style, {
194
+ position: "fixed",
195
+ left: `${c.left}px`,
196
+ top: `${c.top}px`,
197
+ width: `${c.width}px`,
198
+ height: `${c.height}px`,
199
+ margin: "0",
200
+ boxSizing: "border-box",
201
+ pointerEvents: "none",
202
+ zIndex: "2147483647",
203
+ transition: "none",
204
+ cursor: "grabbing",
205
+ willChange: "transform"
206
+ }), (this.options.ghostParent || this.element).appendChild(r);
207
+ const f = r.getBoundingClientRect(), d = { x: c.left - f.left, y: c.top - f.top };
208
+ (d.x || d.y) && (r.style.left = `${c.left + d.x}px`, r.style.top = `${c.top + d.y}px`), this.options.ghostClass ? r.classList.add(...this.options.ghostClass.split(/\s+/).filter(Boolean)) : Object.assign(r.style, {
209
+ opacity: "0.95",
210
+ boxShadow: "0 12px 28px rgba(15, 23, 42, 0.22), 0 2px 6px rgba(15, 23, 42, 0.12)",
211
+ transform: "scale(1.02)"
212
+ }), s.ghost = r, s.offset = { x: n - c.left - d.x, y: o - c.top - d.y }, s.pointer = { x: n, y: o }, this.options.placeholderClass ? (s.placeholderClass = this.options.placeholderClass.split(/\s+/).filter(Boolean), t.classList.add(...s.placeholderClass)) : (s.placeholderStyle = { opacity: t.style.opacity }, t.style.opacity = "0.35"), document.documentElement.style.setProperty("user-select", "none"), document.documentElement.style.setProperty("-webkit-user-select", "none"), window.getSelection?.()?.removeAllRanges(), this.pending?.timer && clearTimeout(this.pending.timer), this.pending = null;
213
+ } else
214
+ t.setAttribute("data-sortable-lifted", "");
215
+ this.options.onStart?.({ item: t, from: this.element, oldIndex: l, source: e }), e === "keyboard" && M(this.announcements().lifted(this.context(t)));
216
+ }
217
+ }
218
+ function W(i, t) {
219
+ if (!s) return null;
220
+ const e = s.from.peers();
221
+ let o = document.elementFromPoint(i, t);
222
+ for (; o; ) {
223
+ const r = w.get(o);
224
+ if (r && e.includes(r)) return r;
225
+ o = o.parentElement;
226
+ }
227
+ let l = null, c = N;
228
+ for (const r of e) {
229
+ const f = r.element.getBoundingClientRect(), d = Math.max(f.left - i, 0, i - f.right), a = Math.max(f.top - t, 0, t - f.bottom), u = Math.hypot(d, a);
230
+ u <= c && (c = u, l = r);
231
+ }
232
+ return l;
233
+ }
234
+ const N = 24;
235
+ function U(i, t, e, n) {
236
+ const o = e.options.canMove || s?.from.options.canMove;
237
+ return !o || o({ item: i, from: t, to: e.element, newIndex: n }) !== !1;
238
+ }
239
+ function C(i, t) {
240
+ if (!s) return !1;
241
+ const e = s.item, n = e.parentElement, o = i.items().filter((u) => u !== e), l = Math.max(0, Math.min(t, o.length));
242
+ if ((i.element === n ? i.items().indexOf(e) : -1) === l || !U(e, s.home.container, i, l)) return !1;
243
+ const r = /* @__PURE__ */ new Set([...i.items(), ...w.get(n)?.items() || []]), f = /* @__PURE__ */ new Map(), d = k() ? 0 : i.options.animation ?? 150;
244
+ d > 0 && r.forEach((u) => f.set(u, u.getBoundingClientRect()));
245
+ const a = o[l] || null;
246
+ if (a) i.element.insertBefore(e, a);
247
+ else {
248
+ const u = o[o.length - 1];
249
+ i.element.insertBefore(e, u ? u.nextSibling : null);
250
+ }
251
+ return d > 0 && f.forEach((u, p) => {
252
+ if (p === e && s?.source === "pointer") return;
253
+ const h = p.getBoundingClientRect(), g = u.left - h.left, m = u.top - h.top;
254
+ (g || m) && typeof p.animate == "function" && p.animate([{ transform: `translate(${g}px, ${m}px)` }, { transform: "none" }], { duration: d, easing: "cubic-bezier(0.2, 0, 0, 1)" });
255
+ }), !0;
256
+ }
257
+ function L(i, t) {
258
+ if (!s || !s.ghost || !s.offset) return;
259
+ s.pointer = { x: i, y: t }, s.ghost.style.left = `${i - s.offset.x}px`, s.ghost.style.top = `${t - s.offset.y}px`, q();
260
+ const e = W(i, t);
261
+ if (!e) return;
262
+ const n = e.items(), o = document.elementFromPoint(i, t), l = o ? e.itemOf(o) : null;
263
+ if (l && l !== s.item) {
264
+ if (s.lastTarget === l) return;
265
+ const r = n.indexOf(l);
266
+ let f;
267
+ if (e.element === s.item.parentElement)
268
+ f = r;
269
+ else {
270
+ const d = l.getBoundingClientRect(), u = B(e) === "horizontal" ? i > d.left + d.width / 2 : t > d.top + d.height / 2;
271
+ f = r + (u ? 1 : 0);
272
+ }
273
+ C(e, f) && (s.lastTarget = l);
274
+ return;
275
+ }
276
+ if (l === s.item) return;
277
+ s.lastTarget = null;
278
+ const c = n.filter((r) => r !== s.item);
279
+ e.element !== s.item.parentElement ? (c.length === 0 || O(e, c, i, t)) && C(e, c.length) : c.length && O(e, c, i, t) && C(e, c.length);
280
+ }
281
+ function O(i, t, e, n) {
282
+ const o = t[t.length - 1].getBoundingClientRect();
283
+ return B(i) === "horizontal" ? e > o.right : n > o.bottom;
284
+ }
285
+ function B(i) {
286
+ if (i.options.direction) return i.options.direction;
287
+ const t = i.items();
288
+ if (t.length < 2) return "vertical";
289
+ const e = t[0].getBoundingClientRect(), n = t[1].getBoundingClientRect();
290
+ return !e.width && !e.height ? "vertical" : Math.abs(e.top - n.top) < Math.max(4, e.height / 2) ? t.some((l) => l.getBoundingClientRect().top > e.bottom - 1) ? "grid" : "horizontal" : "vertical";
291
+ }
292
+ function q() {
293
+ if (!s || s.scrollFrame || !s.pointer) return;
294
+ const i = () => {
295
+ if (!s || !s.pointer) return;
296
+ s.scrollFrame = 0;
297
+ const { x: t, y: e } = s.pointer, n = 48, o = (r) => Math.ceil((n - r) / n * 18), l = document.elementFromPoint(t, e);
298
+ let c = !1;
299
+ for (let r = l; r && !(r === document.body || r === document.documentElement); r = r.parentElement) {
300
+ const f = getComputedStyle(r), d = /(auto|scroll)/.test(f.overflowY) && r.scrollHeight > r.clientHeight, a = /(auto|scroll)/.test(f.overflowX) && r.scrollWidth > r.clientWidth;
301
+ if (!a && !d) continue;
302
+ const u = r.getBoundingClientRect();
303
+ if (d && e - u.top < n && r.scrollTop > 0 ? (r.scrollTop -= o(e - u.top), c = !0) : d && u.bottom - e < n && r.scrollTop < r.scrollHeight - r.clientHeight && (r.scrollTop += o(u.bottom - e), c = !0), a && t - u.left < n && r.scrollLeft > 0 ? (r.scrollLeft -= o(t - u.left), c = !0) : a && u.right - t < n && r.scrollLeft < r.scrollWidth - r.clientWidth && (r.scrollLeft += o(u.right - t), c = !0), c) break;
304
+ }
305
+ if (!c) {
306
+ const r = document.scrollingElement || document.documentElement;
307
+ e < n && r.scrollTop > 0 ? (window.scrollBy(0, -o(e)), c = !0) : innerHeight - e < n && r.scrollTop < r.scrollHeight - innerHeight && (window.scrollBy(0, o(innerHeight - e)), c = !0);
308
+ }
309
+ c && (s.lastTarget = null, L(t, e), s.scrollFrame = requestAnimationFrame(i));
310
+ };
311
+ s.scrollFrame = requestAnimationFrame(i);
312
+ }
313
+ function X(i) {
314
+ if (!s) return;
315
+ const t = s.item, e = w.get(t.parentElement);
316
+ if (!e) return;
317
+ const n = e.items(), o = n.indexOf(t), l = B(e), c = l === "horizontal" ? "ArrowLeft" : "ArrowUp", r = l === "horizontal" ? "ArrowRight" : "ArrowDown", f = l === "horizontal" ? ["ArrowUp", "ArrowDown"] : ["ArrowLeft", "ArrowRight"];
318
+ let d = e, a = o;
319
+ if (i === "Home") a = 0;
320
+ else if (i === "End") a = n.length - 1;
321
+ else if (l === "grid") {
322
+ const g = Y(n);
323
+ if (i === "ArrowLeft" ? a = o - 1 : i === "ArrowRight" ? a = o + 1 : i === "ArrowUp" ? a = o - g : a = o + g, a < 0 || a >= n.length) return;
324
+ } else if (i === c) {
325
+ if (o === 0) return;
326
+ a = o - 1;
327
+ } else if (i === r) {
328
+ if (o === n.length - 1) return;
329
+ a = o + 1;
330
+ } else if (f.includes(i)) {
331
+ const g = s.from.peers().sort((v, I) => {
332
+ const A = v.element.getBoundingClientRect(), E = I.element.getBoundingClientRect();
333
+ return l === "horizontal" ? A.top - E.top || A.left - E.left : A.left - E.left || A.top - E.top;
334
+ }), m = g.indexOf(e), b = i === f[1];
335
+ if (d = g[m + (b ? 1 : -1)], !d) return;
336
+ a = Math.min(o, d.items().length);
337
+ } else
338
+ return;
339
+ const u = document.activeElement, p = C(d, a);
340
+ if (u && u !== document.activeElement && u.focus({ preventScroll: !0 }), t.scrollIntoView?.({ block: "nearest", inline: "nearest" }), !p) return;
341
+ const h = d.context(t);
342
+ M(d === e ? d.announcements().moved(h) : d.announcements().movedToList(h));
343
+ }
344
+ function Y(i) {
345
+ if (!i.length) return 1;
346
+ const t = i[0].getBoundingClientRect().top, e = i.filter((n) => Math.abs(n.getBoundingClientRect().top - t) < 2).length;
347
+ return Math.max(1, e);
348
+ }
349
+ function j() {
350
+ const i = (t) => {
351
+ t.stopPropagation(), t.preventDefault();
352
+ };
353
+ window.addEventListener("click", i, { capture: !0, once: !0 }), setTimeout(() => window.removeEventListener("click", i, { capture: !0 }), 50);
354
+ }
355
+ function x(i) {
356
+ if (!s) return;
357
+ const t = s;
358
+ s = null;
359
+ const { item: e, from: n, source: o, oldIndex: l, home: c } = t;
360
+ t.scrollFrame && cancelAnimationFrame(t.scrollFrame), n.unlistenWindow();
361
+ const r = w.get(e.parentElement) || n;
362
+ let f = r.items().indexOf(e);
363
+ const d = r !== n || f !== l, a = () => c.container.insertBefore(e, c.next && c.next.parentNode === c.container ? c.next : null);
364
+ i && d && a();
365
+ const u = !i && d, p = i ? n : r;
366
+ i && (f = l);
367
+ const h = t.ghost;
368
+ if (e.removeAttribute("data-sortable-dragging"), e.removeAttribute("data-sortable-lifted"), document.documentElement.removeAttribute("data-sortable-active"), document.documentElement.style.removeProperty("user-select"), document.documentElement.style.removeProperty("-webkit-user-select"), t.placeholderStyle && (e.style.opacity = t.placeholderStyle.opacity), t.placeholderClass && e.classList.remove(...t.placeholderClass), h) {
369
+ const m = e.getBoundingClientRect(), b = k() ? 0 : Math.min(n.options.animation ?? 150, 200);
370
+ if (b && typeof h.animate == "function") {
371
+ const v = parseFloat(h.style.left), I = parseFloat(h.style.top);
372
+ h.animate(
373
+ [{ transform: h.style.transform || "none" }, { transform: `translate(${m.left - v}px, ${m.top - I}px)` }],
374
+ { duration: b, easing: "cubic-bezier(0.2, 0, 0, 1)", fill: "forwards" }
375
+ ).onfinish = () => h.remove(), setTimeout(() => h.remove(), b + 100);
376
+ } else
377
+ h.remove();
378
+ }
379
+ u && (n.options.revert || p.options.revert) && a();
380
+ const g = n.idOf(e);
381
+ if (u) {
382
+ const m = { item: e, itemId: g, from: n.element, to: p.element, fromId: n.listId(), toId: p.listId(), oldIndex: l, newIndex: f, source: o };
383
+ p === n ? n.options.onSort?.({ ...m, type: "update" }) : (n.options.onSort?.({ ...m, type: "remove" }), p.options.onSort?.({ ...m, type: "add" }));
384
+ }
385
+ if (n.options.onEnd?.({ item: e, itemId: g, from: n.element, to: p.element, oldIndex: l, newIndex: f, changed: u, cancelled: i, source: o }), o === "keyboard") {
386
+ const m = p.context(e);
387
+ M(i ? n.announcements().cancelled(m) : p.announcements().dropped(m));
388
+ const b = t.focusTarget;
389
+ if (b) {
390
+ b.focus({ preventScroll: !0 });
391
+ const v = () => {
392
+ b.isConnected && document.activeElement !== b && (document.activeElement === document.body || !document.activeElement) && b.focus({ preventScroll: !0 });
393
+ };
394
+ setTimeout(v, 0), setTimeout(v, 50);
395
+ }
396
+ }
397
+ }
398
+ function _(i, t = {}) {
399
+ if (typeof document > "u")
400
+ return { element: i, toArray: () => [], sort: () => {
401
+ }, update: () => {
402
+ }, destroy: () => {
403
+ } };
404
+ w.get(i)?.destroy();
405
+ const e = new H(i, t);
406
+ return w.set(i, e), {
407
+ element: i,
408
+ toArray: () => e.items().map((o) => e.idOf(o)).filter((o) => !!o),
409
+ sort(o) {
410
+ const l = e.items(), c = new Map(l.map((a) => [e.idOf(a), a])), r = o.map((a) => c.get(a)).filter((a) => !!a), f = l.filter((a) => !r.includes(a)), d = l[l.length - 1]?.nextSibling || null;
411
+ [...r, ...f].forEach((a) => i.insertBefore(a, d));
412
+ },
413
+ update(o) {
414
+ e.options = { ...e.options, ...o };
415
+ },
416
+ destroy() {
417
+ e.destroy();
418
+ }
419
+ };
420
+ }
421
+ export {
422
+ K as applySortEvent,
423
+ _ as createSortable,
424
+ z as moveBetween,
425
+ F as moveItem
426
+ };
package/dist/react.esm.js CHANGED
@@ -1,6 +1,6 @@
1
- import { jsx as G, jsxs as vt, Fragment as St } from "react/jsx-runtime";
1
+ import { jsx as Q, jsxs as vt, Fragment as St } from "react/jsx-runtime";
2
2
  import g from "react";
3
- import { createPortal as J } from "react-dom";
3
+ import { createPortal as G } from "react-dom";
4
4
  import "@editora/ui-core/sortable";
5
5
  const N = typeof window < "u" ? g.useLayoutEffect : g.useEffect;
6
6
  function wt(e, s, ...i) {
@@ -46,7 +46,7 @@ function f(e, s, i) {
46
46
  }
47
47
  l !== i && e.setAttribute(s, i);
48
48
  }
49
- function _(e, s, i) {
49
+ function J(e, s, i) {
50
50
  if (i) {
51
51
  e.hasAttribute(s) || e.setAttribute(s, "");
52
52
  return;
@@ -139,8 +139,8 @@ const It = [], qt = [], Ct = g.forwardRef(function(s, i) {
139
139
  onBeforeChange: B,
140
140
  onChange: F,
141
141
  onItemsChange: j,
142
- onSelectionChange: k,
143
- onPersistRequest: Q,
142
+ onSelectionChange: _,
143
+ onPersistRequest: k,
144
144
  children: ft,
145
145
  ...bt
146
146
  } = s, b = l ?? It, h = m ?? qt, u = xt(i), [R, gt] = g.useState(0), st = !!(c || w || x);
@@ -163,30 +163,30 @@ const It = [], qt = [], Ct = g.forwardRef(function(s, i) {
163
163
  },
164
164
  {
165
165
  type: "selection-change",
166
- listener: k ? ((t) => {
166
+ listener: _ ? ((t) => {
167
167
  const r = O(t);
168
- r && k(r);
168
+ r && _(r);
169
169
  }) : void 0
170
170
  },
171
171
  {
172
172
  type: "persist-request",
173
- listener: Q ? ((t) => {
173
+ listener: k ? ((t) => {
174
174
  const r = O(t);
175
- r && Q(r);
175
+ r && k(r);
176
176
  }) : void 0
177
177
  }
178
178
  ],
179
- [B, F, j, k, Q]
179
+ [B, F, j, _, k]
180
180
  ), Et(
181
181
  u,
182
182
  (t) => {
183
- H(t, "lists", b), H(t, "items", h), H(t, "selection", o ?? null), f(t, "filter-query", n ?? null), f(t, "sort", p && p !== "manual" ? p : null), f(t, "persist-key", M ?? null), _(t, "allow-filtered-drag", E), T === !1 ? t.setAttribute("allow-nesting", "false") : t.removeAttribute("allow-nesting"), f(t, "nest-trigger", P && P !== "gutter" ? P : null), f(t, "list-max-height", typeof q == "number" ? String(q) + "px" : q ?? null), f(t, "group", U ?? null), f(t, "touch-hold-delay", typeof I == "number" && Number.isFinite(I) ? String(I) : null), V === "always" ? t.setAttribute("drop-indicator-visibility", "always") : t.removeAttribute("drop-indicator-visibility"), W === "container" ? t.setAttribute("dropzone-style", "container") : t.removeAttribute("dropzone-style"), $ === "compact" ? t.setAttribute("drag-preview-size", "compact") : t.removeAttribute("drag-preview-size"), S === "item" ? t.setAttribute("drag-handle-mode", "item") : t.removeAttribute("drag-handle-mode"), f(t, "drag-handle-selector", y ?? null);
183
+ H(t, "lists", b), H(t, "items", h), H(t, "selection", o ?? null), f(t, "filter-query", n ?? null), f(t, "sort", p && p !== "manual" ? p : null), f(t, "persist-key", M ?? null), J(t, "allow-filtered-drag", E), T === !1 ? t.setAttribute("allow-nesting", "false") : t.removeAttribute("allow-nesting"), f(t, "nest-trigger", P && P !== "gutter" ? P : null), f(t, "list-max-height", typeof q == "number" ? String(q) + "px" : q ?? null), f(t, "group", U ?? null), f(t, "touch-hold-delay", typeof I == "number" && Number.isFinite(I) ? String(I) : null), V === "always" ? t.setAttribute("drop-indicator-visibility", "always") : t.removeAttribute("drop-indicator-visibility"), W === "container" ? t.setAttribute("dropzone-style", "container") : t.removeAttribute("dropzone-style"), $ === "compact" ? t.setAttribute("drag-preview-size", "compact") : t.removeAttribute("drag-preview-size"), S === "item" ? t.setAttribute("drag-handle-mode", "item") : t.removeAttribute("drag-handle-mode"), f(t, "drag-handle-selector", y ?? null);
184
184
  const r = ct(X);
185
185
  r ? t.style.setProperty("--ui-sortable-item-radius", r) : t.style.removeProperty("--ui-sortable-item-radius");
186
186
  const d = ct(Y);
187
187
  d ? t.style.setProperty("--ui-sortable-handle-radius", d) : t.style.removeProperty("--ui-sortable-handle-radius");
188
188
  const a = Pt(Z ?? tt);
189
- a ? t.style.setProperty("--ui-sortable-list-gap", a) : t.style.removeProperty("--ui-sortable-list-gap"), c ? t.setAttribute("custom-item-rendering", "true") : t.removeAttribute("custom-item-rendering"), z === !1 ? t.setAttribute("show-selection-badge", "false") : t.removeAttribute("show-selection-badge"), t.canDrop = et ?? null, H(t, "labels", C && Object.keys(C).length ? C : null), _(t, "hide-toolbar", rt), _(t, "disabled", A);
189
+ a ? t.style.setProperty("--ui-sortable-list-gap", a) : t.style.removeProperty("--ui-sortable-list-gap"), c ? t.setAttribute("custom-item-rendering", "true") : t.removeAttribute("custom-item-rendering"), z === !1 ? t.setAttribute("show-selection-badge", "false") : t.removeAttribute("show-selection-badge"), t.canDrop = et ?? null, H(t, "labels", C && Object.keys(C).length ? C : null), J(t, "hide-toolbar", rt), J(t, "disabled", A);
190
190
  },
191
191
  [
192
192
  b,
@@ -289,8 +289,8 @@ const It = [], qt = [], Ct = g.forwardRef(function(s, i) {
289
289
  const r = u.current?.shadowRoot?.querySelector(
290
290
  `.item-custom-content[data-item-content-target="${K(t.id)}"]`
291
291
  );
292
- return r ? J(
293
- /* @__PURE__ */ G(
292
+ return r ? G(
293
+ /* @__PURE__ */ Q(
294
294
  "div",
295
295
  {
296
296
  "data-sortable-custom-item": "",
@@ -314,8 +314,8 @@ const It = [], qt = [], Ct = g.forwardRef(function(s, i) {
314
314
  const r = u.current?.shadowRoot?.querySelector(
315
315
  `.list-custom-header[data-list-header-target="${K(t.id)}"]`
316
316
  );
317
- return r ? J(
318
- /* @__PURE__ */ G("div", { "data-sortable-custom-list-header": "", "data-sortable-list-id": t.id, children: w(t, {
317
+ return r ? G(
318
+ /* @__PURE__ */ Q("div", { "data-sortable-custom-list-header": "", "data-sortable-list-id": t.id, children: w(t, {
319
319
  itemCount: ot.get(t.id) ?? 0,
320
320
  dragLocked: at
321
321
  }) }),
@@ -326,8 +326,8 @@ const It = [], qt = [], Ct = g.forwardRef(function(s, i) {
326
326
  const r = u.current?.shadowRoot?.querySelector(
327
327
  `.list-custom-empty[data-list-empty-target="${K(t.id)}"]`
328
328
  );
329
- return r ? J(
330
- /* @__PURE__ */ G("div", { "data-sortable-custom-empty": "", "data-sortable-list-id": t.id, children: x(t, {
329
+ return r ? G(
330
+ /* @__PURE__ */ Q("div", { "data-sortable-custom-empty": "", "data-sortable-list-id": t.id, children: x(t, {
331
331
  itemCount: ot.get(t.id) ?? 0,
332
332
  dragLocked: at,
333
333
  filterQuery: n ?? ""
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@editora/ui-sortable",
3
- "version": "0.1.13",
4
- "description": "Standalone install for the Editora sortable web component, with reordering, transfer, hierarchy, cloning, and keyboard-accessible drag and drop.",
3
+ "version": "0.2.0",
4
+ "description": "Drag-and-drop for the web: the <ui-sortable> board and list component, or sortable behaviour for your own markup (createSortable and a React useSortable hook), with keyboard, touch, screen-reader announcements and lists that exchange items.",
5
5
  "homepage": "https://editora-ecosystem.netlify.app/docs/ui-react/interactive/sortable",
6
6
  "bugs": {
7
7
  "url": "https://github.com/ajaykr089/Editora/issues"
@@ -13,33 +13,45 @@
13
13
  },
14
14
  "main": "dist/index.cjs.js",
15
15
  "module": "dist/index.esm.js",
16
- "types": "index.d.ts",
16
+ "types": "types/index.d.ts",
17
17
  "exports": {
18
18
  ".": {
19
- "types": "./index.d.ts",
19
+ "types": "./types/index.d.ts",
20
20
  "import": "./dist/index.esm.js",
21
21
  "require": "./dist/index.cjs.js",
22
22
  "default": "./dist/index.esm.js"
23
23
  },
24
24
  "./react": {
25
- "types": "./react.d.ts",
25
+ "types": "./types/react.d.ts",
26
26
  "import": "./dist/react.esm.js",
27
27
  "require": "./dist/react.cjs.js",
28
28
  "default": "./dist/react.esm.js"
29
- }
29
+ },
30
+ "./headless": {
31
+ "types": "./types/headless.d.ts",
32
+ "import": "./dist/headless.esm.js",
33
+ "require": "./dist/headless.cjs.js",
34
+ "default": "./dist/headless.esm.js"
35
+ },
36
+ "./headless/react": {
37
+ "types": "./types/headless-react.d.ts",
38
+ "import": "./dist/headless-react.esm.js",
39
+ "require": "./dist/headless-react.cjs.js",
40
+ "default": "./dist/headless-react.esm.js"
41
+ },
42
+ "./package.json": "./package.json"
30
43
  },
31
44
  "files": [
32
45
  "dist",
33
- "index.d.ts",
34
- "react.d.ts"
46
+ "types"
35
47
  ],
36
48
  "scripts": {
37
- "build": "vite build",
49
+ "build": "vite build && tsc -p tsconfig.build.json",
38
50
  "dev": "vite build --watch",
39
51
  "test": "vitest run",
40
52
  "test:watch": "vitest",
41
- "clean": "rimraf dist",
42
- "prepare": "npm run build"
53
+ "clean": "rimraf dist types",
54
+ "prepare": "vite build"
43
55
  },
44
56
  "dependencies": {
45
57
  "@editora/ui-core": "^0.6.0"
@@ -73,12 +85,35 @@
73
85
  "web-components",
74
86
  "kanban",
75
87
  "tree",
76
- "accessible-ui"
88
+ "accessible-ui",
89
+ "headless",
90
+ "react-hook",
91
+ "useSortable",
92
+ "reorder",
93
+ "dnd"
77
94
  ],
78
95
  "author": "Ajay Kumar <ajaykr089@gmail.com>",
79
96
  "license": "MIT",
80
- "sideEffects": true,
97
+ "sideEffects": [
98
+ "./dist/index.*",
99
+ "./dist/react.*",
100
+ "./src/index.ts",
101
+ "./src/react.tsx"
102
+ ],
81
103
  "publishConfig": {
82
104
  "access": "public"
105
+ },
106
+ "typesVersions": {
107
+ "*": {
108
+ "react": [
109
+ "types/react.d.ts"
110
+ ],
111
+ "headless": [
112
+ "types/headless.d.ts"
113
+ ],
114
+ "headless/react": [
115
+ "types/headless-react.d.ts"
116
+ ]
117
+ }
83
118
  }
84
119
  }
@@ -0,0 +1,16 @@
1
+ import { type Sortable, type SortableEvent, type SortableOptions } from './headless';
2
+ export interface UseSortableOptions<T> extends Omit<SortableOptions, 'revert' | 'onSort'> {
3
+ /** The data the list renders, in the order it renders it: one entry per item element. */
4
+ items: readonly T[];
5
+ /** The new order. For a move between lists, called on both: without the item, and with it. */
6
+ onChange: (items: T[], event: SortableEvent) => void;
7
+ }
8
+ export interface UseSortableResult {
9
+ /** Put it on the element whose children are the items. */
10
+ ref: (element: HTMLElement | null) => void;
11
+ /** The list, for toArray() and the rest; null until the element is rendered. */
12
+ sortable: Sortable | null;
13
+ }
14
+ export declare function useSortable<T>(options: UseSortableOptions<T>): UseSortableResult;
15
+ export { moveItem, moveBetween, applySortEvent } from './headless';
16
+ export type { Sortable, SortableOptions, SortableEvent, SortableEndEvent, SortableMove, SortableDirection, SortableSource, SortableAnnouncements, SortableAnnouncementContext, } from './headless';