motion 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.
@@ -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 ||
@@ -2976,7 +2982,8 @@
2976
2982
  if (transition.autoplay === false) {
2977
2983
  this.animation.pause();
2978
2984
  }
2979
- this.animation.onfinish = () => {
2985
+ this.animation.onfinish = this.onFinish = () => {
2986
+ this.animation.onfinish = null;
2980
2987
  this.finishedTime = this.time;
2981
2988
  if (!pseudoElement) {
2982
2989
  const keyframe = getFinalKeyframe(keyframes, this.options, finalKeyframe, this.speed);
@@ -3000,6 +3007,7 @@
3000
3007
  if (this.isStopped)
3001
3008
  return;
3002
3009
  this.manualStartTime = null;
3010
+ this.animation.onfinish = this.onFinish;
3003
3011
  this.animation.play();
3004
3012
  if (this.state === "finished") {
3005
3013
  this.updateFinished();
@@ -3009,9 +3017,12 @@
3009
3017
  this.animation.pause();
3010
3018
  }
3011
3019
  complete() {
3020
+ if (!this.isStopped)
3021
+ this.animation.onfinish = this.onFinish;
3012
3022
  this.animation.finish?.();
3013
3023
  }
3014
3024
  cancel() {
3025
+ this.animation.onfinish = null;
3015
3026
  try {
3016
3027
  this.animation.cancel();
3017
3028
  }
@@ -3035,10 +3046,8 @@
3035
3046
  else {
3036
3047
  this.commitStyles();
3037
3048
  }
3038
- if (!this.isPseudoElement) {
3039
- this.animation.onfinish = null;
3049
+ if (!this.isPseudoElement)
3040
3050
  this.cancel();
3041
- }
3042
3051
  }
3043
3052
  /**
3044
3053
  * WAAPI doesn't natively have any interruption capabilities.
@@ -3074,8 +3083,13 @@
3074
3083
  this.manualStartTime = null;
3075
3084
  this.finishedTime = null;
3076
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
+ */
3077
3090
  if (wasFinished) {
3078
3091
  this.animation.pause();
3092
+ this.updateFinished();
3079
3093
  }
3080
3094
  }
3081
3095
  /**
@@ -3109,7 +3123,7 @@
3109
3123
  if (this.allowFlatten) {
3110
3124
  this.animation.effect?.updateTiming({ easing: "linear" });
3111
3125
  }
3112
- this.animation.onfinish = null;
3126
+ this.animation.onfinish = this.onFinish = null;
3113
3127
  if (timeline && supportsScrollTimeline()) {
3114
3128
  this.animation.timeline = timeline;
3115
3129
  onAttach?.(this.animation);
@@ -3503,10 +3517,39 @@
3503
3517
  * rebuilding the WAAPI animation every frame.
3504
3518
  */
3505
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;
3506
3526
  /**
3507
3527
  * Named easings that are the same cubic-bezier in JS and CSS.
3508
3528
  */
3509
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)));
3510
3553
  /**
3511
3554
  * Whether two eased progresses match. False if either is NaN.
3512
3555
  */
@@ -3665,22 +3708,48 @@
3665
3708
  return this.isAccelerated() ? this.sampleAt(time.now()) : undefined;
3666
3709
  }
3667
3710
  /**
3668
- * If this is a two-keyframe tween whose easing WAAPI runs exactly,
3669
- * 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.
3670
3715
  */
3671
3716
  tween() {
3672
- const { keyframes: keyframes$1, type, ease, times, repeat, delay = 0 } = this.options;
3673
- const easing = isBezierDefinition(ease)
3674
- ? cubicBezierAsString(ease)
3675
- : isNativeEasing(ease) && supportedWaapiEasing[ease];
3676
- if (easing &&
3677
- !repeat &&
3678
- !times &&
3679
- keyframes$1.length === 2 &&
3680
- type === keyframes &&
3681
- this.holdTime === null) {
3682
- const start = this.startTime + delay / this.speed;
3683
- 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
+ ];
3684
3753
  }
