motion-dom 14.9.1 → 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 ||
@@ -2766,8 +2772,13 @@
2766
2772
  this.manualStartTime = null;
2767
2773
  this.finishedTime = null;
2768
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
+ */
2769
2779
  if (wasFinished) {
2770
2780
  this.animation.pause();
2781
+ this.updateFinished();
2771
2782
  }
2772
2783
  }
2773
2784
  /**
@@ -3195,10 +3206,39 @@
3195
3206
  * rebuilding the WAAPI animation every frame.
3196
3207
  */
3197
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;
3198
3215
  /**
3199
3216
  * Named easings that are the same cubic-bezier in JS and CSS.
3200
3217
  */
3201
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)));
3202
3242
  /**
3203
3243
  * Whether two eased progresses match. False if either is NaN.
3204
3244
  */
@@ -3357,22 +3397,48 @@
3357
3397
  return this.isAccelerated() ? this.sampleAt(time.now()) : undefined;
3358
3398
  }
3359
3399
  /**
3360
- * If this is a two-keyframe tween whose easing WAAPI runs exactly,
3361
- * 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.
3362
3404
  */
3363
3405
  tween() {
3364
- const { keyframes: keyframes$1, type, ease, times, repeat, delay = 0 } = this.options;
3365
- const easing = motionUtils.isBezierDefinition(ease)
3366
- ? cubicBezierAsString(ease)
3367
- : isNativeEasing(ease) && supportedWaapiEasing[ease];
3368
- if (easing &&
3369
- !repeat &&
3370
- !times &&
3371
- keyframes$1.length === 2 &&
3372
- type === keyframes &&
3373
- this.holdTime === null) {
3374
- const start = this.startTime + delay / this.speed;
3375
- 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
+ ];
3376
3442
  }
3377
3443
  }
3378
3444
  /**
@@ -3416,7 +3482,14 @@
3416
3482
  */
3417
3483
  sync() {
3418
3484
  const now = time.now();
3419
- 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);
3420
3493
  }
3421
3494
  play() {
3422
3495
  /**
@@ -3430,10 +3503,7 @@
3430
3503
  }
3431
3504
  pause() {
3432
3505
  super.pause();
3433
- if (this.isAccelerated()) {
3434
- this.sync();
3435
- this.group.schedule(false);
3436
- }
3506
+ this.isAccelerated() && this.group.schedule(false);
3437
3507
  }
3438
3508
  /**
3439
3509
  * JSAnimation finishes on its next tick, which only happens once the
@@ -3456,14 +3526,8 @@
3456
3526
  }
3457
3527
  return super.time;
3458
3528
  }
3459
- /**
3460
- * A JS animation sets its motion value from a seek on its next tick.
3461
- * This doesn't tick, so set it now, rather than leave it to be set
3462
- * when this next stops.
3463
- */
3464
3529
  set time(newTime) {
3465
3530
  super.time = newTime;
3466
- this.isAccelerated() && this.sync();
3467
3531
  }
3468
3532
  /**
3469
3533
  * Scroll timelines set time every frame, so run these on the main
@@ -3475,6 +3539,20 @@
3475
3539
  return super.attachTimeline(timeline);
3476
3540
  }
3477
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
+ };
3478
3556
  function createGroup(owner) {
3479
3557
  const element = owner.current;
3480
3558
  const tracks = new Set();
@@ -3494,6 +3572,7 @@
3494
3572
  let isEnded = false;
3495
3573
  let hasChurned = false;
3496
3574
  let lastChurn = -Infinity;
3575
+ let wake;
3497
3576
  const has = (key) => {
3498
3577
  for (const track of tracks)
3499
3578
  if (track.name === key)
@@ -3561,6 +3640,7 @@
3561
3640
  * transform. The renderer writes its own version on its next render.
3562
3641
  */
3563
3642
  const stop = (shouldCommit = true) => {
3643
+ clearTimeout(wake);
3564
3644
  shouldCommit && commit();
3565
3645
  segments.forEach(({ animation }) => cancel(animation));
3566
3646
  segments = [];
@@ -3614,12 +3694,31 @@
3614
3694
  * two timestamps, the easing that describes it.
3615
3695
  */
3616
3696
  const getEasing = (moving, from, to, bounds) => {
3617
- const samples = Math.min(maxSamples, Math.max(2, Math.ceil((to - from) / sampleDelta)));
3697
+ const samples = countSamples(from, to);
3618
3698
  /**
3619
3699
  * Too many keyframes or repeats to follow with this many samples.
3620
3700
  */
3621
3701
  if (bounds.length * 4 > samples)
3622
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
+ }
3623
3722
  const at = spread(from, to, samples).concat(bounds).sort(byTime);
3624
3723
  const position = (t) => motionUtils.progress(from, to, t);
3625
3724
  let shared;
@@ -3676,11 +3775,30 @@
3676
3775
  */
3677
3776
  const sample = (moving, from, to, bounds) => {
3678
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);
3679
3797
  /**
3680
3798
  * Values that aren't numbers, like calc(), are left to the
3681
3799
  * browser to interpolate.
3682
3800
  */
3683
- 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) -
3684
3802
  (track.numberAt(a) + track.numberAt(b)) / 2) >
