motion-dom 13.5.0 → 14.0.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.
Files changed (29) hide show
  1. package/dist/cjs/index.js +260 -442
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/es/animation/interfaces/visual-element-target.mjs +2 -4
  4. package/dist/es/animation/interfaces/visual-element-target.mjs.map +1 -1
  5. package/dist/es/animation/interfaces/visual-element-variant.mjs +29 -49
  6. package/dist/es/animation/interfaces/visual-element-variant.mjs.map +1 -1
  7. package/dist/es/animation/interfaces/visual-element.mjs +5 -14
  8. package/dist/es/animation/interfaces/visual-element.mjs.map +1 -1
  9. package/dist/es/effects/MotionValueState.mjs +6 -1
  10. package/dist/es/effects/MotionValueState.mjs.map +1 -1
  11. package/dist/es/effects/style/index.mjs +1 -1
  12. package/dist/es/effects/style/index.mjs.map +1 -1
  13. package/dist/es/effects/svg/index.mjs +18 -7
  14. package/dist/es/effects/svg/index.mjs.map +1 -1
  15. package/dist/es/index.mjs +1 -1
  16. package/dist/es/render/VisualElement.mjs +17 -57
  17. package/dist/es/render/VisualElement.mjs.map +1 -1
  18. package/dist/es/render/utils/animation-state.mjs +144 -267
  19. package/dist/es/render/utils/animation-state.mjs.map +1 -1
  20. package/dist/es/render/utils/resolve-dynamic-variants.mjs +7 -3
  21. package/dist/es/render/utils/resolve-dynamic-variants.mjs.map +1 -1
  22. package/dist/es/render/utils/resolve-variants.mjs +8 -19
  23. package/dist/es/render/utils/resolve-variants.mjs.map +1 -1
  24. package/dist/index.d.ts +16 -24
  25. package/dist/motion-dom.dev.js +260 -442
  26. package/dist/motion-dom.js +1 -1
  27. package/dist/size-rollup-style-effect.js +1 -1
  28. package/dist/size-rollup-style-effect.js.map +1 -1
  29. package/package.json +3 -3
package/dist/cjs/index.js CHANGED
@@ -4077,36 +4077,29 @@ function getValueState(visualElement) {
4077
4077
  return state;
4078
4078
  }
4079
4079
  function resolveVariantFromProps(props, definition, custom, visualElement) {
4080
+ const resolveFunction = (def) => typeof def === "function"
4081
+ ? def(custom !== undefined ? custom : props.custom, ...getValueState(visualElement))
4082
+ : def;
4080
4083
  /**
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.
4084
+ * Resolve a function, then a variant label (which the function may have
4085
+ * returned), then a function again as the label may point to one. The
4086
+ * final function can only return a target object.
4090
4087
  */
4088
+ definition = resolveFunction(definition);
4091
4089
  if (typeof definition === "string") {
4092
4090
  definition = props.variants && props.variants[definition];
4093
4091
  }
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;
4092
+ return resolveFunction(definition);
4104
4093
  }
4105
4094
 
4106
4095
  function resolveVariant(visualElement, definition, custom) {
4107
- const props = visualElement.getProps();
4108
- return resolveVariantFromProps(props, definition, custom !== undefined ? custom : props.custom, visualElement);
4096
+ return resolveVariantFromProps(visualElement.getProps(), definition, custom, visualElement);
4109
4097
  }
4098
+ /**
4099
+ * Exit variants resolve with AnimatePresence's custom, every other type
4100
+ * with the element's own.
4101
+ */
4102
+ const getTypeCustom = (visualElement, type) => type === "exit" ? visualElement.presenceContext?.custom : undefined;
4110
4103
 