3685
3754
  }
3686
3755
  /**
@@ -3724,7 +3793,14 @@
3724
3793
  */
3725
3794
  sync() {
3726
3795
  const now = time.now();
3727
- 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);
3728
3804
  }
3729
3805
  play() {
3730
3806
  /**
@@ -3738,19 +3814,18 @@
3738
3814
  }
3739
3815
  pause() {
3740
3816
  super.pause();
3741
- if (this.isAccelerated()) {
3742
- this.sync();
3743
- this.group.schedule(false);
3744
- }
3817
+ this.isAccelerated() && this.group.schedule(false);
3745
3818
  }
3746
3819
  /**
3747
- * JSAnimation finishes on its next tick, which only happens on the
3748
- * main thread, so finish now.
3820
+ * JSAnimation finishes on its next tick, which only happens once the
3821
+ * group is on the main thread, so finish now. That includes an
3822
+ * animation that can't be accelerated but reached its end before the
3823
+ * group could move there.
3749
3824
  */
3750
3825
  complete() {
3751
3826
  super.complete();
3752
3827
  const { group } = this;
3753
- group && !group.isJS && this.canAccelerate && this.tick(time.now());
3828
+ group && !group.isJS && this.tick(time.now());
3754
3829
  }
3755
3830
  /**
3756
3831
  * Without a tick to rebase it, a held time still includes the delay,
@@ -3762,14 +3837,8 @@
3762
3837
  }
3763
3838
  return super.time;
3764
3839
  }
3765
- /**
3766
- * A JS animation sets its motion value from a seek on its next tick.
3767
- * This doesn't tick, so set it now, rather than leave it to be set
3768
- * when this next stops.
3769
- */
3770
3840
  set time(newTime) {
3771
3841
  super.time = newTime;
3772
- this.isAccelerated() && this.sync();
3773
3842
  }
3774
3843
  /**
3775
3844
  * Scroll timelines set time every frame, so run these on the main
@@ -3781,6 +3850,20 @@
3781
3850
  return super.attachTimeline(timeline);
3782
3851
  }
3783
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
+ };
3784
3867
  function createGroup(owner) {
3785
3868
  const element = owner.current;
3786
3869
  const tracks = new Set();
@@ -3800,6 +3883,7 @@
3800
3883
  let isEnded = false;
3801
3884
  let hasChurned = false;
3802
3885
  let lastChurn = -Infinity;
3886
+ let wake;
3803
3887
  const has = (key) => {
3804
3888
  for (const track of tracks)
3805
3889
  if (track.name === key)
@@ -3853,13 +3937,23 @@
3853
3937
  const commit = (values = valuesAt()) => {
3854
3938
  element.style.transform = toTransform(values, true);
3855
3939
  };
3940
+ /**
3941
+ * Cancel a WAAPI animation and remove its handlers, as WebKit never
3942
+ * collects an animation once it has run, nor anything its handlers
3943
+ * reference (#4034).
3944
+ */
3945
+ const cancel = (animation) => {
3946
+ animation.onfinish = animation.oncancel = null;
3947
+ animation.cancel();
3948
+ };
3856
3949
  /**
3857
3950
  * Cancel the WAAPI animations, by default first committing the
3858
3951
  * transform. The renderer writes its own version on its next render.
3859
3952
  */
3860
3953
  const stop = (shouldCommit = true) => {
3954
+ clearTimeout(wake);
3861
3955
  shouldCommit && commit();
3862
- segments.forEach(({ animation }) => animation.cancel());
3956
+ segments.forEach(({ animation }) => cancel(animation));
3863
3957
  segments = [];
3864
3958
  };
3865
3959
  /**
@@ -3880,7 +3974,7 @@
3880
3974
  animation.onfinish = () => {
3881
3975
  segment.isDone = true;
3882
3976
  if (segments.some((s) => s.end === Infinity)) {
3883
- animation.cancel();
3977
+ cancel(animation);
3884
3978
  segments = segments.filter((s) => s !== segment);
3885
3979
  }
3886
3980
  group.schedule(false);
@@ -3911,12 +4005,31 @@
3911
4005
  * two timestamps, the easing that describes it.
3912
4006
  */
3913
4007
  const getEasing = (moving, from, to, bounds) => {
3914
- const samples = Math.min(maxSamples, Math.max(2, Math.ceil((to - from) / sampleDelta)));
4008
+ const samples = countSamples(from, to);
3915
4009
  /**
3916
4010
  * Too many keyframes or repeats to follow with this many samples.
3917
4011
  */
3918
4012
  if (bounds.length * 4 > samples)
3919
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
+ }
3920
4033
  const at = spread(from, to, samples).concat(bounds).sort(byTime);
3921
4034
  const position = (t) => progress(from, to, t);
3922
4035
  let shared;
@@ -3973,11 +4086,30 @@
3973
4086
  */
3974
4087
  const sample = (moving, from, to, bounds) => {
3975
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);
3976
4108
  /**
3977
4109
  * Values that aren't numbers, like calc(), are left to the
3978
4110
  * browser to interpolate.
3979
4111
  */
3980
- 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) -
3981
4113
  (track.numberAt(a) + track.numberAt(b)) / 2) >
