67 lines
2.6 KiB
JavaScript
67 lines
2.6 KiB
JavaScript
import { defaults, noop, time } from "@motionone/utils";
|
|
import { stopAnimation } from "./stop-animation";
|
|
const createAnimation = (factory) => factory();
|
|
export const withControls = (animationFactory, options, duration = defaults.duration) => {
|
|
return new Proxy({
|
|
animations: animationFactory.map(createAnimation).filter(Boolean),
|
|
duration,
|
|
options,
|
|
}, controls);
|
|
};
|
|
/**
|
|
* TODO:
|
|
* Currently this returns the first animation, ideally it would return
|
|
* the first active animation.
|
|
*/
|
|
const getActiveAnimation = (state) => state.animations[0];
|
|
export const controls = {
|
|
get: (target, key) => {
|
|
const activeAnimation = getActiveAnimation(target);
|
|
switch (key) {
|
|
case "duration":
|
|
return target.duration;
|
|
case "currentTime":
|
|
return time.s((activeAnimation === null || activeAnimation === void 0 ? void 0 : activeAnimation[key]) || 0);
|
|
case "playbackRate":
|
|
case "playState":
|
|
return activeAnimation === null || activeAnimation === void 0 ? void 0 : activeAnimation[key];
|
|
case "finished":
|
|
if (!target.finished) {
|
|
target.finished = Promise.all(target.animations.map(selectFinished)).catch(noop);
|
|
}
|
|
return target.finished;
|
|
case "stop":
|
|
return () => {
|
|
target.animations.forEach((animation) => stopAnimation(animation));
|
|
};
|
|
case "forEachNative":
|
|
/**
|
|
* This is for internal use only, fire a callback for each
|
|
* underlying animation.
|
|
*/
|
|
return (callback) => {
|
|
target.animations.forEach((animation) => callback(animation, target));
|
|
};
|
|
default:
|
|
return typeof (activeAnimation === null || activeAnimation === void 0 ? void 0 : activeAnimation[key]) ===
|
|
"undefined"
|
|
? undefined
|
|
: () => target.animations.forEach((animation) => animation[key]());
|
|
}
|
|
},
|
|
set: (target, key, value) => {
|
|
switch (key) {
|
|
case "currentTime":
|
|
value = time.ms(value);
|
|
// Fall-through
|
|
case "playbackRate":
|
|
for (let i = 0; i < target.animations.length; i++) {
|
|
target.animations[i][key] = value;
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
},
|
|
};
|
|
const selectFinished = (animation) => animation.finished;
|
|
//# sourceMappingURL=controls.js.map
|