motion 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.
@@ -2192,6 +2192,12 @@
2192
2192
  return millisecondsToSeconds(this.currentTime);
2193
2193
  }
2194
2194
  set time(newTime) {
2195
+ /**
2196
+ * Seeking out of "finished" leaves the animation paused, so play()
2197
+ * won't renew finished. Renew it here.
2198
+ */
2199
+ if (this.state === "finished")
2200
+ this.updateFinished();
2195
2201
  newTime = secondsToMilliseconds(newTime);
2196
2202
  this.currentTime = newTime;
2197
2203
  if (this.startTime === null ||
@@ -3077,8 +3083,13 @@
3077
3083
  this.manualStartTime = null;
3078
3084
  this.finishedTime = null;
3079
3085
  this.animation.currentTime = secondsToMilliseconds(newTime);
3086
+ /**
3087
+ * Seeking out of "finished" leaves the animation paused, so play()
3088
+ * won't renew finished. Renew it here.
3089
+ */
3080
3090
  if (wasFinished) {
3081
3091
  this.animation.pause();
3092
+ this.updateFinished();
3082
3093
  }
3083
3094
  }
3084
3095
  /**
@@ -3506,10 +3517,39 @@
3506
3517
  * rebuilding the WAAPI animation every frame.
3507
3518
  */
3508
3519
  const churnWindow = 100;
3520
+ /**
3521
+ * A WAAPI animation that starts moving later than this, as most segments
3522
+ * of a long sequence do, is built this long before it starts, rather than
3523
+ * all at once at the start.
3524
+ */
3525
+ const buildAhead = 100;
3509
3526
  /**
3510
3527
  * Named easings that are the same cubic-bezier in JS and CSS.
3511
3528
  */
3512
3529
  const isNativeEasing = (ease) => /^(linear|ease(In|Out|InOut))$/.test(ease);
3530
+ /**
3531
+ * The last trace of each easing WAAPI can't run into linear(), with its
3532
+ * number of samples, so the elements of a sequence that share an easing
3533
+ * and duration trace it once.
3534
+ */
3535
+ const traced = new WeakMap();
3536
+ const trace = (ease, samples) => {
3537
+ let last = traced.get(ease);
3538
+ if (last?.[0] !== samples) {
3539
+ last = [
3540
+ samples,
3541
+ "linear(" +
3542
+ defaultOffset$1(Array(samples + 1)).map((p) => round(ease(p)) + " " + round(p * 100) + "%") +
3543
+ ")",
3544
+ ];
3545
+ traced.set(ease, last);
3546
+ }
3547
+ return last[1];
3548
+ };
3549
+ /**
3550
+ * How many samples trace an easing between two timestamps.
3551
+ */
3552
+ const countSamples = (from, to) => Math.min(maxSamples, Math.max(2, Math.ceil((to - from) / sampleDelta)));
3513
3553
  /**
3514
3554
  * Whether two eased progresses match. False if either is NaN.
3515
3555
  */
@@ -3668,22 +3708,48 @@
3668
3708
  return this.isAccelerated() ? this.sampleAt(time.now()) : undefined;
3669
3709
  }
3670
3710
  /**
3671
- * If this is a two-keyframe tween whose easing WAAPI runs exactly,
3672
- * when it starts and stops moving, and that easing.
3711
+ * If this moves between two keyframes only, holding still either side
3712
+ * of them, as each value of a sequence segment does, when it starts
3713
+ * and stops moving, and its easing: as a string if WAAPI runs it
3714
+ * exactly, otherwise its function, to trace.
3673
3715
  */