3982
4114
  (track.name.startsWith("scale")
3983
4115
  ? scaleTolerance
@@ -4002,10 +4134,13 @@
4002
4134
  }
4003
4135
  };
4004
4136
  /**
4005
- * Split at least every 100ms, and at every keyframe and repeat,
4006
- * 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.
4007
4139
  */
4008
- 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);
4009
4144
  for (const t of bounds)
4010
4145
  ends.push(t, Math.min(t + 0.01, to));
4011
4146
  let start = from;
@@ -4025,12 +4160,20 @@
4025
4160
  for (const track of moving) {
4026
4161
  const tween = track.tween();
4027
4162
  if (!tween ||
4028
- shared?.some((v, i) => i < 2
4029
- ? Math.abs(tween[i] - v) > 1
4030
- : tween[i] !== v)) {
4163
+ (shared &&
4164
+ (Math.abs(tween[0] - shared[0]) > 1 ||
4165
+ Math.abs(tween[1] - shared[1]) > 1))) {
4031
4166
  return;
4032
4167
  }
4033
- 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
+ ];
4034
4177
  }
4035
4178
  return shared;
4036
4179
  };
@@ -4085,14 +4228,8 @@
4085
4228
  const [trackEnd, trackCycle] = track.timing();
4086
4229
  const isHeld = trackEnd === Infinity;
4087
4230
  built.set(track, [track.at(now), track.speed, isHeld]);
4088
- /**
4089
- * Held animations don't move, so keep their motion values up
4090
- * to date, as a JSAnimation would on a seek.
4091
- */
4092
- if (isHeld) {
4093
- track.sync();
4231
+ if (isHeld)
4094
4232
  continue;
4095
- }
4096
4233
  if (track.speed < 0)
4097
4234
  return;
4098
4235
  if (trackCycle) {
@@ -4132,12 +4269,35 @@
4132
4269
  */
4133
4270
  const tween = !cycle && getTween(moving);
4134
4271
  try {
4135
- if (tween && tween[1] > now) {
4136
- const [from, to, easing] = tween;
4137
- add(run(moving, from, to, easing), from - now, to - from);
4138
- }
4139
- else if (end > now) {
4140
- 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);
4141
4301
  /**
4142
4302
  * Animations that end sooner finish on the events of
4143
4303
  * empty animations that end with them.
@@ -4186,9 +4346,14 @@
4186
4346
  const [builtTime, builtSpeed, wasHeld] = state;
4187
4347
  const trackTime = track.at(now);
4188
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
+ */
4189
4353
  if (wasHeld) {
4190
- if (isHeld && trackTime === builtTime)
4354
+ if (isHeld && (trackTime === builtTime || !segments.length)) {
4191
4355
  continue;
4356
+ }
4192
4357
  return false;
4193
4358
  }
4194
4359
  const trackElapsed = (trackTime - builtTime) / builtSpeed;
@@ -4318,6 +4483,12 @@
4318
4483
  if (!animate(now))
4319
4484
  return group.demote();
4320
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());
4321
4492
  };
