motion 11.17.0 → 11.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -1367,7 +1367,7 @@ class MotionValue {
1367
1367
  * This will be replaced by the build step with the latest version number.
1368
1368
  * When MotionValues are provided to motion components, warn if versions are mixed.
1369
1369
  */
1370
- this.version = "11.17.0";
1370
+ this.version = "11.18.0";
1371
1371
  /**
1372
1372
  * Tracks whether this value can output a velocity. Currently this is only true
1373
1373
  * if the value is numerical, but we might be able to widen the scope here and support
@@ -4297,7 +4297,7 @@ function updateMotionValuesFromProps(element, next, prev) {
4297
4297
  * and warn against mismatches.
4298
4298
  */
4299
4299
  if (process.env.NODE_ENV === "development") {
4300
- warnOnce(nextValue.version === "11.17.0", `Attempting to mix Motion versions ${nextValue.version} with 11.17.0 may not work as expected.`);
4300
+ warnOnce(nextValue.version === "11.18.0", `Attempting to mix Motion versions ${nextValue.version} with 11.18.0 may not work as expected.`);
4301
4301
  }
4302
4302
  }
4303
4303
  else if (isMotionValue(prevValue)) {
@@ -4430,7 +4430,8 @@ class VisualElement {
4430
4430
  frame.render(this.render, false, true);
4431
4431
  }
4432
4432
  };
4433
- const { latestValues, renderState } = visualState;
4433
+ const { latestValues, renderState, onUpdate } = visualState;
4434
+ this.onUpdate = onUpdate;
4434
4435
  this.latestValues = latestValues;
4435
4436
  this.baseTarget = { ...latestValues };
4436
4437
  this.initialValues = props.initial ? { ...latestValues } : {};
@@ -4630,6 +4631,7 @@ class VisualElement {
4630
4631
  if (this.handleChildMotionValue) {
4631
4632
  this.handleChildMotionValue();
4632
4633
  }
4634
+ this.onUpdate && this.onUpdate(this);
4633
4635
  }
4634
4636
  getProps() {
4635
4637
  return this.props;
package/dist/cjs/mini.js CHANGED
@@ -139,7 +139,7 @@ class GroupPlaybackControls extends BaseGroupPlaybackControls {
139
139
  }
140
140
  }
141
141
 