4111
4104
  const positionalKeys = new Set([
4112
4105
  "width",
@@ -4189,7 +4182,7 @@ const isBrowser$1 = typeof window !== "undefined";
4189
4182
  * had been set to true in the meantime.
4190
4183
  */
4191
4184
  function shouldBlockAnimation({ protectedKeys, needsAnimating }, key) {
4192
- const shouldBlock = protectedKeys.hasOwnProperty(key) && needsAnimating[key] !== true;
4185
+ const shouldBlock = key in protectedKeys && !needsAnimating[key];
4193
4186
  needsAnimating[key] = false;
4194
4187
  return shouldBlock;
4195
4188
  }
@@ -4204,9 +4197,7 @@ function animateTarget(visualElement, targetAndTransition, { delay = 0, transiti
4204
4197
  if (transitionOverride)
4205
4198
  transition = transitionOverride;
4206
4199
  const animations = [];
4207
- const animationTypeState = type &&
4208
- visualElement.animationState &&
4209
- visualElement.animationState.getState()[type];
4200
+ const animationTypeState = type && visualElement.animationState?.getState()[type];
4210
4201
  const path = transition?.path;
4211
4202
  if (path) {
4212
4203
  // path mutates `target` to claim x/y; loop below skips them.
@@ -4280,75 +4271,46 @@ function animateTarget(visualElement, targetAndTransition, { delay = 0, transiti
4280
4271
  }
4281
4272
 
4282
4273
  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();
4274
+ const resolved = resolveVariant(visualElement, variant, getTypeCustom(visualElement, options.type));
4275
+ const transition = options.transitionOverride ||
4276
+ (resolved?.transition ?? (visualElement.getDefaultTransition() || {}));
4277
+ const getAnimation = () => Promise.all(resolved ? animateTarget(visualElement, resolved, options) : []);
4278
+ const getChildAnimations = (delay = 0) => {
4279
+ const { variantChildren } = visualElement;
4280
+ const { delayChildren = 0, staggerChildren, staggerDirection, } = transition;
4281
+ const animations = [];
4282
+ variantChildren?.forEach((child) => {
4283
+ child.notify("AnimationStart", variant);
4284
+ animations.push(animateVariant(child, variant, {
4285
+ ...options,
4286
+ delay: delay +
4287
+ (typeof delayChildren === "function"
4288
+ ? 0
4289
+ : delayChildren) +
4290
+ calcChildStagger(variantChildren, child, delayChildren, staggerChildren, staggerDirection),
4291
+ }).then(() => child.notify("AnimationComplete", variant)));
4292
+ });
4293
+ return Promise.all(animations);
4294
+ };
4307
4295
  /**
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.
4296
+ * when: "beforeChildren" | "afterChildren" runs this element's animation
4297
+ * and its children's in sequence.
4310
4298
  */
4311
4299
  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);
4300
+ return when
4301
+ ? when === "beforeChildren"
4302
+ ? getAnimation().then(() => getChildAnimations())
4303
+ : getChildAnimations().then(getAnimation)
4304
+ : Promise.all([getAnimation(), getChildAnimations(options.delay)]);
4334
4305
  }
4335
4306
 
4336
4307
  function animateVisualElement(visualElement, definition, options = {}) {
4337
4308
  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
- }
4309
+ const animation = Array.isArray(definition)
4310
+ ? Promise.all(definition.map((variant) => animateVariant(visualElement, variant, options)))
4311
+ : typeof definition === "string"
4312
+ ? animateVariant(visualElement, definition, options)
4313
+ : Promise.all(animateTarget(visualElement, resolveVariant(visualElement, definition, options.custom), options));
4352
4314
  return animation.then(() => {
4353
4315
  visualElement.notify("AnimationComplete", definition);
4354
4316
  });
@@ -4874,7 +4836,12 @@ class MotionValueState {
4874
4836
  const onRemove = () => {
4875
4837
  cancelOnChange();
4876
4838
  render && !computed && this.cancel(render);
4877
- this.values.delete(name);
4839
+ /**
4840
+ * A later set() of the same name has already removed this
4841
+ * entry, so leave the one that replaced it.
4842
+ */
4843
+ this.values.get(name)?.onRemove === onRemove &&
4844
+ this.values.delete(name);
4878
4845
  };
4879
4846
  this.values.set(name, {
4880
4847
  value,
@@ -5353,24 +5320,35 @@ const addSVGValue = (element, state, key, value) => {
5353
5320
  else if (key.startsWith("attr")) {
5354
5321
  return addAttrValue(element, state, key, value, convertAttrKey(key));
5355
5322
  }
5356
- const handler = isCSSVar(key) || key in element.style ? addStyleValue : addAttrValue;
5323
+ /**
5324
+ * Transforms and origins aren't CSS properties (bar `x`/`y` in some
5325
+ * browsers), so check them by name rather than `key in element.style`.
5326
+ */
5327
+ const handler = transformProps.has(key) ||
5328
+ originProps.has(key) ||
5329
+ isCSSVar(key) ||
5330
+ key in element.style
5331
+ ? addStyleValue
5332
+ : addAttrValue;
5357
5333
  return handler(element, state, key, value);
5358
5334
  };
5359
5335
  /**
5360
5336
  * Reads the current value of `key` from an SVG element as the origin of
5361
5337
  * an animation, as the SVG VisualElement does: transforms start from
5362
5338
  * 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`).
5339
+ * computed style, falling back to the attribute when that's empty (e.g.
5340
+ * the element isn't rendered), and everything else is read from the
5341
+ * attribute, dash-cased (`strokeWidth` -> `stroke-width`) or, failing
5342
+ * that, as written (`baseFrequency`).
5367
5343
  */
5368
5344
  const readSVGValue = (element, key) => {
5369
5345
  if (transformProps.has(key)) {
5370
5346
  return numberValueTypes[key]?.default || 0;
5371
5347
  }
5372
5348
  if (isCSSVar(key) || cssStyleProperties.includes(key)) {
5373
- return readStyleValue(element, key);
5349
+ return (readStyleValue(element, key) ||
5350
+ element.getAttribute(camelToDash(key)) ||
5351
+ 0);
5374
5352
  }
5375
5353
  key = convertAttrKey(key);
5376
5354
  return (element.getAttribute(camelToDash(key)) ??
@@ -7955,8 +7933,20 @@ class VisualElement {
7955
7933
  if (this.projection && !this.projection.instance) {
7956
7934
  this.projection.mount(instance);
7957
7935
  }
7958
- if (this.parent && this.isVariantNode && !this.isControllingVariants) {
7959
- this.removeFromVariantTree = this.parent.addVariantChild(this);
7936
+ /**
7937
+ * Join the closest variant node above, so its variant changes
7938
+ * propagate here, unless inherit={false} cuts the chain.
7939
+ */
7940
+ if (this.isVariantNode && !this.isControllingVariants) {
7941
+ let node = this;
7942
+ do
7943
+ node = node.props.inherit !== false && node.parent;
7944
+ while (node && !node.isVariantNode);
7945
+ if (node) {
7946
+ const { variantChildren } = node;
7947
+ variantChildren.add(this);
7948
+ this.removeFromVariantTree = () => variantChildren.delete(this);
7949
+ }
7960
7950
  }
7961
7951
  this.values.forEach((value, key) => this.bindToMotionValue(key, value));
7962
7952
  /**
@@ -8066,9 +8056,11 @@ class VisualElement {
8066
8056
  }
8067
8057
  sortNodePosition(other) {
8068
8058
  /**
8069
- * If these nodes aren't even of the same type we can't compare their depth.
8059
+ * If either node isn't mounted, or they aren't even of the same type,
8060
+ * we can't compare their document order.
8070
8061
  */
8071
8062
  if (!this.current ||
8063
+ !other.current ||
8072
8064
  !this.sortInstanceNodePosition ||
8073
8065
  this.type !== other.type) {
8074
8066
  return 0;
@@ -8174,24 +8166,6 @@ class VisualElement {
8174
8166
  getTransformPagePoint() {
8175
8167
  return this.props.transformPagePoint;
8176
8168
  }
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
8169
  /**
8196
8170
  * Add a motion value and bind it to this visual element.
8197
8171
  */
@@ -8286,41 +8260,6 @@ class VisualElement {
8286
8260
  setBaseTarget(key, value) {
8287
8261
  this.baseTarget[key] = value;
8288
8262
  }
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
8263
  on(eventName, callback) {
8325
8264
  if (!this.events[eventName]) {
8326
8265
  this.events[eventName] = new motionUtils.SubscriptionManager();
@@ -8625,34 +8564,6 @@ class SVGVisualElement extends DOMVisualElement {
8625
8564
  }
8626
8565
  }
8627
8566
 
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
8567
  function shallowCompare(next, prev) {
8657
8568
  if (!Array.isArray(prev))
8658
8569
  return false;
@@ -8667,332 +8578,211 @@ function shallowCompare(next, prev) {
8667
8578
  }
8668
8579
 
8669
8580
  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
- }
8581
+ /**
8582
+ * Diffs the element's animation props (one layer per AnimationType, highest
8583
+ * priority wins each value) against the previous call and starts animations
8584
+ * for what changed.
8585
+ *
8586
+ * Uses `any` type for visualElement to avoid circular dependencies. It reads
8587
+ * props, parent, presenceContext, variantChildren, enteringChildren,
8588
+ * manuallyAnimateOnMount, blockInitialAnimation, getValue(),
8589
+ * getBaseTargetFromProps(), baseTarget and initialValues.
8590
+ */
8676
8591
  function createAnimationState(visualElement) {
8677
- let animate = createAnimateFunction(visualElement);
8592
+ let animate = (animations) => Promise.all(animations.map(({ animation, options }) => animateVisualElement(visualElement, animation, options)));
8678
8593
  let state = createState();
8679
8594
  let isInitialRender = true;
8680
8595
  /**
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.
8596
+ * After a reset (StrictMode, Suspense, AnimatePresence re-entry) the next
8597
+ * animateChanges() mounts again, but initial={false} no longer applies.
8686
8598
  */
8687
8599
  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
8600
  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
- */
8601
+ const { props, parent, manuallyAnimateOnMount } = visualElement;
8602
+ const isMounting = isInitialRender || wasReset;
8726
8603
  const animations = [];
8727
8604
  /**
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.
8605
+ * Values removed from a type. A lower-priority type that defines
8606
+ * them animates them back, otherwise they fall back to a base target.
8730
8607
  */
8731
8608
  const removedKeys = new Set();
8732
8609
  /**
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.
8610
+ * Values claimed by active, higher-priority types.
8736
8611
  */
8737
8612
  let encounteredKeys = {};
8738
8613
  /**
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.
8614
+ * Once a type has been deactivated, lower-priority variant labels
8615
+ * re-animate so variant children animate back too.
8741
8616
  */
8742
8617
  let removedVariantIndex = Infinity;
8743
8618
  /**
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.
8619
+ * Variant labels are inherited from the closest variant-controlling
8620
+ * ancestor, unless an element on the way has inherit={false}.
8748
8621
  */
8749
- for (let i = 0; i < numAnimationTypes; i++) {
8750
- const type = reversePriorityOrder[i];
8622
+ let source = visualElement;
8623
+ do
8624
+ source = source.props.inherit !== false && source.parent;
8625
+ while (source && !source.isControllingVariants);
8626
+ reversePriorityOrder.forEach((type, i) => {
8751
8627
  const typeState = state[type];
8752
- const prop = props[type] !== undefined
8753
- ? props[type]
8754
- : context[type];
8628
+ const ownProp = props[type];
8629
+ const inheritedProp = source && source.props[type];
8630
+ const prop = ownProp !== undefined
8631
+ ? ownProp
8632
+ : isVariantLabel(inheritedProp) || inheritedProp === false
8633
+ ? inheritedProp
8634
+ : undefined;
8755
8635
  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
8636
  const activeDelta = type === changedActiveType ? typeState.isActive : null;
8761
8637
  if (activeDelta === false)
8762
8638
  removedVariantIndex = i;
8763
8639
  /**
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.
8640
+ * Inherited labels are animated by the parent through
8641
+ * variantChildren, unless this element mounted after its parent.
8780
8642
  */
8643
+ const isInherited = propIsVariant &&
8644
+ ownProp === undefined &&
8645
+ !(isMounting && manuallyAnimateOnMount);
8781
8646
  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
8647
+ if ((!typeState.isActive && activeDelta === null) ||
8787
8648
  (!prop && !typeState.prevProp) ||
8788
- // Or if the prop doesn't define an animation
8789
8649
  isAnimationControls(prop) ||
8790
8650
  typeof prop === "boolean") {
8791
- continue;
8651
+ return;
8792
8652
  }
8793
8653
  /**
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.
8654
+ * Don't re-resolve a running exit: a changed custom would start
8655
+ * new value animations that stop the originals, leaving the exit
8656
+ * promise unresolved and the element stuck in the DOM.
8799
8657
  */
8800
8658
  if (type === "exit" && typeState.isActive && activeDelta !== true) {
8801
- if (typeState.prevResolvedValues) {
8802
- encounteredKeys = {
8803
- ...encounteredKeys,
8804
- ...typeState.prevResolvedValues,
8805
- };
8806
- }
8807
- continue;
8659
+ encounteredKeys = {
8660
+ ...encounteredKeys,
8661
+ ...typeState.prevResolvedValues,
8662
+ };
8663
+ return;
8808
8664
  }
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
8665
  const variantDidChange = checkVariantsDidChange(typeState.prevProp, prop);
8815
8666
  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);
8667
+ (propIsVariant &&
8668
+ ((activeDelta && !isInherited) || i > removedVariantIndex));
8823
8669
  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
8670
  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);
8671
+ let resolvedValues = {};
8672
+ if (activeDelta !== false) {
8673
+ for (const definition of definitionList) {
8674
+ const { transition, transitionEnd, ...target } = resolveVariant(visualElement, definition, getTypeCustom(visualElement, type)) || {};
8675
+ resolvedValues = {
8676
+ ...resolvedValues,
8677
+ ...target,
8678
+ ...transitionEnd,
8679
+ };
8855
8680
  }
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) {
8681
+ }
8682
+ const { prevResolvedValues } = typeState;
8683
+ for (const key in { ...prevResolvedValues, ...resolvedValues }) {
8684
+ if (key in encounteredKeys)
8685
+ continue;
8862
8686
  const next = resolvedValues[key];
8863
8687
  const prev = prevResolvedValues[key];
8864
- // If we've already handled this we can just skip ahead
8865
- if (encounteredKeys.hasOwnProperty(key))
8866
- continue;
8867
8688
  /**
8868
- * If the value has changed, we probably want to animate it.
8689
+ * Keyframes compare by value, but replay whenever the
8690
+ * variant label changed.
8869
8691
  */
8870
- let valueHasChanged = false;
8871
- if (isKeyframesTarget(next) && isKeyframesTarget(prev)) {
8872
- valueHasChanged =
8873
- !shallowCompare(next, prev) || variantDidChange;
8874
- }
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
- }
8692
+ const valueHasChanged = Array.isArray(next) && Array.isArray(prev)
8693
+ ? !shallowCompare(next, prev) || variantDidChange
8694
+ : next !== prev;
8695
+ if (valueHasChanged &&
8696
+ (next === undefined || next === null)) {
8697
+ removedKeys.add(key);
8887
8698
  }
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);
8699
+ else if (valueHasChanged ||
8700
+ (next !== undefined && removedKeys.has(key))) {
8701
+ shouldAnimateType = true;
8702
+ if (removedKeys.delete(key))
8703
+ handledRemovedValues = true;
8704
+ typeState.needsAnimating[key] = true;
8705
+ const motionValue = visualElement.getValue(key);
8706
+ if (motionValue)
8707
+ motionValue.liveStyle = false;
8894
8708
  }
8895
8709
  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
8710
  typeState.protectedKeys[key] = true;
8901
8711
  }
8902
8712
  }
8903
- /**
8904
- * Update the typeState so next time animateChanges is called we can compare the
8905
- * latest prop and resolvedValues to these.
8906
- */
8907
8713
  typeState.prevProp = prop;
8908
8714
  typeState.prevResolvedValues = resolvedValues;
8909
8715
  if (typeState.isActive) {
8910
8716
  encounteredKeys = { ...encounteredKeys, ...resolvedValues };
8911
8717
  }
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) => {
8718
+ if (shouldAnimateType &&
8719
+ (!(isInherited && variantDidChange) || handledRemovedValues)) {
8720
+ for (const animation of definitionList) {
8924
8721
  const options = { type };
8925
8722
  /**
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.
8723
+ * Elements that mount into an already-mounted parent
8724
+ * animate themselves, staggered by the parent variant's
8725
+ * delayChildren.
8929
8726
  */
8930
8727
  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
- }
8728
+ isMounting &&
8729
+ manuallyAnimateOnMount &&
8730
+ parent?.enteringChildren) {
8731
+ const delayChildren = resolveVariant(parent, animation)
8732
+ ?.transition?.delayChildren;
8733
+ options.delay = calcChildStagger(parent.enteringChildren, visualElement, delayChildren);
8941
8734
  }
8942
- return {
8943
- animation: animation,
8944
- options,
8945
- };
8946
- }));
8735
+ animations.push({ animation, options });
8736
+ }
8947
8737
  }
8948
- }
8738
+ });
8949
8739
  /**
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.
8740
+ * Removed values that no lower-priority type defines animate back to
8741
+ * initial, then style, then the value as first read. If initial no
8742
+ * longer defines a value it once did, it stays where it is.
8953
8743
  */
8954
8744
  if (removedKeys.size) {
8745
+ const { initial } = props;
8955
8746
  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
- }
8747
+ const resolvedInitial = typeof initial !== "boolean" &&
8748
+ resolveVariant(visualElement, Array.isArray(initial) ? initial[0] : initial, visualElement.presenceContext?.custom);
8749
+ if (resolvedInitial && resolvedInitial.transition) {
8750
+ fallbackAnimation.transition = resolvedInitial.transition;
8967
8751
  }
8968
8752
  removedKeys.forEach((key) => {
8969
- const fallbackTarget = visualElement.getBaseTarget(key);
8970
8753
  const motionValue = visualElement.getValue(key);
8971
8754
  if (motionValue)
8972
8755
  motionValue.liveStyle = true;
8973
- // @ts-expect-error - @mattgperry to figure if we should do something here
8974
- fallbackAnimation[key] = fallbackTarget ?? null;
8756
+ const fromInitial = resolvedInitial && !Array.isArray(initial)
8757
+ ? resolvedInitial[key]
8758
+ : undefined;
8759
+ const fromProps = visualElement.getBaseTargetFromProps(props, key);
8760
+ fallbackAnimation[key] =
8761
+ (fromInitial !== undefined
8762
+ ? fromInitial
8763
+ : fromProps !== undefined && !isMotionValue(fromProps)
8764
+ ? fromProps
8765
+ : visualElement.initialValues[key] === undefined
8766
+ ? visualElement.baseTarget[key]
8767
+ : undefined) ?? null;
8975
8768
  });
8976
8769
  animations.push({ animation: fallbackAnimation });
8977
8770
  }
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();
8771
+ const blockAnimation = (isMounting && visualElement.blockInitialAnimation) ||
8772
+ (isInitialRender &&
8773
+ !manuallyAnimateOnMount &&
8774
+ (props.initial === false || props.initial === props.animate));
8775
+ isInitialRender = wasReset = false;
8776
+ return !blockAnimation && animations.length
8777
+ ? animate(animations)
8778
+ : Promise.resolve();
8987
8779
  }
8988
8780
  /**
8989
8781
  * Change whether a certain animation type is active.
8990
8782
  */
8991
8783
  function setActive(type, isActive) {
8992
- // If the active state hasn't changed, we can safely do nothing here
8993
8784
  if (state[type].isActive === isActive)
8994
8785
  return Promise.resolve();
8995
- // Propagate active change to children
8996
8786
  visualElement.variantChildren?.forEach((child) => child.animationState?.setActive(type, isActive));
8997
8787
  state[type].isActive = isActive;
8998
8788
  const animations = animateChanges(type);
@@ -9004,7 +8794,13 @@ function createAnimationState(visualElement) {
9004
8794
  return {
9005
8795
  animateChanges,
9006
8796
  setActive,
9007
- setAnimateFunction,
8797
+ /**
8798
+ * Allows tests to inject a mocked animate function.
8799
+ * @internal
8800
+ */
8801
+ setAnimateFunction: (makeAnimator) => {
8802
+ animate = makeAnimator(visualElement);
8803
+ },
9008
8804
  getState: () => state,
9009
8805
  reset: () => {
9010
8806
  state = createState();
@@ -9021,24 +8817,45 @@ function checkVariantsDidChange(prev, next) {
9021
8817
  }
9022
8818
  return false;
9023
8819
  }
9024
- function createTypeState(isActive = false) {
9025
- return {
9026
- isActive,
9027
- protectedKeys: {},
9028
- needsAnimating: {},
9029
- prevResolvedValues: {},
9030
- };
9031
- }
9032
8820
  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
- };
8821
+ const state = {};
8822
+ for (const type of variantPriorityOrder) {
8823
+ state[type] = {
8824
+ isActive: type === "animate",
8825
+ protectedKeys: {},
8826
+ needsAnimating: {},
8827
+ prevResolvedValues: {},
8828
+ };
8829
+ }
8830
+ return state;
8831
+ }
8832
+
8833
+ const numVariantProps = variantProps.length;
8834
+ /**
8835
+ * Get variant context from a visual element's parent chain.
8836
+ * Uses `any` type for visualElement to avoid circular dependencies.
8837
+ */
8838
+ function getVariantContext(visualElement) {
8839
+ if (!visualElement)
8840
+ return undefined;
8841
+ if (!visualElement.isControllingVariants) {
8842
+ const context = visualElement.parent
8843
+ ? getVariantContext(visualElement.parent) || {}
8844
+ : {};
8845
+ if (visualElement.props.initial !== undefined) {
8846
+ context.initial = visualElement.props.initial;
8847
+ }
8848
+ return context;
8849
+ }
8850
+ const context = {};
8851
+ for (let i = 0; i < numVariantProps; i++) {
8852
+ const name = variantProps[i];
8853
+ const prop = visualElement.props[name];
8854
+ if (isVariantLabel(prop) || prop === false) {
8855
+ context[name] = prop;
8856
+ }
8857
+ }
8858
+ return context;
9042
8859
  }
9043
8860
 
9044
8861
  /**
@@ -11865,6 +11682,7 @@ exports.number = number;
11865
11682
  exports.numberValueTypes = numberValueTypes;
11866
11683
  exports.optimizedAppearDataAttribute = optimizedAppearDataAttribute;
11867
11684
  exports.optimizedAppearDataId = optimizedAppearDataId;
11685
+ exports.originProps = originProps;
11868
11686
  exports.parseAnimateLayoutArgs = parseAnimateLayoutArgs;
11869
11687
  exports.parseCSSVariable = parseCSSVariable;
11870
11688
  exports.parseValueFromTransform = parseValueFromTransform;