4322
4493
  const group = {
4323
4494
  tracks,
@@ -4337,7 +4508,7 @@
4337
4508
  isScheduled = true;
4338
4509
  frameData.isProcessing
4339
4510
  ? frame.render(build, false, true)
4340
- : queueMicrotask(build);
4511
+ : buildSoon(build);
4341
4512
  }
4342
4513
  },
4343
4514
  /**
@@ -4666,7 +4837,8 @@
4666
4837
  constructor(animation) {
4667
4838
  super();
4668
4839
  this.animation = animation;
4669
- animation.onfinish = () => {
4840
+ animation.onfinish = this.onFinish = () => {
4841
+ animation.onfinish = null;
4670
4842
  this.finishedTime = this.time;
4671
4843
  this.notifyFinished();
4672
4844
  };
@@ -9261,12 +9433,15 @@
9261
9433
  let hasRelativeLayoutChanged = false;
9262
9434
  const relativeParent = node.getClosestProjectingParent();
9263
9435
  /**
9264
- * A snapshot resumed from another element is only relative to this
9265
- * node's relativeParent if it's a layoutRoot that contains both.
9436
+ * A snapshot from another element is only relative to this node's
9437
+ * relativeParent if it's a layoutRoot that contains both. A node can
9438
+ * hold one without resumeFrom, when the element it came from changed
9439
+ * layoutId but stayed mounted (see switchLayoutId), or a remix
9440
+ * layout exit cleared it (see markLayoutExit).
9266
9441
  */
