@studiometa/js-toolkit 3.0.0-alpha.8 → 3.0.0-beta.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 (94) hide show
  1. package/Base/Base.js +2 -2
  2. package/Base/Base.js.map +2 -2
  3. package/Base/features.d.ts +1 -0
  4. package/Base/features.js +1 -0
  5. package/Base/features.js.map +2 -2
  6. package/Base/managers/ChildrenManager.d.ts +1 -1
  7. package/Base/managers/ChildrenManager.js +8 -1
  8. package/Base/managers/ChildrenManager.js.map +2 -2
  9. package/Base/managers/EventsManager.js +15 -17
  10. package/Base/managers/EventsManager.js.map +2 -2
  11. package/Base/managers/OptionsManager.d.ts +2 -2
  12. package/Base/managers/OptionsManager.js +8 -8
  13. package/Base/managers/OptionsManager.js.map +2 -2
  14. package/Base/managers/RefsManager.js +7 -5
  15. package/Base/managers/RefsManager.js.map +2 -2
  16. package/Base/utils.d.ts +4 -0
  17. package/Base/utils.js +7 -3
  18. package/Base/utils.js.map +2 -2
  19. package/decorators/withBreakpointObserver.js +14 -9
  20. package/decorators/withBreakpointObserver.js.map +2 -2
  21. package/decorators/withDrag.d.ts +1 -1
  22. package/decorators/withDrag.js.map +1 -1
  23. package/decorators/withRelativePointer.d.ts +1 -1
  24. package/decorators/withRelativePointer.js.map +1 -1
  25. package/decorators/withResponsiveOptions.d.ts +3 -1
  26. package/decorators/withResponsiveOptions.js +22 -2
  27. package/decorators/withResponsiveOptions.js.map +2 -2
  28. package/helpers/createApp.js +5 -1
  29. package/helpers/createApp.js.map +2 -2
  30. package/helpers/getDirectChildren.js +10 -5
  31. package/helpers/getDirectChildren.js.map +2 -2
  32. package/package.json +1 -1
  33. package/services/AbstractService.d.ts +98 -0
  34. package/services/AbstractService.js +115 -0
  35. package/services/AbstractService.js.map +7 -0
  36. package/services/DragService.d.ts +124 -0
  37. package/services/DragService.js +217 -0
  38. package/services/DragService.js.map +7 -0
  39. package/services/KeyService.d.ts +29 -0
  40. package/services/KeyService.js +48 -0
  41. package/services/KeyService.js.map +7 -0
  42. package/services/LoadService.d.ts +15 -0
  43. package/services/LoadService.js +19 -0
  44. package/services/LoadService.js.map +7 -0
  45. package/services/PointerService.d.ts +56 -0
  46. package/services/PointerService.js +129 -0
  47. package/services/PointerService.js.map +7 -0
  48. package/services/RafService.d.ts +19 -0
  49. package/services/RafService.js +45 -0
  50. package/services/RafService.js.map +7 -0
  51. package/services/ResizeService.d.ts +29 -0
  52. package/services/ResizeService.js +66 -0
  53. package/services/ResizeService.js.map +7 -0
  54. package/services/{scroll.d.ts → ScrollService.d.ts} +14 -3
  55. package/services/ScrollService.js +90 -0
  56. package/services/ScrollService.js.map +7 -0
  57. package/services/index.d.ts +8 -41
  58. package/services/index.js +8 -18
  59. package/services/index.js.map +3 -3
  60. package/services/utils.d.ts +14 -0
  61. package/services/utils.js +11 -0
  62. package/services/utils.js.map +7 -0
  63. package/tsconfig.build.tsbuildinfo +1 -0
  64. package/utils/index.d.ts +1 -0
  65. package/utils/index.js +1 -0
  66. package/utils/index.js.map +2 -2
  67. package/utils/loadElement.d.ts +42 -0
  68. package/utils/loadElement.js +33 -0
  69. package/utils/loadElement.js.map +7 -0
  70. package/utils/tween.js +1 -1
  71. package/utils/tween.js.map +2 -2
  72. package/services/drag.d.ts +0 -75
  73. package/services/drag.js +0 -199
  74. package/services/drag.js.map +0 -7
  75. package/services/key.d.ts +0 -21
  76. package/services/key.js +0 -65
  77. package/services/key.js.map +0 -7
  78. package/services/load.d.ts +0 -11
  79. package/services/load.js +0 -38
  80. package/services/load.js.map +0 -7
  81. package/services/pointer.d.ts +0 -32
  82. package/services/pointer.js +0 -123
  83. package/services/pointer.js.map +0 -7
  84. package/services/raf.d.ts +0 -9
  85. package/services/raf.js +0 -56
  86. package/services/raf.js.map +0 -7
  87. package/services/resize.d.ts +0 -15
  88. package/services/resize.js +0 -67
  89. package/services/resize.js.map +0 -7
  90. package/services/scroll.js +0 -105
  91. package/services/scroll.js.map +0 -7
  92. package/services/service.d.ts +0 -16
  93. package/services/service.js +0 -47
  94. package/services/service.js.map +0 -7
