@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
@@ -1,158 +1,128 @@
1
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
- import Service from './Service.js';
3
-
1
+ import Service from "./Service.js";
4
2
  function isTouchEvent(event) {
5
- return typeof TouchEvent !== 'undefined' && event instanceof TouchEvent;
3
+ return typeof TouchEvent !== "undefined" && event instanceof TouchEvent;
6
4
  }
7
-
8
5
  class Pointer extends Service {
9
- constructor() {
10
- super(...arguments);
11
-
12
- _defineProperty(this, "isDown", false);
13
-
14
- _defineProperty(this, "y", window.innerHeight / 2);
15
-
16
- _defineProperty(this, "yLast", window.innerHeight / 2);
17
-
18
- _defineProperty(this, "x", window.innerWidth / 2);
19
-
20
- _defineProperty(this, "xLast", window.innerWidth / 2);
21
-
22
- _defineProperty(this, "event", void 0);
23
-
24
- _defineProperty(this, "hasRaf", false);
25
- }
26
-
6
+ props = {
7
+ event: null,
8
+ isDown: false,
9
+ x: window.innerWidth / 2,
10
+ y: window.innerHeight / 2,
11
+ changed: {
12
+ x: false,
13
+ y: false
14
+ },
15
+ last: {
16
+ x: window.innerWidth / 2,
17
+ y: window.innerHeight / 2
18
+ },
19
+ delta: {
20
+ x: 0,
21
+ y: 0
22
+ },
23
+ progress: {
24
+ x: 0.5,
25
+ y: 0.5
26
+ },
27
+ max: {
28
+ x: window.innerWidth,
29
+ y: window.innerHeight
30
+ }
31
+ };
32
+ hasRaf = false;
27
33
  init() {
28
- document.documentElement.addEventListener('mouseenter', this, {
34
+ document.documentElement.addEventListener("mouseenter", this, {
29
35
  once: true,
30
36
  capture: true
31
37
  });
32
- var options = {
33
- passive: true,
34
- capture: true
35
- };
36
- document.addEventListener('mousemove', this, options);
37
- document.addEventListener('touchmove', this, options);
38
- document.addEventListener('mousedown', this, options);
39
- document.addEventListener('touchstart', this, options);
40
- document.addEventListener('mouseup', this, options);
41
- document.addEventListener('touchend', this, options);
38
+ const options = { passive: true, capture: true };
39
+ document.addEventListener("mousemove", this, options);
40
+ document.addEventListener("touchmove", this, options);
41
+ document.addEventListener("mousedown", this, options);
42
+ document.addEventListener("touchstart", this, options);
43
+ document.addEventListener("mouseup", this, options);
44
+ document.addEventListener("touchend", this, options);
42
45
  return this;
43
46
  }
44
-
45
47
  handleEvent(event) {
46
48
  switch (event.type) {
47
- case 'mouseenter':
48
- case 'mousemove':
49
- case 'touchmove':
49
+ case "mouseenter":
50
+ case "mousemove":
51
+ case "touchmove":
50
52
  this.handler(event);
51
53
  break;
52
-
53
- case 'mousedown':
54
- case 'touchstart':
54
+ case "mousedown":
55
+ case "touchstart":
55
56
  this.downHandler();
56
57
  break;
57
-
58
- case 'mouseup':
59
- case 'touchend':
58
+ case "mouseup":
59
+ case "touchend":
60
60
  this.upHandler();
61
61
  break;
62
62
  }
63
63
  }
64
-
65
64
  kill() {
66
- document.removeEventListener('mousemove', this);
67
- document.removeEventListener('touchmove', this);
68
- document.removeEventListener('mousedown', this);
69
- document.removeEventListener('touchstart', this);
70
- document.removeEventListener('mouseup', this);
71
- document.removeEventListener('touchend', this);
65
+ document.removeEventListener("mousemove", this);
66
+ document.removeEventListener("touchmove", this);
67
+ document.removeEventListener("mousedown", this);
68
+ document.removeEventListener("touchstart", this);
69
+ document.removeEventListener("mouseup", this);
70
+ document.removeEventListener("touchend", this);
72
71
  return this;
73
72
  }
74
-
75
73
  handler(event) {
76
- this.updateValues(event);
74
+ this.updateProps(event);
77
75
  this.trigger(this.props);
78
76
  }
79
-
80
77
  downHandler() {
81
- this.isDown = true;
78
+ this.props.isDown = true;
82
79
  this.trigger(this.props);
83
80
  }
84
-
85
81
  upHandler() {
86
- this.isDown = false;
82
+ this.props.isDown = false;
87
83
  this.trigger(this.props);
88
84
  }
89
-
90
- updateValues(event) {
91
- var _event$touches$, _event$touches$2;
92
-
93
- this.event = event;
94
- this.yLast = this.y;
95
- this.xLast = this.x;
96
- var y = isTouchEvent(event) ? (_event$touches$ = event.touches[0]) === null || _event$touches$ === void 0 ? void 0 : _event$touches$.clientY : event.clientY;
97
-
98
- if (y !== this.y) {
99
- this.y = y;
85
+ updateProps(event) {
86
+ this.props.event = event;
87
+ const yLast = this.props.y;
88
+ const xLast = this.props.x;
89
+ const y = isTouchEvent(event) ? event.touches[0]?.clientY : event.clientY;
90
+ if (y !== this.props.y) {
91
+ this.props.y = y;
100
92
  }
101
-
102
- var x = isTouchEvent(event) ? (_event$touches$2 = event.touches[0]) === null || _event$touches$2 === void 0 ? void 0 : _event$touches$2.clientX : event.clientX;
103
-
104
- if (x !== this.x) {
105
- this.x = x;
93
+ const x = isTouchEvent(event) ? event.touches[0]?.clientX : event.clientX;
94
+ if (x !== this.props.x) {
95
+ this.props.x = x;
106
96
  }
97
+ this.props.changed.x = this.props.x !== xLast;
98
+ this.props.changed.y = this.props.y !== yLast;
99
+ this.props.last.x = xLast;
100
+ this.props.last.y = yLast;
101
+ this.props.delta.x = this.props.x - xLast;
102
+ this.props.delta.y = this.props.y - yLast;
103
+ this.props.max.x = window.innerWidth;
104
+ this.props.max.y = window.innerHeight;
105
+ this.props.progress.x = this.props.x / this.props.max.x;
106
+ this.props.progress.y = this.props.y / this.props.max.y;
107
+ return this.props;
107
108
  }
108
-
109
- get props() {
110
- return {
111
- event: this.event,
112
- isDown: this.isDown,
113
- x: this.x,
114
- y: this.y,
115
- changed: {
116
- x: this.x !== this.xLast,
117
- y: this.y !== this.yLast
118
- },
119
- last: {
120
- x: this.xLast,
121
- y: this.yLast
122
- },
123
- delta: {
124
- x: this.x - this.xLast,
125
- y: this.y - this.yLast
126
- },
127
- progress: {
128
- x: this.x / window.innerWidth,
129
- y: this.y / window.innerHeight
130
- },
131
- max: {
132
- x: window.innerWidth,
133
- y: window.innerHeight
134
- }
135
- };
136
- }
137
-
138
109
  }
139
-
140
- var pointer = null;
141
- export default function usePointer() {
110
+ let instance;
111
+ let pointer;
112
+ function usePointer() {
142
113
  if (!pointer) {
143
- pointer = new Pointer();
114
+ if (!instance) {
115
+ instance = new Pointer();
116
+ }
117
+ pointer = {
118
+ add: instance.add.bind(instance),
119
+ remove: instance.remove.bind(instance),
120
+ has: instance.has.bind(instance),
121
+ props: instance.updateProps.bind(instance)
122
+ };
144
123
  }
145
-
146
- var add = pointer.add.bind(pointer);
147
- var remove = pointer.remove.bind(pointer);
148
- var has = pointer.has.bind(pointer);
149
-
150
- var props = () => pointer.props;
151
-
152
- return {
153
- add,
154
- remove,
155
- has,
156
- props
157
- };
158
- }
124
+ return pointer;
125
+ }
126
+ export {
127
+ usePointer as default
128
+ };
@@ -0,0 +1,81 @@
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/raf.js
30
+ var raf_exports = {};
31
+ __export(raf_exports, {
32
+ default: () => useRaf
33
+ });
34
+ var import_Service = __toESM(require("./Service.cjs"), 1);
35
+ var import_nextFrame = require("../utils/nextFrame.cjs");
36
+ var Raf = class extends import_Service.default {
37
+ isTicking = false;
38
+ id = 0;
39
+ raf = (0, import_nextFrame.getRaf)();
40
+ cancelRaf = (0, import_nextFrame.getCancelRaf)();
41
+ props = {
42
+ time: performance.now()
43
+ };
44
+ init() {
45
+ this.isTicking = true;
46
+ this.loop();
47
+ }
48
+ loop() {
49
+ this.updateProps();
50
+ this.trigger(this.props);
51
+ if (!this.isTicking) {
52
+ return;
53
+ }
54
+ this.id = this.raf(() => this.loop());
55
+ }
56
+ kill() {
57
+ this.cancelRaf(this.id);
58
+ this.isTicking = false;
59
+ }
60
+ updateProps() {
61
+ this.props.time = window.performance.now();
62
+ return this.props;
63
+ }
64
+ };
65
+ var instance;
66
+ var raf;
67
+ function useRaf() {
68
+ if (!raf) {
69
+ if (!instance) {
70
+ instance = new Raf();
71
+ }
72
+ raf = {
73
+ add: instance.add.bind(instance),
74
+ remove: instance.remove.bind(instance),
75
+ has: instance.has.bind(instance),
76
+ props: instance.updateProps.bind(instance)
77
+ };
78
+ }
79
+ return raf;
80
+ }
81
+ module.exports = __toCommonJS(raf_exports);
package/services/raf.d.ts CHANGED
@@ -9,20 +9,10 @@
9
9
  * props();
10
10
  * ```
11
11
  *
12
- * @return {ServiceInterface & RafService}
12
+ * @return {RafService}
13
13
  */
14
- export default function useRaf(): ServiceInterface & RafService;
15
- export type ServiceInterface = import('./index').ServiceInterface;
14
+ export default function useRaf(): RafService;
15
+ export type RafService = import('./index').ServiceInterface<RafServiceProps>;
16
16
  export type RafServiceProps = {
17
17
  time: DOMHighResTimeStamp;
18
18
  };
19
- export type RafService = {
20
- /**
21
- * Add a function to the resize service. The key must be uniq.
22
- */
23
- add: (key: string, callback: (props: RafServiceProps) => void) => void;
24
- /**
25
- * Get the current values of the resize service props.
26
- */
27
- props: () => RafServiceProps;
28
- };
package/services/raf.js CHANGED
@@ -1,70 +1,50 @@
1
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
- import Service from './Service.js';
3
- import { getRaf, getCancelRaf } from '../utils/nextFrame.js';
4
-
1
+ import Service from "./Service.js";
2
+ import { getRaf, getCancelRaf } from "../utils/nextFrame.js";
5
3
  class Raf extends Service {
6
- constructor() {
7
- super();
8
-
9
- _defineProperty(this, "isTicking", false);
10
-
11
- _defineProperty(this, "id", 0);
12
-
13
- _defineProperty(this, "raf", getRaf());
14
-
15
- _defineProperty(this, "cancelRaf", getCancelRaf());
16
-
17
- this.loop = this.loop.bind(this);
18
- }
19
-
4
+ isTicking = false;
5
+ id = 0;
6
+ raf = getRaf();
7
+ cancelRaf = getCancelRaf();
8
+ props = {
9
+ time: performance.now()
10
+ };
20
11
  init() {
21
12
  this.isTicking = true;
22
13
  this.loop();
23
- return this;
24
14
  }
25
-
26
15
  loop() {
16
+ this.updateProps();
27
17
  this.trigger(this.props);
28
-
29
18
  if (!this.isTicking) {
30
19
  return;
31
20
  }
32
-
33
- this.id = this.raf(this.loop);
21
+ this.id = this.raf(() => this.loop());
34
22
  }
35
-
36
23
  kill() {
37
24
  this.cancelRaf(this.id);
38
25
  this.isTicking = false;
39
- return this;
40
26
  }
41
-
42
- get props() {
43
- return {
44
- time: window.performance.now()
45
- };
27
+ updateProps() {
28
+ this.props.time = window.performance.now();
29
+ return this.props;
46
30
  }
47
-
48
31
  }
49
-
50
- var instance = null;
51
- export default function useRaf() {
52
- if (!instance) {
53
- instance = new Raf();
54
- }
55
-
56
- var add = instance.add.bind(instance);
57
- var remove = instance.remove.bind(instance);
58
- var has = instance.has.bind(instance);
59
-
60
- function props() {
61
- return instance.props;
32
+ let instance;
33
+ let raf;
34
+ function useRaf() {
35
+ if (!raf) {
36
+ if (!instance) {
37
+ instance = new Raf();
38
+ }
39
+ raf = {
40
+ add: instance.add.bind(instance),
41
+ remove: instance.remove.bind(instance),
42
+ has: instance.has.bind(instance),
43
+ props: instance.updateProps.bind(instance)
44
+ };
62
45
  }
63
-
64
- return {
65
- add,
66
- remove,
67
- has,
68
- props
69
- };
70
- }
46
+ return raf;
47
+ }
48
+ export {
49
+ useRaf as default
50
+ };
@@ -0,0 +1,106 @@
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/resize.js
30
+ var resize_exports = {};
31
+ __export(resize_exports, {
32
+ default: () => useResize
33
+ });
34
+ var import_Service = __toESM(require("./Service.cjs"), 1);
35
+ var import_debounce = __toESM(require("../utils/debounce.cjs"), 1);
36
+ var Resize = class extends import_Service.default {
37
+ props = {
38
+ width: window.innerWidth,
39
+ height: window.innerHeight,
40
+ ratio: window.innerWidth / window.innerHeight,
41
+ orientation: "square"
42
+ };
43
+ handler;
44
+ init() {
45
+ this.handler = (0, import_debounce.default)(() => {
46
+ this.updateProps();
47
+ this.trigger(this.props);
48
+ }).bind(this);
49
+ window.addEventListener("resize", this.handler);
50
+ return this;
51
+ }
52
+ kill() {
53
+ window.removeEventListener("resize", this.handler);
54
+ }
55
+ updateProps() {
56
+ this.props.width = window.innerWidth;
57
+ this.props.height = window.innerHeight;
58
+ this.props.ratio = window.innerWidth / window.innerHeight;
59
+ this.props.orientation = "square";
60
+ if (this.props.ratio > 1) {
61
+ this.props.orientation = "landscape";
62
+ }
63
+ if (this.props.ratio < 1) {
64
+ this.props.orientation = "portrait";
65
+ }
66
+ if (this.breakpointElement) {
67
+ this.props.breakpoint = this.breakpoint;
68
+ this.props.breakpoints = this.breakpoints;
69
+ } else {
70
+ this.props.breakpoint = void 0;
71
+ this.props.breakpoints = void 0;
72
+ }
73
+ return this.props;
74
+ }
75
+ get breakpointElement() {
76
+ return document.querySelector("[data-breakpoint]");
77
+ }
78
+ get breakpoint() {
79
+ return window.getComputedStyle(this.breakpointElement, "::before").getPropertyValue("content").replace(/"/g, "");
80
+ }
81
+ get breakpoints() {
82
+ const breakpoints = window.getComputedStyle(this.breakpointElement, "::after").getPropertyValue("content").replace(/"/g, "");
83
+ return breakpoints.split(",");
84
+ }
85
+ };
86
+ var instance;
87
+ var resize;
88
+ function useResize() {
89
+ if (!resize) {
90
+ if (!instance) {
91
+ instance = new Resize();
92
+ }
93
+ const add = instance.add.bind(instance);
94
+ const remove = instance.remove.bind(instance);
95
+ const has = instance.has.bind(instance);
96
+ const props = instance.updateProps.bind(instance);
97
+ resize = {
98
+ add,
99
+ remove,
100
+ has,
101
+ props
102
+ };
103
+ }
104
+ return resize;
105
+ }
106
+ module.exports = __toCommonJS(resize_exports);
@@ -8,10 +8,10 @@
8
8
  * remove(key);
9
9
  * props();
10
10
  * ```
11
- * @return {ServiceInterface & ResizeService}
11
+ * @return {ResizeService}
12
12
  */
13
- export default function useResize(): ServiceInterface & ResizeService;
14
- export type ServiceInterface = import('./index').ServiceInterface;
13
+ export default function useResize(): ResizeService;
14
+ export type ResizeService = import('./index').ServiceInterface<ResizeServiceProps>;
15
15
  export type ResizeServiceProps = {
16
16
  width: number;
17
17
  height: number;
@@ -20,13 +20,3 @@ export type ResizeServiceProps = {
20
20
  breakpoint?: string;
21
21
  breakpoints?: string[];
22
22
  };
23
- export type ResizeService = {
24
- /**
25
- * Add a function to the resize service. The key must be uniq.
26
- */
27
- add: (key: string, callback: (props: ResizeServiceProps) => void) => void;
28
- /**
29
- * Get the current values of the resize service props.
30
- */
31
- props: () => ResizeServiceProps;
32
- };