@dfosco/tiny-canvas 1.1.0 → 1.1.1

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,243 +1,244 @@
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";
1
+ import { jsx as W } from "react/jsx-runtime";
2
+ import me, { useRef as le, useState as F, useEffect as Q, Children as Ee, useCallback as Ce } from "react";
3
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) {
4
+ function Ae(e, t = {}) {
5
+ let n, o, { bounds: a, axis: l = "both", gpuAcceleration: c = !0, legacyTranslate: p = !1, transform: y, applyUserSelectHack: C = !0, disabled: u = !1, ignoreMultitouch: m = !1, recomputeBounds: S = ae, grid: B, threshold: w = se, position: v, cancel: N, handle: q, defaultClass: d = "neodrag", defaultClassDragging: D = "neodrag-dragging", defaultClassDragged: M = "neodrag-dragged", defaultPosition: be = { x: 0, y: 0 }, onDragStart: Se, onDrag: we, onDragEnd: De } = t, H = !1, T = !1, de = 0, X = !1, O = !1, R = 0, $ = 0, L = 0, k = 0, _ = 0, G = 0, { x: P, y: J } = v ? { x: (v == null ? void 0 : v.x) ?? 0, y: (v == null ? void 0 : v.y) ?? 0 } : be;
6
+ te(P, J);
7
+ let x, I, Y, K, Z, fe = "", xe = !!v;
8
+ S = { ...ae, ...S }, w = { ...se, ...w ?? {} };
9
+ let j = /* @__PURE__ */ new Set();
10
+ function ge(r) {
11
+ H && !T && O && X && Z && (T = !0, (function(s) {
12
12
  re("neodrag:start", Se, s);
13
- })(t), A.add(x), w && (fe = ee.userSelect, ee.userSelect = "none"));
13
+ })(r), A.add(D), C && (fe = ee.userSelect, ee.userSelect = "none"));
14
14
  }
15
15
  const ee = document.body.style, A = e.classList;