9267
- if (!node.resumeFrom ||
9442
+ if (!isShared ||
9268
9443
  (relativeParent?.options.layoutRoot &&
9269
- node.resumeFrom.path.includes(relativeParent))) {
9444
+ node.resumeFrom?.path.includes(relativeParent))) {
9270
9445
  /**
9271
9446
  * If the relativeParent is itself resuming from a different element then
9272
9447
  * the relative snapshot is not relavent
@@ -9783,9 +9958,13 @@
9783
9958
  * If this element has been mounted before (e.g. after a Suspense
9784
9959
  * unmount/remount), reset motion values to their initial state
9785
9960
  * so animations replay correctly from initial → animate, unless
9786
- * the presence asks to keep them.
9961
+ * the presence asks to keep them or the element is only shown
9962
+ * again to exit (#4049).
9787
9963
  */
9788
- if (this.hasBeenMounted && !this.presenceContext?.keepValuesOnRemount) {
9964
+ const presence = this.presenceContext;
9965
+ if (this.hasBeenMounted &&
9966
+ presence?.isPresent !== false &&
9967
+ !presence?.keepValuesOnRemount) {
9789
9968
  for (const key in this.initialValues) {
9790
9969
  this.values.get(key)?.jump(this.initialValues[key]);
9791
9970
  this.latestValues[key] = this.initialValues[key];
@@ -10858,7 +11037,7 @@
10858
11037
 
10859
11038
  const overflowStyles = /*#__PURE__*/ new Set(["auto", "scroll"]);
10860
11039
  class PanSession {
10861
- constructor(event, handlers, { transformPagePoint, contextWindow = window, dragSnapToOrigin = false, distanceThreshold = 3, element, } = {}) {
11040
+ constructor(event, handlers, { transformPagePoint, contextWindow = window, dragSnapToOrigin = false, distanceThreshold = 3, element, immediate, } = {}) {
10862
11041
  this.startEvent = null;
10863
11042
  this.lastMoveEvent = null;
10864
11043
  this.lastMoveEventInfo = null;
@@ -10944,7 +11123,7 @@
10944
11123
  this.dragSnapToOrigin = dragSnapToOrigin;
10945
11124
  this.handlers = handlers;
10946
11125
  this.transformPagePoint = transformPagePoint;
10947
- this.distanceThreshold = distanceThreshold;
11126
+ this.distanceThreshold = immediate ? 0 : distanceThreshold;
10948
11127
  this.contextWindow = contextWindow || window;
10949
11128
  const info = extractEventInfo(event);
10950
11129
  const initialInfo = transformPoint(info, this.transformPagePoint);
@@ -10968,6 +11147,10 @@
10968
11147
  if (element) {
10969
11148
  this.startScrollTracking(element);
10970
11149
  }
11150
+ if (immediate) {
11151
+ this.handlePointerMove(event, info);
11152
+ this.updatePoint();
11153
+ }
10971
11154
  }
10972
11155
  /**
10973
11156
  * Start tracking scroll on ancestors and window.
@@ -11343,14 +11526,14 @@
11343
11526
  this.visualElement = visualElement;
11344
11527
  this.readProps = readProps;
11345
11528
  }
11346
- start(originEvent, { snapToCursor = false, distanceThreshold } = {}) {
11529
+ start(originEvent, options = {}) {
11347
11530
  /**
11348
11531
  * Don't start dragging if this component is exiting
11349
11532
  */
11350
11533
  if (this.visualElement.presenceContext?.isPresent === false)
11351
11534
  return;
11352
11535
  const onSessionStart = (event) => {
11353
- if (snapToCursor)
11536
+ if (options.snapToCursor)
11354
11537
  this.snapToCursor(event);
11355
11538
  this.stopAnimation();
11356
11539
  };
@@ -11472,9 +11655,9 @@
11472
11655
  onSessionEnd,
11473
11656
  resumeAnimation,
11474
11657
  }, {
11658
+ ...options,
11475
11659
  transformPagePoint: this.visualElement.getTransformPagePoint(),
11476
11660
  dragSnapToOrigin,
11477
- distanceThreshold,
11478
11661
  contextWindow: this.visualElement.current?.ownerDocument.defaultView,
11479
11662
  element: this.visualElement.current,
11480
11663
  });
@@ -11922,8 +12105,10 @@
11922
12105
  return isElementConstraints(dragConstraints) && !!onMeasureDragConstraints;
11923
12106
  }
11924
12107
 
11925
- 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, }) => ({
11926
12109
  drag: axis || true,
12110
+ _dragX: x || (y && motionValue(0)),
12111
+ _dragY: y || (x && motionValue(0)),
11927
12112
  dragConstraints: constraints,
11928
12113
  dragElastic: elastic,
11929
12114
  dragMomentum: momentum,
@@ -12132,7 +12317,7 @@
12132
12317
  */
12133
12318
  let isKeyPressing = false;
12134
12319
  const handleKeydown = filterEvents(() => {
12135
- if (isPressing.has(element))
12320
+ if (isKeyPressing || isPressing.has(element))
12136
12321
  return;
12137
12322
  isKeyPressing = true;
12138
12323
  firePointerEvent(element, "down");
@@ -12164,6 +12349,18 @@
12164
12349
  return isPrimaryPointer(event) && !isDragActive();
12165
12350
  }
12166
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();
12167
12364
  /**
12168
12365
  * Create a press gesture.
12169
12366
  *
@@ -12181,22 +12378,110 @@
12181
12378
  * to be focusable for this to work, whereas a press gesture will
12182
12379
  * make an element focusable by default.
12183
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
+ *
12184
12392
  * @public
12185
12393
  */
12186
12394
  function press(targetOrSelector, onPressStart, options = {}) {
12187
12395
  onPressStart = bindToScope(onPressStart);
12188
12396
  const [targets, eventOptions, cancelEvents] = setupGesture(targetOrSelector, options);
12397
+ const sequences = new WeakMap();
12189
12398
  const startPress = (startEvent) => {
12190
12399
  const target = startEvent.currentTarget;
12191
12400
  if (!isValidPressEvent(startEvent))
12192
12401
  return;
12193
12402
  if (claimedPointerDownEvents.has(startEvent))
12194
12403
  return;
12195
- isPressing.add(target);
12196
12404
  if (options.stopPropagation) {
12197
12405
  claimedPointerDownEvents.add(startEvent);
12198
12406
  }
12199
- 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
+ };
12200
12485
  /**
12201
12486
  * End listeners run in the capture phase so a descendant calling
12202
12487
  * stopPropagation() in its own pointerup handler can't prevent the
@@ -12207,14 +12492,55 @@
12207
12492
  const onPointerEnd = (endEvent, success) => {
12208
12493
  window.removeEventListener("pointerup", onPointerUp, endEventOptions);
12209
12494
  window.removeEventListener("pointercancel", onPointerCancel, endEventOptions);
12210
- if (isPressing.has(target)) {
12211
- isPressing.delete(target);
12212
- }
12213
- 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))
12214
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);
12215
12536
  }
12216
- if (typeof onPressEnd === "function") {
12217
- 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
+ });
12218
12544
  }
12219
12545
  };
12220
12546
  const onPointerUp = (upEvent) => {
@@ -12226,8 +12552,35 @@
12226
12552
  const onPointerCancel = (cancelEvent) => {
12227
12553
  onPointerEnd(cancelEvent, false);
12228
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
+ };
12229
12568
  window.addEventListener("pointerup", onPointerUp, endEventOptions);
12230
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
+ }
12231
12584
  };
12232
12585
  targets.forEach((target) => {
12233
12586
  const pointerDownTarget = options.useGlobalTarget ? window : target;
@@ -15163,13 +15516,45 @@
15163
15516
  return segment;
15164
15517
  });
15165
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;
15166
15526
  animationDefinitions.forEach(({ keyframes, transition }, subject) => {
15167
- animations.push(...animateSubject(subject, keyframes, transition));
15527
+ animations.push(...animateSubject(subject, keyframes, {
15528
+ ...transition,
15529
+ reduceMotion,
15530
+ }));
15168
15531
  });
15169
15532
  return animations;
15170
15533
  }
15171
15534
 
15172
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
+ }
15173
15558
  /**
15174
15559
  * Copy a segment with a new `at`, adding a transition if it has none.
15175
15560
  */
@@ -15257,15 +15642,6 @@
15257
15642
  this.invalidate();
15258
15643
  this.flush();
15259
15644
  }
15260
- /**
15261
- * Register with the scope (for instance from useAnimate()) so it
15262
- * stops when the scope is torn down.
15263
- */
15264
- const { scope } = options;
15265
- if (scope) {
15266
- scope.animations.push(this);
15267
- this.finished.then(() => removeItem(scope.animations, this));
15268
- }
15269
15645
  }
15270
15646
  /**
15271
15647
  * Add a segment, label or timeline. `at` overrides any `at` already
@@ -15340,10 +15716,18 @@
15340
15716
  * Play forwards or backwards to a time or label, then pause there.
15341
15717
  */
15342
15718
  playTo(position) {
15343
- 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];
15344
15728
  const speed = Math.abs(this.speed);
15345
- this.speed = target < this.time ? -speed : speed;
15346
- this.stopAt = target;
15729
+ this.speed = target < time ? -speed : speed;
15730
+ this.stopAt = stopAt;
15347
15731
  this.clock || this.invalidate();
15348
15732
  this.play();
15349
15733
  return this;
@@ -15471,6 +15855,9 @@
15471
15855
  this.parent = parent;
15472
15856
  }
15473
15857
  build() {
15858
+ const prevDuration = this.isWrapped
15859
+ ? this.group.iterationDuration
15860
+ : this.totalDuration;
15474
15861
  this.isDirty = false;
15475
15862
  this.isWrapped = false;
15476
15863
  const prev = this.group;
@@ -15555,9 +15942,19 @@
15555
15942
  })));
