motion-dom 13.5.0 → 13.5.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.
Files changed (34) hide show
  1. package/dist/cjs/index.js +284 -443
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/es/animation/NativeAnimation.mjs +4 -1
  4. package/dist/es/animation/NativeAnimation.mjs.map +1 -1
  5. package/dist/es/animation/interfaces/visual-element-target.mjs +2 -4
  6. package/dist/es/animation/interfaces/visual-element-target.mjs.map +1 -1
  7. package/dist/es/animation/interfaces/visual-element-variant.mjs +29 -49
  8. package/dist/es/animation/interfaces/visual-element-variant.mjs.map +1 -1
  9. package/dist/es/animation/interfaces/visual-element.mjs +5 -14
  10. package/dist/es/animation/interfaces/visual-element.mjs.map +1 -1
  11. package/dist/es/effects/MotionValueState.mjs +6 -1
  12. package/dist/es/effects/MotionValueState.mjs.map +1 -1
  13. package/dist/es/effects/style/index.mjs +1 -1
  14. package/dist/es/effects/style/index.mjs.map +1 -1
  15. package/dist/es/effects/svg/index.mjs +18 -7
  16. package/dist/es/effects/svg/index.mjs.map +1 -1
  17. package/dist/es/index.mjs +2 -1
  18. package/dist/es/index.mjs.map +1 -1
  19. package/dist/es/render/VisualElement.mjs +17 -57
  20. package/dist/es/render/VisualElement.mjs.map +1 -1
  21. package/dist/es/render/utils/animation-state.mjs +144 -267
  22. package/dist/es/render/utils/animation-state.mjs.map +1 -1
  23. package/dist/es/render/utils/resolve-dynamic-variants.mjs +7 -3
  24. package/dist/es/render/utils/resolve-dynamic-variants.mjs.map +1 -1
  25. package/dist/es/render/utils/resolve-variants.mjs +8 -19
  26. package/dist/es/render/utils/resolve-variants.mjs.map +1 -1
  27. package/dist/es/scroll/observe.mjs +23 -0
  28. package/dist/es/scroll/observe.mjs.map +1 -0
  29. package/dist/index.d.ts +36 -27
  30. package/dist/motion-dom.dev.js +284 -443
  31. package/dist/motion-dom.js +1 -1
  32. package/dist/size-rollup-style-effect.js +1 -1
  33. package/dist/size-rollup-style-effect.js.map +1 -1
  34. package/package.json +3 -3
