@studiometa/js-toolkit 2.0.1 → 2.1.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.
- package/Base/index.cjs +53 -16
- package/Base/index.d.ts +22 -0
- package/Base/index.js +53 -16
- package/Base/managers/AbstractManager.cjs +56 -0
- package/Base/managers/AbstractManager.d.ts +44 -0
- package/Base/managers/AbstractManager.js +35 -0
- package/Base/managers/ChildrenManager.cjs +79 -100
- package/Base/managers/ChildrenManager.d.ts +7 -82
- package/Base/managers/ChildrenManager.js +76 -100
- package/Base/managers/EventsManager.cjs +75 -99
- package/Base/managers/EventsManager.d.ts +31 -100
- package/Base/managers/EventsManager.js +75 -99
- package/Base/managers/OptionsManager.cjs +65 -78
- package/Base/managers/OptionsManager.d.ts +37 -79
- package/Base/managers/OptionsManager.js +66 -79
- package/Base/managers/RefsManager.cjs +44 -68
- package/Base/managers/RefsManager.d.ts +10 -64
- package/Base/managers/RefsManager.js +41 -69
- package/Base/managers/ResponsiveOptionsManager.cjs +62 -0
- package/Base/managers/ResponsiveOptionsManager.d.ts +26 -0
- package/Base/managers/ResponsiveOptionsManager.js +37 -0
- package/Base/managers/ServicesManager.cjs +4 -10
- package/Base/managers/ServicesManager.d.ts +2 -11
- package/Base/managers/ServicesManager.js +4 -10
- package/decorators/index.cjs +2 -0
- package/decorators/index.d.ts +1 -0
- package/decorators/index.js +6 -4
- package/decorators/withResponsiveOptions.cjs +39 -0
- package/decorators/withResponsiveOptions.d.ts +8 -0
- package/decorators/withResponsiveOptions.js +14 -0
- package/decorators/withScrolledInView.cjs +58 -23
- package/decorators/withScrolledInView.d.ts +4 -2
- package/decorators/withScrolledInView.js +59 -24
- package/package.json +2 -1
- package/utils/css/animate.cjs +190 -0
- package/utils/css/animate.d.ts +57 -0
- package/utils/css/animate.js +165 -0
- package/utils/css/getOffsetSizes.cjs +47 -0
- package/utils/css/getOffsetSizes.d.ts +15 -0
- package/utils/css/getOffsetSizes.js +25 -0
- package/utils/css/index.cjs +6 -0
- package/utils/css/index.d.ts +3 -0
- package/utils/css/index.js +9 -4
- package/utils/css/transform.cjs +91 -0
- package/utils/css/transform.d.ts +43 -0
- package/utils/css/transform.js +66 -0
- package/utils/index.cjs +4 -0
- package/utils/index.d.ts +2 -0
- package/utils/index.js +4 -0
- package/utils/isDefined.cjs +28 -0
- package/utils/isDefined.d.ts +7 -0
- package/utils/isDefined.js +6 -0
- package/utils/isFunction.cjs +28 -0
- package/utils/isFunction.d.ts +7 -0
- package/utils/isFunction.js +6 -0
- package/utils/math/index.cjs +2 -0
- package/utils/math/index.d.ts +1 -0
- package/utils/math/index.js +2 -0
|
@@ -5,12 +5,12 @@ var __publicField = (obj, key, value) => {
|
|
|
5
5
|
return value;
|
|
6
6
|
};
|
|
7
7
|
import withMountWhenInView from "./withMountWhenInView.js";
|
|
8
|
-
import { damp, clamp, clamp01 } from "../utils/index.js";
|
|
9
|
-
function withScrolledInView(BaseClass, options) {
|
|
8
|
+
import { damp, clamp, clamp01, getOffsetSizes, isFunction } from "../utils/index.js";
|
|
9
|
+
function withScrolledInView(BaseClass, options = {}) {
|
|
10
10
|
var _a;
|
|
11
11
|
return _a = class extends withMountWhenInView(BaseClass, options) {
|
|
12
|
-
constructor() {
|
|
13
|
-
super(
|
|
12
|
+
constructor(element) {
|
|
13
|
+
super(element);
|
|
14
14
|
__publicField(this, "__props", {
|
|
15
15
|
start: {
|
|
16
16
|
x: 0,
|
|
@@ -35,37 +35,72 @@ function withScrolledInView(BaseClass, options) {
|
|
|
35
35
|
});
|
|
36
36
|
__publicField(this, "dampFactor", 0.1);
|
|
37
37
|
__publicField(this, "dampPrecision", 1e-3);
|
|
38
|
+
const delegate = {
|
|
39
|
+
handleEvent(event) {
|
|
40
|
+
delegate[event.type](event.detail[0]);
|
|
41
|
+
},
|
|
42
|
+
resized: () => {
|
|
43
|
+
this.__setProps();
|
|
44
|
+
},
|
|
45
|
+
scrolled: (props) => {
|
|
46
|
+
if (!this.$services.has("ticked") && props.changed.y || props.changed.x) {
|
|
47
|
+
this.$services.enable("ticked");
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
ticked: () => {
|
|
51
|
+
this.__props.current.x = clamp(window.pageXOffset, this.__props.start.x, this.__props.end.x);
|
|
52
|
+
this.__props.progress.x = clamp01((this.__props.current.x - this.__props.start.x) / (this.__props.end.x - this.__props.start.x));
|
|
53
|
+
this.__props.dampedProgress.x = damp(this.__props.progress.x, this.__props.dampedProgress.x, this.dampFactor, this.dampPrecision);
|
|
54
|
+
this.__props.current.y = clamp(window.pageYOffset, this.__props.start.y, this.__props.end.y);
|
|
55
|
+
this.__props.progress.y = clamp01((this.__props.current.y - this.__props.start.y) / (this.__props.end.y - this.__props.start.y));
|
|
56
|
+
this.__props.dampedProgress.y = damp(this.__props.progress.y, this.__props.dampedProgress.y, this.dampFactor, this.dampPrecision);
|
|
57
|
+
if (this.__props.dampedProgress.x === this.__props.progress.x && this.__props.dampedProgress.y === this.__props.progress.y) {
|
|
58
|
+
this.$services.disable("ticked");
|
|
59
|
+
}
|
|
60
|
+
this.__callMethod("scrolledInView", this.__props);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
this.$on("before-mounted", () => {
|
|
64
|
+
this.$on("resized", delegate);
|
|
65
|
+
this.$on("scrolled", delegate);
|
|
66
|
+
this.$on("ticked", delegate);
|
|
67
|
+
});
|
|
68
|
+
this.$on("mounted", () => {
|
|
69
|
+
this.__setProps();
|
|
70
|
+
});
|
|
71
|
+
this.$on("destroyed", () => {
|
|
72
|
+
this.$off("resized", delegate);
|
|
73
|
+
this.$off("scrolled", delegate);
|
|
74
|
+
this.$off("ticked", delegate);
|
|
75
|
+
});
|
|
38
76
|
}
|
|
39
77
|
mounted() {
|
|
40
|
-
|
|
78
|
+
if (isFunction(super.mounted))
|
|
79
|
+
super.mounted();
|
|
41
80
|
}
|
|
42
|
-
resized() {
|
|
43
|
-
|
|
81
|
+
resized(props) {
|
|
82
|
+
if (isFunction(super.resized))
|
|
83
|
+
super.resized(props);
|
|
44
84
|
}
|
|
45
85
|
scrolled(props) {
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
86
|
+
if (isFunction(super.scrolled))
|
|
87
|
+
super.scrolled(props);
|
|
88
|
+
}
|
|
89
|
+
ticked(props) {
|
|
90
|
+
if (isFunction(super.ticked))
|
|
91
|
+
super.ticked(props);
|
|
49
92
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
this.__props.dampedProgress.x = damp(this.__props.progress.x, this.__props.dampedProgress.x, this.dampFactor, this.dampPrecision);
|
|
54
|
-
this.__props.current.y = clamp(window.pageYOffset, this.__props.start.y, this.__props.end.y);
|
|
55
|
-
this.__props.progress.y = clamp01((this.__props.current.y - this.__props.start.y) / (this.__props.end.y - this.__props.start.y));
|
|
56
|
-
this.__props.dampedProgress.y = damp(this.__props.progress.y, this.__props.dampedProgress.y, this.dampFactor, this.dampPrecision);
|
|
57
|
-
if (this.__props.dampedProgress.x === this.__props.progress.x && this.__props.dampedProgress.y === this.__props.progress.y) {
|
|
58
|
-
this.$services.disable("ticked");
|
|
59
|
-
}
|
|
60
|
-
this.__callMethod("scrolledInView", this.__props);
|
|
93
|
+
destroyed() {
|
|
94
|
+
if (isFunction(super.destroyed))
|
|
95
|
+
super.destroyed();
|
|
61
96
|
}
|
|
62
97
|
__setProps() {
|
|
63
|
-
const sizes = this.$el.getBoundingClientRect();
|
|
64
|
-
const yEnd = sizes.
|
|
98
|
+
const sizes = options.useOffsetSizes ? getOffsetSizes(this.$el) : this.$el.getBoundingClientRect();
|
|
99
|
+
const yEnd = sizes.y + window.pageYOffset + sizes.height;
|
|
65
100
|
const yStart = yEnd - window.innerHeight - sizes.height;
|
|
66
101
|
const yCurrent = clamp(window.pageYOffset, yStart, yEnd);
|
|
67
102
|
const yProgress = clamp01((yCurrent - yStart) / (yEnd - yStart));
|
|
68
|
-
const xEnd = sizes.
|
|
103
|
+
const xEnd = sizes.x + window.pageXOffset + sizes.width;
|
|
69
104
|
const xStart = xEnd - window.innerWidth - sizes.width;
|
|
70
105
|
const xCurrent = clamp(window.pageXOffset, xStart, xEnd);
|
|
71
106
|
const xProgress = clamp01((xCurrent - xStart) / (xEnd - xStart));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@studiometa/js-toolkit",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "A set of useful little bits of JavaScript to boost your project! 🚀",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"toolkit"
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
|
+
"@motionone/easing": "^10.8.0",
|
|
42
43
|
"deepmerge": "^4.2.2"
|
|
43
44
|
}
|
|
44
45
|
}
|
|
@@ -0,0 +1,190 @@
|
|
|
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/css/animate.js
|
|
23
|
+
var animate_exports = {};
|
|
24
|
+
__export(animate_exports, {
|
|
25
|
+
animate: () => animate
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(animate_exports);
|
|
28
|
+
var import_easing = require("@motionone/easing");
|
|
29
|
+
var import_math = require("../math/index.cjs");
|
|
30
|
+
var import_isDefined = __toESM(require("../isDefined.cjs"), 1);
|
|
31
|
+
var import_transform = __toESM(require("./transform.cjs"), 1);
|
|
32
|
+
var id = 0;
|
|
33
|
+
var running = /* @__PURE__ */ new WeakMap();
|
|
34
|
+
var noop = () => {
|
|
35
|
+
};
|
|
36
|
+
var PROGRESS_PRECISION = 1e-4;
|
|
37
|
+
var CSSUnitConverter = {
|
|
38
|
+
"%": (value, sizeRef) => value * sizeRef / 100,
|
|
39
|
+
vh: (value) => value * window.innerHeight / 100,
|
|
40
|
+
vw: (value) => value * window.innerWidth / 100,
|
|
41
|
+
vmin: (value) => value * Math.min(window.innerWidth, window.innerHeight) / 100,
|
|
42
|
+
vmax: (value) => value * Math.max(window.innerWidth, window.innerHeight) / 100
|
|
43
|
+
};
|
|
44
|
+
function getAnimationStepValue(val, sizeRef) {
|
|
45
|
+
if (typeof val === "number") {
|
|
46
|
+
return val;
|
|
47
|
+
}
|
|
48
|
+
if (!val[1] || !CSSUnitConverter[val[1]]) {
|
|
49
|
+
return val[0];
|
|
50
|
+
}
|
|
51
|
+
return CSSUnitConverter[val[1]](val[0], sizeRef);
|
|
52
|
+
}
|
|
53
|
+
function linear(value) {
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
function normalizeEase(ease) {
|
|
57
|
+
if (!(0, import_isDefined.default)(ease)) {
|
|
58
|
+
return linear;
|
|
59
|
+
}
|
|
60
|
+
if (Array.isArray(ease)) {
|
|
61
|
+
return (0, import_easing.cubicBezier)(...ease);
|
|
62
|
+
}
|
|
63
|
+
return ease;
|
|
64
|
+
}
|
|
65
|
+
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);
|
|
66
|
+
var widthBasedTranslateRenderStrategy = generateTranslateRenderStrategy("offsetWidth");
|
|
67
|
+
var heightBasedTranslateRenderStrategy = generateTranslateRenderStrategy("offsetHeight");
|
|
68
|
+
var generateLerpRenderStrategy = (defaultValue) => (element, fromValue, toValue, progress) => (0, import_math.lerp)(fromValue != null ? fromValue : defaultValue, toValue != null ? toValue : defaultValue, progress);
|
|
69
|
+
var scaleRenderStrategy = generateLerpRenderStrategy(1);
|
|
70
|
+
var degreesRenderStrategy = generateLerpRenderStrategy(0);
|
|
71
|
+
var transformRenderStrategies = {
|
|
72
|
+
x: widthBasedTranslateRenderStrategy,
|
|
73
|
+
y: heightBasedTranslateRenderStrategy,
|
|
74
|
+
z: widthBasedTranslateRenderStrategy,
|
|
75
|
+
scale: scaleRenderStrategy,
|
|
76
|
+
scaleX: scaleRenderStrategy,
|
|
77
|
+
scaleY: scaleRenderStrategy,
|
|
78
|
+
scaleZ: scaleRenderStrategy,
|
|
79
|
+
skew: degreesRenderStrategy,
|
|
80
|
+
skewX: degreesRenderStrategy,
|
|
81
|
+
skewY: degreesRenderStrategy,
|
|
82
|
+
rotate: degreesRenderStrategy,
|
|
83
|
+
rotateX: degreesRenderStrategy,
|
|
84
|
+
rotateY: degreesRenderStrategy,
|
|
85
|
+
rotateZ: degreesRenderStrategy
|
|
86
|
+
};
|
|
87
|
+
function render(element, from, to, progress) {
|
|
88
|
+
var _a, _b;
|
|
89
|
+
const stepProgress = to.easing((0, import_math.map)(progress, from.offset, to.offset, 0, 1));
|
|
90
|
+
if ((0, import_isDefined.default)(from.opacity) || (0, import_isDefined.default)(to.opacity)) {
|
|
91
|
+
element.style.opacity = (0, import_math.map)(stepProgress, 0, 1, (_a = from.opacity) != null ? _a : 1, (_b = to.opacity) != null ? _b : 1);
|
|
92
|
+
} else if (element.style.opacity) {
|
|
93
|
+
element.style.opacity = "";
|
|
94
|
+
}
|
|
95
|
+
if ((0, import_isDefined.default)(to.transformOrigin)) {
|
|
96
|
+
element.style.transformOrigin = to.transformOrigin;
|
|
97
|
+
} else if (element.style.transformOrigin) {
|
|
98
|
+
element.style.transformOrigin = "";
|
|
99
|
+
}
|
|
100
|
+
(0, import_transform.default)(element, Object.fromEntries(import_transform.TRANSFORM_PROPS.filter((name) => (0, import_isDefined.default)(from[name]) || (0, import_isDefined.default)(to[name])).map((name) => {
|
|
101
|
+
return [name, transformRenderStrategies[name](element, from[name], to[name], stepProgress)];
|
|
102
|
+
})));
|
|
103
|
+
}
|
|
104
|
+
function animate(element, keyframes, options = {}) {
|
|
105
|
+
var _a;
|
|
106
|
+
let progressValue = 0;
|
|
107
|
+
let easedProgress = 0;
|
|
108
|
+
if (!running.has(element)) {
|
|
109
|
+
running.set(element, /* @__PURE__ */ new Map());
|
|
110
|
+
}
|
|
111
|
+
const ease = normalizeEase(options.easing);
|
|
112
|
+
let duration = (_a = options.duration) != null ? _a : 1;
|
|
113
|
+
duration *= 1e3;
|
|
114
|
+
let startTime = performance.now();
|
|
115
|
+
let endTime = startTime + duration;
|
|
116
|
+
const key = `animate-${id}`;
|
|
117
|
+
id += 1;
|
|
118
|
+
const { onProgress = noop, onFinish = noop } = options;
|
|
119
|
+
let isRunning = false;
|
|
120
|
+
const keyframesCount = keyframes.length - 1;
|
|
121
|
+
const normalizedKeyframes = keyframes.map((keyframe, index) => {
|
|
122
|
+
var _a2;
|
|
123
|
+
return {
|
|
124
|
+
...keyframe,
|
|
125
|
+
offset: (_a2 = keyframe.offset) != null ? _a2 : index / keyframesCount,
|
|
126
|
+
easing: normalizeEase(keyframe.easing)
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
function pause() {
|
|
130
|
+
isRunning = false;
|
|
131
|
+
}
|
|
132
|
+
function progress(newProgress) {
|
|
133
|
+
if (typeof newProgress === "undefined") {
|
|
134
|
+
return progressValue;
|
|
135
|
+
}
|
|
136
|
+
progressValue = newProgress;
|
|
137
|
+
if (Math.abs(1 - progressValue) < PROGRESS_PRECISION) {
|
|
138
|
+
progressValue = 1;
|
|
139
|
+
pause();
|
|
140
|
+
requestAnimationFrame(() => onFinish(progressValue, easedProgress));
|
|
141
|
+
}
|
|
142
|
+
easedProgress = ease(progressValue);
|
|
143
|
+
let toIndex = 0;
|
|
144
|
+
while (normalizedKeyframes[toIndex] && normalizedKeyframes[toIndex].offset <= easedProgress && normalizedKeyframes[toIndex].offset !== 1) {
|
|
145
|
+
toIndex += 1;
|
|
146
|
+
}
|
|
147
|
+
render(element, normalizedKeyframes[toIndex - 1], normalizedKeyframes[toIndex], easedProgress);
|
|
148
|
+
onProgress(progressValue, easedProgress);
|
|
149
|
+
return progressValue;
|
|
150
|
+
}
|
|
151
|
+
function tick(time) {
|
|
152
|
+
if (!isRunning) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
progress((0, import_math.clamp01)((0, import_math.map)(time, startTime, endTime, 0, 1)));
|
|
156
|
+
requestAnimationFrame(tick);
|
|
157
|
+
}
|
|
158
|
+
function start() {
|
|
159
|
+
const runningKeys = running.get(element);
|
|
160
|
+
runningKeys.forEach((runningPause, runningKey) => {
|
|
161
|
+
runningPause();
|
|
162
|
+
runningKeys.delete(runningKey);
|
|
163
|
+
});
|
|
164
|
+
runningKeys.set(key, pause);
|
|
165
|
+
running.set(element, runningKeys);
|
|
166
|
+
startTime = performance.now();
|
|
167
|
+
endTime = startTime + duration;
|
|
168
|
+
progressValue = 0;
|
|
169
|
+
easedProgress = 0;
|
|
170
|
+
isRunning = true;
|
|
171
|
+
requestAnimationFrame(tick);
|
|
172
|
+
}
|
|
173
|
+
function play() {
|
|
174
|
+
if (isRunning) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
startTime = performance.now() - (0, import_math.lerp)(0, duration, progressValue);
|
|
178
|
+
endTime = startTime + duration;
|
|
179
|
+
isRunning = true;
|
|
180
|
+
requestAnimationFrame(tick);
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
start,
|
|
184
|
+
finish: () => progress(1),
|
|
185
|
+
pause,
|
|
186
|
+
play,
|
|
187
|
+
progress
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
if (module.exports.default) module.exports = module.exports.default;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('./transform.js').TransformProps} TransformProps
|
|
3
|
+
* @typedef {import('../math/ease.js').EasingFunction} EasingFn
|
|
4
|
+
* @typedef {[number, number, number, number]} BezierCurve
|
|
5
|
+
* @typedef {{
|
|
6
|
+
* duration?: number;
|
|
7
|
+
* easing?: EasingFn|BezierCurve;
|
|
8
|
+
* onProgress?: (progress: number, easedProgress: number) => void;
|
|
9
|
+
* onFinish?: (progress: number, easedProgress: number) => void;
|
|
10
|
+
* }} Options
|
|
11
|
+
* @typedef {TransformProps & {
|
|
12
|
+
* opacity?: number;
|
|
13
|
+
* transformOrigin?: string;
|
|
14
|
+
* easing?: EasingFn|BezierCurve;
|
|
15
|
+
* offset?: number;
|
|
16
|
+
* }} Keyframe
|
|
17
|
+
* @typedef {Keyframe & {
|
|
18
|
+
* easing: EasingFn;
|
|
19
|
+
* offset: number;
|
|
20
|
+
* }} NormalizedKeyframe
|
|
21
|
+
* @typedef {{
|
|
22
|
+
* start: () => void;
|
|
23
|
+
* pause: () => void;
|
|
24
|
+
* play: () => void;
|
|
25
|
+
* finish: () => void;
|
|
26
|
+
* progress: (value: number?) => number
|
|
27
|
+
* }} Animate
|
|
28
|
+
*/
|
|
29
|
+
/**
|
|
30
|
+
* Animate an element.
|
|
31
|
+
* @param {HTMLElement} element
|
|
32
|
+
* @param {Keyframe[]} keyframes
|
|
33
|
+
* @param {Options} options
|
|
34
|
+
* @returns {Animate}
|
|
35
|
+
*/
|
|
36
|
+
export function animate(element: HTMLElement, keyframes: Keyframe[], options?: Options): Animate;
|
|
37
|
+
export type TransformProps = import('./transform.js').TransformProps;
|
|
38
|
+
export type EasingFn = import('../math/ease.js').EasingFunction;
|
|
39
|
+
export type BezierCurve = [number, number, number, number];
|
|
40
|
+
export type Options = Options;
|
|
41
|
+
export type Keyframe = TransformProps & {
|
|
42
|
+
opacity?: number;
|
|
43
|
+
transformOrigin?: string;
|
|
44
|
+
easing?: EasingFn | [number, number, number, number];
|
|
45
|
+
offset?: number;
|
|
46
|
+
};
|
|
47
|
+
export type NormalizedKeyframe = Keyframe & {
|
|
48
|
+
easing: EasingFn;
|
|
49
|
+
offset: number;
|
|
50
|
+
};
|
|
51
|
+
export type Animate = {
|
|
52
|
+
start: () => void;
|
|
53
|
+
pause: () => void;
|
|
54
|
+
play: () => void;
|
|
55
|
+
finish: () => void;
|
|
56
|
+
progress: (value: number | null) => number;
|
|
57
|
+
};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { cubicBezier } from "@motionone/easing";
|
|
2
|
+
import { lerp, map, clamp01 } from "../math/index.js";
|
|
3
|
+
import isDefined from "../isDefined.js";
|
|
4
|
+
import transform, { TRANSFORM_PROPS } from "./transform.js";
|
|
5
|
+
let id = 0;
|
|
6
|
+
const running = /* @__PURE__ */ new WeakMap();
|
|
7
|
+
const noop = () => {
|
|
8
|
+
};
|
|
9
|
+
const PROGRESS_PRECISION = 1e-4;
|
|
10
|
+
const CSSUnitConverter = {
|
|
11
|
+
"%": (value, sizeRef) => value * sizeRef / 100,
|
|
12
|
+
vh: (value) => value * window.innerHeight / 100,
|
|
13
|
+
vw: (value) => value * window.innerWidth / 100,
|
|
14
|
+
vmin: (value) => value * Math.min(window.innerWidth, window.innerHeight) / 100,
|
|
15
|
+
vmax: (value) => value * Math.max(window.innerWidth, window.innerHeight) / 100
|
|
16
|
+
};
|
|
17
|
+
function getAnimationStepValue(val, sizeRef) {
|
|
18
|
+
if (typeof val === "number") {
|
|
19
|
+
return val;
|
|
20
|
+
}
|
|
21
|
+
if (!val[1] || !CSSUnitConverter[val[1]]) {
|
|
22
|
+
return val[0];
|
|
23
|
+
}
|
|
24
|
+
return CSSUnitConverter[val[1]](val[0], sizeRef);
|
|
25
|
+
}
|
|
26
|
+
function linear(value) {
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
function normalizeEase(ease) {
|
|
30
|
+
if (!isDefined(ease)) {
|
|
31
|
+
return linear;
|
|
32
|
+
}
|
|
33
|
+
if (Array.isArray(ease)) {
|
|
34
|
+
return cubicBezier(...ease);
|
|
35
|
+
}
|
|
36
|
+
return ease;
|
|
37
|
+
}
|
|
38
|
+
const generateTranslateRenderStrategy = (sizeRef) => (element, fromValue, toValue, progress) => lerp(getAnimationStepValue(fromValue != null ? fromValue : 0, element[sizeRef]), getAnimationStepValue(toValue != null ? toValue : 0, element[sizeRef]), progress);
|
|
39
|
+
const widthBasedTranslateRenderStrategy = generateTranslateRenderStrategy("offsetWidth");
|
|
40
|
+
const heightBasedTranslateRenderStrategy = generateTranslateRenderStrategy("offsetHeight");
|
|
41
|
+
const generateLerpRenderStrategy = (defaultValue) => (element, fromValue, toValue, progress) => lerp(fromValue != null ? fromValue : defaultValue, toValue != null ? toValue : defaultValue, progress);
|
|
42
|
+
const scaleRenderStrategy = generateLerpRenderStrategy(1);
|
|
43
|
+
const degreesRenderStrategy = generateLerpRenderStrategy(0);
|
|
44
|
+
const transformRenderStrategies = {
|
|
45
|
+
x: widthBasedTranslateRenderStrategy,
|
|
46
|
+
y: heightBasedTranslateRenderStrategy,
|
|
47
|
+
z: widthBasedTranslateRenderStrategy,
|
|
48
|
+
scale: scaleRenderStrategy,
|
|
49
|
+
scaleX: scaleRenderStrategy,
|
|
50
|
+
scaleY: scaleRenderStrategy,
|
|
51
|
+
scaleZ: scaleRenderStrategy,
|
|
52
|
+
skew: degreesRenderStrategy,
|
|
53
|
+
skewX: degreesRenderStrategy,
|
|
54
|
+
skewY: degreesRenderStrategy,
|
|
55
|
+
rotate: degreesRenderStrategy,
|
|
56
|
+
rotateX: degreesRenderStrategy,
|
|
57
|
+
rotateY: degreesRenderStrategy,
|
|
58
|
+
rotateZ: degreesRenderStrategy
|
|
59
|
+
};
|
|
60
|
+
function render(element, from, to, progress) {
|
|
61
|
+
var _a, _b;
|
|
62
|
+
const stepProgress = to.easing(map(progress, from.offset, to.offset, 0, 1));
|
|
63
|
+
if (isDefined(from.opacity) || isDefined(to.opacity)) {
|
|
64
|
+
element.style.opacity = map(stepProgress, 0, 1, (_a = from.opacity) != null ? _a : 1, (_b = to.opacity) != null ? _b : 1);
|
|
65
|
+
} else if (element.style.opacity) {
|
|
66
|
+
element.style.opacity = "";
|
|
67
|
+
}
|
|
68
|
+
if (isDefined(to.transformOrigin)) {
|
|
69
|
+
element.style.transformOrigin = to.transformOrigin;
|
|
70
|
+
} else if (element.style.transformOrigin) {
|
|
71
|
+
element.style.transformOrigin = "";
|
|
72
|
+
}
|
|
73
|
+
transform(element, Object.fromEntries(TRANSFORM_PROPS.filter((name) => isDefined(from[name]) || isDefined(to[name])).map((name) => {
|
|
74
|
+
return [name, transformRenderStrategies[name](element, from[name], to[name], stepProgress)];
|
|
75
|
+
})));
|
|
76
|
+
}
|
|
77
|
+
function animate(element, keyframes, options = {}) {
|
|
78
|
+
var _a;
|
|
79
|
+
let progressValue = 0;
|
|
80
|
+
let easedProgress = 0;
|
|
81
|
+
if (!running.has(element)) {
|
|
82
|
+
running.set(element, /* @__PURE__ */ new Map());
|
|
83
|
+
}
|
|
84
|
+
const ease = normalizeEase(options.easing);
|
|
85
|
+
let duration = (_a = options.duration) != null ? _a : 1;
|
|
86
|
+
duration *= 1e3;
|
|
87
|
+
let startTime = performance.now();
|
|
88
|
+
let endTime = startTime + duration;
|
|
89
|
+
const key = `animate-${id}`;
|
|
90
|
+
id += 1;
|
|
91
|
+
const { onProgress = noop, onFinish = noop } = options;
|
|
92
|
+
let isRunning = false;
|
|
93
|
+
const keyframesCount = keyframes.length - 1;
|
|
94
|
+
const normalizedKeyframes = keyframes.map((keyframe, index) => {
|
|
95
|
+
var _a2;
|
|
96
|
+
return {
|
|
97
|
+
...keyframe,
|
|
98
|
+
offset: (_a2 = keyframe.offset) != null ? _a2 : index / keyframesCount,
|
|
99
|
+
easing: normalizeEase(keyframe.easing)
|
|
100
|
+
};
|
|
101
|
+
});
|
|
102
|
+
function pause() {
|
|
103
|
+
isRunning = false;
|
|
104
|
+
}
|
|
105
|
+
function progress(newProgress) {
|
|
106
|
+
if (typeof newProgress === "undefined") {
|
|
107
|
+
return progressValue;
|
|
108
|
+
}
|
|
109
|
+
progressValue = newProgress;
|
|
110
|
+
if (Math.abs(1 - progressValue) < PROGRESS_PRECISION) {
|
|
111
|
+
progressValue = 1;
|
|
112
|
+
pause();
|
|
113
|
+
requestAnimationFrame(() => onFinish(progressValue, easedProgress));
|
|
114
|
+
}
|
|
115
|
+
easedProgress = ease(progressValue);
|
|
116
|
+
let toIndex = 0;
|
|
117
|
+
while (normalizedKeyframes[toIndex] && normalizedKeyframes[toIndex].offset <= easedProgress && normalizedKeyframes[toIndex].offset !== 1) {
|
|
118
|
+
toIndex += 1;
|
|
119
|
+
}
|
|
120
|
+
render(element, normalizedKeyframes[toIndex - 1], normalizedKeyframes[toIndex], easedProgress);
|
|
121
|
+
onProgress(progressValue, easedProgress);
|
|
122
|
+
return progressValue;
|
|
123
|
+
}
|
|
124
|
+
function tick(time) {
|
|
125
|
+
if (!isRunning) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
progress(clamp01(map(time, startTime, endTime, 0, 1)));
|
|
129
|
+
requestAnimationFrame(tick);
|
|
130
|
+
}
|
|
131
|
+
function start() {
|
|
132
|
+
const runningKeys = running.get(element);
|
|
133
|
+
runningKeys.forEach((runningPause, runningKey) => {
|
|
134
|
+
runningPause();
|
|
135
|
+
runningKeys.delete(runningKey);
|
|
136
|
+
});
|
|
137
|
+
runningKeys.set(key, pause);
|
|
138
|
+
running.set(element, runningKeys);
|
|
139
|
+
startTime = performance.now();
|
|
140
|
+
endTime = startTime + duration;
|
|
141
|
+
progressValue = 0;
|
|
142
|
+
easedProgress = 0;
|
|
143
|
+
isRunning = true;
|
|
144
|
+
requestAnimationFrame(tick);
|
|
145
|
+
}
|
|
146
|
+
function play() {
|
|
147
|
+
if (isRunning) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
startTime = performance.now() - lerp(0, duration, progressValue);
|
|
151
|
+
endTime = startTime + duration;
|
|
152
|
+
isRunning = true;
|
|
153
|
+
requestAnimationFrame(tick);
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
start,
|
|
157
|
+
finish: () => progress(1),
|
|
158
|
+
pause,
|
|
159
|
+
play,
|
|
160
|
+
progress
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
export {
|
|
164
|
+
animate
|
|
165
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
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/utils/css/getOffsetSizes.js
|
|
20
|
+
var getOffsetSizes_exports = {};
|
|
21
|
+
__export(getOffsetSizes_exports, {
|
|
22
|
+
default: () => getOffsetSizes
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(getOffsetSizes_exports);
|
|
25
|
+
function getOffsetSizes(element) {
|
|
26
|
+
let parent = element;
|
|
27
|
+
let x = -window.pageXOffset;
|
|
28
|
+
let y = -window.pageYOffset;
|
|
29
|
+
while (parent) {
|
|
30
|
+
x += parent.offsetLeft;
|
|
31
|
+
y += parent.offsetTop;
|
|
32
|
+
parent = parent.offsetParent;
|
|
33
|
+
}
|
|
34
|
+
const width = element.offsetWidth;
|
|
35
|
+
const height = element.offsetWidth;
|
|
36
|
+
return {
|
|
37
|
+
x,
|
|
38
|
+
y,
|
|
39
|
+
width,
|
|
40
|
+
height,
|
|
41
|
+
top: y,
|
|
42
|
+
right: width + x,
|
|
43
|
+
bottom: height + y,
|
|
44
|
+
left: x
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
if (module.exports.default) module.exports = module.exports.default;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get a `DOMRect` like `Object` for an element, without its transforms.
|
|
3
|
+
* @param {HTMLElement} element
|
|
4
|
+
* @returns {{ x: number, y: number, width: number, height: number, top: number, right: number, bottom: number, left: number }}
|
|
5
|
+
*/
|
|
6
|
+
export default function getOffsetSizes(element: HTMLElement): {
|
|
7
|
+
x: number;
|
|
8
|
+
y: number;
|
|
9
|
+
width: number;
|
|
10
|
+
height: number;
|
|
11
|
+
top: number;
|
|
12
|
+
right: number;
|
|
13
|
+
bottom: number;
|
|
14
|
+
left: number;
|
|
15
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
function getOffsetSizes(element) {
|
|
2
|
+
let parent = element;
|
|
3
|
+
let x = -window.pageXOffset;
|
|
4
|
+
let y = -window.pageYOffset;
|
|
5
|
+
while (parent) {
|
|
6
|
+
x += parent.offsetLeft;
|
|
7
|
+
y += parent.offsetTop;
|
|
8
|
+
parent = parent.offsetParent;
|
|
9
|
+
}
|
|
10
|
+
const width = element.offsetWidth;
|
|
11
|
+
const height = element.offsetWidth;
|
|
12
|
+
return {
|
|
13
|
+
x,
|
|
14
|
+
y,
|
|
15
|
+
width,
|
|
16
|
+
height,
|
|
17
|
+
top: y,
|
|
18
|
+
right: width + x,
|
|
19
|
+
bottom: height + y,
|
|
20
|
+
left: x
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
getOffsetSizes as default
|
|
25
|
+
};
|
package/utils/css/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
|
|
|
@@ -24,15 +25,20 @@ var css_exports = {};
|
|
|
24
25
|
__export(css_exports, {
|
|
25
26
|
addClass: () => import_classes.add,
|
|
26
27
|
addStyle: () => import_styles.add,
|
|
28
|
+
getOffsetSizes: () => import_getOffsetSizes.default,
|
|
27
29
|
matrix: () => import_matrix.default,
|
|
28
30
|
removeClass: () => import_classes.remove,
|
|
29
31
|
removeStyle: () => import_styles.remove,
|
|
30
32
|
toggleClass: () => import_classes.toggle,
|
|
33
|
+
transform: () => import_transform.default,
|
|
31
34
|
transition: () => import_transition.default
|
|
32
35
|
});
|
|
33
36
|
module.exports = __toCommonJS(css_exports);
|
|
37
|
+
__reExport(css_exports, require("./animate.cjs"), module.exports);
|
|
34
38
|
var import_classes = require("./classes.cjs");
|
|
35
39
|
var import_styles = require("./styles.cjs");
|
|
40
|
+
var import_getOffsetSizes = __toESM(require("./getOffsetSizes.cjs"), 1);
|
|
36
41
|
var import_matrix = __toESM(require("./matrix.cjs"), 1);
|
|
42
|
+
var import_transform = __toESM(require("./transform.cjs"), 1);
|
|
37
43
|
var import_transition = __toESM(require("./transition.cjs"), 1);
|
|
38
44
|
if (module.exports.default) module.exports = module.exports.default;
|
package/utils/css/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
export * from "./animate.js";
|
|
2
|
+
export { default as getOffsetSizes } from "./getOffsetSizes.js";
|
|
1
3
|
export { default as matrix } from "./matrix.js";
|
|
4
|
+
export { default as transform } from "./transform.js";
|
|
2
5
|
export { default as transition } from "./transition.js";
|
|
3
6
|
export { add as addClass, remove as removeClass, toggle as toggleClass } from "./classes.js";
|
|
4
7
|
export { add as addStyle, remove as removeStyle } from "./styles.js";
|