@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/services/drag.js CHANGED
@@ -1,216 +1,163 @@
1
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
-
3
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
4
-
5
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
6
-
7
- import Service from './Service.js';
8
- import usePointer from './pointer.js';
9
- import useRaf from './raf.js';
10
- import inertiaFinalValue from '../utils/math/inertiaFinalValue.js';
11
- var MODES = {
12
- START: 'start',
13
- DRAG: 'drag',
14
- DROP: 'drop',
15
- INERTIA: 'inertia',
16
- STOP: 'stop'
1
+ import Service from "./Service.js";
2
+ import usePointer from "./pointer.js";
3
+ import useRaf from "./raf.js";
4
+ import inertiaFinalValue from "../utils/math/inertiaFinalValue.js";
5
+ const MODES = {
6
+ START: "start",
7
+ DRAG: "drag",
8
+ DROP: "drop",
9
+ INERTIA: "inertia",
10
+ STOP: "stop"
17
11
  };
18
- var instanceIds = 0;
19
-
12
+ let instanceIds = 0;
20
13
  class Drag extends Service {
21
- constructor(target) {
22
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
23
- super();
24
-
25
- _defineProperty(this, "id", void 0);
26
-
27
- _defineProperty(this, "target", void 0);
28
-
29
- _defineProperty(this, "mode", void 0);
30
-
31
- _defineProperty(this, "isGrabbing", false);
32
-
33
- _defineProperty(this, "hasInertia", false);
34
-
35
- _defineProperty(this, "x", 0);
36
-
37
- _defineProperty(this, "y", 0);
38
-
39
- _defineProperty(this, "delta", {
14
+ id;
15
+ props = {
16
+ target: null,
17
+ mode: void 0,
18
+ isGrabbing: false,
19
+ hasInertia: false,
20
+ x: 0,
21
+ y: 0,
22
+ delta: {
40
23
  x: 0,
41
24
  y: 0
42
- });
43
-
44
- _defineProperty(this, "origin", {
25
+ },
26
+ origin: {
45
27
  x: 0,
46
28
  y: 0
47
- });
48
-
49
- _defineProperty(this, "distance", {
29
+ },
30
+ distance: {
50
31
  x: 0,
51
32
  y: 0
52
- });
53
-
54
- _defineProperty(this, "final", {
33
+ },
34
+ final: {
55
35
  x: 0,
56
36
  y: 0
57
- });
58
-
59
- _defineProperty(this, "options", {
60
- dampFactor: 0.85
61
- });
62
-
37
+ }
38
+ };
39
+ options = {
40
+ dampFactor: 0.85
41
+ };
42
+ constructor(target, options = {}) {
43
+ super();
63
44
  instanceIds += 1;
64
- this.id = "drag-".concat(instanceIds);
65
- this.target = target;
66
- this.options = _objectSpread(_objectSpread({}, this.options), options);
45
+ this.id = `drag-${instanceIds}`;
46
+ this.props.target = target;
47
+ this.options = { ...this.options, ...options };
67
48
  this.pointerHandler = this.pointerHandler.bind(this);
68
49
  this.rafHandler = this.rafHandler.bind(this);
69
50
  return this;
70
51
  }
71
-
72
52
  init() {
73
- var options = {
74
- passive: true
75
- };
76
- this.target.addEventListener('mousedown', this, options);
77
- this.target.addEventListener('touchstart', this, options);
78
- var pointer = usePointer();
53
+ const options = { passive: true };
54
+ this.props.target.addEventListener("mousedown", this, options);
55
+ this.props.target.addEventListener("touchstart", this, options);
56
+ const pointer = usePointer();
79
57
  pointer.add(this.id, this.pointerHandler);
80
58
  return this;
81
59
  }
82
-
83
60
  kill() {
84
- this.target.removeEventListener('mousemove', this);
85
- this.target.removeEventListener('touchmove', this);
86
- var pointer = usePointer();
61
+ this.props.target.removeEventListener("mousemove", this);
62
+ this.props.target.removeEventListener("touchmove", this);
63
+ const pointer = usePointer();
87
64
  pointer.remove(this.id);
88
65
  return this;
89
66
  }
90
-
91
67
  handleEvent(event) {
92
- var x = event instanceof MouseEvent ? event.clientX : event.touches[0].clientX;
93
- var y = event instanceof MouseEvent ? event.clientY : event.touches[0].clientY;
68
+ const x = event instanceof MouseEvent ? event.clientX : event.touches[0].clientX;
69
+ const y = event instanceof MouseEvent ? event.clientY : event.touches[0].clientY;
94
70
  this.start(x, y);
95
71
  }
96
-
97
72
  start(x, y) {
98
- this.x = x;
99
- this.y = y;
100
- this.origin.x = x;
101
- this.origin.y = y;
102
- this.delta.x = 0;
103
- this.delta.y = 0;
104
- this.distance.x = 0;
105
- this.distance.y = 0;
106
- this.final.x = x;
107
- this.final.y = y;
108
- this.mode = MODES.START;
109
- this.isGrabbing = true;
73
+ this.props.x = x;
74
+ this.props.y = y;
75
+ this.props.origin.x = x;
76
+ this.props.origin.y = y;
77
+ this.props.delta.x = 0;
78
+ this.props.delta.y = 0;
79
+ this.props.distance.x = 0;
80
+ this.props.distance.y = 0;
81
+ this.props.final.x = x;
82
+ this.props.final.y = y;
83
+ this.props.mode = MODES.START;
84
+ this.props.isGrabbing = true;
110
85
  this.trigger(this.props);
111
86
  }
112
-
113
87
  drop() {
114
- this.isGrabbing = false;
115
- this.mode = MODES.DROP;
116
- this.hasInertia = true;
117
- this.final.x = inertiaFinalValue(this.x, this.delta.x, this.options.dampFactor);
118
- this.final.y = inertiaFinalValue(this.y, this.delta.y, this.options.dampFactor);
88
+ this.props.isGrabbing = false;
89
+ this.props.mode = MODES.DROP;
90
+ this.props.hasInertia = true;
91
+ this.props.final.x = inertiaFinalValue(this.props.x, this.props.delta.x, this.options.dampFactor);
92
+ this.props.final.y = inertiaFinalValue(this.props.y, this.props.delta.y, this.options.dampFactor);
119
93
  this.trigger(this.props);
120
94
  setTimeout(() => {
121
- var raf = useRaf();
95
+ const raf = useRaf();
122
96
  raf.remove(this.id);
123
97
  raf.add(this.id, this.rafHandler);
124
98
  }, 0);
125
99
  }
126
-
127
100
  stop() {
128
101
  useRaf().remove(this.id);
129
- this.hasInertia = false;
130
- this.mode = MODES.STOP;
102
+ this.props.hasInertia = false;
103
+ this.props.mode = MODES.STOP;
131
104
  this.trigger(this.props);
132
105
  }
133
-
134
106
  rafHandler() {
135
- if (!this.isGrabbing) {
136
- this.x += this.delta.x;
137
- this.y += this.delta.y;
138
- this.distance.x = this.x - this.origin.x;
139
- this.distance.y = this.y - this.origin.y;
140
- this.delta.x *= this.options.dampFactor;
141
- this.delta.y *= this.options.dampFactor;
142
-
143
- if (this.mode !== MODES.INERTIA) {
144
- this.mode = MODES.INERTIA;
107
+ if (!this.props.isGrabbing) {
108
+ this.props.x += this.props.delta.x;
109
+ this.props.y += this.props.delta.y;
110
+ this.props.distance.x = this.props.x - this.props.origin.x;
111
+ this.props.distance.y = this.props.y - this.props.origin.y;
112
+ this.props.delta.x *= this.options.dampFactor;
113
+ this.props.delta.y *= this.options.dampFactor;
114
+ if (this.props.mode !== MODES.INERTIA) {
115
+ this.props.mode = MODES.INERTIA;
145
116
  }
146
-
147
117
  this.trigger(this.props);
148
-
149
- if (Math.abs(this.delta.x) < 0.1 && Math.abs(this.delta.y) < 0.1) {
118
+ if (Math.abs(this.props.delta.x) < 0.1 && Math.abs(this.props.delta.y) < 0.1) {
150
119
  this.stop();
151
120
  }
152
121
  }
153
122
  }
154
-
155
123
  pointerHandler(props) {
156
- if (this.isGrabbing) {
157
- this.x = props.x;
158
- this.y = props.y;
159
- this.delta.x = props.delta.x;
160
- this.delta.y = props.delta.y;
161
- this.final.x = props.x;
162
- this.final.y = props.y;
163
- this.distance.x = this.x - this.origin.x;
164
- this.distance.y = this.y - this.origin.y;
165
-
166
- if (this.mode !== MODES.DRAG) {
167
- this.mode = MODES.DRAG;
124
+ if (this.props.isGrabbing) {
125
+ this.props.x = props.x;
126
+ this.props.y = props.y;
127
+ this.props.delta.x = props.delta.x;
128
+ this.props.delta.y = props.delta.y;
129
+ this.props.final.x = props.x;
130
+ this.props.final.y = props.y;
131
+ this.props.distance.x = this.props.x - this.props.origin.x;
132
+ this.props.distance.y = this.props.y - this.props.origin.y;
133
+ if (this.props.mode !== MODES.DRAG) {
134
+ this.props.mode = MODES.DRAG;
168
135
  }
169
-
170
136
  this.trigger(this.props);
171
-
172
137
  if (!props.isDown) {
173
138
  this.drop();
174
139
  }
175
140
  }
176
141
  }
177
-
178
- get props() {
179
- return {
180
- target: this.target,
181
- mode: this.mode,
182
- isGrabbing: this.isGrabbing,
183
- hasInertia: this.hasInertia,
184
- x: this.x,
185
- y: this.y,
186
- delta: this.delta,
187
- origin: this.origin,
188
- distance: this.distance,
189
- final: this.final
190
- };
142
+ updateProps() {
143
+ return this.props;
191
144
  }
192
-
193
145
  }
194
-
195
- var instances = new WeakMap();
196
- export default function useDrag(target, options) {
197
- var drag = instances.get(target);
198
-
146
+ const instances = /* @__PURE__ */ new WeakMap();
147
+ function useDrag(target, options) {
148
+ let drag = instances.get(target);
199
149
  if (!drag) {
200
- drag = new Drag(target, options);
150
+ const instance = new Drag(target, options);
151
+ drag = {
152
+ add: instance.add.bind(instance),
153
+ remove: instance.remove.bind(instance),
154
+ has: instance.has.bind(instance),
155
+ props: instance.updateProps.bind(instance)
156
+ };
201
157
  instances.set(target, drag);
202
158
  }
203
-
204
- var add = drag.add.bind(drag);
205
- var remove = drag.remove.bind(drag);
206
- var has = drag.has.bind(drag);
207
-
208
- var props = () => drag.props;
209
-
210
- return {
211
- add,
212
- remove,
213
- has,
214
- props
215
- };
216
- }
159
+ return drag;
160
+ }
161
+ export {
162
+ useDrag as default
163
+ };
@@ -0,0 +1,47 @@
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/index.js
30
+ var services_exports = {};
31
+ __export(services_exports, {
32
+ useDrag: () => import_drag.default,
33
+ useKey: () => import_key.default,
34
+ useLoad: () => import_load.default,
35
+ usePointer: () => import_pointer.default,
36
+ useRaf: () => import_raf.default,
37
+ useResize: () => import_resize.default,
38
+ useScroll: () => import_scroll.default
39
+ });
40
+ var import_drag = __toESM(require("./drag.cjs"), 1);
41
+ var import_key = __toESM(require("./key.cjs"), 1);
42
+ var import_load = __toESM(require("./load.cjs"), 1);
43
+ var import_pointer = __toESM(require("./pointer.cjs"), 1);
44
+ var import_raf = __toESM(require("./raf.cjs"), 1);
45
+ var import_resize = __toESM(require("./resize.cjs"), 1);
46
+ var import_scroll = __toESM(require("./scroll.cjs"), 1);
47
+ module.exports = __toCommonJS(services_exports);
@@ -1,4 +1,11 @@
1
- export type ServiceInterface = {
1
+ export { default as useDrag } from "./drag.js";
2
+ export { default as useKey } from "./key.js";
3
+ export { default as useLoad } from "./load.js";
4
+ export { default as usePointer } from "./pointer.js";
5
+ export { default as useRaf } from "./raf.js";
6
+ export { default as useResize } from "./resize.js";
7
+ export { default as useScroll } from "./scroll.js";
8
+ export type ServiceInterface<T> = {
2
9
  /**
3
10
  * Remove a function from the resize service by its key.
4
11
  */
@@ -6,17 +13,13 @@ export type ServiceInterface = {
6
13
  /**
7
14
  * Add a callback to the service. The callback will receive the current service props as parameter.
8
15
  */
9
- add: (key: string, cb: (props: any) => void) => boolean;
16
+ add: (key: string, cb: (props: T) => void) => void;
10
17
  /**
11
18
  * Test if the service has alreaydy a callback for the given key.
12
19
  */
13
20
  has: (key: string) => boolean;
21
+ /**
22
+ * Get the service's props.
23
+ */
24
+ props: () => T;
14
25
  };
15
- import useKey from "./key.js";
16
- import usePointer from "./pointer.js";
17
- import useDrag from "./drag.js";
18
- import useLoad from "./load.js";
19
- import useRaf from "./raf.js";
20
- import useResize from "./resize.js";
21
- import useScroll from "./scroll.js";
22
- export { useKey, usePointer, useDrag, useLoad, useRaf, useResize, useScroll };
package/services/index.js CHANGED
@@ -1,8 +1,16 @@
1
- import useDrag from './drag.js';
2
- import useKey from './key.js';
3
- import useLoad from './load.js';
4
- import usePointer from './pointer.js';
5
- import useRaf from './raf.js';
6
- import useResize from './resize.js';
7
- import useScroll from './scroll.js';
8
- export { useKey, usePointer, useDrag, useLoad, useRaf, useResize, useScroll };
1
+ import { default as default2 } from "./drag.js";
2
+ import { default as default3 } from "./key.js";
3
+ import { default as default4 } from "./load.js";
4
+ import { default as default5 } from "./pointer.js";
5
+ import { default as default6 } from "./raf.js";
6
+ import { default as default7 } from "./resize.js";
7
+ import { default as default8 } from "./scroll.js";
8
+ export {
9
+ default2 as useDrag,
10
+ default3 as useKey,
11
+ default4 as useLoad,
12
+ default5 as usePointer,
13
+ default6 as useRaf,
14
+ default7 as useResize,
15
+ default8 as useScroll
16
+ };
@@ -0,0 +1,101 @@
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 key2 of __getOwnPropNames(module2))
15
+ if (!__hasOwnProp.call(target, key2) && (copyDefault || key2 !== "default"))
16
+ __defProp(target, key2, { get: () => module2[key2], enumerable: !(desc = __getOwnPropDesc(module2, key2)) || 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/key.js
30
+ var key_exports = {};
31
+ __export(key_exports, {
32
+ default: () => useKey
33
+ });
34
+ var import_Service = __toESM(require("./Service.cjs"), 1);
35
+ var import_keyCodes = __toESM(require("../utils/keyCodes.cjs"), 1);
36
+ var Key = class extends import_Service.default {
37
+ previousEvent;
38
+ props = {
39
+ event: null,
40
+ triggered: 0,
41
+ isUp: false,
42
+ isDown: false,
43
+ ENTER: false,
44
+ SPACE: false,
45
+ TAB: false,
46
+ ESC: false,
47
+ LEFT: false,
48
+ UP: false,
49
+ RIGHT: false,
50
+ DOWN: false,
51
+ direction: "none"
52
+ };
53
+ init() {
54
+ document.addEventListener("keydown", this);
55
+ document.addEventListener("keyup", this);
56
+ }
57
+ handleEvent(event) {
58
+ this.updateProps(event);
59
+ this.trigger(this.props);
60
+ }
61
+ kill() {
62
+ document.removeEventListener("keydown", this);
63
+ document.removeEventListener("keyup", this);
64
+ }
65
+ updateProps(event) {
66
+ this.props.event = event;
67
+ Object.entries(import_keyCodes.default).forEach(([name, code]) => {
68
+ this.props[name] = code === event.keyCode;
69
+ });
70
+ if (!this.previousEvent) {
71
+ this.props.triggered = 0;
72
+ }
73
+ if (this.props.event.type === "keydown" && this.previousEvent?.type === "keydown") {
74
+ this.props.triggered += 1;
75
+ } else {
76
+ this.props.triggered = 1;
77
+ }
78
+ this.previousEvent = this.props.event;
79
+ this.props.direction = this.props.event.type === "keydown" ? "down" : "up";
80
+ this.props.isUp = this.props.event.type === "keyup";
81
+ this.props.isDown = this.props.event.type === "keydown";
82
+ return this.props;
83
+ }
84
+ };
85
+ var instance;
86
+ var key;
87
+ function useKey() {
88
+ if (!key) {
89
+ if (!instance) {
90
+ instance = new Key();
91
+ }
92
+ key = {
93
+ add: instance.add.bind(instance),
94
+ remove: instance.remove.bind(instance),
95
+ has: instance.has.bind(instance),
96
+ props: instance.updateProps.bind(instance)
97
+ };
98
+ }
99
+ return key;
100
+ }
101
+ module.exports = __toCommonJS(key_exports);
package/services/key.d.ts CHANGED
@@ -9,13 +9,14 @@
9
9
  * props();
10
10
  * ```
11
11
  *
12
- * @return {ServiceInterface & KeyService}
12
+ * @return {KeyService}
13
13
  */
14
- export default function useKey(): ServiceInterface & KeyService;
15
- export type ServiceInterface = import('./index').ServiceInterface;
14
+ export default function useKey(): KeyService;
15
+ export type KeyService = import('./index').ServiceInterface<KeyServiceProps>;
16
16
  export type KeyServiceProps = {
17
17
  event: KeyboardEvent;
18
18
  triggered: number;
19
+ direction: 'up' | 'down' | 'none';
19
20
  isUp: boolean;
20
21
  isDown: boolean;
21
22
  ENTER: boolean;
@@ -27,13 +28,3 @@ export type KeyServiceProps = {
27
28
  RIGHT: boolean;
28
29
  DOWN: boolean;
29
30
  };
30
- export type KeyService = {
31
- /**
32
- * Add a function to the resize service. The key must be uniq.
33
- */
34
- add: (key: string, callback: (props: KeyServiceProps) => void) => void;
35
- /**
36
- * Get the current values of the resize service props.
37
- */
38
- props: () => KeyServiceProps;
39
- };