16
- function te(t = R, s = T) {
17
- if (!m) {
18
- if (d) {
19
- let p = `${+t}px, ${+s}px`;
20
- return W(e, "transform", l ? `translate3d(${p}, 0)` : `translate(${p})`);
16
+ function te(r = R, s = $) {
17
+ if (!y) {
18
+ if (p) {
19
+ let g = `${+r}px, ${+s}px`;
20
+ return z(e, "transform", c ? `translate3d(${g}, 0)` : `translate(${g})`);
21
21
  }
22
- return W(e, "translate", `${+t}px ${+s}px`);
22
+ return z(e, "translate", `${+r}px ${+s}px`);
23
23
  }
24
- const g = m({ offsetX: t, offsetY: s, rootNode: e });
25
- ie(g) && W(e, "transform", g);
24
+ const f = y({ offsetX: r, offsetY: s, rootNode: e });
25
+ ie(f) && z(e, "transform", f);
26
26
  }
27
- function re(t, s, g) {
28
- const p = /* @__PURE__ */ (function(h) {
29
- return { offsetX: R, offsetY: T, rootNode: e, currentNode: Z, event: h };
30
- })(g);
31
- e.dispatchEvent(new CustomEvent(t, { detail: p })), s == null || s(p);
27
+ function re(r, s, f) {
28
+ const g = /* @__PURE__ */ (function(h) {
29
+ return { offsetX: R, offsetY: $, rootNode: e, currentNode: Z, event: h };
30
+ })(f);
31
+ e.dispatchEvent(new CustomEvent(r, { detail: g })), s == null || s(g);
32
32
  }
33
- const V = addEventListener, ne = new AbortController(), oe = { signal: ne.signal, capture: !1 };
33
+ const U = addEventListener, ne = new AbortController(), oe = { signal: ne.signal, capture: !1 };
34
34
  function pe() {
35
- let t = e.offsetWidth / $.width;
36
- return isNaN(t) && (t = 1), t;
35
+ let r = e.offsetWidth / I.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];
38
+ return z(e, "touch-action", "none"), U("pointerdown", ((r) => {
39
+ if (u || r.button === 2) return;
40
+ if (j.add(r.pointerId), m && j.size > 1) return r.preventDefault();
41
+ if (S.dragStart && (x = ce(a, e)), ie(q) && ie(N) && q === N) throw new Error("`handle` selector can't be same as `cancel` selector");
42
+ if (A.add(d), Y = (function(i, b) {
43
+ if (!i) return [b];
44
44
  if (ue(i)) return [i];
45
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) {
46
+ const E = b.querySelectorAll(i);
47
+ if (E === 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(E.values());
49
+ })(q, e), K = (function(i, b) {
50
50
  if (!i) return [];
51
51
  if (ue(i)) return [i];
52
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));
53
+ const E = b.querySelectorAll(i);
54
+ if (E === 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(E.values());
56
+ })(N, e), n = /(both|x)/.test(l), o = /(both|y)/.test(l), ye(K, Y)) throw new Error("Element being dragged can't be a child of the element on which `cancel` is applied");
57
+ const s = r.composedPath()[0];
58
+ if (!Y.some(((i) => {
59
+ var b;
60
+ return i.contains(s) || ((b = i.shadowRoot) == null ? void 0 : b.contains(s));
61
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));
62
+ Z = Y.length === 1 ? e : Y.find(((i) => i.contains(s))), H = !0, de = Date.now(), w.delay || (X = !0), I = e.getBoundingClientRect();
63
+ const { clientX: f, clientY: g } = r, h = pe();
64
+ n && (L = f - P / h), o && (k = g - J / h), x && (_ = f - I.left, G = g - I.top);
65
+ }), oe), U("pointermove", ((r) => {
66
+ if (!H || m && j.size > 1) return;
67
+ if (!T) {
68
+ if (X || Date.now() - de >= w.delay && (X = !0, ge(r)), !O) {
69
+ const h = r.clientX - L, i = r.clientY - k;
70
+ Math.sqrt(h ** 2 + i ** 2) >= w.distance && (O = !0, ge(r));
71
71
  }
72
- if (!L) return;
72
+ if (!T) 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
+ S.drag && (x = ce(a, e)), r.preventDefault(), I = e.getBoundingClientRect();
75
+ let s = r.clientX, f = r.clientY;
76
+ const g = pe();
77
+ if (x) {
78
+ const h = { left: x.left + _, top: x.top + G, right: x.right + _ - I.width, bottom: x.bottom + G - I.height };
79
+ s = he(s, h.left, h.right), f = he(f, h.top, h.bottom);
80
80
  }
81
- if (Array.isArray(f)) {
82
- let [h, i] = f;
81
+ if (Array.isArray(B)) {
82
+ let [h, i] = B;
83
83
  if (isNaN(+h) || h < 0) throw new Error("1st argument of `grid` must be a valid positive number");
84
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;
85
+ let b = s - L, E = f - k;
86
+ [b, E] = Ne([h / g, i / g], b, E), s = L + b, f = k + E;
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) => {
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());
88
+ n && (R = Math.round((s - L) * g)), o && ($ = Math.round((f - k) * g)), P = R, J = $, re("neodrag", we, r), te();
89
+ }), oe), U("pointerup", ((r) => {
90
+ j.delete(r.pointerId), H && (T && (U("click", ((s) => s.stopPropagation()), { once: !0, signal: ne.signal, capture: !0 }), S.dragEnd && (x = ce(a, e)), A.remove(D), A.add(M), C && (ee.userSelect = fe), re("neodrag:end", De, r), n && (L = R), o && (k = $)), H = !1, T = !1, X = !1, O = !1);
91
+ }), oe), { destroy: () => ne.abort(), update: (r) => {
92
+ var f, g;
93
+ l = r.axis || "both", u = r.disabled ?? !1, m = r.ignoreMultitouch ?? !1, q = r.handle, a = r.bounds, S = r.recomputeBounds ?? ae, N = r.cancel, C = r.applyUserSelectHack ?? !0, B = r.grid, c = r.gpuAcceleration ?? !0, p = r.legacyTranslate ?? !1, y = r.transform, w = { ...se, ...r.threshold ?? {} };
94
+ const s = A.contains(M);
95
+ A.remove(d, M), d = r.defaultClass ?? "neodrag", D = r.defaultClassDragging ?? "neodrag-dragging", M = r.defaultClassDragged ?? "neodrag-dragged", A.add(d), s && A.add(M), xe && (P = R = ((f = r.position) == null ? void 0 : f.x) ?? R, J = $ = ((g = r.position) == null ? void 0 : g.y) ?? $, te());
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) {
98
+ var he = (e, t, n) => Math.min(Math.max(e, t), n), ie = (e) => typeof e == "string", Ne = ([e, t], n, o) => {
99
+ const a = (l, c) => c === 0 ? 0 : Math.ceil(l / c) * c;
100
+ return [a(n, e), a(o, t)];
101
+ }, ye = (e, t) => e.some(((n) => t.some(((o) => n.contains(o)))));
102
+ function ce(e, t) {
103
103
  if (e === void 0) return;
104
104
  if (ue(e)) return e.getBoundingClientRect();
105
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 };
106
+ const { top: o = 0, left: a = 0, right: l = 0, bottom: c = 0 } = e;
107
+ return { top: o, right: window.innerWidth - l, bottom: window.innerHeight - c, left: a };
108
108
  }
109
- if (e === "parent") return r.parentNode.getBoundingClientRect();
109
+ if (e === "parent") return t.parentNode.getBoundingClientRect();
110
110
  const n = document.querySelector(e);
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 z = (e, t, n) => e.style.setProperty(t, n), ue = (e) => e instanceof HTMLElement;
115
+ function V(e) {
116
+ return e == null || typeof e == "string" || e instanceof HTMLElement ? e : "current" in e ? e.current : Array.isArray(e) ? e.map(((t) => t instanceof HTMLElement ? t : t.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 qe(e, t = {}) {
119
+ const n = le(), [o, a] = F(!1), [l, c] = F();
120
+ let { onDragStart: p, onDrag: y, onDragEnd: C, handle: u, cancel: m } = t, S = V(u), B = V(m);
121
+ function w(d, D) {
122
+ c(d), D == null || D(d);
123
123
  }
124
- function b(u) {
125
- a(!0), y(u, d);
124
+ function v(d) {
125
+ a(!0), w(d, p);
126
126
  }
127
- function q(u) {
128
- y(u, m);
127
+ function N(d) {
128
+ w(d, y);
129
129
  }
130
- function M(u) {
131
- a(!1), y(u, w);
130
+ function q(d) {
131
+ a(!1), w(d, C);
132
132
  }
133
- return P((() => {
133
+ return Q((() => {
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 d = e.current;
136
+ if (!d) return;
137
+ ({ onDragStart: p, onDrag: y, onDragEnd: C } = t);
138
+ const { update: D, destroy: M } = Ae(d, { ...t, handle: S, cancel: B, onDragStart: v, onDrag: N, onDragEnd: q });
139
+ return n.current = D, M;
140
+ }), []), Q((() => {
141
+ var d;
142
+ (d = n.current) == null || d.call(n, { ...t, handle: V(u), cancel: V(m), onDragStart: v, onDrag: N, onDragEnd: q });
143
+ }), [t]), { isDragging: o, dragState: l };
144
144
  }
145
145
  const Me = (e) => {
146
146
  if (!e) return null;
147
- let r = null;
147
+ let t = null;
148
148
  const n = (o) => {
149
149
  if (o.props && o.props.id) {
150
- r = o.props.id;
150
+ t = o.props.id;
151
151
  return;
152
152
  }
153
153
  o.props && o.props.children && me.Children.forEach(o.props.children, n);
154
154
  };
155
- return n(e), r;
155
+ return n(e), t;
156
156
  };
157
- function Ie(e) {
158
- let r = 5381;
157
+ function Re(e) {
158
+ let t = 5381;
159
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");
160
+ t = (t << 5) + t + e.charCodeAt(n) >>> 0;
161
+ return t.toString(16).padStart(8, "0");
162
162
  }
163
163
  function ve(e) {
164
- var c;
164
+ var l;
165
165
  if (e == null || typeof e == "boolean") return "";
166
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;
167
+ const t = e.type, n = typeof t == "function" ? t.displayName || t.name || "Anonymous" : typeof t == "string" ? t : "Fragment", o = (l = e.props) == null ? void 0 : l.children;
168
168
  if (o == null) return n;
169
169
  const a = [];
170
- return me.Children.forEach(o, (l) => {
171
- const d = ve(l);
172
- d && a.push(d);
170
+ return me.Children.forEach(o, (c) => {
171
+ const p = ve(c);
172
+ p && a.push(p);
173
173
  }), a.length ? `${n}(${a.join(",")})` : n;
174
174
  }
175
- const Re = (e, r) => {
175
+ const $e = (e, t) => {
176
176
  const n = ve(e);
177
- return `tc-${Ie(n)}-${r}`;
178
- }, Te = (e) => {
177
+ return `tc-${Re(n)}-${t}`;
178
+ }, Ie = (e) => {
179
179
  try {
180
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 };
181
+ } catch (t) {
182
+ return console.error("Error getting saved coordinates:", t), { x: 0, y: 0 };
183
183
  }
184
- }, $e = () => {
184
+ }, Te = () => {
185
185
  try {
186
186
  localStorage.getItem("tiny-canvas-queue") || localStorage.setItem("tiny-canvas-queue", JSON.stringify([]));
187
187
  } catch (e) {
188
188
  console.error("LocalStorage is not available:", e);
189
189
  }
190
- }, Le = (e, r, n) => {
190
+ }, Le = (e, t, n) => {
191
191
  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));
192
+ const o = JSON.parse(localStorage.getItem("tiny-canvas-queue")) || [], a = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-"), l = { id: e, x: t, y: n, time: a }, c = o.findIndex((p) => p.id === e);
193
+ c >= 0 ? o[c] = l : o.push(l), localStorage.setItem("tiny-canvas-queue", JSON.stringify(o));
194
194
  } catch (o) {
195
195
  console.error("Error saving drag position:", o);
196
196
  }
197
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
198
+ function Be({ children: e, dragId: t }) {
199
+ const n = le(null), o = le(Ie(t)), [a, l] = F({ x: 0, y: 0 }), [c, p] = F(
200
+ () => Math.random() < 0.5 ? -0.5 : 0.5
201
201
  );
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);
202
+ Q(() => {
203
+ const u = n.current, m = o.current;
204
+ if (u && t && m && (m.x !== 0 || m.y !== 0)) {
205
+ u.classList.add("tc-on-translation");
206
+ const S = setTimeout(() => {
207
+ u.classList.remove("tc-on-translation");
208
208
  }, ke * 4);
209
- return () => clearTimeout(y);
209
+ return () => {
210
+ clearTimeout(S);
211
+ };
210
212
  }
211
- }, []), P(() => {
212
- $e(), o.current && a && m({ x: a.x, y: a.y });
213
- }, [a]);
214
- const { isDragging: v } = qe(o, {
213
+ }, [t]), Q(() => {
214
+ Te();
215
+ const u = o.current;
216
+ n.current && u && l({ x: u.x, y: u.y });
217
+ }, []);
218
+ const { isDragging: y } = qe(n, {
215
219
  axis: "both",
216
- bounds: "body",
217
220
  threshold: { delay: 50, distance: 30 },
218
221
  defaultClass: "tc-drag",
219
222
  defaultClassDragging: "tc-on",
220
223
  defaultClassDragged: "tc-off",
221
224
  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);
225
+ position: { x: a.x, y: a.y },
226
+ onDrag: ({ offsetX: u, offsetY: m }) => l({ x: u, y: m }),
227
+ onDragEnd: (u) => {
228
+ l({ x: u.offsetX, y: u.offsetY }), p(Math.random() < 0.5 ? -0.5 : 0.5), t !== null && Le(t, u.offsetX, u.offsetY);
226
229
  }
227
- }), D = v || c ? `${w}deg` : "0deg";
228
- return P(() => {
229
- N(Math.random() < 0.5 ? -0.5 : 0.5);
230
- }, [v]), /* @__PURE__ */ Q(
230
+ }), C = y ? `${c}deg` : "0deg";
231
+ return /* @__PURE__ */ W(
231
232
  "article",
232
233
  {
233
- ref: o,
234
- style: { cursor: v ? "grabbing" : "grab" },
235
- children: /* @__PURE__ */ Q(
234
+ ref: n,
235
+ style: { cursor: y ? "grabbing" : "grab" },
236
+ children: /* @__PURE__ */ W(
236
237
  "div",
237
238
  {
238
239
  className: "tc-draggable-inner",
239
240
  style: {
240
- transform: v || c ? `rotate(${D})` : void 0,
241
+ transform: y ? `rotate(${C})` : void 0,
241
242
  transition: "transform ease-in-out 150ms"
242
243
  },
243
244
  children: e
@@ -246,44 +247,43 @@ function Be({ children: e, gridSize: r, dragId: n }) {
246
247
  }
247
248
  );
248
249
  }
249
- function Xe({
250
+ function Ye({
250
251
  children: e,
251
- centered: r = !0,
252
- dotted: n = !1,
253
- grid: o = !1,
254
- gridSize: a,
255
- colorMode: c = "auto"
252
+ dotted: t = !1,
253
+ grid: n = !1,
254
+ gridSize: o,
255
+ colorMode: a = "auto"
256
256
  }) {
257
- return /* @__PURE__ */ Q(
257
+ return /* @__PURE__ */ W(
258
258
  "main",
259
259
  {
260
260
  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);
261
+ "data-dotted": t || n || void 0,
262
+ "data-color-mode": a !== "auto" ? a : void 0,
263
+ children: Ee.map(e, (c, p) => {
264
+ const y = Me(c) ?? $e(c, p);
265
+ return /* @__PURE__ */ W(Be, { gridSize: o, dragId: y, children: c }, p);
266
266
  })
267
267
  }
268
268
  );
269
269
  }
270
- function Ye({ reload: e = !1 } = {}) {
270
+ function Oe({ reload: e = !1 } = {}) {
271
271
  return Ce(() => {
272
272
  try {
273
273
  localStorage.removeItem("tiny-canvas-queue");
274
- } catch (r) {
275
- console.error("Error clearing canvas state:", r);
274
+ } catch (t) {
275
+ console.error("Error clearing canvas state:", t);
276
276
  }
277
277
  e && window.location.reload();
278
278
  }, [e]);
279
279
  }
280
280
  export {
281
- Xe as Canvas,
281
+ Ye as Canvas,
282
282
  Be as Draggable,
283
283
  Me as findDragId,
284
- Re as generateDragId,
285
- Te as getQueue,
286
- $e as refreshStorage,
284
+ $e as generateDragId,
285
+ Ie as getQueue,
286
+ Te as refreshStorage,
287
287
  Le as saveDrag,
288
- Ye as useResetCanvas
288
+ Oe as useResetCanvas
289
289
  };
package/package.json CHANGED
@@ -1,36 +1,26 @@
1
1
  {
2
2
  "name": "@dfosco/tiny-canvas",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
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",