subject.mjs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { invariant } from 'motion-utils';
  2. import { visualElementStore } from '../../render/store.mjs';
  3. import { isMotionValue } from '../../value/utils/is-motion-value.mjs';
  4. import { animateTarget } from '../interfaces/visual-element-target.mjs';
  5. import { createDOMVisualElement, createObjectVisualElement } from '../utils/create-visual-element.mjs';
  6. import { isDOMKeyframes } from '../utils/is-dom-keyframes.mjs';
  7. import { resolveSubjects } from './resolve-subjects.mjs';
  8. import { animateSingleValue } from './single-value.mjs';
  9. function isSingleValue(subject, keyframes) {
  10. return (isMotionValue(subject) ||
  11. typeof subject === "number" ||
  12. (typeof subject === "string" && !isDOMKeyframes(keyframes)));
  13. }
  14. /**
  15. * Implementation
  16. */
  17. function animateSubject(subject, keyframes, options, scope) {
  18. const animations = [];
  19. if (isSingleValue(subject, keyframes)) {
  20. animations.push(animateSingleValue(subject, isDOMKeyframes(keyframes)
  21. ? keyframes.default || keyframes
  22. : keyframes, options ? options.default || options : options));
  23. }
  24. else {
  25. const subjects = resolveSubjects(subject, keyframes, scope);
  26. const numSubjects = subjects.length;
  27. invariant(Boolean(numSubjects), "No valid elements provided.");
  28. for (let i = 0; i < numSubjects; i++) {
  29. const thisSubject = subjects[i];
  30. const createVisualElement = thisSubject instanceof Element
  31. ? createDOMVisualElement
  32. : createObjectVisualElement;
  33. if (!visualElementStore.has(thisSubject)) {
  34. createVisualElement(thisSubject);
  35. }
  36. const visualElement = visualElementStore.get(thisSubject);
  37. const transition = { ...options };
  38. /**
  39. * Resolve stagger function if provided.
  40. */
  41. if ("delay" in transition &&
  42. typeof transition.delay === "function") {
  43. transition.delay = transition.delay(i, numSubjects);
  44. }
  45. animations.push(...animateTarget(visualElement, { ...keyframes, transition }, {}));
  46. }
  47. }
  48. return animations;
  49. }
  50. export { animateSubject };