@studiometa/js-toolkit 2.0.0-beta.1 → 2.0.0-beta.13

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.
Files changed (184) hide show
  1. package/Base/index.cjs +294 -0
  2. package/Base/index.d.ts +27 -1
  3. package/Base/index.js +149 -179
  4. package/Base/managers/ChildrenManager.cjs +124 -0
  5. package/Base/managers/ChildrenManager.js +29 -55
  6. package/Base/managers/EventsManager.cjs +184 -0
  7. package/Base/managers/EventsManager.d.ts +3 -1
  8. package/Base/managers/EventsManager.js +85 -127
  9. package/Base/managers/OptionsManager.cjs +175 -0
  10. package/Base/managers/OptionsManager.d.ts +2 -0
  11. package/Base/managers/OptionsManager.js +46 -88
  12. package/Base/managers/RefsManager.cjs +98 -0
  13. package/Base/managers/RefsManager.d.ts +2 -0
  14. package/Base/managers/RefsManager.js +24 -35
  15. package/Base/managers/ServicesManager.cjs +132 -0
  16. package/Base/managers/ServicesManager.d.ts +29 -4
  17. package/Base/managers/ServicesManager.js +33 -63
  18. package/Base/utils.cjs +45 -0
  19. package/Base/utils.js +12 -11
  20. package/README.md +13 -70
  21. package/decorators/index.cjs +49 -0
  22. package/decorators/index.d.ts +8 -9
  23. package/decorators/index.js +18 -9
  24. package/decorators/withBreakpointManager.cjs +91 -0
  25. package/decorators/withBreakpointManager.js +18 -39
  26. package/decorators/withBreakpointObserver.cjs +116 -0
  27. package/decorators/withBreakpointObserver.js +40 -88
  28. package/decorators/withDrag.cjs +54 -0
  29. package/decorators/withDrag.js +16 -18
  30. package/decorators/withExtraConfig.cjs +44 -0
  31. package/decorators/withExtraConfig.js +11 -12
  32. package/decorators/withIntersectionObserver.cjs +61 -0
  33. package/decorators/withIntersectionObserver.js +23 -31
  34. package/decorators/withMountWhenInView.cjs +68 -0
  35. package/decorators/withMountWhenInView.js +31 -55
  36. package/decorators/withScrolledInView.cjs +112 -0
  37. package/decorators/withScrolledInView.d.ts +4 -1
  38. package/decorators/withScrolledInView.js +77 -73
  39. package/decorators/withVue2.cjs +60 -0
  40. package/decorators/withVue2.js +21 -32
  41. package/helpers/createApp.cjs +56 -0
  42. package/helpers/createApp.js +12 -12
  43. package/helpers/importOnInteraction.cjs +56 -0
  44. package/helpers/importOnInteraction.js +17 -18
  45. package/helpers/importWhenIdle.cjs +52 -0
  46. package/helpers/importWhenIdle.js +13 -17
  47. package/helpers/importWhenVisible.cjs +54 -0
  48. package/helpers/importWhenVisible.js +16 -18
  49. package/helpers/index.cjs +41 -0
  50. package/helpers/index.d.ts +4 -5
  51. package/helpers/index.js +10 -5
  52. package/helpers/utils.cjs +39 -0
  53. package/helpers/utils.js +7 -6
  54. package/index.cjs +38 -0
  55. package/index.d.ts +4 -21
  56. package/index.js +7 -5
  57. package/package.json +14 -3
  58. package/services/Service.cjs +75 -0
  59. package/services/Service.d.ts +22 -17
  60. package/services/Service.js +14 -28
  61. package/services/drag.cjs +194 -0
  62. package/services/drag.d.ts +4 -18
  63. package/services/drag.js +103 -156
  64. package/services/index.cjs +47 -0
  65. package/services/index.d.ts +13 -10
  66. package/services/index.js +16 -8
  67. package/services/key.cjs +101 -0
  68. package/services/key.d.ts +4 -13
  69. package/services/key.js +58 -77
  70. package/services/load.cjs +70 -0
  71. package/services/load.d.ts +3 -13
  72. package/services/load.js +31 -36
  73. package/services/pointer.cjs +159 -0
  74. package/services/pointer.d.ts +4 -13
  75. package/services/pointer.js +91 -121
  76. package/services/raf.cjs +81 -0
  77. package/services/raf.d.ts +3 -13
  78. package/services/raf.js +32 -52
  79. package/services/resize.cjs +106 -0
  80. package/services/resize.d.ts +3 -13
  81. package/services/resize.js +52 -52
  82. package/services/scroll.cjs +129 -0
  83. package/services/scroll.d.ts +3 -13
  84. package/services/scroll.js +75 -89
  85. package/utils/collide/boundingRectToCircle.cjs +38 -0
  86. package/utils/collide/boundingRectToCircle.d.ts +1 -1
  87. package/utils/collide/boundingRectToCircle.js +7 -10
  88. package/utils/collide/collideCircleCircle.cjs +38 -0
  89. package/utils/collide/collideCircleCircle.js +8 -7
  90. package/utils/collide/collideCircleRect.cjs +50 -0
  91. package/utils/collide/collideCircleRect.js +10 -12
  92. package/utils/collide/collidePointCircle.cjs +35 -0
  93. package/utils/collide/collidePointCircle.js +8 -5
  94. package/utils/collide/collidePointRect.cjs +32 -0
  95. package/utils/collide/collidePointRect.js +5 -2
  96. package/utils/collide/collideRectRect.cjs +32 -0
  97. package/utils/collide/collideRectRect.js +5 -2
  98. package/utils/collide/index.cjs +45 -0
  99. package/utils/collide/index.d.ts +6 -7
  100. package/utils/collide/index.js +14 -7
  101. package/utils/css/classes.cjs +48 -0
  102. package/utils/css/classes.js +13 -12
  103. package/utils/css/index.cjs +41 -0
  104. package/utils/css/index.d.ts +4 -5
  105. package/utils/css/index.js +10 -5
  106. package/utils/css/matrix.cjs +33 -0
  107. package/utils/css/matrix.js +6 -5
  108. package/utils/css/styles.cjs +51 -0
  109. package/utils/css/styles.js +13 -12
  110. package/utils/css/transition.cjs +121 -0
  111. package/utils/css/transition.js +62 -117
  112. package/utils/debounce.cjs +38 -0
  113. package/utils/debounce.js +7 -9
  114. package/utils/focusTrap.cjs +88 -0
  115. package/utils/focusTrap.js +30 -21
  116. package/utils/history.cjs +95 -0
  117. package/utils/history.js +28 -57
  118. package/utils/index.cjs +57 -0
  119. package/utils/index.d.ts +12 -35
  120. package/utils/index.js +30 -17
  121. package/utils/keyCodes.cjs +39 -0
  122. package/utils/keyCodes.js +5 -2
  123. package/utils/math/clamp.cjs +32 -0
  124. package/utils/math/clamp.js +5 -2
  125. package/utils/math/clamp01.cjs +38 -0
  126. package/utils/math/clamp01.js +6 -3
  127. package/utils/math/damp.cjs +32 -0
  128. package/utils/math/damp.js +5 -4
  129. package/utils/math/ease.cjs +92 -0
  130. package/utils/math/ease.d.ts +78 -0
  131. package/utils/math/ease.js +66 -0
  132. package/utils/math/index.cjs +48 -0
  133. package/utils/math/index.d.ts +8 -8
  134. package/utils/math/index.js +17 -8
  135. package/utils/math/inertiaFinalValue.cjs +41 -0
  136. package/utils/math/inertiaFinalValue.js +8 -10
  137. package/utils/math/lerp.cjs +32 -0
  138. package/utils/math/lerp.js +5 -2
  139. package/utils/math/map.cjs +32 -0
  140. package/utils/math/map.js +5 -2
  141. package/utils/math/round.cjs +32 -0
  142. package/utils/math/round.js +5 -3
  143. package/utils/memoize.cjs +47 -0
  144. package/utils/memoize.js +10 -19
  145. package/utils/nextFrame.cjs +44 -0
  146. package/utils/nextFrame.js +14 -9
  147. package/utils/object/autoBind.cjs +55 -0
  148. package/utils/object/autoBind.js +12 -22
  149. package/utils/object/getAllProperties.cjs +41 -0
  150. package/utils/object/getAllProperties.js +11 -14
  151. package/utils/object/index.cjs +39 -0
  152. package/utils/object/index.d.ts +3 -4
  153. package/utils/object/index.js +8 -4
  154. package/utils/object/isObject.cjs +32 -0
  155. package/utils/object/isObject.js +6 -3
  156. package/utils/scrollTo.cjs +86 -0
  157. package/utils/scrollTo.js +20 -40
  158. package/utils/string/index.cjs +53 -0
  159. package/utils/string/index.d.ts +10 -11
  160. package/utils/string/index.js +22 -11
  161. package/utils/string/withLeadingCharacters.cjs +38 -0
  162. package/utils/string/withLeadingCharacters.js +7 -4
  163. package/utils/string/withLeadingSlash.cjs +38 -0
  164. package/utils/string/withLeadingSlash.js +7 -4
  165. package/utils/string/withTrailingCharacters.cjs +38 -0
  166. package/utils/string/withTrailingCharacters.js +7 -4
  167. package/utils/string/withTrailingSlash.cjs +38 -0
  168. package/utils/string/withTrailingSlash.js +7 -4
  169. package/utils/string/withoutLeadingCharacters.cjs +32 -0
  170. package/utils/string/withoutLeadingCharacters.js +6 -3
  171. package/utils/string/withoutLeadingCharactersRecursive.cjs +43 -0
  172. package/utils/string/withoutLeadingCharactersRecursive.js +8 -7
  173. package/utils/string/withoutLeadingSlash.cjs +38 -0
  174. package/utils/string/withoutLeadingSlash.js +7 -4
  175. package/utils/string/withoutTrailingCharacters.cjs +32 -0
  176. package/utils/string/withoutTrailingCharacters.js +6 -3
  177. package/utils/string/withoutTrailingCharactersRecursive.cjs +43 -0
  178. package/utils/string/withoutTrailingCharactersRecursive.js +8 -7
  179. package/utils/string/withoutTrailingSlash.cjs +38 -0
  180. package/utils/string/withoutTrailingSlash.js +7 -4
  181. package/utils/throttle.cjs +40 -0
  182. package/utils/throttle.js +9 -9
  183. package/index.modern.js +0 -1
  184. package/index.umd.js +0 -1
