@studiometa/js-toolkit 2.2.3 → 2.4.1

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 (87) hide show
  1. package/Base/index.cjs +29 -36
  2. package/Base/index.d.ts +0 -8
  3. package/Base/index.js +18 -25
  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 +5 -2
  13. package/Base/managers/ServicesManager.js +6 -3
  14. package/Base/utils.cjs +23 -1
  15. package/Base/utils.d.ts +25 -0
  16. package/Base/utils.js +23 -1
  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/raf.cjs +2 -5
  38. package/services/raf.js +1 -1
  39. package/utils/css/animate.cjs +31 -92
  40. package/utils/css/animate.d.ts +4 -16
  41. package/utils/css/animate.js +28 -89
  42. package/utils/css/classes.cjs +2 -1
  43. package/utils/css/classes.js +2 -1
  44. package/utils/css/styles.cjs +2 -5
  45. package/utils/css/styles.js +1 -1
  46. package/utils/css/transform.cjs +13 -16
  47. package/utils/css/transform.js +1 -1
  48. package/utils/css/transition.cjs +5 -3
  49. package/utils/css/transition.js +5 -3
  50. package/utils/focusTrap.cjs +2 -1
  51. package/utils/focusTrap.js +2 -1
  52. package/utils/{isFunction.cjs → has.cjs} +7 -7
  53. package/utils/has.d.ts +6 -0
  54. package/utils/has.js +6 -0
  55. package/utils/history.cjs +5 -7
  56. package/utils/history.js +4 -3
  57. package/utils/index.cjs +5 -5
  58. package/utils/index.d.ts +3 -2
  59. package/utils/index.js +5 -5
  60. package/utils/{isDefined.cjs → is.cjs} +20 -8
  61. package/utils/is.d.ts +8 -0
  62. package/utils/is.js +18 -0
  63. package/utils/math/inertiaFinalValue.cjs +5 -3
  64. package/utils/math/inertiaFinalValue.js +2 -3
  65. package/utils/nextFrame.cjs +5 -3
  66. package/utils/nextFrame.js +5 -3
  67. package/utils/nextMicrotask.cjs +2 -1
  68. package/utils/nextMicrotask.js +2 -1
  69. package/utils/nextTick.cjs +2 -1
  70. package/utils/nextTick.js +2 -1
  71. package/utils/object/autoBind.cjs +3 -2
  72. package/utils/object/autoBind.js +3 -2
  73. package/utils/object/getAllProperties.cjs +2 -1
  74. package/utils/object/getAllProperties.js +2 -1
  75. package/utils/object/index.cjs +1 -3
  76. package/utils/object/index.d.ts +0 -1
  77. package/utils/object/index.js +1 -3
  78. package/utils/tween.cjs +114 -0
  79. package/utils/tween.d.ts +25 -0
  80. package/utils/tween.js +89 -0
  81. package/utils/isDefined.d.ts +0 -7
  82. package/utils/isDefined.js +0 -6
  83. package/utils/isFunction.d.ts +0 -7
  84. package/utils/isFunction.js +0 -6
  85. package/utils/object/isObject.cjs +0 -28
  86. package/utils/object/isObject.d.ts +0 -7
  87. package/utils/object/isObject.js +0 -6
@@ -4,6 +4,7 @@ var __publicField = (obj, key, value) => {
4
4
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
5
  return value;
6
6
  };
