@studiometa/js-toolkit 2.2.2 → 2.4.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 (116) hide show
  1. package/Base/index.cjs +29 -37
  2. package/Base/index.d.ts +0 -8
  3. package/Base/index.js +18 -26
  4. package/Base/managers/EventsManager.cjs +35 -36
  5. package/Base/managers/EventsManager.js +35 -36
  6. package/Base/managers/OptionsManager.cjs +19 -13
  7. package/Base/managers/OptionsManager.js +19 -13
  8. package/Base/managers/RefsManager.cjs +8 -5
  9. package/Base/managers/RefsManager.js +8 -5
  10. package/Base/managers/ResponsiveOptionsManager.cjs +4 -1
  11. package/Base/managers/ResponsiveOptionsManager.js +4 -1
  12. package/Base/managers/ServicesManager.cjs +9 -7
  13. package/Base/managers/ServicesManager.js +9 -7
  14. package/Base/utils.cjs +22 -4
  15. package/Base/utils.d.ts +25 -8
  16. package/Base/utils.js +22 -4
  17. package/README.md +3 -3
  18. package/decorators/withBreakpointManager.cjs +15 -5
  19. package/decorators/withBreakpointManager.js +15 -5
  20. package/decorators/withBreakpointObserver.cjs +8 -2
  21. package/decorators/withBreakpointObserver.js +8 -2
  22. package/decorators/withVue2.cjs +10 -3
  23. package/decorators/withVue2.js +10 -3
  24. package/helpers/getDirectChildren.cjs +46 -0
  25. package/helpers/getDirectChildren.d.ts +24 -0
  26. package/helpers/getDirectChildren.js +24 -0
  27. package/helpers/importOnInteraction.cjs +2 -1
  28. package/helpers/importOnInteraction.js +2 -1
  29. package/helpers/importWhenVisible.cjs +2 -1
  30. package/helpers/importWhenVisible.js +2 -1
  31. package/helpers/index.cjs +2 -0
  32. package/helpers/index.d.ts +1 -0
  33. package/helpers/index.js +1 -0
  34. package/helpers/utils.cjs +3 -2
  35. package/helpers/utils.js +3 -2
  36. package/package.json +4 -1
  37. package/services/drag.cjs +126 -148
  38. package/services/drag.d.ts +10 -10
  39. package/services/drag.js +125 -148
  40. package/services/index.cjs +3 -1
  41. package/services/index.d.ts +1 -0
  42. package/services/index.js +3 -1
  43. package/services/key.cjs +48 -63
  44. package/services/key.d.ts +0 -8
  45. package/services/key.js +47 -63
  46. package/services/load.cjs +24 -40
  47. package/services/load.d.ts +0 -8
  48. package/services/load.js +23 -37
  49. package/services/pointer.cjs +73 -103
  50. package/services/pointer.d.ts +1 -9
  51. package/services/pointer.js +72 -100
  52. package/services/raf.cjs +38 -58
  53. package/services/raf.d.ts +0 -8
  54. package/services/raf.js +37 -55
  55. package/services/resize.cjs +59 -68
  56. package/services/resize.d.ts +0 -7
  57. package/services/resize.js +58 -68
  58. package/services/scroll.cjs +49 -71
  59. package/services/scroll.js +48 -71
  60. package/services/service.cjs +68 -0
  61. package/services/service.d.ts +18 -0
  62. package/services/service.js +46 -0
  63. package/utils/css/animate.cjs +31 -96
  64. package/utils/css/animate.d.ts +4 -16
  65. package/utils/css/animate.js +28 -93
  66. package/utils/css/classes.cjs +2 -1
  67. package/utils/css/classes.js +2 -1
  68. package/utils/css/styles.cjs +2 -5
  69. package/utils/css/styles.js +1 -1
  70. package/utils/css/transform.cjs +13 -16
  71. package/utils/css/transform.js +1 -1
  72. package/utils/css/transition.cjs +5 -3
  73. package/utils/css/transition.js +5 -3
  74. package/utils/focusTrap.cjs +2 -1
  75. package/utils/focusTrap.js +2 -1
  76. package/utils/{isFunction.cjs → has.cjs} +7 -7
  77. package/utils/has.d.ts +6 -0
  78. package/utils/has.js +6 -0
  79. package/utils/history.cjs +5 -7
  80. package/utils/history.js +4 -3
  81. package/utils/index.cjs +6 -5
  82. package/utils/index.d.ts +4 -2
  83. package/utils/index.js +6 -5
  84. package/utils/{object/isObject.cjs → is.cjs} +20 -8
  85. package/utils/is.d.ts +8 -0
  86. package/utils/is.js +18 -0
  87. package/utils/math/inertiaFinalValue.cjs +5 -3
  88. package/utils/math/inertiaFinalValue.js +2 -3
  89. package/utils/nextFrame.cjs +5 -3
  90. package/utils/nextFrame.js +5 -3
  91. package/utils/nextMicrotask.cjs +2 -1
  92. package/utils/nextMicrotask.js +2 -1
  93. package/utils/nextTick.cjs +2 -1
  94. package/utils/nextTick.js +2 -1
  95. package/utils/{isDefined.cjs → noop.cjs} +10 -7
  96. package/utils/noop.d.ts +12 -0
  97. package/utils/noop.js +9 -0
  98. package/utils/object/autoBind.cjs +3 -2
  99. package/utils/object/autoBind.js +3 -2
  100. package/utils/object/getAllProperties.cjs +2 -1
  101. package/utils/object/getAllProperties.js +2 -1
  102. package/utils/object/index.cjs +1 -3
  103. package/utils/object/index.d.ts +0 -1
  104. package/utils/object/index.js +1 -3
  105. package/utils/tween.cjs +114 -0
  106. package/utils/tween.d.ts +25 -0
  107. package/utils/tween.js +89 -0
  108. package/services/Service.cjs +0 -76
  109. package/services/Service.d.ts +0 -76
  110. package/services/Service.js +0 -55
  111. package/utils/isDefined.d.ts +0 -7
  112. package/utils/isDefined.js +0 -6
  113. package/utils/isFunction.d.ts +0 -7
  114. package/utils/isFunction.js +0 -6
  115. package/utils/object/isObject.d.ts +0 -7
  116. package/utils/object/isObject.js +0 -6