package/index.d.ts CHANGED
@@ -1,21 +1,4 @@
1
- import Base from "./Base/index.js";
2
- import { createApp } from "./helpers/index.js";
3
- import { importOnInteraction } from "./helpers/index.js";
4
- import { importWhenIdle } from "./helpers/index.js";
5
- import { importWhenVisible } from "./helpers/index.js";
6
- import { useDrag } from "./services/index.js";
7
- import { useKey } from "./services/index.js";
8
- import { useLoad } from "./services/index.js";
9
- import { usePointer } from "./services/index.js";
10
- import { useRaf } from "./services/index.js";
11
- import { useResize } from "./services/index.js";
12
- import { useScroll } from "./services/index.js";
13
- import { withBreakpointManager } from "./decorators/index.js";
14
- import { withBreakpointObserver } from "./decorators/index.js";
15
- import { withDrag } from "./decorators/index.js";
16
- import { withExtraConfig } from "./decorators/index.js";
17
- import { withIntersectionObserver } from "./decorators/index.js";
18
- import { withMountWhenInView } from "./decorators/index.js";
19
- import { withScrolledInView } from "./decorators/index.js";
20
- import { withVue2 } from "./decorators/index.js";
21
- export { Base, createApp, importOnInteraction, importWhenIdle, importWhenVisible, useDrag, useKey, useLoad, usePointer, useRaf, useResize, useScroll, withBreakpointManager, withBreakpointObserver, withDrag, withExtraConfig, withIntersectionObserver, withMountWhenInView, withScrolledInView, withVue2 };
1
+ export { default as Base } from "./Base/index.js";
2
+ export * from "./decorators/index.js";
3
+ export * from "./helpers/index.js";
4
+ export * from "./services/index.js";
package/index.js CHANGED
@@ -1,5 +1,7 @@
1
- import Base from './Base/index.js';
2
- import { withDrag, withBreakpointManager, withBreakpointObserver, withExtraConfig, withIntersectionObserver, withMountWhenInView, withScrolledInView, withVue2 } from './decorators/index.js';
3
- import { createApp, importOnInteraction, importWhenIdle, importWhenVisible } from './helpers/index.js';
4
- import { useDrag, useKey, useLoad, usePointer, useRaf, useResize, useScroll } from './services/index.js';
5
- export { Base, createApp, importOnInteraction, importWhenIdle, importWhenVisible, useDrag, useKey, useLoad, usePointer, useRaf, useResize, useScroll, withBreakpointManager, withBreakpointObserver, withDrag, withExtraConfig, withIntersectionObserver, withMountWhenInView, withScrolledInView, withVue2 };
1
+ import { default as default2 } from "./Base/index.js";
2
+ export * from "./decorators/index.js";
3
+ export * from "./helpers/index.js";
4
+ export * from "./services/index.js";
5
+ export {
6
+ default2 as Base
7
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@studiometa/js-toolkit",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.0.0-beta.13",
4
4
  "description": "A set of useful little bits of JavaScript to boost your project! 🚀",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,16 +17,27 @@
17
17
  "homepage": "https://github.com/studiometa/js-toolkit#readme",
18
18
  "type": "module",
19
19
  "sideEffects": false,
20
- "unpkg": "./index.umd.js",
21
20
  "main": "./index.js",
22
21
  "module": "./index.js",
22
+ "types": "./index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "import": "./index.js",
26
+ "require": "./index.cjs",
27
+ "types": "./index.d.ts"
28
+ },
29
+ "./utils": {
30
+ "import": "./utils/index.js",
31
+ "require": "./utils/index.cjs",
32
+ "types": "./utils/index.d.ts"
33
+ }
34
+ },
23
35
  "keywords": [
24
36
  "js-toolkit",
25
37
  "component",
26
38
  "studiometa",
27
39
  "toolkit"
28
40
  ],
