motion-dom 12.28.2 → 12.29.1-alpha.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
@@ -1618,6 +1618,30 @@ class JSAnimation extends WithPromise {
1618
1618
  this.startTime = 0;
1619
1619
  return this.tick(sampleTime, true);
1620
1620
  }
1621
+ /**
1622
+ * Returns whether this animation can provide accurate velocity sampling.
1623
+ * This is false for animations using mixKeyframes (non-numeric values)
1624
+ * or animations that aren't currently running.
1625
+ */
1626
+ get canSampleVelocity() {
1627
+ return (this.state === "running" &&
1628
+ !this.isStopped &&
1629
+ !this.mixKeyframes &&
1630
+ !!this.generator);
1631
+ }
1632
+ /**
1633
+ * Sample the animation's value at a specific time in milliseconds.
1634
+ * Used for velocity calculations via finite differencing.
1635
+ */
1636
+ sampleAt(t) {
1637
+ return this.generator.next(t).value;
1638
+ }
1639
+ /**
1640
+ * The current elapsed time of the animation in milliseconds.
1641
+ */
1642
+ get elapsed() {
1643
+ return this.currentTime;
1644
+ }
1621
1645
  attachTimeline(timeline) {
1622
1646
  if (this.options.allowFlatten) {
1623
1647
  this.options.type = "keyframes";
@@ -3309,6 +3333,7 @@ function shouldBlockAnimation({ protectedKeys, needsAnimating }, key) {
3309
3333
  }
3310
3334
  function animateTarget(visualElement, targetAndTransition, { delay = 0, transitionOverride, type } = {}) {
3311
3335
  let { transition = visualElement.getDefaultTransition(), transitionEnd, ...target } = targetAndTransition;
3336
+ const reduceMotion = transition?.reduceMotion;
3312
3337
  if (transitionOverride)
3313
3338
  transition = transitionOverride;
3314
3339
  const animations = [];
@@ -3354,7 +3379,8 @@ function animateTarget(visualElement, targetAndTransition, { delay = 0, transiti
3354
3379
  }
3355
3380
  }
3356
3381
  addValueToWillChange(visualElement, key);
3357
- value.start(animateMotionValue(key, value, valueTarget, visualElement.shouldReduceMotion && positionalKeys.has(key)
3382
+ const shouldReduceMotion = reduceMotion ?? visualElement.shouldReduceMotion;
3383
+ value.start(animateMotionValue(key, value, valueTarget, shouldReduceMotion && positionalKeys.has(key)
3358
3384
  ? { type: false }
3359
3385
  : valueTransition, visualElement, isHandoff));
3360
3386
  const animation = value.animation;
@@ -4734,16 +4760,25 @@ function attachFollow(value, source, options = {}) {
4734
4760
  }
4735
4761
  };
4736
4762
  const startAnimation = () => {
4737
- stopAnimation();
4738
4763
  const currentValue = asNumber$1(value.get());
4739
4764
  const targetValue = asNumber$1(latestValue);
4740
4765
  // Don't animate if we're already at the target
4741
4766
  if (currentValue === targetValue) {
4767
+ stopAnimation();
4742
4768
  return;
4743
4769
  }
4770
+ // Get velocity from the running animation before stopping it.
4771
+ // This provides the instantaneous velocity from the spring physics,
4772
+ // which is more accurate than the motion value's finite difference.
4773
+ let velocity = value.getVelocity();
4774
+ if (activeAnimation?.canSampleVelocity) {
4775
+ const elapsed = activeAnimation.elapsed;
4776
+ velocity = calcGeneratorVelocity((t) => activeAnimation.sampleAt(t), elapsed, activeAnimation.sampleAt(elapsed));
4777
+ }
4778
+ stopAnimation();
4744
4779
  activeAnimation = new JSAnimation({
4745
4780
  keyframes: [currentValue, targetValue],
4746
- velocity: value.getVelocity(),
4781
+ velocity,
4747
4782
  // Default to spring if no type specified (matches useSpring behavior)
4748
4783
  type: "spring",
4749
4784
  restDelta: 0.001,
@@ -7340,17 +7375,30 @@ class NodeStack {
7340
7375
  if (prevLead) {
7341
7376
  prevLead.instance && prevLead.scheduleRender();
7342
7377
  node.scheduleRender();
7343
- node.resumeFrom = prevLead;
7344
- if (preserveFollowOpacity) {
7345
- node.resumeFrom.preserveOpacity = true;
7346
- }
7347
- if (prevLead.snapshot) {
7348
- node.snapshot = prevLead.snapshot;
7349
- node.snapshot.latestValues =
7350
- prevLead.animationValues || prevLead.latestValues;
7351
- }
7352
- if (node.root && node.root.isUpdating) {
7353
- node.isLayoutDirty = true;
7378
+ /**
7379
+ * If both the new and previous lead have the same defined layoutDependency,
7380
+ * skip the shared layout animation. This allows components with layoutId
7381
+ * to opt-out of animations when their layoutDependency hasn't changed,
7382
+ * even when the component unmounts and remounts in a different location.
7383
+ */
7384
+ const prevDep = prevLead.options.layoutDependency;
7385
+ const nextDep = node.options.layoutDependency;
7386
+ const dependencyMatches = prevDep !== undefined &&
7387
+ nextDep !== undefined &&
7388
+ prevDep === nextDep;
7389
+ if (!dependencyMatches) {
7390
+ node.resumeFrom = prevLead;
7391
+ if (preserveFollowOpacity) {
7392
+ node.resumeFrom.preserveOpacity = true;
7393
+ }
7394
+ if (prevLead.snapshot) {
7395
+ node.snapshot = prevLead.snapshot;
7396
+ node.snapshot.latestValues =
7397
+ prevLead.animationValues || prevLead.latestValues;
7398
+ }
7399
+ if (node.root && node.root.isUpdating) {
7400
+ node.isLayoutDirty = true;
7401
+ }
7354
7402
  }
7355
7403
  const { crossfade } = node.options;
7356
7404
  if (crossfade === false) {