UNPKG

motion-dom

Version:
49 lines (46 loc) • 2.03 kB
import { invariant, removeItem } from 'motion-utils'; import { motionValue } from '../../value/index.mjs'; import { animateMotionValue } from '../interfaces/motion-value.mjs'; import { getValueTransition } from '../utils/get-value-transition.mjs'; /** * Effects registered via `animate.addEffect()`, most recent first. */ const effects = []; function addEffect(effect) { invariant(typeof effect.test === "function" && typeof effect.read === "function", "Effects passed to animate.addEffect() need test() and read().", "effect-missing-test"); removeEffect(effect); effects.unshift(effect); } function removeEffect(effect) { removeItem(effects, effect); } function findEffect(subject) { return effects.find((effect) => effect.test(subject)); } /** * Animate the keys of `subject` via `effect`. Motion values are created on * first animation, seeded from `effect.read()` or the first keyframe, and * then bound to the subject via the effect for the rest of its life. */ function animateEffectSubject(effect, subject, keyframes, transition = {}) { const animations = []; for (const key in keyframes) { const target = keyframes[key]; let value = effect.get(subject, key); if (!value) { const initial = effect.read(subject, key, target) ?? firstKeyframe(target); invariant(initial !== undefined, `"${key}" can't be read from the animated subject. Provide [from, to] keyframes.`, "effect-unreadable-value"); value = motionValue(initial); effect(subject, { [key]: value }); } value.start(animateMotionValue(key, value, target, getValueTransition(transition, key))); value.animation && animations.push(value.animation); } return animations; } function firstKeyframe(target) { const first = Array.isArray(target) ? target[0] : undefined; return first === null ? undefined : first; } export { addEffect, animateEffectSubject, findEffect, removeEffect }; //# sourceMappingURL=effects.mjs.map