3674
3716
  tween() {
3675
- const { keyframes: keyframes$1, type, ease, times, repeat, delay = 0 } = this.options;
3676
- const easing = isBezierDefinition(ease)
3677
- ? cubicBezierAsString(ease)
3678
- : isNativeEasing(ease) && supportedWaapiEasing[ease];
3679
- if (easing &&
3680
- !repeat &&
3681
- !times &&
3682
- keyframes$1.length === 2 &&
3683
- type === keyframes &&
3684
- this.holdTime === null) {
3685
- const start = this.startTime + delay / this.speed;
3686
- return [start, start + this.resolvedDuration / this.speed, easing];
3717
+ const { keyframes: keyframes$1, type, times, repeat, delay = 0 } = this.options;
3718
+ let span = -1;
3719
+ if (repeat || type !== keyframes || this.holdTime !== null) {
3720
+ return;
3721
+ }
3722
+ for (let i = 1; i < keyframes$1.length; i++) {
3723
+ if (keyframes$1[i] !== keyframes$1[i - 1]) {
3724
+ if (span >= 0)
3725
+ return;
3726
+ span = i - 1;
3727
+ }
3728
+ }
3729
+ let { ease } = this.options;
3730
+ if (isEasingArray(ease))
3731
+ ease = ease[span];
3732
+ const offsets = times || defaultOffset$1(keyframes$1);
3733
+ const at = (i) => this.startTime +
3734
+ (delay + offsets[i] * this.totalDuration) / this.speed;
3735
+ if (span >= 0) {
3736
+ const easing = isBezierDefinition(ease)
3737
+ ? cubicBezierAsString(ease)
3738
+ : isNativeEasing(ease)
3739
+ ? supportedWaapiEasing[ease]
3740
+ : easingDefinitionToFunction(ease);
3741
+ /**
3742
+ * An easing that doesn't run from 0 to 1 doesn't move between
3743
+ * the keyframes, so is traced from the values it gives instead.
3744
+ */
3745
+ return [
3746
+ at(span),
3747
+ at(span + 1),
3748
+ typeof easing === "string" ||
3749
+ (isNear$1(easing(0), 0) && isNear$1(easing(1), 1))
3750
+ ? easing
3751
+ : undefined,
3752
+ ];
3687
3753
  }
3688
3754
  }
3689
3755
  /**
@@ -3727,7 +3793,14 @@
3727
3793
  */
3728
3794
  sync() {
3729
3795
  const now = time.now();
3730
- this.options.motionValue.setWithVelocity(this.sampleAt(now - sampleDelta, true), this.sampleAt(now, true), sampleDelta);
3796
+ const value = this.sampleAt(now, true);
3797
+ /**
3798
+ * A held value doesn't move, so there's no need to sample it
3799
+ * twice.
3800
+ */
3801
+ this.options.motionValue.setWithVelocity(this.holdTime === null
3802
+ ? this.sampleAt(now - sampleDelta, true)
3803
+ : value, value, sampleDelta);
3731
3804
  }
3732
3805
  play() {
3733
3806
  /**
@@ -3741,10 +3814,7 @@
3741
3814
  }
3742
3815
  pause() {
3743
3816
  super.pause();
3744
- if (this.isAccelerated()) {
3745
- this.sync();
3746
- this.group.schedule(false);
3747
- }
3817
+ this.isAccelerated() && this.group.schedule(false);
3748
3818
  }
3749
3819
  /**
3750
3820
  * JSAnimation finishes on its next tick, which only happens once the
@@ -3767,14 +3837,8 @@
3767
3837
  }
3768
3838
  return super.time;
3769
3839
  }
3770
- /**
3771
- * A JS animation sets its motion value from a seek on its next tick.
3772
- * This doesn't tick, so set it now, rather than leave it to be set
3773
- * when this next stops.
3774
- */
3775
3840
  set time(newTime) {
3776
3841
  super.time = newTime;
3777
- this.isAccelerated() && this.sync();
3778
3842
  }
3779
3843
  /**
3780
3844
  * Scroll timelines set time every frame, so run these on the main
@@ -3786,6 +3850,20 @@
3786
3850
  return super.attachTimeline(timeline);
3787
3851
  }
3788
3852
  }
3853
+ /**
3854
+ * Groups that build straight after the current task build in one
3855
+ * microtask, rather than one each.
3856
+ */
3857
+ let builds;
3858
+ const buildSoon = (build) => {
3859
+ builds ||
3860
+ queueMicrotask(() => {
3861
+ const queue = builds;
3862
+ builds = undefined;
3863
+ queue.forEach((build) => build());
3864
+ });
3865
+ (builds || (builds = [])).push(build);
3866
+ };
3789
3867
  function createGroup(owner) {
3790
3868
  const element = owner.current;
3791
3869
  const tracks = new Set();
@@ -3805,6 +3883,7 @@
3805
3883
  let isEnded = false;
3806
3884
  let hasChurned = false;
3807
3885
  let lastChurn = -Infinity;
3886
+ let wake;
3808
3887
  const has = (key) => {
3809
3888
  for (const track of tracks)
3810
3889
  if (track.name === key)
@@ -3872,6 +3951,7 @@
3872
3951
  * transform. The renderer writes its own version on its next render.
3873
3952
  */
3874
3953
  const stop = (shouldCommit = true) => {
3954
+ clearTimeout(wake);
3875
3955
  shouldCommit && commit();
3876
3956
  segments.forEach(({ animation }) => cancel(animation));
3877
3957
  segments = [];
@@ -3925,12 +4005,31 @@
3925
4005
  * two timestamps, the easing that describes it.
3926
4006
  */
3927
4007
  const getEasing = (moving, from, to, bounds) => {
3928
- const samples = Math.min(maxSamples, Math.max(2, Math.ceil((to - from) / sampleDelta)));
4008
+ const samples = countSamples(from, to);
3929
4009
  /**
3930
4010
  * Too many keyframes or repeats to follow with this many samples.
3931
4011
  */
3932
4012
  if (bounds.length * 4 > samples)
3933
4013
  return;
4014
+ /**
4015
+ * First check half way, where values with different easings
4016
+ * usually differ, before sampling every value many times.
4017
+ */
4018
+ let quick;
4019
+ for (const track of moving) {
4020
+ const first = track.numberAt(from);
4021
+ const p = (track.numberAt((from + to) / 2) - first) /
4022
+ (track.numberAt(to) - first);
4023
+ /**
4024
+ * NaN if it doesn't move.
4025
+ */
4026
+ if (p !== p)
4027
+ continue;
4028
+ if (quick === undefined)
4029
+ quick = p;
4030
+ else if (!isNear$1(p, quick))
4031
+ return;
4032
+ }
3934
4033
  const at = spread(from, to, samples).concat(bounds).sort(byTime);
3935
4034
  const position = (t) => progress(from, to, t);
3936
4035
  let shared;
@@ -3987,11 +4086,30 @@
3987
4086
  */
3988
4087
  const sample = (moving, from, to, bounds) => {
3989
4088
  const times = [from];
4089
+ /**
4090
+ * A linear tween is straight between its keyframes, so only the
4091
+ * other values can curve, and a tween only while it moves.
4092
+ */
4093
+ const curved = [];
4094
+ let curveFrom = to;
4095
+ let curveTo = from;
4096
+ for (const track of moving) {
4097
+ const tween = track.tween();
4098
+ if (tween?.[2] !== "linear") {
4099
+ const [trackEnd, cycle] = track.timing();
4100
+ const [a, b] = tween || [from, cycle ? to : trackEnd];
4101
+ curveFrom = Math.min(curveFrom, a);
4102
+ curveTo = Math.max(curveTo, b);
4103
+ curved.push(track);
4104
+ }
4105
+ }
4106
+ curveFrom = Math.max(from, curveFrom);
4107
+ curveTo = Math.min(to, curveTo);
3990
4108
  /**
3991
4109
  * Values that aren't numbers, like calc(), are left to the
3992
4110
  * browser to interpolate.
3993
4111
  */
3994
- const isStraight = (a, b) => moving.every((track) => !(Math.abs(track.numberAt((a + b) / 2) -
4112
+ const isStraight = (a, b) => curved.every((track) => !(Math.abs(track.numberAt((a + b) / 2) -
3995
4113
  (track.numberAt(a) + track.numberAt(b)) / 2) >
3996
4114
  (track.name.startsWith("scale")
3997
4115
  ? scaleTolerance
@@ -4016,10 +4134,13 @@
4016
4134
  }
4017
4135
  };
4018
4136
  /**
4019
- * Split at least every 100ms, and at every keyframe and repeat,
4020
- * either side of where a value can jump.
4137
+ * Split at least every 100ms where a value can curve, and at every
4138
+ * keyframe and repeat, either side of where a value can jump.
4021
4139
  */
4022
- const ends = spread(from, to, Math.ceil((to - from) / 100));
4140
+ const ends = curveTo > curveFrom
4141
+ ? spread(curveFrom, curveTo, Math.ceil((curveTo - curveFrom) / 100))
4142
+ : [];
4143
+ ends.push(to);
4023
4144
  for (const t of bounds)
4024
4145
  ends.push(t, Math.min(t + 0.01, to));
4025
4146
  let start = from;
@@ -4039,12 +4160,20 @@
4039
4160
  for (const track of moving) {
4040
4161
  const tween = track.tween();
4041
4162
  if (!tween ||
4042
- shared?.some((v, i) => i < 2
4043
- ? Math.abs(tween[i] - v) > 1
4044
- : tween[i] !== v)) {
4163
+ (shared &&
4164
+ (Math.abs(tween[0] - shared[0]) > 1 ||
4165
+ Math.abs(tween[1] - shared[1]) > 1))) {
4045
4166
  return;
4046
4167
  }
4047
- shared = tween;
4168
+ /**
4169
+ * Easings that differ, like the springs a sequence makes for
4170
+ * each value, can still be the same curve.
4171
+ */
4172
+ shared = [
4173
+ tween[0],
4174
+ tween[1],
4175
+ !shared || tween[2] === shared[2] ? tween[2] : undefined,
4176
+ ];
4048
4177
  }
4049
4178
  return shared;
4050
4179
  };
@@ -4099,14 +4228,8 @@
4099
4228
  const [trackEnd, trackCycle] = track.timing();
4100
4229
  const isHeld = trackEnd === Infinity;
4101
4230
  built.set(track, [track.at(now), track.speed, isHeld]);
4102
- /**
4103
- * Held animations don't move, so keep their motion values up
4104
- * to date, as a JSAnimation would on a seek.
4105
- */
4106
- if (isHeld) {
4107
- track.sync();
4231
+ if (isHeld)
4108
4232
  continue;
4109
- }
4110
4233
  if (track.speed < 0)
4111
4234
  return;
4112
4235
  if (trackCycle) {
@@ -4146,12 +4269,35 @@
4146
4269
  */
4147
4270
  const tween = !cycle && getTween(moving);
4148
4271
  try {
4149
- if (tween && tween[1] > now) {
4150
- const [from, to, easing] = tween;
4151
- add(run(moving, from, to, easing), from - now, to - from);
4152
- }
4153
- else if (end > now) {
4154
- add(run(moving, now, end), 0, end - now);
4272
+ if (end > now) {
4273
+ let easing;
4274
+ if (tween && tween[1] > now) {
4275
+ const [from, to, ease] = tween;
4276
+ /**
4277
+ * Until it's built, the motion values hold where it
4278
+ * starts, for the renderer to write.
4279
+ */
4280
+ if (from - now > buildAhead) {
4281
+ moving.forEach((track) => track.sync());
4282
+ wake = setTimeout(() => group.schedule(true), from - now - buildAhead);
4283
+ return true;
4284
+ }
4285
+ easing =
4286
+ typeof ease === "string"
4287
+ ? ease
4288
+ : ease
4289
+ ? trace(ease, countSamples(from, to))
4290
+ : getEasing(moving, from, to, []);
4291
+ /**
4292
+ * It holds its last keyframe until the animations end.
4293
+ */
4294
+ easing &&
4295
+ add(run(moving, from, to, easing, {
4296
+ fill: "both",
4297
+ endDelay: end - to,
4298
+ }), from - now, end - from);
4299
+ }
4300
+ easing || add(run(moving, now, end), 0, end - now);
4155
4301
  /**
4156
4302
  * Animations that end sooner finish on the events of
4157
4303
  * empty animations that end with them.
@@ -4200,9 +4346,14 @@
4200
4346
  const [builtTime, builtSpeed, wasHeld] = state;
4201
4347
  const trackTime = track.at(now);
4202
4348
  const isHeld = track.timing()[0] === Infinity;
4349
+ /**
4350
+ * With nothing on the compositor, a held animation is set
4351
+ * where it's held, so it can be seeked as often as needed.
4352
+ */
4203
4353
  if (wasHeld) {
4204
- if (isHeld && trackTime === builtTime)
4354
+ if (isHeld && (trackTime === builtTime || !segments.length)) {
4205
4355
  continue;
4356
+ }
4206
4357
  return false;
4207
4358
  }
4208
4359
  const trackElapsed = (trackTime - builtTime) / builtSpeed;
@@ -4332,6 +4483,12 @@
4332
4483
  if (!animate(now))
4333
4484
  return group.demote();
4334
4485
  }
4486
+ /**
4487
+ * Held animations don't move, so set their motion values to where
4488
+ * they're held, as a JSAnimation would on its next tick. Pausing
4489
+ * and seeking schedule this, so it's once however many there are.
4490
+ */
4491
+ tracks.forEach((track) => track.timing()[0] === Infinity && track.sync());
4335
4492
  };
4336
4493
  const group = {
4337
4494
  tracks,
@@ -4351,7 +4508,7 @@
4351
4508
  isScheduled = true;
4352
4509
  frameData.isProcessing
4353
4510
  ? frame.render(build, false, true)
4354
- : queueMicrotask(build);
4511
+ : buildSoon(build);
4355
4512
  }
4356
4513
  },
4357
4514
  /**
@@ -10880,7 +11037,7 @@
10880
11037
 
10881
11038
  const overflowStyles = /*#__PURE__*/ new Set(["auto", "scroll"]);
10882
11039
  class PanSession {
10883
- constructor(event, handlers, { transformPagePoint, contextWindow = window, dragSnapToOrigin = false, distanceThreshold = 3, element, } = {}) {
11040
+ constructor(event, handlers, { transformPagePoint, contextWindow = window, dragSnapToOrigin = false, distanceThreshold = 3, element, immediate, } = {}) {
10884
11041
  this.startEvent = null;
10885
11042
  this.lastMoveEvent = null;
10886
11043
  this.lastMoveEventInfo = null;
@@ -10966,7 +11123,7 @@
10966
11123
  this.dragSnapToOrigin = dragSnapToOrigin;
10967
11124
  this.handlers = handlers;
10968
11125
  this.transformPagePoint = transformPagePoint;
10969
- this.distanceThreshold = distanceThreshold;
11126
+ this.distanceThreshold = immediate ? 0 : distanceThreshold;
10970
11127
  this.contextWindow = contextWindow || window;
10971
11128
  const info = extractEventInfo(event);
10972
11129
  const initialInfo = transformPoint(info, this.transformPagePoint);
@@ -10990,6 +11147,10 @@
10990
11147
  if (element) {
10991
11148
  this.startScrollTracking(element);
10992
11149
  }
11150
+ if (immediate) {
11151
+ this.handlePointerMove(event, info);
11152
+ this.updatePoint();
11153
+ }
10993
11154
  }
10994
11155
  /**
10995
11156
  * Start tracking scroll on ancestors and window.
@@ -11365,14 +11526,14 @@
11365
11526
  this.visualElement = visualElement;
11366
11527
  this.readProps = readProps;
11367
11528
  }
11368
- start(originEvent, { snapToCursor = false, distanceThreshold } = {}) {
11529
+ start(originEvent, options = {}) {
11369
11530
  /**
11370
11531
  * Don't start dragging if this component is exiting
11371
11532
  */
11372
11533
  if (this.visualElement.presenceContext?.isPresent === false)
11373
11534
  return;
11374
11535
  const onSessionStart = (event) => {
11375
- if (snapToCursor)
11536
+ if (options.snapToCursor)
11376
11537
  this.snapToCursor(event);
11377
11538
  this.stopAnimation();
11378
11539
  };
@@ -11494,9 +11655,9 @@
11494
11655
  onSessionEnd,
11495
11656
  resumeAnimation,
11496
11657
  }, {
11658
+ ...options,
11497
11659
  transformPagePoint: this.visualElement.getTransformPagePoint(),
11498
11660
  dragSnapToOrigin,
11499
- distanceThreshold,
11500
11661
  contextWindow: this.visualElement.current?.ownerDocument.defaultView,
11501
11662
  element: this.visualElement.current,
11502
11663
  });
@@ -11944,8 +12105,10 @@
11944
12105
  return isElementConstraints(dragConstraints) && !!onMeasureDragConstraints;
11945
12106
  }
11946
12107
 
11947
- const toProps = ({ axis, constraints, elastic, momentum, transition, snapToOrigin, directionLock, propagation, onStart, onMove, onEnd, onDirectionLock, onTransitionEnd, onMeasureConstraints, }) => ({
12108
+ const toProps = ({ axis, constraints, elastic, momentum, transition, snapToOrigin, directionLock, propagation, onStart, onMove, onEnd, onDirectionLock, onTransitionEnd, onMeasureConstraints, x, y, }) => ({
11948
12109
  drag: axis || true,
12110
+ _dragX: x || (y && motionValue(0)),
12111
+ _dragY: y || (x && motionValue(0)),
11949
12112
  dragConstraints: constraints,
11950
12113
  dragElastic: elastic,
11951
12114
  dragMomentum: momentum,
@@ -12154,7 +12317,7 @@
12154
12317
  */
12155
12318
  let isKeyPressing = false;
12156
12319
  const handleKeydown = filterEvents(() => {
12157
- if (isPressing.has(element))
12320
+ if (isKeyPressing || isPressing.has(element))
12158
12321
  return;
12159
12322
  isKeyPressing = true;
12160
12323
  firePointerEvent(element, "down");
@@ -12186,6 +12349,18 @@
12186
12349
  return isPrimaryPointer(event) && !isDragActive();
12187
12350
  }
12188
12351
  const claimedPointerDownEvents = new WeakSet();
12352
+ /**
12353
+ * Max distance in px between consecutive presses of a `count` sequence.
12354
+ * Android's `ViewConfiguration` double tap slop is 100dp.
12355
+ */
12356
+ const sequenceSlop = 100;
12357
+ /**
12358
+ * Max distance in px a pointer can move during a held press (`hold`
12359
+ * pending, or `repeat`). Android's `ViewConfiguration` touch slop is 8dp.
12360
+ */
12361
+ const holdSlop = 8;
12362
+ const exceeds = (a, b, max) => Math.hypot(a.clientX - b.clientX, a.clientY - b.clientY) > max;
12363
+ const preventDefault = (event) => event.preventDefault();
12189
12364
  /**
12190
12365
  * Create a press gesture.
12191
12366
  *
@@ -12203,22 +12378,110 @@
12203
12378
  * to be focusable for this to work, whereas a press gesture will
12204
12379
  * make an element focusable by default.
12205
12380
  *
12381
+ * `onPressStart` fires on the first pointer or key down, and can return
12382
+ * a function to call when the press ends, or an object of handlers for
12383
+ * this press:
12384
+ *
12385
+ * ```js
12386
+ * press(button, () => ({
12387
+ * onHold: (progress) => {},
12388
+ * onComplete: (event, { success }) => {},
12389
+ * }), { hold: 0.5 })
12390
+ * ```
12391
+ *
12206
12392
  * @public
12207
12393
  */
12208
12394
  function press(targetOrSelector, onPressStart, options = {}) {
12209
12395
  onPressStart = bindToScope(onPressStart);
12210
12396
  const [targets, eventOptions, cancelEvents] = setupGesture(targetOrSelector, options);
12397
+ const sequences = new WeakMap();
12211
12398
  const startPress = (startEvent) => {
12212
12399
  const target = startEvent.currentTarget;
12213
12400
  if (!isValidPressEvent(startEvent))
12214
12401
  return;
12215
12402
  if (claimedPointerDownEvents.has(startEvent))
12216
12403
  return;
12217
- isPressing.add(target);
12218
12404
  if (options.stopPropagation) {
12219
12405
  claimedPointerDownEvents.add(startEvent);
12220
12406
  }
12221
- const onPressEnd = onPressStart(target, startEvent);
12407
+ /**
12408
+ * Options are read per press so they can be updated between presses.
12409
+ */
12410
+ const { count: presses = 1, countTimeout = 0.3, hold = 0, repeat, } = options;
12411
+ const downTime = time.now();
12412
+ const holdTime = secondsToMilliseconds(hold);
12413
+ const timeout = secondsToMilliseconds(countTimeout);
12414
+ let previous = sequences.get(target);
12415
+ /**
12416
+ * A press during a `count` sequence continues it. If it's too late
12417
+ * or too far away, the sequence is cancelled and this press starts
12418
+ * a new one.
12419
+ */
12420
+ if (previous?.expire(exceeds(previous.event, startEvent, sequenceSlop))) {
12421
+ previous = undefined;
12422
+ }
12423
+ else if (previous) {
12424
+ cancelFrame(previous.expire);
12425
+ sequences.delete(target);
12426
+ }
12427
+ const count = previous ? previous.count + 1 : 1;
12428
+ const isFinal = count >= presses;
12429
+ const isHeld = isFinal && (hold || repeat);
12430
+ let isActive = !hold;
12431
+ let repeats = 0;
12432
+ let handlers;
12433
+ isPressing.add(target);
12434
+ if (previous) {
12435
+ handlers = previous.handlers;
12436
+ }
12437
+ else {
12438
+ const result = onPressStart(target, startEvent);
12439
+ handlers =
12440
+ typeof result === "function"
12441
+ ? { onComplete: result }
12442
+ : result || {};
12443
+ }
12444
+ isFinal && isActive && handlers.onActive?.();
12445
+ /**
12446
+ * While held, `onHold` reports progress from 0 to 1 over `hold`,
12447
+ * then over each `repeat`. Each cycle reports exactly 1 at its end,
12448
+ * then 0 on the next frame.
12449
+ */
12450
+ const repeatTime = secondsToMilliseconds(repeat);
12451
+ let length = holdTime || repeatTime;
12452
+ let cycleEnd = downTime + length;
12453
+ let isCycleStart = true;
12454
+ const tick = () => {
12455
+ if (eventOptions.signal.aborted)
12456
+ return cancelFrame(tick);
12457
+ let progress = Math.min(1 - (cycleEnd - time.now()) / length, 1);
12458
+ /**
12459
+ * The frame after a boundary reports 0, so each cycle visibly
12460
+ * restarts. Boundaries are timed from `downTime`, so this
12461
+ * doesn't delay the next one. If a long frame has already
12462
+ * passed the next boundary, it reports 1 instead.
12463
+ */
12464
+ if (isCycleStart && progress < 1)
12465
+ progress = 0;
12466
+ isCycleStart = false;
12467
+ handlers.onHold?.(progress);
12468
+ if (progress < 1)
12469
+ return;
12470
+ if (isActive) {
12471
+ handlers.onRepeat?.(++repeats);
12472
+ }
12473
+ else {
12474
+ isActive = true;
12475
+ handlers.onActive?.();
12476
+ }
12477
+ if (repeat) {
12478
+ isCycleStart = true;
12479
+ cycleEnd += length = repeatTime;
12480
+ }
12481
+ else {
12482
+ cancelFrame(tick);
12483
+ }
12484
+ };
12222
12485
  /**
12223
12486
  * End listeners run in the capture phase so a descendant calling
12224
12487
  * stopPropagation() in its own pointerup handler can't prevent the
@@ -12229,14 +12492,55 @@
12229
12492
  const onPointerEnd = (endEvent, success) => {
12230
12493
  window.removeEventListener("pointerup", onPointerUp, endEventOptions);
12231
12494
  window.removeEventListener("pointercancel", onPointerCancel, endEventOptions);
12232
- if (isPressing.has(target)) {
12233
- isPressing.delete(target);
12234
- }
12235
- if (!isValidPressEvent(endEvent)) {
12495
+ window.removeEventListener("pointermove", onPointerMove, endEventOptions);
12496
+ target.removeEventListener("contextmenu", preventDefault);
12497
+ cancelFrame(tick);
12498
+ isPressing.delete(target);
12499
+ if (!isPrimaryPointer(endEvent))
12236
12500
  return;
12501
+ /**
12502
+ * A press that ends while a drag is active is a cancel.
12503
+ */
12504
+ if (isDragActive())
12505
+ success = false;
12506
+ if (success && !isFinal) {
12507
+ /**
12508
+ * Wait for the next press of the sequence, and cancel the
12509
+ * gesture if it doesn't arrive in time.
12510
+ */
12511
+ const end = time.now();
12512
+ /**
12513
+ * Cancels the sequence if it's `force`d, or the interval
12514
+ * has passed. Runs every frame, where `force` is frame
12515
+ * data, so it's checked with `=== true`.
12516
+ */
12517
+ const expire = (force) => {
12518
+ const isAborted = eventOptions.signal.aborted;
12519
+ if (isAborted ||
12520
+ force === true ||
12521
+ time.now() - end > timeout) {
12522
+ cancelFrame(expire);
12523
+ sequences.delete(target);
12524
+ isAborted ||
12525
+ handlers.onComplete?.(endEvent, { success: false });
12526
+ return true;
12527
+ }
12528
+ };
12529
+ sequences.set(target, {
12530
+ count,
12531
+ event: startEvent,
12532
+ handlers,
12533
+ expire,
12534
+ });
12535
+ frame.update(expire, true);
12237
12536
  }
12238
- if (typeof onPressEnd === "function") {
12239
- onPressEnd(endEvent, { success });
12537
+ else {
12538
+ /**
12539
+ * Releasing before `hold` is reached is a cancel.
12540
+ */
12541
+ handlers.onComplete?.(endEvent, {
12542
+ success: success && isActive,
12543
+ });
12240
12544
  }
12241
12545
  };
12242
12546
  const onPointerUp = (upEvent) => {
@@ -12248,8 +12552,35 @@
12248
12552
  const onPointerCancel = (cancelEvent) => {
12249
12553
  onPointerEnd(cancelEvent, false);
12250
12554
  };
12555
+ /**
12556
+ * Moving during a pending long press, or a repeating press, cancels
12557
+ * it, so scrolling on touch devices doesn't trigger it. A drag
12558
+ * starting during a `count` sequence cancels it.
12559
+ */
12560
+ const onPointerMove = (moveEvent) => {
12561
+ if (isPrimaryPointer(moveEvent) &&
12562
+ (((repeat || !isActive) &&
12563
+ exceeds(startEvent, moveEvent, holdSlop)) ||
12564
+ (presses > 1 && isDragActive()))) {
12565
+ onPointerCancel(moveEvent);
12566
+ }
12567
+ };
12251
12568
  window.addEventListener("pointerup", onPointerUp, endEventOptions);
12252
12569
  window.addEventListener("pointercancel", onPointerCancel, endEventOptions);
12570
+ if (isHeld || presses > 1) {
12571
+ window.addEventListener("pointermove", onPointerMove, endEventOptions);
12572
+ }
12573
+ if (isHeld) {
12574
+ frame.update(tick, true);
12575
+ /**
12576
+ * Touch devices open a context menu or callout on long press,
12577
+ * which would cancel the gesture.
12578
+ */
12579
+ target.addEventListener("contextmenu", preventDefault, {
12580
+ ...eventOptions,
12581
+ passive: false,
12582
+ });
12583
+ }
12253
12584
  };
12254
12585
  targets.forEach((target) => {
12255
12586
  const pointerDownTarget = options.useGlobalTarget ? window : target;
@@ -15185,13 +15516,45 @@
15185
15516
  return segment;
15186
15517
  });
15187
15518
  const animationDefinitions = createAnimationsFromSequence(processedSequence, options, scope, { spring }, info);
15519
+ /**
15520
+ * Transitions are made per value, but reduceMotion is read from the
15521
+ * top level. animate() resolves it into options, and the default
15522
+ * transition covers timelines made directly.
15523
+ */
15524
+ const reduceMotion = options?.reduceMotion ??
15525
+ options?.defaultTransition?.reduceMotion;
15188
15526
  animationDefinitions.forEach(({ keyframes, transition }, subject) => {
15189
- animations.push(...animateSubject(subject, keyframes, transition));
15527
+ animations.push(...animateSubject(subject, keyframes, {
15528
+ ...transition,
15529
+ reduceMotion,
15530
+ }));
15190
15531
  });
15191
15532
  return animations;
15192
15533
  }
15193
15534
 
15194
15535
  let markerId = 0;
15536
+ /**
15537
+ * Split a time across repeats into its repeat and the playhead position in
15538
+ * the sequence. A time on a repeat boundary is the end of the repeat
15539
+ * before it, as it is for the clock's value. With `index`, join a position
15540
+ * in that repeat back into a time. Mirrored and reversed repeats play
15541
+ * every other repeat backwards.
15542
+ */
15543
+ function inRepeat({ repeat = 0, repeatDelay = 0, repeatType = "loop", delay = 0, }, duration, time, index) {
15544
+ const cycle = duration + repeatDelay;
15545
+ const isSplit = index === undefined;
15546
+ if (isSplit) {
15547
+ const t = Math.max(0, time - delay);
15548
+ index =
15549
+ cycle && t > 0
15550
+ ? Math.min(repeat, Math.ceil(t / cycle - 1e-9) - 1)
15551
+ : 0;
15552
+ time = Math.min(t - index * cycle, duration);
15553
+ }
15554
+ if (repeatType !== "loop" && index % 2)
15555
+ time = duration - time;
15556
+ return [index, isSplit ? time : delay + index * cycle + time];
15557
+ }
15195
15558
  /**
15196
15559
  * Copy a segment with a new `at`, adding a transition if it has none.
15197
15560
  */
@@ -15279,15 +15642,6 @@
15279
15642
  this.invalidate();
15280
15643
  this.flush();
15281
15644
  }
15282
- /**
15283
- * Register with the scope (for instance from useAnimate()) so it
15284
- * stops when the scope is torn down.
15285
- */
15286
- const { scope } = options;
15287
- if (scope) {
15288
- scope.animations.push(this);
15289
- this.finished.then(() => removeItem(scope.animations, this));
15290
- }
15291
15645
  }
15292
15646
  /**
15293
15647
  * Add a segment, label or timeline. `at` overrides any `at` already
@@ -15362,10 +15716,18 @@
15362
15716
  * Play forwards or backwards to a time or label, then pause there.
15363
15717
  */
15364
15718
  playTo(position) {
15365
- const target = this.resolve(position);
15719
+ const stopAt = this.resolve(position);
15720
+ const duration = this.iterationDuration;
15721
+ /**
15722
+ * A repeating timeline plays to the position in the repeat it's
15723
+ * already in, rather than all the way back to the first.
15724
+ */
15725
+ const time = this.time;
15726
+ const [index] = inRepeat(this.options, duration, time);
15727
+ const target = inRepeat(this.options, duration, stopAt, index)[1];
15366
15728
  const speed = Math.abs(this.speed);
15367
- this.speed = target < this.time ? -speed : speed;
15368
- this.stopAt = target;
15729
+ this.speed = target < time ? -speed : speed;
15730
+ this.stopAt = stopAt;
15369
15731
  this.clock || this.invalidate();
15370
15732
  this.play();
15371
15733
  return this;
@@ -15493,6 +15855,9 @@
15493
15855
  this.parent = parent;
15494
15856
  }
15495
15857
  build() {
15858
+ const prevDuration = this.isWrapped
15859
+ ? this.group.iterationDuration
15860
+ : this.totalDuration;
15496
15861
  this.isDirty = false;
15497
15862
  this.isWrapped = false;
15498
15863
  const prev = this.group;
@@ -15577,9 +15942,19 @@
15577
15942
  })));
15578
15943
  }
15579
15944
  const group = (this.group = new GroupAnimationWithThen(animations));
15580
- time = Math.min(time, duration);
15581
- if (this.isDone && time < duration)
15945
+ /**
15946
+ * Keep the playhead in the same repeat and at the same position,
15947
+ * so changing a repeating timeline doesn't jump it.
15948
+ */
15949
+ const { delay = 0, repeat = 0, repeatDelay = 0 } = this.options;
15950
+ const [index, position] = inRepeat(this.options, prevDuration, time);
15951
+ if (time > delay) {
15952
+ time = inRepeat(this.options, duration, Math.min(position, duration), index)[1];
15953
+ }
15954
+ if (this.isDone &&
15955
+ time < delay + (repeat + 1) * (duration + repeatDelay) - repeatDelay) {
15582
15956
  this.newFinished();
15957
+ }
15583
15958
  if (this.scrollTimeline) {
15584
15959
  this.detachScroll = group.attachTimeline(this.scrollTimeline);
15585
15960
  }
@@ -15626,12 +16001,7 @@
15626
16001
  * The repeat the clock is in, from its time across all repeats.
15627
16002
  */
15628
16003
  currentRepeat() {
15629
- const { repeat = 0, repeatDelay = 0, delay = 0 } = this.options;
15630
- const cycle = this.totalDuration + repeatDelay;
15631
- const time = (this.clockAnimation?.time ?? 0) - delay;
15632
- return cycle && time > 0
15633
- ? Math.min(repeat, Math.floor(time / cycle))
15634
- : 0;
16004
+ return inRepeat(this.options, this.totalDuration, this.clockAnimation?.time ?? 0)[0];
15635
16005
  }
15636
16006
  onClock(time) {
15637
16007
  let prevTime = this.prevTime;
@@ -15640,22 +16010,23 @@
15640
16010
  return;
15641
16011
  const { onUpdate, onRepeat, repeatType = "loop" } = this.options;
15642
16012
  onUpdate?.(time);
16013
+ /**
16014
+ * A frame can cross any number of repeats. Finish crossing each
16015
+ * repeat it leaves, then cross the next from its edge. A loop
16016
+ * starts each repeat from the same edge. The others turn around.
16017
+ */
15643
16018
  const repeatIndex = this.currentRepeat();
15644
- if (repeatIndex !== this.repeatIndex) {
15645
- this.repeatIndex = repeatIndex;
16019
+ const step = repeatIndex > this.repeatIndex ? 1 : -1;
16020
+ while (repeatIndex !== this.repeatIndex) {
16021
+ const isForward = (this.speed > 0) ===
16022
+ (repeatType === "loop" || !(this.repeatIndex % 2));
16023
+ const edge = isForward ? this.totalDuration : 0;
16024
+ if (this.cross(prevTime, edge))
16025
+ return;
16026
+ this.repeatIndex += step;
15646
16027
  onRepeat?.();
15647
- /**
15648
- * A loop jumps the playhead back to the start, or to the
15649
- * end when playing backwards. Finish crossing the repeat
15650
- * that ended, then cross the new one from its edge.
15651
- */
15652
- if (repeatType === "loop") {
15653
- const isForward = this.speed > 0;
15654
- if (this.cross(prevTime, isForward ? this.totalDuration : 0)) {
15655
- return;
15656
- }
15657
- prevTime = isForward ? -1 : Infinity;
15658
- }
16028
+ prevTime =
16029
+ repeatType === "loop" ? (isForward ? -1 : Infinity) : edge;
15659
16030
  }
15660
16031
  this.cross(prevTime, time);
15661
16032
  }
@@ -15682,9 +16053,12 @@
15682
16053
  return true;
15683
16054
  }
15684
16055
  }
15685
- pauseAt(time) {
16056
+ /**
16057
+ * Pause at a playhead position in the current repeat.
16058
+ */
16059
+ pauseAt(position) {
15686
16060
  this.pause();
15687
- this.time = time;
16061
+ this.time = inRepeat(this.options, this.totalDuration, position, this.repeatIndex)[1];
15688
16062
  return true;
15689
16063
  }
15690
16064
  watch() {
@@ -15699,8 +16073,13 @@
15699
16073
  * direction, as it does for a single animation.
15700
16074
  */
15701
16075
  const callback = (this.speed < 0 && onReverseComplete) || onComplete;
16076
+ /**
16077
+ * Resolve this run's promise, even if the callback plays the
16078
+ * timeline again and so makes a new one.
16079
+ */
16080
+ const resolve = this.resolveFinished;
15702
16081
  callback?.();
15703
- this.resolveFinished();
16082
+ resolve();
15704
16083
  });
15705
16084
  }
15706
16085
  newFinished() {
@@ -15709,14 +16088,20 @@
15709
16088
  this.resolveFinished = resolve;
15710
16089
  });
15711
16090
  /**
15712
- * Stay in the scope while playing, including after a change
15713
- * restarts a finished timeline.
16091
+ * Stay in the scopeMotion() scope this was created in, and the
16092
+ * useAnimate() scope, while playing. Each replay, including a
16093
+ * change that restarts a finished timeline, registers again.
15714
16094
  */
16095
+ const { scope } = this.options;
15715
16096
  const remove = this.motionScope?.add(() => {
15716
16097
  this.isKilled = true;
15717
16098
  this.stop();
15718
16099
  });
15719
- remove && this.finished.then(remove);
16100
+ scope?.animations.push(this);
16101
+ this.finished.then(() => {
16102
+ remove?.();
16103
+ scope && removeItem(scope.animations, this);
16104
+ });
15720
16105
  }
15721
16106
  }
15722
16107
 
@@ -15752,6 +16137,16 @@
15752
16137
  ...sequenceOptions.defaultTransition,
15753
16138
  };
15754
16139
  }
16140
+ /**
16141
+ * Resolve reduceMotion in the same order as a single
16142
+ * animation: options, then the default transition (which
16143
+ * includes the scope's), then useAnimate()'s.
16144
+ */
16145
+ sequenceOptions.reduceMotion =
16146
+ optionsOrKeyframes?.reduceMotion ??
16147
+ sequenceOptions.defaultTransition
16148
+ ?.reduceMotion ??
16149
+ reduceMotion;
15755
16150
  timeline = new Timeline(subjectOrSequence, sequenceOptions);
15756
16151
  }
15757
16152
  else {