@studiometa/js-toolkit 2.0.0-alpha.9 → 2.0.0-beta.4

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 (43) hide show
  1. package/Base/index.d.ts +26 -1
  2. package/Base/index.js +66 -15
  3. package/Base/managers/ChildrenManager.js +4 -0
  4. package/Base/managers/EventsManager.js +6 -0
  5. package/Base/managers/OptionsManager.js +3 -0
  6. package/Base/managers/RefsManager.js +4 -0
  7. package/Base/managers/ServicesManager.d.ts +7 -0
  8. package/Base/managers/ServicesManager.js +21 -4
  9. package/Base/utils.d.ts +0 -8
  10. package/Base/utils.js +0 -25
  11. package/README.md +21 -11
  12. package/decorators/index.d.ts +3 -1
  13. package/decorators/index.js +3 -1
  14. package/decorators/withDrag.d.ts +3 -3
  15. package/decorators/withDrag.js +6 -12
  16. package/decorators/withIntersectionObserver.js +1 -4
  17. package/decorators/withMountWhenInView.d.ts +4 -4
  18. package/decorators/withMountWhenInView.js +6 -2
  19. package/decorators/withScrolledInView.d.ts +14 -0
  20. package/decorators/withScrolledInView.js +77 -0
  21. package/decorators/withVue2.d.ts +7 -0
  22. package/decorators/withVue2.js +45 -0
  23. package/index.d.ts +3 -1
  24. package/index.js +2 -2
  25. package/index.modern.js +1 -1
  26. package/index.umd.js +1 -1
  27. package/package.json +1 -1
  28. package/services/drag.d.ts +1 -1
  29. package/services/drag.js +5 -5
  30. package/utils/collide/index.d.ts +4 -4
  31. package/utils/collide/index.js +4 -4
  32. package/utils/history.d.ts +6 -6
  33. package/utils/history.js +6 -2
  34. package/utils/index.d.ts +29 -7
  35. package/utils/index.js +11 -7
  36. package/utils/math/damp.d.ts +6 -5
  37. package/utils/math/damp.js +2 -1
  38. package/utils/math/index.d.ts +2 -1
  39. package/utils/math/index.js +2 -1
  40. package/utils/math/inertiaFinalValue.d.ts +2 -2
  41. package/utils/math/inertiaFinalValue.js +4 -4
  42. package/utils/scrollTo.d.ts +11 -0
  43. package/utils/scrollTo.js +75 -0
package/utils/index.d.ts CHANGED
@@ -1,13 +1,35 @@
1
- import * as collide from "./collide/index.js";
2
- import * as css from "./css/index.js";
1
+ import { matrix } from "./css/index.js";
2
+ import { transition } from "./css/index.js";
3
+ import { collidePointRect } from "./collide/index.js";
4
+ import { collideRectRect } from "./collide/index.js";
5
+ import { boundingRectToCircle } from "./collide/index.js";
6
+ import { collideCircleRect } from "./collide/index.js";
7
+ import { collideCircleCircle } from "./collide/index.js";
8
+ import { collidePointCircle } from "./collide/index.js";
9
+ import { objectToURLSearchParams } from "./history.js";
10
+ import { push as historyPush } from "./history.js";
11
+ import { replace as historyReplace } from "./history.js";
12
+ import { clamp } from "./math/index.js";
13
+ import { clamp01 } from "./math/index.js";
14
+ import { damp } from "./math/index.js";
15
+ import { inertiaFinalValue } from "./math/index.js";
16
+ import { lerp } from "./math/index.js";
17
+ import { map } from "./math/index.js";
18
+ import { round } from "./math/index.js";
19
+ import { withLeadingCharacters } from "./string/index.js";
20
+ import { withLeadingSlash } from "./string/index.js";
21
+ import { withoutLeadingCharacters } from "./string/index.js";
22
+ import { withoutLeadingCharactersRecursive } from "./string/index.js";
23
+ import { withoutLeadingSlash } from "./string/index.js";
24
+ import { withoutTrailingCharacters } from "./string/index.js";
25
+ import { withoutTrailingCharactersRecursive } from "./string/index.js";
26
+ import { withoutTrailingSlash } from "./string/index.js";
27
+ import { withTrailingCharacters } from "./string/index.js";
28
+ import { withTrailingSlash } from "./string/index.js";
3
29
  import debounce from "./debounce.js";
4
30
  import focusTrap from "./focusTrap.js";
5
- import * as history from "./history.js";
6
31
  import keyCodes from "./keyCodes.js";
7
- import * as math from "./math/index.js";
8
32
  import memoize from "./memoize.js";
9
33
  import nextFrame from "./nextFrame.js";
10
- import * as object from "./object/index.js";
11
- import * as string from "./string/index.js";
12
34
  import throttle from "./throttle.js";
