motion-dom 14.10.0 → 14.11.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.
Files changed (46) hide show
  1. package/dist/cjs/index.js +886 -126
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/es/animation/GroupAnimation.mjs +21 -15
  4. package/dist/es/animation/GroupAnimation.mjs.map +1 -1
  5. package/dist/es/animation/animate/effects.mjs +15 -4
  6. package/dist/es/animation/animate/effects.mjs.map +1 -1
  7. package/dist/es/animation/animate/single-value.mjs +1 -1
  8. package/dist/es/animation/animate/single-value.mjs.map +1 -1
  9. package/dist/es/animation/generators/keyframes.mjs +44 -16
  10. package/dist/es/animation/generators/keyframes.mjs.map +1 -1
  11. package/dist/es/animation/interfaces/visual-element-target.mjs +1 -1
  12. package/dist/es/animation/interfaces/visual-element-target.mjs.map +1 -1
  13. package/dist/es/animation/keyframes/DOMKeyframesResolver.mjs +8 -3
  14. package/dist/es/animation/keyframes/DOMKeyframesResolver.mjs.map +1 -1
  15. package/dist/es/animation/keyframes/get-final.mjs +18 -4
  16. package/dist/es/animation/keyframes/get-final.mjs.map +1 -1
  17. package/dist/es/animation/waapi/transforms/TransformAnimation.mjs +155 -42
  18. package/dist/es/animation/waapi/transforms/TransformAnimation.mjs.map +1 -1
  19. package/dist/es/gestures/drag/VisualElementDragControls.mjs +4 -0
  20. package/dist/es/gestures/drag/VisualElementDragControls.mjs.map +1 -1
  21. package/dist/es/gestures/pan/PanSession.mjs.map +1 -1
  22. package/dist/es/gestures/pinch/index.mjs +552 -0
  23. package/dist/es/gestures/pinch/index.mjs.map +1 -0
  24. package/dist/es/gestures/press/index.mjs +6 -2
  25. package/dist/es/gestures/press/index.mjs.map +1 -1
  26. package/dist/es/index.mjs +1 -0
  27. package/dist/es/index.mjs.map +1 -1
  28. package/dist/es/projection/styles/transform.mjs +39 -17
  29. package/dist/es/projection/styles/transform.mjs.map +1 -1
  30. package/dist/es/render/utils/get-variant-context.mjs.map +1 -1
  31. package/dist/es/render/utils/variant-props.mjs +1 -0
  32. package/dist/es/render/utils/variant-props.mjs.map +1 -1
  33. package/dist/es/value/index.mjs +24 -20
  34. package/dist/es/value/index.mjs.map +1 -1
  35. package/dist/es/value/types/utils/animatable-none.mjs +13 -1
  36. package/dist/es/value/types/utils/animatable-none.mjs.map +1 -1
  37. package/dist/index.d.ts +360 -105
  38. package/dist/motion-dom.dev.js +886 -126
  39. package/dist/motion-dom.js +1 -1
  40. package/dist/size-rollup-motion-value.js +1 -1
  41. package/dist/size-rollup-motion-value.js.map +1 -1
  42. package/dist/size-rollup-pinch.js +2 -0
  43. package/dist/size-rollup-pinch.js.map +1 -0
  44. package/dist/size-rollup-style-effect.js +1 -1
  45. package/dist/size-rollup-style-effect.js.map +1 -1
  46. package/package.json +6 -2