29
- "types": "index.d.ts",
30
41
  "dependencies": {
31
42
  "@babel/runtime": "^7.15.4",
32
43
  "deepmerge": "^4.2.2"
@@ -0,0 +1,75 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __reExport = (target, module2, copyDefault, desc) => {
11
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
12
+ for (let key of __getOwnPropNames(module2))
13
+ if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
14
+ __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
15
+ }
16
+ return target;
17
+ };
18
+ var __toCommonJS = /* @__PURE__ */ ((cache) => {
19
+ return (module2, temp) => {
20
+ return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
21
+ };
22
+ })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
23
+
24
+ // packages/js-toolkit/services/Service.js
25
+ var Service_exports = {};
26
+ __export(Service_exports, {
27
+ default: () => Service
28
+ });
29
+ var Service = class {
30
+ constructor() {
31
+ this.callbacks = /* @__PURE__ */ new Map();
32
+ this.isInit = false;
33
+ }
34
+ props;
35
+ updateProps(...args) {
36
+ this.props = args;
37
+ throw new Error("The `updateProps` method must be implemented.");
38
+ }
39
+ init() {
40
+ throw new Error("The `init` method must be implemented.");
41
+ }
42
+ kill() {
43
+ throw new Error("The `kill` method must be implemented.");
44
+ }
45
+ add(key, callback) {
46
+ if (this.has(key)) {
47
+ throw new Error(`A callback with the key \`${key}\` has already been registered.`);
48
+ }
49
+ if (this.callbacks.size === 0 && !this.isInit) {
50
+ this.init();
51
+ this.isInit = true;
52
+ }
53
+ this.callbacks.set(key, callback);
54
+ }
55
+ has(key) {
56
+ return this.callbacks.has(key);
57
+ }
58
+ get(key) {
59
+ return this.callbacks.get(key);
60
+ }
61
+ remove(key) {
62
+ this.callbacks.delete(key);
63
+ if (this.callbacks.size === 0 && this.isInit) {
64
+ this.kill();
65
+ this.isInit = false;
66
+ }
67
+ }
68
+ trigger(...args) {
69
+ this.callbacks.forEach(function forEachCallback(callback) {
70
+ callback(...args);
71
+ });
72
+ return this;
73
+ }
74
+ };
75
+ module.exports = __toCommonJS(Service_exports);
@@ -5,33 +5,39 @@ export default class Service {
5
5
  callbacks: Map<any, any>;
6
6
  isInit: boolean;
7
7
  /**
8
- * Getter to get the services properties.
9
- * This getter MUST be implementer by the service extending this class.
10
- * @return {Object}
8
+ * @type {unknown}
11
9
  */
12
- get props(): any;
10
+ props: unknown;
11
+ /**
12
+ * Method to update the services properties.
13
+ * This method MUST be implemented by the service extending this class.
14
+ *
15
+ * @param {...any} args
16
+ * @return {this['props']}
17
+ */
18
+ updateProps(...args: any[]): unknown;
13
19
  /**
14
20
  * Method to initialize the service behaviors.
15
21
  * This method MUST be implemented by the service extending this class.
16
22
  *
17
- * @return {Service} The current instance
23
+ * @return {void}
18
24
  */
19
- init(): Service;
25
+ init(): void;
20
26
  /**
21
27
  * Method to kill the service behaviors.
22
28
  * This method MUST be implemented by the service extending this class.
23
29
  *
24
- * @return {Service} The current instance
30
+ * @return {void}
25
31
  */
26
- kill(): Service;
32
+ kill(): void;
27
33
  /**
28
34
  * Add a callback.
29
35
  *
30
36
  * @param {String} key The callback's identifier
31
37
  * @param {Function} callback The callback function
32
- * @return {this} The current instance
38
+ * @return {void}
33
39
  */
34
- add(key: string, callback: Function): this;
40
+ add(key: string, callback: Function): void;
35
41
  /**
36
42
  * Test if a callback with the given key has already been added.
37
43
  *
@@ -42,22 +48,21 @@ export default class Service {
42
48
  /**
43
49
  * Get the callback tied to the given key.
44
50
  *
45
- * @param {String} key The identifier to get
51
+ * @param {string} key The identifier to get
46
52
  * @return {Function} The callback function
47
53
  */
48
54
  get(key: string): Function;
49
55
  /**
50
56
  * Remove the callback tied to the given key.
51
57
  *
52
- * @param {String} key The identifier to remove
53
- * @return {Service} The current instance
58
+ * @param {string} key The identifier to remove
54
59
  */
55
- remove(key: string): Service;
60
+ remove(key: string): void;
56
61
  /**
57
62
  * Trigger each added callback with the given arguments.
58
63
  *
59
- * @param {Array} args All the arguments to apply to the callback
60
- * @return {Service} The current instance
64
+ * @param {...any} args All the arguments to apply to the callback
65
+ * @return {this} The current instance
61
66
  */
62
- trigger(...args: any[]): Service;
67
+ trigger(...args: any[]): this;
63
68
  }
