motion-dom 12.27.5 → 12.28.1

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.
@@ -4066,7 +4066,8 @@
4066
4066
  valueIsDefault = value === (key.startsWith("scale") ? 1 : 0);
4067
4067
  }
4068
4068
  else {
4069
- valueIsDefault = parseFloat(value) === 0;
4069
+ const parsed = parseFloat(value);
4070
+ valueIsDefault = key.startsWith("scale") ? parsed === 1 : parsed === 0;
4070
4071
  }
4071
4072
  if (!valueIsDefault) {
4072
4073
  transformIsDefault = false;
@@ -4681,6 +4682,101 @@
4681
4682
  return useImmediate ? interpolator(inputValue) : interpolator;
4682
4683
  }
4683
4684
 
4685
+ /**
4686
+ * Create a `MotionValue` that animates to its latest value using any transition type.
4687
+ * Can either be a value or track another `MotionValue`.
4688
+ *
4689
+ * ```jsx
4690
+ * const x = motionValue(0)
4691
+ * const y = followValue(x, { type: "spring", stiffness: 300 })
4692
+ * // or with tween
4693
+ * const z = followValue(x, { type: "tween", duration: 0.5, ease: "easeOut" })
4694
+ * ```
4695
+ *
4696
+ * @param source - Initial value or MotionValue to track
4697
+ * @param options - Animation transition options
4698
+ * @returns `MotionValue`
4699
+ *
4700
+ * @public
4701
+ */
4702
+ function followValue(source, options) {
4703
+ const initialValue = isMotionValue(source) ? source.get() : source;
4704
+ const value = motionValue(initialValue);
4705
+ attachFollow(value, source, options);
4706
+ return value;
4707
+ }
4708
+ /**
4709
+ * Attach an animation to a MotionValue that will animate whenever the value changes.
4710
+ * Similar to attachSpring but supports any transition type (spring, tween, inertia, etc.)
4711
+ *
4712
+ * @param value - The MotionValue to animate
4713
+ * @param source - Initial value or MotionValue to track
4714
+ * @param options - Animation transition options
4715
+ * @returns Cleanup function
4716
+ *
4717
+ * @public
4718
+ */
4719
+ function attachFollow(value, source, options = {}) {
4720
+ const initialValue = value.get();
4721
+ let activeAnimation = null;
4722
+ let latestValue = initialValue;
4723
+ let latestSetter;
4724
+ const unit = typeof initialValue === "string"
4725
+ ? initialValue.replace(/[\d.-]/g, "")
4726
+ : undefined;
4727
+ const stopAnimation = () => {
4728
+ if (activeAnimation) {
4729
+ activeAnimation.stop();
4730
+ activeAnimation = null;
4731
+ }
4732
+ };
4733
+ const startAnimation = () => {
4734
+ stopAnimation();
4735
+ const currentValue = asNumber$1(value.get());
4736
+ const targetValue = asNumber$1(latestValue);
4737
+ // Don't animate if we're already at the target
4738
+ if (currentValue === targetValue) {
4739
+ return;
4740
+ }
4741
+ activeAnimation = new JSAnimation({
4742
+ keyframes: [currentValue, targetValue],
4743
+ velocity: value.getVelocity(),
4744
+ // Default to spring if no type specified (matches useSpring behavior)
4745
+ type: "spring",
4746
+ restDelta: 0.001,
4747
+ restSpeed: 0.01,
4748
+ ...options,
4749
+ onUpdate: latestSetter,
4750
+ });
4751
+ };
4752
+ value.attach((v, set) => {
4753
+ latestValue = v;
4754
+ latestSetter = (latest) => set(parseValue(latest, unit));
4755
+ frame.postRender(() => {
4756
+ startAnimation();
4757
+ value["events"].animationStart?.notify();
4758
+ activeAnimation?.then(() => {
4759
+ value["events"].animationComplete?.notify();
4760
+ });
4761
+ });
4762
+ }, stopAnimation);
4763
+ if (isMotionValue(source)) {
4764
+ const removeSourceOnChange = source.on("change", (v) => value.set(parseValue(v, unit)));
4765
+ const removeValueOnDestroy = value.on("destroy", removeSourceOnChange);
4766
+ return () => {
4767
+ removeSourceOnChange();
4768
+ removeValueOnDestroy();
4769
+ };
4770
+ }
4771
+ return stopAnimation;
4772
+ }
4773
+ function parseValue(v, unit) {
4774
+ return unit ? v + unit : v;
4775
+ }
4776
+ function asNumber$1(v) {
4777
+ return typeof v === "number" ? v : parseFloat(v);
4778
+ }
4779
+
4684
4780
  function subscribeValue(inputValues, outputValue, getLatest) {
4685
4781
  const update = () => outputValue.set(getLatest());
4686
4782
  const scheduleUpdate = () => frame.preRender(update, false, true);
@@ -4770,72 +4866,30 @@
4770
4866
  *
4771
4867
  * ```jsx
4772
4868
  * const x = motionValue(0)
4773
- * const y = transformValue(() => x.get() * 2) // double x
4869
+ * const y = springValue(x, { stiffness: 300 })
4774
4870
  * ```
4775
4871
  *
4776
- * @param transformer - A transform function. This function must be pure with no side-effects or conditional statements.
4872
+ * @param source - Initial value or MotionValue to track
4873
+ * @param options - Spring configuration options
4777
4874
  * @returns `MotionValue`
4778
4875
  *
4779
4876
  * @public
4780
4877
  */
4781
4878
  function springValue(source, options) {
4782
- const initialValue = isMotionValue(source) ? source.get() : source;
4783
- const value = motionValue(initialValue);
4784
- attachSpring(value, source, options);
4785
- return value;
4879
+ return followValue(source, { type: "spring", ...options });
4786
4880
  }
4881
+ /**
4882
+ * Attach a spring animation to a MotionValue that will animate whenever the value changes.
4883
+ *
4884
+ * @param value - The MotionValue to animate
4885
+ * @param source - Initial value or MotionValue to track
4886
+ * @param options - Spring configuration options
4887
+ * @returns Cleanup function
4888
+ *
4889
+ * @public
4890
+ */
4787
4891
  function attachSpring(value, source, options) {
4788
- const initialValue = value.get();
4789
- let activeAnimation = null;
4790
- let latestValue = initialValue;
4791
- let latestSetter;
4792
- const unit = typeof initialValue === "string"
4793
- ? initialValue.replace(/[\d.-]/g, "")
4794
- : undefined;
4795
- const stopAnimation = () => {
4796
- if (activeAnimation) {
4797
- activeAnimation.stop();
4798
- activeAnimation = null;
4799
- }
4800
- };
4801
- const startAnimation = () => {
4802
- stopAnimation();
4803
- activeAnimation = new JSAnimation({
4804
- keyframes: [asNumber$1(value.get()), asNumber$1(latestValue)],
4805
- velocity: value.getVelocity(),
4806
- type: "spring",
4807
- restDelta: 0.001,
4808
- restSpeed: 0.01,
4809
- ...options,
4810
- onUpdate: latestSetter,
4811
- });
4812
- };
4813
- value.attach((v, set) => {
4814
- latestValue = v;
4815
- latestSetter = (latest) => set(parseValue(latest, unit));
4816
- frame.postRender(() => {
4817
- startAnimation();
4818
- value['events'].animationStart?.notify();
4819
- activeAnimation?.then(() => {
4820
- value['events'].animationComplete?.notify();
4821
- });
4822
- });
4823
- }, stopAnimation);
4824
- if (isMotionValue(source)) {
4825
- const removeSourceOnChange = source.on("change", (v) => value.set(parseValue(v, unit)));
4826
- const removeValueOnDestroy = value.on("destroy", removeSourceOnChange);
4827
- return () => {
4828
- removeSourceOnChange();
4829
- removeValueOnDestroy();
4830
- };
4831
- }
4832
- return stopAnimation;
4833
- }
4834
- function parseValue(v, unit) {
4835
- return unit ? v + unit : v;
4836
- }
4837
- function asNumber$1(v) {
4838
- return typeof v === "number" ? v : parseFloat(v);
4892
+ return attachFollow(value, source, { type: "spring", ...options });
4839
4893
  }
4840
4894
 
4841
4895
  /**
@@ -6035,7 +6089,8 @@
6035
6089
  valueIsDefault = value === (key.startsWith("scale") ? 1 : 0);
6036
6090
  }
6037
6091
  else {
6038
- valueIsDefault = parseFloat(value) === 0;
6092
+ const parsed = parseFloat(value);
6093
+ valueIsDefault = key.startsWith("scale") ? parsed === 1 : parsed === 0;
6039
6094
  }
6040
6095
  if (!valueIsDefault || transformTemplate) {
6041
6096
  const valueAsType = getValueAsType(value, numberValueTypes[key]);
@@ -9386,6 +9441,7 @@
9386
9441
  exports.applyPxDefaults = applyPxDefaults;
9387
9442
  exports.applyTreeDeltas = applyTreeDeltas;
9388
9443
  exports.aspectRatio = aspectRatio;
9444
+ exports.attachFollow = attachFollow;
9389
9445
  exports.attachSpring = attachSpring;
9390
9446
  exports.attrEffect = attrEffect;
9391
9447
  exports.axisDeltaEquals = axisDeltaEquals;
@@ -9450,6 +9506,7 @@
9450
9506
  exports.findDimensionValueType = findDimensionValueType;
9451
9507
  exports.findValueType = findValueType;
9452
9508
  exports.flushKeyframeResolvers = flushKeyframeResolvers;
9509
+ exports.followValue = followValue;
9453
9510
  exports.frame = frame;
9454
9511
  exports.frameData = frameData;
9455
9512
  exports.frameSteps = frameSteps;