13
- export { collide, css, debounce, focusTrap, history, keyCodes, math, memoize, nextFrame, object, string, throttle };
35
+ export { matrix, transition, collidePointRect, collideRectRect, boundingRectToCircle, collideCircleRect, collideCircleCircle, collidePointCircle, objectToURLSearchParams, historyPush, historyReplace, clamp, clamp01, damp, inertiaFinalValue, lerp, map, round, withLeadingCharacters, withLeadingSlash, withoutLeadingCharacters, withoutLeadingCharactersRecursive, withoutLeadingSlash, withoutTrailingCharacters, withoutTrailingCharactersRecursive, withoutTrailingSlash, withTrailingCharacters, withTrailingSlash, debounce, focusTrap, keyCodes, memoize, nextFrame, throttle };
package/utils/index.js CHANGED
@@ -1,13 +1,17 @@
1
- import * as collide from './collide/index.js';
2
- import * as css from './css/index.js';
3
- import * as history from './history.js';
4
- import * as math from './math/index.js';
5
- import * as object from './object/index.js';
6
- import * as string from './string/index.js';
7
1
  import debounce from './debounce.js';
8
2
  import focusTrap from './focusTrap.js';
9
3
  import keyCodes from './keyCodes.js';
10
4
  import memoize from './memoize.js';
11
5
  import nextFrame from './nextFrame.js';
12
6
  import throttle from './throttle.js';
13
- export { collide, css, debounce, focusTrap, history, keyCodes, math, memoize, nextFrame, object, string, throttle };
7
+ import { matrix, transition } from './css/index.js';
8
+ import { collidePointRect, collideRectRect, boundingRectToCircle, collideCircleRect, collideCircleCircle, collidePointCircle } from './collide/index.js';
9
+ import { objectToURLSearchParams, push as historyPush, replace as historyReplace } from './history.js';
10
+ import { clamp, clamp01, damp, inertiaFinalValue, lerp, map, round } from './math/index.js';
11
+ import { withLeadingCharacters, withLeadingSlash, withoutLeadingCharacters, withoutLeadingCharactersRecursive, withoutLeadingSlash, withoutTrailingCharacters, withoutTrailingCharactersRecursive, withoutTrailingSlash, withTrailingCharacters, withTrailingSlash } from './string/index.js';
12
+ export { matrix, transition };
13
+ export { collidePointRect, collideRectRect, boundingRectToCircle, collideCircleRect, collideCircleCircle, collidePointCircle };
14
+ export { objectToURLSearchParams, historyPush, historyReplace };
15
+ export { clamp, clamp01, damp, inertiaFinalValue, lerp, map, round };
16
+ export { withLeadingCharacters, withLeadingSlash, withoutLeadingCharacters, withoutLeadingCharactersRecursive, withoutLeadingSlash, withoutTrailingCharacters, withoutTrailingCharactersRecursive, withoutTrailingSlash, withTrailingCharacters, withTrailingSlash };
17
+ export { debounce, focusTrap, keyCodes, memoize, nextFrame, throttle };
@@ -1,9 +1,10 @@
1
1
  /**
2
2
  * Get the next damped value for a given factor.
3
3
  *
4
- * @param {Number} targetValue The final value.
5
- * @param {Number} currentValue The current value.
6
- * @param {Number=} [factor=0.5] The factor used to reach the target value.
7
- * @return {Number} The next value.
4
+ * @param {number} targetValue The final value.
5
+ * @param {number} currentValue The current value.
6
+ * @param {number=} [factor=0.5] The factor used to reach the target value.
7
+ * @param {number=} [precision=0.01] The precision used to calculate the latest value.
8
+ * @return {number} The next value.
8
9
  */
9
- export default function damp(targetValue: number, currentValue: number, factor?: number | undefined): number;
10
+ export default function damp(targetValue: number, currentValue: number, factor?: number | undefined, precision?: number | undefined): number;
@@ -1,4 +1,5 @@
1
1
  export default function damp(targetValue, currentValue) {
2
2
  var factor = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0.5;
3
- return Math.abs(targetValue - currentValue) < 0.001 ? targetValue : currentValue + (targetValue - currentValue) * factor;
3
+ var precision = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0.01;
4
+ return Math.abs(targetValue - currentValue) < precision ? targetValue : currentValue + (targetValue - currentValue) * factor;
4
5
  }
@@ -1,7 +1,8 @@
1
1
  import clamp from "./clamp.js";
2
2
  import clamp01 from "./clamp01.js";
3
3
  import damp from "./damp.js";
4
+ import inertiaFinalValue from "./inertiaFinalValue.js";
4
5
  import lerp from "./lerp.js";
5
6
  import map from "./map.js";
6
7
  import round from "./round.js";
7
- export { clamp, clamp01, damp, lerp, map, round };
8
+ export { clamp, clamp01, damp, inertiaFinalValue, lerp, map, round };
@@ -1,7 +1,8 @@
1
1
  import clamp from './clamp.js';
2
2
  import clamp01 from './clamp01.js';