@@ -1,32 +0,0 @@
1
- import type { ServiceInterface } from './index.js';
2
- export type PointerService = ServiceInterface<PointerServiceProps>;
3
- export interface PointerServiceProps {
4
- event: MouseEvent | TouchEvent;
5
- isDown: boolean;
6
- x: number;
7
- y: number;
8
- changed: {
9
- x: boolean;
10
- y: boolean;
11
- };
12
- last: {
13
- x: number;
14
- y: number;
15
- };
16
- delta: {
17
- x: number;
18
- y: number;
19
- };
20
- progress: {
21
- x: number;
22
- y: number;
23
- };
24
- max: {
25
- x: number;
26
- y: number;
27
- };
28
- }
29
- /**
30
- * Use the pointer service.
31
- */
32
- export default function usePointer(target?: HTMLElement): PointerService;
@@ -1,123 +0,0 @@
1
- import { useService } from "./service.js";
2
- import { isDefined } from "../utils/index.js";
3
- function isTouchEvent(event) {
4
- return typeof TouchEvent !== "undefined" && event instanceof TouchEvent;
5
- }
6
- const events = ["mousemove", "touchmove", "mousedown", "touchstart", "mouseup", "touchend"];
7
- function createPointerService(target) {
8
- function getTargetSize() {
9
- return isDefined(target) ? target.getBoundingClientRect() : {
10
- width: window.innerWidth,
11
- height: window.innerHeight,
12
- x: 0,
13
- y: 0
14
- };
15
- }
16
- function updateProps(event) {
17
- props.event = event;
18
- const yLast = props.y;
19
- const xLast = props.x;
20
- const targetSize2 = getTargetSize();
21
- let y = isTouchEvent(event) ? event.touches[0]?.clientY : event.clientY;
22
- y -= targetSize2.y;
23
- if (y !== props.y) {
24
- props.y = y;
25
- }
26
- let x = isTouchEvent(event) ? event.touches[0]?.clientX : event.clientX;
27
- x -= targetSize2.x;
28
- if (x !== props.x) {
29
- props.x = x;
30
- }
31
- props.changed.x = props.x !== xLast;
32
- props.changed.y = props.y !== yLast;
33
- props.last.x = xLast;
34
- props.last.y = yLast;
35
- props.delta.x = props.x - xLast;
36
- props.delta.y = props.y - yLast;
37
- props.max.x = targetSize2.width;
38
- props.max.y = targetSize2.height;
39
- props.progress.x = props.x / props.max.x;
40
- props.progress.y = props.y / props.max.y;
41
- return props;
42
- }
43
- function handleEvent(event) {
44
- switch (event.type) {
45
- case "mouseenter":
46
- case "mousemove":
47
- case "touchmove":
48
- trigger(updateProps(event));
49
- break;
50
- case "mousedown":
51
- case "touchstart":
52
- props.isDown = true;
53
- trigger(props);
54
- break;
55
- case "mouseup":
56
- case "touchend":
57
- props.isDown = false;
58
- trigger(props);
59
- break;
60
- }
61
- }
62
- const targetSize = getTargetSize();
63
- const { add, remove, has, trigger, props } = useService({
64
- props: {
65
- event: null,
66
- isDown: false,
67
- x: targetSize.x / 2,
68
- y: targetSize.y / 2,
69
- changed: {
70
- x: false,
71
- y: false
72
- },
73
- last: {
74
- x: targetSize.x / 2,
75
- y: targetSize.y / 2
76
- },
77
- delta: {
78
- x: 0,
79
- y: 0
80
- },
81
- progress: {
82
- x: 0.5,
83
- y: 0.5
84
- },
85
- max: {
86
- x: targetSize.width,
87
- y: targetSize.height
88
- }
89
- },
90
- init() {
91
- document.documentElement.addEventListener("mouseenter", handleEvent, {
92
- once: true,
93
- capture: true
94
- });
95
- const options = { passive: true, capture: true };
96
- for (const event of events) {
97
- document.addEventListener(event, handleEvent, options);
98
- }
99
- },
100
- kill() {
101
- for (const event of events) {
102
- document.removeEventListener(event, handleEvent);
103
- }
104
- }
105
- });
106
- return {
107
- add,
108
- remove,
109
- has,
110
- props: () => props
111
- };
112
- }
113
- const instances = /* @__PURE__ */ new Map();
114
- function usePointer(target) {
115
- if (!instances.has(target ?? window)) {
116
- instances.set(target ?? window, createPointerService(target));
117
- }
118
- return instances.get(target ?? window);
119
- }
120
- export {
121
- usePointer as default
122
- };
123
- //# sourceMappingURL=pointer.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../packages/js-toolkit/services/pointer.ts"],
4
- "sourcesContent": ["/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */\nimport { useService } from './service.js';\nimport type { ServiceInterface } from './index.js';\nimport { isDefined } from '../utils/index.js';\n\nexport type PointerService = ServiceInterface<PointerServiceProps>;\n\nexport interface PointerServiceProps {\n event: MouseEvent | TouchEvent;\n isDown: boolean;\n x: number;\n y: number;\n changed: { x: boolean; y: boolean };\n last: { x: number; y: number };\n delta: { x: number; y: number };\n progress: { x: number; y: number };\n max: { x: number; y: number };\n}\n\n/**\n * Test if an event is an instance of TouchEvent.\n */\nfunction isTouchEvent(event: TouchEvent | MouseEvent): boolean {\n return typeof TouchEvent !== 'undefined' && event instanceof TouchEvent;\n}\n\nconst events = ['mousemove', 'touchmove', 'mousedown', 'touchstart', 'mouseup', 'touchend'];\n\n/**\n * Get pointer service.\n */\nfunction createPointerService(target: HTMLElement | undefined): PointerService {\n function getTargetSize() {\n return isDefined(target)\n ? target.getBoundingClientRect()\n : {\n width: window.innerWidth,\n height: window.innerHeight,\n x: 0,\n y: 0,\n };\n }\n\n /**\n * Update the pointer positions.\n */\n function updateProps(event: MouseEvent | TouchEvent): PointerServiceProps {\n props.event = event;\n const yLast = props.y;\n const xLast = props.x;\n\n const targetSize = getTargetSize();\n\n // Check pointer Y\n // We either get data from a touch event `event.touches[0].clientY` or from\n // a mouse event `event.clientY`.\n let y = isTouchEvent(event)\n ? (event as TouchEvent).touches[0]?.clientY\n : (event as MouseEvent).clientY;\n y -= targetSize.y;\n if (y !== props.y) {\n props.y = y;\n }\n\n // Check pointer X\n // We either get data from a touch event `event.touches[0].clientX` or from\n // a mouse event `event.clientX`.\n let x = isTouchEvent(event)\n ? (event as TouchEvent).touches[0]?.clientX\n : (event as MouseEvent).clientX;\n x -= targetSize.x;\n if (x !== props.x) {\n props.x = x;\n }\n\n props.changed.x = props.x !== xLast;\n props.changed.y = props.y !== yLast;\n\n props.last.x = xLast;\n props.last.y = yLast;\n\n props.delta.x = props.x - xLast;\n props.delta.y = props.y - yLast;\n\n props.max.x = targetSize.width;\n props.max.y = targetSize.height;\n\n props.progress.x = props.x / props.max.x;\n props.progress.y = props.y / props.max.y;\n\n return props;\n }\n\n /**\n * Handle events.\n *\n * @todo handle scroll as well\n */\n function handleEvent(event: MouseEvent | TouchEvent) {\n // eslint-disable-next-line default-case\n switch (event.type) {\n case 'mouseenter':\n case 'mousemove':\n case 'touchmove':\n trigger(updateProps(event));\n break;\n case 'mousedown':\n case 'touchstart':\n props.isDown = true;\n trigger(props);\n break;\n case 'mouseup':\n case 'touchend':\n props.isDown = false;\n trigger(props);\n break;\n }\n }\n\n const targetSize = getTargetSize();\n const { add, remove, has, trigger, props } = useService({\n props: {\n event: null,\n isDown: false,\n x: targetSize.x / 2,\n y: targetSize.y / 2,\n changed: {\n x: false,\n y: false,\n },\n last: {\n x: targetSize.x / 2,\n y: targetSize.y / 2,\n },\n delta: {\n x: 0,\n y: 0,\n },\n progress: {\n x: 0.5,\n y: 0.5,\n },\n max: {\n x: targetSize.width,\n y: targetSize.height,\n },\n } as PointerServiceProps,\n init() {\n document.documentElement.addEventListener('mouseenter', handleEvent, {\n once: true,\n capture: true,\n });\n\n const options = { passive: true, capture: true };\n for (const event of events) {\n document.addEventListener(event, handleEvent, options);\n }\n },\n kill() {\n for (const event of events) {\n document.removeEventListener(event, handleEvent);\n }\n },\n });\n\n return {\n add,\n remove,\n has,\n props: () => props,\n };\n}\n\nconst instances = new Map<HTMLElement | Window, PointerService>();\n\n/**\n * Use the pointer service.\n */\nexport default function usePointer(target?: HTMLElement): PointerService {\n if (!instances.has(target ?? window)) {\n instances.set(target ?? window, createPointerService(target));\n }\n return instances.get(target ?? window) as PointerService;\n}\n"],
5
- "mappings": "AACA,SAAS,kBAAkB;AAE3B,SAAS,iBAAiB;AAmB1B,SAAS,aAAa,OAAyC;AAC7D,SAAO,OAAO,eAAe,eAAe,iBAAiB;AAC/D;AAEA,MAAM,SAAS,CAAC,aAAa,aAAa,aAAa,cAAc,WAAW,UAAU;AAK1F,SAAS,qBAAqB,QAAiD;AAC7E,WAAS,gBAAgB;AACvB,WAAO,UAAU,MAAM,IACnB,O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AACvC;",
6
- "names": ["targetSize"]
7
- }
package/services/raf.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import type { ServiceInterface } from './index.js';
2
- export type RafService = ServiceInterface<RafServiceProps>;
3
- export interface RafServiceProps {
4
- time: DOMHighResTimeStamp;
5
- }
6
- /**
7
- * Use the RequestAnimationFrame (raf) service.
8
- */
9
- export default function useRaf(): RafService;
package/services/raf.js DELETED
@@ -1,56 +0,0 @@
1
- import { useService } from "./service.js";
2
- import { useScheduler } from "../utils/scheduler.js";
3
- import { isFunction } from "../utils/is.js";
4
- const scheduler = useScheduler(["update", "render"]);
5
- function createRafService() {
6
- let isTicking = false;
7
- function trigger(props2) {
8
- for (const callback of callbacks.values()) {
9
- scheduler.update(() => {
10
- const render = callback(props2);
11
- if (isFunction(render)) {
12
- scheduler.render(() => {
13
- render(props2);
14
- });
15
- }
16
- });
17
- }
18
- }
19
- function loop() {
20
- props.time = performance.now();
21
- trigger(props);
22
- if (!isTicking) {
23
- return;
24
- }
25
- requestAnimationFrame(loop);
26
- }
27
- const { add, remove, has, props, callbacks } = useService({
28
- props: {
29
- time: performance.now()
30
- },
31
- init() {
32
- isTicking = true;
33
- requestAnimationFrame(loop);
34
- },
35
- kill() {
36
- isTicking = false;
37
- }
38
- });
39
- return {
40
- add,
41
- remove,
42
- has,
43
- props: () => props
44
- };
45
- }
46
- let raf;
47
- function useRaf() {
48
- if (!raf) {
49
- raf = createRafService();
50
- }
51
- return raf;
52
- }
53
- export {
54
- useRaf as default
55
- };
56
- //# sourceMappingURL=raf.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../packages/js-toolkit/services/raf.ts"],
4
- "sourcesContent": ["/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */\nimport { useService } from './service.js';\nimport { useScheduler } from '../utils/scheduler.js';\nimport { isFunction } from '../utils/is.js';\nimport type { ServiceInterface } from './index.js';\n\nconst scheduler = useScheduler(['update', 'render']);\n\nexport type RafService = ServiceInterface<RafServiceProps>;\n\nexport interface RafServiceProps {\n time: DOMHighResTimeStamp;\n}\n\n/**\n * Create raf service.\n */\nfunction createRafService(): RafService {\n let isTicking = false;\n\n /**\n * Trigger callbacks.\n */\n function trigger(props: RafServiceProps) {\n for (const callback of callbacks.values()) {\n scheduler.update(() => {\n const render = callback(props) as (() => unknown) | void;\n if (isFunction(render)) {\n scheduler.render(() => {\n // @ts-ignore\n render(props);\n });\n }\n });\n }\n }\n\n /**\n * Loop.\n */\n function loop() {\n // eslint-disable-next-line no-use-before-define\n props.time = performance.now();\n trigger(props);\n\n if (!isTicking) {\n return;\n }\n\n requestAnimationFrame(loop);\n }\n\n const { add, remove, has, props, callbacks } = useService({\n props: {\n time: performance.now(),\n } as RafServiceProps,\n init() {\n isTicking = true;\n requestAnimationFrame(loop);\n },\n kill() {\n isTicking = false;\n },\n });\n\n return {\n add,\n remove,\n has,\n props: () => props,\n };\n}\n\nlet raf;\n\n/**\n * Use the RequestAnimationFrame (raf) service.\n */\nexport default function useRaf(): RafService {\n if (!raf) {\n raf = createRafService();\n }\n\n return raf;\n}\n"],
5
- "mappings": "AACA,SAAS,kBAAkB;AAC3B,SAAS,oBAAoB;AAC7B,SAAS,kBAAkB;AAG3B,MAAM,YAAY,aAAa,CAAC,UAAU,QAAQ,CAAC;AAWnD,SAAS,mBAA+B;AACtC,MAAI,YAAY;AAKhB,WAAS,QAAQA,QAAwB;AACvC,eAAW,YAAY,UAAU,OAAO,GAAG;AACzC,gBAAU,OAAO,MAAM;AACrB,cAAM,SAAS,SAASA,MAAK;AAC7B,YAAI,WAAW,MAAM,GAAG;AACtB,oBAAU,OAAO,MAAM;AAErB,mBAAOA,MAAK;AAAA,UACd,CAAC;AAAA,QACH;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAKA,WAAS,OAAO;AAEd,UAAM,OAAO,YAAY,IAAI;AAC7B,YAAQ,KAAK;AAEb,QAAI,CAAC,WAAW;AACd;AAAA,IACF;AAEA,0BAAsB,IAAI;AAAA,EAC5B;AAEA,QAAM,EAAE,KAAK,QAAQ,KAAK,OAAO,UAAU,IAAI,WAAW;AAAA,IACxD,OAAO;AAAA,MACL,MAAM,YAAY,IAAI;AAAA,IACxB;AAAA,IACA,OAAO;AACL,kBAAY;AACZ,4BAAsB,IAAI;AAAA,IAC5B;AAAA,IACA,OAAO;AACL,kBAAY;AAAA,IACd;AAAA,EACF,CAAC;AAED,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,OAAO,MAAM;AAAA,EACf;AACF;AAEA,IAAI;AAKW,SAAR,SAAsC;AAC3C,MAAI,CAAC,KAAK;AACR,UAAM,iBAAiB;AAAA,EACzB;AAEA,SAAO;AACT;",
6
- "names": ["props"]
7
- }
@@ -1,15 +0,0 @@
1
- import type { Features } from '../Base/features.js';
2
- import type { ResizeServiceInterface } from './index.js';
3
- export interface ResizeServiceProps<U extends Features['breakpoints'] = Features['breakpoints']> {
4
- width: number;
5
- height: number;
6
- ratio: number;
7
- orientation: 'square' | 'landscape' | 'portrait';
8
- breakpoint: string;
9
- breakpoints: string[];
10
- activeBreakpoints: Record<keyof U, boolean>;
11
- }
12
- /**
13
- * Use the resize service.
14
- */
15
- export default function useResize<T extends Features['breakpoints'] = Features['breakpoints']>(breakpoints?: T): ResizeServiceInterface<T>;
@@ -1,67 +0,0 @@
1
- import { useService } from "./service.js";
2
- import { features } from "../Base/features.js";
3
- import debounce from "../utils/debounce.js";
4
- function createResizeService(breakpoints) {
5
- function updateProps() {
6
- props.width = window.innerWidth;
7
- props.height = window.innerHeight;
8
- props.ratio = window.innerWidth / window.innerHeight;
9
- props.orientation = "square";
10
- if (props.ratio > 1) {
11
- props.orientation = "landscape";
12
- }
13
- if (props.ratio < 1) {
14
- props.orientation = "portrait";
15
- }
16
- return props;
17
- }
18
- const onResize = debounce(() => {
19
- trigger(updateProps());
20
- });
21
- const { add, remove, has, trigger, props } = useService({
22
- props: {
23
- width: window.innerWidth,
24
- height: window.innerHeight,
25
- ratio: window.innerWidth / window.innerHeight,
26
- orientation: "square",
27
- get breakpoint() {
28
- return Object.keys(this.activeBreakpoints).toReversed().find((name) => this.activeBreakpoints[name]);
29
- },
30
- get breakpoints() {
31
- return Object.keys(this.activeBreakpoints);
32
- },
33
- get activeBreakpoints() {
34
- return Object.fromEntries(
35
- Object.entries(breakpoints ?? features.get("breakpoints")).map(([name, breakpoint]) => [
36
- name,
37
- window.matchMedia(`(min-width: ${breakpoint})`).matches
38
- ])
39
- );
40
- }
41
- },
42
- init() {
43
- window.addEventListener("resize", onResize);
44
- },
45
- kill() {
46
- window.removeEventListener("resize", onResize);
47
- }
48
- });
49
- return {
50
- add,
51
- remove,
52
- has,
53
- props: () => props
54
- };
55
- }
56
- const instances = /* @__PURE__ */ new Map();
57
- function useResize(breakpoints) {
58
- const key = JSON.stringify(breakpoints);
59
- if (!instances.has(key)) {
60
- instances.set(key, createResizeService(breakpoints));
61
- }
62
- return instances.get(key);
63
- }
64
- export {
65
- useResize as default
66
- };
67
- //# sourceMappingURL=resize.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../packages/js-toolkit/services/resize.ts"],
4
- "sourcesContent": ["/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */\nimport { useService } from './service.js';\nimport { features } from '../Base/features.js';\nimport type { Features } from '../Base/features.js';\nimport debounce from '../utils/debounce.js';\nimport type { ResizeServiceInterface } from './index.js';\n\nexport interface ResizeServiceProps<U extends Features['breakpoints'] = Features['breakpoints']> {\n width: number;\n height: number;\n ratio: number;\n orientation: 'square' | 'landscape' | 'portrait';\n breakpoint: string;\n breakpoints: string[];\n activeBreakpoints: Record<keyof U, boolean>;\n}\n\n/**\n * Get resize service.\n */\nfunction createResizeService<T extends Features['breakpoints'] = Features['breakpoints']>(\n breakpoints?: T,\n): ResizeServiceInterface<T> {\n /**\n * Update props.\n */\n function updateProps(): ResizeServiceProps<T> {\n props.width = window.innerWidth;\n props.height = window.innerHeight;\n props.ratio = window.innerWidth / window.innerHeight;\n props.orientation = 'square';\n\n if (props.ratio > 1) {\n props.orientation = 'landscape';\n }\n\n if (props.ratio < 1) {\n props.orientation = 'portrait';\n }\n\n return props;\n }\n\n const onResize = debounce(() => {\n trigger(updateProps());\n });\n\n const { add, remove, has, trigger, props } = useService({\n props: {\n width: window.innerWidth,\n height: window.innerHeight,\n ratio: window.innerWidth / window.innerHeight,\n orientation: 'square',\n get breakpoint() {\n return Object.keys(this.activeBreakpoints)\n .toReversed()\n .find((name) => this.activeBreakpoints[name]);\n },\n get breakpoints() {\n return Object.keys(this.activeBreakpoints);\n },\n get activeBreakpoints() {\n return Object.fromEntries(\n Object.entries(breakpoints ?? features.get('breakpoints')).map(([name, breakpoint]) => [\n name,\n window.matchMedia(`(min-width: ${breakpoint})`).matches,\n ]),\n ) as Record<keyof T, boolean>;\n },\n } as ResizeServiceProps<T>,\n init() {\n window.addEventListener('resize', onResize);\n },\n kill() {\n window.removeEventListener('resize', onResize);\n },\n });\n\n return {\n add,\n remove,\n has,\n props: () => props,\n };\n}\n\nconst instances: Map<string, ResizeServiceInterface | undefined> = new Map();\n\n/**\n * Use the resize service.\n */\nexport default function useResize<T extends Features['breakpoints'] = Features['breakpoints']>(\n breakpoints?: T,\n): ResizeServiceInterface<T> {\n const key = JSON.stringify(breakpoints);\n\n if (!instances.has(key)) {\n instances.set(key, createResizeService(breakpoints));\n }\n\n return (instances as Map<string, ResizeServiceInterface<T>>).get(key);\n}\n"],
5
- "mappings": "AACA,SAAS,kBAAkB;AAC3B,SAAS,gBAAgB;AAEzB,OAAO,cAAc;AAgBrB,SAAS,oBACP,aAC2B;AAI3B,WAAS,cAAqC;AAC5C,UAAM,QAAQ,OAAO;AACrB,UAAM,SAAS,OAAO;AACtB,UAAM,QAAQ,OAAO,aAAa,OAAO;AACzC,UAAM,cAAc;AAEpB,QAAI,MAAM,QAAQ,GAAG;AACnB,YAAM,cAAc;AAAA,IACtB;AAEA,QAAI,MAAM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tE;",
6
- "names": []
7
- }
@@ -1,105 +0,0 @@
1
- import { useService } from "./service.js";
2
- import debounce from "../utils/debounce.js";
3
- function createScrollService() {
4
- function updateProps() {
5
- const yLast = props.y;
6
- const xLast = props.x;
7
- if (window.scrollY !== props.y) {
8
- props.y = window.scrollY;
9
- }
10
- if (window.scrollX !== props.x) {
11
- props.x = window.scrollX;
12
- }
13
- props.changed.x = props.changedX = props.x !== xLast;
14
- props.changed.y = props.changedY = props.y !== yLast;
15
- props.last.x = props.lastX = xLast;
16
- props.last.y = props.lastY = yLast;
17
- props.delta.x = props.deltaX = props.x - xLast;
18
- props.delta.y = props.deltaY = props.y - yLast;
19
- props.max.x = props.maxX = (document.scrollingElement || document.body).scrollWidth - window.innerWidth;
20
- props.max.y = props.maxY = (document.scrollingElement || document.body).scrollHeight - window.innerHeight;
21
- props.progress.x = props.progressX = props.max.x === 0 ? 1 : props.x / props.max.x;
22
- props.progress.y = props.progressY = props.max.y === 0 ? 1 : props.y / props.max.y;
23
- props.isUp = props.y < yLast;
24
- props.isRight = props.x > xLast;
25
- props.isDown = props.y > yLast;
26
- props.isLeft = props.x < xLast;
27
- props.direction.x = props.isRight ? "RIGHT" : props.isLeft ? "LEFT" : "NONE";
28
- props.direction.y = props.isDown ? "DOWN" : props.isUp ? "UP" : "NONE";
29
- return props;
30
- }
31
- const onScrollDebounced = debounce(() => {
32
- trigger(updateProps());
33
- }, 100);
34
- function onScroll() {
35
- trigger(updateProps());
36
- onScrollDebounced();
37
- }
38
- const { add, remove, has, props, trigger } = useService({
39
- props: {
40
- x: window.scrollX,
41
- y: window.scrollY,
42
- changed: {
43
- x: false,
44
- y: false
45
- },
46
- changedX: false,
47
- changedY: false,
48
- last: {
49
- x: window.scrollX,
50
- y: window.scrollY
51
- },
52
- lastX: window.scrollX,
53
- lastY: window.scrollY,
54
- delta: {
55
- x: 0,
56
- y: 0
57
- },
58
- deltaX: 0,
59
- deltaY: 0,
60
- max: {
61
- x: (document.scrollingElement || document.body).scrollWidth - window.innerWidth,
62
- y: (document.scrollingElement || document.body).scrollHeight - window.innerHeight
63
- },
64
- maxX: (document.scrollingElement || document.body).scrollWidth - window.innerWidth,
65
- maxY: (document.scrollingElement || document.body).scrollHeight - window.innerHeight,
66
- progress: {
67
- x: 0,
68
- y: 0
69
- },
70
- progressX: 0,
71
- progressY: 0,
72
- direction: {
73
- x: "NONE",
74
- y: "NONE"
75
- },
76
- isUp: false,
77
- isRight: false,
78
- isDown: false,
79
- isLeft: false
80
- },
81
- init() {
82
- document.addEventListener("scroll", onScroll, { passive: true, capture: true });
83
- },
84
- kill() {
85
- document.removeEventListener("scroll", onScroll);
86
- }
87
- });
88
- return {
89
- add,
90
- remove,
91
- has,
92
- props: () => props
93
- };
94
- }
95
- let scroll;
96
- function useScroll() {
97
- if (!scroll) {
98
- scroll = createScrollService();
99
- }
100
- return scroll;
101
- }
102
- export {
103
- useScroll as default
104
- };
105
- //# sourceMappingURL=scroll.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../packages/js-toolkit/services/scroll.ts"],
4
- "sourcesContent": ["/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */\nimport { useService } from './service.js';\nimport debounce from '../utils/debounce.js';\nimport type { ServiceInterface } from './index.js';\n\nexport type ScrollService = ServiceInterface<ScrollServiceProps>;\n\nexport interface ScrollServiceProps {\n x: number;\n y: number;\n changed: { x: boolean; y: boolean };\n changedX: boolean;\n changedY: boolean;\n last: { x: number; y: number };\n lastX: number;\n lastY: number;\n delta: { x: number; y: number };\n deltaX: number;\n deltaY: number;\n progress: { x: number; y: number };\n progressX: number;\n progressY: number;\n max: { x: number; y: number };\n maxX: number;\n maxY: number;\n direction: { x: 'LEFT' | 'RIGHT' | 'NONE'; y: 'UP' | 'DOWN' | 'NONE' };\n isUp: boolean;\n isRight: boolean;\n isDown: boolean;\n isLeft: boolean;\n}\n\n/**\n * Create scroll service.\n */\nfunction createScrollService(): ScrollService {\n /**\n * Update props.\n */\n function updateProps(): ScrollServiceProps {\n const yLast = props.y;\n const xLast = props.x;\n\n // Check scroll Y\n if (window.scrollY !== props.y) {\n props.y = window.scrollY;\n }\n\n // Check scroll x\n if (window.scrollX !== props.x) {\n props.x = window.scrollX;\n }\n\n props.changed.x = props.changedX = props.x !== xLast;\n props.changed.y = props.changedY = props.y !== yLast;\n props.last.x = props.lastX = xLast;\n props.last.y = props.lastY = yLast;\n props.delta.x = props.deltaX = props.x - xLast;\n props.delta.y = props.deltaY = props.y - yLast;\n props.max.x = props.maxX = (document.scrollingElement || document.body).scrollWidth - window.innerWidth;\n props.max.y = props.maxY = (document.scrollingElement || document.body).scrollHeight - window.innerHeight;\n props.progress.x = props.progressX = props.max.x === 0 ? 1 : props.x / props.max.x;\n props.progress.y = props.progressY = props.max.y === 0 ? 1 : props.y / props.max.y;\n props.isUp = props.y < yLast;\n props.isRight = props.x > xLast;\n props.isDown = props.y > yLast;\n props.isLeft = props.x < xLast;\n /* eslint-disable no-nested-ternary */\n props.direction.x = props.isRight ? 'RIGHT' : props.isLeft ? 'LEFT' : 'NONE';\n props.direction.y = props.isDown ? 'DOWN' : props.isUp ? 'UP' : 'NONE';\n /* eslint-enable no-nested-ternary */\n\n return props;\n }\n\n const onScrollDebounced = debounce(() => {\n trigger(updateProps());\n }, 100);\n\n /**\n * Scroll handler.\n */\n function onScroll() {\n trigger(updateProps());\n onScrollDebounced();\n }\n\n const { add, remove, has, props, trigger } = useService({\n props: {\n x: window.scrollX,\n y: window.scrollY,\n changed: {\n x: false,\n y: false,\n },\n changedX: false,\n changedY: false,\n last: {\n x: window.scrollX,\n y: window.scrollY,\n },\n lastX: window.scrollX,\n lastY: window.scrollY,\n delta: {\n x: 0,\n y: 0,\n },\n deltaX: 0,\n deltaY: 0,\n max: {\n x: (document.scrollingElement || document.body).scrollWidth - window.innerWidth,\n y: (document.scrollingElement || document.body).scrollHeight - window.innerHeight,\n },\n maxX: (document.scrollingElement || document.body).scrollWidth - window.innerWidth,\n maxY: (document.scrollingElement || document.body).scrollHeight - window.innerHeight,\n progress: {\n x: 0,\n y: 0,\n },\n progressX: 0,\n progressY: 0,\n direction: {\n x: 'NONE',\n y: 'NONE',\n },\n isUp: false,\n isRight: false,\n isDown: false,\n isLeft: false,\n } as ScrollServiceProps,\n init() {\n document.addEventListener('scroll', onScroll, { passive: true, capture: true });\n },\n kill() {\n document.removeEventListener('scroll', onScroll);\n },\n });\n\n return {\n add,\n remove,\n has,\n props: () => props,\n };\n}\n\nlet scroll;\n\n/**\n * Use the scroll service.\n *\n * ```js\n * import { useScroll } from '@studiometa/js-toolkit/services';\n * const { add, remove, props } = useScroll();\n * add(key, (props) => {});\n * remove(key);\n * props();\n * ```\n */\nexport default function useScroll(): ScrollService {\n if (!scroll) {\n scroll = createScrollService();\n }\n\n return scroll;\n}\n"],
5
- "mappings": "AACA,SAAS,kBAAkB;AAC3B,OAAO,cAAc;AAiCrB,SAAS,sBAAqC;AAI5C,WAAS,cAAkC;AACzC,UAAM,QAAQ,MAAM;AACpB,UAAM,QAAQ,MAAM;AAGpB,QAAI,OAAO,YAAY,MAAM,GAAG;AAC9B,YAAM,IAAI,OAAO;AAAA,IACnB;AAGA,QAAI,OAAO,YAAY,MAAM,GAAG;AAC9B,YAAM,IAAI,OAAO;AAAA,IACnB;AAEA,UAAM,QAAQ,IAAI,MAAM,WAAW,MAAM,MAAM;AAC/C,UAAM,QAAQ,IAAI,MAAM,WAAW,MAAM,MAAM;AAC/C,UAAM,KAAK,IAAI,MAAM,QAAQ;AAC7B,UAAM,KAAK,IAAI,MAAM,QAAQ;AAC7B,UAAM,MAAM,IAAI,MAAM,SAAS,MAAM,IAAI;AACzC,UAAM,MAAM,IAAI,MAAM,SAAS,MAAM,IAAI;AACzC,UAAM,IAAI,IAAI,MAAM,QAAQ,SAAS,oBAAoB,SAAS,MAAM,cAAc,OAAO;AAC7F,UAAM,IAAI,IAAI,MAAM,QAAQ,SAAS,oBAAoB,SAAS,MAAM,eAAe,O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Ac,OAAO;AAAA,MACxE,OAAO,SAAS,oBAAoB,SAAS,MAAM,eAAe,OAAO;AAAA,MACzE,UAAU;AAAA,QACR,GAAG;AAAA,QACH,GAAG;AAAA,MACL;AAAA,MACA,WAAW;AAAA,MACX,WAAW;AAAA,MACX,WAAW;AAAA,QACT,GAAG;AAAA,QACH,GAAG;AAAA,MACL;AAAA,MACA,MAAM;AAAA,MACN,SAAS;AAAA,MACT,QAAQ;AAAA,MACR,QAAQ;AAAA,IACV;AAAA,IACA,OAAO;AACL,eAAS,iBAAiB,UAAU,UAAU,EAAE,SAAS,MAAM,SAAS,KAAK,CAAC;AAAA,IAChF;AAAA,IACA,OAAO;AACL,eAAS,oBAAoB,UAAU,QAAQ;AAAA,IACjD;AAAA,EACF,CAAC;AAED,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,OAAO,MAAM;AAAA,EACf;AACF;AAEA,IAAI;AAaW,SAAR,YAA4C;AACjD,MAAI,CAAC,QAAQ;AACX,aAAS,oBAAoB;AAAA,EAC/B;AAEA,SAAO;AACT;",
6
- "names": []
7
- }
@@ -1,16 +0,0 @@
1
- import type { ServiceInterface } from './index.js';
2
- export interface ServiceOptions<T> {
3
- init: () => void;
4
- kill: () => void;
5
- props: T;
6
- }
7
- export interface UseServiceInterface<T> extends Omit<ServiceInterface<T>, 'props'> {
8
- callbacks: Map<string, (props: T) => unknown>;
9
- props: T;
10
- get: (key: string) => (props: T) => unknown;
11
- trigger: (props: T) => unknown;
12
- }
13
- /**
14
- * Use a service.
15
- */
16
- export declare function useService<T>(options: ServiceOptions<T>): UseServiceInterface<T>;
@@ -1,47 +0,0 @@
1
- function useService(options) {
2
- const callbacks = /* @__PURE__ */ new Map();
3
- let isInit = false;
4
- const { init, kill, props } = options;
5
- function has(key) {
6
- return callbacks.has(key);
7
- }
8
- function get(key) {
9
- return callbacks.get(key);
10
- }
11
- function add(key, callback) {
12
- if (has(key)) {
13
- console.warn(`The key \`${key}\` has already been added.`);
14
- return;
15
- }
16
- if (callbacks.size === 0 && !isInit) {
17
- init();
18
- isInit = true;
19
- }
20
- callbacks.set(key, callback);
21
- }
22
- function remove(key) {
23
- callbacks.delete(key);
24
- if (callbacks.size === 0 && isInit) {
25
- kill();
26
- isInit = false;
27
- }
28
- }
29
- function trigger(p) {
30
- for (const callback of callbacks.values()) {
31
- callback(p);
32
- }
33
- }
34
- return {
35
- callbacks,
36
- props,
37
- add,
38
- remove,
39
- has,
40
- get,
41
- trigger
42
- };
43
- }
44
- export {
45
- useService
46
- };
47
- //# sourceMappingURL=service.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../packages/js-toolkit/services/service.ts"],
4
- "sourcesContent": ["// eslint-disable-next-line jsdoc/require-returns\nimport type { ServiceInterface } from './index.js';\n\nexport interface ServiceOptions<T> {\n init: () => void;\n kill: () => void;\n props: T;\n}\n\nexport interface UseServiceInterface<T> extends Omit<ServiceInterface<T>, 'props'> {\n callbacks: Map<string, (props: T) => unknown>;\n props: T;\n get: (key: string) => (props: T) => unknown;\n trigger: (props: T) => unknown;\n}\n\n/**\n * Use a service.\n */\nexport function useService<T>(options: ServiceOptions<T>): UseServiceInterface<T> {\n const callbacks: Map<string, (props: T) => unknown> = new Map();\n let isInit = false;\n\n const { init, kill, props } = options;\n\n /**\n * Does the service has the given key?\n */\n function has(key: string): boolean {\n return callbacks.has(key);\n }\n\n /**\n * Get a service callback by its key.\n */\n function get(key: string): (props: T) => unknown {\n return callbacks.get(key);\n }\n\n /**\n * Add a callback to the service.\n */\n function add(key: string, callback: (props: T) => unknown) {\n if (has(key)) {\n console.warn(`The key \\`${key}\\` has already been added.`);\n return;\n }\n\n // Initialize the service when we add the first callback\n if (callbacks.size === 0 && !isInit) {\n init();\n isInit = true;\n }\n\n callbacks.set(key, callback);\n }\n\n /**\n * Remove a callback from the service by its key.\n */\n function remove(key: string) {\n callbacks.delete(key);\n\n // Kill the service when we remove the last callback\n if (callbacks.size === 0 && isInit) {\n kill();\n isInit = false;\n }\n }\n\n /**\n * Trigger all service callbacks with service props.\n */\n function trigger(p: T) {\n for (const callback of callbacks.values()) {\n callback(p);\n }\n }\n\n return {\n callbacks,\n props,\n add,\n remove,\n has,\n get,\n trigger,\n };\n}\n"],
5
- "mappings": "AAmBO,SAAS,WAAc,SAAoD;AAChF,QAAM,YAAgD,oBAAI,IAAI;AAC9D,MAAI,SAAS;AAEb,QAAM,EAAE,MAAM,MAAM,MAAM,IAAI;AAK9B,WAAS,IAAI,KAAsB;AACjC,WAAO,UAAU,IAAI,GAAG;AAAA,EAC1B;AAKA,WAAS,IAAI,KAAoC;AAC/C,WAAO,UAAU,IAAI,GAAG;AAAA,EAC1B;AAKA,WAAS,IAAI,KAAa,UAAiC;AACzD,QAAI,IAAI,GAAG,GAAG;AACZ,cAAQ,KAAK,aAAa,GAAG,4BAA4B;AACzD;AAAA,IACF;AAGA,QAAI,UAAU,SAAS,KAAK,CAAC,QAAQ;AACnC,WAAK;AACL,eAAS;AAAA,IACX;AAEA,cAAU,IAAI,KAAK,QAAQ;AAAA,EAC7B;AAKA,WAAS,OAAO,KAAa;AAC3B,cAAU,OAAO,GAAG;AAGpB,QAAI,UAAU,SAAS,KAAK,QAAQ;AAClC,WAAK;AACL,eAAS;AAAA,IACX;AAAA,EACF;AAKA,WAAS,QAAQ,GAAM;AACrB,eAAW,YAAY,UAAU,OAAO,GAAG;AACzC,eAAS,CAAC;AAAA,IACZ;AAAA,EACF;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;",
6
- "names": []
7
- }