@studiometa/js-toolkit 2.0.0-beta.15 → 2.0.0-beta.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@studiometa/js-toolkit",
3
- "version": "2.0.0-beta.15",
3
+ "version": "2.0.0-beta.16",
4
4
  "description": "A set of useful little bits of JavaScript to boost your project! 🚀",
5
5
  "publishConfig": {
6
6
  "access": "public"
package/services/drag.cjs CHANGED
@@ -47,7 +47,7 @@ var MODES = {
47
47
  INERTIA: "inertia",
48
48
  STOP: "stop"
49
49
  };
50
- var instanceIds = 0;
50
+ var id = 0;
51
51
  var Drag = class extends import_Service.default {
52
52
  constructor(target, options = {}) {
53
53
  super();
@@ -77,10 +77,11 @@ var Drag = class extends import_Service.default {
77
77
  }
78
78
  });
79
79
  __publicField(this, "options", {
80
- dampFactor: 0.85
80
+ dampFactor: 0.85,
81
+ dragTreshold: 10
81
82
  });
82
- instanceIds += 1;
83
- this.id = `drag-${instanceIds}`;
83
+ id += 1;
84
+ this.id = `drag-${id}`;
84
85
  this.props.target = target;
85
86
  this.options = { ...this.options, ...options };
86
87
  this.pointerHandler = this.pointerHandler.bind(this);
@@ -91,18 +92,34 @@ var Drag = class extends import_Service.default {
91
92
  const options = { passive: true };
92
93
  this.props.target.addEventListener("mousedown", this, options);
93
94
  this.props.target.addEventListener("touchstart", this, options);
95
+ this.props.target.addEventListener("dragstart", this, { capture: true });
96
+ this.props.target.addEventListener("click", this, { capture: true });
94
97
  const pointer = (0, import_pointer.default)();
95
98
  pointer.add(this.id, this.pointerHandler);
96
99
  return this;
97
100
  }
98
101
  kill() {
99
- this.props.target.removeEventListener("mousemove", this);
100
- this.props.target.removeEventListener("touchmove", this);
102
+ this.props.target.removeEventListener("mousedown", this);
103
+ this.props.target.removeEventListener("touchstart", this);
104
+ this.props.target.removeEventListener("dragstart", this);
105
+ this.props.target.removeEventListener("click", this);
101
106
  const pointer = (0, import_pointer.default)();
102
107
  pointer.remove(this.id);
103
108
  return this;
104
109
  }
105
110
  handleEvent(event) {
111
+ if (event.type === "dragstart") {
112
+ event.preventDefault();
113
+ return;
114
+ }
115
+ if (event.type === "click") {
116
+ if (this.__shouldPreventClick) {
117
+ event.stopImmediatePropagation();
118
+ event.stopPropagation();
119
+ event.preventDefault();
120
+ }
121
+ return;
122
+ }
106
123
  const x = event instanceof MouseEvent ? event.clientX : event.touches[0].clientX;
107
124
  const y = event instanceof MouseEvent ? event.clientY : event.touches[0].clientY;
108
125
  this.start(x, y);
@@ -159,6 +176,7 @@ var Drag = class extends import_Service.default {
159
176
  }
160
177
  }
161
178
  pointerHandler(props) {
179
+ props.event.preventDefault();
162
180
  if (this.props.isGrabbing) {
163
181
  this.props.x = props.x;
164
182
  this.props.y = props.y;
@@ -180,6 +198,9 @@ var Drag = class extends import_Service.default {
180
198
  updateProps() {
181
199
  return this.props;
182
200
  }
201
+ get __shouldPreventClick() {
202
+ return Math.abs(this.props.distance.x) > this.options.dragTreshold || Math.abs(this.props.distance.y) > this.options.dragTreshold;
203
+ }
183
204
  };
184
205
  var instances = /* @__PURE__ */ new WeakMap();
185
206
  function useDrag(target, options) {
@@ -74,4 +74,5 @@ export type DragServiceProps = {
74
74
  };
75
75
  export type DragServiceOptions = {
76
76
  dampFactor?: number | undefined;
77
+ dragTreshold?: number | undefined;
77
78
  };
package/services/drag.js CHANGED
@@ -15,7 +15,7 @@ const MODES = {
15
15
  INERTIA: "inertia",
16
16
  STOP: "stop"
17
17
  };
18
- let instanceIds = 0;
18
+ let id = 0;
19
19
  class Drag extends Service {
20
20
  constructor(target, options = {}) {
21
21
  super();
@@ -45,10 +45,11 @@ class Drag extends Service {
45
45
  }
46
46
  });
47
47
  __publicField(this, "options", {
48
- dampFactor: 0.85
48
+ dampFactor: 0.85,
49
+ dragTreshold: 10
49
50
  });
50
- instanceIds += 1;
51
- this.id = `drag-${instanceIds}`;
51
+ id += 1;
52
+ this.id = `drag-${id}`;
52
53
  this.props.target = target;
53
54
  this.options = { ...this.options, ...options };
54
55
  this.pointerHandler = this.pointerHandler.bind(this);
@@ -59,18 +60,34 @@ class Drag extends Service {
59
60
  const options = { passive: true };
60
61
  this.props.target.addEventListener("mousedown", this, options);
61
62
  this.props.target.addEventListener("touchstart", this, options);
63
+ this.props.target.addEventListener("dragstart", this, { capture: true });
64
+ this.props.target.addEventListener("click", this, { capture: true });
62
65
  const pointer = usePointer();
63
66
  pointer.add(this.id, this.pointerHandler);
64
67
  return this;
65
68
  }
66
69
  kill() {
67
- this.props.target.removeEventListener("mousemove", this);
68
- this.props.target.removeEventListener("touchmove", this);
70
+ this.props.target.removeEventListener("mousedown", this);
71
+ this.props.target.removeEventListener("touchstart", this);
72
+ this.props.target.removeEventListener("dragstart", this);
73
+ this.props.target.removeEventListener("click", this);
69
74
  const pointer = usePointer();
70
75
  pointer.remove(this.id);
71
76
  return this;
72
77
  }
73
78
  handleEvent(event) {
79
+ if (event.type === "dragstart") {
80
+ event.preventDefault();
81
+ return;
82
+ }
83
+ if (event.type === "click") {
84
+ if (this.__shouldPreventClick) {
85
+ event.stopImmediatePropagation();
86
+ event.stopPropagation();
87
+ event.preventDefault();
88
+ }
89
+ return;
90
+ }
74
91
  const x = event instanceof MouseEvent ? event.clientX : event.touches[0].clientX;
75
92
  const y = event instanceof MouseEvent ? event.clientY : event.touches[0].clientY;
76
93
  this.start(x, y);
@@ -127,6 +144,7 @@ class Drag extends Service {
127
144
  }
128
145
  }
129
146
  pointerHandler(props) {
147
+ props.event.preventDefault();
130
148
  if (this.props.isGrabbing) {
131
149
  this.props.x = props.x;
132
150
  this.props.y = props.y;
@@ -148,6 +166,9 @@ class Drag extends Service {
148
166
  updateProps() {
149
167
  return this.props;
150
168
  }
169
+ get __shouldPreventClick() {
170
+ return Math.abs(this.props.distance.x) > this.options.dragTreshold || Math.abs(this.props.distance.y) > this.options.dragTreshold;
171
+ }
151
172
  }
152
173
  const instances = /* @__PURE__ */ new WeakMap();
153
174
  function useDrag(target, options) {