15556
15943
  }
15557
15944
  const group = (this.group = new GroupAnimationWithThen(animations));
15558
- time = Math.min(time, duration);
15559
- 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) {
15560
15956
  this.newFinished();
15957
+ }
15561
15958
  if (this.scrollTimeline) {
15562
15959
  this.detachScroll = group.attachTimeline(this.scrollTimeline);
15563
15960
  }
@@ -15604,12 +16001,7 @@
15604
16001
  * The repeat the clock is in, from its time across all repeats.
15605
16002
  */
15606
16003
  currentRepeat() {
15607
- const { repeat = 0, repeatDelay = 0, delay = 0 } = this.options;
15608
- const cycle = this.totalDuration + repeatDelay;
15609
- const time = (this.clockAnimation?.time ?? 0) - delay;
15610
- return cycle && time > 0
15611
- ? Math.min(repeat, Math.floor(time / cycle))
15612
- : 0;
16004
+ return inRepeat(this.options, this.totalDuration, this.clockAnimation?.time ?? 0)[0];
15613
16005
  }
15614
16006
  onClock(time) {
15615
16007
  let prevTime = this.prevTime;
@@ -15618,22 +16010,23 @@
15618
16010
  return;
15619
16011
  const { onUpdate, onRepeat, repeatType = "loop" } = this.options;
15620
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
+ */
15621
16018
  const repeatIndex = this.currentRepeat();
15622
- if (repeatIndex !== this.repeatIndex) {
15623
- 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;
15624
16027
  onRepeat?.();
15625
- /**
15626
- * A loop jumps the playhead back to the start, or to the
15627
- * end when playing backwards. Finish crossing the repeat
15628
- * that ended, then cross the new one from its edge.
15629
- */
15630
- if (repeatType === "loop") {
15631
- const isForward = this.speed > 0;
15632
- if (this.cross(prevTime, isForward ? this.totalDuration : 0)) {
15633
- return;
15634
- }
15635
- prevTime = isForward ? -1 : Infinity;
15636
- }
16028
+ prevTime =
16029
+ repeatType === "loop" ? (isForward ? -1 : Infinity) : edge;
15637
16030
  }
15638
16031
  this.cross(prevTime, time);
15639
16032
  }