3685
3803
  (track.name.startsWith("scale")
3686
3804
  ? scaleTolerance
@@ -3705,10 +3823,13 @@
3705
3823
  }
3706
3824
  };
3707
3825
  /**
3708
- * Split at least every 100ms, and at every keyframe and repeat,
3709
- * 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.
3710
3828
  */
3711
- 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);
3712
3833
  for (const t of bounds)
3713
3834
  ends.push(t, Math.min(t + 0.01, to));
3714
3835
  let start = from;
@@ -3728,12 +3849,20 @@
3728
3849
  for (const track of moving) {
3729
3850
  const tween = track.tween();
3730
3851
  if (!tween ||
3731
- shared?.some((v, i) => i < 2
3732
- ? Math.abs(tween[i] - v) > 1
3733
- : tween[i] !== v)) {
3852
+ (shared &&
3853
+ (Math.abs(tween[0] - shared[0]) > 1 ||
3854
+ Math.abs(tween[1] - shared[1]) > 1))) {
3734
3855
  return;
3735
3856
  }
3736
- 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
+ ];
3737
3866
  }
3738
3867
  return shared;
3739
3868
  };
@@ -3788,14 +3917,8 @@
3788
3917
  const [trackEnd, trackCycle] = track.timing();
3789
3918
  const isHeld = trackEnd === Infinity;
3790
3919
  built.set(track, [track.at(now), track.speed, isHeld]);
3791
- /**
3792
- * Held animations don't move, so keep their motion values up
3793
- * to date, as a JSAnimation would on a seek.
3794
- */
3795
- if (isHeld) {
3796
- track.sync();
3920
+ if (isHeld)
3797
3921
  continue;
3798
- }
3799
3922
  if (track.speed < 0)
3800
3923
  return;
3801
3924
  if (trackCycle) {
@@ -3835,12 +3958,35 @@
3835
3958
  */
3836
3959
  const tween = !cycle && getTween(moving);
3837
3960
  try {
3838
- if (tween && tween[1] > now) {
3839
- const [from, to, easing] = tween;
3840
- add(run(moving, from, to, easing), from - now, to - from);
3841
- }
3842
- else if (end > now) {
3843
- 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);
3844
3990
  /**
3845
3991
  * Animations that end sooner finish on the events of
3846
3992
  * empty animations that end with them.
@@ -3889,9 +4035,14 @@
3889
4035
  const [builtTime, builtSpeed, wasHeld] = state;
3890
4036
  const trackTime = track.at(now);
3891
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
+ */
3892
4042
  if (wasHeld) {
3893
- if (isHeld && trackTime === builtTime)
4043
+ if (isHeld && (trackTime === builtTime || !segments.length)) {
3894
4044
  continue;
4045
+ }
3895
4046
  return false;
3896
4047
  }
3897
4048
  const trackElapsed = (trackTime - builtTime) / builtSpeed;
@@ -4021,6 +4172,12 @@
4021
4172
  if (!animate(now))
4022
4173
  return group.demote();
4023
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());
4024
4181
  };
4025
4182
  const group = {
4026
4183
  tracks,
@@ -4040,7 +4197,7 @@
4040
4197
  isScheduled = true;
4041
4198
  frameData.isProcessing
4042
4199
  ? frame.render(build, false, true)
4043
- : queueMicrotask(build);
4200
+ : buildSoon(build);
4044
4201
  }
4045
4202
  },
4046
4203
  /**
@@ -10569,7 +10726,7 @@
10569
10726
 
10570
10727
  const overflowStyles = /*#__PURE__*/ new Set(["auto", "scroll"]);