7
+ import { isDev, isFunction } from "../utils/index.js";
7
8
  var withVue2_default = (BaseClass, Vue) => {
8
9
  var _a, _b, _c;
9
10
  return _c = class extends BaseClass {
@@ -13,10 +14,16 @@ var withVue2_default = (BaseClass, Vue) => {
13
14
  __publicField(this, "$vue");
14
15
  const vueConfig = (_a2 = this.vueConfig) != null ? _a2 : this.constructor.vueConfig;
15
16
  if (!vueConfig) {
16
- throw new Error("[withVue] You must define a `vueConfig` object.");
17
+ if (isDev) {
18
+ throw new Error("[withVue] You must define a `vueConfig` object.");
19
+ }
20
+ return;
17
21
  }
18
- if (typeof vueConfig.render !== "function") {
19
- throw new Error("[withVue] You must define a `render` function in vueConfig.");
22
+ if (!isFunction(vueConfig.render)) {
23
+ if (isDev) {
24
+ throw new Error("[withVue] You must define a `render` function in vueConfig.");
25
+ }
26
+ return;
20
27
  }
21
28
  this.$vue = new Vue(vueConfig);
22
29
  this.$on("mounted", () => {
@@ -0,0 +1,46 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // packages/js-toolkit/helpers/getDirectChildren.js
20
+ var getDirectChildren_exports = {};
21
+ __export(getDirectChildren_exports, {
22
+ getDirectChildren: () => getDirectChildren,
23
+ isDirectChild: () => isDirectChild
24
+ });
25
+ module.exports = __toCommonJS(getDirectChildren_exports);
26
+ var import_utils = require("../utils/index.cjs");
27
+ function getDirectChildren(parentInstance, parentName, childrenName) {
28
+ const children = parentInstance.$children[childrenName];
29
+ const nestedParents = parentInstance.$children[parentName];
30
+ if (!(0, import_utils.isArray)(children)) {
31
+ return [];
32
+ }
33
+ if (!(0, import_utils.isArray)(nestedParents) || nestedParents.length <= 0) {
34
+ return children;
35
+ }
36
+ return children.filter((child) => {
37
+ return nestedParents.every((nestedParent) => {
38
+ const nestedChildren = nestedParent.$children[childrenName];
39
+ return (0, import_utils.isArray)(nestedChildren) ? !nestedChildren.includes(child) : true;
40
+ });
41
+ });
42
+ }
43
+ function isDirectChild(parentInstance, parentName, childrenName, childInstance) {
44
+ return getDirectChildren(parentInstance, parentName, childrenName).includes(childInstance);
45
+ }
46
+ if (module.exports.default) module.exports = module.exports.default;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @typedef {import('../Base/index.js').default} Base
3
+ */
4
+ /**
5
+ * Get direct children from a parent when working with nested components.
6
+ *
7
+ * @template {Base} T
8
+ * @param {Base} parentInstance
9
+ * @param {string} parentName
10
+ * @param {string} childrenName
11
+ * @returns {Array<T>}
12
+ */
13
+ export function getDirectChildren<T extends import("../Base/index.js").default>(parentInstance: Base, parentName: string, childrenName: string): T[];
14
+ /**
15
+ * Test if a component instance is a direct child from the given component.
16
+ *
17
+ * @param {Base} parentInstance
18
+ * @param {string} parentName
19
+ * @param {string} childrenName
20
+ * @param {Base} childInstance
21
+ * @returns {boolean}
22
+ */
23
+ export function isDirectChild(parentInstance: Base, parentName: string, childrenName: string, childInstance: Base): boolean;
24
+ export type Base = import('../Base/index.js').default;
@@ -0,0 +1,24 @@
1
+ import { isArray } from "../utils/index.js";
2
+ function getDirectChildren(parentInstance, parentName, childrenName) {
3
+ const children = parentInstance.$children[childrenName];
4
+ const nestedParents = parentInstance.$children[parentName];
5
+ if (!isArray(children)) {
6
+ return [];
7
+ }
8
+ if (!isArray(nestedParents) || nestedParents.length <= 0) {
9
+ return children;
10
+ }
11
+ return children.filter((child) => {
12
+ return nestedParents.every((nestedParent) => {
13
+ const nestedChildren = nestedParent.$children[childrenName];
14
+ return isArray(nestedChildren) ? !nestedChildren.includes(child) : true;
15
+ });
16
+ });
17
+ }
18
+ function isDirectChild(parentInstance, parentName, childrenName, childInstance) {
19
+ return getDirectChildren(parentInstance, parentName, childrenName).includes(childInstance);
20
+ }
21
+ export {
22
+ getDirectChildren,
23
+ isDirectChild
24
+ };
@@ -23,8 +23,9 @@ __export(importOnInteraction_exports, {
23
23
  });
24
24
  module.exports = __toCommonJS(importOnInteraction_exports);
25
25
  var import_utils = require("./utils.cjs");
26
+ var import_utils2 = require("../utils/index.cjs");
26
27
  function importOnInteraction(fn, nameOrSelectorOrElement, events, parent) {
27
- const normalizedEvents = typeof events === "string" ? [events] : events;
28
+ const normalizedEvents = (0, import_utils2.isString)(events) ? [events] : events;
28
29
  let ResolvedClass;
29
30
  const resolver = (resolve) => {
30
31
  fn().then((module2) => {
@@ -1,6 +1,7 @@
1
1
  import { getTargetElements } from "./utils.js";
2
+ import { isString } from "../utils/index.js";
2
3
  function importOnInteraction(fn, nameOrSelectorOrElement, events, parent) {
3
- const normalizedEvents = typeof events === "string" ? [events] : events;
4
+ const normalizedEvents = isString(events) ? [events] : events;
4
5
  let ResolvedClass;
5
6
  const resolver = (resolve) => {
6
7
  fn().then((module) => {
@@ -23,13 +23,14 @@ __export(importWhenVisible_exports, {
23
23
  });
24
24
  module.exports = __toCommonJS(importWhenVisible_exports);
25
25
  var import_utils = require("./utils.cjs");
26
+ var import_utils2 = require("../utils/index.cjs");
26
27
  function importWhenVisible(fn, nameOrSelectorOrElement, parent, observerOptions = {}) {
27
28
  let ResolvedClass;
28
29
  const resolver = (resolve, cb) => {
29
30
  fn().then((module2) => {
30
31
  ResolvedClass = "default" in module2 ? module2.default : module2;
31
32
  resolve(ResolvedClass);
32
- if (typeof cb === "function") {
33
+ if ((0, import_utils2.isFunction)(cb)) {
33
34
  cb();
34
35
  }
35
36
  });
@@ -1,11 +1,12 @@
1
1
  import { getTargetElements } from "./utils.js";
2
+ import { isFunction } from "../utils/index.js";
2
3
  function importWhenVisible(fn, nameOrSelectorOrElement, parent, observerOptions = {}) {
3
4
  let ResolvedClass;
4
5
  const resolver = (resolve, cb) => {
5
6
  fn().then((module) => {
6
7
  ResolvedClass = "default" in module ? module.default : module;
7
8
  resolve(ResolvedClass);
8
- if (typeof cb === "function") {
9
+ if (isFunction(cb)) {
9
10
  cb();
10
11
  }
11
12
  });
package/helpers/index.cjs CHANGED
@@ -16,6 +16,7 @@ var __copyProps = (to, from, except, desc) => {
16
16
  }
17
17
  return to;
18
18
  };
19
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
20
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
20
21
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
22
 
@@ -30,6 +31,7 @@ __export(helpers_exports, {
30
31
  });
31
32
  module.exports = __toCommonJS(helpers_exports);
32
33
  var import_createApp = __toESM(require("./createApp.cjs"), 1);
34
+ __reExport(helpers_exports, require("./getDirectChildren.cjs"), module.exports);
33
35
  var import_getInstanceFromElement = __toESM(require("./getInstanceFromElement.cjs"), 1);
34
36
  var import_importOnInteraction = __toESM(require("./importOnInteraction.cjs"), 1);
35
37
  var import_importWhenIdle = __toESM(require("./importWhenIdle.cjs"), 1);
@@ -1,4 +1,5 @@
1
1
  export { default as createApp } from "./createApp.js";
2
+ export * from "./getDirectChildren.js";
2
3
  export { default as getInstanceFromElement } from "./getInstanceFromElement.js";
3
4
  export { default as importOnInteraction } from "./importOnInteraction.js";
4
5
  export { default as importWhenIdle } from "./importWhenIdle.js";
package/helpers/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { default as default2 } from "./createApp.js";
2
+ export * from "./getDirectChildren.js";
2
3
  import { default as default3 } from "./getInstanceFromElement.js";
3
4
  import { default as default4 } from "./importOnInteraction.js";
4
5
  import { default as default5 } from "./importWhenIdle.js";
package/helpers/utils.cjs CHANGED
@@ -23,11 +23,12 @@ __export(utils_exports, {
23
23
  });
24
24
  module.exports = __toCommonJS(utils_exports);
25
25
  var import_utils = require("../Base/utils.cjs");
26
+ var import_utils2 = require("../utils/index.cjs");
26
27
  function getTargetElements(nameOrSelectorOrElement, context) {
27
- if (typeof nameOrSelectorOrElement === "string") {
28
+ if ((0, import_utils2.isString)(nameOrSelectorOrElement)) {
28
29
  return (0, import_utils.getComponentElements)(nameOrSelectorOrElement, context);
29
30
  }
30
- if (!Array.isArray(nameOrSelectorOrElement)) {
31
+ if (!(0, import_utils2.isArray)(nameOrSelectorOrElement)) {
31
32
  return [nameOrSelectorOrElement];
32
33
  }
33
34
  return nameOrSelectorOrElement;
package/helpers/utils.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import { getComponentElements } from "../Base/utils.js";
2
+ import { isString, isArray } from "../utils/index.js";
2
3
  function getTargetElements(nameOrSelectorOrElement, context) {
3
- if (typeof nameOrSelectorOrElement === "string") {
4
+ if (isString(nameOrSelectorOrElement)) {
4
5
  return getComponentElements(nameOrSelectorOrElement, context);
5
6
  }
6
- if (!Array.isArray(nameOrSelectorOrElement)) {
7
+ if (!isArray(nameOrSelectorOrElement)) {
7
8
  return [nameOrSelectorOrElement];
8
9
  }
9
10
  return nameOrSelectorOrElement;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@studiometa/js-toolkit",
3
- "version": "2.2.3",
3
+ "version": "2.4.1",
4
4
  "description": "A set of useful little bits of JavaScript to boost your project! 🚀",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -41,5 +41,8 @@
41
41
  "dependencies": {
42
42
  "@motionone/easing": "^10.9.0",
43
43
  "deepmerge": "^4.2.2"
44
+ },
45
+ "devDependencies": {
46
+ "vue": "2.6"
44
47
  }
45
48
  }
package/services/raf.cjs CHANGED
@@ -1,8 +1,6 @@
1
- var __create = Object.create;
2
1
  var __defProp = Object.defineProperty;
3
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
5
  var __export = (target, all) => {
8
6
  for (var name in all)
@@ -16,7 +14,6 @@ var __copyProps = (to, from, except, desc) => {
16
14
  }
17
15
  return to;
18
16
  };
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
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
18
 
22
19
  // packages/js-toolkit/services/raf.js
@@ -28,7 +25,7 @@ module.exports = __toCommonJS(raf_exports);
28
25
  var import_service = require("./service.cjs");
29
26
  var import_nextFrame = require("../utils/nextFrame.cjs");
30
27
  var import_scheduler = require("../utils/scheduler.cjs");
31
- var import_isFunction = __toESM(require("../utils/isFunction.cjs"), 1);
28
+ var import_is = require("../utils/is.cjs");
32
29
  var scheduler = (0, import_scheduler.useScheduler)(["update", "render"]);
33
30
  function createRafService() {
34
31
  let isTicking = false;
@@ -37,7 +34,7 @@ function createRafService() {
37
34
  callbacks.forEach(function forEachCallback(callback) {
38
35
  scheduler.update(function rafUpdate() {
39
36
  const render = callback(props2);
40
- if ((0, import_isFunction.default)(render)) {
37
+ if ((0, import_is.isFunction)(render)) {
41
38
  scheduler.render(function rafRender() {
42
39
  render(props2);
43
40
  });
package/services/raf.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { useService } from "./service.js";
2
2
  import { getRaf as getRequestAnimationFrame } from "../utils/nextFrame.js";
3
3
  import { useScheduler } from "../utils/scheduler.js";
4
- import isFunction from "../utils/isFunction.js";
4
+ import { isFunction } from "../utils/is.js";
5
5
  const scheduler = useScheduler(["update", "render"]);
6
6
  function createRafService() {
7
7
  let isTicking = false;
@@ -25,16 +25,13 @@ __export(animate_exports, {
25
25
  animate: () => animate
26
26
  });
27
27
  module.exports = __toCommonJS(animate_exports);
28
- var import_easing = require("@motionone/easing");
29
28
  var import_math = require("../math/index.cjs");
30
- var import_isDefined = __toESM(require("../isDefined.cjs"), 1);
29
+ var import_is = require("../is.cjs");
31
30
  var import_transform = __toESM(require("./transform.cjs"), 1);
32
- var import_raf = __toESM(require("../../services/raf.cjs"), 1);
33
31
  var import_scheduler = require("../scheduler.cjs");
34
- var import_noop = require("../noop.cjs");
32
+ var import_tween = require("../tween.cjs");
35
33
  var id = 0;
36
34
  var running = /* @__PURE__ */ new WeakMap();
37
- var PROGRESS_PRECISION = 1e-4;
38
35
  var CSSUnitConverter = {
39
36
  "%": (value, getSizeRef) => value * getSizeRef() / 100,
40
37
  vh: (value) => value * window.innerHeight / 100,
@@ -43,7 +40,7 @@ var CSSUnitConverter = {
43
40
  vmax: (value) => value * Math.max(window.innerWidth, window.innerHeight) / 100
44
41
  };
45
42
  function getAnimationStepValue(val, getSizeRef) {
46
- if (typeof val === "number") {
43
+ if ((0, import_is.isNumber)(val)) {
47
44
  return val;
48
45
  }
49
46
  if (!val[1] || !CSSUnitConverter[val[1]]) {
@@ -51,15 +48,6 @@ function getAnimationStepValue(val, getSizeRef) {
51
48
  }
52
49
  return CSSUnitConverter[val[1]](val[0], getSizeRef);
53
50
  }
54
- function normalizeEase(ease) {
55
- if (!(0, import_isDefined.default)(ease)) {
56
- return import_noop.noopValue;
57
- }
58
- if (Array.isArray(ease)) {
59
- return (0, import_easing.cubicBezier)(...ease);
60
- }
61
- return ease;
62
- }
63
51
  var generateTranslateRenderStrategy = (sizeRef) => (element, fromValue, toValue, progress) => (0, import_math.lerp)(getAnimationStepValue(fromValue != null ? fromValue : 0, () => element[sizeRef]), getAnimationStepValue(toValue != null ? toValue : 0, () => element[sizeRef]), progress);
64
52
  var widthBasedTranslateRenderStrategy = generateTranslateRenderStrategy("offsetWidth");
65
53
  var heightBasedTranslateRenderStrategy = generateTranslateRenderStrategy("offsetHeight");
@@ -87,18 +75,18 @@ function render(element, from, to, progress) {
87
75
  import_scheduler.domScheduler.read(function renderRead() {
88
76
  var _a, _b;
89
77
  let opacity = false;
90
- if ((0, import_isDefined.default)(from.opacity) || (0, import_isDefined.default)(to.opacity)) {
78
+ if ((0, import_is.isDefined)(from.opacity) || (0, import_is.isDefined)(to.opacity)) {
91
79
  opacity = (0, import_math.map)(stepProgress, 0, 1, (_a = from.opacity) != null ? _a : 1, (_b = to.opacity) != null ? _b : 1);
92
80
  } else if (element.style.opacity) {
93
81
  opacity = "";
94
82
  }
95
83
  let transformOrigin = false;
96
- if ((0, import_isDefined.default)(to.transformOrigin)) {
84
+ if ((0, import_is.isDefined)(to.transformOrigin)) {
97
85
  transformOrigin = to.transformOrigin;
98
86
  } else if (element.style.transformOrigin) {
99
87
  transformOrigin = "";
100
88
  }
101
- const props = Object.fromEntries(import_transform.TRANSFORM_PROPS.filter((name) => (0, import_isDefined.default)(from[name]) || (0, import_isDefined.default)(to[name])).map((name) => {
89
+ const props = Object.fromEntries(import_transform.TRANSFORM_PROPS.filter((name) => (0, import_is.isDefined)(from[name]) || (0, import_is.isDefined)(to[name])).map((name) => {
102
90
  return [name, transformRenderStrategies[name](element, from[name], to[name], stepProgress)];
103
91
  }));
104
92
  import_scheduler.domScheduler.write(function renderWrite() {
@@ -113,91 +101,42 @@ function render(element, from, to, progress) {
113
101
  });
114
102
  }
115
103
  function animate(element, keyframes, options = {}) {
116
- var _a;
117
- const raf = (0, import_raf.default)();
118
- let progressValue = 0;
119
- let easedProgress = 0;
120
- if (!running.has(element)) {
121
- running.set(element, /* @__PURE__ */ new Map());
122
- }
123
- const ease = normalizeEase(options.easing);
124
- let duration = (_a = options.duration) != null ? _a : 1;
125
- duration *= 1e3;
126
- let startTime = performance.now();
127
- let endTime = startTime + duration;
128
- const key = `animate-${id}`;
129
- id += 1;
130
- const { onProgress = import_noop.noop, onFinish = import_noop.noop } = options;
131
- let isRunning = false;
132
104
  const keyframesCount = keyframes.length - 1;
133
105
  const normalizedKeyframes = keyframes.map((keyframe, index) => {
134
- var _a2;
106
+ var _a;
135
107
  return {
136
108
  ...keyframe,
137
- offset: (_a2 = keyframe.offset) != null ? _a2 : index / keyframesCount,
138
- easing: normalizeEase(keyframe.easing)
109
+ offset: (_a = keyframe.offset) != null ? _a : index / keyframesCount,
110
+ easing: (0, import_tween.normalizeEase)(keyframe.easing)
139
111
  };
140
112
  });
141
- function pause() {
142
- isRunning = false;
143
- raf.remove(key);
113
+ if (!running.has(element)) {
114
+ running.set(element, /* @__PURE__ */ new Map());
144
115
  }
145
- function progress(newProgress) {
146
- if (typeof newProgress === "undefined") {
147
- return progressValue;
148
- }
149
- progressValue = newProgress;
150
- if (Math.abs(1 - progressValue) < PROGRESS_PRECISION) {
151
- progressValue = 1;
152
- pause();
153
- import_scheduler.domScheduler.afterWrite(() => onFinish(progressValue, easedProgress));
154
- }
155
- easedProgress = ease(progressValue);
116
+ const key = `animate-${id}`;
117
+ id += 1;
118
+ function callback(progress) {
156
119
  let toIndex = 0;
157
- while (normalizedKeyframes[toIndex] && normalizedKeyframes[toIndex].offset <= easedProgress && normalizedKeyframes[toIndex].offset !== 1) {
120
+ while (normalizedKeyframes[toIndex] && normalizedKeyframes[toIndex].offset <= progress && normalizedKeyframes[toIndex].offset !== 1) {
158
121
  toIndex += 1;
159
122
  }
160
- render(element, normalizedKeyframes[toIndex - 1], normalizedKeyframes[toIndex], easedProgress);
161
- onProgress(progressValue, easedProgress);
162
- return progressValue;
163
- }
164
- function tick(props) {
165
- if (!isRunning) {
166
- raf.remove(key);
167
- return;
168
- }
169
- progress((0, import_math.clamp01)((0, import_math.map)(props.time, startTime, endTime, 0, 1)));
123
+ render(element, normalizedKeyframes[toIndex - 1], normalizedKeyframes[toIndex], progress);
170
124
  }
171
- function start() {
172
- const runningKeys = running.get(element);
173
- runningKeys.forEach((runningPause, runningKey) => {
174
- runningPause();
175
- runningKeys.delete(runningKey);
176
- });
177
- runningKeys.set(key, pause);
178
- running.set(element, runningKeys);
179
- startTime = performance.now();
180
- endTime = startTime + duration;
181
- progressValue = 0;
182
- easedProgress = 0;
183
- isRunning = true;
184
- raf.add(key, tick);
185
- }
186
- function play() {
187
- if (isRunning) {
188
- return;
125
+ const controls = (0, import_tween.tween)(callback, {
126
+ ...options,
127
+ onStart() {
128
+ if ((0, import_is.isFunction)(options.onStart)) {
129
+ options.onStart();
130
+ }
131
+ const runningKeys = running.get(element);
132
+ runningKeys.forEach((runningPause, runningKey) => {
133
+ runningPause();
134
+ runningKeys.delete(runningKey);
135
+ });
136
+ runningKeys.set(key, controls.pause);
137
+ running.set(element, runningKeys);
189
138
  }
190
- startTime = performance.now() - (0, import_math.lerp)(0, duration, progressValue);
191
- endTime = startTime + duration;
192
- isRunning = true;
193
- raf.add(key, tick);
194
- }
195
- return {
196
- start,
197
- finish: () => progress(1),
198
- pause,
199
- play,
200
- progress
201
- };
139
+ });
140
+ return controls;
202
141
  }
203
142
  if (module.exports.default) module.exports = module.exports.default;
@@ -1,10 +1,9 @@
1
1
  /**
2
2
  * @typedef {import('./transform.js').TransformProps} TransformProps
3
- * @typedef {[number, number, number, number]} BezierCurve
4
3
  * @typedef {TransformProps & {
5
4
  * opacity?: number;
6
5
  * transformOrigin?: string;
7
- * easing?: import('../math/createEases.js').EasingFunction|BezierCurve;
6
+ * easing?: import('../math/createEases.js').EasingFunction|import('../tween.js').BezierCurve;
8
7
  * offset?: number;
9
8
  * }} Keyframe
10
9
  * @typedef {Keyframe & {
@@ -23,26 +22,15 @@
23
22
  * Animate an element.
24
23
  * @param {HTMLElement} element
25
24
  * @param {Keyframe[]} keyframes
26
- * @param {{
27
- * duration?: number;
28
- * easing?: import('../math/createEases.js').EasingFunction|BezierCurve;
29
- * onProgress?: (progress: number, easedProgress: number) => void;
30
- * onFinish?: (progress: number, easedProgress: number) => void;
31
- * }} [options]
25
+ * @param {import('../tween.js').TweenOptions} [options]
32
26
  * @returns {Animate}
33
27
  */
34
- export function animate(element: HTMLElement, keyframes: Keyframe[], options?: {
35
- duration?: number;
36
- easing?: import('../math/createEases.js').EasingFunction | BezierCurve;
37
- onProgress?: (progress: number, easedProgress: number) => void;
38
- onFinish?: (progress: number, easedProgress: number) => void;
39
- }): Animate;
28
+ export function animate(element: HTMLElement, keyframes: Keyframe[], options?: import('../tween.js').TweenOptions): Animate;
40
29
  export type TransformProps = import('./transform.js').TransformProps;
41
- export type BezierCurve = [number, number, number, number];
42
30
  export type Keyframe = TransformProps & {
43
31
  opacity?: number;
44
32
  transformOrigin?: string;
45
- easing?: import('../math/createEases.js').EasingFunction | [number, number, number, number];
33
+ easing?: import('../math/createEases.js').EasingFunction | import('../tween.js').BezierCurve;
46
34
  offset?: number;
47
35
  };
48
36
  export type NormalizedKeyframe = Keyframe & {