UNPKG

framer-motion

Version:

A simple and powerful React animation library

43 lines (40 loc) 1.27 kB
import { motionValue } from '../value/index.mjs'; import { isMotionValue } from '../value/utils/is-motion-value.mjs'; import { startAnimation } from './utils/transitions.mjs'; /** * Animate a single value or a `MotionValue`. * * The first argument is either a `MotionValue` to animate, or an initial animation value. * * The second is either a value to animate to, or an array of keyframes to animate through. * * The third argument can be either tween or spring options, and optional lifecycle methods: `onUpdate`, `onPlay`, `onComplete`, `onRepeat` and `onStop`. * * Returns `AnimationPlaybackControls`, currently just a `stop` method. * * ```javascript * const x = useMotionValue(0) * * useEffect(() => { * const controls = animate(x, 100, { * type: "spring", * stiffness: 2000, * onComplete: v => {} * }) * * return controls.stop * }) * ``` * * @public */ function animate(from, to, transition) { if (transition === void 0) { transition = {}; } var value = isMotionValue(from) ? from : motionValue(from); startAnimation("", value, to, transition); return { stop: function () { return value.stop(); }, isAnimating: function () { return value.isAnimating(); }, }; } export { animate };