package/dist/cjs/index.js CHANGED
@@ -2733,13 +2733,16 @@ class NativeAnimation extends WithPromise {
2733
2733
  /**
2734
2734
  * Attaches a timeline to the animation, for instance the `ScrollTimeline`.
2735
2735
  */
2736
- attachTimeline({ timeline, onAttach, observe, }) {
2736
+ attachTimeline({ timeline, onAttach, observe,
2737
+ // Deprecated rangeStart/rangeEnd from framer-motion 13.0–13.4. Removed in v14.
2738
+ ...range }) {
2737
2739
  if (this.allowFlatten) {
2738
2740
  this.animation.effect?.updateTiming({ easing: "linear" });
2739
2741
  }
2740
2742
  this.animation.onfinish = null;
2741
2743
  if (timeline && supportsScrollTimeline()) {
2742
2744
  this.animation.timeline = timeline;
2745
+ Object.assign(this.animation, range);
2743
2746
  onAttach?.(this.animation);
2744
2747
  return motionUtils.noop;
2745
2748
  }
@@ -4077,36 +4080,29 @@ function getValueState(visualElement) {
4077
4080
  return state;
4078
4081
  }
4079
4082
  function resolveVariantFromProps(props, definition, custom, visualElement) {
4083
+ const resolveFunction = (def) => typeof def === "function"
4084
+ ? def(custom !== undefined ? custom : props.custom, ...getValueState(visualElement))
4085
+ : def;
4080
4086
  /**
4081
- * If the variant definition is a function, resolve.
4082
- */
4083
- if (typeof definition === "function") {
4084
- const [current, velocity] = getValueState(visualElement);
4085
- definition = definition(custom !== undefined ? custom : props.custom, current, velocity);
4086
- }
4087
- /**
4088
- * If the variant definition is a variant label, or
4089
- * the function returned a variant label, resolve.
4087
+ * Resolve a function, then a variant label (which the function may have
4088
+ * returned), then a function again as the label may point to one. The
4089
+ * final function can only return a target object.
4090
4090
  */
4091
+ definition = resolveFunction(definition);
4091
4092
  if (typeof definition === "string") {
4092
4093
  definition = props.variants && props.variants[definition];
4093
4094
  }
4094
- /**
4095
- * At this point we've resolved both functions and variant labels,
4096
- * but the resolved variant label might itself have been a function.
4097
- * If so, resolve. This can only have returned a valid target object.
4098
- */
4099
- if (typeof definition === "function") {
4100
- const [current, velocity] = getValueState(visualElement);
4101
- definition = definition(custom !== undefined ? custom : props.custom, current, velocity);
4102
- }
4103
- return definition;
4095
+ return resolveFunction(definition);
4104
4096
  }
4105
4097
 
4106
4098
  function resolveVariant(visualElement, definition, custom) {
4107
- const props = visualElement.getProps();
4108
- return resolveVariantFromProps(props, definition, custom !== undefined ? custom : props.custom, visualElement);
4099
+ return resolveVariantFromProps(visualElement.getProps(), definition, custom, visualElement);
4109
4100
  }
4101
+ /**
4102
+ * Exit variants resolve with AnimatePresence's custom, every other type
4103
+ * with the element's own.
4104
+ */
4105
+ const getTypeCustom = (visualElement, type) => type === "exit" ? visualElement.presenceContext?.custom : undefined;
4110
4106
 
4111
4107
  const positionalKeys = new Set([
4112
4108
  "width",
@@ -4189,7 +4185,7 @@ const isBrowser$1 = typeof window !== "undefined";
4189
4185
  * had been set to true in the meantime.
4190
4186
  */
4191
4187
  function shouldBlockAnimation({ protectedKeys, needsAnimating }, key) {
4192
- const shouldBlock = protectedKeys.hasOwnProperty(key) && needsAnimating[key] !== true;
4188
+ const shouldBlock = key in protectedKeys && !needsAnimating[key];
4193
4189
  needsAnimating[key] = false;
4194
4190
  return shouldBlock;
4195
4191
  }
@@ -4204,9 +4200,7 @@ function animateTarget(visualElement, targetAndTransition, { delay = 0, transiti
4204
4200
  if (transitionOverride)
4205
4201
  transition = transitionOverride;
4206
4202
  const animations = [];
4207
- const animationTypeState = type &&
4208
- visualElement.animationState &&
4209
- visualElement.animationState.getState()[type];
4203
+ const animationTypeState = type && visualElement.animationState?.getState()[type];
4210
4204
  const path = transition?.path;
4211
4205
  if (path) {
4212
4206
  // path mutates `target` to claim x/y; loop below skips them.
@@ -4280,75 +4274,46 @@ function animateTarget(visualElement, targetAndTransition, { delay = 0, transiti
4280
4274
  }
4281
4275
 
4282
4276
  function animateVariant(visualElement, variant, options = {}) {
4283
- const resolved = resolveVariant(visualElement, variant, options.type === "exit"
4284
- ? visualElement.presenceContext?.custom
4285
- : undefined);
4286
- let { transition = visualElement.getDefaultTransition() || {} } = resolved || {};
4287
- if (options.transitionOverride) {
4288
- transition = options.transitionOverride;
4289
- }
4290
- /**
4291
- * If we have a variant, create a callback that runs it as an animation.
4292
- * Otherwise, we resolve a Promise immediately for a composable no-op.
4293
- */
4294
- const getAnimation = resolved
4295
- ? () => Promise.all(animateTarget(visualElement, resolved, options))
4296
- : () => Promise.resolve();
4297
- /**
4298
- * If we have children, create a callback that runs all their animations.
4299
- * Otherwise, we resolve a Promise immediately for a composable no-op.
4300
- */
4301
- const getChildAnimations = visualElement.variantChildren && visualElement.variantChildren.size
4302
- ? (forwardDelay = 0) => {
4303
- const { delayChildren = 0, staggerChildren, staggerDirection, } = transition;
4304
- return animateChildren(visualElement, variant, forwardDelay, delayChildren, staggerChildren, staggerDirection, options);
4305
- }
4306
- : () => Promise.resolve();
4277
+ const resolved = resolveVariant(visualElement, variant, getTypeCustom(visualElement, options.type));
4278
+ const transition = options.transitionOverride ||
4279
+ (resolved?.transition ?? (visualElement.getDefaultTransition() || {}));
4280
+ const getAnimation = () => Promise.all(resolved ? animateTarget(visualElement, resolved, options) : []);
4281
+ const getChildAnimations = (delay = 0) => {
4282
+ const { variantChildren } = visualElement;
4283
+ const { delayChildren = 0, staggerChildren, staggerDirection, } = transition;
4284
+ const animations = [];
4285
+ variantChildren?.forEach((child) => {
4286
+ child.notify("AnimationStart", variant);
4287
+ animations.push(animateVariant(child, variant, {
4288
+ ...options,
4289
+ delay: delay +
4290
+ (typeof delayChildren === "function"
4291
+ ? 0
4292
+ : delayChildren) +
4293
+ calcChildStagger(variantChildren, child, delayChildren, staggerChildren, staggerDirection),
4294
+ }).then(() => child.notify("AnimationComplete", variant)));
4295
+ });
4296
+ return Promise.all(animations);
4297
+ };
4307
4298
  /**
4308
- * If the transition explicitly defines a "when" option, we need to resolve either
4309
- * this animation or all children animations before playing the other.
4299
+ * when: "beforeChildren" | "afterChildren" runs this element's animation
4300
+ * and its children's in sequence.
4310
4301
  */
4311
4302
  const { when } = transition;
4312
- if (when) {
4313
- const [first, last] = when === "beforeChildren"
4314
- ? [getAnimation, getChildAnimations]
4315
- : [getChildAnimations, getAnimation];
4316
- return first().then(() => last());
4317
- }
4318
- else {
4319
- return Promise.all([getAnimation(), getChildAnimations(options.delay)]);
4320
- }
4321
- }
4322
- function animateChildren(visualElement, variant, delay = 0, delayChildren = 0, staggerChildren = 0, staggerDirection = 1, options) {
4323
- const animations = [];
4324
- for (const child of visualElement.variantChildren) {
4325
- child.notify("AnimationStart", variant);
4326
- animations.push(animateVariant(child, variant, {
4327
- ...options,
4328
- delay: delay +
4329
- (typeof delayChildren === "function" ? 0 : delayChildren) +
4330
- calcChildStagger(visualElement.variantChildren, child, delayChildren, staggerChildren, staggerDirection),
4331
- }).then(() => child.notify("AnimationComplete", variant)));
4332
- }
4333
- return Promise.all(animations);
4303
+ return when
4304
+ ? when === "beforeChildren"
4305
+ ? getAnimation().then(() => getChildAnimations())
4306
+ : getChildAnimations().then(getAnimation)
4307
+ : Promise.all([getAnimation(), getChildAnimations(options.delay)]);
4334
4308
  }
4335
4309
 
4336
4310
  function animateVisualElement(visualElement, definition, options = {}) {
4337
4311
  visualElement.notify("AnimationStart", definition);
4338
- let animation;
4339
- if (Array.isArray(definition)) {
4340
- const animations = definition.map((variant) => animateVariant(visualElement, variant, options));
4341
- animation = Promise.all(animations);
4342
- }
4343
- else if (typeof definition === "string") {
4344
- animation = animateVariant(visualElement, definition, options);
4345
- }
4346
- else {
4347
- const resolvedDefinition = typeof definition === "function"
4348
- ? resolveVariant(visualElement, definition, options.custom)
4349
- : definition;
4350
- animation = Promise.all(animateTarget(visualElement, resolvedDefinition, options));
4351
- }
4312
+ const animation = Array.isArray(definition)
4313
+ ? Promise.all(definition.map((variant) => animateVariant(visualElement, variant, options)))
4314
+ : typeof definition === "string"
4315
+ ? animateVariant(visualElement, definition, options)
4316
+ : Promise.all(animateTarget(visualElement, resolveVariant(visualElement, definition, options.custom), options));
4352
4317
  return animation.then(() => {
4353
4318
  visualElement.notify("AnimationComplete", definition);
4354
4319
  });
@@ -4874,7 +4839,12 @@ class MotionValueState {
4874
4839
  const onRemove = () => {
4875
4840
  cancelOnChange();
4876
4841
  render && !computed && this.cancel(render);
4877
- this.values.delete(name);
4842
+ /**
4843
+ * A later set() of the same name has already removed this
4844
+ * entry, so leave the one that replaced it.
4845
+ */
4846
+ this.values.get(name)?.onRemove === onRemove &&
4847
+ this.values.delete(name);
4878
4848
  };
4879
4849
  this.values.set(name, {
4880
4850
  value,
@@ -5353,24 +5323,35 @@ const addSVGValue = (element, state, key, value) => {
5353
5323
  else if (key.startsWith("attr")) {
5354
5324
  return addAttrValue(element, state, key, value, convertAttrKey(key));
5355
5325
  }
5356
- const handler = isCSSVar(key) || key in element.style ? addStyleValue : addAttrValue;
5326
+ /**
5327
+ * Transforms and origins aren't CSS properties (bar `x`/`y` in some
5328
+ * browsers), so check them by name rather than `key in element.style`.
5329
+ */
5330
+ const handler = transformProps.has(key) ||
5331
+ originProps.has(key) ||
5332
+ isCSSVar(key) ||
5333
+ key in element.style
5334
+ ? addStyleValue
5335
+ : addAttrValue;
5357
5336
  return handler(element, state, key, value);
5358
5337
  };
5359
5338
  /**
5360
5339
  * Reads the current value of `key` from an SVG element as the origin of
5361
5340
  * an animation, as the SVG VisualElement does: transforms start from
5362
5341
  * their defaults, CSS variables and the few CSS-only properties come from
5363
- * computed style
5364
- * and everything else is read from the attribute, dash-cased
5365
- * (`strokeWidth` -> `stroke-width`) or, failing that, as written
5366
- * (`baseFrequency`).
5342
+ * computed style, falling back to the attribute when that's empty (e.g.
5343
+ * the element isn't rendered), and everything else is read from the
5344
+ * attribute, dash-cased (`strokeWidth` -> `stroke-width`) or, failing
5345
+ * that, as written (`baseFrequency`).
5367
5346
  */
5368
5347
  const readSVGValue = (element, key) => {
5369
5348
  if (transformProps.has(key)) {
5370
5349
  return numberValueTypes[key]?.default || 0;
5371
5350
  }
5372
5351
  if (isCSSVar(key) || cssStyleProperties.includes(key)) {
5373
- return readStyleValue(element, key);
5352
+ return (readStyleValue(element, key) ||
5353
+ element.getAttribute(camelToDash(key)) ||
5354
+ 0);
5374
5355
  }
5375
5356
  key = convertAttrKey(key);
5376
5357
  return (element.getAttribute(camelToDash(key)) ??
@@ -6073,6 +6054,25 @@ function resize(a, b) {
6073
6054
  return typeof a === "function" ? resizeWindow(a) : resizeElement(a, b);
6074
6055
  }
6075
6056
 
6057
+ /**
6058
+ * @deprecated Kept because framer-motion 13.0–13.4 import it from
6059
+ * motion-dom with a ^ range. Removed in v14.
6060
+ */
6061
+ function observeTimeline(update, timeline) {
6062
+ let prevProgress;
6063
+ const onFrame = () => {
6064
+ const { currentTime } = timeline;
6065
+ const percentage = currentTime === null ? 0 : currentTime.value;
6066
+ const progress = percentage / 100;
6067
+ if (prevProgress !== progress) {
6068
+ update(progress);
6069
+ }
6070
+ prevProgress = progress;
6071
+ };
6072
+ frame.preUpdate(onFrame, true);
6073
+ return () => cancelFrame(onFrame);
6074
+ }
6075
+
6076
6076
  const statsBuffer = {
6077
6077
  value: null,
6078
6078
  addProjectionMetrics: null,
@@ -7955,8 +7955,20 @@ class VisualElement {
7955
7955
  if (this.projection && !this.projection.instance) {
7956
7956
  this.projection.mount(instance);
7957
7957
  }
7958
- if (this.parent && this.isVariantNode && !this.isControllingVariants) {
7959
- this.removeFromVariantTree = this.parent.addVariantChild(this);
7958
+ /**
7959
+ * Join the closest variant node above, so its variant changes
7960
+ * propagate here, unless inherit={false} cuts the chain.
7961
+ */
7962
+ if (this.isVariantNode && !this.isControllingVariants) {
7963
+ let node = this;
7964
+ do
7965
+ node = node.props.inherit !== false && node.parent;
7966
+ while (node && !node.isVariantNode);
7967
+ if (node) {
7968
+ const { variantChildren } = node;
7969
+ variantChildren.add(this);
7970
+ this.removeFromVariantTree = () => variantChildren.delete(this);
7971
+ }
7960
7972
  }
7961
7973
  this.values.forEach((value, key) => this.bindToMotionValue(key, value));
7962
7974
  /**
@@ -8066,9 +8078,11 @@ class VisualElement {
8066
8078
  }
8067
8079
  sortNodePosition(other) {
8068
8080
  /**
8069
- * If these nodes aren't even of the same type we can't compare their depth.
8081
+ * If either node isn't mounted, or they aren't even of the same type,
8082
+ * we can't compare their document order.
8070
8083
  */
8071
8084
  if (!this.current ||
8085
+ !other.current ||
8072
8086
  !this.sortInstanceNodePosition ||
8073
8087
  this.type !== other.type) {
8074
8088
  return 0;
@@ -8174,24 +8188,6 @@ class VisualElement {
8174
8188
  getTransformPagePoint() {
8175
8189
  return this.props.transformPagePoint;
8176
8190
  }
8177
- getClosestVariantNode() {
8178
- return this.isVariantNode
8179
- ? this
8180
- : this.parent
8181
- ? this.parent.getClosestVariantNode()
8182
- : undefined;
8183
- }
8184
- /**
8185
- * Add a child visual element to our set of children.
8186
- */
8187
- addVariantChild(child) {
8188
- const closestVariantNode = this.getClosestVariantNode();
8189
- if (closestVariantNode) {
8190
- closestVariantNode.variantChildren &&
8191
- closestVariantNode.variantChildren.add(child);
8192
- return () => closestVariantNode.variantChildren.delete(child);
8193
- }
8194
- }
8195
8191
  /**
8196
8192
  * Add a motion value and bind it to this visual element.
8197
8193
  */
@@ -8286,41 +8282,6 @@ class VisualElement {
8286
8282
  setBaseTarget(key, value) {
8287
8283
  this.baseTarget[key] = value;
8288
8284
  }
8289
- /**
8290
- * Find the base target for a value thats been removed from all animation
8291
- * props.
8292
- */
8293
- getBaseTarget(key) {
8294
- const { initial } = this.props;
8295
- let valueFromInitial;
8296
- if (typeof initial === "string" || typeof initial === "object") {
8297
- const variant = resolveVariantFromProps(this.props, initial, this.presenceContext?.custom);
8298
- if (variant) {
8299
- valueFromInitial = variant[key];
8300
- }
8301
- }
8302
- /**
8303
- * If this value still exists in the current initial variant, read that.
8304
- */
8305
- if (initial && valueFromInitial !== undefined) {
8306
- return valueFromInitial;
8307
- }
8308
- /**
8309
- * Alternatively, if this VisualElement config has defined a getBaseTarget
8310
- * so we can read the value from an alternative source, try that.
8311
- */
8312
- const target = this.getBaseTargetFromProps(this.props, key);
8313
- if (target !== undefined && !isMotionValue(target))
8314
- return target;
8315
- /**
8316
- * If the value was initially defined on initial, but it doesn't any more,
8317
- * return undefined. Otherwise return the value as initially read from the DOM.
8318
- */
8319
- return this.initialValues[key] !== undefined &&
8320
- valueFromInitial === undefined
8321
- ? undefined
8322
- : this.baseTarget[key];
8323
- }
8324
8285
  on(eventName, callback) {
8325
8286
  if (!this.events[eventName]) {
8326
8287
  this.events[eventName] = new motionUtils.SubscriptionManager();
@@ -8625,34 +8586,6 @@ class SVGVisualElement extends DOMVisualElement {
8625
8586
  }
8626
8587
  }
8627
8588
 
8628
- const numVariantProps = variantProps.length;
8629
- /**
8630
- * Get variant context from a visual element's parent chain.
8631
- * Uses `any` type for visualElement to avoid circular dependencies.
8632
- */
8633
- function getVariantContext(visualElement) {
8634
- if (!visualElement)
8635
- return undefined;
8636
- if (!visualElement.isControllingVariants) {
8637
- const context = visualElement.parent
8638
- ? getVariantContext(visualElement.parent) || {}
8639
- : {};
8640
- if (visualElement.props.initial !== undefined) {
8641
- context.initial = visualElement.props.initial;
8642
- }
8643
- return context;
8644
- }
8645
- const context = {};
8646
- for (let i = 0; i < numVariantProps; i++) {
8647
- const name = variantProps[i];
8648
- const prop = visualElement.props[name];
8649
- if (isVariantLabel(prop) || prop === false) {
8650
- context[name] = prop;
8651
- }
8652
- }
8653
- return context;
8654
- }
8655
-
8656
8589
  function shallowCompare(next, prev) {
8657
8590
  if (!Array.isArray(prev))
8658
8591
  return false;
@@ -8667,332 +8600,211 @@ function shallowCompare(next, prev) {
8667
8600
  }
8668
8601
 
8669
8602
  const reversePriorityOrder = [...variantPriorityOrder].reverse();
8670
- const numAnimationTypes = variantPriorityOrder.length;
8671
- function createAnimateFunction(visualElement) {
8672
- return (animations) => {
8673
- return Promise.all(animations.map(({ animation, options }) => animateVisualElement(visualElement, animation, options)));
8674
- };
8675
- }
8603
+ /**
8604
+ * Diffs the element's animation props (one layer per AnimationType, highest
8605
+ * priority wins each value) against the previous call and starts animations
8606
+ * for what changed.
8607
+ *
8608
+ * Uses `any` type for visualElement to avoid circular dependencies. It reads
8609
+ * props, parent, presenceContext, variantChildren, enteringChildren,
8610
+ * manuallyAnimateOnMount, blockInitialAnimation, getValue(),
8611
+ * getBaseTargetFromProps(), baseTarget and initialValues.
8612
+ */
8676
8613
  function createAnimationState(visualElement) {
8677
- let animate = createAnimateFunction(visualElement);
8614
+ let animate = (animations) => Promise.all(animations.map(({ animation, options }) => animateVisualElement(visualElement, animation, options)));
8678
8615
  let state = createState();
8679
8616
  let isInitialRender = true;
8680
8617
  /**
8681
- * Track whether the animation state has been reset (e.g. via StrictMode
8682
- * double-invocation or Suspense unmount/remount). On the first
8683
- * animateChanges() call after a reset we need to behave like the initial
8684
- * render for variant-inheritance checks, even though isInitialRender is
8685
- * already false.
8618
+ * After a reset (StrictMode, Suspense, AnimatePresence re-entry) the next
8619
+ * animateChanges() mounts again, but initial={false} no longer applies.
8686
8620
  */
8687
8621
  let wasReset = false;
8688
- /**
8689
- * This function will be used to reduce the animation definitions for
8690
- * each active animation type into an object of resolved values for it.
8691
- */
8692
- const buildResolvedTypeValues = (type) => (acc, definition) => {
8693
- const resolved = resolveVariant(visualElement, definition, type === "exit"
8694
- ? visualElement.presenceContext?.custom
8695
- : undefined);
8696
- if (resolved) {
8697
- const { transition, transitionEnd, ...target } = resolved;
8698
- acc = { ...acc, ...target, ...transitionEnd };
8699
- }
8700
- return acc;
8701
- };
8702
- /**
8703
- * This just allows us to inject mocked animation functions
8704
- * @internal
8705
- */
8706
- function setAnimateFunction(makeAnimator) {
8707
- animate = makeAnimator(visualElement);
8708
- }
8709
- /**
8710
- * When we receive new props, we need to:
8711
- * 1. Create a list of protected keys for each type. This is a directory of
8712
- * value keys that are currently being "handled" by types of a higher priority
8713
- * so that whenever an animation is played of a given type, these values are
8714
- * protected from being animated.
8715
- * 2. Determine if an animation type needs animating.
8716
- * 3. Determine if any values have been removed from a type and figure out
8717
- * what to animate those to.
8718
- */
8719
8622
  function animateChanges(changedActiveType) {
8720
- const { props } = visualElement;
8721
- const context = getVariantContext(visualElement.parent) || {};
8722
- /**
8723
- * A list of animations that we'll build into as we iterate through the animation
8724
- * types. This will get executed at the end of the function.
8725
- */
8623
+ const { props, parent, manuallyAnimateOnMount } = visualElement;
8624
+ const isMounting = isInitialRender || wasReset;
8726
8625
  const animations = [];
8727
8626
  /**
8728
- * Keep track of which values have been removed. Then, as we hit lower priority
8729
- * animation types, we can check if they contain removed values and animate to that.
8627
+ * Values removed from a type. A lower-priority type that defines
8628
+ * them animates them back, otherwise they fall back to a base target.
8730
8629
  */
8731
8630
  const removedKeys = new Set();
8732
8631
  /**
8733
- * A dictionary of all encountered keys. This is an object to let us build into and
8734
- * copy it without iteration. Each time we hit an animation type we set its protected
8735
- * keys - the keys its not allowed to animate - to the latest version of this object.
8632
+ * Values claimed by active, higher-priority types.
8736
8633
  */
8737
8634
  let encounteredKeys = {};
8738
8635
  /**
8739
- * If a variant has been removed at a given index, and this component is controlling
8740
- * variant animations, we want to ensure lower-priority variants are forced to animate.
8636
+ * Once a type has been deactivated, lower-priority variant labels
8637
+ * re-animate so variant children animate back too.
8741
8638
  */
8742
8639
  let removedVariantIndex = Infinity;
8743
8640
  /**
8744
- * Iterate through all animation types in reverse priority order. For each, we want to
8745
- * detect which values it's handling and whether or not they've changed (and therefore
8746
- * need to be animated). If any values have been removed, we want to detect those in
8747
- * lower priority props and flag for animation.
8641
+ * Variant labels are inherited from the closest variant-controlling
8642
+ * ancestor, unless an element on the way has inherit={false}.
8748
8643
  */
8749
- for (let i = 0; i < numAnimationTypes; i++) {
8750
- const type = reversePriorityOrder[i];
8644
+ let source = visualElement;
8645
+ do
8646
+ source = source.props.inherit !== false && source.parent;
8647
+ while (source && !source.isControllingVariants);
8648
+ reversePriorityOrder.forEach((type, i) => {
8751
8649
  const typeState = state[type];
8752
- const prop = props[type] !== undefined
8753
- ? props[type]
8754
- : context[type];
8650
+ const ownProp = props[type];
8651
+ const inheritedProp = source && source.props[type];
8652
+ const prop = ownProp !== undefined
8653
+ ? ownProp
8654
+ : isVariantLabel(inheritedProp) || inheritedProp === false
8655
+ ? inheritedProp
8656
+ : undefined;
8755
8657
  const propIsVariant = isVariantLabel(prop);
8756
- /**
8757
- * If this type has *just* changed isActive status, set activeDelta
8758
- * to that status. Otherwise set to null.
8759
- */
8760
8658
  const activeDelta = type === changedActiveType ? typeState.isActive : null;
8761
8659
  if (activeDelta === false)
8762
8660
  removedVariantIndex = i;
8763
8661
  /**
8764
- * If this prop is an inherited variant, rather than been set directly on the
8765
- * component itself, we want to make sure we allow the parent to trigger animations.
8766
- *
8767
- * TODO: Can probably change this to a !isControllingVariants check
8768
- */
8769
- let isInherited = prop === context[type] &&
8770
- prop !== props[type] &&
8771
- propIsVariant;
8772
- if (isInherited &&
8773
- (isInitialRender || wasReset) &&
8774
- visualElement.manuallyAnimateOnMount) {
8775
- isInherited = false;
8776
- }
8777
- /**
8778
- * Set all encountered keys so far as the protected keys for this type. This will
8779
- * be any key that has been animated or otherwise handled by active, higher-priority types.
8662
+ * Inherited labels are animated by the parent through
8663
+ * variantChildren, unless this element mounted after its parent.
8780
8664
  */
8665
+ const isInherited = propIsVariant &&
8666
+ ownProp === undefined &&
8667
+ !(isMounting && manuallyAnimateOnMount);
8781
8668
  typeState.protectedKeys = { ...encounteredKeys };
8782
- // Check if we can skip analysing this prop early
8783
- if (
8784
- // If it isn't active and hasn't *just* been set as inactive
8785
- (!typeState.isActive && activeDelta === null) ||
8786
- // If we didn't and don't have any defined prop for this animation type
8669
+ if ((!typeState.isActive && activeDelta === null) ||
8787
8670
  (!prop && !typeState.prevProp) ||
8788
- // Or if the prop doesn't define an animation
8789
8671
  isAnimationControls(prop) ||
8790
8672
  typeof prop === "boolean") {
8791
- continue;
8673
+ return;
8792
8674
  }
8793
8675
  /**
8794
- * If exit is already active and wasn't just activated, skip
8795
- * re-processing to prevent interrupting running exit animations.
8796
- * Re-resolving exit with a changed custom value can start new
8797
- * value animations that stop the originals, leaving the exit
8798
- * animation promise unresolved and the component stuck in the DOM.
8676
+ * Don't re-resolve a running exit: a changed custom would start
8677
+ * new value animations that stop the originals, leaving the exit
8678
+ * promise unresolved and the element stuck in the DOM.
8799
8679
  */
8800
8680
  if (type === "exit" && typeState.isActive && activeDelta !== true) {
8801
- if (typeState.prevResolvedValues) {
8802
- encounteredKeys = {
8803
- ...encounteredKeys,
8804
- ...typeState.prevResolvedValues,
8805
- };
8806
- }
8807
- continue;
8681
+ encounteredKeys = {
8682
+ ...encounteredKeys,
8683
+ ...typeState.prevResolvedValues,
8684
+ };
8685
+ return;
8808
8686
  }
8809
- /**
8810
- * As we go look through the values defined on this type, if we detect
8811
- * a changed value or a value that was removed in a higher priority, we set
8812
- * this to true and add this prop to the animation list.
8813
- */
8814
8687
  const variantDidChange = checkVariantsDidChange(typeState.prevProp, prop);
8815
8688
  let shouldAnimateType = variantDidChange ||
8816
- // If we're making this variant active, we want to always make it active
8817
- (type === changedActiveType &&
8818
- typeState.isActive &&
8819
- !isInherited &&
8820
- propIsVariant) ||
8821
- // If we removed a higher-priority variant (i is in reverse order)
8822
- (i > removedVariantIndex && propIsVariant);
8689
+ (propIsVariant &&
8690
+ ((activeDelta && !isInherited) || i > removedVariantIndex));
8823
8691
  let handledRemovedValues = false;
8824
- /**
8825
- * As animations can be set as variant lists, variants or target objects, we
8826
- * coerce everything to an array if it isn't one already
8827
- */
8828
8692
  const definitionList = Array.isArray(prop) ? prop : [prop];
8829
- /**
8830
- * Build an object of all the resolved values. We'll use this in the subsequent
8831
- * animateChanges calls to determine whether a value has changed.
8832
- */
8833
- let resolvedValues = definitionList.reduce(buildResolvedTypeValues(type), {});
8834
- if (activeDelta === false)
8835
- resolvedValues = {};
8836
- /**
8837
- * Now we need to loop through all the keys in the prev prop and this prop,
8838
- * and decide:
8839
- * 1. If the value has changed, and needs animating
8840
- * 2. If it has been removed, and needs adding to the removedKeys set
8841
- * 3. If it has been removed in a higher priority type and needs animating
8842
- * 4. If it hasn't been removed in a higher priority but hasn't changed, and
8843
- * needs adding to the type's protectedKeys list.
8844
- */
8845
- const { prevResolvedValues = {} } = typeState;
8846
- const allKeys = {
8847
- ...prevResolvedValues,
8848
- ...resolvedValues,
8849
- };
8850
- const markToAnimate = (key) => {
8851
- shouldAnimateType = true;
8852
- if (removedKeys.has(key)) {
8853
- handledRemovedValues = true;
8854
- removedKeys.delete(key);
8693
+ let resolvedValues = {};
8694
+ if (activeDelta !== false) {
8695
+ for (const definition of definitionList) {
8696
+ const { transition, transitionEnd, ...target } = resolveVariant(visualElement, definition, getTypeCustom(visualElement, type)) || {};
8697
+ resolvedValues = {
8698
+ ...resolvedValues,
8699
+ ...target,
8700
+ ...transitionEnd,
8701
+ };
8855
8702
  }
8856
- typeState.needsAnimating[key] = true;
8857
- const motionValue = visualElement.getValue(key);
8858
- if (motionValue)
8859
- motionValue.liveStyle = false;
8860
- };
8861
- for (const key in allKeys) {
8703
+ }
8704
+ const { prevResolvedValues } = typeState;
8705
+ for (const key in { ...prevResolvedValues, ...resolvedValues }) {
8706
+ if (key in encounteredKeys)
8707
+ continue;
8862
8708
  const next = resolvedValues[key];
8863
8709
  const prev = prevResolvedValues[key];
8864
- // If we've already handled this we can just skip ahead
8865
- if (encounteredKeys.hasOwnProperty(key))
8866
- continue;
8867
8710
  /**
8868
- * If the value has changed, we probably want to animate it.
8711
+ * Keyframes compare by value, but replay whenever the
8712
+ * variant label changed.
8869
8713
  */
8870
- let valueHasChanged = false;
8871
- if (isKeyframesTarget(next) && isKeyframesTarget(prev)) {
8872
- valueHasChanged =
8873
- !shallowCompare(next, prev) || variantDidChange;
8714
+ const valueHasChanged = Array.isArray(next) && Array.isArray(prev)
8715
+ ? !shallowCompare(next, prev) || variantDidChange
8716
+ : next !== prev;
8717
+ if (valueHasChanged &&
8718
+ (next === undefined || next === null)) {
8719
+ removedKeys.add(key);
8874
8720
  }
8875
- else {
8876
- valueHasChanged = next !== prev;
8877
- }
8878
- if (valueHasChanged) {
8879
- if (next !== undefined && next !== null) {
8880
- // If next is defined and doesn't equal prev, it needs animating
8881
- markToAnimate(key);
8882
- }
8883
- else {
8884
- // If it's undefined, it's been removed.
8885
- removedKeys.add(key);
8886
- }
8887
- }
8888
- else if (next !== undefined && removedKeys.has(key)) {
8889
- /**
8890
- * If next hasn't changed and it isn't undefined, we want to check if it's
8891
- * been removed by a higher priority
8892
- */
8893
- markToAnimate(key);
8721
+ else if (valueHasChanged ||
8722
+ (next !== undefined && removedKeys.has(key))) {
8723
+ shouldAnimateType = true;
8724
+ if (removedKeys.delete(key))
8725
+ handledRemovedValues = true;
8726
+ typeState.needsAnimating[key] = true;
8727
+ const motionValue = visualElement.getValue(key);
8728
+ if (motionValue)
8729
+ motionValue.liveStyle = false;
8894
8730
  }
8895
8731
  else {
8896
- /**
8897
- * If it hasn't changed, we add it to the list of protected values
8898
- * to ensure it doesn't get animated.
8899
- */
8900
8732
  typeState.protectedKeys[key] = true;
8901
8733
  }
8902
8734
  }
8903
- /**
8904
- * Update the typeState so next time animateChanges is called we can compare the
8905
- * latest prop and resolvedValues to these.
8906
- */
8907
8735
  typeState.prevProp = prop;
8908
8736
  typeState.prevResolvedValues = resolvedValues;
8909
8737
  if (typeState.isActive) {
8910
8738
  encounteredKeys = { ...encounteredKeys, ...resolvedValues };
8911
8739
  }
8912
- if ((isInitialRender || wasReset) &&
8913
- visualElement.blockInitialAnimation) {
8914
- shouldAnimateType = false;
8915
- }
8916
- /**
8917
- * If this is an inherited prop we want to skip this animation
8918
- * unless the inherited variants haven't changed on this render.
8919
- */
8920
- const willAnimateViaParent = isInherited && variantDidChange;
8921
- const needsAnimating = !willAnimateViaParent || handledRemovedValues;
8922
- if (shouldAnimateType && needsAnimating) {
8923
- animations.push(...definitionList.map((animation) => {
8740
+ if (shouldAnimateType &&
8741
+ (!(isInherited && variantDidChange) || handledRemovedValues)) {
8742
+ for (const animation of definitionList) {
8924
8743
  const options = { type };
8925
8744
  /**
8926
- * If we're performing the initial animation, but we're not
8927
- * rendering at the same time as the variant-controlling parent,
8928
- * we want to use the parent's transition to calculate the stagger.
8745
+ * Elements that mount into an already-mounted parent
8746
+ * animate themselves, staggered by the parent variant's
8747
+ * delayChildren.
8929
8748
  */
8930
8749
  if (typeof animation === "string" &&
8931
- (isInitialRender || wasReset) &&
8932
- !willAnimateViaParent &&
8933
- visualElement.manuallyAnimateOnMount &&
8934
- visualElement.parent) {
8935
- const { parent } = visualElement;
8936
- const parentVariant = resolveVariant(parent, animation);
8937
- if (parent.enteringChildren && parentVariant) {
8938
- const { delayChildren } = parentVariant.transition || {};
8939
- options.delay = calcChildStagger(parent.enteringChildren, visualElement, delayChildren);
8940
- }
8750
+ isMounting &&
8751
+ manuallyAnimateOnMount &&
8752
+ parent?.enteringChildren) {
8753
+ const delayChildren = resolveVariant(parent, animation)
8754
+ ?.transition?.delayChildren;
8755
+ options.delay = calcChildStagger(parent.enteringChildren, visualElement, delayChildren);
8941
8756
  }
8942
- return {
8943
- animation: animation,
8944
- options,
8945
- };
8946
- }));
8757
+ animations.push({ animation, options });
8758
+ }
8947
8759
  }
8948
- }
8760
+ });
8949
8761
  /**
8950
- * If there are some removed value that haven't been dealt with,
8951
- * we need to create a new animation that falls back either to the value
8952
- * defined in the style prop, or the last read value.
8762
+ * Removed values that no lower-priority type defines animate back to
8763
+ * initial, then style, then the value as first read. If initial no
8764
+ * longer defines a value it once did, it stays where it is.
8953
8765
  */
8954
8766
  if (removedKeys.size) {
8767
+ const { initial } = props;
8955
8768
  const fallbackAnimation = {};
8956
- /**
8957
- * If the initial prop contains a transition we can use that, otherwise
8958
- * allow the animation function to use the visual element's default.
8959
- */
8960
- if (typeof props.initial !== "boolean") {
8961
- const initialTransition = resolveVariant(visualElement, Array.isArray(props.initial)
8962
- ? props.initial[0]
8963
- : props.initial);
8964
- if (initialTransition && initialTransition.transition) {
8965
- fallbackAnimation.transition = initialTransition.transition;
8966
- }
8769
+ const resolvedInitial = typeof initial !== "boolean" &&
8770
+ resolveVariant(visualElement, Array.isArray(initial) ? initial[0] : initial, visualElement.presenceContext?.custom);
8771
+ if (resolvedInitial && resolvedInitial.transition) {
8772
+ fallbackAnimation.transition = resolvedInitial.transition;
8967
8773
  }
8968
8774
  removedKeys.forEach((key) => {
8969
- const fallbackTarget = visualElement.getBaseTarget(key);
8970
8775
  const motionValue = visualElement.getValue(key);
8971
8776
  if (motionValue)
8972
8777
  motionValue.liveStyle = true;
8973
- // @ts-expect-error - @mattgperry to figure if we should do something here
8974
- fallbackAnimation[key] = fallbackTarget ?? null;
8778
+ const fromInitial = resolvedInitial && !Array.isArray(initial)
8779
+ ? resolvedInitial[key]
8780
+ : undefined;
8781
+ const fromProps = visualElement.getBaseTargetFromProps(props, key);
8782
+ fallbackAnimation[key] =
8783
+ (fromInitial !== undefined
8784
+ ? fromInitial
8785
+ : fromProps !== undefined && !isMotionValue(fromProps)
8786
+ ? fromProps
8787
+ : visualElement.initialValues[key] === undefined
8788
+ ? visualElement.baseTarget[key]
8789
+ : undefined) ?? null;
8975
8790
  });
8976
8791
  animations.push({ animation: fallbackAnimation });
8977
8792
  }
8978
- let shouldAnimate = Boolean(animations.length);
8979
- if (isInitialRender &&
8980
- (props.initial === false || props.initial === props.animate) &&
8981
- !visualElement.manuallyAnimateOnMount) {
8982
- shouldAnimate = false;
8983
- }
8984
- isInitialRender = false;
8985
- wasReset = false;
8986
- return shouldAnimate ? animate(animations) : Promise.resolve();
8793
+ const blockAnimation = (isMounting && visualElement.blockInitialAnimation) ||
8794
+ (isInitialRender &&
8795
+ !manuallyAnimateOnMount &&
8796
+ (props.initial === false || props.initial === props.animate));
8797
+ isInitialRender = wasReset = false;
8798
+ return !blockAnimation && animations.length
8799
+ ? animate(animations)
8800
+ : Promise.resolve();
8987
8801
  }
8988
8802
  /**
8989
8803
  * Change whether a certain animation type is active.
8990
8804
  */
8991
8805
  function setActive(type, isActive) {
8992
- // If the active state hasn't changed, we can safely do nothing here
8993
8806
  if (state[type].isActive === isActive)
8994
8807
  return Promise.resolve();
8995
- // Propagate active change to children
8996
8808
  visualElement.variantChildren?.forEach((child) => child.animationState?.setActive(type, isActive));
8997
8809
  state[type].isActive = isActive;
8998
8810
  const animations = animateChanges(type);
@@ -9004,7 +8816,13 @@ function createAnimationState(visualElement) {
9004
8816
  return {
9005
8817
  animateChanges,
9006
8818
  setActive,
9007
- setAnimateFunction,
8819
+ /**
8820
+ * Allows tests to inject a mocked animate function.
8821
+ * @internal
8822
+ */
8823
+ setAnimateFunction: (makeAnimator) => {
8824
+ animate = makeAnimator(visualElement);
8825
+ },
9008
8826
  getState: () => state,
9009
8827
  reset: () => {
9010
8828
  state = createState();
@@ -9021,24 +8839,45 @@ function checkVariantsDidChange(prev, next) {
9021
8839
  }
9022
8840
  return false;
9023
8841
  }
9024
- function createTypeState(isActive = false) {
9025
- return {
9026
- isActive,
9027
- protectedKeys: {},
9028
- needsAnimating: {},
9029
- prevResolvedValues: {},
9030
- };
9031
- }
9032
8842
  function createState() {
9033
- return {
9034
- animate: createTypeState(true),
9035
- whileInView: createTypeState(),
9036
- whileHover: createTypeState(),
9037
- whileTap: createTypeState(),
9038
- whileDrag: createTypeState(),
9039
- whileFocus: createTypeState(),
9040
- exit: createTypeState(),
9041
- };
8843
+ const state = {};
8844
+ for (const type of variantPriorityOrder) {
8845
+ state[type] = {
8846
+ isActive: type === "animate",
8847
+ protectedKeys: {},
8848
+ needsAnimating: {},
8849
+ prevResolvedValues: {},
8850
+ };
8851
+ }
8852
+ return state;
8853
+ }
8854
+
8855
+ const numVariantProps = variantProps.length;
8856
+ /**
8857
+ * Get variant context from a visual element's parent chain.
8858
+ * Uses `any` type for visualElement to avoid circular dependencies.
8859
+ */
8860
+ function getVariantContext(visualElement) {
8861
+ if (!visualElement)
8862
+ return undefined;
8863
+ if (!visualElement.isControllingVariants) {
8864
+ const context = visualElement.parent
8865
+ ? getVariantContext(visualElement.parent) || {}
8866
+ : {};
8867
+ if (visualElement.props.initial !== undefined) {
8868
+ context.initial = visualElement.props.initial;
8869
+ }
8870
+ return context;
8871
+ }
8872
+ const context = {};
8873
+ for (let i = 0; i < numVariantProps; i++) {
8874
+ const name = variantProps[i];
8875
+ const prop = visualElement.props[name];
8876
+ if (isVariantLabel(prop) || prop === false) {
8877
+ context[name] = prop;
8878
+ }
8879
+ }
8880
+ return context;
9042
8881
  }
9043
8882
 
9044
8883
  /**
@@ -11863,8 +11702,10 @@ exports.motionValue = motionValue;
11863
11702
  exports.nodeGroup = nodeGroup;
11864
11703
  exports.number = number;
11865
11704
  exports.numberValueTypes = numberValueTypes;
11705
+ exports.observeTimeline = observeTimeline;
11866
11706
  exports.optimizedAppearDataAttribute = optimizedAppearDataAttribute;
11867
11707
  exports.optimizedAppearDataId = optimizedAppearDataId;
11708
+ exports.originProps = originProps;
11868
11709
  exports.parseAnimateLayoutArgs = parseAnimateLayoutArgs;
11869
11710
  exports.parseCSSVariable = parseCSSVariable;
11870
11711
  exports.parseValueFromTransform = parseValueFromTransform;