use-animation.mjs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { animationControls } from './animation-controls.mjs';
  2. import { useConstant } from '../../utils/use-constant.mjs';
  3. import { useIsomorphicLayoutEffect } from '../../utils/use-isomorphic-effect.mjs';
  4. /**
  5. * Creates `AnimationControls`, which can be used to manually start, stop
  6. * and sequence animations on one or more components.
  7. *
  8. * The returned `AnimationControls` should be passed to the `animate` property
  9. * of the components you want to animate.
  10. *
  11. * These components can then be animated with the `start` method.
  12. *
  13. * ```jsx
  14. * import * as React from 'react'
  15. * import { motion, useAnimation } from 'framer-motion'
  16. *
  17. * export function MyComponent(props) {
  18. * const controls = useAnimation()
  19. *
  20. * controls.start({
  21. * x: 100,
  22. * transition: { duration: 0.5 },
  23. * })
  24. *
  25. * return <motion.div animate={controls} />
  26. * }
  27. * ```
  28. *
  29. * @returns Animation controller with `start` and `stop` methods
  30. *
  31. * @public
  32. */
  33. function useAnimationControls() {
  34. const controls = useConstant(animationControls);
  35. useIsomorphicLayoutEffect(controls.mount, []);
  36. return controls;
  37. }
  38. const useAnimation = useAnimationControls;
  39. export { useAnimation, useAnimationControls };