10571
10728
  class PanSession {
10572
- constructor(event, handlers, { transformPagePoint, contextWindow = window, dragSnapToOrigin = false, distanceThreshold = 3, element, } = {}) {
10729
+ constructor(event, handlers, { transformPagePoint, contextWindow = window, dragSnapToOrigin = false, distanceThreshold = 3, element, immediate, } = {}) {
10573
10730
  this.startEvent = null;
10574
10731
  this.lastMoveEvent = null;
10575
10732
  this.lastMoveEventInfo = null;
@@ -10655,7 +10812,7 @@
10655
10812
  this.dragSnapToOrigin = dragSnapToOrigin;
10656
10813
  this.handlers = handlers;
10657
10814
  this.transformPagePoint = transformPagePoint;
10658
- this.distanceThreshold = distanceThreshold;
10815
+ this.distanceThreshold = immediate ? 0 : distanceThreshold;
10659
10816
  this.contextWindow = contextWindow || window;
10660
10817
  const info = extractEventInfo(event);
10661
10818
  const initialInfo = transformPoint(info, this.transformPagePoint);
@@ -10679,6 +10836,10 @@
10679
10836
  if (element) {
10680
10837
  this.startScrollTracking(element);
10681
10838
  }
10839
+ if (immediate) {
10840
+ this.handlePointerMove(event, info);
10841
+ this.updatePoint();
10842
+ }
10682
10843
  }
10683
10844
  /**
10684
10845
  * Start tracking scroll on ancestors and window.
@@ -11054,14 +11215,14 @@
11054
11215
  this.visualElement = visualElement;
11055
11216
  this.readProps = readProps;
11056
11217
  }
11057
- start(originEvent, { snapToCursor = false, distanceThreshold } = {}) {
11218
+ start(originEvent, options = {}) {
11058
11219
  /**
11059
11220
  * Don't start dragging if this component is exiting
11060
11221
  */
11061
11222
  if (this.visualElement.presenceContext?.isPresent === false)
11062
11223
  return;
11063
11224
  const onSessionStart = (event) => {
11064
- if (snapToCursor)
11225
+ if (options.snapToCursor)
11065
11226
  this.snapToCursor(event);
11066
11227
  this.stopAnimation();
11067
11228
  };
@@ -11183,9 +11344,9 @@
11183
11344
  onSessionEnd,
11184
11345
  resumeAnimation,
11185
11346
  }, {
11347
+ ...options,
11186
11348
  transformPagePoint: this.visualElement.getTransformPagePoint(),
11187
11349
  dragSnapToOrigin,
11188
- distanceThreshold,
11189
11350
  contextWindow: this.visualElement.current?.ownerDocument.defaultView,
11190
11351
  element: this.visualElement.current,
11191
11352
  });
@@ -11633,8 +11794,10 @@
11633
11794
  return isElementConstraints(dragConstraints) && !!onMeasureDragConstraints;
11634
11795
  }
11635
11796
 
11636
- 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, }) => ({
11637
11798
  drag: axis || true,
11799
+ _dragX: x || (y && motionValue(0)),
11800
+ _dragY: y || (x && motionValue(0)),
11638
11801
  dragConstraints: constraints,
11639
11802
  dragElastic: elastic,
11640
11803
  dragMomentum: momentum,
@@ -11843,7 +12006,7 @@
11843
12006
  */
11844
12007
  let isKeyPressing = false;
11845
12008
  const handleKeydown = filterEvents(() => {
11846
- if (isPressing.has(element))
12009
+ if (isKeyPressing || isPressing.has(element))
11847
12010
  return;
11848
12011
  isKeyPressing = true;
11849
12012
  firePointerEvent(element, "down");
@@ -11875,6 +12038,18 @@
11875
12038
  return isPrimaryPointer(event) && !isDragActive();
11876
12039
  }
11877
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();
11878
12053
  /**
11879
12054
  * Create a press gesture.
11880
12055
  *
@@ -11892,22 +12067,110 @@
11892
12067
  * to be focusable for this to work, whereas a press gesture will
11893
12068
  * make an element focusable by default.
11894
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
+ *
11895
12081
  * @public
11896
12082
  */
11897
12083
  function press(targetOrSelector, onPressStart, options = {}) {
11898
12084
  onPressStart = bindToScope(onPressStart);
11899
12085
  const [targets, eventOptions, cancelEvents] = setupGesture(targetOrSelector, options);
12086
+ const sequences = new WeakMap();
11900
12087
  const startPress = (startEvent) => {
11901
12088
  const target = startEvent.currentTarget;
11902
12089
  if (!isValidPressEvent(startEvent))
11903
12090
  return;
11904
12091
  if (claimedPointerDownEvents.has(startEvent))
11905
12092
  return;
11906
- isPressing.add(target);
11907
12093
  if (options.stopPropagation) {
11908
12094
  claimedPointerDownEvents.add(startEvent);
11909
12095
  }
11910
- 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
+ };
11911
12174
  /**
11912
12175
  * End listeners run in the capture phase so a descendant calling
11913
12176
  * stopPropagation() in its own pointerup handler can't prevent the
@@ -11918,14 +12181,55 @@
11918
12181
  const onPointerEnd = (endEvent, success) => {
11919
12182
  window.removeEventListener("pointerup", onPointerUp, endEventOptions);
11920
12183
  window.removeEventListener("pointercancel", onPointerCancel, endEventOptions);
11921
- if (isPressing.has(target)) {
11922
- isPressing.delete(target);
11923
- }
11924
- 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))
11925
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);
11926
12225
  }
11927
- if (typeof onPressEnd === "function") {
11928
- 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
+ });
11929
12233
  }
11930
12234
  };
11931
12235
  const onPointerUp = (upEvent) => {
@@ -11937,8 +12241,35 @@
11937
12241
  const onPointerCancel = (cancelEvent) => {
11938
12242
  onPointerEnd(cancelEvent, false);
11939
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
+ };
11940
12257
  window.addEventListener("pointerup", onPointerUp, endEventOptions);
11941
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
+ }
11942
12273
  };
11943
12274
  targets.forEach((target) => {
11944
12275
  const pointerDownTarget = options.useGlobalTarget ? window : target;