motion-dom 14.9.0 → 14.10.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.
@@ -1881,6 +1881,12 @@
1881
1881
  return motionUtils.millisecondsToSeconds(this.currentTime);
1882
1882
  }
1883
1883
  set time(newTime) {
1884
+ /**
1885
+ * Seeking out of "finished" leaves the animation paused, so play()
1886
+ * won't renew finished. Renew it here.
1887
+ */
1888
+ if (this.state === "finished")
1889
+ this.updateFinished();
1884
1890
  newTime = motionUtils.secondsToMilliseconds(newTime);
1885
1891
  this.currentTime = newTime;
1886
1892
  if (this.startTime === null ||
@@ -2665,7 +2671,8 @@
2665
2671
  if (transition.autoplay === false) {
2666
2672
  this.animation.pause();
2667
2673
  }
2668
- this.animation.onfinish = () => {
2674
+ this.animation.onfinish = this.onFinish = () => {
2675
+ this.animation.onfinish = null;
2669
2676
  this.finishedTime = this.time;
2670
2677
  if (!pseudoElement) {
2671
2678
  const keyframe = getFinalKeyframe(keyframes, this.options, finalKeyframe, this.speed);
@@ -2689,6 +2696,7 @@
2689
2696
  if (this.isStopped)
2690
2697
  return;
2691
2698
  this.manualStartTime = null;
2699
+ this.animation.onfinish = this.onFinish;
2692
2700
  this.animation.play();
2693
2701
  if (this.state === "finished") {
2694
2702
  this.updateFinished();
@@ -2698,9 +2706,12 @@
2698
2706
  this.animation.pause();
2699
2707
  }
2700
2708
  complete() {
2709
+ if (!this.isStopped)
2710
+ this.animation.onfinish = this.onFinish;
2701
2711
  this.animation.finish?.();
2702
2712
  }
2703
2713
  cancel() {
2714
+ this.animation.onfinish = null;
2704
2715
  try {
2705
2716
  this.animation.cancel();
2706
2717
  }
@@ -2724,10 +2735,8 @@
2724
2735
  else {
2725
2736
  this.commitStyles();
2726
2737
  }
2727
- if (!this.isPseudoElement) {
2728
- this.animation.onfinish = null;
2738
+ if (!this.isPseudoElement)
2729
2739
  this.cancel();
2730
- }
2731
2740
  }
2732
2741
  /**
2733
2742
  * WAAPI doesn't natively have any interruption capabilities.
@@ -2763,8 +2772,13 @@
2763
2772
  this.manualStartTime = null;
2764
2773
  this.finishedTime = null;
2765
2774
  this.animation.currentTime = motionUtils.secondsToMilliseconds(newTime);
2775
+ /**
2776
+ * Seeking out of "finished" leaves the animation paused, so play()
2777
+ * won't renew finished. Renew it here.
2778
+ */
2766
2779
  if (wasFinished) {
2767
2780
  this.animation.pause();
2781
+ this.updateFinished();
2768
2782
  }
2769
2783
  }
2770
2784
  /**
@@ -2798,7 +2812,7 @@
2798
2812
  if (this.allowFlatten) {
2799
2813
  this.animation.effect?.updateTiming({ easing: "linear" });
2800
2814
  }
2801
- this.animation.onfinish = null;
2815
+ this.animation.onfinish = this.onFinish = null;
2802
2816
  if (timeline && supportsScrollTimeline()) {
2803
2817
  this.animation.timeline = timeline;
2804
2818
  onAttach?.(this.animation);
@@ -3192,10 +3206,39 @@
3192
3206
  * rebuilding the WAAPI animation every frame.
3193
3207
  */
3194
3208
  const churnWindow = 100;
3209
+ /**
3210
+ * A WAAPI animation that starts moving later than this, as most segments
3211
+ * of a long sequence do, is built this long before it starts, rather than
3212
+ * all at once at the start.
3213
+ */
3214
+ const buildAhead = 100;
3195
3215
  /**
3196
3216
  * Named easings that are the same cubic-bezier in JS and CSS.
3197
3217
  */
3198
3218
  const isNativeEasing = (ease) => /^(linear|ease(In|Out|InOut))$/.test(ease);
3219
+ /**
3220
+ * The last trace of each easing WAAPI can't run into linear(), with its
3221
+ * number of samples, so the elements of a sequence that share an easing
3222
+ * and duration trace it once.
3223
+ */
3224
+ const traced = new WeakMap();
3225
+ const trace = (ease, samples) => {
3226
+ let last = traced.get(ease);
3227
+ if (last?.[0] !== samples) {
3228
+ last = [
3229
+ samples,
3230
+ "linear(" +
3231
+ defaultOffset(Array(samples + 1)).map((p) => round(ease(p)) + " " + round(p * 100) + "%") +
3232
+ ")",
3233
+ ];
3234
+ traced.set(ease, last);
3235
+ }
3236
+ return last[1];
3237
+ };
3238
+ /**
3239
+ * How many samples trace an easing between two timestamps.
3240
+ */
3241
+ const countSamples = (from, to) => Math.min(maxSamples, Math.max(2, Math.ceil((to - from) / sampleDelta)));
3199
3242
  /**
3200
3243
  * Whether two eased progresses match. False if either is NaN.
3201
3244
  */
@@ -3354,22 +3397,48 @@
3354
3397
  return this.isAccelerated() ? this.sampleAt(time.now()) : undefined;
3355
3398
  }
3356
3399
  /**
3357
- * If this is a two-keyframe tween whose easing WAAPI runs exactly,
3358
- * when it starts and stops moving, and that easing.
3400
+ * If this moves between two keyframes only, holding still either side
3401
+ * of them, as each value of a sequence segment does, when it starts
3402
+ * and stops moving, and its easing: as a string if WAAPI runs it
3403
+ * exactly, otherwise its function, to trace.
3359
3404
  */
3360
3405
  tween() {
3361
- const { keyframes: keyframes$1, type, ease, times, repeat, delay = 0 } = this.options;
3362
- const easing = motionUtils.isBezierDefinition(ease)
3363
- ? cubicBezierAsString(ease)
3364
- : isNativeEasing(ease) && supportedWaapiEasing[ease];
3365
- if (easing &&
3366
- !repeat &&
3367
- !times &&
3368
- keyframes$1.length === 2 &&
3369
- type === keyframes &&
3370
- this.holdTime === null) {
3371
- const start = this.startTime + delay / this.speed;
3372
- return [start, start + this.resolvedDuration / this.speed, easing];
3406
+ const { keyframes: keyframes$1, type, times, repeat, delay = 0 } = this.options;
3407
+ let span = -1;
3408
+ if (repeat || type !== keyframes || this.holdTime !== null) {
3409
+ return;
3410
+ }
3411
+ for (let i = 1; i < keyframes$1.length; i++) {
3412
+ if (keyframes$1[i] !== keyframes$1[i - 1]) {
3413
+ if (span >= 0)
3414
+ return;
3415
+ span = i - 1;
3416
+ }
3417
+ }
3418
+ let { ease } = this.options;
3419
+ if (motionUtils.isEasingArray(ease))
3420
+ ease = ease[span];
3421
+ const offsets = times || defaultOffset(keyframes$1);
3422
+ const at = (i) => this.startTime +
3423
+ (delay + offsets[i] * this.totalDuration) / this.speed;
3424
+ if (span >= 0) {
3425
+ const easing = motionUtils.isBezierDefinition(ease)
3426
+ ? cubicBezierAsString(ease)
3427
+ : isNativeEasing(ease)
3428
+ ? supportedWaapiEasing[ease]
3429
+ : motionUtils.easingDefinitionToFunction(ease);
3430
+ /**
3431
+ * An easing that doesn't run from 0 to 1 doesn't move between
3432
+ * the keyframes, so is traced from the values it gives instead.
3433
+ */
3434
+ return [
3435
+ at(span),
3436
+ at(span + 1),
3437
+ typeof easing === "string" ||
3438
+ (isNear$1(easing(0), 0) && isNear$1(easing(1), 1))
3439
+ ? easing
3440
+ : undefined,
3441
+ ];
3373
3442
  }
3374
3443
  }
3375
3444
  /**
@@ -3413,7 +3482,14 @@
3413
3482
  */
3414
3483
  sync() {
3415
3484
  const now = time.now();
3416
- this.options.motionValue.setWithVelocity(this.sampleAt(now - sampleDelta, true), this.sampleAt(now, true), sampleDelta);
3485
+ const value = this.sampleAt(now, true);
3486
+ /**
3487
+ * A held value doesn't move, so there's no need to sample it
3488
+ * twice.
3489
+ */
3490
+ this.options.motionValue.setWithVelocity(this.holdTime === null
3491
+ ? this.sampleAt(now - sampleDelta, true)
3492
+ : value, value, sampleDelta);
3417
3493
  }
3418
3494
  play() {
3419
3495
  /**
@@ -3427,19 +3503,18 @@
3427
3503
  }
3428
3504
  pause() {
3429
3505
  super.pause();
3430
- if (this.isAccelerated()) {
3431
- this.sync();
3432
- this.group.schedule(false);
3433
- }
3506
+ this.isAccelerated() && this.group.schedule(false);
3434
3507
  }
3435
3508
  /**
3436
- * JSAnimation finishes on its next tick, which only happens on the
3437
- * main thread, so finish now.
3509
+ * JSAnimation finishes on its next tick, which only happens once the
3510
+ * group is on the main thread, so finish now. That includes an
3511
+ * animation that can't be accelerated but reached its end before the
3512
+ * group could move there.
3438
3513
  */
3439
3514
  complete() {
3440
3515
  super.complete();
3441
3516
  const { group } = this;
3442
- group && !group.isJS && this.canAccelerate && this.tick(time.now());
3517
+ group && !group.isJS && this.tick(time.now());
3443
3518
  }
3444
3519
  /**
3445
3520
  * Without a tick to rebase it, a held time still includes the delay,
@@ -3451,14 +3526,8 @@
3451
3526
  }
3452
3527
  return super.time;
3453
3528
  }
3454
- /**
3455
- * A JS animation sets its motion value from a seek on its next tick.
3456
- * This doesn't tick, so set it now, rather than leave it to be set
3457
- * when this next stops.
3458
- */
3459
3529
  set time(newTime) {
3460
3530
  super.time = newTime;
3461
- this.isAccelerated() && this.sync();
3462
3531
  }
3463
3532
  /**
3464
3533
  * Scroll timelines set time every frame, so run these on the main
@@ -3470,6 +3539,20 @@
3470
3539
  return super.attachTimeline(timeline);
3471
3540
  }
3472
3541
  }
3542
+ /**
3543
+ * Groups that build straight after the current task build in one
3544
+ * microtask, rather than one each.
3545
+ */
3546
+ let builds;
3547
+ const buildSoon = (build) => {
3548
+ builds ||
3549
+ queueMicrotask(() => {
3550
+ const queue = builds;
3551
+ builds = undefined;
3552
+ queue.forEach((build) => build());
3553
+ });
3554
+ (builds || (builds = [])).push(build);
3555
+ };
3473
3556
  function createGroup(owner) {
3474
3557
  const element = owner.current;
3475
3558
  const tracks = new Set();
@@ -3489,6 +3572,7 @@
3489
3572
  let isEnded = false;
3490
3573
  let hasChurned = false;
3491
3574
  let lastChurn = -Infinity;
3575
+ let wake;
3492
3576
  const has = (key) => {
3493
3577
  for (const track of tracks)
3494
3578
  if (track.name === key)
@@ -3542,13 +3626,23 @@
3542
3626
  const commit = (values = valuesAt()) => {
3543
3627
  element.style.transform = toTransform(values, true);
3544
3628
  };
3629
+ /**
3630
+ * Cancel a WAAPI animation and remove its handlers, as WebKit never
3631
+ * collects an animation once it has run, nor anything its handlers
3632
+ * reference (#4034).
3633
+ */
3634
+ const cancel = (animation) => {
3635
+ animation.onfinish = animation.oncancel = null;
3636
+ animation.cancel();
3637
+ };
3545
3638
  /**
3546
3639
  * Cancel the WAAPI animations, by default first committing the
3547
3640
  * transform. The renderer writes its own version on its next render.
3548
3641
  */
3549
3642
  const stop = (shouldCommit = true) => {
3643
+ clearTimeout(wake);
3550
3644
  shouldCommit && commit();
3551
- segments.forEach(({ animation }) => animation.cancel());
3645
+ segments.forEach(({ animation }) => cancel(animation));
3552
3646
  segments = [];
3553
3647
  };
3554
3648
  /**
@@ -3569,7 +3663,7 @@
3569
3663
  animation.onfinish = () => {
3570
3664
  segment.isDone = true;
3571
3665
  if (segments.some((s) => s.end === Infinity)) {
3572
- animation.cancel();
3666
+ cancel(animation);
3573
3667
  segments = segments.filter((s) => s !== segment);
3574
3668
  }
3575
3669
  group.schedule(false);
@@ -3600,12 +3694,31 @@
3600
3694
  * two timestamps, the easing that describes it.
3601
3695
  */
3602
3696
  const getEasing = (moving, from, to, bounds) => {
3603
- const samples = Math.min(maxSamples, Math.max(2, Math.ceil((to - from) / sampleDelta)));
3697
+ const samples = countSamples(from, to);
3604
3698
  /**
3605
3699
  * Too many keyframes or repeats to follow with this many samples.
3606
3700
  */
3607
3701
  if (bounds.length * 4 > samples)
3608
3702
  return;
3703
+ /**
3704
+ * First check half way, where values with different easings
3705
+ * usually differ, before sampling every value many times.
3706
+ */
3707
+ let quick;
3708
+ for (const track of moving) {
3709
+ const first = track.numberAt(from);
3710
+ const p = (track.numberAt((from + to) / 2) - first) /
3711
+ (track.numberAt(to) - first);
3712
+ /**
3713
+ * NaN if it doesn't move.
3714
+ */
3715
+ if (p !== p)
3716
+ continue;
3717
+ if (quick === undefined)
3718
+ quick = p;
3719
+ else if (!isNear$1(p, quick))
3720
+ return;
3721
+ }
3609
3722
  const at = spread(from, to, samples).concat(bounds).sort(byTime);
3610
3723
  const position = (t) => motionUtils.progress(from, to, t);
3611
3724
  let shared;
@@ -3662,11 +3775,30 @@
3662
3775
  */
3663
3776
  const sample = (moving, from, to, bounds) => {
3664
3777
  const times = [from];
3778
+ /**
3779
+ * A linear tween is straight between its keyframes, so only the
3780
+ * other values can curve, and a tween only while it moves.
3781
+ */
3782
+ const curved = [];
3783
+ let curveFrom = to;
3784
+ let curveTo = from;
3785
+ for (const track of moving) {
3786
+ const tween = track.tween();
3787
+ if (tween?.[2] !== "linear") {
3788
+ const [trackEnd, cycle] = track.timing();
3789
+ const [a, b] = tween || [from, cycle ? to : trackEnd];
3790
+ curveFrom = Math.min(curveFrom, a);
3791
+ curveTo = Math.max(curveTo, b);
3792
+ curved.push(track);
3793
+ }
3794
+ }
3795
+ curveFrom = Math.max(from, curveFrom);
3796
+ curveTo = Math.min(to, curveTo);
3665
3797
  /**
3666
3798
  * Values that aren't numbers, like calc(), are left to the
3667
3799
  * browser to interpolate.
3668
3800
  */
3669
- const isStraight = (a, b) => moving.every((track) => !(Math.abs(track.numberAt((a + b) / 2) -
3801
+ const isStraight = (a, b) => curved.every((track) => !(Math.abs(track.numberAt((a + b) / 2) -
3670
3802
  (track.numberAt(a) + track.numberAt(b)) / 2) >
3671
3803
  (track.name.startsWith("scale")
3672
3804
  ? scaleTolerance
@@ -3691,10 +3823,13 @@
3691
3823
  }
3692
3824
  };
3693
3825
  /**
3694
- * Split at least every 100ms, and at every keyframe and repeat,
3695
- * either side of where a value can jump.
3826
+ * Split at least every 100ms where a value can curve, and at every
3827
+ * keyframe and repeat, either side of where a value can jump.
3696
3828
  */
3697
- const ends = spread(from, to, Math.ceil((to - from) / 100));
3829
+ const ends = curveTo > curveFrom
3830
+ ? spread(curveFrom, curveTo, Math.ceil((curveTo - curveFrom) / 100))
3831
+ : [];
3832
+ ends.push(to);
3698
3833
  for (const t of bounds)
3699
3834
  ends.push(t, Math.min(t + 0.01, to));
3700
3835
  let start = from;
@@ -3714,12 +3849,20 @@
3714
3849
  for (const track of moving) {
3715
3850
  const tween = track.tween();
3716
3851
  if (!tween ||
3717
- shared?.some((v, i) => i < 2
3718
- ? Math.abs(tween[i] - v) > 1
3719
- : tween[i] !== v)) {
3852
+ (shared &&
3853
+ (Math.abs(tween[0] - shared[0]) > 1 ||
3854
+ Math.abs(tween[1] - shared[1]) > 1))) {
3720
3855
  return;
3721
3856
  }
3722
- shared = tween;
3857
+ /**
3858
+ * Easings that differ, like the springs a sequence makes for
3859
+ * each value, can still be the same curve.
3860
+ */
3861
+ shared = [
3862
+ tween[0],
3863
+ tween[1],
3864
+ !shared || tween[2] === shared[2] ? tween[2] : undefined,
3865
+ ];
3723
3866
  }
3724
3867
  return shared;
3725
3868
  };
@@ -3774,14 +3917,8 @@
3774
3917
  const [trackEnd, trackCycle] = track.timing();
3775
3918
  const isHeld = trackEnd === Infinity;
3776
3919
  built.set(track, [track.at(now), track.speed, isHeld]);
3777
- /**
3778
- * Held animations don't move, so keep their motion values up
3779
- * to date, as a JSAnimation would on a seek.
3780
- */
3781
- if (isHeld) {
3782
- track.sync();
3920
+ if (isHeld)
3783
3921
  continue;
3784
- }
3785
3922
  if (track.speed < 0)
3786
3923
  return;
3787
3924
  if (trackCycle) {
@@ -3821,12 +3958,35 @@
3821
3958
  */
3822
3959
  const tween = !cycle && getTween(moving);
3823
3960
  try {
3824
- if (tween && tween[1] > now) {
3825
- const [from, to, easing] = tween;
3826
- add(run(moving, from, to, easing), from - now, to - from);
3827
- }
3828
- else if (end > now) {
3829
- add(run(moving, now, end), 0, end - now);
3961
+ if (end > now) {
3962
+ let easing;
3963
+ if (tween && tween[1] > now) {
3964
+ const [from, to, ease] = tween;
3965
+ /**
3966
+ * Until it's built, the motion values hold where it
3967
+ * starts, for the renderer to write.
3968
+ */
3969
+ if (from - now > buildAhead) {
3970
+ moving.forEach((track) => track.sync());
3971
+ wake = setTimeout(() => group.schedule(true), from - now - buildAhead);
3972
+ return true;
3973
+ }
3974
+ easing =
3975
+ typeof ease === "string"
3976
+ ? ease
3977
+ : ease
3978
+ ? trace(ease, countSamples(from, to))
3979
+ : getEasing(moving, from, to, []);
3980
+ /**
3981
+ * It holds its last keyframe until the animations end.
3982
+ */
3983
+ easing &&
3984
+ add(run(moving, from, to, easing, {
3985
+ fill: "both",
3986
+ endDelay: end - to,
3987
+ }), from - now, end - from);
3988
+ }
3989
+ easing || add(run(moving, now, end), 0, end - now);
3830
3990
  /**
3831
3991
  * Animations that end sooner finish on the events of
3832
3992
  * empty animations that end with them.
@@ -3875,9 +4035,14 @@
3875
4035
  const [builtTime, builtSpeed, wasHeld] = state;
3876
4036
  const trackTime = track.at(now);
3877
4037
  const isHeld = track.timing()[0] === Infinity;
4038
+ /**
4039
+ * With nothing on the compositor, a held animation is set
4040
+ * where it's held, so it can be seeked as often as needed.
4041
+ */
3878
4042
  if (wasHeld) {
3879
- if (isHeld && trackTime === builtTime)
4043
+ if (isHeld && (trackTime === builtTime || !segments.length)) {
3880
4044
  continue;
4045
+ }
3881
4046
  return false;
3882
4047
  }
3883
4048
  const trackElapsed = (trackTime - builtTime) / builtSpeed;
@@ -4007,6 +4172,12 @@
4007
4172
  if (!animate(now))
4008
4173
  return group.demote();
4009
4174
  }
4175
+ /**
4176
+ * Held animations don't move, so set their motion values to where
4177
+ * they're held, as a JSAnimation would on its next tick. Pausing
4178
+ * and seeking schedule this, so it's once however many there are.
4179
+ */
4180
+ tracks.forEach((track) => track.timing()[0] === Infinity && track.sync());
4010
4181
  };
4011
4182
  const group = {
4012
4183
  tracks,
@@ -4026,7 +4197,7 @@
4026
4197
  isScheduled = true;
4027
4198
  frameData.isProcessing
4028
4199
  ? frame.render(build, false, true)
4029
- : queueMicrotask(build);
4200
+ : buildSoon(build);
4030
4201
  }
4031
4202
  },
4032
4203
  /**
@@ -4355,7 +4526,8 @@
4355
4526
  constructor(animation) {
4356
4527
  super();
4357
4528
  this.animation = animation;
4358
- animation.onfinish = () => {
4529
+ animation.onfinish = this.onFinish = () => {
4530
+ animation.onfinish = null;
4359
4531
  this.finishedTime = this.time;
4360
4532
  this.notifyFinished();
4361
4533
  };
@@ -8957,12 +9129,15 @@
8957
9129
  let hasRelativeLayoutChanged = false;
8958
9130
  const relativeParent = node.getClosestProjectingParent();
8959
9131
  /**
8960
- * A snapshot resumed from another element is only relative to this
8961
- * node's relativeParent if it's a layoutRoot that contains both.
9132
+ * A snapshot from another element is only relative to this node's
9133
+ * relativeParent if it's a layoutRoot that contains both. A node can
9134
+ * hold one without resumeFrom, when the element it came from changed
9135
+ * layoutId but stayed mounted (see switchLayoutId), or a remix
9136
+ * layout exit cleared it (see markLayoutExit).
8962
9137
  */
8963
- if (!node.resumeFrom ||
9138
+ if (!isShared ||
8964
9139
  (relativeParent?.options.layoutRoot &&
8965
- node.resumeFrom.path.includes(relativeParent))) {
9140
+ node.resumeFrom?.path.includes(relativeParent))) {
8966
9141
  /**
8967
9142
  * If the relativeParent is itself resuming from a different element then
8968
9143
  * the relative snapshot is not relavent
@@ -9479,9 +9654,13 @@
9479
9654
  * If this element has been mounted before (e.g. after a Suspense
9480
9655
  * unmount/remount), reset motion values to their initial state
9481
9656
  * so animations replay correctly from initial → animate, unless
9482
- * the presence asks to keep them.
9657
+ * the presence asks to keep them or the element is only shown
9658
+ * again to exit (#4049).
9483
9659
  */
9484
- if (this.hasBeenMounted && !this.presenceContext?.keepValuesOnRemount) {
9660
+ const presence = this.presenceContext;
9661
+ if (this.hasBeenMounted &&
9662
+ presence?.isPresent !== false &&
9663
+ !presence?.keepValuesOnRemount) {
9485
9664
  for (const key in this.initialValues) {
9486
9665
  this.values.get(key)?.jump(this.initialValues[key]);
9487
9666
  this.latestValues[key] = this.initialValues[key];
@@ -10547,7 +10726,7 @@
10547
10726
 
10548
10727
  const overflowStyles = /*#__PURE__*/ new Set(["auto", "scroll"]);
10549
10728
  class PanSession {
10550
- constructor(event, handlers, { transformPagePoint, contextWindow = window, dragSnapToOrigin = false, distanceThreshold = 3, element, } = {}) {
10729
+ constructor(event, handlers, { transformPagePoint, contextWindow = window, dragSnapToOrigin = false, distanceThreshold = 3, element, immediate, } = {}) {
10551
10730
  this.startEvent = null;
10552
10731
  this.lastMoveEvent = null;
10553
10732
  this.lastMoveEventInfo = null;
@@ -10633,7 +10812,7 @@
10633
10812
  this.dragSnapToOrigin = dragSnapToOrigin;
10634
10813
  this.handlers = handlers;
10635
10814
  this.transformPagePoint = transformPagePoint;
10636
- this.distanceThreshold = distanceThreshold;
10815
+ this.distanceThreshold = immediate ? 0 : distanceThreshold;
10637
10816
  this.contextWindow = contextWindow || window;
10638
10817
  const info = extractEventInfo(event);
10639
10818
  const initialInfo = transformPoint(info, this.transformPagePoint);
@@ -10657,6 +10836,10 @@
10657
10836
  if (element) {
10658
10837
  this.startScrollTracking(element);
10659
10838
  }
10839
+ if (immediate) {
10840
+ this.handlePointerMove(event, info);
10841
+ this.updatePoint();
10842
+ }
10660
10843
  }
10661
10844
  /**
10662
10845
  * Start tracking scroll on ancestors and window.
@@ -11032,14 +11215,14 @@
11032
11215
  this.visualElement = visualElement;
11033
11216
  this.readProps = readProps;
11034
11217
  }
11035
- start(originEvent, { snapToCursor = false, distanceThreshold } = {}) {
11218
+ start(originEvent, options = {}) {
11036
11219
  /**
11037
11220
  * Don't start dragging if this component is exiting
11038
11221
  */
11039
11222
  if (this.visualElement.presenceContext?.isPresent === false)
11040
11223
  return;
11041
11224
  const onSessionStart = (event) => {
11042
- if (snapToCursor)
11225
+ if (options.snapToCursor)
11043
11226
  this.snapToCursor(event);
11044
11227
  this.stopAnimation();
11045
11228
  };
@@ -11161,9 +11344,9 @@
11161
11344
  onSessionEnd,
11162
11345
  resumeAnimation,
11163
11346
  }, {
11347
+ ...options,
11164
11348
  transformPagePoint: this.visualElement.getTransformPagePoint(),
11165
11349
  dragSnapToOrigin,
11166
- distanceThreshold,
11167
11350
  contextWindow: this.visualElement.current?.ownerDocument.defaultView,
11168
11351
  element: this.visualElement.current,
11169
11352
  });
@@ -11611,8 +11794,10 @@
11611
11794
  return isElementConstraints(dragConstraints) && !!onMeasureDragConstraints;
11612
11795
  }
11613
11796
 
11614
- const toProps = ({ axis, constraints, elastic, momentum, transition, snapToOrigin, directionLock, propagation, onStart, onMove, onEnd, onDirectionLock, onTransitionEnd, onMeasureConstraints, }) => ({
11797
+ const toProps = ({ axis, constraints, elastic, momentum, transition, snapToOrigin, directionLock, propagation, onStart, onMove, onEnd, onDirectionLock, onTransitionEnd, onMeasureConstraints, x, y, }) => ({
11615
11798
  drag: axis || true,
11799
+ _dragX: x || (y && motionValue(0)),
11800
+ _dragY: y || (x && motionValue(0)),
11616
11801
  dragConstraints: constraints,
11617
11802
  dragElastic: elastic,
11618
11803
  dragMomentum: momentum,
@@ -11821,7 +12006,7 @@
11821
12006
  */
11822
12007
  let isKeyPressing = false;
11823
12008
  const handleKeydown = filterEvents(() => {
11824
- if (isPressing.has(element))
12009
+ if (isKeyPressing || isPressing.has(element))
11825
12010
  return;
11826
12011
  isKeyPressing = true;
11827
12012
  firePointerEvent(element, "down");
@@ -11853,6 +12038,18 @@
11853
12038
  return isPrimaryPointer(event) && !isDragActive();
11854
12039
  }
11855
12040
  const claimedPointerDownEvents = new WeakSet();
12041
+ /**
12042
+ * Max distance in px between consecutive presses of a `count` sequence.
12043
+ * Android's `ViewConfiguration` double tap slop is 100dp.
12044
+ */
12045
+ const sequenceSlop = 100;
12046
+ /**
12047
+ * Max distance in px a pointer can move during a held press (`hold`
12048
+ * pending, or `repeat`). Android's `ViewConfiguration` touch slop is 8dp.
12049
+ */
12050
+ const holdSlop = 8;
12051
+ const exceeds = (a, b, max) => Math.hypot(a.clientX - b.clientX, a.clientY - b.clientY) > max;
12052
+ const preventDefault = (event) => event.preventDefault();
11856
12053
  /**
11857
12054
  * Create a press gesture.
11858
12055
  *
@@ -11870,22 +12067,110 @@
11870
12067
  * to be focusable for this to work, whereas a press gesture will
11871
12068
  * make an element focusable by default.
11872
12069
  *
12070
+ * `onPressStart` fires on the first pointer or key down, and can return
12071
+ * a function to call when the press ends, or an object of handlers for
12072
+ * this press:
12073
+ *
12074
+ * ```js
12075
+ * press(button, () => ({
12076
+ * onHold: (progress) => {},
12077
+ * onComplete: (event, { success }) => {},
12078
+ * }), { hold: 0.5 })
12079
+ * ```
12080
+ *
11873
12081
  * @public
11874
12082
  */
11875
12083
  function press(targetOrSelector, onPressStart, options = {}) {
11876
12084
  onPressStart = bindToScope(onPressStart);
11877
12085
  const [targets, eventOptions, cancelEvents] = setupGesture(targetOrSelector, options);
12086
+ const sequences = new WeakMap();
11878
12087
  const startPress = (startEvent) => {
11879
12088
  const target = startEvent.currentTarget;
11880
12089
  if (!isValidPressEvent(startEvent))
11881
12090
  return;
11882
12091
  if (claimedPointerDownEvents.has(startEvent))
11883
12092
  return;
11884
- isPressing.add(target);
11885
12093
  if (options.stopPropagation) {
11886
12094
  claimedPointerDownEvents.add(startEvent);
11887
12095
  }
11888
- const onPressEnd = onPressStart(target, startEvent);
12096
+ /**
12097
+ * Options are read per press so they can be updated between presses.
12098
+ */
12099
+ const { count: presses = 1, countTimeout = 0.3, hold = 0, repeat, } = options;
12100
+ const downTime = time.now();
12101
+ const holdTime = motionUtils.secondsToMilliseconds(hold);
12102
+ const timeout = motionUtils.secondsToMilliseconds(countTimeout);
12103
+ let previous = sequences.get(target);
12104
+ /**
12105
+ * A press during a `count` sequence continues it. If it's too late
12106
+ * or too far away, the sequence is cancelled and this press starts
12107
+ * a new one.
12108
+ */
12109
+ if (previous?.expire(exceeds(previous.event, startEvent, sequenceSlop))) {
12110
+ previous = undefined;
12111
+ }
12112
+ else if (previous) {
12113
+ cancelFrame(previous.expire);
12114
+ sequences.delete(target);
12115
+ }
12116
+ const count = previous ? previous.count + 1 : 1;
12117
+ const isFinal = count >= presses;
12118
+ const isHeld = isFinal && (hold || repeat);
12119
+ let isActive = !hold;
12120
+ let repeats = 0;
12121
+ let handlers;
12122
+ isPressing.add(target);
12123
+ if (previous) {
12124
+ handlers = previous.handlers;
12125
+ }
12126
+ else {
12127
+ const result = onPressStart(target, startEvent);
12128
+ handlers =
12129
+ typeof result === "function"
12130
+ ? { onComplete: result }
12131
+ : result || {};
12132
+ }
12133
+ isFinal && isActive && handlers.onActive?.();
12134
+ /**
12135
+ * While held, `onHold` reports progress from 0 to 1 over `hold`,
12136
+ * then over each `repeat`. Each cycle reports exactly 1 at its end,
12137
+ * then 0 on the next frame.
12138
+ */
12139
+ const repeatTime = motionUtils.secondsToMilliseconds(repeat);
12140
+ let length = holdTime || repeatTime;
12141
+ let cycleEnd = downTime + length;
12142
+ let isCycleStart = true;
12143
+ const tick = () => {
12144
+ if (eventOptions.signal.aborted)
12145
+ return cancelFrame(tick);
12146
+ let progress = Math.min(1 - (cycleEnd - time.now()) / length, 1);
12147
+ /**
12148
+ * The frame after a boundary reports 0, so each cycle visibly
12149
+ * restarts. Boundaries are timed from `downTime`, so this
12150
+ * doesn't delay the next one. If a long frame has already
12151
+ * passed the next boundary, it reports 1 instead.
12152
+ */
12153
+ if (isCycleStart && progress < 1)
12154
+ progress = 0;
12155
+ isCycleStart = false;
12156
+ handlers.onHold?.(progress);
12157
+ if (progress < 1)
12158
+ return;
12159
+ if (isActive) {
12160
+ handlers.onRepeat?.(++repeats);
12161
+ }
12162
+ else {
12163
+ isActive = true;
12164
+ handlers.onActive?.();
12165
+ }
12166
+ if (repeat) {
12167
+ isCycleStart = true;
12168
+ cycleEnd += length = repeatTime;
12169
+ }
12170
+ else {
12171
+ cancelFrame(tick);
12172
+ }
12173
+ };
11889
12174
  /**
11890
12175
  * End listeners run in the capture phase so a descendant calling
11891
12176
  * stopPropagation() in its own pointerup handler can't prevent the
@@ -11896,14 +12181,55 @@
11896
12181
  const onPointerEnd = (endEvent, success) => {
11897
12182
  window.removeEventListener("pointerup", onPointerUp, endEventOptions);
11898
12183
  window.removeEventListener("pointercancel", onPointerCancel, endEventOptions);
11899
- if (isPressing.has(target)) {
11900
- isPressing.delete(target);
11901
- }
11902
- if (!isValidPressEvent(endEvent)) {
12184
+ window.removeEventListener("pointermove", onPointerMove, endEventOptions);
12185
+ target.removeEventListener("contextmenu", preventDefault);
12186
+ cancelFrame(tick);
12187
+ isPressing.delete(target);
12188
+ if (!isPrimaryPointer(endEvent))
11903
12189
  return;
12190
+ /**
12191
+ * A press that ends while a drag is active is a cancel.
12192
+ */
12193
+ if (isDragActive())
12194
+ success = false;
12195
+ if (success && !isFinal) {
12196
+ /**
12197
+ * Wait for the next press of the sequence, and cancel the
12198
+ * gesture if it doesn't arrive in time.
12199
+ */
12200
+ const end = time.now();
12201
+ /**
12202
+ * Cancels the sequence if it's `force`d, or the interval
12203
+ * has passed. Runs every frame, where `force` is frame
12204
+ * data, so it's checked with `=== true`.
12205
+ */
12206
+ const expire = (force) => {
12207
+ const isAborted = eventOptions.signal.aborted;
12208
+ if (isAborted ||
12209
+ force === true ||
12210
+ time.now() - end > timeout) {
12211
+ cancelFrame(expire);
12212
+ sequences.delete(target);
12213
+ isAborted ||
12214
+ handlers.onComplete?.(endEvent, { success: false });
12215
+ return true;
12216
+ }
12217
+ };
12218
+ sequences.set(target, {
12219
+ count,
12220
+ event: startEvent,
12221
+ handlers,
12222
+ expire,
12223
+ });
12224
+ frame.update(expire, true);
11904
12225
  }
11905
- if (typeof onPressEnd === "function") {
11906
- onPressEnd(endEvent, { success });
12226
+ else {
12227
+ /**
12228
+ * Releasing before `hold` is reached is a cancel.
12229
+ */
12230
+ handlers.onComplete?.(endEvent, {
12231
+ success: success && isActive,
12232
+ });
11907
12233
  }
11908
12234
  };
11909
12235
  const onPointerUp = (upEvent) => {
@@ -11915,8 +12241,35 @@
11915
12241
  const onPointerCancel = (cancelEvent) => {
11916
12242
  onPointerEnd(cancelEvent, false);
11917
12243
  };
12244
+ /**
12245
+ * Moving during a pending long press, or a repeating press, cancels
12246
+ * it, so scrolling on touch devices doesn't trigger it. A drag
12247
+ * starting during a `count` sequence cancels it.
12248
+ */
12249
+ const onPointerMove = (moveEvent) => {
12250
+ if (isPrimaryPointer(moveEvent) &&
12251
+ (((repeat || !isActive) &&
12252
+ exceeds(startEvent, moveEvent, holdSlop)) ||
12253
+ (presses > 1 && isDragActive()))) {
12254
+ onPointerCancel(moveEvent);
12255
+ }
12256
+ };
11918
12257
  window.addEventListener("pointerup", onPointerUp, endEventOptions);
11919
12258
  window.addEventListener("pointercancel", onPointerCancel, endEventOptions);
12259
+ if (isHeld || presses > 1) {
12260
+ window.addEventListener("pointermove", onPointerMove, endEventOptions);
12261
+ }
12262
+ if (isHeld) {
12263
+ frame.update(tick, true);
12264
+ /**
12265
+ * Touch devices open a context menu or callout on long press,
12266
+ * which would cancel the gesture.
12267
+ */
12268
+ target.addEventListener("contextmenu", preventDefault, {
12269
+ ...eventOptions,
12270
+ passive: false,
12271
+ });
12272
+ }
11920
12273
  };
11921
12274
  targets.forEach((target) => {
11922
12275
  const pointerDownTarget = options.useGlobalTarget ? window : target;