@@ -1,63 +1,49 @@
1
- export default class Service {
1
+ class Service {
2
2
  constructor() {
3
- this.callbacks = new Map();
3
+ this.callbacks = /* @__PURE__ */ new Map();
4
4
  this.isInit = false;
5
5
  }
6
-
7
- get props() {
8
- throw new Error('The `props` getter must be implemented.');
6
+ props;
7
+ updateProps(...args) {
8
+ this.props = args;
9
+ throw new Error("The `updateProps` method must be implemented.");
9
10
  }
10
-
11
11
  init() {
12
- throw new Error('The `init` method must be implemented.');
12
+ throw new Error("The `init` method must be implemented.");
13
13
  }
14
-
15
14
  kill() {
16
- throw new Error('The `kill` method must be implemented.');
15
+ throw new Error("The `kill` method must be implemented.");
17
16
  }
18
-
19
17
  add(key, callback) {
20
18
  if (this.has(key)) {
21
- throw new Error("A callback with the key `".concat(key, "` has already been registered."));
19
+ throw new Error(`A callback with the key \`${key}\` has already been registered.`);
22
20
  }
23
-
24
21
  if (this.callbacks.size === 0 && !this.isInit) {
25
22
  this.init();
26
23
  this.isInit = true;
27
24
  }
28
-
29
25
  this.callbacks.set(key, callback);
30
- return this;
31
26
  }
32
-
33
27
  has(key) {
34
28
  return this.callbacks.has(key);
35
29
  }
36
-
37
30
  get(key) {
38
31
  return this.callbacks.get(key);
39
32
  }
40
-
41
33
  remove(key) {
42
34
  this.callbacks.delete(key);
43
-
44
35
  if (this.callbacks.size === 0 && this.isInit) {
45
36
  this.kill();
46
37
  this.isInit = false;
47
38
  }
48
-
49
- return this;
50
39
  }
51
-
52
- trigger() {
53
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
54
- args[_key] = arguments[_key];
55
- }
56
-
40
+ trigger(...args) {
57
41
  this.callbacks.forEach(function forEachCallback(callback) {
58
42
  callback(...args);
59
43
  });
60
44
  return this;
61
45
  }
62
-
63
- }
46
+ }
47
+ export {
48
+ Service as default
49
+ };
@@ -0,0 +1,194 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __reExport = (target, module2, copyDefault, desc) => {
13
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
14
+ for (let key of __getOwnPropNames(module2))
15
+ if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
16
+ __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
17
+ }
18
+ return target;
19
+ };
20
+ var __toESM = (module2, isNodeMode) => {
21
+ return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", !isNodeMode && module2 && module2.__esModule ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
22
+ };
23
+ var __toCommonJS = /* @__PURE__ */ ((cache) => {
24
+ return (module2, temp) => {
25
+ return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
26
+ };
27
+ })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
28
+
29
+ // packages/js-toolkit/services/drag.js
30
+ var drag_exports = {};
31
+ __export(drag_exports, {
32
+ default: () => useDrag
33
+ });
34
+ var import_Service = __toESM(require("./Service.cjs"), 1);
35
+ var import_pointer = __toESM(require("./pointer.cjs"), 1);
36
+ var import_raf = __toESM(require("./raf.cjs"), 1);
37
+ var import_inertiaFinalValue = __toESM(require("../utils/math/inertiaFinalValue.cjs"), 1);
38
+ var MODES = {
39
+ START: "start",
40
+ DRAG: "drag",
41
+ DROP: "drop",
42
+ INERTIA: "inertia",
43
+ STOP: "stop"
44
+ };
45
+ var instanceIds = 0;
46
+ var Drag = class extends import_Service.default {
47
+ id;
48
+ props = {
49
+ target: null,
50
+ mode: void 0,
51
+ isGrabbing: false,
52
+ hasInertia: false,
53
+ x: 0,
54
+ y: 0,
55
+ delta: {
56
+ x: 0,
57
+ y: 0
58
+ },
59
+ origin: {
60
+ x: 0,
61
+ y: 0
62
+ },
63
+ distance: {
64
+ x: 0,
65
+ y: 0
66
+ },
67
+ final: {
68
+ x: 0,
69
+ y: 0
70
+ }
71
+ };
72
+ options = {
73
+ dampFactor: 0.85
74
+ };
75
+ constructor(target, options = {}) {
76
+ super();
77
+ instanceIds += 1;
78
+ this.id = `drag-${instanceIds}`;
79
+ this.props.target = target;
80
+ this.options = { ...this.options, ...options };
81
+ this.pointerHandler = this.pointerHandler.bind(this);
82
+ this.rafHandler = this.rafHandler.bind(this);
83
+ return this;
84
+ }
85
+ init() {
86
+ const options = { passive: true };
87
+ this.props.target.addEventListener("mousedown", this, options);
88
+ this.props.target.addEventListener("touchstart", this, options);
89
+ const pointer = (0, import_pointer.default)();
90
+ pointer.add(this.id, this.pointerHandler);
91
+ return this;
92
+ }
93
+ kill() {
94
+ this.props.target.removeEventListener("mousemove", this);
95
+ this.props.target.removeEventListener("touchmove", this);
96
+ const pointer = (0, import_pointer.default)();
97
+ pointer.remove(this.id);
98
+ return this;
99
+ }
100
+ handleEvent(event) {
101
+ const x = event instanceof MouseEvent ? event.clientX : event.touches[0].clientX;
102
+ const y = event instanceof MouseEvent ? event.clientY : event.touches[0].clientY;
103
+ this.start(x, y);
104
+ }
105
+ start(x, y) {
106
+ this.props.x = x;
107
+ this.props.y = y;
108
+ this.props.origin.x = x;
109
+ this.props.origin.y = y;
110
+ this.props.delta.x = 0;
111
+ this.props.delta.y = 0;
112
+ this.props.distance.x = 0;
113
+ this.props.distance.y = 0;
114
+ this.props.final.x = x;
115
+ this.props.final.y = y;
116
+ this.props.mode = MODES.START;
117
+ this.props.isGrabbing = true;
118
+ this.trigger(this.props);
119
+ }
120
+ drop() {
121
+ this.props.isGrabbing = false;
122
+ this.props.mode = MODES.DROP;
123
+ this.props.hasInertia = true;
124
+ this.props.final.x = (0, import_inertiaFinalValue.default)(this.props.x, this.props.delta.x, this.options.dampFactor);
125
+ this.props.final.y = (0, import_inertiaFinalValue.default)(this.props.y, this.props.delta.y, this.options.dampFactor);
126
+ this.trigger(this.props);
127
+ setTimeout(() => {
128
+ const raf = (0, import_raf.default)();
129
+ raf.remove(this.id);
130
+ raf.add(this.id, this.rafHandler);
131
+ }, 0);
132
+ }
133
+ stop() {
134
+ (0, import_raf.default)().remove(this.id);
135
+ this.props.hasInertia = false;
136
+ this.props.mode = MODES.STOP;
137
+ this.trigger(this.props);
138
+ }
139
+ rafHandler() {
140
+ if (!this.props.isGrabbing) {
141
+ this.props.x += this.props.delta.x;
142
+ this.props.y += this.props.delta.y;
143
+ this.props.distance.x = this.props.x - this.props.origin.x;
144
+ this.props.distance.y = this.props.y - this.props.origin.y;
145
+ this.props.delta.x *= this.options.dampFactor;
146
+ this.props.delta.y *= this.options.dampFactor;
147
+ if (this.props.mode !== MODES.INERTIA) {
148
+ this.props.mode = MODES.INERTIA;
149
+ }
150
+ this.trigger(this.props);
151
+ if (Math.abs(this.props.delta.x) < 0.1 && Math.abs(this.props.delta.y) < 0.1) {
152
+ this.stop();
153
+ }
154
+ }
155
+ }
156
+ pointerHandler(props) {
157
+ if (this.props.isGrabbing) {
158
+ this.props.x = props.x;
159
+ this.props.y = props.y;
160
+ this.props.delta.x = props.delta.x;
161
+ this.props.delta.y = props.delta.y;
162
+ this.props.final.x = props.x;
163
+ this.props.final.y = props.y;
164
+ this.props.distance.x = this.props.x - this.props.origin.x;
165
+ this.props.distance.y = this.props.y - this.props.origin.y;
166
+ if (this.props.mode !== MODES.DRAG) {
167
+ this.props.mode = MODES.DRAG;
168
+ }
169
+ this.trigger(this.props);
170
+ if (!props.isDown) {
171
+ this.drop();
172
+ }
173
+ }
174
+ }
175
+ updateProps() {
176
+ return this.props;
177
+ }
178
+ };
179
+ var instances = /* @__PURE__ */ new WeakMap();
180
+ function useDrag(target, options) {
181
+ let drag = instances.get(target);
182
+ if (!drag) {
183
+ const instance = new Drag(target, options);
184
+ drag = {
185
+ add: instance.add.bind(instance),
186
+ remove: instance.remove.bind(instance),
187
+ has: instance.has.bind(instance),
188
+ props: instance.updateProps.bind(instance)
189
+ };
190
+ instances.set(target, drag);
191
+ }
192
+ return drag;
193
+ }
194
+ module.exports = __toCommonJS(drag_exports);
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Use the pointer.
2
+ * Use the drag service.
3
3
  *