package/dist/cjs/index.js CHANGED
@@ -1425,25 +1425,53 @@ function keyframes({ duration = 300, keyframes: keyframeValues, times, ease = "e
1425
1425
  value: keyframeValues[0],
1426
1426
  };
1427
1427
  /**
1428
- * Fast setup path for two keyframes one easing. interpolate() handles
1429
- * a custom mixer, and falls back to the usual one where it declines.
1430
- */
1431
- if (!customMixer &&
1432
- keyframeValues.length === 2 &&
1433
- !Array.isArray(easingFunctions) &&
1434
- (!times ||
1435
- times.length !== 2 ||
1436
- (times[0] === 0 && times[1] === 1))) {
1437
- const [origin, target] = keyframeValues;
1438
- const mixer = origin === target
1439
- ? undefined
1440
- : (motionUtils.MotionGlobalConfig.mix || mix)(origin, target);
1428
+ * Two keyframes, or keyframes that hold, move once, then hold, as each
1429
+ * value of a sequence does, ease and mix only the pair that moves.
1430
+ */
1431
+ let span = -1;
1432
+ if (!customMixer) {
1433
+ for (let i = 1; i < keyframeValues.length; i++) {
1434
+ if (keyframeValues[i] !== keyframeValues[i - 1]) {
1435
+ if (span >= 0) {
1436
+ span = -2;
1437
+ break;
1438
+ }
1439
+ span = i - 1;
1440
+ }
1441
+ }
1442
+ }
1443
+ if (span >= 0) {
1444
+ const offsets = times && times.length === keyframeValues.length
1445
+ ? times
1446
+ : defaultOffset(keyframeValues);
1447
+ const from = offsets[span] * duration;
1448
+ const to = offsets[span + 1] * duration;
1449
+ const origin = keyframeValues[span];
1450
+ const target = keyframeValues[span + 1];
1451
+ const ease = Array.isArray(easingFunctions)
1452
+ ? easingFunctions[span]
1453
+ : easingFunctions;
1454
+ const mixer = (motionUtils.MotionGlobalConfig.mix || mix)(origin, target);
1455
+ const last = keyframeValues.length - 2;
1441
1456
  return {
1442
1457
  calculatedDuration: duration,
1443
1458
  next: (t) => {
1444
- state.value = mixer
1445
- ? mixer(easingFunctions(duration > 0 ? motionUtils.clamp(0, 1, t / duration) : 1))
1446
- : target;
1459
+ /**
1460
+ * Held keyframes either side of the move are mixed as
1461
+ * interpolate() mixes them, so colours keep its format.
1462
+ */
1463
+ state.value =
1464
+ span && t < from
1465
+ ? mixer(0)
1466
+ : span < last && (t >= to || !duration)
1467
+ ? typeof target === "number"
1468
+ ? target
1469
+ : mixer(1)
1470
+ : mixer(ease(t >= to || !duration
1471
+ ? 1
1472
+ : t <= from
1473
+ ? 0
1474
+ : (t - from) / (to - from)));
1447
1475
  state.done = t >= duration;
1448
1476
  return state;
1449
1477
  },
@@ -1492,13 +1520,27 @@ function calcGeneratorVelocity(generator, t, initialVelocity = 0) {
1492
1520
  : getGeneratorVelocity((s) => generator.next(s).value, t, generator.next(t).value);
1493
1521
  }
1494
1522
 
1495
- const isNotNull = (value) => value !== null;
1496
1523
  function getFinalKeyframe(keyframes, { repeat, repeatType = "loop" }, finalKeyframe, speed = 1) {
1497
- const resolvedKeyframes = keyframes.filter(isNotNull);
1524
+ /**
1525
+ * The first and last keyframes that aren't null, and how many there
1526
+ * are. Found in one pass rather than filtering into a new array, as
1527
+ * this runs for every value of every animation.
1528
+ */
1529
+ let first;
1530
+ let last;
1531
+ let numResolved = 0;
1532
+ for (let i = 0; i < keyframes.length; i++) {
1533
+ const keyframe = keyframes[i];
1534
+ if (keyframe !== null) {
1535
+ if (!numResolved++)
1536
+ first = keyframe;
1537
+ last = keyframe;
1538
+ }
1539
+ }
1498
1540
  const useFirstKeyframe = speed < 0 || (repeat && repeatType !== "loop" && repeat % 2 === 1);
1499
- const index = useFirstKeyframe ? 0 : resolvedKeyframes.length - 1;
1541
+ const index = useFirstKeyframe ? 0 : numResolved - 1;
1500
1542
  return !index || finalKeyframe === undefined
1501
- ? resolvedKeyframes[index]
1543
+ ? (index ? last : first)
1502
1544
  : finalKeyframe;
1503
1545
  }
1504
1546
 
@@ -2178,14 +2220,26 @@ const defaultValueTypes = {
2178
2220
  const getDefaultValueType = (key) => defaultValueTypes[key];
2179
2221
 
2180
2222
  const customTypes = /*@__PURE__*/ new Set([filter, mask]);
2223
+ /**
2224
+ * The last call and its result. animate() on many elements asks for the
2225
+ * same none for each one, and parsing the value is the slow part.
2226
+ */
2227
+ let lastKey;
2228
+ let lastValue;
2229
+ let lastNone;
2181
2230
  function getAnimatableNone(key, value) {
2231
+ if (key === lastKey && value === lastValue)
2232
+ return lastNone;
2182
2233
  let defaultValueType = getDefaultValueType(key);
2183
2234
  if (!customTypes.has(defaultValueType))
2184
2235
  defaultValueType = complex;
2185
2236
  // If value is not recognised as animatable, ie "none", create an animatable version origin based on the target
2186
- return defaultValueType.getAnimatableNone
2237
+ lastNone = defaultValueType.getAnimatableNone
2187
2238
  ? defaultValueType.getAnimatableNone(value)
2188
2239
  : undefined;
2240
+ lastKey = key;
2241
+ lastValue = value;
2242
+ return lastNone;
2189
2243
  }
2190
2244
 
2191
2245
  function fillWildcards(keyframes) {
@@ -3220,7 +3274,8 @@ const buildAhead = 100;
3220
3274
  /**
3221
3275
  * Named easings that are the same cubic-bezier in JS and CSS.
3222
3276
  */
3223
- const isNativeEasing = (ease) => /^(linear|ease(In|Out|InOut))$/.test(ease);
3277
+ const nativeEasings = new Set(["linear", "easeIn", "easeOut", "easeInOut"]);
3278
+ const isNativeEasing = (ease) => nativeEasings.has(ease);
3224
3279
  /**
3225
3280
  * The last trace of each easing WAAPI can't run into linear(), with its
3226
3281
  * number of samples, so the elements of a sequence that share an easing
@@ -3304,6 +3359,54 @@ function canAccelerateTransform(options) {
3304
3359
  */
3305
3360
  !needsMixer(options));
3306
3361
  }
3362
+ /**
3363
+ * Starting or stopping tells the group to update its WAAPI animation.
3364
+ * The frame loop driver is only made once the group is on the main
3365
+ * thread, as accelerated animations never tick.
3366
+ */
3367
+ class TransformDriver {
3368
+ constructor(element, update, makeDriver, animation) {
3369
+ this.element = element;
3370
+ this.update = update;
3371
+ this.makeDriver = makeDriver;
3372
+ this.animation = animation;
3373
+ }
3374
+ get js() {
3375
+ return (this.driver || (this.driver = this.makeDriver(this.update)));
3376
+ }
3377
+ now() {
3378
+ return this.driver || this.makeDriver !== frameloopDriver
3379
+ ? this.js.now()
3380
+ : frameData.isProcessing
3381
+ ? frameData.timestamp
3382
+ : time.now();
3383
+ }
3384
+ /**
3385
+ * keepAlive is false when seeking. Seeking the WAAPI animation is
3386
+ * free, but if a seek needs a rebuild and keeps happening, the group
3387
+ * moves to the main thread.
3388
+ */
3389
+ start(keepAlive = true) {
3390
+ const group = getGroup(this.element);
3391
+ !group || group.isJS
3392
+ ? this.js.start(keepAlive)
3393
+ : group.schedule(false, !keepAlive);
3394
+ }
3395
+ stopJS() {
3396
+ this.driver?.stop();
3397
+ }
3398
+ stop() {
3399
+ this.stopJS();
3400
+ const group = getGroup(this.element);
3401
+ group && !group.isFinishing && group.release(this.animation);
3402
+ }
3403
+ }
3404
+ /**
3405
+ * JSAnimation's time setter, looked up on first use rather than at module
3406
+ * load, so the module has no top-level side effects and stays
3407
+ * tree-shakeable in flattened bundles.
3408
+ */
3409
+ let setTime;
3307
3410
  class TransformAnimation extends JSAnimation {
3308
3411
  constructor(options) {
3309
3412
  const element = getOwner(options).current;
@@ -3315,35 +3418,16 @@ class TransformAnimation extends JSAnimation {
3315
3418
  const canAccelerate = canAccelerateTransform(options);
3316
3419
  /**
3317
3420
  * The driver ticks on the main thread only once the group has
3318
- * moved there. Until then, starting or stopping tells the group to
3319
- * update its WAAPI animation.
3421
+ * moved there, so its frame loop driver is made then.
3320
3422
  */
3321
- options.driver = (update) => {
3322
- const js = driver(update);
3323
- return {
3324
- js,
3325
- now: js.now,
3326
- start: (keepAlive = true) => {
3327
- const group = getGroup(element);
3328
- const isJS = !group || group.isJS;
3329
- /**
3330
- * keepAlive is false when seeking. Seeking the WAAPI
3331
- * animation is free, but if a seek needs a rebuild and
3332
- * keeps happening, the group moves to the main thread.
3333
- */
3334
- isJS
3335
- ? js.start(keepAlive)
3336
- : group.schedule(false, !keepAlive);
3337
- },
3338
- stop: () => {
3339
- js.stop();
3340
- const group = getGroup(element);
3341
- group && !group.isFinishing && group.release(animation);
3342
- },
3343
- };
3344
- };
3423
+ options.driver = (update) => new TransformDriver(element, update, driver, animation);
3345
3424
  super(options);
3346
3425
  animation = this;
3426
+ /**
3427
+ * The first driver is made in super(), before this was assigned.
3428
+ */
3429
+ const firstDriver = this.driver;
3430
+ firstDriver && (firstDriver.animation = this);
3347
3431
  this.canAccelerate = canAccelerate;
3348
3432
  /**
3349
3433
  * stop is bound in JSAnimation, so wrap it rather than override.
@@ -3357,12 +3441,29 @@ class TransformAnimation extends JSAnimation {
3357
3441
  stop();
3358
3442
  };
3359
3443
  }
3444
+ /**
3445
+ * Each keyframe's offset, made once for the keyframes rather than on
3446
+ * every build.
3447
+ */
3448
+ offsets() {
3449
+ const { keyframes, times } = this.options;
3450
+ if (times)
3451
+ return times;
3452
+ if (this.offsetsOf !== keyframes) {
3453
+ this.offsetsOf = keyframes;
3454
+ this.cachedOffsets = defaultOffset(keyframes);
3455
+ }
3456
+ return this.cachedOffsets;
3457
+ }
3360
3458
  get group() {
3361
3459
  return getGroup(getOwner(this.options).current);
3362
3460
  }
3363
3461
  get js() {
3364
3462
  return this.driver?.js;
3365
3463
  }
3464
+ stopJS() {
3465
+ this.driver?.stopJS();
3466
+ }
3366
3467
  get name() {
3367
3468
  return this.options.name;
3368
3469
  }
@@ -3408,7 +3509,7 @@ class TransformAnimation extends JSAnimation {
3408
3509
  * exactly, otherwise its function, to trace.
3409
3510
  */
3410
3511
  tween() {
3411
- const { keyframes: keyframes$1, type, times, repeat, delay = 0 } = this.options;
3512
+ const { keyframes: keyframes$1, type, repeat, delay = 0 } = this.options;
3412
3513
  let span = -1;
3413
3514
  if (repeat || type !== keyframes || this.holdTime !== null) {
3414
3515
  return;
@@ -3423,7 +3524,7 @@ class TransformAnimation extends JSAnimation {
3423
3524
  let { ease } = this.options;
3424
3525
  if (motionUtils.isEasingArray(ease))
3425
3526
  ease = ease[span];
3426
- const offsets = times || defaultOffset(keyframes$1);
3527
+ const offsets = this.offsets();
3427
3528
  const at = (i) => this.startTime +
3428
3529
  (delay + offsets[i] * this.totalDuration) / this.speed;
3429
3530
  if (span >= 0) {
@@ -3452,9 +3553,9 @@ class TransformAnimation extends JSAnimation {
3452
3553
  */
3453
3554
  boundaries(from, to) {
3454
3555
  const { startTime, speed, resolvedDuration, options } = this;
3455
- const { delay = 0, repeat = 0, repeatDelay = 0, repeatType, times, keyframes: keyframes$1, type, } = options;
3556
+ const { delay = 0, repeat = 0, repeatDelay = 0, repeatType, type, } = options;
3456
3557
  const offsets = (type || keyframes) === keyframes
3457
- ? times || defaultOffset(keyframes$1)
3558
+ ? this.offsets()
3458
3559
  : [0, 1];
3459
3560
  const start = startTime + delay / speed;
3460
3561
  const result = [];
@@ -3531,8 +3632,12 @@ class TransformAnimation extends JSAnimation {
3531
3632
  }
3532
3633
  return super.time;
3533
3634
  }
3635
+ /**
3636
+ * Calls JSAnimation's setter directly, as `super.time =` is a slow
3637
+ * path in V8 and this runs for every animation of a seeked sequence.
3638
+ */
3534
3639
  set time(newTime) {
3535
- super.time = newTime;
3640
+ (setTime ?? (setTime = Object.getOwnPropertyDescriptor(JSAnimation.prototype, "time").set)).call(this, newTime);
3536
3641
  }
3537
3642
  /**
3538
3643
  * Scroll timelines set time every frame, so run these on the main
@@ -3578,6 +3683,12 @@ function createGroup(owner) {
3578
3683
  let hasChurned = false;
3579
3684
  let lastChurn = -Infinity;
3580
3685
  let wake;
3686
+ let wakeAt = Infinity;
3687
+ /**
3688
+ * While waiting to build a tween that starts later, how long after
3689
+ * the last build to build it.
3690
+ */
3691
+ let buildAt;
3581
3692
  const has = (key) => {
3582
3693
  for (const track of tracks)
3583
3694
  if (track.name === key)
@@ -3646,6 +3757,8 @@ function createGroup(owner) {
3646
3757
  */
3647
3758
  const stop = (shouldCommit = true) => {
3648
3759
  clearTimeout(wake);
3760
+ wakeAt = Infinity;
3761
+ buildAt = undefined;
3649
3762
  shouldCommit && commit();
3650
3763
  segments.forEach(({ animation }) => cancel(animation));
3651
3764
  segments = [];
@@ -3875,7 +3988,13 @@ function createGroup(owner) {
3875
3988
  * A WAAPI animation of the composed transform between two timestamps.
3876
3989
  */
3877
3990
  const run = (moving, from, to, nativeEasing, options = { fill: "both" }, settled) => {
3878
- const bounds = moving.flatMap((track) => track.boundaries(from, to));
3991
+ /**
3992
+ * With an easing given, there's nothing to sample, so no need
3993
+ * for the boundaries.
3994
+ */
3995
+ const bounds = nativeEasing
3996
+ ? []
3997
+ : moving.flatMap((track) => track.boundaries(from, to));
3879
3998
  const easing = nativeEasing || getEasing(moving, from, to, bounds);
3880
3999
  const times = easing ? [from, to] : sample(moving, from, to, bounds);
3881
4000
  const transform = times.map((t) => compose(t, settled));
@@ -3898,6 +4017,23 @@ function createGroup(owner) {
3898
4017
  easing: easing || "linear",
3899
4018
  });
3900
4019
  };
4020
+ /**
4021
+ * Check whether it's time to build in this long. A check that's due
4022
+ * sooner is kept, and checks again if it's early, so pausing and
4023
+ * playing don't each replace a timer. One due later, after a seek or
4024
+ * speed change while held, is replaced.
4025
+ */
4026
+ const wakeIn = (delay) => {
4027
+ const at = time.now() + delay;
4028
+ if (at < wakeAt) {
4029
+ clearTimeout(wake);
4030
+ wakeAt = at;
4031
+ wake = setTimeout(() => {
4032
+ wakeAt = Infinity;
4033
+ group.schedule(false);
4034
+ }, delay);
4035
+ }
4036
+ };
3901
4037
  /**
3902
4038
  * Replace the WAAPI animations with ones built from now. Returns
3903
4039
  * undefined if the animations can't be composed.
@@ -3973,7 +4109,7 @@ function createGroup(owner) {
3973
4109
  */
3974
4110
  if (from - now > buildAhead) {
3975
4111
  moving.forEach((track) => track.sync());
3976
- wake = setTimeout(() => group.schedule(true), from - now - buildAhead);
4112
+ wakeIn((buildAt = from - now - buildAhead));
3977
4113
  return true;
3978
4114
  }
3979
4115
  easing =
@@ -4028,9 +4164,11 @@ function createGroup(owner) {
4028
4164
  /**
4029
4165
  * If every animation has been paused, played, seeked or sped up
4030
4166
  * together since the WAAPI animation was built, do the same to it.
4031
- * Returns false if it needs rebuilding instead.
4167
+ * Returns false if it needs rebuilding instead. Seeking to before a
4168
+ * tween starts rebuilds, so a running group seeked every frame still
4169
+ * moves to the main thread.
4032
4170
  */
4033
- const retime = (now) => {
4171
+ const retime = (now, isSeek) => {
4034
4172
  let elapsed;
4035
4173
  let rate = 0;
4036
4174
  for (const track of tracks) {
@@ -4063,12 +4201,33 @@ function createGroup(owner) {
4063
4201
  }
4064
4202
  if (elapsed === undefined)
4065
4203
  return true;
4066
- if (!segments.length)
4067
- return false;
4204
+ /**
4205
+ * Waiting to build a tween that starts later. Until then the
4206
+ * values hold where they start, so only when to build changes.
4207
+ * Held, it has nothing on the compositor, so it can be seeked.
4208
+ */
4209
+ if (!segments.length) {
4210
+ if (buildAt === undefined || (isSeek && rate))
4211
+ return false;
4212
+ if (rate) {
4213
+ const delay = (buildAt - elapsed) / rate;
4214
+ if (delay < 1)
4215
+ return false;
4216
+ wakeIn(delay);
4217
+ }
4218
+ return true;
4219
+ }
4220
+ /**
4221
+ * Only a tween is built to start later, and it holds its first
4222
+ * keyframe until then, so it can be paused and played before it
4223
+ * starts.
4224
+ */
4068
4225
  for (const { offset, end, isDone } of segments) {
4069
4226
  const local = elapsed - offset;
4070
4227
  if (end < Infinity &&
4071
- (isDone ? local < end - 1 : local < 0 || local >= end)) {
4228
+ (isDone
4229
+ ? local < end - 1
4230
+ : (local < 0 && (isSeek || offset <= 0)) || local >= end)) {
4072
4231
  return false;
4073
4232
  }
4074
4233
  }
@@ -4089,7 +4248,8 @@ function createGroup(owner) {
4089
4248
  animation.updatePlaybackRate(rate);
4090
4249
  }
4091
4250
  else if (rate) {
4092
- animation.playbackRate = rate;
4251
+ animation.playbackRate !== rate &&
4252
+ (animation.playbackRate = rate);
4093
4253
  animation.startTime = now - local / rate;
4094
4254
  }
4095
4255
  else {
@@ -4163,12 +4323,12 @@ function createGroup(owner) {
4163
4323
  if (group.isJS) {
4164
4324
  group.isJS = false;
4165
4325
  needsBuild = true;
4166
- tracks.forEach((track) => track.js?.stop());
4326
+ tracks.forEach((track) => track.stopJS());
4167
4327
  }
4168
4328
  const shouldBuild = needsBuild;
4169
4329
  const churned = hasChurned;
4170
4330
  needsBuild = hasChurned = false;
4171
- if (shouldBuild || !retime(now)) {
4331
+ if (shouldBuild || !retime(now, churned)) {
4172
4332
  if (churned) {
4173
4333
  if (now - lastChurn < churnWindow)
4174
4334
  return group.demote();
@@ -4177,6 +4337,13 @@ function createGroup(owner) {
4177
4337
  if (!animate(now))
4178
4338
  return group.demote();
4179
4339
  }
4340
+ else if (buildAt !== undefined && !churned) {
4341
+ /**
4342
+ * Still waiting to build, so the values still hold where
4343
+ * they start.
4344
+ */
4345
+ return;
4346
+ }
4180
4347
  /**
4181
4348
  * Held animations don't move, so set their motion values to where
4182
4349
  * they're held, as a JSAnimation would on its next tick. Pausing
@@ -4444,7 +4611,10 @@ class AsyncMotionValueAnimation extends WithPromise {
4444
4611
  class GroupAnimation {
4445
4612
  constructor(animations) {
4446
4613
  // Bound to accommodate common `return animation.stop` pattern
4447
- this.stop = () => this.runAll("stop");
4614
+ this.stop = () => {
4615
+ for (const animation of this.animations)
4616
+ animation.stop();
4617
+ };
4448
4618
  this.animations = animations.filter(Boolean);
4449
4619
  }
4450
4620
  get finished() {
@@ -4456,11 +4626,6 @@ class GroupAnimation {
4456
4626
  getAll(propName) {
4457
4627
  return this.animations[0][propName];
4458
4628
  }
4459
- setAll(propName, newValue) {
4460
- for (let i = 0; i < this.animations.length; i++) {
4461
- this.animations[i][propName] = newValue;
4462
- }
4463
- }
4464
4629
  attachTimeline(timeline) {
4465
4630
  const subscriptions = this.animations.map((animation) => animation.attachTimeline(timeline));
4466
4631
  return () => {
@@ -4473,14 +4638,21 @@ class GroupAnimation {
4473
4638
  get time() {
4474
4639
  return this.getAll("time");
4475
4640
  }
4641
+ /**
4642
+ * Setters and methods name the property directly, rather than through
4643
+ * a shared helper, so each call site stays monomorphic. Sequences
4644
+ * call these on every animation, many times a second.
4645
+ */
4476
4646
  set time(time) {
4477
- this.setAll("time", time);
4647
+ for (const animation of this.animations)
4648
+ animation.time = time;
4478
4649
  }
4479
4650
  get speed() {
4480
4651
  return this.getAll("speed");
4481
4652
  }
4482
4653
  set speed(speed) {
4483
- this.setAll("speed", speed);
4654
+ for (const animation of this.animations)
4655
+ animation.speed = speed;
4484
4656
  }
4485
4657
  get state() {
4486
4658
  return this.getAll("state");
@@ -4494,20 +4666,21 @@ class GroupAnimation {
4494
4666
  get iterationDuration() {
4495
4667
  return getMax(this.animations, "iterationDuration");
4496
4668
  }
4497
- runAll(methodName) {
4498
- this.animations.forEach((controls) => controls[methodName]());
4499
- }
4500
4669
  play() {
4501
- this.runAll("play");
4670
+ for (const animation of this.animations)
4671
+ animation.play();
4502
4672
  }
4503
4673
  pause() {
4504
- this.runAll("pause");
4674
+ for (const animation of this.animations)
4675
+ animation.pause();
4505
4676
  }
4506
4677
  cancel() {
4507
- this.runAll("cancel");
4678
+ for (const animation of this.animations)
4679
+ animation.cancel();
4508
4680
  }
4509
4681
  complete() {
4510
- this.runAll("complete");
4682
+ for (const animation of this.animations)
4683
+ animation.complete();
4511
4684
  }
4512
4685
  }
4513
4686
  function getMax(animations, propName) {
@@ -4867,28 +5040,32 @@ class MotionValue {
4867
5040
  * @param animation - A function that starts the provided animation
4868
5041
  */
4869
5042
  start(startAnimation) {
5043
+ return new Promise((resolve) => this.run(startAnimation, resolve));
5044
+ }
5045
+ /**
5046
+ * As start(), without making a promise, for animations started
5047
+ * internally, which complete through their own controls.
5048
+ */
5049
+ run(startAnimation, onComplete) {
4870
5050
  this.stop();
4871
- return new Promise((resolve) => {
4872
- this.hasAnimated = true;
4873
- /**
4874
- * Complete synchronously via the callback rather than chaining a
4875
- * second promise per animation. Guard against animations that
4876
- * complete before startAnimation has returned.
4877
- */
4878
- let isComplete = false;
4879
- let animation;
4880
- // eslint-disable-next-line prefer-const -- read by the callback before assignment when completing synchronously
4881
- animation = startAnimation(() => {
4882
- isComplete = true;
4883
- this.events.animationComplete?.notify();
4884
- if (this.animation === animation)
4885
- this.animation = undefined;
4886
- resolve();
4887
- });
4888
- if (!isComplete)
4889
- this.animation = animation;
4890
- this.events.animationStart?.notify();
5051
+ this.hasAnimated = true;
5052
+ /**
5053
+ * Guard against animations that complete before startAnimation
5054
+ * has returned.
5055
+ */
5056
+ let isComplete = false;
5057
+ let animation;
5058
+ // eslint-disable-next-line prefer-const -- read by the callback before assignment when completing synchronously
5059
+ animation = startAnimation(() => {
5060
+ isComplete = true;
5061
+ this.events.animationComplete?.notify();
5062
+ if (this.animation === animation)
5063
+ this.animation = undefined;
5064
+ onComplete?.();
4891
5065
  });
5066
+ if (!isComplete)
5067
+ this.animation = animation;
5068
+ this.events.animationStart?.notify();
4892
5069
  }
4893
5070
  /**
4894
5071
  * Stop the currently active animation.
@@ -5511,7 +5688,7 @@ function animateTarget(visualElement, targetAndTransition, { delay = 0, transiti
5511
5688
  }
5512
5689
  addValueToWillChange(visualElement, key);
5513
5690
  const shouldReduceMotion = reduceMotion ?? visualElement.shouldReduceMotion;
5514
- value.start(animateMotionValue(key, value, valueTarget, shouldReduceMotion && positionalKeys.has(key)
5691
+ value.run(animateMotionValue(key, value, valueTarget, shouldReduceMotion && positionalKeys.has(key)
5515
5692
  ? { type: false }
5516
5693
  : valueTransition, visualElement, isHandoff));
5517
5694
  const animation = value.animation;
@@ -5646,9 +5823,14 @@ function makeNoneKeyframesAnimatable(unresolvedKeyframes, noneKeyframeIndexes, n
5646
5823
  }
5647
5824
 
5648
5825
  class DOMKeyframesResolver extends KeyframeResolver {
5649
- constructor() {
5650
- super(...arguments);
5651
- this.isAsync = true;
5826
+ /**
5827
+ * Passes isAsync to the base constructor rather than setting it as a
5828
+ * class field, which would compile to a constructor that spreads
5829
+ * `arguments` into super(). That spread is slow in the interpreter,
5830
+ * and animate() constructs one resolver per value.
5831
+ */
5832
+ constructor(unresolvedKeyframes, onComplete, name, motionValue, element) {
5833
+ super(unresolvedKeyframes, onComplete, name, motionValue, element, true);
5652
5834
  }
5653
5835
  /**
5654
5836
  * Keyframes that don't need reading from the DOM, or measuring,
@@ -5948,7 +6130,7 @@ function animateValues(getValue, keyframes, transition = {}, element) {
5948
6130
  frame.update(() => value.set(target));
5949
6131
  continue;
5950
6132
  }
5951
- value.start(animateMotionValue(key, value, target, reduceMotion && positionalKeys.has(key)
6133
+ value.run(animateMotionValue(key, value, target, reduceMotion && positionalKeys.has(key)
5952
6134
  ? { type: false }
5953
6135
  : transition, element));
5954
6136
  value.animation &&
@@ -5978,8 +6160,14 @@ function animateValues(getValue, keyframes, transition = {}, element) {
5978
6160
  * measurement) via `element`.
5979
6161
  */
5980
6162
  function animateEffectSubject(effect, subject, keyframes, transition, element) {
5981
- return animateValues((key) => {
5982
- let value = effect.get(subject, key);
6163
+ /**
6164
+ * Values made for this call are bound with one effect call, after the
6165
+ * loop, rather than one each.
6166
+ */
6167
+ let created;
6168
+ let isBound = false;
6169
+ const animations = animateValues((key) => {
6170
+ let value = created?.[key] ?? effect.get(subject, key);
5983
6171
  if (!value) {
5984
6172
  let initial;
5985
6173
  if (!element) {
@@ -5991,10 +6179,15 @@ function animateEffectSubject(effect, subject, keyframes, transition, element) {
5991
6179
  motionUtils.invariant(initial !== undefined, `"${key}" can't be read from the animated subject. Provide [from, to] keyframes.`, "effect-unreadable-value");
5992
6180
  }
5993
6181
  value = motionValue(initial, { owner: element });
5994
- effect(subject, { [key]: value });
6182
+ isBound
6183
+ ? effect(subject, { [key]: value })
6184
+ : ((created || (created = {}))[key] = value);
5995
6185
  }
5996
6186
  return value;
5997
6187
  }, keyframes, transition, element);
6188
+ isBound = true;
6189
+ created && effect(subject, created);
6190
+ return animations;
5998
6191
  }
5999
6192
  function firstKeyframe(target) {
6000
6193
  const first = Array.isArray(target) ? target[0] : undefined;
@@ -6990,7 +7183,7 @@ const processFrame = (timestamp = time.now()) => {
6990
7183
 
6991
7184
  function animateSingleValue(value, keyframes, options) {
6992
7185
  const motionValue$1 = isMotionValue(value) ? value : motionValue(value);
6993
- motionValue$1.start(animateMotionValue("", motionValue$1, keyframes, options));
7186
+ motionValue$1.run(animateMotionValue("", motionValue$1, keyframes, options));
6994
7187
  return motionValue$1.animation;
6995
7188
  }
6996
7189
 
@@ -7497,13 +7690,7 @@ function buildProjectionTransform(delta, treeScale, latestTransform) {
7497
7690
  if (xTranslate || yTranslate || zTranslate) {
7498
7691
  transform = `translate3d(${xTranslate}px, ${yTranslate}px, ${zTranslate}px) `;
7499
7692
  }
7500
- /**
7501
- * Apply scale correction for the tree transform.
7502
- * This will apply scale to the screen-orientated axes.
7503
- */
7504
- if (treeScale.x !== 1 || treeScale.y !== 1) {
7505
- transform += `scale(${1 / treeScale.x}, ${1 / treeScale.y}) `;
7506
- }
7693
+ let angles = "";
7507
7694
  if (latestTransform) {
7508
7695
  const { transformPerspective } = latestTransform;
7509
7696
  if (transformPerspective)
@@ -7512,20 +7699,48 @@ function buildProjectionTransform(delta, treeScale, latestTransform) {
7512
7699
  const key = angleTransforms[i];
7513
7700
  const value = latestTransform[key];
7514
7701
  if (value)
7515
- transform += `${key}(${value}deg) `;
7702
+ angles += `${key}(${value}deg) `;
7516
7703
  // Additive `rotate()` so user `rotate` isn't clobbered.
7517
7704
  if (!i && latestTransform.pathRotation)
7518
- transform += `rotate(${latestTransform.pathRotation}deg) `;
7705
+ angles += `rotate(${latestTransform.pathRotation}deg) `;
7519
7706
  }
7520
7707
  }
7521
- /**
7522
- * Apply scale to match the size of the element to the size we want it.
7523
- * This will apply scale to the element-orientated axes.
7524
- */
7525
- const elementScaleX = delta.x.scale * treeScale.x;
7526
- const elementScaleY = delta.y.scale * treeScale.y;
7527
- if (elementScaleX !== 1 || elementScaleY !== 1) {
7528
- transform += `scale(${elementScaleX}, ${elementScaleY})`;
7708
+ if (angles) {
7709
+ /**
7710
+ * Apply scale correction for the tree transform.
7711
+ * This will apply scale to the screen-orientated axes.
7712
+ */
7713
+ if (treeScale.x !== 1 || treeScale.y !== 1) {
7714
+ transform += `scale(${1 / treeScale.x}, ${1 / treeScale.y}) `;
7715
+ }
7716
+ transform += angles;
7717
+ /**
7718
+ * Apply scale to match the size of the element to the size we want it.
7719
+ * This will apply scale to the element-orientated axes.
7720
+ */
7721
+ const elementScaleX = delta.x.scale * treeScale.x;
7722
+ const elementScaleY = delta.y.scale * treeScale.y;
7723
+ if (elementScaleX !== 1 || elementScaleY !== 1) {
7724
+ transform += `scale(${elementScaleX}, ${elementScaleY})`;
7725
+ }
7726
+ }
7727
+ else if (delta.x.scale !== 1 || delta.y.scale !== 1) {
7728
+ /**
7729
+ * With no rotation or skew between them, the tree scale correction
7730
+ * (1 / treeScale) and the element scale (delta.scale * treeScale)
7731
+ * combine into a single scale. A shorter transform is quicker for
7732
+ * the browser to parse, which matters when it's set every frame.
7733
+ */
7734
+ transform += `scale(${delta.x.scale}, ${delta.y.scale})`;
7735
+ }
7736
+ else if (!transform && (treeScale.x !== 1 || treeScale.y !== 1)) {
7737
+ /**
7738
+ * Inside a scaled tree the transform must never be "none", even when
7739
+ * this element's own delta is the identity: scale correction for
7740
+ * borderRadius and boxShadow is skipped for "none", and the element
7741
+ * would gain and lose its stacking context as the delta snaps.
7742
+ */
7743
+ transform = "scale(1, 1)";
7529
7744
  }
7530
7745
  return transform || "none";
7531
7746
  }
@@ -9410,6 +9625,7 @@ const variantPriorityOrder = [
9410
9625
  "whileHover",
9411
9626
  "whileTap",
9412
9627
  "whileDrag",
9628
+ "whilePinch",
9413
9629
  "exit",
9414
9630
  ];
9415
9631
  const variantProps = /* @__PURE__ */ ["initial"].concat(variantPriorityOrder);
@@ -11203,6 +11419,10 @@ class VisualElementDragControls {
11203
11419
  this.openDragLock = null;
11204
11420
  this.isDragging = false;
11205
11421
  this.currentDirection = null;
11422
+ /**
11423
+ * Where the drag started, which pointer offsets are added to. Public
11424
+ * so pinch() can restart a drag from where it leaves the element.
11425
+ */
11206
11426
  this.originPoint = { x: 0, y: 0 };
11207
11427
  /**
11208
11428
  * The permitted boundaries of travel, in pixels.
@@ -11974,6 +12194,540 @@ function hover(elementOrSelector, onHoverStart, options = {}) {
11974
12194
  return cancel;
11975
12195
  }
11976
12196
 
12197
+ /**
12198
+ * A trackpad pinch arrives as a stream of `wheel` events with no end
12199
+ * event, so it ends once none have arrived for this long.
12200
+ */
12201
+ const wheelTimeout = 150;
12202
+ /**
12203
+ * Convert an `x`/`y` set in other units (e.g. `"-50%"` to centre an
12204
+ * element) to pixels, so the gesture can add to it.
12205
+ */
12206
+ const toPixels = (value, size) => {
12207
+ const latest = value.get();
12208
+ if (typeof latest === "string") {
12209
+ value.set(parseFloat(latest) * (latest.endsWith("%") ? size / 100 : 1));
12210
+ }
12211
+ };
12212
+ const limitsOf = (option) => typeof option === "object" ? option : {};
12213
+ /**
12214
+ * Resolve `elastic` to a resistance for each side, as for `dragElastic`.
12215
+ */
12216
+ const resolveElastic = (elastic = defaultElastic) => {
12217
+ const side = (value) => value === true ? defaultElastic : +(value || 0);
12218
+ return typeof elastic === "object"
12219
+ ? { min: side(elastic.min), max: side(elastic.max) }
12220
+ : { min: side(elastic), max: side(elastic) };
12221
+ };
12222
+ const clampTo = (value, { min = -Infinity, max = Infinity }) => motionUtils.clamp(min, max, value);
12223
+ /**
12224
+ * The raw gesture value that elastic shows as `value`. A pinch that starts
12225
+ * while a value is still past its limit (like during a spring back) carries
12226
+ * on from it, rather than jumping.
12227
+ */
12228
+ const unconstrain = (value, option, resistance) => {
12229
+ const limit = clampTo(value, limitsOf(option));
12230
+ const elastic = resistance[value < limit ? "min" : "max"];
12231
+ return elastic ? limit + (value - limit) / elastic : value;
12232
+ };
12233
+ /**
12234
+ * Listen for pinch gestures on `element`: two touches (or pens), a trackpad
12235
+ * pinch (`wheel` with `ctrlKey`, as Chrome, Edge and Firefox report it),
12236
+ * and Safari's `gesture*` events.
12237
+ *
12238
+ * `getValues` is called each time a gesture starts, in the frame's read
12239
+ * step, for the scale/rotate/x/y values to write to. It runs alongside the
12240
+ * element measurement so neither forces a layout.
12241
+ *
12242
+ * `getOptions` is read whenever it's needed, so options can change while
12243
+ * listening. Return `false` to disable the gesture, which also ends one in
12244
+ * progress.
12245
+ *
12246
+ * `onPinchStart` is called once as each gesture starts, with the gesture's
12247
+ * info as a third argument. It can return the gesture's handlers, as for
12248
+ * `pinch()`.
12249
+ */
12250
+ function addPinchListeners(element, getValues, getEnabledOptions, onPinchStart) {
12251
+ const pointers = new Map();
12252
+ /**
12253
+ * The gesture as last reported: focal point, log of a distance and an
12254
+ * angle. Only changes in distance and angle matter, so trackpad and
12255
+ * Safari input write a virtual distance (the scale) here. Logs keep
12256
+ * a long trackpad pinch from overflowing.
12257
+ */
12258
+ const current = { x: 0, y: 0, l: 0, a: 0 };
12259
+ const start = { ...current };
12260
+ const prev = { ...current };
12261
+ let source;
12262
+ let lastEvent;
12263
+ let values;
12264
+ let originX = 0;
12265
+ let originY = 0;
12266
+ let lastX = 0;
12267
+ let lastY = 0;
12268
+ let hasMoved = false;
12269
+ let wheelTime = 0;
12270
+ let handlers = {};
12271
+ let unlock;
12272
+ /**
12273
+ * The gesture's scale and rotation before limits and elastic apply.
12274
+ */
12275
+ let rawScale = 1;
12276
+ let rawRotate = 0;
12277
+ let stopSnap = () => { };
12278
+ /**
12279
+ * The latest enabled options, so a gesture disabled mid-way can still
12280
+ * report its end.
12281
+ */
12282
+ let options = {};
12283
+ const getOptions = () => (options = getEnabledOptions() || options);
12284
+ const info = () => ({
12285
+ point: { x: current.x, y: current.y },
12286
+ offset: { x: current.x - start.x, y: current.y - start.y },
12287
+ scale: Math.exp(current.l - start.l),
12288
+ rotate: current.a - start.a,
12289
+ });
12290
+ /**
12291
+ * Read the element's box and values, and find its layout origin: its
12292
+ * centre without `x`/`y`. Scale and rotate are applied around the
12293
+ * centre (the default transform origin), so they don't move it.
12294
+ */
12295
+ const measure = () => {
12296
+ const { left, top, width, height } = element.getBoundingClientRect();
12297
+ const { scale, rotate, pan, elastic } = getOptions();
12298
+ const [s, r, x, y] = (values = getValues());
12299
+ const resistance = resolveElastic(elastic);
12300
+ /**
12301
+ * Stop animations on values this gesture writes, and leave the
12302
+ * rest running.
12303
+ */
12304
+ scale !== false && s.stop();
12305
+ rotate !== false && r.stop();
12306
+ rawScale = unconstrain(s.get(), scale, resistance);
12307
+ rawRotate = unconstrain(r.get(), rotate, resistance);
12308
+ if (pan !== false) {
12309
+ x.stop();
12310
+ y.stop();
12311
+ toPixels(x, element.offsetWidth ?? width);
12312
+ toPixels(y, element.offsetHeight ?? height);
12313
+ }
12314
+ lastX = x.get();
12315
+ lastY = y.get();
12316
+ originX = left + width / 2 - lastX;
12317
+ originY = top + height / 2 - lastY;
12318
+ };
12319
+ const dragControls = () => elementDragControls.get(visualElementStore.get(element));
12320
+ /**
12321
+ * On release, spring scale and rotation back inside their limits (or
12322
+ * to a `snap` angle), keeping the focal point in place, or everything
12323
+ * back to its origin with `snapToOrigin`.
12324
+ */
12325
+ const snap = () => {
12326
+ const { scale: scaleOption, rotate: rotateOption, pan, snapToOrigin, transition, } = options;
12327
+ const [scale, rotate, x, y] = values;
12328
+ const s0 = scale.get();
12329
+ const r0 = rotate.get();
12330
+ const x0 = x.get();
12331
+ const y0 = y.get();
12332
+ const { snap: step } = limitsOf(rotateOption);
12333
+ let s1 = clampTo(s0, limitsOf(scaleOption));
12334
+ let r1 = clampTo(step ? Math.round(r0 / step) * step : r0, limitsOf(rotateOption));
12335
+ if (snapToOrigin) {
12336
+ s1 = 1;
12337
+ // The nearest upright angle, so it turns the shortest way
12338
+ r1 = Math.round(r0 / 360) * 360;
12339
+ }
12340
+ /**
12341
+ * Keep the focal point in place, unless the element doesn't move
12342
+ * with it, or a drag carries on and owns x/y.
12343
+ */
12344
+ const moves = pan !== false && !dragControls()?.isDragging;
12345
+ const vx = originX + x0 - current.x;
12346
+ const vy = originY + y0 - current.y;
12347
+ if (!s0 ||
12348
+ (s1 === s0 && r1 === r0 && !(snapToOrigin && moves && (x0 || y0)))) {
12349
+ return;
12350
+ }
12351
+ const generator = spring({
12352
+ keyframes: [0, 1],
12353
+ stiffness: 200,
12354
+ damping: 40,
12355
+ ...transition,
12356
+ });
12357
+ const startTime = time.now();
12358
+ /**
12359
+ * Drive the values from one spring, from 0 to 1, so they stay in
12360
+ * step and the focal point stays in place.
12361
+ */
12362
+ const tick = () => {
12363
+ if (dragControls()?.isDragging)
12364
+ return stopSnap();
12365
+ const { value: p, done } = generator.next(time.now() - startTime);
12366
+ if (done)
12367
+ stopSnap();
12368
+ const k = mixNumber$1(s0, s1, p);
12369
+ const r = mixNumber$1(r0, r1, p);
12370
+ const angle = ((r - r0) * Math.PI) / 180;
12371
+ const cos = (Math.cos(angle) * k) / s0;
12372
+ const sin = (Math.sin(angle) * k) / s0;
12373
+ scaleOption !== false && scale.set(k);
12374
+ rotateOption !== false && rotate.set(r);
12375
+ if (moves) {
12376
+ x.set(snapToOrigin
12377
+ ? mixNumber$1(x0, 0, p)
12378
+ : x0 + cos * vx - sin * vy - vx);
12379
+ y.set(snapToOrigin
12380
+ ? mixNumber$1(y0, 0, p)
12381
+ : y0 + sin * vx + cos * vy - vy);
12382
+ }
12383
+ };
12384
+ stopSnap = () => cancelFrame(tick);
12385
+ frame.update(tick, true);
12386
+ };
12387
+ /**
12388
+ * Apply the change in the gesture since the last frame as an
12389
+ * incremental scale and rotation around the previous focal point,
12390
+ * plus the focal point's movement.
12391
+ */
12392
+ const apply = () => {
12393
+ if (!values || !hasMoved)
12394
+ return;
12395
+ hasMoved = false;
12396
+ const { rotate: rotateOption, scale: scaleOption, pan, elastic, } = getOptions();
12397
+ const [scale, rotate, x, y] = values;
12398
+ const resistance = resolveElastic(elastic);
12399
+ const latest = scale.get();
12400
+ let theta = 0;
12401
+ let k = 1;
12402
+ /**
12403
+ * Apply limits and elastic to the gesture's raw scale and rotation.
12404
+ * At a hard limit, the raw value stops too, so moving back responds
12405
+ * straight away.
12406
+ */
12407
+ const constrain = (raw, option) => {
12408
+ const limits = limitsOf(option);
12409
+ const limit = clampTo(raw, limits);
12410
+ return resistance[raw < limit ? "min" : "max"]
12411
+ ? [applyConstraints(raw, limits, resistance), raw]
12412
+ : [limit, limit];
12413
+ };
12414
+ if (scaleOption !== false && latest) {
12415
+ const [next, raw] = constrain(motionUtils.clamp(1e-9, 1e9, rawScale * Math.exp(current.l - prev.l)), scaleOption);
12416
+ rawScale = raw;
12417
+ k = next / latest;
12418
+ scale.set(next);
12419
+ }
12420
+ if (rotateOption !== false) {
12421
+ const [next, raw] = constrain(rawRotate + current.a - prev.a, rotateOption);
12422
+ rawRotate = raw;
12423
+ theta = next - rotate.get();
12424
+ if (theta)
12425
+ rotate.set(next);
12426
+ }
12427
+ if (pan !== false) {
12428
+ /**
12429
+ * Pinch owns x/y while it runs, so stop any animation started
12430
+ * on them since (like a drag's snap back) taking over after.
12431
+ */
12432
+ x.stop();
12433
+ y.stop();
12434
+ /**
12435
+ * If drag() or the drag prop is dragging this element with one
12436
+ * of the touches, it has set x/y this frame to where it started
12437
+ * plus the touch's offset. Carry on from our own values instead,
12438
+ * and restart the drag from them and the touch's latest point.
12439
+ * That way it continues from wherever the pinch leaves the
12440
+ * element, and its release velocity only counts movement after
12441
+ * the pinch, so a zoom doesn't end in a fling.
12442
+ */
12443
+ const drag = dragControls();
12444
+ const isDragging = drag?.isDragging;
12445
+ if (!isDragging) {
12446
+ lastX = x.get();
12447
+ lastY = y.get();
12448
+ }
12449
+ const angle = (theta * Math.PI) / 180;
12450
+ const cos = Math.cos(angle) * k;
12451
+ const sin = Math.sin(angle) * k;
12452
+ const dx = prev.x - originX - lastX;
12453
+ const dy = prev.y - originY - lastY;
12454
+ /**
12455
+ * Move the centre so the previous focal point, scaled and
12456
+ * rotated around the centre, lands on the current focal point.
12457
+ */
12458
+ lastX += dx + current.x - prev.x - (cos * dx - sin * dy);
12459
+ lastY += dy + current.y - prev.y - (sin * dx + cos * dy);
12460
+ if (isDragging) {
12461
+ const history = drag.panSession.history;
12462
+ history.splice(0, history.length - 1);
12463
+ /**
12464
+ * Date the point long ago, so drag only measures release
12465
+ * velocity from moves after the pinch. A move that drag
12466
+ * processes as the touch lifts is part of the pinch.
12467
+ */
12468
+ history[0].timestamp = -Infinity;
12469
+ drag.originPoint.x = lastX;
12470
+ drag.originPoint.y = lastY;
12471
+ }
12472
+ x.set(lastX);
12473
+ y.set(lastY);
12474
+ }
12475
+ Object.assign(prev, current);
12476
+ handlers.onPinch?.(lastEvent, info());
12477
+ };
12478
+ /**
12479
+ * Runs after the update step, where drag sets x/y, so it has the final
12480
+ * say on them each frame.
12481
+ */
12482
+ const update = () => {
12483
+ if (!getEnabledOptions())
12484
+ return end(lastEvent);
12485
+ apply();
12486
+ if (source === "wheel" && time.now() - wheelTime > wheelTimeout) {
12487
+ end(lastEvent);
12488
+ }
12489
+ };
12490
+ const begin = (event, from) => {
12491
+ if (!getEnabledOptions())
12492
+ return;
12493
+ stopSnap();
12494
+ source = from;
12495
+ lastEvent = event;
12496
+ hasMoved = false;
12497
+ values = undefined;
12498
+ handlers = {};
12499
+ Object.assign(start, current);
12500
+ Object.assign(prev, current);
12501
+ frame.read(measure);
12502
+ frame.preRender(update, true);
12503
+ const result = onPinchStart(element, event, info());
12504
+ handlers =
12505
+ typeof result === "function" ? { onComplete: result } : result || {};
12506
+ };
12507
+ const end = (event) => {
12508
+ if (!source)
12509
+ return;
12510
+ cancelFrame(measure);
12511
+ cancelFrame(update);
12512
+ if (getEnabledOptions()) {
12513
+ apply();
12514
+ values && snap();
12515
+ }
12516
+ source = undefined;
12517
+ handlers.onComplete?.(event, info());
12518
+ };
12519
+ const moved = (event) => {
12520
+ lastEvent = event;
12521
+ hasMoved = true;
12522
+ };
12523
+ const readPointers = () => {
12524
+ const [a, b] = pointers.values();
12525
+ const angle = (Math.atan2(b.y - a.y, b.x - a.x) * 180) / Math.PI;
12526
+ const delta = angle - current.a;
12527
+ current.x = (a.x + b.x) / 2;
12528
+ current.y = (a.y + b.y) / 2;
12529
+ current.l = Math.log(Math.hypot(b.x - a.x, b.y - a.y) || 1);
12530
+ /**
12531
+ * Take the shortest way round, so rotation keeps counting past
12532
+ * 180deg rather than flipping to -180deg.
12533
+ */
12534
+ current.a += delta - 360 * Math.round(delta / 360);
12535
+ };
12536
+ const setPointer = (event) => pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
12537
+ const onPointerMove = (event) => {
12538
+ if (!pointers.has(event.pointerId))
12539
+ return;
12540
+ setPointer(event);
12541
+ if (source === "pointer") {
12542
+ readPointers();
12543
+ moved(event);
12544
+ }
12545
+ };
12546
+ const onPointerUp = (event) => {
12547
+ if (!pointers.delete(event.pointerId))
12548
+ return;
12549
+ if (source === "pointer")
12550
+ end(event);
12551
+ if (!pointers.size) {
12552
+ toggleWindowListeners();
12553
+ release();
12554
+ }
12555
+ };
12556
+ /**
12557
+ * Once a touch is down on the element, listen on the window too, so
12558
+ * the second touch can land beside it (for instance on a small image).
12559
+ */
12560
+ const onPointerDown = (event) => {
12561
+ if (source ||
12562
+ pointers.size > 1 ||
12563
+ pointers.has(event.pointerId) ||
12564
+ event.pointerType === "mouse") {
12565
+ return;
12566
+ }
12567
+ if (!pointers.size)
12568
+ toggleWindowListeners(true);
12569
+ setPointer(event);
12570
+ if (pointers.size === 2) {
12571
+ readPointers();
12572
+ begin(event, "pointer");
12573
+ /**
12574
+ * Unless this element drags, hold the drag lock until every
12575
+ * touch lifts. A press on it then ends as a cancel, rather than
12576
+ * a tap, and a draggable parent doesn't drag.
12577
+ */
12578
+ if (source &&
12579
+ !unlock &&
12580
+ !elementDragControls.get(visualElementStore.get(element))) {
12581
+ unlock = setDragLock(true);
12582
+ }
12583
+ }
12584
+ };
12585
+ /**
12586
+ * Release the drag lock a frame later, so a press ending on this same
12587
+ * pointerup still sees it.
12588
+ */
12589
+ const release = () => {
12590
+ const done = unlock;
12591
+ unlock = undefined;
12592
+ done && frame.postRender(done);
12593
+ };
12594
+ const toggleWindowListeners = (add) => {
12595
+ const method = add ? "addEventListener" : "removeEventListener";
12596
+ window[method]("pointerdown", onPointerDown);
12597
+ window[method]("pointermove", onPointerMove);
12598
+ window[method]("pointerup", onPointerUp);
12599
+ window[method]("pointercancel", onPointerUp);
12600
+ };
12601
+ const onWheel = (event) => {
12602
+ if (!event.ctrlKey || (source && source !== "wheel"))
12603
+ return;
12604
+ current.x = event.clientX;
12605
+ current.y = event.clientY;
12606
+ if (!source) {
12607
+ current.l = 0;
12608
+ begin(event, "wheel");
12609
+ if (!source)
12610
+ return;
12611
+ }
12612
+ event.preventDefault();
12613
+ /**
12614
+ * Lines (Firefox's mouse wheel) to pixels, and cap each event so a
12615
+ * mouse wheel notch with ctrl held (about 100px) zooms a step.
12616
+ */
12617
+ current.l -=
12618
+ motionUtils.clamp(-50, 50, event.deltaY * (event.deltaMode ? 16 : 1)) / 100;
12619
+ wheelTime = time.now();
12620
+ moved(event);
12621
+ };
12622
+ /**
12623
+ * Safari reports trackpad pinches as `gesture*` events. iOS Safari
12624
+ * fires them for touches too, alongside the pointer events we track.
12625
+ */
12626
+ const onGesture = (event) => {
12627
+ if (!pointers.size && (!source || source === "gesture")) {
12628
+ const { scale, rotation, clientX, clientY } = event;
12629
+ current.x = clientX;
12630
+ current.y = clientY;
12631
+ current.l = Math.log(scale);
12632
+ current.a = rotation;
12633
+ if (source) {
12634
+ moved(event);
12635
+ if (event.type === "gestureend")
12636
+ end(event);
12637
+ }
12638
+ else if (event.type === "gesturestart") {
12639
+ begin(event, "gesture");
12640
+ }
12641
+ }
12642
+ if (source)
12643
+ event.preventDefault();
12644
+ };
12645
+ const listeners = {
12646
+ pointerdown: onPointerDown,
12647
+ wheel: onWheel,
12648
+ gesturestart: onGesture,
12649
+ gesturechange: onGesture,
12650
+ gestureend: onGesture,
12651
+ };
12652
+ for (const name in listeners) {
12653
+ element.addEventListener(name, listeners[name], { passive: false });
12654
+ }
12655
+ return () => {
12656
+ for (const name in listeners) {
12657
+ element.removeEventListener(name, listeners[name]);
12658
+ }
12659
+ toggleWindowListeners();
12660
+ end(lastEvent);
12661
+ stopSnap();
12662
+ pointers.clear();
12663
+ release();
12664
+ };
12665
+ }
12666
+ const valueKeys = ["scale", "rotate", "x", "y"];
12667
+ /**
12668
+ * The element's scale/rotate/x/y motion values: those of its
12669
+ * VisualElement if it has one (it's a <motion.*> component, or drag() or
12670
+ * animateLayout() own it), otherwise those animate() binds to it, created
12671
+ * from its current style if this is the first time.
12672
+ */
12673
+ const getElementValues = (element) => {
12674
+ const visualElement = visualElementStore.get(element);
12675
+ const effect = isSVGElement(element) ? svgSubjectEffect : styleSubjectEffect;
12676
+ return valueKeys.map((key) => {
12677
+ if (visualElement) {
12678
+ return visualElement.getValue(key, key === "scale" ? 1 : 0);
12679
+ }
12680
+ let value = effect.get(element, key);
12681
+ if (!value) {
12682
+ value = motionValue(readStyleValue(element, key));
12683
+ effect(element, { [key]: value });
12684
+ }
12685
+ return value;
12686
+ });
12687
+ };
12688
+ function pinch(elementOrSelector, startOrOptions, options = {}) {
12689
+ let onPinchStart = () => { };
12690
+ if (typeof startOrOptions === "function") {
12691
+ onPinchStart = startOrOptions;
12692
+ }
12693
+ else if (startOrOptions) {
12694
+ options = startOrOptions;
12695
+ }
12696
+ const scope = activeScope.current;
12697
+ /**
12698
+ * Run the start callback and the handlers it returns in the active
12699
+ * scope, so work they start is collected. A returned function is
12700
+ * bound by `scope.bind` itself.
12701
+ */
12702
+ if (scope) {
12703
+ const start = scope.bind(onPinchStart);
12704
+ onPinchStart = (element, event) => {
12705
+ const result = start(element, event);
12706
+ if (typeof result !== "object")
12707
+ return result;
12708
+ const handlers = {};
12709
+ for (const key in result) {
12710
+ const name = key;
12711
+ const handler = result[name];
12712
+ handlers[name] = handler && scope.bind(handler);
12713
+ }
12714
+ return handlers;
12715
+ };
12716
+ }
12717
+ const cleanups = resolveElements(elementOrSelector).map((element) => {
12718
+ scope?.track(element);
12719
+ const { style } = element;
12720
+ const touchAction = style.touchAction;
12721
+ style.touchAction = "none";
12722
+ const remove = addPinchListeners(element, () => getElementValues(element), () => options, onPinchStart);
12723
+ return () => {
12724
+ remove();
12725
+ style.touchAction = touchAction;
12726
+ };
12727
+ });
12728
+ return addToScope(() => cleanups.forEach((cleanup) => cleanup()));
12729
+ }
12730
+
11977
12731
  /**
11978
12732
  * Recursively traverse up the tree to check whether the provided child node
11979
12733
  * is the parent or a descendant of it.
@@ -12189,14 +12943,18 @@ function press(targetOrSelector, onPressStart, options = {}) {
12189
12943
  */
12190
12944
  const endEventOptions = { ...eventOptions, capture: true };
12191
12945
  const onPointerEnd = (endEvent, success) => {
12946
+ /**
12947
+ * Another touch lifting (for instance after a pinch) doesn't
12948
+ * end the press.
12949
+ */
12950
+ if (!isPrimaryPointer(endEvent))
12951
+ return;
12192
12952
  window.removeEventListener("pointerup", onPointerUp, endEventOptions);
12193
12953
  window.removeEventListener("pointercancel", onPointerCancel, endEventOptions);
12194
12954
  window.removeEventListener("pointermove", onPointerMove, endEventOptions);
12195
12955
  target.removeEventListener("contextmenu", preventDefault);
12196
12956
  cancelFrame(tick);
12197
12957
  isPressing.delete(target);
12198
- if (!isPrimaryPointer(endEvent))
12199
- return;
12200
12958
  /**
12201
12959
  * A press that ends while a drag is active is a cancel.
12202
12960
  */
@@ -14714,6 +15472,7 @@ exports.addAttrValue = addAttrValue;
14714
15472
  exports.addBuilderToScope = addBuilderToScope;
14715
15473
  exports.addDomEvent = addDomEvent;
14716
15474
  exports.addEffect = addEffect;
15475
+ exports.addPinchListeners = addPinchListeners;
14717
15476
  exports.addPointerEvent = addPointerEvent;
14718
15477
  exports.addPointerInfo = addPointerInfo;
14719
15478
  exports.addSVGValue = addSVGValue;
@@ -14909,6 +15668,7 @@ exports.parseAnimateLayoutArgs = parseAnimateLayoutArgs;
14909
15668
  exports.parseCSSVariable = parseCSSVariable;
14910
15669
  exports.parseValueFromTransform = parseValueFromTransform;
14911
15670
  exports.percent = percent;
15671
+ exports.pinch = pinch;
14912
15672
  exports.pixelsToPercent = pixelsToPercent;
14913
15673
  exports.positionalKeys = positionalKeys;
14914
15674
  exports.prefersReducedMotion = prefersReducedMotion;