@studiometa/js-toolkit 2.2.1 → 2.3.0

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 (131) hide show
  1. package/Base/index.cjs +60 -69
  2. package/Base/index.d.ts +0 -8
  3. package/Base/index.js +67 -82
  4. package/Base/managers/AbstractManager.cjs +5 -10
  5. package/Base/managers/AbstractManager.js +5 -11
  6. package/Base/managers/ChildrenManager.cjs +2 -11
  7. package/Base/managers/ChildrenManager.js +2 -12
  8. package/Base/managers/EventsManager.cjs +61 -67
  9. package/Base/managers/EventsManager.js +61 -68
  10. package/Base/managers/OptionsManager.cjs +26 -27
  11. package/Base/managers/OptionsManager.js +26 -28
  12. package/Base/managers/RefsManager.cjs +9 -7
  13. package/Base/managers/RefsManager.js +9 -7
  14. package/Base/managers/ResponsiveOptionsManager.cjs +4 -1
  15. package/Base/managers/ResponsiveOptionsManager.js +4 -1
  16. package/Base/managers/ServicesManager.cjs +10 -16
  17. package/Base/managers/ServicesManager.js +10 -17
  18. package/Base/utils.cjs +22 -4
  19. package/Base/utils.d.ts +25 -8
  20. package/Base/utils.js +22 -4
  21. package/decorators/withBreakpointManager.cjs +15 -5
  22. package/decorators/withBreakpointManager.js +15 -5
  23. package/decorators/withBreakpointObserver.cjs +19 -18
  24. package/decorators/withBreakpointObserver.js +19 -19
  25. package/decorators/withDrag.cjs +6 -11
  26. package/decorators/withDrag.js +6 -12
  27. package/decorators/withExtraConfig.cjs +3 -8
  28. package/decorators/withExtraConfig.js +3 -9
  29. package/decorators/withFreezedOptions.cjs +1 -9
  30. package/decorators/withFreezedOptions.js +1 -10
  31. package/decorators/withIntersectionObserver.cjs +11 -16
  32. package/decorators/withIntersectionObserver.js +11 -17
  33. package/decorators/withMountWhenInView.cjs +12 -17
  34. package/decorators/withMountWhenInView.js +12 -18
  35. package/decorators/withScrolledInView.cjs +30 -35
  36. package/decorators/withScrolledInView.js +30 -36
  37. package/decorators/withVue2.cjs +20 -18
  38. package/decorators/withVue2.js +20 -19
  39. package/helpers/getInstanceFromElement.cjs +1 -2
  40. package/helpers/getInstanceFromElement.js +1 -2
  41. package/helpers/importOnInteraction.cjs +3 -2
  42. package/helpers/importOnInteraction.js +3 -2
  43. package/helpers/importWhenIdle.cjs +2 -3
  44. package/helpers/importWhenIdle.js +2 -3
  45. package/helpers/importWhenVisible.cjs +3 -2
  46. package/helpers/importWhenVisible.js +3 -2
  47. package/helpers/utils.cjs +3 -2
  48. package/helpers/utils.js +3 -2
  49. package/package.json +4 -1
  50. package/services/drag.cjs +126 -148
  51. package/services/drag.d.ts +10 -10
  52. package/services/drag.js +125 -148
  53. package/services/index.cjs +3 -1
  54. package/services/index.d.ts +1 -0
  55. package/services/index.js +3 -1
  56. package/services/key.cjs +48 -63
  57. package/services/key.d.ts +0 -8
  58. package/services/key.js +47 -63
  59. package/services/load.cjs +24 -40
  60. package/services/load.d.ts +0 -8
  61. package/services/load.js +23 -37
  62. package/services/pointer.cjs +72 -103
  63. package/services/pointer.d.ts +1 -9
  64. package/services/pointer.js +71 -100
  65. package/services/raf.cjs +38 -58
  66. package/services/raf.d.ts +0 -8
  67. package/services/raf.js +37 -55
  68. package/services/resize.cjs +59 -68
  69. package/services/resize.d.ts +0 -7
  70. package/services/resize.js +58 -68
  71. package/services/scroll.cjs +49 -71
  72. package/services/scroll.js +48 -71
  73. package/services/service.cjs +68 -0
  74. package/services/service.d.ts +18 -0
  75. package/services/service.js +46 -0
  76. package/utils/css/animate.cjs +33 -102
  77. package/utils/css/animate.d.ts +4 -13
  78. package/utils/css/animate.js +31 -100
  79. package/utils/css/classes.cjs +2 -1
  80. package/utils/css/classes.js +2 -1
  81. package/utils/css/matrix.cjs +1 -2
  82. package/utils/css/matrix.js +1 -2
  83. package/utils/css/styles.cjs +2 -5
  84. package/utils/css/styles.js +1 -1
  85. package/utils/css/transform.cjs +14 -18
  86. package/utils/css/transform.js +2 -3
  87. package/utils/css/transition.cjs +7 -10
  88. package/utils/css/transition.js +7 -11
  89. package/utils/focusTrap.cjs +2 -1
  90. package/utils/focusTrap.js +2 -1
  91. package/utils/{isFunction.cjs → has.cjs} +7 -7
  92. package/utils/has.d.ts +6 -0
  93. package/utils/has.js +6 -0
  94. package/utils/history.cjs +5 -7
  95. package/utils/history.js +4 -3
  96. package/utils/index.cjs +6 -5
  97. package/utils/index.d.ts +4 -2
  98. package/utils/index.js +6 -5
  99. package/utils/{object/isObject.cjs → is.cjs} +20 -8
  100. package/utils/is.d.ts +8 -0
  101. package/utils/is.js +18 -0
  102. package/utils/math/inertiaFinalValue.cjs +5 -3
  103. package/utils/math/inertiaFinalValue.js +2 -3
  104. package/utils/nextFrame.cjs +5 -3
  105. package/utils/nextFrame.js +5 -3
  106. package/utils/nextMicrotask.cjs +2 -1
  107. package/utils/nextMicrotask.js +2 -1
  108. package/utils/nextTick.cjs +2 -1
  109. package/utils/nextTick.js +2 -1
  110. package/utils/{isDefined.cjs → noop.cjs} +10 -7
  111. package/utils/noop.d.ts +12 -0
  112. package/utils/noop.js +9 -0
  113. package/utils/object/autoBind.cjs +3 -2
  114. package/utils/object/autoBind.js +3 -2
  115. package/utils/object/getAllProperties.cjs +2 -1
  116. package/utils/object/getAllProperties.js +2 -1
  117. package/utils/object/index.cjs +1 -3
  118. package/utils/object/index.d.ts +0 -1
  119. package/utils/object/index.js +1 -3
  120. package/utils/tween.cjs +113 -0
  121. package/utils/tween.d.ts +25 -0
  122. package/utils/tween.js +88 -0
  123. package/services/Service.cjs +0 -76
  124. package/services/Service.d.ts +0 -76
  125. package/services/Service.js +0 -55
  126. package/utils/isDefined.d.ts +0 -7
  127. package/utils/isDefined.js +0 -6
  128. package/utils/isFunction.d.ts +0 -7
  129. package/utils/isFunction.js +0 -6
  130. package/utils/object/isObject.d.ts +0 -7
  131. package/utils/object/isObject.js +0 -6
