motion-dom 12.30.1 → 12.31.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -3367,11 +3367,15 @@ function animateTarget(visualElement, targetAndTransition, { delay = 0, transiti
3367
3367
  }
3368
3368
  }
3369
3369
  if (transitionEnd) {
3370
- Promise.all(animations).then(() => {
3371
- frame.update(() => {
3372
- transitionEnd && setTarget(visualElement, transitionEnd);
3373
- });
3370
+ const applyTransitionEnd = () => frame.update(() => {
3371
+ transitionEnd && setTarget(visualElement, transitionEnd);
3374
3372
  });
3373
+ if (animations.length) {
3374
+ Promise.all(animations).then(applyTransitionEnd);
3375
+ }
3376
+ else {
3377
+ applyTransitionEnd();
3378
+ }
3375
3379
  }
3376
3380
  return animations;
3377
3381
  }
@@ -4225,23 +4229,55 @@ function isValidHover(event) {
4225
4229
  */
4226
4230
  function hover(elementOrSelector, onHoverStart, options = {}) {
4227
4231
  const [elements, eventOptions, cancel] = setupGesture(elementOrSelector, options);
4228
- const onPointerEnter = (enterEvent) => {
4229
- if (!isValidHover(enterEvent))
4230
- return;
4231
- const { target } = enterEvent;
4232
- const onHoverEnd = onHoverStart(target, enterEvent);
4233
- if (typeof onHoverEnd !== "function" || !target)
4234
- return;
4232
+ elements.forEach((element) => {
4233
+ let isPressed = false;
4234
+ let deferredHoverEnd = false;
4235
+ let hoverEndCallback;
4236
+ const removePointerLeave = () => {
4237
+ element.removeEventListener("pointerleave", onPointerLeave);
4238
+ };
4239
+ const endHover = (event) => {
4240
+ if (hoverEndCallback) {
4241
+ hoverEndCallback(event);
4242
+ hoverEndCallback = undefined;
4243
+ }
4244
+ removePointerLeave();
4245
+ };
4246
+ const onPointerUp = (event) => {
4247
+ isPressed = false;
4248
+ window.removeEventListener("pointerup", onPointerUp);
4249
+ window.removeEventListener("pointercancel", onPointerUp);
4250
+ if (deferredHoverEnd) {
4251
+ deferredHoverEnd = false;
4252
+ endHover(event);
4253
+ }
4254
+ };
4255
+ const onPointerDown = () => {
4256
+ isPressed = true;
4257
+ window.addEventListener("pointerup", onPointerUp, eventOptions);
4258
+ window.addEventListener("pointercancel", onPointerUp, eventOptions);
4259
+ };
4235
4260
  const onPointerLeave = (leaveEvent) => {
4236
- if (!isValidHover(leaveEvent))
4261
+ if (leaveEvent.pointerType === "touch")
4262
+ return;
4263
+ if (isPressed) {
4264
+ deferredHoverEnd = true;
4237
4265
  return;
4238
- onHoverEnd(leaveEvent);
4239
- target.removeEventListener("pointerleave", onPointerLeave);
4266
+ }
4267
+ endHover(leaveEvent);
4268
+ };
4269
+ const onPointerEnter = (enterEvent) => {
4270
+ if (!isValidHover(enterEvent))
4271
+ return;
4272
+ deferredHoverEnd = false;
4273
+ const onHoverEnd = onHoverStart(element, enterEvent);
4274
+ if (typeof onHoverEnd !== "function")
4275
+ return;
4276
+ hoverEndCallback = onHoverEnd;
4277
+ element.addEventListener("pointerleave", onPointerLeave, eventOptions);
4240
4278
  };
4241
- target.addEventListener("pointerleave", onPointerLeave, eventOptions);
4242
- };
4243
- elements.forEach((element) => {
4244
4279
  element.addEventListener("pointerenter", onPointerEnter, eventOptions);
4280
+ element.addEventListener("pointerdown", onPointerDown, eventOptions);
4245
4281
  });
4246
4282
  return cancel;
4247
4283
  }
@@ -5443,6 +5479,11 @@ class VisualElement {
5443
5479
  * if any motion values need to be removed after props are updated.
5444
5480
  */
5445
5481
  this.prevMotionValues = {};
5482
+ /**
5483
+ * Track whether this element has been mounted before, to detect
5484
+ * remounts after Suspense unmount/remount cycles.
5485
+ */
5486
+ this.hasBeenMounted = false;
5446
5487
  /**
5447
5488
  * An object containing a SubscriptionManager for each active event.
5448
5489
  */
@@ -5506,6 +5547,17 @@ class VisualElement {
5506
5547
  }
5507
5548
  }
5508
5549
  mount(instance) {
5550
+ /**
5551
+ * If this element has been mounted before (e.g. after a Suspense
5552
+ * unmount/remount), reset motion values to their initial state
5553
+ * so animations replay correctly from initial → animate.
5554
+ */
5555
+ if (this.hasBeenMounted) {
5556
+ for (const key in this.initialValues) {
5557
+ this.values.get(key)?.jump(this.initialValues[key]);
5558
+ this.latestValues[key] = this.initialValues[key];
5559
+ }
5560
+ }
5509
5561
  this.current = instance;
5510
5562
  visualElementStore.set(instance, this);
5511
5563
  if (this.projection && !this.projection.instance) {
@@ -5541,6 +5593,7 @@ class VisualElement {
5541
5593
  this.shouldSkipAnimations = this.skipAnimationsConfig ?? false;
5542
5594
  this.parent?.addChild(this);
5543
5595
  this.update(this.props, this.presenceContext);
5596
+ this.hasBeenMounted = true;
5544
5597
  }
5545
5598
  unmount() {
5546
5599
  this.projection && this.projection.unmount();