142
- function getValueTransition(transition, key) {
142
+ function getValueTransition$1(transition, key) {
143
143
  return transition
144
144
  ? transition[key] ||
145
145
  transition["default"] ||
@@ -356,6 +356,399 @@ function resolveElements(elementOrSelector, scope, selectorCache) {
356
356
  return Array.from(elementOrSelector);
357
357
  }
358
358
 
359
+ const wrap = (min, max, v) => {
360
+ const rangeSize = max - min;
361
+ return ((((v - min) % rangeSize) + rangeSize) % rangeSize) + min;
362
+ };
363
+
364
+ const isEasingArray = (ease) => {
365
+ return Array.isArray(ease) && typeof ease[0] !== "number";
366
+ };
367
+
368
+ function getEasingForSegment(easing, i) {
369
+ return isEasingArray(easing) ? easing[wrap(0, easing.length, i)] : easing;
370
+ }
371
+
372
+ /*
373
+ Value in range from progress
374
+
375
+ Given a lower limit and an upper limit, we return the value within
376
+ that range as expressed by progress (usually a number from 0 to 1)
377
+
378
+ So progress = 0.5 would change
379
+
380
+ from -------- to
381
+
382
+ to
383
+
384
+ from ---- to
385
+
386
+ E.g. from = 10, to = 20, progress = 0.5 => 15
387
+
388
+ @param [number]: Lower limit of range
389
+ @param [number]: Upper limit of range
390
+ @param [number]: The progress between lower and upper limits expressed 0-1
391
+ @return [number]: Value as calculated from progress within range (not limited within range)
392
+ */
393
+ const mixNumber = (from, to, progress) => {
394
+ return from + (to - from) * progress;
395
+ };
396
+
397
+ function fillOffset(offset, remaining) {
398
+ const min = offset[offset.length - 1];
399
+ for (let i = 1; i <= remaining; i++) {
400
+ const offsetProgress = progress(0, remaining, i);
401
+ offset.push(mixNumber(min, 1, offsetProgress));
402
+ }
403
+ }
404
+
405
+ function defaultOffset(arr) {
406
+ const offset = [0];
407
+ fillOffset(offset, arr.length - 1);
408
+ return offset;
409
+ }
410
+
411
+ const isMotionValue = (value) => Boolean(value && value.getVelocity);
412
+
413
+ function isDOMKeyframes(keyframes) {
414
+ return typeof keyframes === "object" && !Array.isArray(keyframes);
415
+ }
416
+
417
+ function resolveSubjects(subject, keyframes, scope, selectorCache) {
418
+ if (typeof subject === "string" && isDOMKeyframes(keyframes)) {
419
+ return resolveElements(subject, scope, selectorCache);
420
+ }
421
+ else if (subject instanceof NodeList) {
422
+ return Array.from(subject);
423
+ }
424
+ else if (Array.isArray(subject)) {
425
+ return subject;
426
+ }
427
+ else {
428
+ return [subject];
429
+ }
430
+ }
431
+
432
+ function calculateRepeatDuration(duration, repeat, _repeatDelay) {
433
+ return duration * (repeat + 1);
434
+ }
435
+
436
+ /**
437
+ * Given a absolute or relative time definition and current/prev time state of the sequence,
438
+ * calculate an absolute time for the next keyframes.
439
+ */
440
+ function calcNextTime(current, next, prev, labels) {
441
+ var _a;
442
+ if (typeof next === "number") {
443
+ return next;
444
+ }
445
+ else if (next.startsWith("-") || next.startsWith("+")) {
446
+ return Math.max(0, current + parseFloat(next));
447
+ }
448
+ else if (next === "<") {
449
+ return prev;
450
+ }
451
+ else {
452
+ return (_a = labels.get(next)) !== null && _a !== void 0 ? _a : current;
453
+ }
454
+ }
455
+
456
+ function removeItem(arr, item) {
457
+ const index = arr.indexOf(item);
458
+ if (index > -1)
459
+ arr.splice(index, 1);
460
+ }
461
+
462
+ function eraseKeyframes(sequence, startTime, endTime) {
463
+ for (let i = 0; i < sequence.length; i++) {
464
+ const keyframe = sequence[i];
465
+ if (keyframe.at > startTime && keyframe.at < endTime) {
466
+ removeItem(sequence, keyframe);
467
+ // If we remove this item we have to push the pointer back one
468
+ i--;
469
+ }
470
+ }
471
+ }
472
+ function addKeyframes(sequence, keyframes, easing, offset, startTime, endTime) {
473
+ /**
474
+ * Erase every existing value between currentTime and targetTime,
475
+ * this will essentially splice this timeline into any currently
476
+ * defined ones.
477
+ */
478
+ eraseKeyframes(sequence, startTime, endTime);
479
+ for (let i = 0; i < keyframes.length; i++) {
480
+ sequence.push({
481
+ value: keyframes[i],
482
+ at: mixNumber(startTime, endTime, offset[i]),
483
+ easing: getEasingForSegment(easing, i),
484
+ });
485
+ }
486
+ }
487
+
488
+ /**
489
+ * Take an array of times that represent repeated keyframes. For instance
490
+ * if we have original times of [0, 0.5, 1] then our repeated times will
491
+ * be [0, 0.5, 1, 1, 1.5, 2]. Loop over the times and scale them back
492
+ * down to a 0-1 scale.
493
+ */
494
+ function normalizeTimes(times, repeat) {
495
+ for (let i = 0; i < times.length; i++) {
496
+ times[i] = times[i] / (repeat + 1);
497
+ }
498
+ }
499
+
500
+ function compareByTime(a, b) {
501
+ if (a.at === b.at) {
502
+ if (a.value === null)
503
+ return 1;
504
+ if (b.value === null)
505
+ return -1;
506
+ return 0;
507
+ }
508
+ else {
509
+ return a.at - b.at;
510
+ }
511
+ }
512
+
513
+ const defaultSegmentEasing = "easeInOut";
514
+ const MAX_REPEAT = 20;
515
+ function createAnimationsFromSequence(sequence, { defaultTransition = {}, ...sequenceTransition } = {}, scope, generators) {
516
+ const defaultDuration = defaultTransition.duration || 0.3;
517
+ const animationDefinitions = new Map();
518
+ const sequences = new Map();
519
+ const elementCache = {};
520
+ const timeLabels = new Map();
521
+ let prevTime = 0;
522
+ let currentTime = 0;
523
+ let totalDuration = 0;
524
+ /**
525
+ * Build the timeline by mapping over the sequence array and converting
526
+ * the definitions into keyframes and offsets with absolute time values.
527
+ * These will later get converted into relative offsets in a second pass.
528
+ */
529
+ for (let i = 0; i < sequence.length; i++) {
530
+ const segment = sequence[i];
531
+ /**
532
+ * If this is a timeline label, mark it and skip the rest of this iteration.
533
+ */
534
+ if (typeof segment === "string") {
535
+ timeLabels.set(segment, currentTime);
536
+ continue;
537
+ }
538
+ else if (!Array.isArray(segment)) {
539
+ timeLabels.set(segment.name, calcNextTime(currentTime, segment.at, prevTime, timeLabels));
540
+ continue;
541
+ }
542
+ let [subject, keyframes, transition = {}] = segment;
543
+ /**
544
+ * If a relative or absolute time value has been specified we need to resolve
545
+ * it in relation to the currentTime.
546
+ */
547
+ if (transition.at !== undefined) {
548
+ currentTime = calcNextTime(currentTime, transition.at, prevTime, timeLabels);
549
+ }
550
+ /**
551
+ * Keep track of the maximum duration in this definition. This will be
552
+ * applied to currentTime once the definition has been parsed.
553
+ */
554
+ let maxDuration = 0;
555
+ const resolveValueSequence = (valueKeyframes, valueTransition, valueSequence, elementIndex = 0, numSubjects = 0) => {
556
+ const valueKeyframesAsList = keyframesAsList(valueKeyframes);
557
+ const { delay = 0, times = defaultOffset(valueKeyframesAsList), type = "keyframes", repeat, repeatType, repeatDelay = 0, ...remainingTransition } = valueTransition;
558
+ let { ease = defaultTransition.ease || "easeOut", duration } = valueTransition;
559
+ /**
560
+ * Resolve stagger() if defined.
561
+ */
562
+ const calculatedDelay = typeof delay === "function"
563
+ ? delay(elementIndex, numSubjects)
564
+ : delay;
565
+ /**
566
+ * If this animation should and can use a spring, generate a spring easing function.
567
+ */
568
+ const numKeyframes = valueKeyframesAsList.length;
569
+ const createGenerator = isGenerator(type)
570
+ ? type
571
+ : generators === null || generators === void 0 ? void 0 : generators[type];
572
+ if (numKeyframes <= 2 && createGenerator) {
573
+ /**
574
+ * As we're creating an easing function from a spring,
575
+ * ideally we want to generate it using the real distance
576
+ * between the two keyframes. However this isn't always
577
+ * possible - in these situations we use 0-100.
578
+ */
579
+ let absoluteDelta = 100;
580
+ if (numKeyframes === 2 &&
581
+ isNumberKeyframesArray(valueKeyframesAsList)) {
582
+ const delta = valueKeyframesAsList[1] - valueKeyframesAsList[0];
583
+ absoluteDelta = Math.abs(delta);
584
+ }
585
+ const springTransition = { ...remainingTransition };
586
+ if (duration !== undefined) {
587
+ springTransition.duration = secondsToMilliseconds(duration);
588
+ }
589
+ const springEasing = createGeneratorEasing(springTransition, absoluteDelta, createGenerator);
590
+ ease = springEasing.ease;
591
+ duration = springEasing.duration;
592
+ }
593
+ duration !== null && duration !== void 0 ? duration : (duration = defaultDuration);
594
+ const startTime = currentTime + calculatedDelay;
595
+ /**
596
+ * If there's only one time offset of 0, fill in a second with length 1
597
+ */
598
+ if (times.length === 1 && times[0] === 0) {
599
+ times[1] = 1;
600
+ }
601
+ /**
602
+ * Fill out if offset if fewer offsets than keyframes
603
+ */
604
+ const remainder = times.length - valueKeyframesAsList.length;
605
+ remainder > 0 && fillOffset(times, remainder);
606
+ /**
607
+ * If only one value has been set, ie [1], push a null to the start of
608
+ * the keyframe array. This will let us mark a keyframe at this point
609
+ * that will later be hydrated with the previous value.
610
+ */
611
+ valueKeyframesAsList.length === 1 &&
612
+ valueKeyframesAsList.unshift(null);
613
+ /**
614
+ * Handle repeat options
615
+ */
616
+ if (repeat) {
617
+ invariant(repeat < MAX_REPEAT, "Repeat count too high, must be less than 20");
618
+ duration = calculateRepeatDuration(duration, repeat);
619
+ const originalKeyframes = [...valueKeyframesAsList];
620
+ const originalTimes = [...times];
621
+ ease = Array.isArray(ease) ? [...ease] : [ease];
622
+ const originalEase = [...ease];
623
+ for (let repeatIndex = 0; repeatIndex < repeat; repeatIndex++) {
624
+ valueKeyframesAsList.push(...originalKeyframes);
625
+ for (let keyframeIndex = 0; keyframeIndex < originalKeyframes.length; keyframeIndex++) {
626
+ times.push(originalTimes[keyframeIndex] + (repeatIndex + 1));
627
+ ease.push(keyframeIndex === 0
628
+ ? "linear"
629
+ : getEasingForSegment(originalEase, keyframeIndex - 1));
630
+ }
631
+ }
632
+ normalizeTimes(times, repeat);
633
+ }
634
+ const targetTime = startTime + duration;
635
+ /**
636
+ * Add keyframes, mapping offsets to absolute time.
637
+ */
638
+ addKeyframes(valueSequence, valueKeyframesAsList, ease, times, startTime, targetTime);
639
+ maxDuration = Math.max(calculatedDelay + duration, maxDuration);
640
+ totalDuration = Math.max(targetTime, totalDuration);
641
+ };
642
+ if (isMotionValue(subject)) {
643
+ const subjectSequence = getSubjectSequence(subject, sequences);
644
+ resolveValueSequence(keyframes, transition, getValueSequence("default", subjectSequence));
645
+ }
646
+ else {
647
+ const subjects = resolveSubjects(subject, keyframes, scope, elementCache);
648
+ const numSubjects = subjects.length;
649
+ /**
650
+ * For every element in this segment, process the defined values.
651
+ */
652
+ for (let subjectIndex = 0; subjectIndex < numSubjects; subjectIndex++) {
653
+ /**
654
+ * Cast necessary, but we know these are of this type
655
+ */
656
+ keyframes = keyframes;
657
+ transition = transition;
658
+ const thisSubject = subjects[subjectIndex];
659
+ const subjectSequence = getSubjectSequence(thisSubject, sequences);
660
+ for (const key in keyframes) {
661
+ resolveValueSequence(keyframes[key], getValueTransition(transition, key), getValueSequence(key, subjectSequence), subjectIndex, numSubjects);
662
+ }
663
+ }
664
+ }
665
+ prevTime = currentTime;
666
+ currentTime += maxDuration;
667
+ }
668
+ /**
669
+ * For every element and value combination create a new animation.
670
+ */
671
+ sequences.forEach((valueSequences, element) => {
672
+ for (const key in valueSequences) {
673
+ const valueSequence = valueSequences[key];
674
+ /**
675
+ * Arrange all the keyframes in ascending time order.
676
+ */
677
+ valueSequence.sort(compareByTime);
678
+ const keyframes = [];
679
+ const valueOffset = [];
680
+ const valueEasing = [];
681
+ /**
682
+ * For each keyframe, translate absolute times into
683
+ * relative offsets based on the total duration of the timeline.
684
+ */
685
+ for (let i = 0; i < valueSequence.length; i++) {
686
+ const { at, value, easing } = valueSequence[i];
687
+ keyframes.push(value);
688
+ valueOffset.push(progress(0, totalDuration, at));
689
+ valueEasing.push(easing || "easeOut");
690
+ }
691
+ /**
692
+ * If the first keyframe doesn't land on offset: 0
693
+ * provide one by duplicating the initial keyframe. This ensures
694
+ * it snaps to the first keyframe when the animation starts.
695
+ */
696
+ if (valueOffset[0] !== 0) {
697
+ valueOffset.unshift(0);
698
+ keyframes.unshift(keyframes[0]);
699
+ valueEasing.unshift(defaultSegmentEasing);
700
+ }
701
+ /**
702
+ * If the last keyframe doesn't land on offset: 1
703
+ * provide one with a null wildcard value. This will ensure it
704
+ * stays static until the end of the animation.
705
+ */
706
+ if (valueOffset[valueOffset.length - 1] !== 1) {
707
+ valueOffset.push(1);
708
+ keyframes.push(null);
709
+ }
710
+ if (!animationDefinitions.has(element)) {
711
+ animationDefinitions.set(element, {
712
+ keyframes: {},
713
+ transition: {},
714
+ });
715
+ }
716
+ const definition = animationDefinitions.get(element);
717
+ definition.keyframes[key] = keyframes;
718
+ definition.transition[key] = {
719
+ ...defaultTransition,
720
+ duration: totalDuration,
721
+ ease: valueEasing,
722
+ times: valueOffset,
723
+ ...sequenceTransition,
724
+ };
725
+ }
726
+ });
727
+ return animationDefinitions;
728
+ }
729
+ function getSubjectSequence(subject, sequences) {
730
+ !sequences.has(subject) && sequences.set(subject, {});
731
+ return sequences.get(subject);
732
+ }
733
+ function getValueSequence(name, sequences) {
734
+ if (!sequences[name])
735
+ sequences[name] = [];
736
+ return sequences[name];
737
+ }
738
+ function keyframesAsList(keyframes) {
739
+ return Array.isArray(keyframes) ? keyframes : [keyframes];
740
+ }
741
+ function getValueTransition(transition, key) {
742
+ return transition && transition[key]
743
+ ? {
744
+ ...transition,
745
+ ...transition[key],
746
+ }
747
+ : { ...transition };
748
+ }
749
+ const isNumber = (keyframe) => typeof keyframe === "number";
750
+ const isNumberKeyframesArray = (keyframes) => keyframes.every(isNumber);
751
+
359
752
  function startWaapiAnimation(element, valueName, keyframes, { delay = 0, duration = 300, repeat = 0, repeatType = "loop", ease = "easeInOut", times, } = {}) {
360
753
  const keyframeOptions = { [valueName]: keyframes };
361
754
  if (times)
@@ -567,7 +960,7 @@ function animateElements(elementOrSelector, keyframes, options, scope) {
567
960
  for (const valueName in keyframes) {
568
961
  const valueKeyframes = keyframes[valueName];
569
962
  const valueOptions = {
570
- ...getValueTransition(elementTransition, valueName),
963
+ ...getValueTransition$1(elementTransition, valueName),
571
964
  };
572
965
  valueOptions.duration = valueOptions.duration
573
966
  ? secondsToMilliseconds(valueOptions.duration)
@@ -579,6 +972,14 @@ function animateElements(elementOrSelector, keyframes, options, scope) {
579
972
  return animations;
580
973
  }
581
974
 
975
+ function animateSequence(definition, options) {
976
+ const animations = [];
977
+ createAnimationsFromSequence(definition, options).forEach(({ keyframes, transition }, element) => {
978
+ animations.push(...animateElements(element, keyframes, transition));
979
+ });
980
+ return new GroupPlaybackControls(animations);
981
+ }
982
+
582
983
  const createScopedWaapiAnimate = (scope) => {
583
984
  function scopedAnimate(elementOrSelector, keyframes, options) {
584
985
  return new GroupPlaybackControls(animateElements(elementOrSelector, keyframes, options, scope));
@@ -588,3 +989,4 @@ const createScopedWaapiAnimate = (scope) => {
588
989
  const animateMini = /*@__PURE__*/ createScopedWaapiAnimate();
589
990
 
590
991
  exports.animate = animateMini;
992
+ exports.animateSequence = animateSequence;