motion-values.mjs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { motionValue } from 'motion-dom';
  2. import { warnOnce } from 'motion-utils';
  3. import { isMotionValue } from '../../value/utils/is-motion-value.mjs';
  4. function updateMotionValuesFromProps(element, next, prev) {
  5. for (const key in next) {
  6. const nextValue = next[key];
  7. const prevValue = prev[key];
  8. if (isMotionValue(nextValue)) {
  9. /**
  10. * If this is a motion value found in props or style, we want to add it
  11. * to our visual element's motion value map.
  12. */
  13. element.addValue(key, nextValue);
  14. /**
  15. * Check the version of the incoming motion value with this version
  16. * and warn against mismatches.
  17. */
  18. if (process.env.NODE_ENV === "development") {
  19. warnOnce(nextValue.version === "12.7.3", `Attempting to mix Motion versions ${nextValue.version} with 12.7.3 may not work as expected.`);
  20. }
  21. }
  22. else if (isMotionValue(prevValue)) {
  23. /**
  24. * If we're swapping from a motion value to a static value,
  25. * create a new motion value from that
  26. */
  27. element.addValue(key, motionValue(nextValue, { owner: element }));
  28. }
  29. else if (prevValue !== nextValue) {
  30. /**
  31. * If this is a flat value that has changed, update the motion value
  32. * or create one if it doesn't exist. We only want to do this if we're
  33. * not handling the value with our animation state.
  34. */
  35. if (element.hasValue(key)) {
  36. const existingValue = element.getValue(key);
  37. if (existingValue.liveStyle === true) {
  38. existingValue.jump(nextValue);
  39. }
  40. else if (!existingValue.hasAnimated) {
  41. existingValue.set(nextValue);
  42. }
  43. }
  44. else {
  45. const latestValue = element.getStaticValue(key);
  46. element.addValue(key, motionValue(latestValue !== undefined ? latestValue : nextValue, { owner: element }));
  47. }
  48. }
  49. }
  50. // Handle removed values
  51. for (const key in prev) {
  52. if (next[key] === undefined)
  53. element.removeValue(key);
  54. }
  55. return next;
  56. }
  57. export { updateMotionValuesFromProps };