@@ -15660,9 +16053,12 @@
15660
16053
  return true;
15661
16054
  }
15662
16055
  }
15663
- pauseAt(time) {
16056
+ /**
16057
+ * Pause at a playhead position in the current repeat.
16058
+ */
16059
+ pauseAt(position) {
15664
16060
  this.pause();
15665
- this.time = time;
16061
+ this.time = inRepeat(this.options, this.totalDuration, position, this.repeatIndex)[1];
15666
16062
  return true;
15667
16063
  }
15668
16064
  watch() {
@@ -15677,8 +16073,13 @@
15677
16073
  * direction, as it does for a single animation.
15678
16074
  */
15679
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;
15680
16081
  callback?.();
15681
- this.resolveFinished();
16082
+ resolve();
15682
16083
  });
15683
16084
  }
15684
16085
  newFinished() {
@@ -15687,14 +16088,20 @@
15687
16088
  this.resolveFinished = resolve;
15688
16089
  });
15689
16090
  /**
15690
- * Stay in the scope while playing, including after a change
15691
- * 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.
15692
16094
  */
16095
+ const { scope } = this.options;
15693
16096
  const remove = this.motionScope?.add(() => {
15694
16097
  this.isKilled = true;
15695
16098
  this.stop();
15696
16099
  });
15697
- remove && this.finished.then(remove);
16100
+ scope?.animations.push(this);
16101
+ this.finished.then(() => {
16102
+ remove?.();
16103
+ scope && removeItem(scope.animations, this);
16104
+ });
15698
16105
  }
15699
16106
  }
15700
16107
 
@@ -15730,6 +16137,16 @@
15730
16137
  ...sequenceOptions.defaultTransition,
15731
16138
  };
15732
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;
15733
16150
  timeline = new Timeline(subjectOrSequence, sequenceOptions);
15734
16151
  }
15735
16152
  else {