@studiometa/js-toolkit 2.0.0-beta.5 → 2.0.0-beta.9

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 (61) hide show
  1. package/Base/index.d.ts +27 -1
  2. package/Base/index.js +98 -42
  3. package/Base/managers/EventsManager.d.ts +3 -1
  4. package/Base/managers/EventsManager.js +6 -2
  5. package/Base/managers/OptionsManager.d.ts +2 -0
  6. package/Base/managers/RefsManager.d.ts +2 -0
  7. package/Base/managers/RefsManager.js +5 -0
  8. package/Base/managers/ServicesManager.d.ts +29 -4
  9. package/Base/managers/ServicesManager.js +6 -2
  10. package/README.md +13 -70
  11. package/decorators/index.d.ts +8 -9
  12. package/decorators/index.js +8 -9
  13. package/decorators/withBreakpointObserver.js +2 -2
  14. package/decorators/withDrag.js +8 -2
  15. package/decorators/withIntersectionObserver.js +4 -3
  16. package/decorators/withMountWhenInView.js +7 -4
  17. package/decorators/withScrolledInView.d.ts +4 -1
  18. package/decorators/withScrolledInView.js +56 -39
  19. package/decorators/withVue2.js +2 -2
  20. package/helpers/index.d.ts +4 -5
  21. package/helpers/index.js +4 -5
  22. package/index.d.ts +4 -21
  23. package/index.js +4 -5
  24. package/index.modern.js +1 -1
  25. package/index.umd.js +1 -1
  26. package/package.json +1 -1
  27. package/services/Service.d.ts +22 -17
  28. package/services/Service.js +12 -7
  29. package/services/drag.d.ts +4 -18
  30. package/services/drag.js +81 -104
  31. package/services/index.d.ts +13 -10
  32. package/services/index.js +7 -8
  33. package/services/key.d.ts +4 -13
  34. package/services/key.js +55 -56
  35. package/services/load.d.ts +3 -13
  36. package/services/load.js +32 -26
  37. package/services/pointer.d.ts +4 -13
  38. package/services/pointer.js +61 -64
  39. package/services/raf.d.ts +3 -13
  40. package/services/raf.js +22 -25
  41. package/services/resize.d.ts +3 -13
  42. package/services/resize.js +48 -30
  43. package/services/scroll.d.ts +3 -13
  44. package/services/scroll.js +65 -61
  45. package/utils/collide/index.d.ts +6 -7
  46. package/utils/collide/index.js +6 -7
  47. package/utils/css/index.d.ts +4 -5
  48. package/utils/css/index.js +6 -5
  49. package/utils/css/transition.js +2 -2
  50. package/utils/history.js +2 -2
  51. package/utils/index.d.ts +11 -35
  52. package/utils/index.js +11 -17
  53. package/utils/math/ease.d.ts +78 -0
  54. package/utils/math/ease.js +41 -0
  55. package/utils/math/index.d.ts +8 -8
  56. package/utils/math/index.js +8 -8
  57. package/utils/object/getAllProperties.js +1 -1
  58. package/utils/object/index.d.ts +3 -4
  59. package/utils/object/index.js +3 -4
  60. package/utils/string/index.d.ts +10 -11
  61. package/utils/string/index.js +10 -11
package/services/index.js CHANGED
@@ -1,8 +1,7 @@
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
+ 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';
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
- };
package/services/key.js CHANGED
@@ -1,9 +1,4 @@
1
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
2
  import Service from './Service.js';
8
3
  import keyCodes from '../utils/keyCodes.js';
9
4
 