3
3
  import damp from './damp.js';
4
+ import inertiaFinalValue from './inertiaFinalValue.js';
4
5
  import lerp from './lerp.js';
5
6
  import map from './map.js';
6
7
  import round from './round.js';
7
- export { clamp, clamp01, damp, lerp, map, round };
8
+ export { clamp, clamp01, damp, inertiaFinalValue, lerp, map, round };
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * @param {Number} initialValue The final value.
5
5
  * @param {Number} initialDelta The current value.
6
- * @param {Number=} [factor=0.85] The speed to reach the target value.
6
+ * @param {Number=} [dampFactor=0.85] The speed to reach the target value.
7
7
  * @return {Number} The next value.
8
8
  */
9
- export default function inertiaFinalValue(initialValue: number, initialDelta: number, factor?: number | undefined): number;
9
+ export default function inertiaFinalValue(initialValue: number, initialDelta: number, dampFactor?: number | undefined): number;
@@ -1,8 +1,8 @@
1
1
  export default function inertiaFinalValue(initialValue, initialDelta) {
2
- var factor = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0.85;
2
+ var dampFactor = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0.85;
3
3
 
4
- if (factor >= 1 || factor <= 0) {
5
- throw new Error("The `factor` parameter must be a number greater than 0 and smaller than 1, `".concat(factor, "` given."));
4
+ if (dampFactor >= 1 || dampFactor <= 0) {
5
+ throw new Error("The `factor` parameter must be a number greater than 0 and smaller than 1, `".concat(dampFactor, "` given."));
6
6
  }
7
7
 
8
8
  var delta = initialDelta;
@@ -10,7 +10,7 @@ export default function inertiaFinalValue(initialValue, initialDelta) {
10
10
 
11
11
  while (Math.abs(delta) > 0.1) {
12
12
  finalValue += delta;
13
- delta *= factor;
13
+ delta *= dampFactor;
14
14
  }
15
15
 
16
16
  return finalValue;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Handler for the click event on anchor links
3
+ *
4
+ * @param {HTMLElement|string} selectorElement The target element.
5
+ * @param {{ offset?: number, dampFactor?: number }} [options] Options for the scroll.
6
+ * @return {Promise<number>} A promising resolving with the target scroll position.
7
+ */
8
+ export default function scrollTo(selectorElement: HTMLElement | string, { offset, dampFactor }?: {
9
+ offset?: number;
10
+ dampFactor?: number;
11
+ }): Promise<number>;
@@ -0,0 +1,75 @@
1
+ import damp from './math/damp.js';
2
+ export default function scrollTo(selectorElement) {
3
+ var {
4
+ offset = 0,
5
+ dampFactor = 0.2
6
+ } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
7
+ var targetElement = null;
8
+
9
+ if (selectorElement instanceof HTMLElement) {
10
+ targetElement = selectorElement;
11
+ } else {
12
+ targetElement = document.querySelector(selectorElement);
13
+ }
14
+
15
+ if (!targetElement) {
16
+ return Promise.resolve(window.pageYOffset);
17
+ }
18
+
19
+ var sizes = targetElement.getBoundingClientRect();
20
+ var scrollMargin = getComputedStyle(targetElement).scrollMarginTop || '0';
21
+ var max = document.body.offsetHeight - window.innerHeight;
22
+ var scrollTarget = sizes.top + window.pageYOffset + parseInt(scrollMargin, 10) + offset;
23
+
24
+ if (scrollTarget > max) {
25
+ scrollTarget = max;
26
+ }
27
+
28
+ return new Promise(resolve => {
29
+ var isScrolling = false;
30
+ var scroll = window.pageYOffset;
31
+ var dampScroll = window.pageYOffset;
32
+
33
+ function eventHandler() {
34
+ isScrolling = false;
35
+ }
36
+
37
+ function end() {
38
+ window.removeEventListener('wheel', eventHandler);
39
+ window.removeEventListener('touchmove', eventHandler);
40
+ resolve(window.pageYOffset);
41
+ }
42
+
43
+ function loop() {
44
+ if (!isScrolling) {
45
+ return end();
46
+ }
47
+
48
+ dampScroll = damp(scroll, dampScroll, dampFactor);
49
+ window.scrollTo({
50
+ top: dampScroll
51
+ });
52
+
53
+ if (dampScroll === scroll) {
54
+ isScrolling = false;
55
+ }
56
+
57
+ return requestAnimationFrame(loop);
58
+ }
59
+
60
+ function start(target) {
61
+ isScrolling = true;
62
+ dampScroll = window.pageYOffset;
63
+ scroll = target;
64
+ window.addEventListener('wheel', eventHandler, {
65
+ passive: true
66
+ });
67
+ window.addEventListener('touchmove', eventHandler, {
68
+ passive: true
69
+ });
70
+ loop();
71
+ }
72
+
73
+ start(scrollTarget);
74
+ });
75
+ }