package/utils/is.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export const isDev: boolean;
2
+ export function isFunction(value: unknown): value is Function;
3
+ export function isDefined(value: unknown): boolean;
4
+ export function isString(value: unknown): value is string;
5
+ export function isObject(value: unknown): boolean;
6
+ export function isNumber(value: unknown): value is number;
7
+ export function isBoolean(value: unknown): value is boolean;
8
+ export const isArray: (arg: any) => arg is any[];
package/utils/is.js ADDED
@@ -0,0 +1,18 @@
1
+ const isDev = typeof __DEV__ !== "undefined" && __DEV__;
2
+ const isFunction = (value) => typeof value === "function";
3
+ const isDefined = (value) => typeof value !== "undefined";
4
+ const isString = (value) => typeof value === "string";
5
+ const isObject = (value) => typeof value === "object" && !!value && value.toString() === "[object Object]";
6
+ const isNumber = (value) => typeof value === "number";
7
+ const isBoolean = (value) => typeof value === "boolean";
8
+ const isArray = Array.isArray;
9
+ export {
10
+ isArray,
11
+ isBoolean,
12
+ isDefined,
13
+ isDev,
14
+ isFunction,
15
+ isNumber,
16
+ isObject,
17
+ isString
18
+ };
@@ -1,6 +1,8 @@
1
+ var __create = Object.create;
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
7
  var __export = (target, all) => {
6
8
  for (var name in all)
@@ -14,6 +16,7 @@ var __copyProps = (to, from, except, desc) => {
14
16
  }
15
17
  return to;
16
18
  };
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));
17
20
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
21
 
19
22
  // packages/js-toolkit/utils/math/inertiaFinalValue.js
@@ -22,10 +25,9 @@ __export(inertiaFinalValue_exports, {
22
25
  default: () => inertiaFinalValue
23
26
  });
24
27
  module.exports = __toCommonJS(inertiaFinalValue_exports);