@@ -11,79 +6,83 @@ class Key extends Service {
11
6
  constructor() {
12
7
  super(...arguments);
13
8
 
14
- _defineProperty(this, "event", {});
15
-
16
- _defineProperty(this, "triggered", 0);
17
-
18
- _defineProperty(this, "previousEvent", {});
9
+ _defineProperty(this, "previousEvent", void 0);
10
+
11
+ _defineProperty(this, "props", {
12
+ event: null,
13
+ triggered: 0,
14
+ isUp: false,
15
+ isDown: false,
16
+ ENTER: false,
17
+ SPACE: false,
18
+ TAB: false,
19
+ ESC: false,
20
+ LEFT: false,
21
+ UP: false,
22
+ RIGHT: false,
23
+ DOWN: false,
24
+ direction: 'none'
25
+ });
19
26
  }
20
27
 
21
28
  init() {
22
- this.handler = event => {
23
- this.event = event;
24
- this.trigger(this.props);
25
- };
29
+ document.addEventListener('keydown', this);
30
+ document.addEventListener('keyup', this);
31
+ }
26
32
 
27
- document.addEventListener('keydown', this.handler, {
28
- passive: false
29
- });
30
- document.addEventListener('keyup', this.handler, {
31
- passive: false
32
- });
33
- return this;
33
+ handleEvent(event) {
34
+ this.updateProps(event);
35
+ this.trigger(this.props);
34
36
  }
35
37
 
36
38
  kill() {
37
- document.removeEventListener('keydown', this.handler);
38
- document.removeEventListener('keyup', this.handler);
39
- return this;
39
+ document.removeEventListener('keydown', this);
40
+ document.removeEventListener('keyup', this);
40
41
  }
41
42
 
42
- get props() {
43
- var keys = Object.entries(keyCodes).reduce((acc, _ref) => {
43
+ updateProps(event) {
44
+ var _this$previousEvent;
45
+
46
+ this.props.event = event;
47
+ Object.entries(keyCodes).forEach(_ref => {
44
48
  var [name, code] = _ref;
45
- acc[name] = code === this.event.keyCode;
46
- return acc;
47
- }, {});
49
+ this.props[name] = code === event.keyCode;
50
+ });
48
51
 
49
- if (!this.previousEvent.type) {
50
- this.triggered = 0;
52
+ if (!this.previousEvent) {
53
+ this.props.triggered = 0;
51
54
  }
52
55
 
53
- if (this.event.type === 'keydown' && this.previousEvent.type === 'keydown') {
54
- this.triggered += 1;
56
+ if (this.props.event.type === 'keydown' && ((_this$previousEvent = this.previousEvent) === null || _this$previousEvent === void 0 ? void 0 : _this$previousEvent.type) === 'keydown') {
57
+ this.props.triggered += 1;
55
58
  } else {
56
- this.triggered = 1;
59
+ this.props.triggered = 1;
57
60
  }
58
61
 
59
- this.previousEvent = this.event;
60
- return _objectSpread({
61
- event: this.event,
62
- triggered: this.triggered,
63
- direction: this.event.type === 'keydown' ? 'down' : 'up',
64
- isUp: this.event.type === 'keyup',
65
- isDown: this.event.type === 'keydown'
66
- }, keys);
62
+ this.previousEvent = this.props.event;
63
+ this.props.direction = this.props.event.type === 'keydown' ? 'down' : 'up';
64
+ this.props.isUp = this.props.event.type === 'keyup';
65
+ this.props.isDown = this.props.event.type === 'keydown';
66
+ return this.props;
67
67
  }
68
68
 
69
69
  }
70
70
 
71
- var key = null;
71
+ var instance;
72
+ var key;
72
73
  export default function useKey() {
73
74
  if (!key) {
74
- key = new Key();
75
- }
76
-
77
- var add = key.add.bind(key);
78
- var remove = key.remove.bind(key);
79
- var has = key.has.bind(key);
75
+ if (!instance) {
76
+ instance = new Key();
77
+ }
80
78
 
81
- var props = () => key.props;
79
+ key = {
80
+ add: instance.add.bind(instance),
81
+ remove: instance.remove.bind(instance),
82
+ has: instance.has.bind(instance),
83
+ props: instance.updateProps.bind(instance)
84
+ };
85
+ }
82
86
 
83
- return {
84
- add,
85
- remove,
86
- has,
87
- props
88
- };
87
+ return key;
89
88
  }
@@ -9,20 +9,10 @@
9
9
  * props();
10
10
  * ```
11
11
  *
12
- * @return {ServiceInterface & LoadService}
12
+ * @return {LoadService}
13
13
  */
14
- export default function useLoad(): ServiceInterface & LoadService;
15
- export type ServiceInterface = import('./index').ServiceInterface;
14
+ export default function useLoad(): LoadService;
15
+ export type LoadService = import('./index').ServiceInterface<LoadServiceProps>;
16
16
  export type LoadServiceProps = {
17
17
  time: DOMHighResTimeStamp;
18
18
  };
19
- export type LoadService = {
20
- /**
21
- * Add a function to the resize service. The key must be uniq.
22
- */
23
- add: (key: string, callback: (props: LoadServiceProps) => void) => void;
24
- /**
25
- * Get the current values of the resize service props.
26
- */
27
- props: () => LoadServiceProps;
28
- };
package/services/load.js CHANGED
@@ -1,44 +1,50 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
1
2
  import Service from './Service.js';
2
3
 
3
4
  class Load extends Service {
5
+ constructor() {
6
+ super(...arguments);
7
+
8
+ _defineProperty(this, "props", {
9
+ time: window.performance.now()
10
+ });
11
+ }
12
+
4
13
  init() {
5
- this.handler = () => {
6
- this.trigger(this.props);
7
- };
14
+ window.addEventListener('load', this);
15
+ }
8
16
 
9
- window.addEventListener('load', this.handler);
10
- return this;
17
+ handleEvent() {
18
+ this.updateProps();
19
+ this.trigger(this.props);
11
20
  }
12
21
 
13
22
  kill() {
14
- window.removeEventListener('load', this.handler);
15
- return this;
23
+ window.removeEventListener('load', this);
16
24
  }
17
25
 
18
- get props() {
19
- return {};
26
+ updateProps() {
27
+ this.props.time = window.performance.now();
28
+ return this.props;
20
29
  }
21
30
 
22
31
  }
23
32
 
24
- var instance = null;
33
+ var instance;
34
+ var load;
25
35
  export default function useLoad() {
26
- if (!instance) {
27
- instance = new Load();
28
- }
29
-
30
- var add = instance.add.bind(instance);
31
- var remove = instance.remove.bind(instance);
32
- var has = instance.has.bind(instance);
33
-
34
- function props() {
35
- return instance.props;
36
+ if (!load) {
37
+ if (!instance) {
38
+ instance = new Load();
39
+ }
40
+
41
+ load = {
42
+ add: instance.add.bind(instance),
43
+ remove: instance.remove.bind(instance),
44
+ has: instance.has.bind(instance),
45
+ props: instance.updateProps.bind(instance)
46
+ };
36
47
  }
37
48
 
38
- return {
39
- add,
40
- remove,
41
- has,
42
- props
43
- };
49
+ return load;
44
50
  }
@@ -9,10 +9,11 @@
9
9
  * props();
10
10
  * ```
11
11
  *
12
- * @return {ServiceInterface & PointerService}
12
+ * @todo Add element as parameter to get the pointer position relatively from.
13
+ * @return {PointerService}
13
14
  */
14
- export default function usePointer(): ServiceInterface & PointerService;
15
- export type ServiceInterface = import('./index').ServiceInterface;
15
+ export default function usePointer(): PointerService;
16
+ export type PointerService = import('./index').ServiceInterface<PointerServiceProps>;
16
17
  export type PointerServiceProps = {
17
18
  event: MouseEvent | TouchEvent;
18
19
  isDown: boolean;
@@ -39,13 +40,3 @@ export type PointerServiceProps = {
39
40
  y: number;
40
41
  };
41
42
  };
42
- export type PointerService = {
43
- /**
44
- * Add a function to the resize service. The key must be uniq.
45
- */
46
- add: (key: string, callback: (props: PointerServiceProps) => void) => void;
47
- /**
48
- * Get the current values of the resize service props.
49
- */
50
- props: () => PointerServiceProps;
51
- };
@@ -9,17 +9,32 @@ class Pointer extends Service {
9
9
  constructor() {
10
10
  super(...arguments);
11
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);
12
+ _defineProperty(this, "props", {
13
+ event: null,
14
+ isDown: false,
15
+ x: window.innerWidth / 2,
16
+ y: window.innerHeight / 2,
17
+ changed: {
18
+ x: false,
19
+ y: false
20
+ },
21
+ last: {
22
+ x: window.innerWidth / 2,
23
+ y: window.innerHeight / 2
24
+ },
25
+ delta: {
26
+ x: 0,
27
+ y: 0
28
+ },
29
+ progress: {
30
+ x: 0.5,
31
+ y: 0.5
32
+ },
33
+ max: {
34
+ x: window.innerWidth,
35
+ y: window.innerHeight
36
+ }
37
+ });
23
38
 
24
39
  _defineProperty(this, "hasRaf", false);
25
40
  }
@@ -73,86 +88,68 @@ class Pointer extends Service {
73
88
  }
74
89
 
75
90
  handler(event) {
76
- this.updateValues(event);
91
+ this.updateProps(event);
77
92
  this.trigger(this.props);
78
93
  }
79
94
 
80
95
  downHandler() {
81
- this.isDown = true;
96
+ this.props.isDown = true;
82
97
  this.trigger(this.props);
83
98
  }
84
99
 
85
100
  upHandler() {
86
- this.isDown = false;
101
+ this.props.isDown = false;
87
102
  this.trigger(this.props);
88
103
  }
89
104
 
90
- updateValues(event) {
105
+ updateProps(event) {
91
106
  var _event$touches$, _event$touches$2;
92
107
 
93
- this.event = event;
94
- this.yLast = this.y;
95
- this.xLast = this.x;
108
+ this.props.event = event;
109
+ var yLast = this.props.y;
110
+ var xLast = this.props.x;
96
111
  var y = isTouchEvent(event) ? (_event$touches$ = event.touches[0]) === null || _event$touches$ === void 0 ? void 0 : _event$touches$.clientY : event.clientY;
97
112
 
98
- if (y !== this.y) {
99
- this.y = y;
113
+ if (y !== this.props.y) {
114
+ this.props.y = y;
100
115
  }
101
116
 
102
117
  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
118
 
104
- if (x !== this.x) {
105
- this.x = x;
119
+ if (x !== this.props.x) {
120
+ this.props.x = x;
106
121
  }
107
- }
108
122
 
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
- };
123
+ this.props.changed.x = this.props.x !== xLast;
124
+ this.props.changed.y = this.props.y !== yLast;
125
+ this.props.last.x = xLast;
126
+ this.props.last.y = yLast;
127
+ this.props.delta.x = this.props.x - xLast;
128
+ this.props.delta.y = this.props.y - yLast;
129
+ this.props.max.x = window.innerWidth;
130
+ this.props.max.y = window.innerHeight;
131
+ this.props.progress.x = this.props.x / this.props.max.x;
132
+ this.props.progress.y = this.props.y / this.props.max.y;
133
+ return this.props;
136
134
  }
137
135
 
138
136
  }
139
137
 
140
- var pointer = null;
138
+ var instance;
139
+ var pointer;
141
140
  export default function usePointer() {
142
141
  if (!pointer) {
143
- pointer = new Pointer();
144
- }
145
-
146
- var add = pointer.add.bind(pointer);
147
- var remove = pointer.remove.bind(pointer);
148
- var has = pointer.has.bind(pointer);
142
+ if (!instance) {
143
+ instance = new Pointer();
144
+ }
149
145
 
150
- var props = () => pointer.props;
146
+ pointer = {
147
+ add: instance.add.bind(instance),
148
+ remove: instance.remove.bind(instance),
149
+ has: instance.has.bind(instance),
150
+ props: instance.updateProps.bind(instance)
151
+ };
152
+ }
151
153
 
152
- return {
153
- add,
154
- remove,
155
- has,
156
- props
157
- };
154
+ return pointer;
158
155
  }
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
@@ -4,7 +4,7 @@ import { getRaf, getCancelRaf } from '../utils/nextFrame.js';
4
4
 
5
5
  class Raf extends Service {
6
6
  constructor() {
7
- super();
7
+ super(...arguments);
8
8
 
9
9
  _defineProperty(this, "isTicking", false);
10
10
 
@@ -14,57 +14,54 @@ class Raf extends Service {
14
14
 
15
15
  _defineProperty(this, "cancelRaf", getCancelRaf());
16
16
 
17
- this.loop = this.loop.bind(this);
17
+ _defineProperty(this, "props", {
18
+ time: performance.now()
19
+ });
18
20
  }
19
21
 
20
22
  init() {
21
23
  this.isTicking = true;
22
24
  this.loop();
23
- return this;
24
25
  }
25
26
 
26
27
  loop() {
28
+ this.updateProps();
27
29
  this.trigger(this.props);
28
30
 
29
31
  if (!this.isTicking) {
30
32
  return;
31
33
  }
32
34
 
33
- this.id = this.raf(this.loop);
35
+ this.id = this.raf(() => this.loop());
34
36
  }
35
37
 
36
38
  kill() {
37
39
  this.cancelRaf(this.id);
38
40
  this.isTicking = false;
39
- return this;
40
41
  }
41
42
 
42
- get props() {
43
- return {
44
- time: window.performance.now()
45
- };
43
+ updateProps() {
44
+ this.props.time = window.performance.now();
45
+ return this.props;
46
46
  }
47
47
 
48
48
  }
49
49
 
50
- var instance = null;
50
+ var instance;
51
+ var raf;
51
52
  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);
53
+ if (!raf) {
54
+ if (!instance) {
55
+ instance = new Raf();
56
+ }
59
57
 
60
- function props() {
61
- return instance.props;
58
+ raf = {
59
+ add: instance.add.bind(instance),
60
+ remove: instance.remove.bind(instance),
61
+ has: instance.has.bind(instance),
62
+ props: instance.updateProps.bind(instance)
63
+ };
62
64
  }
63
65
 
64
- return {
65
- add,
66
- remove,
67
- has,
68
- props
69
- };
66
+ return raf;
70
67
  }
@@ -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
- };