4
4
  * ```js
5
5
  * import { useDrag } from '@studiometa/js-toolkit/services';
@@ -12,10 +12,10 @@
12
12
  *
13
13
  * @param {HTMLElement} target
14
14
  * @param {DragServiceOptions} options
15
- * @return {ServiceInterface & DragService}
15
+ * @return {DragService}
16
16
  */
17
- export default function useDrag(target: HTMLElement, options: DragServiceOptions): ServiceInterface & DragService;
18
- export type ServiceInterface = import('./index').ServiceInterface;
17
+ export default function useDrag(target: HTMLElement, options: DragServiceOptions): DragService;
18
+ export type DragService = import('./index').ServiceInterface<DragServiceProps>;
19
19
  export type PointerServiceProps = import('./pointer.js').PointerServiceProps;
20
20
  export type DragLifecycle = 'start' | 'drag' | 'drop' | 'inertia' | 'stop';
21
21
  export type DragServiceProps = {
@@ -75,17 +75,3 @@ export type DragServiceProps = {
75
75
  export type DragServiceOptions = {
76
76
  dampFactor?: number | undefined;
77
77
  };
78
- export type DragService = {
79
- /**
80
- * Add a callback function to the service. The key must be the HTML element targeted by the drag.
81
- */
82
- add: (target: HTMLElement, callback: (props: DragServiceProps) => void, options?: DragServiceOptions) => void;
83
- /**
84
- * Remove a callback function to the service.
85
- */
86
- remove: (target: HTMLElement) => void;
87
- /**
88
- * Get the current values of the resize service props.
89
- */
90
- props: () => DragServiceProps;
91
- };