28
+ var import_clamp = __toESM(require("./clamp.cjs"), 1);
25
29
  function inertiaFinalValue(initialValue, initialDelta, dampFactor = 0.85) {
26
- if (dampFactor >= 1 || dampFactor <= 0) {
27
- throw new Error(`The \`factor\` parameter must be a number greater than 0 and smaller than 1, \`${dampFactor}\` given.`);
28
- }
30
+ dampFactor = (0, import_clamp.default)(dampFactor, 1e-5, 0.99999);
29
31
  let delta = initialDelta;
30
32
  let finalValue = initialValue;
31
33
  while (Math.abs(delta) > 0.1) {
@@ -1,7 +1,6 @@
1
+ import clamp from "./clamp.js";
1
2
  function inertiaFinalValue(initialValue, initialDelta, dampFactor = 0.85) {
2
- if (dampFactor >= 1 || dampFactor <= 0) {
3
- throw new Error(`The \`factor\` parameter must be a number greater than 0 and smaller than 1, \`${dampFactor}\` given.`);
4
- }
3
+ dampFactor = clamp(dampFactor, 1e-5, 0.99999);
5
4
  let delta = initialDelta;
6
5
  let finalValue = initialValue;
7
6
  while (Math.abs(delta) > 0.1) {
@@ -24,15 +24,17 @@ __export(nextFrame_exports, {
24
24
  nextFrame: () => nextFrame
25
25
  });
26
26
  module.exports = __toCommonJS(nextFrame_exports);
27
+ var import_is = require("./is.cjs");
28
+ var import_has = require("./has.cjs");
27
29
  function getRaf() {
28
- return typeof window !== "undefined" && window.requestAnimationFrame ? window.requestAnimationFrame.bind(window) : setTimeout;
30
+ return (0, import_has.hasWindow)() && window.requestAnimationFrame ? window.requestAnimationFrame.bind(window) : setTimeout;
29
31
  }
30
32
  function getCancelRaf() {
31
- return typeof window !== "undefined" && window.cancelAnimationFrame ? window.cancelAnimationFrame.bind(window) : clearTimeout;
33
+ return (0, import_has.hasWindow)() && window.cancelAnimationFrame ? window.cancelAnimationFrame.bind(window) : clearTimeout;
32
34
  }
33
35
  function nextFrame(fn) {
34
36
  return new Promise((resolve) => {
35
- getRaf()(() => resolve(typeof fn === "function" && fn()));
37
+ getRaf()(() => resolve((0, import_is.isFunction)(fn) && fn()));
36
38
  });
37
39
  }
38
40
  if (module.exports.default) module.exports = module.exports.default;
@@ -1,12 +1,14 @@
1
+ import { isFunction } from "./is.js";
2
+ import { hasWindow } from "./has.js";
1
3
  function getRaf() {
2
- return typeof window !== "undefined" && window.requestAnimationFrame ? window.requestAnimationFrame.bind(window) : setTimeout;
4
+ return hasWindow() && window.requestAnimationFrame ? window.requestAnimationFrame.bind(window) : setTimeout;
3
5
  }
4
6
  function getCancelRaf() {
5
- return typeof window !== "undefined" && window.cancelAnimationFrame ? window.cancelAnimationFrame.bind(window) : clearTimeout;
7
+ return hasWindow() && window.cancelAnimationFrame ? window.cancelAnimationFrame.bind(window) : clearTimeout;
6
8
  }
7
9
  function nextFrame(fn) {
8
10
  return new Promise((resolve) => {
9
- getRaf()(() => resolve(typeof fn === "function" && fn()));
11
+ getRaf()(() => resolve(isFunction(fn) && fn()));
10
12
  });
11
13
  }
12
14
  export {
@@ -22,7 +22,8 @@ __export(nextMicrotask_exports, {
22
22
  default: () => nextMicrotask
23
23
  });
24
24
  module.exports = __toCommonJS(nextMicrotask_exports);
25
+ var import_is = require("./is.cjs");
25
26
  async function nextMicrotask(fn) {
26
- return Promise.resolve().then(() => typeof fn === "function" && fn());
27
+ return Promise.resolve().then(() => (0, import_is.isFunction)(fn) && fn());
27
28
  }
28
29
  if (module.exports.default) module.exports = module.exports.default;
@@ -1,5 +1,6 @@
1
+ import { isFunction } from "./is.js";
1
2
  async function nextMicrotask(fn) {
2
- return Promise.resolve().then(() => typeof fn === "function" && fn());
3
+ return Promise.resolve().then(() => isFunction(fn) && fn());
3
4
  }
4
5
  export {
5
6
  nextMicrotask as default
@@ -22,9 +22,10 @@ __export(nextTick_exports, {
22
22
  default: () => nextTick
23
23
  });
24
24
  module.exports = __toCommonJS(nextTick_exports);
25
+ var import_is = require("./is.cjs");
25
26
  async function nextTick(fn) {
26
27
  return new Promise((resolve) => setTimeout(() => {
27
- resolve(typeof fn === "function" && fn());
28
+ resolve((0, import_is.isFunction)(fn) && fn());
28
29
  }, 0));
29
30
  }
30
31
  if (module.exports.default) module.exports = module.exports.default;
package/utils/nextTick.js CHANGED
@@ -1,6 +1,7 @@
1
+ import { isFunction } from "./is.js";
1
2
  async function nextTick(fn) {
2
3
  return new Promise((resolve) => setTimeout(() => {
3
- resolve(typeof fn === "function" && fn());
4
+ resolve(isFunction(fn) && fn());
4
5
  }, 0));
5
6
  }
6
7
  export {
@@ -16,13 +16,16 @@ var __copyProps = (to, from, except, desc) => {
16
16
  };
17
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
18
 
19
- // packages/js-toolkit/utils/isDefined.js
20
- var isDefined_exports = {};
21
- __export(isDefined_exports, {
22
- default: () => isDefined
19
+ // packages/js-toolkit/utils/noop.js
20
+ var noop_exports = {};
21
+ __export(noop_exports, {
22
+ noop: () => noop,
23
+ noopValue: () => noopValue
23
24
  });
24
- module.exports = __toCommonJS(isDefined_exports);
25
- function isDefined(value) {
26
- return typeof value !== "undefined";
25
+ module.exports = __toCommonJS(noop_exports);
26
+ function noop() {
27
+ }
28
+ function noopValue(value) {
29
+ return value;
27
30
  }
28
31
  if (module.exports.default) module.exports = module.exports.default;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * No operation function.
3
+ * @returns {void}
4
+ */
5
+ export function noop(): void;
6
+ /**
7
+ * No operation function which return the given value unaltered.
8
+ * @template {any} T
9
+ * @param {T} value
10
+ * @returns {T}
11
+ */
12
+ export function noopValue<T extends unknown>(value: T): T;
package/utils/noop.js ADDED
@@ -0,0 +1,9 @@
1
+ function noop() {
2
+ }
3
+ function noopValue(value) {
4
+ return value;
5
+ }
6
+ export {
7
+ noop,
8
+ noopValue
9
+ };
@@ -26,10 +26,11 @@ __export(autoBind_exports, {
26
26
  });
27
27
  module.exports = __toCommonJS(autoBind_exports);
28
28
  var import_getAllProperties = __toESM(require("./getAllProperties.cjs"), 1);
29
+ var import_is = require("../is.cjs");
29
30
  function autoBind(instance, options) {
30
31
  const { exclude, include } = options || {};
31
32
  const filter = (key) => {
32
- const match = (pattern) => typeof pattern === "string" ? key === pattern : pattern.test(key);
33
+ const match = (pattern) => (0, import_is.isString)(pattern) ? key === pattern : pattern.test(key);
33
34
  if (include) {
34
35
  return include.some(match);
35
36
  }
@@ -40,7 +41,7 @@ function autoBind(instance, options) {
40
41
  };
41
42
  (0, import_getAllProperties.default)(instance).filter(([key]) => key !== "constructor" && filter(key)).forEach(([key, object]) => {
42
43
  const descriptor = Object.getOwnPropertyDescriptor(object, key);
43
- if (descriptor && typeof descriptor.value === "function") {
44
+ if (descriptor && (0, import_is.isFunction)(descriptor.value)) {
44
45
  instance[key] = instance[key].bind(instance);
45
46
  }
46
47
  });
@@ -1,8 +1,9 @@
1
1
  import getAllProperties from "./getAllProperties.js";
2
+ import { isString, isFunction } from "../is.js";
2
3
  function autoBind(instance, options) {
3
4
  const { exclude, include } = options || {};
4
5
  const filter = (key) => {
5
- const match = (pattern) => typeof pattern === "string" ? key === pattern : pattern.test(key);
6
+ const match = (pattern) => isString(pattern) ? key === pattern : pattern.test(key);
6
7
  if (include) {
7
8
  return include.some(match);
8
9
  }
@@ -13,7 +14,7 @@ function autoBind(instance, options) {
13
14
  };
14
15
  getAllProperties(instance).filter(([key]) => key !== "constructor" && filter(key)).forEach(([key, object]) => {
15
16
  const descriptor = Object.getOwnPropertyDescriptor(object, key);
16
- if (descriptor && typeof descriptor.value === "function") {
17
+ if (descriptor && isFunction(descriptor.value)) {
17
18
  instance[key] = instance[key].bind(instance);
18
19
  }
19
20
  });
@@ -22,13 +22,14 @@ __export(getAllProperties_exports, {
22
22
  default: () => getAllProperties
23
23
  });
24
24
  module.exports = __toCommonJS(getAllProperties_exports);
25
+ var import_is = require("../is.cjs");
25
26
  function getAllProperties(object, props = [], testFn = null) {
26
27
  const proto = Object.getPrototypeOf(object);
27
28
  if (proto === Object.prototype || proto === null) {
28
29
  return props;
29
30
  }
30
31
  let foundProps = Object.getOwnPropertyNames(proto);
31
- if (typeof testFn === "function") {
32
+ if ((0, import_is.isFunction)(testFn)) {
32
33
  foundProps = foundProps.filter((name) => testFn(name, proto));
33
34
  }
34
35
  foundProps = foundProps.map((name) => [name, proto]).reduce((acc, val) => [...acc, val], props);
@@ -1,10 +1,11 @@
1
+ import { isFunction } from "../is.js";
1
2
  function getAllProperties(object, props = [], testFn = null) {
2
3
  const proto = Object.getPrototypeOf(object);
3
4
  if (proto === Object.prototype || proto === null) {
4
5
  return props;
5
6
  }
6
7
  let foundProps = Object.getOwnPropertyNames(proto);
7
- if (typeof testFn === "function") {
8
+ if (isFunction(testFn)) {
8
9
  foundProps = foundProps.filter((name) => testFn(name, proto));
9
10
  }
10
11
  foundProps = foundProps.map((name) => [name, proto]).reduce((acc, val) => [...acc, val], props);
@@ -23,11 +23,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
23
23
  var object_exports = {};
24
24
  __export(object_exports, {
25
25
  autoBind: () => import_autoBind.default,
26
- getAllProperties: () => import_getAllProperties.default,
27
- isObject: () => import_isObject.default
26
+ getAllProperties: () => import_getAllProperties.default
28
27
  });
29
28
  module.exports = __toCommonJS(object_exports);
30
29
  var import_autoBind = __toESM(require("./autoBind.cjs"), 1);
31
30
  var import_getAllProperties = __toESM(require("./getAllProperties.cjs"), 1);
32
- var import_isObject = __toESM(require("./isObject.cjs"), 1);
33
31
  if (module.exports.default) module.exports = module.exports.default;
@@ -1,3 +1,2 @@
1
1
  export { default as autoBind } from "./autoBind.js";
2
2
  export { default as getAllProperties } from "./getAllProperties.js";
3
- export { default as isObject } from "./isObject.js";
@@ -1,8 +1,6 @@
1
1
  import { default as default2 } from "./autoBind.js";
2
2
  import { default as default3 } from "./getAllProperties.js";
3
- import { default as default4 } from "./isObject.js";
4
3
  export {
5
4
  default2 as autoBind,
6
- default3 as getAllProperties,
7
- default4 as isObject
5
+ default3 as getAllProperties
8
6
  };
@@ -0,0 +1,114 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
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));
20
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
+
22
+ // packages/js-toolkit/utils/tween.js
23
+ var tween_exports = {};
24
+ __export(tween_exports, {
25
+ normalizeEase: () => normalizeEase,
26
+ tween: () => tween
27
+ });
28
+ module.exports = __toCommonJS(tween_exports);
29
+ var import_easing = require("@motionone/easing");
30
+ var import_math = require("./math/index.cjs");
31
+ var import_is = require("./is.cjs");
32
+ var import_noop = require("./noop.cjs");
33
+ var import_raf = __toESM(require("../services/raf.cjs"), 1);
34
+ var id = 0;
35
+ var PROGRESS_PRECISION = 1e-4;
36
+ function normalizeEase(ease) {
37
+ if (!(0, import_is.isDefined)(ease)) {
38
+ return import_noop.noopValue;
39
+ }
40
+ if ((0, import_is.isArray)(ease)) {
41
+ return (0, import_easing.cubicBezier)(...ease);
42
+ }
43
+ return ease;
44
+ }
45
+ function tween(callback, options = {}) {
46
+ var _a;
47
+ const raf = (0, import_raf.default)();
48
+ let progressValue = 0;
49
+ let easedProgress = 0;
50
+ const ease = normalizeEase(options.easing);
51
+ let duration = (_a = options.duration) != null ? _a : 1;
52
+ duration *= 1e3;
53
+ let startTime = performance.now();
54
+ let endTime = startTime + duration;
55
+ const key = `tw-${id}`;
56
+ id += 1;
57
+ const { onStart = import_noop.noop, onProgress = import_noop.noop, onFinish = import_noop.noop } = options;
58
+ let isRunning = false;
59
+ function pause() {
60
+ isRunning = false;
61
+ raf.remove(key);
62
+ }
63
+ function progress(newProgress) {
64
+ if (typeof newProgress === "undefined") {
65
+ return easedProgress;
66
+ }
67
+ progressValue = newProgress;
68
+ easedProgress = ease(progressValue);
69
+ if (Math.abs(1 - easedProgress) < PROGRESS_PRECISION) {
70
+ progressValue = 1;
71
+ easedProgress = 1;
72
+ }
73
+ callback(easedProgress);
74
+ onProgress(progressValue, easedProgress);
75
+ if (easedProgress === 1) {
76
+ pause();
77
+ requestAnimationFrame(() => onFinish(progressValue, easedProgress));
78
+ }
79
+ return progressValue;
80
+ }
81
+ function tick(props) {
82
+ if (!isRunning) {
83
+ raf.remove(key);
84
+ return;
85
+ }
86
+ progress((0, import_math.clamp01)((0, import_math.map)(props.time, startTime, endTime, 0, 1)));
87
+ }
88
+ function start() {
89
+ onStart();
90
+ startTime = performance.now();
91
+ endTime = startTime + duration;
92
+ progressValue = 0;
93
+ easedProgress = 0;
94
+ isRunning = true;
95
+ raf.add(key, tick);
96
+ }
97
+ function play() {
98
+ if (isRunning) {
99
+ return;
100
+ }
101
+ startTime = performance.now() - (0, import_math.lerp)(0, duration, progressValue);
102
+ endTime = startTime + duration;
103
+ isRunning = true;
104
+ raf.add(key, tick);
105
+ }
106
+ return {
107
+ start,
108
+ finish: () => progress(1),
109
+ pause,
110
+ play,
111
+ progress
112
+ };
113
+ }
114
+ if (module.exports.default) module.exports = module.exports.default;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @typedef {[number, number, number, number]} BezierCurve
3
+ * @typedef {{
4
+ * duration?: number;
5
+ * easing?: import('./math/createEases.js').EasingFunction|BezierCurve;
6
+ * onStart?: () => void;
7
+ * onProgress?: (progress: number, easedProgress: number) => void;
8
+ * onFinish?: (progress: number, easedProgress: number) => void;
9
+ * }} TweenOptions
10
+ */
11
+ /**
12
+ * Normalize a easing function with default fallbacks.
13
+ * @param {((p:number) => number)|[number,number,number,number]} ease
14
+ * @returns {(p:number) => number}
15
+ */
16
+ export function normalizeEase(ease: [number, number, number, number] | ((p: number) => number)): (p: number) => number;
17
+ /**
18
+ * Tween from 0 to 1.
19
+ * @param {(progress:number)=>any} callback
20
+ * @param {TweenOptions} [options]
21
+ * @returns {any}
22
+ */
23
+ export function tween(callback: (progress: number) => any, options?: TweenOptions): any;
24
+ export type BezierCurve = [number, number, number, number];
25
+ export type TweenOptions = TweenOptions;
package/utils/tween.js ADDED
@@ -0,0 +1,89 @@
1
+ import { cubicBezier } from "@motionone/easing";
2
+ import { lerp, map, clamp01 } from "./math/index.js";
3
+ import { isDefined, isArray } from "./is.js";
4
+ import { noop, noopValue as linear } from "./noop.js";
5
+ import useRaf from "../services/raf.js";
6
+ let id = 0;
7
+ const PROGRESS_PRECISION = 1e-4;
8
+ function normalizeEase(ease) {
9
+ if (!isDefined(ease)) {
10
+ return linear;
11
+ }
12
+ if (isArray(ease)) {
13
+ return cubicBezier(...ease);
14
+ }
15
+ return ease;
16
+ }
17
+ function tween(callback, options = {}) {
18
+ var _a;
19
+ const raf = useRaf();
20
+ let progressValue = 0;
21
+ let easedProgress = 0;
22
+ const ease = normalizeEase(options.easing);
23
+ let duration = (_a = options.duration) != null ? _a : 1;
24
+ duration *= 1e3;
25
+ let startTime = performance.now();
26
+ let endTime = startTime + duration;
27
+ const key = `tw-${id}`;
28
+ id += 1;
29
+ const { onStart = noop, onProgress = noop, onFinish = noop } = options;
30
+ let isRunning = false;
31
+ function pause() {
32
+ isRunning = false;
33
+ raf.remove(key);
34
+ }
35
+ function progress(newProgress) {
36
+ if (typeof newProgress === "undefined") {
37
+ return easedProgress;
38
+ }
39
+ progressValue = newProgress;
40
+ easedProgress = ease(progressValue);
41
+ if (Math.abs(1 - easedProgress) < PROGRESS_PRECISION) {
42
+ progressValue = 1;
43
+ easedProgress = 1;
44
+ }
45
+ callback(easedProgress);
46
+ onProgress(progressValue, easedProgress);
47
+ if (easedProgress === 1) {
48
+ pause();
49
+ requestAnimationFrame(() => onFinish(progressValue, easedProgress));
50
+ }
51
+ return progressValue;
52
+ }
53
+ function tick(props) {
54
+ if (!isRunning) {
55
+ raf.remove(key);
56
+ return;
57
+ }
58
+ progress(clamp01(map(props.time, startTime, endTime, 0, 1)));
59
+ }
60
+ function start() {
61
+ onStart();
62
+ startTime = performance.now();
63
+ endTime = startTime + duration;
64
+ progressValue = 0;
65
+ easedProgress = 0;
66
+ isRunning = true;
67
+ raf.add(key, tick);
68
+ }
69
+ function play() {
70
+ if (isRunning) {
71
+ return;
72
+ }
73
+ startTime = performance.now() - lerp(0, duration, progressValue);
74
+ endTime = startTime + duration;
75
+ isRunning = true;
76
+ raf.add(key, tick);
77
+ }
78
+ return {
79
+ start,
80
+ finish: () => progress(1),
81
+ pause,
82
+ play,
83
+ progress
84
+ };
85
+ }
86
+ export {
87
+ normalizeEase,
88
+ tween
89
+ };
@@ -1,76 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var __publicField = (obj, key, value) => {
20
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
21
- return value;
22
- };
23
-
24
- // packages/js-toolkit/services/Service.js
25
- var Service_exports = {};
26
- __export(Service_exports, {
27
- default: () => Service
28
- });
29
- module.exports = __toCommonJS(Service_exports);
30
- var Service = class {
31
- constructor() {
32
- __publicField(this, "callbacks", /* @__PURE__ */ new Map());
33
- __publicField(this, "isInit", false);
34
- __publicField(this, "props");
35
- }
36
- updateProps(...args) {
37
- this.props = args;
38
- throw new Error("The `updateProps` method must be implemented.");
39
- }
40
- init() {
41
- throw new Error("The `init` method must be implemented.");
42
- }
43
- kill() {
44
- throw new Error("The `kill` method must be implemented.");
45
- }
46
- add(key, callback) {
47
- if (this.has(key)) {
48
- throw new Error(`A callback with the key \`${key}\` has already been registered.`);
49
- }
50
- if (this.callbacks.size === 0 && !this.isInit) {
51
- this.init();
52
- this.isInit = true;
53
- }
54
- this.callbacks.set(key, callback);
55
- }
56
- has(key) {
57
- return this.callbacks.has(key);
58
- }
59
- get(key) {
60
- return this.callbacks.get(key);
61
- }
62
- remove(key) {
63
- this.callbacks.delete(key);
64
- if (this.callbacks.size === 0 && this.isInit) {
65
- this.kill();
66
- this.isInit = false;
67
- }
68
- }
69
- trigger(...args) {
70
- this.callbacks.forEach(function forEachCallback(callback) {
71
- callback(...args);
72
- });
73
- return this;
74
- }
75
- };
76
- if (module.exports.default) module.exports = module.exports.default;