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