package/services/raf.cjs CHANGED
@@ -1,10 +1,7 @@
1
- var __create = Object.create;
2
1
  var __defProp = Object.defineProperty;
3
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
5
  var __export = (target, all) => {
9
6
  for (var name in all)
10
7
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -17,12 +14,7 @@ var __copyProps = (to, from, except, desc) => {
17
14
  }
18
15
  return to;
19
16
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
21
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
- var __publicField = (obj, key, value) => {
23
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
24
- return value;
25
- };
26
18
 
27
19
  // packages/js-toolkit/services/raf.js
28
20
  var raf_exports = {};
@@ -30,69 +22,57 @@ __export(raf_exports, {
30
22
  default: () => useRaf
31
23
  });
32
24
  module.exports = __toCommonJS(raf_exports);
33
- var import_Service = __toESM(require("./Service.cjs"), 1);
25
+ var import_service = require("./service.cjs");
34
26
  var import_nextFrame = require("../utils/nextFrame.cjs");
35
27
  var import_scheduler = require("../utils/scheduler.cjs");
36
- var import_isFunction = __toESM(require("../utils/isFunction.cjs"), 1);
28
+ var import_is = require("../utils/is.cjs");
37
29
  var scheduler = (0, import_scheduler.useScheduler)(["update", "render"]);
38
- var Raf = class extends import_Service.default {
39
- constructor() {
40
- super(...arguments);
41
- __publicField(this, "isTicking", false);
42
- __publicField(this, "id", 0);
43
- __publicField(this, "raf", (0, import_nextFrame.getRaf)());
44
- __publicField(this, "cancelRaf", (0, import_nextFrame.getCancelRaf)());
45
- __publicField(this, "props", {
46
- time: performance.now()
47
- });
48
- }
49
- init() {
50
- this.isTicking = true;
51
- this.loop();
52
- }
53
- loop() {
54
- this.updateProps();
55
- this.trigger(this.props);
56
- if (!this.isTicking) {
57
- return;
58
- }
59
- this.id = this.raf(() => this.loop());
60
- }
61
- kill() {
62
- this.cancelRaf(this.id);
63
- this.isTicking = false;
64
- }
65
- updateProps() {
66
- this.props.time = window.performance.now();
67
- return this.props;
68
- }
69
- trigger(...args) {
70
- this.callbacks.forEach(function forEachCallback(callback) {
30
+ function createRafService() {
31
+ let isTicking = false;
32
+ const RAF = (0, import_nextFrame.getRaf)();
33
+ function trigger(props2) {
34
+ callbacks.forEach(function forEachCallback(callback) {
71
35
  scheduler.update(function rafUpdate() {
72
- const render = callback(...args);
73
- if ((0, import_isFunction.default)(render)) {
36
+ const render = callback(props2);
37
+ if ((0, import_is.isFunction)(render)) {
74
38
  scheduler.render(function rafRender() {
75
- render(...args);
39
+ render(props2);
76
40
  });
77
41
  }
78
42
  });
79
43
  });
80
- return this;
81
44
  }
82
- };
83
- var instance;
45
+ function loop() {
46
+ props.time = performance.now();
47
+ trigger(props);
48
+ if (!isTicking) {
49
+ return;
50
+ }
51
+ RAF(loop);
52
+ }
53
+ const { add, remove, has, props, callbacks } = (0, import_service.useService)({
54
+ props: {
55
+ time: performance.now()
56
+ },
57
+ init() {
58
+ isTicking = true;
59
+ RAF(loop);
60
+ },
61
+ kill() {
62
+ isTicking = false;
63
+ }
64
+ });
65
+ return {
66
+ add,
67
+ remove,
68
+ has,
69
+ props: () => props
70
+ };
71
+ }
84
72
  var raf;
85
73
  function useRaf() {
86
74
  if (!raf) {
87
- if (!instance) {
88
- instance = new Raf();
89
- }
90
- raf = {
91
- add: instance.add.bind(instance),
92
- remove: instance.remove.bind(instance),
93
- has: instance.has.bind(instance),
94
- props: instance.updateProps.bind(instance)
95
- };
75
+ raf = createRafService();
96
76
  }
97
77
  return raf;
98
78
  }
package/services/raf.d.ts CHANGED
@@ -1,14 +1,6 @@
1
1
  /**
2
2
  * Use the RequestAnimationFrame (raf) service.
3
3
  *
4
- * ```js
5
- * import { useRaf } from '@studiometa/js/services';
6
- * const { add, remove, props } = useRag();
7
- * add(id, (props) => {});
8
- * remove(id);
9
- * props();
10
- * ```
11
- *
12
4
  * @returns {RafService}
13
5
  */
14
6
  export default function useRaf(): RafService;
package/services/raf.js CHANGED
@@ -1,72 +1,54 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => {
4
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- return value;
6
- };
7
- import Service from "./Service.js";
8
- import { getRaf, getCancelRaf } from "../utils/nextFrame.js";
1
+ import { useService } from "./service.js";
2
+ import { getRaf as getRequestAnimationFrame } from "../utils/nextFrame.js";
9
3
  import { useScheduler } from "../utils/scheduler.js";
10
- import isFunction from "../utils/isFunction.js";
4
+ import { isFunction } from "../utils/is.js";
11
5
  const scheduler = useScheduler(["update", "render"]);
12
- class Raf extends Service {
13
- constructor() {
14
- super(...arguments);
15
- __publicField(this, "isTicking", false);
16
- __publicField(this, "id", 0);
17
- __publicField(this, "raf", getRaf());
18
- __publicField(this, "cancelRaf", getCancelRaf());
19
- __publicField(this, "props", {
20
- time: performance.now()
21
- });
22
- }
23
- init() {
24
- this.isTicking = true;
25
- this.loop();
26
- }
27
- loop() {
28
- this.updateProps();
29
- this.trigger(this.props);
30
- if (!this.isTicking) {
31
- return;
32
- }
33
- this.id = this.raf(() => this.loop());
34
- }
35
- kill() {
36
- this.cancelRaf(this.id);
37
- this.isTicking = false;
38
- }
39
- updateProps() {
40
- this.props.time = window.performance.now();
41
- return this.props;
42
- }
43
- trigger(...args) {
44
- this.callbacks.forEach(function forEachCallback(callback) {
6
+ function createRafService() {
7
+ let isTicking = false;
8
+ const RAF = getRequestAnimationFrame();
9
+ function trigger(props2) {
10
+ callbacks.forEach(function forEachCallback(callback) {
45
11
  scheduler.update(function rafUpdate() {
46
- const render = callback(...args);
12
+ const render = callback(props2);
47
13
  if (isFunction(render)) {
48
14
  scheduler.render(function rafRender() {
49
- render(...args);
15
+ render(props2);
50
16
  });
51
17
  }
52
18
  });
53
19
  });
54
- return this;
55
20
  }
21
+ function loop() {
22
+ props.time = performance.now();
23
+ trigger(props);
24
+ if (!isTicking) {
25
+ return;
26
+ }
27
+ RAF(loop);
28
+ }
29
+ const { add, remove, has, props, callbacks } = useService({
30
+ props: {
31
+ time: performance.now()
32
+ },
33
+ init() {
34
+ isTicking = true;
35
+ RAF(loop);
36
+ },
37
+ kill() {
38
+ isTicking = false;
39
+ }
40
+ });
41
+ return {
42
+ add,
43
+ remove,
44
+ has,
45
+ props: () => props
46
+ };
56
47
  }
57
- let instance;
58
48
  let raf;
59
49
  function useRaf() {
60
50
  if (!raf) {
61
- if (!instance) {
62
- instance = new Raf();
63
- }
64
- raf = {
65
- add: instance.add.bind(instance),
66
- remove: instance.remove.bind(instance),
67
- has: instance.has.bind(instance),
68
- props: instance.updateProps.bind(instance)
69
- };
51
+ raf = createRafService();
70
52
  }
71
53
  return raf;
72
54
  }
@@ -4,7 +4,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __getProtoOf = Object.getPrototypeOf;
6
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
7
  var __export = (target, all) => {
9
8
  for (var name in all)
10
9
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -19,10 +18,6 @@ var __copyProps = (to, from, except, desc) => {
19
18
  };
20
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
21
20
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
- var __publicField = (obj, key, value) => {
23
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
24
- return value;
25
- };
26
21
 
27
22
  // packages/js-toolkit/services/resize.js
28
23
  var resize_exports = {};
@@ -30,78 +25,74 @@ __export(resize_exports, {
30
25
  default: () => useResize
31
26
  });
32
27
  module.exports = __toCommonJS(resize_exports);
33
- var import_Service = __toESM(require("./Service.cjs"), 1);
28
+ var import_service = require("./service.cjs");
34
29
  var import_debounce = __toESM(require("../utils/debounce.cjs"), 1);
35
- var Resize = class extends import_Service.default {
36
- constructor() {
37
- super(...arguments);
38
- __publicField(this, "props", {
39
- width: window.innerWidth,
40
- height: window.innerHeight,
41
- ratio: window.innerWidth / window.innerHeight,
42
- orientation: "square"
43
- });
44
- __publicField(this, "handler");
45
- }
46
- init() {
47
- this.handler = (0, import_debounce.default)(() => {
48
- this.updateProps();
49
- this.trigger(this.props);
50
- }).bind(this);
51
- window.addEventListener("resize", this.handler);
52
- return this;
30
+ var breakpointElement;
31
+ var breakpoints = [];
32
+ function getBreakpointElement() {
33
+ if (!breakpointElement) {
34
+ breakpointElement = document.querySelector("[data-breakpoint]");
53
35
  }
54
- kill() {
55
- window.removeEventListener("resize", this.handler);
36
+ return breakpointElement;
37
+ }
38
+ function getBreakpoint() {
39
+ return getBreakpointElement() ? window.getComputedStyle(getBreakpointElement(), "::before").getPropertyValue("content").replaceAll('"', "") : void 0;
40
+ }
41
+ function getBreakpoints() {
42
+ if (!getBreakpointElement() || breakpoints.length) {
43
+ return breakpoints;
56
44
  }
57
- updateProps() {
58
- this.props.width = window.innerWidth;
59
- this.props.height = window.innerHeight;
60
- this.props.ratio = window.innerWidth / window.innerHeight;
61
- this.props.orientation = "square";
62
- if (this.props.ratio > 1) {
63
- this.props.orientation = "landscape";
64
- }
65
- if (this.props.ratio < 1) {
66
- this.props.orientation = "portrait";
45
+ breakpoints = window.getComputedStyle(getBreakpointElement(), "::after").getPropertyValue("content").replaceAll('"', "").split(",");
46
+ return breakpoints;
47
+ }
48
+ function createResizeService() {
49
+ function updateProps() {
50
+ props.width = window.innerWidth;
51
+ props.height = window.innerHeight;
52
+ props.ratio = window.innerWidth / window.innerHeight;
53
+ props.orientation = "square";
54
+ if (props.ratio > 1) {
55
+ props.orientation = "landscape";
67
56
  }
68
- if (this.breakpointElement) {
69
- this.props.breakpoint = this.breakpoint;
70
- this.props.breakpoints = this.breakpoints;
71
- } else {
72
- this.props.breakpoint = void 0;
73
- this.props.breakpoints = void 0;
57
+ if (props.ratio < 1) {
58
+ props.orientation = "portrait";
74
59
  }
75
- return this.props;
76
- }
77
- get breakpointElement() {
78
- return document.querySelector("[data-breakpoint]");
60
+ return props;
79
61
  }
80
- get breakpoint() {
81
- return window.getComputedStyle(this.breakpointElement, "::before").getPropertyValue("content").replaceAll('"', "");
82
- }
83
- get breakpoints() {
84
- const breakpoints = window.getComputedStyle(this.breakpointElement, "::after").getPropertyValue("content").replaceAll('"', "");
85
- return breakpoints.split(",");
86
- }
87
- };
88
- var instance;
62
+ const onResize = (0, import_debounce.default)(() => {
63
+ trigger(updateProps());
64
+ });
65
+ const { add, remove, has, trigger, props } = (0, import_service.useService)({
66
+ props: {
67
+ width: window.innerWidth,
68
+ height: window.innerHeight,
69
+ ratio: window.innerWidth / window.innerHeight,
70
+ orientation: "square",
71
+ get breakpoint() {
72
+ return getBreakpoint();
73
+ },
74
+ get breakpoints() {
75
+ return getBreakpoints();
76
+ }
77
+ },
78
+ init() {
79
+ window.addEventListener("resize", onResize);
80
+ },
81
+ kill() {
82
+ window.removeEventListener("resize", onResize);
83
+ }
84
+ });
85
+ return {
86
+ add,
87
+ remove,
88
+ has,
89
+ props: () => props
90
+ };
91
+ }
89
92
  var resize;
90
93
  function useResize() {
91
94
  if (!resize) {
92
- if (!instance) {
93
- instance = new Resize();
94
- }
95
- const add = instance.add.bind(instance);
96
- const remove = instance.remove.bind(instance);
97
- const has = instance.has.bind(instance);
98
- const props = instance.updateProps.bind(instance);
99
- resize = {
100
- add,
101
- remove,
102
- has,
103
- props
104
- };
95
+ resize = createResizeService();
105
96
  }
106
97
  return resize;
107
98
  }
@@ -1,13 +1,6 @@
1
1
  /**
2
2
  * Use the resize service.
3
3
  *
4
- * ```js
5
- * import useResize from '@studiometa/js-toolkit/services/resize';
6
- * const { add, remove, props } = useResize();
7
- * add(key, (props) => {});
8
- * remove(key);
9
- * props();
10
- * ```
11
4
  * @returns {ResizeService}
12
5
  */
13
6
  export default function useResize(): ResizeService;
@@ -1,81 +1,71 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => {
4
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- return value;
6
- };
7
- import Service from "./Service.js";
1
+ import { useService } from "./service.js";
8
2
  import debounce from "../utils/debounce.js";
9
- class Resize extends Service {
10
- constructor() {
11
- super(...arguments);
12
- __publicField(this, "props", {
13
- width: window.innerWidth,
14
- height: window.innerHeight,
15
- ratio: window.innerWidth / window.innerHeight,
16
- orientation: "square"
17
- });
18
- __publicField(this, "handler");
19
- }
20
- init() {
21
- this.handler = debounce(() => {
22
- this.updateProps();
23
- this.trigger(this.props);
24
- }).bind(this);
25
- window.addEventListener("resize", this.handler);
26
- return this;
3
+ let breakpointElement;
4
+ let breakpoints = [];
5
+ function getBreakpointElement() {
6
+ if (!breakpointElement) {
7
+ breakpointElement = document.querySelector("[data-breakpoint]");
27
8
  }
28
- kill() {
29
- window.removeEventListener("resize", this.handler);
9
+ return breakpointElement;
10
+ }
11
+ function getBreakpoint() {
12
+ return getBreakpointElement() ? window.getComputedStyle(getBreakpointElement(), "::before").getPropertyValue("content").replaceAll('"', "") : void 0;
13
+ }
14
+ function getBreakpoints() {
15
+ if (!getBreakpointElement() || breakpoints.length) {
16
+ return breakpoints;
30
17
  }
31
- updateProps() {
32
- this.props.width = window.innerWidth;
33
- this.props.height = window.innerHeight;
34
- this.props.ratio = window.innerWidth / window.innerHeight;
35
- this.props.orientation = "square";
36
- if (this.props.ratio > 1) {
37
- this.props.orientation = "landscape";
38
- }
39
- if (this.props.ratio < 1) {
40
- this.props.orientation = "portrait";
18
+ breakpoints = window.getComputedStyle(getBreakpointElement(), "::after").getPropertyValue("content").replaceAll('"', "").split(",");
19
+ return breakpoints;
20
+ }
21
+ function createResizeService() {
22
+ function updateProps() {
23
+ props.width = window.innerWidth;
24
+ props.height = window.innerHeight;
25
+ props.ratio = window.innerWidth / window.innerHeight;
26
+ props.orientation = "square";
27
+ if (props.ratio > 1) {
28
+ props.orientation = "landscape";
41
29
  }
42
- if (this.breakpointElement) {
43
- this.props.breakpoint = this.breakpoint;
44
- this.props.breakpoints = this.breakpoints;
45
- } else {
46
- this.props.breakpoint = void 0;
47
- this.props.breakpoints = void 0;
30
+ if (props.ratio < 1) {
31
+ props.orientation = "portrait";
48
32
  }
49
- return this.props;
50
- }
51
- get breakpointElement() {
52
- return document.querySelector("[data-breakpoint]");
53
- }
54
- get breakpoint() {
55
- return window.getComputedStyle(this.breakpointElement, "::before").getPropertyValue("content").replaceAll('"', "");
56
- }
57
- get breakpoints() {
58
- const breakpoints = window.getComputedStyle(this.breakpointElement, "::after").getPropertyValue("content").replaceAll('"', "");
59
- return breakpoints.split(",");
33
+ return props;
60
34
  }
35
+ const onResize = debounce(() => {
36
+ trigger(updateProps());
37
+ });
38
+ const { add, remove, has, trigger, props } = useService({
39
+ props: {
40
+ width: window.innerWidth,
41
+ height: window.innerHeight,
42
+ ratio: window.innerWidth / window.innerHeight,
43
+ orientation: "square",
44
+ get breakpoint() {
45
+ return getBreakpoint();
46
+ },
47
+ get breakpoints() {
48
+ return getBreakpoints();
49
+ }
50
+ },
51
+ init() {
52
+ window.addEventListener("resize", onResize);
53
+ },
54
+ kill() {
55
+ window.removeEventListener("resize", onResize);
56
+ }
57
+ });
58
+ return {
59
+ add,
60
+ remove,
61
+ has,
62
+ props: () => props
63
+ };
61
64
  }
62
- let instance;
63
65
  let resize;
64
66
  function useResize() {
65
67
  if (!resize) {
66
- if (!instance) {
67
- instance = new Resize();
68
- }
69
- const add = instance.add.bind(instance);
70
- const remove = instance.remove.bind(instance);
71
- const has = instance.has.bind(instance);
72
- const props = instance.updateProps.bind(instance);
73
- resize = {
74
- add,
75
- remove,
76
- has,
77
- props
78
- };
68
+ resize = createResizeService();
79
69
  }
80
70
  return resize;
81
71
  }