index.mjs 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { GroupAnimationWithThen } from 'motion-dom';
  2. import { animateSequence } from './sequence.mjs';
  3. import { animateSubject } from './subject.mjs';
  4. function isSequence(value) {
  5. return Array.isArray(value) && value.some(Array.isArray);
  6. }
  7. /**
  8. * Creates an animation function that is optionally scoped
  9. * to a specific element.
  10. */
  11. function createScopedAnimate(scope) {
  12. /**
  13. * Implementation
  14. */
  15. function scopedAnimate(subjectOrSequence, optionsOrKeyframes, options) {
  16. let animations = [];
  17. if (isSequence(subjectOrSequence)) {
  18. animations = animateSequence(subjectOrSequence, optionsOrKeyframes, scope);
  19. }
  20. else {
  21. animations = animateSubject(subjectOrSequence, optionsOrKeyframes, options, scope);
  22. }
  23. const animation = new GroupAnimationWithThen(animations);
  24. if (scope) {
  25. scope.animations.push(animation);
  26. }
  27. return animation;
  28. }
  29. return scopedAnimate;
  30. }
  31. const animate = createScopedAnimate();
  32. export { animate, createScopedAnimate };