@dfosco/tiny-canvas 1.1.0 → 1.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.
@@ -1 +1 @@
1
- :root{--tc-border-radius: var(--borderRadius-large, 12px);--tc-shadow-rest: var(--shadow-resting-small, 0 1px 3px rgba(0, 0, 0, .12));--tc-shadow-float: var(--shadow-floating-small, 0 4px 12px rgba(0, 0, 0, .15));--tc-border-color: var(--borderColor-muted, rgba(0, 0, 0, .15));--tc-bg-muted: var(--bgColor-muted, #f6f8fa);--tc-dot-color: var(--overlay-backdrop-bgColor, rgba(0, 0, 0, .08));--tc-grid-size: var(--golden-number, 36px);--tc-grid-offset: calc(var(--tc-grid-size) / -2);--tc-font-stack: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif}.tc-canvas{min-width:100vw;min-height:100vh;margin:0;font-family:var(--tc-font-stack);position:relative;background-color:var(--tc-bg-muted);width:100vw;max-width:100vw;color-scheme:light dark}.tc-canvas[data-color-mode=light]{color-scheme:light}.tc-canvas[data-color-mode=dark]{color-scheme:dark}.tc-canvas[data-dotted]{background-image:radial-gradient(var(--tc-dot-color) 2px,transparent 2px);background-size:var(--tc-grid-size) var(--tc-grid-size);background-position:var(--tc-grid-offset) var(--tc-grid-offset)}@media(prefers-color-scheme:dark){.tc-canvas:not([data-color-mode=light]){--tc-bg-muted: var(--bgColor-muted, #161b22);--tc-dot-color: var(--overlay-backdrop-bgColor, rgba(255, 255, 255, .1));--tc-border-color: var(--borderColor-muted, rgba(255, 255, 255, .15));--tc-shadow-rest: var(--shadow-resting-small, 0 1px 3px rgba(0, 0, 0, .3));--tc-shadow-float: var(--shadow-floating-small, 0 4px 12px rgba(0, 0, 0, .4))}}.tc-canvas[data-color-mode=dark]{--tc-bg-muted: var(--bgColor-muted, #161b22);--tc-dot-color: var(--overlay-backdrop-bgColor, rgba(255, 255, 255, .1));--tc-border-color: var(--borderColor-muted, rgba(255, 255, 255, .15));--tc-shadow-rest: var(--shadow-resting-small, 0 1px 3px rgba(0, 0, 0, .3));--tc-shadow-float: var(--shadow-floating-small, 0 4px 12px rgba(0, 0, 0, .4))}body .tc-draggable-inner:not(:active){touch-action:none}.tc-on-translation{transition:transform calc(var(--tc-translation-ms, .25s) * 2) cubic-bezier(.78,.1,.51,.78) var(--tc-translation-ms, .25s)}.tc-drag{position:absolute;width:fit-content}.tc-draggable-inner{position:relative;width:fit-content;height:min-content}.tc-draggable-inner:after{content:"";position:absolute;z-index:-1;width:100%;height:100%;top:0;border-radius:var(--tc-border-radius);box-shadow:var(--tc-shadow-float);opacity:0;transition:opacity .3s ease-in-out}.tc-on-translation .tc-draggable-inner:after,.tc-draggable-inner:active:after,.tc-draggable-inner:hover:after{opacity:0}.tc-on-translation .tc-draggable-inner,.tc-draggable-inner:active,.tc-draggable-inner:hover{outline:none}
1
+ :root{--tc-border-radius: var(--borderRadius-large, 12px);--tc-shadow-rest: var(--shadow-resting-small, 0 1px 3px rgba(0, 0, 0, .12));--tc-shadow-float: var(--shadow-floating-small, 0 4px 12px rgba(0, 0, 0, .15));--tc-border-color: var(--borderColor-muted, rgba(0, 0, 0, .15));--tc-bg-muted: var(--bgColor-muted, #f6f8fa);--tc-dot-color: var(--overlay-backdrop-bgColor, rgba(0, 0, 0, .08));--tc-grid-size: var(--golden-number, 36px);--tc-grid-offset: calc(var(--tc-grid-size) / -2);--tc-font-stack: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif}.tc-canvas{min-width:100vw;min-height:100vh;width:10000px;height:10000px;margin:0;font-family:var(--tc-font-stack);position:relative;background-color:var(--tc-bg-muted);color-scheme:light dark}.tc-canvas[data-color-mode=light]{color-scheme:light}.tc-canvas[data-color-mode=dark]{color-scheme:dark}.tc-canvas[data-dotted]{background-image:radial-gradient(var(--tc-dot-color) 2px,transparent 2px);background-size:var(--tc-grid-size) var(--tc-grid-size);background-position:var(--tc-grid-offset) var(--tc-grid-offset)}@media(prefers-color-scheme:dark){.tc-canvas:not([data-color-mode=light]){--tc-bg-muted: var(--bgColor-muted, #161b22);--tc-dot-color: var(--overlay-backdrop-bgColor, rgba(255, 255, 255, .1));--tc-border-color: var(--borderColor-muted, rgba(255, 255, 255, .15));--tc-shadow-rest: var(--shadow-resting-small, 0 1px 3px rgba(0, 0, 0, .3));--tc-shadow-float: var(--shadow-floating-small, 0 4px 12px rgba(0, 0, 0, .4))}}.tc-canvas[data-color-mode=dark]{--tc-bg-muted: var(--bgColor-muted, #161b22);--tc-dot-color: var(--overlay-backdrop-bgColor, rgba(255, 255, 255, .1));--tc-border-color: var(--borderColor-muted, rgba(255, 255, 255, .15));--tc-shadow-rest: var(--shadow-resting-small, 0 1px 3px rgba(0, 0, 0, .3));--tc-shadow-float: var(--shadow-floating-small, 0 4px 12px rgba(0, 0, 0, .4))}body .tc-draggable-inner:not(:active){touch-action:none}.tc-on-translation{transition:transform calc(var(--tc-translation-ms, .25s) * 2) cubic-bezier(.78,.1,.51,.78) var(--tc-translation-ms, .25s)}.tc-drag{position:absolute;width:fit-content}.tc-draggable-inner{position:relative;width:fit-content;height:min-content}.tc-draggable-inner:after{content:"";position:absolute;z-index:-1;width:100%;height:100%;top:0;border-radius:var(--tc-border-radius);box-shadow:var(--tc-shadow-float);opacity:0;transition:opacity .3s ease-in-out}.tc-on-translation .tc-draggable-inner:after,.tc-draggable-inner:active:after,.tc-draggable-inner:hover:after{opacity:0}.tc-on-translation .tc-draggable-inner,.tc-draggable-inner:active,.tc-draggable-inner:hover{outline:none}
@@ -1,289 +1,320 @@
1
- import { jsx as Q } from "react/jsx-runtime";
2
- import me, { useRef as le, useState as Y, useEffect as P, Children as Ee, useCallback as Ce } from "react";
3
- var ae = { dragStart: !0 }, se = { delay: 0, distance: 3 };
4
- function Ae(e, r = {}) {
5
- let n, o, { bounds: a, axis: c = "both", gpuAcceleration: l = !0, legacyTranslate: d = !1, transform: m, applyUserSelectHack: w = !0, disabled: N = !1, ignoreMultitouch: v = !1, recomputeBounds: D = ae, grid: f, threshold: y = se, position: b, cancel: q, handle: M, defaultClass: u = "neodrag", defaultClassDragging: x = "neodrag-dragging", defaultClassDragged: I = "neodrag-dragged", defaultPosition: be = { x: 0, y: 0 }, onDragStart: Se, onDrag: we, onDragEnd: De } = r, H = !1, L = !1, de = 0, O = !1, J = !1, R = 0, T = 0, k = 0, B = 0, _ = 0, G = 0, { x: j, y: z } = b ? { x: (b == null ? void 0 : b.x) ?? 0, y: (b == null ? void 0 : b.y) ?? 0 } : be;
6
- te(j, z);
7
- let E, $, X, K, Z, fe = "", xe = !!b;
8
- D = { ...ae, ...D }, y = { ...se, ...y ?? {} };
9
- let U = /* @__PURE__ */ new Set();
10
- function ge(t) {
11
- H && !L && J && O && Z && (L = !0, (function(s) {
12
- re("neodrag:start", Se, s);
13
- })(t), A.add(x), w && (fe = ee.userSelect, ee.userSelect = "none"));
1
+ import { jsx as Z } from "react/jsx-runtime";
2
+ import vt, { useRef as W, useState as B, useEffect as ft, Children as Mt, useCallback as Rt } from "react";
3
+ var it = { dragStart: !0 }, ct = { delay: 0, distance: 3 };
4
+ function It(t, e = {}) {
5
+ let n, o, { bounds: a, axis: s = "both", gpuAcceleration: f = !0, legacyTranslate: d = !1, transform: v, applyUserSelectHack: w = !0, disabled: D = !1, ignoreMultitouch: N = !1, recomputeBounds: x = it, grid: I, threshold: y = ct, position: S, cancel: u, handle: m, defaultClass: i = "neodrag", defaultClassDragging: b = "neodrag-dragging", defaultClassDragged: A = "neodrag-dragged", defaultPosition: Et = { x: 0, y: 0 }, onDragStart: xt, onDrag: Nt, onDragEnd: At } = e, L = !1, $ = !1, gt = 0, Y = !1, _ = !1, T = 0, O = 0, q = 0, X = 0, G = 0, K = 0, { x: U, y: J } = S ? { x: (S == null ? void 0 : S.x) ?? 0, y: (S == null ? void 0 : S.y) ?? 0 } : Et;
6
+ nt(U, J);
7
+ let C, P, k, tt, et, pt = "", Ct = !!S;
8
+ x = { ...it, ...x }, y = { ...ct, ...y ?? {} };
9
+ let j = /* @__PURE__ */ new Set();
10
+ function ht(r) {
11
+ L && !$ && _ && Y && et && ($ = !0, (function(c) {
12
+ ot("neodrag:start", xt, c);
13
+ })(r), R.add(b), w && (pt = rt.userSelect, rt.userSelect = "none"));
14
14
  }
15
- const ee = document.body.style, A = e.classList;
16
- function te(t = R, s = T) {
17
- if (!m) {
15
+ const rt = document.body.style, R = t.classList;
16
+ function nt(r = T, c = O) {
17
+ if (!v) {
18
18
  if (d) {
19
- let p = `${+t}px, ${+s}px`;
20
- return W(e, "transform", l ? `translate3d(${p}, 0)` : `translate(${p})`);
19
+ let p = `${+r}px, ${+c}px`;
20
+ return Q(t, "transform", f ? `translate3d(${p}, 0)` : `translate(${p})`);
21
21
  }
22
- return W(e, "translate", `${+t}px ${+s}px`);
22
+ return Q(t, "translate", `${+r}px ${+c}px`);
23
23
  }
24
- const g = m({ offsetX: t, offsetY: s, rootNode: e });
25
- ie(g) && W(e, "transform", g);
24
+ const g = v({ offsetX: r, offsetY: c, rootNode: t });
25
+ lt(g) && Q(t, "transform", g);
26
26
  }
27
- function re(t, s, g) {
27
+ function ot(r, c, g) {
28
28
  const p = /* @__PURE__ */ (function(h) {
29
- return { offsetX: R, offsetY: T, rootNode: e, currentNode: Z, event: h };
29
+ return { offsetX: T, offsetY: O, rootNode: t, currentNode: et, event: h };
30
30
  })(g);
31
- e.dispatchEvent(new CustomEvent(t, { detail: p })), s == null || s(p);
31
+ t.dispatchEvent(new CustomEvent(r, { detail: p })), c == null || c(p);
32
32
  }
33
- const V = addEventListener, ne = new AbortController(), oe = { signal: ne.signal, capture: !1 };
34
- function pe() {
35
- let t = e.offsetWidth / $.width;
36
- return isNaN(t) && (t = 1), t;
33
+ const F = addEventListener, at = new AbortController(), st = { signal: at.signal, capture: !1 };
34
+ function yt() {
35
+ let r = t.offsetWidth / P.width;
36
+ return isNaN(r) && (r = 1), r;
37
37
  }
38
- return W(e, "touch-action", "none"), V("pointerdown", ((t) => {
39
- if (N || t.button === 2) return;
40
- if (U.add(t.pointerId), v && U.size > 1) return t.preventDefault();
41
- if (D.dragStart && (E = ce(a, e)), ie(M) && ie(q) && M === q) throw new Error("`handle` selector can't be same as `cancel` selector");
42
- if (A.add(u), X = (function(i, S) {
43
- if (!i) return [S];
44
- if (ue(i)) return [i];
45
- if (Array.isArray(i)) return i;
46
- const C = S.querySelectorAll(i);
47
- if (C === null) throw new Error("Selector passed for `handle` option should be child of the element on which the action is applied");
48
- return Array.from(C.values());
49
- })(M, e), K = (function(i, S) {
50
- if (!i) return [];
51
- if (ue(i)) return [i];
52
- if (Array.isArray(i)) return i;
53
- const C = S.querySelectorAll(i);
54
- if (C === null) throw new Error("Selector passed for `cancel` option should be child of the element on which the action is applied");
55
- return Array.from(C.values());
56
- })(q, e), n = /(both|x)/.test(c), o = /(both|y)/.test(c), ye(K, X)) throw new Error("Element being dragged can't be a child of the element on which `cancel` is applied");
57
- const s = t.composedPath()[0];
58
- if (!X.some(((i) => {
59
- var S;
60
- return i.contains(s) || ((S = i.shadowRoot) == null ? void 0 : S.contains(s));
61
- })) || ye(K, [s])) return;
62
- Z = X.length === 1 ? e : X.find(((i) => i.contains(s))), H = !0, de = Date.now(), y.delay || (O = !0), $ = e.getBoundingClientRect();
63
- const { clientX: g, clientY: p } = t, h = pe();
64
- n && (k = g - j / h), o && (B = p - z / h), E && (_ = g - $.left, G = p - $.top);
65
- }), oe), V("pointermove", ((t) => {
66
- if (!H || v && U.size > 1) return;
67
- if (!L) {
68
- if (O || Date.now() - de >= y.delay && (O = !0, ge(t)), !J) {
69
- const h = t.clientX - k, i = t.clientY - B;
70
- Math.sqrt(h ** 2 + i ** 2) >= y.distance && (J = !0, ge(t));
38
+ return Q(t, "touch-action", "none"), F("pointerdown", ((r) => {
39
+ if (D || r.button === 2) return;
40
+ if (j.add(r.pointerId), N && j.size > 1) return r.preventDefault();
41
+ if (x.dragStart && (C = ut(a, t)), lt(m) && lt(u) && m === u) throw new Error("`handle` selector can't be same as `cancel` selector");
42
+ if (R.add(i), k = (function(l, E) {
43
+ if (!l) return [E];
44
+ if (dt(l)) return [l];
45
+ if (Array.isArray(l)) return l;
46
+ const M = E.querySelectorAll(l);
47
+ if (M === null) throw new Error("Selector passed for `handle` option should be child of the element on which the action is applied");
48
+ return Array.from(M.values());
49
+ })(m, t), tt = (function(l, E) {
50
+ if (!l) return [];
51
+ if (dt(l)) return [l];
52
+ if (Array.isArray(l)) return l;
53
+ const M = E.querySelectorAll(l);
54
+ if (M === null) throw new Error("Selector passed for `cancel` option should be child of the element on which the action is applied");
55
+ return Array.from(M.values());
56
+ })(u, t), n = /(both|x)/.test(s), o = /(both|y)/.test(s), St(tt, k)) throw new Error("Element being dragged can't be a child of the element on which `cancel` is applied");
57
+ const c = r.composedPath()[0];
58
+ if (!k.some(((l) => {
59
+ var E;
60
+ return l.contains(c) || ((E = l.shadowRoot) == null ? void 0 : E.contains(c));
61
+ })) || St(tt, [c])) return;
62
+ et = k.length === 1 ? t : k.find(((l) => l.contains(c))), L = !0, gt = Date.now(), y.delay || (Y = !0), P = t.getBoundingClientRect();
63
+ const { clientX: g, clientY: p } = r, h = yt();
64
+ n && (q = g - U / h), o && (X = p - J / h), C && (G = g - P.left, K = p - P.top);
65
+ }), st), F("pointermove", ((r) => {
66
+ if (!L || N && j.size > 1) return;
67
+ if (!$) {
68
+ if (Y || Date.now() - gt >= y.delay && (Y = !0, ht(r)), !_) {
69
+ const h = r.clientX - q, l = r.clientY - X;
70
+ Math.sqrt(h ** 2 + l ** 2) >= y.distance && (_ = !0, ht(r));
71
71
  }
72
- if (!L) return;
72
+ if (!$) return;
73
73
  }
74
- D.drag && (E = ce(a, e)), t.preventDefault(), $ = e.getBoundingClientRect();
75
- let s = t.clientX, g = t.clientY;
76
- const p = pe();
77
- if (E) {
78
- const h = { left: E.left + _, top: E.top + G, right: E.right + _ - $.width, bottom: E.bottom + G - $.height };
79
- s = he(s, h.left, h.right), g = he(g, h.top, h.bottom);
74
+ x.drag && (C = ut(a, t)), r.preventDefault(), P = t.getBoundingClientRect();
75
+ let c = r.clientX, g = r.clientY;
76
+ const p = yt();
77
+ if (C) {
78
+ const h = { left: C.left + G, top: C.top + K, right: C.right + G - P.width, bottom: C.bottom + K - P.height };
79
+ c = mt(c, h.left, h.right), g = mt(g, h.top, h.bottom);
80
80
  }
81
- if (Array.isArray(f)) {
82
- let [h, i] = f;
81
+ if (Array.isArray(I)) {
82
+ let [h, l] = I;
83
83
  if (isNaN(+h) || h < 0) throw new Error("1st argument of `grid` must be a valid positive number");
84
- if (isNaN(+i) || i < 0) throw new Error("2nd argument of `grid` must be a valid positive number");
85
- let S = s - k, C = g - B;
86
- [S, C] = Ne([h / p, i / p], S, C), s = k + S, g = B + C;
84
+ if (isNaN(+l) || l < 0) throw new Error("2nd argument of `grid` must be a valid positive number");
85
+ let E = c - q, M = g - X;
86
+ [E, M] = Tt([h / p, l / p], E, M), c = q + E, g = X + M;
87
87
  }
88
- n && (R = Math.round((s - k) * p)), o && (T = Math.round((g - B) * p)), j = R, z = T, re("neodrag", we, t), te();
89
- }), oe), V("pointerup", ((t) => {
90
- U.delete(t.pointerId), H && (L && (V("click", ((s) => s.stopPropagation()), { once: !0, signal: ne.signal, capture: !0 }), D.dragEnd && (E = ce(a, e)), A.remove(x), A.add(I), w && (ee.userSelect = fe), re("neodrag:end", De, t), n && (k = R), o && (B = T)), H = !1, L = !1, O = !1, J = !1);
91
- }), oe), { destroy: () => ne.abort(), update: (t) => {
88
+ n && (T = Math.round((c - q) * p)), o && (O = Math.round((g - X) * p)), U = T, J = O, ot("neodrag", Nt, r), nt();
89
+ }), st), F("pointerup", ((r) => {
90
+ j.delete(r.pointerId), L && ($ && (F("click", ((c) => c.stopPropagation()), { once: !0, signal: at.signal, capture: !0 }), x.dragEnd && (C = ut(a, t)), R.remove(b), R.add(A), w && (rt.userSelect = pt), ot("neodrag:end", At, r), n && (q = T), o && (X = O)), L = !1, $ = !1, Y = !1, _ = !1);
91
+ }), st), { destroy: () => at.abort(), update: (r) => {
92
92
  var g, p;
93
- c = t.axis || "both", N = t.disabled ?? !1, v = t.ignoreMultitouch ?? !1, M = t.handle, a = t.bounds, D = t.recomputeBounds ?? ae, q = t.cancel, w = t.applyUserSelectHack ?? !0, f = t.grid, l = t.gpuAcceleration ?? !0, d = t.legacyTranslate ?? !1, m = t.transform, y = { ...se, ...t.threshold ?? {} };
94
- const s = A.contains(I);
95
- A.remove(u, I), u = t.defaultClass ?? "neodrag", x = t.defaultClassDragging ?? "neodrag-dragging", I = t.defaultClassDragged ?? "neodrag-dragged", A.add(u), s && A.add(I), xe && (j = R = ((g = t.position) == null ? void 0 : g.x) ?? R, z = T = ((p = t.position) == null ? void 0 : p.y) ?? T, te());
93
+ s = r.axis || "both", D = r.disabled ?? !1, N = r.ignoreMultitouch ?? !1, m = r.handle, a = r.bounds, x = r.recomputeBounds ?? it, u = r.cancel, w = r.applyUserSelectHack ?? !0, I = r.grid, f = r.gpuAcceleration ?? !0, d = r.legacyTranslate ?? !1, v = r.transform, y = { ...ct, ...r.threshold ?? {} };
94
+ const c = R.contains(A);
95
+ R.remove(i, A), i = r.defaultClass ?? "neodrag", b = r.defaultClassDragging ?? "neodrag-dragging", A = r.defaultClassDragged ?? "neodrag-dragged", R.add(i), c && R.add(A), Ct && (U = T = ((g = r.position) == null ? void 0 : g.x) ?? T, J = O = ((p = r.position) == null ? void 0 : p.y) ?? O, nt());
96
96
  } };
97
97
  }
98
- var he = (e, r, n) => Math.min(Math.max(e, r), n), ie = (e) => typeof e == "string", Ne = ([e, r], n, o) => {
99
- const a = (c, l) => l === 0 ? 0 : Math.ceil(c / l) * l;
100
- return [a(n, e), a(o, r)];
101
- }, ye = (e, r) => e.some(((n) => r.some(((o) => n.contains(o)))));
102
- function ce(e, r) {
103
- if (e === void 0) return;
104
- if (ue(e)) return e.getBoundingClientRect();
105
- if (typeof e == "object") {
106
- const { top: o = 0, left: a = 0, right: c = 0, bottom: l = 0 } = e;
107
- return { top: o, right: window.innerWidth - c, bottom: window.innerHeight - l, left: a };
98
+ var mt = (t, e, n) => Math.min(Math.max(t, e), n), lt = (t) => typeof t == "string", Tt = ([t, e], n, o) => {
99
+ const a = (s, f) => f === 0 ? 0 : Math.ceil(s / f) * f;
100
+ return [a(n, t), a(o, e)];
101
+ }, St = (t, e) => t.some(((n) => e.some(((o) => n.contains(o)))));
102
+ function ut(t, e) {
103
+ if (t === void 0) return;
104
+ if (dt(t)) return t.getBoundingClientRect();
105
+ if (typeof t == "object") {
106
+ const { top: o = 0, left: a = 0, right: s = 0, bottom: f = 0 } = t;
107
+ return { top: o, right: window.innerWidth - s, bottom: window.innerHeight - f, left: a };
108
108
  }
109
- if (e === "parent") return r.parentNode.getBoundingClientRect();
110
- const n = document.querySelector(e);
109
+ if (t === "parent") return e.parentNode.getBoundingClientRect();
110
+ const n = document.querySelector(t);
111
111
  if (n === null) throw new Error("The selector provided for bound doesn't exists in the document.");
112
112
  return n.getBoundingClientRect();
113
113
  }
114
- var W = (e, r, n) => e.style.setProperty(r, n), ue = (e) => e instanceof HTMLElement;
115
- function F(e) {
116
- return e == null || typeof e == "string" || e instanceof HTMLElement ? e : "current" in e ? e.current : Array.isArray(e) ? e.map(((r) => r instanceof HTMLElement ? r : r.current)) : void 0;
114
+ var Q = (t, e, n) => t.style.setProperty(e, n), dt = (t) => t instanceof HTMLElement;
115
+ function z(t) {
116
+ return t == null || typeof t == "string" || t instanceof HTMLElement ? t : "current" in t ? t.current : Array.isArray(t) ? t.map(((e) => e instanceof HTMLElement ? e : e.current)) : void 0;
117
117
  }
118
- function qe(e, r = {}) {
119
- const n = le(), [o, a] = Y(!1), [c, l] = Y();
120
- let { onDragStart: d, onDrag: m, onDragEnd: w, handle: N, cancel: v } = r, D = F(N), f = F(v);
121
- function y(u, x) {
122
- l(u), x == null || x(u);
118
+ function Ot(t, e = {}) {
119
+ const n = W(), [o, a] = B(!1), [s, f] = B();
120
+ let { onDragStart: d, onDrag: v, onDragEnd: w, handle: D, cancel: N } = e, x = z(D), I = z(N);
121
+ function y(i, b) {
122
+ f(i), b == null || b(i);
123
123
  }
124
- function b(u) {
125
- a(!0), y(u, d);
124
+ function S(i) {
125
+ a(!0), y(i, d);
126
126
  }
127
- function q(u) {
128
- y(u, m);
127
+ function u(i) {
128
+ y(i, v);
129
129
  }
130
- function M(u) {
131
- a(!1), y(u, w);
130
+ function m(i) {
131
+ a(!1), y(i, w);
132
132
  }
133
- return P((() => {
133
+ return ft((() => {
134
134
  if (typeof window > "u") return;
135
- const u = e.current;
136
- if (!u) return;
137
- ({ onDragStart: d, onDrag: m, onDragEnd: w } = r);
138
- const { update: x, destroy: I } = Ae(u, { ...r, handle: D, cancel: f, onDragStart: b, onDrag: q, onDragEnd: M });
139
- return n.current = x, I;
140
- }), []), P((() => {
141
- var u;
142
- (u = n.current) == null || u.call(n, { ...r, handle: F(N), cancel: F(v), onDragStart: b, onDrag: q, onDragEnd: M });
143
- }), [r]), { isDragging: o, dragState: c };
135
+ const i = t.current;
136
+ if (!i) return;
137
+ ({ onDragStart: d, onDrag: v, onDragEnd: w } = e);
138
+ const { update: b, destroy: A } = It(i, { ...e, handle: x, cancel: I, onDragStart: S, onDrag: u, onDragEnd: m });
139
+ return n.current = b, A;
140
+ }), []), ft((() => {
141
+ var i;
142
+ (i = n.current) == null || i.call(n, { ...e, handle: z(D), cancel: z(N), onDragStart: S, onDrag: u, onDragEnd: m });
143
+ }), [e]), { isDragging: o, dragState: s };
144
144
  }
145
- const Me = (e) => {
146
- if (!e) return null;
147
- let r = null;
145
+ const H = "tiny-canvas-queue";
146
+ function wt() {
147
+ const t = localStorage.getItem(H);
148
+ if (!t) return [];
149
+ try {
150
+ const e = JSON.parse(t);
151
+ return Array.isArray(e) ? e : [];
152
+ } catch {
153
+ return localStorage.setItem(H, JSON.stringify([])), [];
154
+ }
155
+ }
156
+ const Pt = (t) => {
157
+ if (!t) return null;
158
+ let e = null;
148
159
  const n = (o) => {
149
160
  if (o.props && o.props.id) {
150
- r = o.props.id;
161
+ e = o.props.id;
151
162
  return;
152
163
  }
153
- o.props && o.props.children && me.Children.forEach(o.props.children, n);
164
+ o.props && o.props.children && vt.Children.forEach(o.props.children, n);
154
165
  };
155
- return n(e), r;
166
+ return n(t), e;
156
167
  };
157
- function Ie(e) {
158
- let r = 5381;
159
- for (let n = 0; n < e.length; n++)
160
- r = (r << 5) + r + e.charCodeAt(n) >>> 0;
161
- return r.toString(16).padStart(8, "0");
168
+ function $t(t) {
169
+ let e = 5381;
170
+ for (let n = 0; n < t.length; n++)
171
+ e = (e << 5) + e + t.charCodeAt(n) >>> 0;
172
+ return e.toString(16).padStart(8, "0");
162
173
  }
163
- function ve(e) {
164
- var c;
165
- if (e == null || typeof e == "boolean") return "";
166
- if (typeof e == "string" || typeof e == "number") return "#text";
167
- const r = e.type, n = typeof r == "function" ? r.displayName || r.name || "Anonymous" : typeof r == "string" ? r : "Fragment", o = (c = e.props) == null ? void 0 : c.children;
174
+ function Dt(t) {
175
+ var s;
176
+ if (t == null || typeof t == "boolean") return "";
177
+ if (typeof t == "string" || typeof t == "number") return "#text";
178
+ const e = t.type, n = typeof e == "function" ? e.displayName || e.name || "Anonymous" : typeof e == "string" ? e : "Fragment", o = (s = t.props) == null ? void 0 : s.children;
168
179
  if (o == null) return n;
169
180
  const a = [];
170
- return me.Children.forEach(o, (l) => {
171
- const d = ve(l);
181
+ return vt.Children.forEach(o, (f) => {
182
+ const d = Dt(f);
172
183
  d && a.push(d);
173
184
  }), a.length ? `${n}(${a.join(",")})` : n;
174
185
  }
175
- const Re = (e, r) => {
176
- const n = ve(e);
177
- return `tc-${Ie(n)}-${r}`;
178
- }, Te = (e) => {
186
+ const qt = (t, e) => {
187
+ const n = Dt(t);
188
+ return `tc-${$t(n)}-${e}`;
189
+ }, Ut = (t) => {
179
190
  try {
180
- return (JSON.parse(localStorage.getItem("tiny-canvas-queue")) || []).reduce((o, a) => (o[a.id] = { id: a.id, x: a.x, y: a.y }, o), {})[e] || { x: 0, y: 0 };
181
- } catch (r) {
182
- return console.error("Error getting saved coordinates:", r), { x: 0, y: 0 };
191
+ return wt().reduce((o, a) => (o[a.id] = { id: a.id, x: a.x, y: a.y }, o), {})[t] || { x: 0, y: 0 };
192
+ } catch (e) {
193
+ return console.error("Error getting saved coordinates:", e), { x: 0, y: 0 };
183
194
  }
184
- }, $e = () => {
195
+ }, Jt = () => {
185
196
  try {
186
- localStorage.getItem("tiny-canvas-queue") || localStorage.setItem("tiny-canvas-queue", JSON.stringify([]));
187
- } catch (e) {
188
- console.error("LocalStorage is not available:", e);
197
+ localStorage.getItem(H) || localStorage.setItem(H, JSON.stringify([]));
198
+ } catch (t) {
199
+ console.error("LocalStorage is not available:", t);
189
200
  }
190
- }, Le = (e, r, n) => {
201
+ }, Xt = (t, e, n) => {
191
202
  try {
192
- const o = JSON.parse(localStorage.getItem("tiny-canvas-queue")) || [], a = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-"), c = { id: e, x: r, y: n, time: a }, l = o.findIndex((d) => d.id === e);
193
- l >= 0 ? o[l] = c : o.push(c), localStorage.setItem("tiny-canvas-queue", JSON.stringify(o));
203
+ const o = wt(), a = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-"), s = { id: t, x: e, y: n, time: a }, f = o.findIndex((d) => d.id === t);
204
+ f >= 0 ? o[f] = s : o.push(s), localStorage.setItem(H, JSON.stringify(o));
194
205
  } catch (o) {
195
206
  console.error("Error saving drag position:", o);
196
207
  }
197
- }, ke = 250;
198
- function Be({ children: e, gridSize: r, dragId: n }) {
199
- const o = le(null), a = le(Te(n)).current, [c, l] = Y(!1), [d, m] = Y({ x: 0, y: 0 }), [w, N] = Y(
200
- Math.random() < 0.5 ? -0.5 : 0.5
208
+ }, Lt = 250, bt = 4, Yt = 10, V = 1.5;
209
+ function kt({ children: t, dragId: e, initialPosition: n, onDragEnd: o }) {
210
+ const a = W(null), s = n || { x: 0, y: 0 }, f = W(s), d = W(!1), [v, w] = B(!1), [D, N] = B(s), [x, I] = B(
211
+ () => Math.random() < 0.5 ? -V : V
201
212
  );
202
- P(() => {
203
- const f = o.current;
204
- if (f && n && a && (a.x !== 0 || a.y !== 0)) {
205
- f.classList.add("tc-on-translation"), l(!0);
206
- const y = setTimeout(() => {
207
- f.classList.remove("tc-on-translation"), l(!1);
208
- }, ke * 4);
209
- return () => clearTimeout(y);
213
+ ft(() => {
214
+ const u = a.current;
215
+ if (u && e && (s.x !== 0 || s.y !== 0)) {
216
+ u.classList.add("tc-on-translation");
217
+ const m = setTimeout(() => {
218
+ u.classList.remove("tc-on-translation");
219
+ }, Lt * 4);
220
+ return () => {
221
+ clearTimeout(m);
222
+ };
210
223
  }
211
- }, []), P(() => {
212
- $e(), o.current && a && m({ x: a.x, y: a.y });
213
- }, [a]);
214
- const { isDragging: v } = qe(o, {
224
+ }, [e, s.x, s.y]);
225
+ const { isDragging: y } = Ot(a, {
215
226
  axis: "both",
216
- bounds: "body",
217
227
  threshold: { delay: 50, distance: 30 },
218
228
  defaultClass: "tc-drag",
219
229
  defaultClassDragging: "tc-on",
220
230
  defaultClassDragged: "tc-off",
221
231
  applyUserSelectHack: !0,
222
- position: { x: d.x, y: d.y },
223
- onDrag: ({ offsetX: f, offsetY: y }) => m({ x: f, y }),
224
- onDragEnd: (f) => {
225
- m({ x: f.offsetX, y: f.offsetY }), n !== null && Le(n, f.offsetX, f.offsetY);
232
+ position: { x: D.x, y: D.y },
233
+ onDragStart: () => {
234
+ f.current = D, d.current = !1, w(!1);
235
+ },
236
+ onDrag: ({ offsetX: u, offsetY: m }) => {
237
+ const i = u - f.current.x, b = m - f.current.y, A = Math.hypot(i, b);
238
+ !d.current && Math.hypot(i, b) >= bt && (d.current = !0), !v && A >= Yt && w(!0), N({ x: u, y: m });
239
+ },
240
+ onDragEnd: (u) => {
241
+ N({ x: u.offsetX, y: u.offsetY }), I(Math.random() < 0.5 ? -V : V), w(!1);
242
+ const m = u.offsetX - f.current.x, i = u.offsetY - f.current.y;
243
+ (d.current || Math.hypot(m, i) >= bt) && (e && Xt(e, u.offsetX, u.offsetY), o == null || o(e, { x: u.offsetX, y: u.offsetY }));
226
244
  }
227
- }), D = v || c ? `${w}deg` : "0deg";
228
- return P(() => {
229
- N(Math.random() < 0.5 ? -0.5 : 0.5);
230
- }, [v]), /* @__PURE__ */ Q(
245
+ }), S = y && v ? `${x}deg` : "0deg";
246
+ return /* @__PURE__ */ Z(
231
247
  "article",
232
248
  {
233
- ref: o,
234
- style: { cursor: v ? "grabbing" : "grab" },
235
- children: /* @__PURE__ */ Q(
249
+ ref: a,
250
+ style: { cursor: y ? "grabbing" : "grab" },
251
+ children: /* @__PURE__ */ Z(
236
252
  "div",
237
253
  {
238
254
  className: "tc-draggable-inner",
239
255
  style: {
240
- transform: v || c ? `rotate(${D})` : void 0,
256
+ transform: y ? `rotate(${S})` : void 0,
241
257
  transition: "transform ease-in-out 150ms"
242
258
  },
243
- children: e
259
+ children: t
244
260
  }
245
261
  )
246
262
  }
247
263
  );
248
264
  }
249
- function Xe({
250
- children: e,
251
- centered: r = !0,
252
- dotted: n = !1,
253
- grid: o = !1,
254
- gridSize: a,
255
- colorMode: c = "auto"
265
+ function Bt(t) {
266
+ var o, a;
267
+ const e = Number((o = t == null ? void 0 : t.props) == null ? void 0 : o["data-tc-x"]), n = Number((a = t == null ? void 0 : t.props) == null ? void 0 : a["data-tc-y"]);
268
+ return Number.isFinite(e) && Number.isFinite(n) ? { x: e, y: n } : null;
269
+ }
270
+ function jt({
271
+ children: t,
272
+ dotted: e = !1,
273
+ grid: n = !1,
274
+ gridSize: o,
275
+ colorMode: a = "auto",
276
+ onDragEnd: s
256
277
  }) {
257
- return /* @__PURE__ */ Q(
278
+ return /* @__PURE__ */ Z(
258
279
  "main",
259
280
  {
260
281
  className: "tc-canvas",
261
- "data-dotted": n || o || void 0,
262
- "data-color-mode": c !== "auto" ? c : void 0,
263
- children: Ee.map(e, (d, m) => {
264
- const w = Me(d) ?? Re(d, m);
265
- return /* @__PURE__ */ Q(Be, { gridSize: a, dragId: w, children: d }, m);
282
+ "data-dotted": e || n || void 0,
283
+ "data-color-mode": a !== "auto" ? a : void 0,
284
+ children: Mt.map(t, (d, v) => {
285
+ const w = Pt(d) ?? qt(d, v), D = Bt(d);
286
+ return /* @__PURE__ */ Z(
287
+ kt,
288
+ {
289
+ gridSize: o,
290
+ dragId: w,
291
+ initialPosition: D,
292
+ onDragEnd: s,
293
+ children: d
294
+ },
295
+ v
296
+ );
266
297
  })
267
298
  }
268
299
  );
269
300
  }
270
- function Ye({ reload: e = !1 } = {}) {
271
- return Ce(() => {
301
+ function Ft({ reload: t = !1 } = {}) {
302
+ return Rt(() => {
272
303
  try {
273
304
  localStorage.removeItem("tiny-canvas-queue");
274
- } catch (r) {
275
- console.error("Error clearing canvas state:", r);
305
+ } catch (e) {
306
+ console.error("Error clearing canvas state:", e);
276
307
  }
277
- e && window.location.reload();
278
- }, [e]);
308
+ t && window.location.reload();
309
+ }, [t]);
279
310
  }
280
311
  export {
281
- Xe as Canvas,
282
- Be as Draggable,
283
- Me as findDragId,
284
- Re as generateDragId,
285
- Te as getQueue,
286
- $e as refreshStorage,
287
- Le as saveDrag,
288
- Ye as useResetCanvas
312
+ jt as Canvas,
313
+ kt as Draggable,
314
+ Pt as findDragId,
315
+ qt as generateDragId,
316
+ Ut as getQueue,
317
+ Jt as refreshStorage,
318
+ Xt as saveDrag,
319
+ Ft as useResetCanvas
289
320
  };
package/package.json CHANGED
@@ -1,36 +1,26 @@
1
1
  {
2
2
  "name": "@dfosco/tiny-canvas",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "A lightweight React canvas with draggable elements and persistent positions",
7
- "main": "./src/index.js",
8
- "module": "./src/index.js",
7
+ "main": "./dist/tiny-canvas.js",
8
+ "module": "./dist/tiny-canvas.js",
9
9
  "types": "./src/index.d.ts",
10
10
  "exports": {
11
11
  ".": {
12
- "import": "./src/index.js",
12
+ "import": "./dist/tiny-canvas.js",
13
13
  "types": "./src/index.d.ts"
14
14
  },
15
- "./style.css": "./src/style.css"
16
- },
17
- "publishConfig": {
18
- "main": "./dist/tiny-canvas.js",
19
- "module": "./dist/tiny-canvas.js",
20
- "exports": {
21
- ".": {
22
- "import": "./dist/tiny-canvas.js",
23
- "types": "./src/index.d.ts"
24
- },
25
- "./style.css": "./dist/tiny-canvas.css"
26
- }
15
+ "./style.css": "./dist/tiny-canvas.css"
27
16
  },
28
17
  "files": [
29
18
  "dist",
30
19
  "src/index.d.ts"
31
20
  ],
32
21
  "scripts": {
33
- "build": "vite build"
22
+ "build": "vite build",
23
+ "prepublishOnly": "npm run build"
34
24
  },
35
25
  "peerDependencies": {
36
26
  "react": "^18.0.0 || ^19.0.0",