motion 12.9.7 → 12.10.1

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.
@@ -984,7 +984,7 @@
984
984
  const frameloopDriver = (update) => {
985
985
  const passTimestamp = ({ timestamp }) => update(timestamp);
986
986
  return {
987
- start: () => frame.update(passTimestamp, true),
987
+ start: (keepAlive = true) => frame.update(passTimestamp, keepAlive),
988
988
  stop: () => cancelFrame(passTimestamp),
989
989
  /**
990
990
  * If we're processing this frame we can use the
@@ -1811,6 +1811,7 @@
1811
1811
  else if (this.driver) {
1812
1812
  this.startTime = this.driver.now() - newTime / this.playbackSpeed;
1813
1813
  }
1814
+ this.driver?.start(false);
1814
1815
  }
1815
1816
  get speed() {
1816
1817
  return this.playbackSpeed;
@@ -1900,6 +1901,7 @@
1900
1901
  this.options.ease = "linear";
1901
1902
  this.initAnimation();
1902
1903
  }
1904
+ this.driver?.stop();
1903
1905
  return timeline.observe(this);
1904
1906
  }
1905
1907
  }
@@ -3367,420 +3369,6 @@
3367
3369
  return Array.from(elementOrSelector);
3368
3370
  }
3369
3371
 
3370
- function styleEffect(subject, values) {
3371
- const elements = resolveElements(subject);
3372
- const subscriptions = [];
3373
- for (let i = 0; i < elements.length; i++) {
3374
- const element = elements[i];
3375
- for (const key in values) {
3376
- const value = values[key];
3377
- /**
3378
- * TODO: Get specific setters for combined props (like x)
3379
- * or values with default types (like color)
3380
- *
3381
- * TODO: CSS variable support
3382
- */
3383
- const updateStyle = () => {
3384
- element.style[key] = value.get();
3385
- };
3386
- const scheduleUpdate = () => frame.render(updateStyle);
3387
- const cancel = value.on("change", scheduleUpdate);
3388
- scheduleUpdate();
3389
- subscriptions.push(() => {
3390
- cancel();
3391
- cancelFrame(updateStyle);
3392
- });
3393
- }
3394
- }
3395
- return () => {
3396
- for (const cancel of subscriptions) {
3397
- cancel();
3398
- }
3399
- };
3400
- }
3401
-
3402
- const { schedule: microtask, cancel: cancelMicrotask } =
3403
- /* @__PURE__ */ createRenderBatcher(queueMicrotask, false);
3404
-
3405
- const isDragging = {
3406
- x: false,
3407
- y: false,
3408
- };
3409
- function isDragActive() {
3410
- return isDragging.x || isDragging.y;
3411
- }
3412
-
3413
- function setDragLock(axis) {
3414
- if (axis === "x" || axis === "y") {
3415
- if (isDragging[axis]) {
3416
- return null;
3417
- }
3418
- else {
3419
- isDragging[axis] = true;
3420
- return () => {
3421
- isDragging[axis] = false;
3422
- };
3423
- }
3424
- }
3425
- else {
3426
- if (isDragging.x || isDragging.y) {
3427
- return null;
3428
- }
3429
- else {
3430
- isDragging.x = isDragging.y = true;
3431
- return () => {
3432
- isDragging.x = isDragging.y = false;
3433
- };
3434
- }
3435
- }
3436
- }
3437
-
3438
- function setupGesture(elementOrSelector, options) {
3439
- const elements = resolveElements(elementOrSelector);
3440
- const gestureAbortController = new AbortController();
3441
- const eventOptions = {
3442
- passive: true,
3443
- ...options,
3444
- signal: gestureAbortController.signal,
3445
- };
3446
- const cancel = () => gestureAbortController.abort();
3447
- return [elements, eventOptions, cancel];
3448
- }
3449
-
3450
- function isValidHover(event) {
3451
- return !(event.pointerType === "touch" || isDragActive());
3452
- }
3453
- /**
3454
- * Create a hover gesture. hover() is different to .addEventListener("pointerenter")
3455
- * in that it has an easier syntax, filters out polyfilled touch events, interoperates
3456
- * with drag gestures, and automatically removes the "pointerennd" event listener when the hover ends.
3457
- *
3458
- * @public
3459
- */
3460
- function hover(elementOrSelector, onHoverStart, options = {}) {
3461
- const [elements, eventOptions, cancel] = setupGesture(elementOrSelector, options);
3462
- const onPointerEnter = (enterEvent) => {
3463
- if (!isValidHover(enterEvent))
3464
- return;
3465
- const { target } = enterEvent;
3466
- const onHoverEnd = onHoverStart(target, enterEvent);
3467
- if (typeof onHoverEnd !== "function" || !target)
3468
- return;
3469
- const onPointerLeave = (leaveEvent) => {
3470
- if (!isValidHover(leaveEvent))
3471
- return;
3472
- onHoverEnd(leaveEvent);
3473
- target.removeEventListener("pointerleave", onPointerLeave);
3474
- };
3475
- target.addEventListener("pointerleave", onPointerLeave, eventOptions);
3476
- };
3477
- elements.forEach((element) => {
3478
- element.addEventListener("pointerenter", onPointerEnter, eventOptions);
3479
- });
3480
- return cancel;
3481
- }
3482
-
3483
- /**
3484
- * Recursively traverse up the tree to check whether the provided child node
3485
- * is the parent or a descendant of it.
3486
- *
3487
- * @param parent - Element to find
3488
- * @param child - Element to test against parent
3489
- */
3490
- const isNodeOrChild = (parent, child) => {
3491
- if (!child) {
3492
- return false;
3493
- }
3494
- else if (parent === child) {
3495
- return true;
3496
- }
3497
- else {
3498
- return isNodeOrChild(parent, child.parentElement);
3499
- }
3500
- };
3501
-
3502
- const isPrimaryPointer = (event) => {
3503
- if (event.pointerType === "mouse") {
3504
- return typeof event.button !== "number" || event.button <= 0;
3505
- }
3506
- else {
3507
- /**
3508
- * isPrimary is true for all mice buttons, whereas every touch point
3509
- * is regarded as its own input. So subsequent concurrent touch points
3510
- * will be false.
3511
- *
3512
- * Specifically match against false here as incomplete versions of
3513
- * PointerEvents in very old browser might have it set as undefined.
3514
- */
3515
- return event.isPrimary !== false;
3516
- }
3517
- };
3518
-
3519
- const focusableElements = new Set([
3520
- "BUTTON",
3521
- "INPUT",
3522
- "SELECT",
3523
- "TEXTAREA",
3524
- "A",
3525
- ]);
3526
- function isElementKeyboardAccessible(element) {
3527
- return (focusableElements.has(element.tagName) ||
3528
- element.tabIndex !== -1);
3529
- }
3530
-
3531
- const isPressing = new WeakSet();
3532
-
3533
- /**
3534
- * Filter out events that are not "Enter" keys.
3535
- */
3536
- function filterEvents(callback) {
3537
- return (event) => {
3538
- if (event.key !== "Enter")
3539
- return;
3540
- callback(event);
3541
- };
3542
- }
3543
- function firePointerEvent(target, type) {
3544
- target.dispatchEvent(new PointerEvent("pointer" + type, { isPrimary: true, bubbles: true }));
3545
- }
3546
- const enableKeyboardPress = (focusEvent, eventOptions) => {
3547
- const element = focusEvent.currentTarget;
3548
- if (!element)
3549
- return;
3550
- const handleKeydown = filterEvents(() => {
3551
- if (isPressing.has(element))
3552
- return;
3553
- firePointerEvent(element, "down");
3554
- const handleKeyup = filterEvents(() => {
3555
- firePointerEvent(element, "up");
3556
- });
3557
- const handleBlur = () => firePointerEvent(element, "cancel");
3558
- element.addEventListener("keyup", handleKeyup, eventOptions);
3559
- element.addEventListener("blur", handleBlur, eventOptions);
3560
- });
3561
- element.addEventListener("keydown", handleKeydown, eventOptions);
3562
- /**
3563
- * Add an event listener that fires on blur to remove the keydown events.
3564
- */
3565
- element.addEventListener("blur", () => element.removeEventListener("keydown", handleKeydown), eventOptions);
3566
- };
3567
-
3568
- /**
3569
- * Filter out events that are not primary pointer events, or are triggering
3570
- * while a Motion gesture is active.
3571
- */
3572
- function isValidPressEvent(event) {
3573
- return isPrimaryPointer(event) && !isDragActive();
3574
- }
3575
- /**
3576
- * Create a press gesture.
3577
- *
3578
- * Press is different to `"pointerdown"`, `"pointerup"` in that it
3579
- * automatically filters out secondary pointer events like right
3580
- * click and multitouch.
3581
- *
3582
- * It also adds accessibility support for keyboards, where
3583
- * an element with a press gesture will receive focus and
3584
- * trigger on Enter `"keydown"` and `"keyup"` events.
3585
- *
3586
- * This is different to a browser's `"click"` event, which does
3587
- * respond to keyboards but only for the `"click"` itself, rather
3588
- * than the press start and end/cancel. The element also needs
3589
- * to be focusable for this to work, whereas a press gesture will
3590
- * make an element focusable by default.
3591
- *
3592
- * @public
3593
- */
3594
- function press(targetOrSelector, onPressStart, options = {}) {
3595
- const [targets, eventOptions, cancelEvents] = setupGesture(targetOrSelector, options);
3596
- const startPress = (startEvent) => {
3597
- const target = startEvent.currentTarget;
3598
- if (!isValidPressEvent(startEvent) || isPressing.has(target))
3599
- return;
3600
- isPressing.add(target);
3601
- const onPressEnd = onPressStart(target, startEvent);
3602
- const onPointerEnd = (endEvent, success) => {
3603
- window.removeEventListener("pointerup", onPointerUp);
3604
- window.removeEventListener("pointercancel", onPointerCancel);
3605
- if (!isValidPressEvent(endEvent) || !isPressing.has(target)) {
3606
- return;
3607
- }
3608
- isPressing.delete(target);
3609
- if (typeof onPressEnd === "function") {
3610
- onPressEnd(endEvent, { success });
3611
- }
3612
- };
3613
- const onPointerUp = (upEvent) => {
3614
- onPointerEnd(upEvent, target === window ||
3615
- target === document ||
3616
- options.useGlobalTarget ||
3617
- isNodeOrChild(target, upEvent.target));
3618
- };
3619
- const onPointerCancel = (cancelEvent) => {
3620
- onPointerEnd(cancelEvent, false);
3621
- };
3622
- window.addEventListener("pointerup", onPointerUp, eventOptions);
3623
- window.addEventListener("pointercancel", onPointerCancel, eventOptions);
3624
- };
3625
- targets.forEach((target) => {
3626
- const pointerDownTarget = options.useGlobalTarget ? window : target;
3627
- pointerDownTarget.addEventListener("pointerdown", startPress, eventOptions);
3628
- if (target instanceof HTMLElement) {
3629
- target.addEventListener("focus", (event) => enableKeyboardPress(event, eventOptions));
3630
- if (!isElementKeyboardAccessible(target) &&
3631
- !target.hasAttribute("tabindex")) {
3632
- target.tabIndex = 0;
3633
- }
3634
- }
3635
- });
3636
- return cancelEvents;
3637
- }
3638
-
3639
- function getComputedStyle$2(element, name) {
3640
- const computedStyle = window.getComputedStyle(element);
3641
- return isCSSVar(name)
3642
- ? computedStyle.getPropertyValue(name)
3643
- : computedStyle[name];
3644
- }
3645
-
3646
- function observeTimeline(update, timeline) {
3647
- let prevProgress;
3648
- const onFrame = () => {
3649
- const { currentTime } = timeline;
3650
- const percentage = currentTime === null ? 0 : currentTime.value;
3651
- const progress = percentage / 100;
3652
- if (prevProgress !== progress) {
3653
- update(progress);
3654
- }
3655
- prevProgress = progress;
3656
- };
3657
- frame.preUpdate(onFrame, true);
3658
- return () => cancelFrame(onFrame);
3659
- }
3660
-
3661
- function record() {
3662
- const { value } = statsBuffer;
3663
- if (value === null) {
3664
- cancelFrame(record);
3665
- return;
3666
- }
3667
- value.frameloop.rate.push(frameData.delta);
3668
- value.animations.mainThread.push(activeAnimations.mainThread);
3669
- value.animations.waapi.push(activeAnimations.waapi);
3670
- value.animations.layout.push(activeAnimations.layout);
3671
- }
3672
- function mean(values) {
3673
- return values.reduce((acc, value) => acc + value, 0) / values.length;
3674
- }
3675
- function summarise(values, calcAverage = mean) {
3676
- if (values.length === 0) {
3677
- return {
3678
- min: 0,
3679
- max: 0,
3680
- avg: 0,
3681
- };
3682
- }
3683
- return {
3684
- min: Math.min(...values),
3685
- max: Math.max(...values),
3686
- avg: calcAverage(values),
3687
- };
3688
- }
3689
- const msToFps = (ms) => Math.round(1000 / ms);
3690
- function clearStatsBuffer() {
3691
- statsBuffer.value = null;
3692
- statsBuffer.addProjectionMetrics = null;
3693
- }
3694
- function reportStats() {
3695
- const { value } = statsBuffer;
3696
- if (!value) {
3697
- throw new Error("Stats are not being measured");
3698
- }
3699
- clearStatsBuffer();
3700
- cancelFrame(record);
3701
- const summary = {
3702
- frameloop: {
3703
- setup: summarise(value.frameloop.setup),
3704
- rate: summarise(value.frameloop.rate),
3705
- read: summarise(value.frameloop.read),
3706
- resolveKeyframes: summarise(value.frameloop.resolveKeyframes),
3707
- preUpdate: summarise(value.frameloop.preUpdate),
3708
- update: summarise(value.frameloop.update),
3709
- preRender: summarise(value.frameloop.preRender),
3710
- render: summarise(value.frameloop.render),
3711
- postRender: summarise(value.frameloop.postRender),
3712
- },
3713
- animations: {
3714
- mainThread: summarise(value.animations.mainThread),
3715
- waapi: summarise(value.animations.waapi),
3716
- layout: summarise(value.animations.layout),
3717
- },
3718
- layoutProjection: {
3719
- nodes: summarise(value.layoutProjection.nodes),
3720
- calculatedTargetDeltas: summarise(value.layoutProjection.calculatedTargetDeltas),
3721
- calculatedProjections: summarise(value.layoutProjection.calculatedProjections),
3722
- },
3723
- };
3724
- /**
3725
- * Convert the rate to FPS
3726
- */
3727
- const { rate } = summary.frameloop;
3728
- rate.min = msToFps(rate.min);
3729
- rate.max = msToFps(rate.max);
3730
- rate.avg = msToFps(rate.avg);
3731
- [rate.min, rate.max] = [rate.max, rate.min];
3732
- return summary;
3733
- }
3734
- function recordStats() {
3735
- if (statsBuffer.value) {
3736
- clearStatsBuffer();
3737
- throw new Error("Stats are already being measured");
3738
- }
3739
- const newStatsBuffer = statsBuffer;
3740
- newStatsBuffer.value = {
3741
- frameloop: {
3742
- setup: [],
3743
- rate: [],
3744
- read: [],
3745
- resolveKeyframes: [],
3746
- preUpdate: [],
3747
- update: [],
3748
- preRender: [],
3749
- render: [],
3750
- postRender: [],
3751
- },
3752
- animations: {
3753
- mainThread: [],
3754
- waapi: [],
3755
- layout: [],
3756
- },
3757
- layoutProjection: {
3758
- nodes: [],
3759
- calculatedTargetDeltas: [],
3760
- calculatedProjections: [],
3761
- },
3762
- };
3763
- newStatsBuffer.addProjectionMetrics = (metrics) => {
3764
- const { layoutProjection } = newStatsBuffer.value;
3765
- layoutProjection.nodes.push(metrics.nodes);
3766
- layoutProjection.calculatedTargetDeltas.push(metrics.calculatedTargetDeltas);
3767
- layoutProjection.calculatedProjections.push(metrics.calculatedProjections);
3768
- };
3769
- frame.postRender(record, true);
3770
- return reportStats;
3771
- }
3772
-
3773
- function transform(...args) {
3774
- const useImmediate = !Array.isArray(args[0]);
3775
- const argOffset = useImmediate ? 0 : -1;
3776
- const inputValue = args[0 + argOffset];
3777
- const inputRange = args[1 + argOffset];
3778
- const outputRange = args[2 + argOffset];
3779
- const options = args[3 + argOffset];
3780
- const interpolator = interpolate(inputRange, outputRange, options);
3781
- return useImmediate ? interpolator(inputValue) : interpolator;
3782
- }
3783
-
3784
3372
  /**
3785
3373
  * Maximum time between the value of two frames, beyond which we
3786
3374
  * assume the velocity has since been 0.
@@ -3799,293 +3387,820 @@
3799
3387
  */
3800
3388
  class MotionValue {
3801
3389
  /**
3802
- * @param init - The initiating value
3803
- * @param config - Optional configuration options
3390
+ * @param init - The initiating value
3391
+ * @param config - Optional configuration options
3392
+ *
3393
+ * - `transformer`: A function to transform incoming values with.
3394
+ */
3395
+ constructor(init, options = {}) {
3396
+ /**
3397
+ * This will be replaced by the build step with the latest version number.
3398
+ * When MotionValues are provided to motion components, warn if versions are mixed.
3399
+ */
3400
+ this.version = "__VERSION__";
3401
+ /**
3402
+ * Tracks whether this value can output a velocity. Currently this is only true
3403
+ * if the value is numerical, but we might be able to widen the scope here and support
3404
+ * other value types.
3405
+ *
3406
+ * @internal
3407
+ */
3408
+ this.canTrackVelocity = null;
3409
+ /**
3410
+ * An object containing a SubscriptionManager for each active event.
3411
+ */
3412
+ this.events = {};
3413
+ this.updateAndNotify = (v, render = true) => {
3414
+ const currentTime = time.now();
3415
+ /**
3416
+ * If we're updating the value during another frame or eventloop
3417
+ * than the previous frame, then the we set the previous frame value
3418
+ * to current.
3419
+ */
3420
+ if (this.updatedAt !== currentTime) {
3421
+ this.setPrevFrameValue();
3422
+ }
3423
+ this.prev = this.current;
3424
+ this.setCurrent(v);
3425
+ // Update update subscribers
3426
+ if (this.current !== this.prev) {
3427
+ this.events.change?.notify(this.current);
3428
+ if (this.dependents) {
3429
+ for (const dependent of this.dependents) {
3430
+ dependent.dirty();
3431
+ }
3432
+ }
3433
+ }
3434
+ // Update render subscribers
3435
+ if (render) {
3436
+ this.events.renderRequest?.notify(this.current);
3437
+ }
3438
+ };
3439
+ this.hasAnimated = false;
3440
+ this.setCurrent(init);
3441
+ this.owner = options.owner;
3442
+ }
3443
+ setCurrent(current) {
3444
+ this.current = current;
3445
+ this.updatedAt = time.now();
3446
+ if (this.canTrackVelocity === null && current !== undefined) {
3447
+ this.canTrackVelocity = isFloat(this.current);
3448
+ }
3449
+ }
3450
+ setPrevFrameValue(prevFrameValue = this.current) {
3451
+ this.prevFrameValue = prevFrameValue;
3452
+ this.prevUpdatedAt = this.updatedAt;
3453
+ }
3454
+ /**
3455
+ * Adds a function that will be notified when the `MotionValue` is updated.
3456
+ *
3457
+ * It returns a function that, when called, will cancel the subscription.
3458
+ *
3459
+ * When calling `onChange` inside a React component, it should be wrapped with the
3460
+ * `useEffect` hook. As it returns an unsubscribe function, this should be returned
3461
+ * from the `useEffect` function to ensure you don't add duplicate subscribers..
3462
+ *
3463
+ * ```jsx
3464
+ * export const MyComponent = () => {
3465
+ * const x = useMotionValue(0)
3466
+ * const y = useMotionValue(0)
3467
+ * const opacity = useMotionValue(1)
3468
+ *
3469
+ * useEffect(() => {
3470
+ * function updateOpacity() {
3471
+ * const maxXY = Math.max(x.get(), y.get())
3472
+ * const newOpacity = transform(maxXY, [0, 100], [1, 0])
3473
+ * opacity.set(newOpacity)
3474
+ * }
3475
+ *
3476
+ * const unsubscribeX = x.on("change", updateOpacity)
3477
+ * const unsubscribeY = y.on("change", updateOpacity)
3478
+ *
3479
+ * return () => {
3480
+ * unsubscribeX()
3481
+ * unsubscribeY()
3482
+ * }
3483
+ * }, [])
3484
+ *
3485
+ * return <motion.div style={{ x }} />
3486
+ * }
3487
+ * ```
3488
+ *
3489
+ * @param subscriber - A function that receives the latest value.
3490
+ * @returns A function that, when called, will cancel this subscription.
3491
+ *
3492
+ * @deprecated
3493
+ */
3494
+ onChange(subscription) {
3495
+ {
3496
+ warnOnce(false, `value.onChange(callback) is deprecated. Switch to value.on("change", callback).`);
3497
+ }
3498
+ return this.on("change", subscription);
3499
+ }
3500
+ on(eventName, callback) {
3501
+ if (!this.events[eventName]) {
3502
+ this.events[eventName] = new SubscriptionManager();
3503
+ }
3504
+ const unsubscribe = this.events[eventName].add(callback);
3505
+ if (eventName === "change") {
3506
+ return () => {
3507
+ unsubscribe();
3508
+ /**
3509
+ * If we have no more change listeners by the start
3510
+ * of the next frame, stop active animations.
3511
+ */
3512
+ frame.read(() => {
3513
+ if (!this.events.change.getSize()) {
3514
+ this.stop();
3515
+ }
3516
+ });
3517
+ };
3518
+ }
3519
+ return unsubscribe;
3520
+ }
3521
+ clearListeners() {
3522
+ for (const eventManagers in this.events) {
3523
+ this.events[eventManagers].clear();
3524
+ }
3525
+ }
3526
+ /**
3527
+ * Attaches a passive effect to the `MotionValue`.
3528
+ */
3529
+ attach(passiveEffect, stopPassiveEffect) {
3530
+ this.passiveEffect = passiveEffect;
3531
+ this.stopPassiveEffect = stopPassiveEffect;
3532
+ }
3533
+ /**
3534
+ * Sets the state of the `MotionValue`.
3535
+ *
3536
+ * @remarks
3537
+ *
3538
+ * ```jsx
3539
+ * const x = useMotionValue(0)
3540
+ * x.set(10)
3541
+ * ```
3542
+ *
3543
+ * @param latest - Latest value to set.
3544
+ * @param render - Whether to notify render subscribers. Defaults to `true`
3545
+ *
3546
+ * @public
3547
+ */
3548
+ set(v, render = true) {
3549
+ if (!render || !this.passiveEffect) {
3550
+ this.updateAndNotify(v, render);
3551
+ }
3552
+ else {
3553
+ this.passiveEffect(v, this.updateAndNotify);
3554
+ }
3555
+ }
3556
+ setWithVelocity(prev, current, delta) {
3557
+ this.set(current);
3558
+ this.prev = undefined;
3559
+ this.prevFrameValue = prev;
3560
+ this.prevUpdatedAt = this.updatedAt - delta;
3561
+ }
3562
+ /**
3563
+ * Set the state of the `MotionValue`, stopping any active animations,
3564
+ * effects, and resets velocity to `0`.
3565
+ */
3566
+ jump(v, endAnimation = true) {
3567
+ this.updateAndNotify(v);
3568
+ this.prev = v;
3569
+ this.prevUpdatedAt = this.prevFrameValue = undefined;
3570
+ endAnimation && this.stop();
3571
+ if (this.stopPassiveEffect)
3572
+ this.stopPassiveEffect();
3573
+ }
3574
+ dirty() {
3575
+ this.events.change?.notify(this.current);
3576
+ }
3577
+ addDependent(dependent) {
3578
+ if (!this.dependents) {
3579
+ this.dependents = new Set();
3580
+ }
3581
+ this.dependents.add(dependent);
3582
+ }
3583
+ removeDependent(dependent) {
3584
+ if (this.dependents) {
3585
+ this.dependents.delete(dependent);
3586
+ }
3587
+ }
3588
+ /**
3589
+ * Returns the latest state of `MotionValue`
3804
3590
  *
3805
- * - `transformer`: A function to transform incoming values with.
3591
+ * @returns - The latest state of `MotionValue`
3592
+ *
3593
+ * @public
3806
3594
  */
3807
- constructor(init, options = {}) {
3808
- /**
3809
- * This will be replaced by the build step with the latest version number.
3810
- * When MotionValues are provided to motion components, warn if versions are mixed.
3811
- */
3812
- this.version = "__VERSION__";
3813
- /**
3814
- * Tracks whether this value can output a velocity. Currently this is only true
3815
- * if the value is numerical, but we might be able to widen the scope here and support
3816
- * other value types.
3817
- *
3818
- * @internal
3819
- */
3820
- this.canTrackVelocity = null;
3821
- /**
3822
- * An object containing a SubscriptionManager for each active event.
3823
- */
3824
- this.events = {};
3825
- this.updateAndNotify = (v, render = true) => {
3826
- const currentTime = time.now();
3827
- /**
3828
- * If we're updating the value during another frame or eventloop
3829
- * than the previous frame, then the we set the previous frame value
3830
- * to current.
3831
- */
3832
- if (this.updatedAt !== currentTime) {
3833
- this.setPrevFrameValue();
3834
- }
3835
- this.prev = this.current;
3836
- this.setCurrent(v);
3837
- // Update update subscribers
3838
- if (this.current !== this.prev) {
3839
- this.events.change?.notify(this.current);
3840
- }
3841
- // Update render subscribers
3842
- if (render) {
3843
- this.events.renderRequest?.notify(this.current);
3844
- }
3845
- };
3846
- this.hasAnimated = false;
3847
- this.setCurrent(init);
3848
- this.owner = options.owner;
3849
- }
3850
- setCurrent(current) {
3851
- this.current = current;
3852
- this.updatedAt = time.now();
3853
- if (this.canTrackVelocity === null && current !== undefined) {
3854
- this.canTrackVelocity = isFloat(this.current);
3595
+ get() {
3596
+ if (collectMotionValues.current) {
3597
+ collectMotionValues.current.push(this);
3855
3598
  }
3599
+ return this.current;
3856
3600
  }
3857
- setPrevFrameValue(prevFrameValue = this.current) {
3858
- this.prevFrameValue = prevFrameValue;
3859
- this.prevUpdatedAt = this.updatedAt;
3601
+ /**
3602
+ * @public
3603
+ */
3604
+ getPrevious() {
3605
+ return this.prev;
3860
3606
  }
3861
3607
  /**
3862
- * Adds a function that will be notified when the `MotionValue` is updated.
3608
+ * Returns the latest velocity of `MotionValue`
3863
3609
  *
3864
- * It returns a function that, when called, will cancel the subscription.
3610
+ * @returns - The latest velocity of `MotionValue`. Returns `0` if the state is non-numerical.
3865
3611
  *
3866
- * When calling `onChange` inside a React component, it should be wrapped with the
3867
- * `useEffect` hook. As it returns an unsubscribe function, this should be returned
3868
- * from the `useEffect` function to ensure you don't add duplicate subscribers..
3612
+ * @public
3613
+ */
3614
+ getVelocity() {
3615
+ const currentTime = time.now();
3616
+ if (!this.canTrackVelocity ||
3617
+ this.prevFrameValue === undefined ||
3618
+ currentTime - this.updatedAt > MAX_VELOCITY_DELTA) {
3619
+ return 0;
3620
+ }
3621
+ const delta = Math.min(this.updatedAt - this.prevUpdatedAt, MAX_VELOCITY_DELTA);
3622
+ // Casts because of parseFloat's poor typing
3623
+ return velocityPerSecond(parseFloat(this.current) -
3624
+ parseFloat(this.prevFrameValue), delta);
3625
+ }
3626
+ /**
3627
+ * Registers a new animation to control this `MotionValue`. Only one
3628
+ * animation can drive a `MotionValue` at one time.
3869
3629
  *
3870
3630
  * ```jsx
3871
- * export const MyComponent = () => {
3872
- * const x = useMotionValue(0)
3873
- * const y = useMotionValue(0)
3874
- * const opacity = useMotionValue(1)
3875
- *
3876
- * useEffect(() => {
3877
- * function updateOpacity() {
3878
- * const maxXY = Math.max(x.get(), y.get())
3879
- * const newOpacity = transform(maxXY, [0, 100], [1, 0])
3880
- * opacity.set(newOpacity)
3881
- * }
3631
+ * value.start()
3632
+ * ```
3882
3633
  *
3883
- * const unsubscribeX = x.on("change", updateOpacity)
3884
- * const unsubscribeY = y.on("change", updateOpacity)
3634
+ * @param animation - A function that starts the provided animation
3635
+ */
3636
+ start(startAnimation) {
3637
+ this.stop();
3638
+ return new Promise((resolve) => {
3639
+ this.hasAnimated = true;
3640
+ this.animation = startAnimation(resolve);
3641
+ if (this.events.animationStart) {
3642
+ this.events.animationStart.notify();
3643
+ }
3644
+ }).then(() => {
3645
+ if (this.events.animationComplete) {
3646
+ this.events.animationComplete.notify();
3647
+ }
3648
+ this.clearAnimation();
3649
+ });
3650
+ }
3651
+ /**
3652
+ * Stop the currently active animation.
3885
3653
  *
3886
- * return () => {
3887
- * unsubscribeX()
3888
- * unsubscribeY()
3889
- * }
3890
- * }, [])
3654
+ * @public
3655
+ */
3656
+ stop() {
3657
+ if (this.animation) {
3658
+ this.animation.stop();
3659
+ if (this.events.animationCancel) {
3660
+ this.events.animationCancel.notify();
3661
+ }
3662
+ }
3663
+ this.clearAnimation();
3664
+ }
3665
+ /**
3666
+ * Returns `true` if this value is currently animating.
3891
3667
  *
3892
- * return <motion.div style={{ x }} />
3893
- * }
3894
- * ```
3668
+ * @public
3669
+ */
3670
+ isAnimating() {
3671
+ return !!this.animation;
3672
+ }
3673
+ clearAnimation() {
3674
+ delete this.animation;
3675
+ }
3676
+ /**
3677
+ * Destroy and clean up subscribers to this `MotionValue`.
3895
3678
  *
3896
- * @param subscriber - A function that receives the latest value.
3897
- * @returns A function that, when called, will cancel this subscription.
3679
+ * The `MotionValue` hooks like `useMotionValue` and `useTransform` automatically
3680
+ * handle the lifecycle of the returned `MotionValue`, so this method is only necessary if you've manually
3681
+ * created a `MotionValue` via the `motionValue` function.
3898
3682
  *
3899
- * @deprecated
3683
+ * @public
3900
3684
  */
3901
- onChange(subscription) {
3902
- {
3903
- warnOnce(false, `value.onChange(callback) is deprecated. Switch to value.on("change", callback).`);
3685
+ destroy() {
3686
+ this.dependents?.clear();
3687
+ this.events.destroy?.notify();
3688
+ this.clearListeners();
3689
+ this.stop();
3690
+ if (this.stopPassiveEffect) {
3691
+ this.stopPassiveEffect();
3904
3692
  }
3905
- return this.on("change", subscription);
3906
3693
  }
3907
- on(eventName, callback) {
3908
- if (!this.events[eventName]) {
3909
- this.events[eventName] = new SubscriptionManager();
3694
+ }
3695
+ function motionValue(init, options) {
3696
+ return new MotionValue(init, options);
3697
+ }
3698
+
3699
+ /**
3700
+ * Provided a value and a ValueType, returns the value as that value type.
3701
+ */
3702
+ const getValueAsType = (value, type) => {
3703
+ return type && typeof value === "number"
3704
+ ? type.transform(value)
3705
+ : value;
3706
+ };
3707
+
3708
+ class MotionValueState {
3709
+ constructor() {
3710
+ this.latest = {};
3711
+ this.values = new Map();
3712
+ }
3713
+ set(name, value, render, computed) {
3714
+ const existingValue = this.values.get(name);
3715
+ if (existingValue) {
3716
+ existingValue.onRemove();
3717
+ }
3718
+ const onChange = () => {
3719
+ this.latest[name] = getValueAsType(value.get(), numberValueTypes[name]);
3720
+ render && frame.render(render);
3721
+ };
3722
+ onChange();
3723
+ const cancelOnChange = value.on("change", onChange);
3724
+ computed && value.addDependent(computed);
3725
+ const remove = () => {
3726
+ cancelOnChange();
3727
+ render && cancelFrame(render);
3728
+ this.values.delete(name);
3729
+ computed && value.removeDependent(computed);
3730
+ };
3731
+ this.values.set(name, { value, onRemove: remove });
3732
+ return remove;
3733
+ }
3734
+ get(name) {
3735
+ return this.values.get(name)?.value;
3736
+ }
3737
+ destroy() {
3738
+ for (const value of this.values.values()) {
3739
+ value.onRemove();
3740
+ }
3741
+ }
3742
+ }
3743
+
3744
+ const translateAlias$1 = {
3745
+ x: "translateX",
3746
+ y: "translateY",
3747
+ z: "translateZ",
3748
+ transformPerspective: "perspective",
3749
+ };
3750
+ function buildTransform$1(state) {
3751
+ let transform = "";
3752
+ let transformIsDefault = true;
3753
+ /**
3754
+ * Loop over all possible transforms in order, adding the ones that
3755
+ * are present to the transform string.
3756
+ */
3757
+ for (let i = 0; i < transformPropOrder.length; i++) {
3758
+ const key = transformPropOrder[i];
3759
+ const value = state.latest[key];
3760
+ if (value === undefined)
3761
+ continue;
3762
+ let valueIsDefault = true;
3763
+ if (typeof value === "number") {
3764
+ valueIsDefault = value === (key.startsWith("scale") ? 1 : 0);
3765
+ }
3766
+ else {
3767
+ valueIsDefault = parseFloat(value) === 0;
3768
+ }
3769
+ if (!valueIsDefault) {
3770
+ transformIsDefault = false;
3771
+ const transformName = translateAlias$1[key] || key;
3772
+ const valueToRender = state.latest[key];
3773
+ transform += `${transformName}(${valueToRender}) `;
3774
+ }
3775
+ }
3776
+ return transformIsDefault ? "none" : transform.trim();
3777
+ }
3778
+
3779
+ const stateMap = new WeakMap();
3780
+ function styleEffect(subject, values) {
3781
+ const elements = resolveElements(subject);
3782
+ const subscriptions = [];
3783
+ for (let i = 0; i < elements.length; i++) {
3784
+ const element = elements[i];
3785
+ const state = stateMap.get(element) ?? new MotionValueState();
3786
+ stateMap.set(element, state);
3787
+ for (const key in values) {
3788
+ const value = values[key];
3789
+ const remove = addValue(element, state, key, value);
3790
+ subscriptions.push(remove);
3791
+ }
3792
+ }
3793
+ return () => {
3794
+ for (const cancel of subscriptions)
3795
+ cancel();
3796
+ };
3797
+ }
3798
+ function addValue(element, state, key, value) {
3799
+ let render = undefined;
3800
+ let computed = undefined;
3801
+ if (transformProps.has(key)) {
3802
+ if (!state.get("transform")) {
3803
+ state.set("transform", new MotionValue("none"), () => {
3804
+ element.style.transform = buildTransform$1(state);
3805
+ });
3806
+ }
3807
+ computed = state.get("transform");
3808
+ }
3809
+ else if (isCSSVar(key)) {
3810
+ render = () => {
3811
+ element.style.setProperty(key, state.latest[key]);
3812
+ };
3813
+ }
3814
+ else {
3815
+ render = () => {
3816
+ element.style[key] = state.latest[key];
3817
+ };
3818
+ }
3819
+ return state.set(key, value, render, computed);
3820
+ }
3821
+
3822
+ const { schedule: microtask, cancel: cancelMicrotask } =
3823
+ /* @__PURE__ */ createRenderBatcher(queueMicrotask, false);
3824
+
3825
+ const isDragging = {
3826
+ x: false,
3827
+ y: false,
3828
+ };
3829
+ function isDragActive() {
3830
+ return isDragging.x || isDragging.y;
3831
+ }
3832
+
3833
+ function setDragLock(axis) {
3834
+ if (axis === "x" || axis === "y") {
3835
+ if (isDragging[axis]) {
3836
+ return null;
3910
3837
  }
3911
- const unsubscribe = this.events[eventName].add(callback);
3912
- if (eventName === "change") {
3838
+ else {
3839
+ isDragging[axis] = true;
3913
3840
  return () => {
3914
- unsubscribe();
3915
- /**
3916
- * If we have no more change listeners by the start
3917
- * of the next frame, stop active animations.
3918
- */
3919
- frame.read(() => {
3920
- if (!this.events.change.getSize()) {
3921
- this.stop();
3922
- }
3923
- });
3841
+ isDragging[axis] = false;
3924
3842
  };
3925
3843
  }
3926
- return unsubscribe;
3927
- }
3928
- clearListeners() {
3929
- for (const eventManagers in this.events) {
3930
- this.events[eventManagers].clear();
3931
- }
3932
- }
3933
- /**
3934
- * Attaches a passive effect to the `MotionValue`.
3935
- */
3936
- attach(passiveEffect, stopPassiveEffect) {
3937
- this.passiveEffect = passiveEffect;
3938
- this.stopPassiveEffect = stopPassiveEffect;
3939
3844
  }
3940
- /**
3941
- * Sets the state of the `MotionValue`.
3942
- *
3943
- * @remarks
3944
- *
3945
- * ```jsx
3946
- * const x = useMotionValue(0)
3947
- * x.set(10)
3948
- * ```
3949
- *
3950
- * @param latest - Latest value to set.
3951
- * @param render - Whether to notify render subscribers. Defaults to `true`
3952
- *
3953
- * @public
3954
- */
3955
- set(v, render = true) {
3956
- if (!render || !this.passiveEffect) {
3957
- this.updateAndNotify(v, render);
3845
+ else {
3846
+ if (isDragging.x || isDragging.y) {
3847
+ return null;
3958
3848
  }
3959
3849
  else {
3960
- this.passiveEffect(v, this.updateAndNotify);
3850
+ isDragging.x = isDragging.y = true;
3851
+ return () => {
3852
+ isDragging.x = isDragging.y = false;
3853
+ };
3961
3854
  }
3962
3855
  }
3963
- setWithVelocity(prev, current, delta) {
3964
- this.set(current);
3965
- this.prev = undefined;
3966
- this.prevFrameValue = prev;
3967
- this.prevUpdatedAt = this.updatedAt - delta;
3856
+ }
3857
+
3858
+ function setupGesture(elementOrSelector, options) {
3859
+ const elements = resolveElements(elementOrSelector);
3860
+ const gestureAbortController = new AbortController();
3861
+ const eventOptions = {
3862
+ passive: true,
3863
+ ...options,
3864
+ signal: gestureAbortController.signal,
3865
+ };
3866
+ const cancel = () => gestureAbortController.abort();
3867
+ return [elements, eventOptions, cancel];
3868
+ }
3869
+
3870
+ function isValidHover(event) {
3871
+ return !(event.pointerType === "touch" || isDragActive());
3872
+ }
3873
+ /**
3874
+ * Create a hover gesture. hover() is different to .addEventListener("pointerenter")
3875
+ * in that it has an easier syntax, filters out polyfilled touch events, interoperates
3876
+ * with drag gestures, and automatically removes the "pointerennd" event listener when the hover ends.
3877
+ *
3878
+ * @public
3879
+ */
3880
+ function hover(elementOrSelector, onHoverStart, options = {}) {
3881
+ const [elements, eventOptions, cancel] = setupGesture(elementOrSelector, options);
3882
+ const onPointerEnter = (enterEvent) => {
3883
+ if (!isValidHover(enterEvent))
3884
+ return;
3885
+ const { target } = enterEvent;
3886
+ const onHoverEnd = onHoverStart(target, enterEvent);
3887
+ if (typeof onHoverEnd !== "function" || !target)
3888
+ return;
3889
+ const onPointerLeave = (leaveEvent) => {
3890
+ if (!isValidHover(leaveEvent))
3891
+ return;
3892
+ onHoverEnd(leaveEvent);
3893
+ target.removeEventListener("pointerleave", onPointerLeave);
3894
+ };
3895
+ target.addEventListener("pointerleave", onPointerLeave, eventOptions);
3896
+ };
3897
+ elements.forEach((element) => {
3898
+ element.addEventListener("pointerenter", onPointerEnter, eventOptions);
3899
+ });
3900
+ return cancel;
3901
+ }
3902
+
3903
+ /**
3904
+ * Recursively traverse up the tree to check whether the provided child node
3905
+ * is the parent or a descendant of it.
3906
+ *
3907
+ * @param parent - Element to find
3908
+ * @param child - Element to test against parent
3909
+ */
3910
+ const isNodeOrChild = (parent, child) => {
3911
+ if (!child) {
3912
+ return false;
3968
3913
  }
3969
- /**
3970
- * Set the state of the `MotionValue`, stopping any active animations,
3971
- * effects, and resets velocity to `0`.
3972
- */
3973
- jump(v, endAnimation = true) {
3974
- this.updateAndNotify(v);
3975
- this.prev = v;
3976
- this.prevUpdatedAt = this.prevFrameValue = undefined;
3977
- endAnimation && this.stop();
3978
- if (this.stopPassiveEffect)
3979
- this.stopPassiveEffect();
3914
+ else if (parent === child) {
3915
+ return true;
3980
3916
  }
3981
- /**
3982
- * Returns the latest state of `MotionValue`
3983
- *
3984
- * @returns - The latest state of `MotionValue`
3985
- *
3986
- * @public
3987
- */
3988
- get() {
3989
- if (collectMotionValues.current) {
3990
- collectMotionValues.current.push(this);
3991
- }
3992
- return this.current;
3917
+ else {
3918
+ return isNodeOrChild(parent, child.parentElement);
3993
3919
  }
3994
- /**
3995
- * @public
3996
- */
3997
- getPrevious() {
3998
- return this.prev;
3920
+ };
3921
+
3922
+ const isPrimaryPointer = (event) => {
3923
+ if (event.pointerType === "mouse") {
3924
+ return typeof event.button !== "number" || event.button <= 0;
3999
3925
  }
4000
- /**
4001
- * Returns the latest velocity of `MotionValue`
4002
- *
4003
- * @returns - The latest velocity of `MotionValue`. Returns `0` if the state is non-numerical.
4004
- *
4005
- * @public
4006
- */
4007
- getVelocity() {
4008
- const currentTime = time.now();
4009
- if (!this.canTrackVelocity ||
4010
- this.prevFrameValue === undefined ||
4011
- currentTime - this.updatedAt > MAX_VELOCITY_DELTA) {
4012
- return 0;
4013
- }
4014
- const delta = Math.min(this.updatedAt - this.prevUpdatedAt, MAX_VELOCITY_DELTA);
4015
- // Casts because of parseFloat's poor typing
4016
- return velocityPerSecond(parseFloat(this.current) -
4017
- parseFloat(this.prevFrameValue), delta);
3926
+ else {
3927
+ /**
3928
+ * isPrimary is true for all mice buttons, whereas every touch point
3929
+ * is regarded as its own input. So subsequent concurrent touch points
3930
+ * will be false.
3931
+ *
3932
+ * Specifically match against false here as incomplete versions of
3933
+ * PointerEvents in very old browser might have it set as undefined.
3934
+ */
3935
+ return event.isPrimary !== false;
4018
3936
  }
4019
- /**
4020
- * Registers a new animation to control this `MotionValue`. Only one
4021
- * animation can drive a `MotionValue` at one time.
4022
- *
4023
- * ```jsx
4024
- * value.start()
4025
- * ```
4026
- *
4027
- * @param animation - A function that starts the provided animation
4028
- */
4029
- start(startAnimation) {
4030
- this.stop();
4031
- return new Promise((resolve) => {
4032
- this.hasAnimated = true;
4033
- this.animation = startAnimation(resolve);
4034
- if (this.events.animationStart) {
4035
- this.events.animationStart.notify();
4036
- }
4037
- }).then(() => {
4038
- if (this.events.animationComplete) {
4039
- this.events.animationComplete.notify();
4040
- }
4041
- this.clearAnimation();
3937
+ };
3938
+
3939
+ const focusableElements = new Set([
3940
+ "BUTTON",
3941
+ "INPUT",
3942
+ "SELECT",
3943
+ "TEXTAREA",
3944
+ "A",
3945
+ ]);
3946
+ function isElementKeyboardAccessible(element) {
3947
+ return (focusableElements.has(element.tagName) ||
3948
+ element.tabIndex !== -1);
3949
+ }
3950
+
3951
+ const isPressing = new WeakSet();
3952
+
3953
+ /**
3954
+ * Filter out events that are not "Enter" keys.
3955
+ */
3956
+ function filterEvents(callback) {
3957
+ return (event) => {
3958
+ if (event.key !== "Enter")
3959
+ return;
3960
+ callback(event);
3961
+ };
3962
+ }
3963
+ function firePointerEvent(target, type) {
3964
+ target.dispatchEvent(new PointerEvent("pointer" + type, { isPrimary: true, bubbles: true }));
3965
+ }
3966
+ const enableKeyboardPress = (focusEvent, eventOptions) => {
3967
+ const element = focusEvent.currentTarget;
3968
+ if (!element)
3969
+ return;
3970
+ const handleKeydown = filterEvents(() => {
3971
+ if (isPressing.has(element))
3972
+ return;
3973
+ firePointerEvent(element, "down");
3974
+ const handleKeyup = filterEvents(() => {
3975
+ firePointerEvent(element, "up");
4042
3976
  });
4043
- }
3977
+ const handleBlur = () => firePointerEvent(element, "cancel");
3978
+ element.addEventListener("keyup", handleKeyup, eventOptions);
3979
+ element.addEventListener("blur", handleBlur, eventOptions);
3980
+ });
3981
+ element.addEventListener("keydown", handleKeydown, eventOptions);
4044
3982
  /**
4045
- * Stop the currently active animation.
4046
- *
4047
- * @public
3983
+ * Add an event listener that fires on blur to remove the keydown events.
4048
3984
  */
4049
- stop() {
4050
- if (this.animation) {
4051
- this.animation.stop();
4052
- if (this.events.animationCancel) {
4053
- this.events.animationCancel.notify();
3985
+ element.addEventListener("blur", () => element.removeEventListener("keydown", handleKeydown), eventOptions);
3986
+ };
3987
+
3988
+ /**
3989
+ * Filter out events that are not primary pointer events, or are triggering
3990
+ * while a Motion gesture is active.
3991
+ */
3992
+ function isValidPressEvent(event) {
3993
+ return isPrimaryPointer(event) && !isDragActive();
3994
+ }
3995
+ /**
3996
+ * Create a press gesture.
3997
+ *
3998
+ * Press is different to `"pointerdown"`, `"pointerup"` in that it
3999
+ * automatically filters out secondary pointer events like right
4000
+ * click and multitouch.
4001
+ *
4002
+ * It also adds accessibility support for keyboards, where
4003
+ * an element with a press gesture will receive focus and
4004
+ * trigger on Enter `"keydown"` and `"keyup"` events.
4005
+ *
4006
+ * This is different to a browser's `"click"` event, which does
4007
+ * respond to keyboards but only for the `"click"` itself, rather
4008
+ * than the press start and end/cancel. The element also needs
4009
+ * to be focusable for this to work, whereas a press gesture will
4010
+ * make an element focusable by default.
4011
+ *
4012
+ * @public
4013
+ */
4014
+ function press(targetOrSelector, onPressStart, options = {}) {
4015
+ const [targets, eventOptions, cancelEvents] = setupGesture(targetOrSelector, options);
4016
+ const startPress = (startEvent) => {
4017
+ const target = startEvent.currentTarget;
4018
+ if (!isValidPressEvent(startEvent) || isPressing.has(target))
4019
+ return;
4020
+ isPressing.add(target);
4021
+ const onPressEnd = onPressStart(target, startEvent);
4022
+ const onPointerEnd = (endEvent, success) => {
4023
+ window.removeEventListener("pointerup", onPointerUp);
4024
+ window.removeEventListener("pointercancel", onPointerCancel);
4025
+ if (isPressing.has(target)) {
4026
+ isPressing.delete(target);
4027
+ }
4028
+ if (!isValidPressEvent(endEvent)) {
4029
+ return;
4030
+ }
4031
+ if (typeof onPressEnd === "function") {
4032
+ onPressEnd(endEvent, { success });
4033
+ }
4034
+ };
4035
+ const onPointerUp = (upEvent) => {
4036
+ onPointerEnd(upEvent, target === window ||
4037
+ target === document ||
4038
+ options.useGlobalTarget ||
4039
+ isNodeOrChild(target, upEvent.target));
4040
+ };
4041
+ const onPointerCancel = (cancelEvent) => {
4042
+ onPointerEnd(cancelEvent, false);
4043
+ };
4044
+ window.addEventListener("pointerup", onPointerUp, eventOptions);
4045
+ window.addEventListener("pointercancel", onPointerCancel, eventOptions);
4046
+ };
4047
+ targets.forEach((target) => {
4048
+ const pointerDownTarget = options.useGlobalTarget ? window : target;
4049
+ pointerDownTarget.addEventListener("pointerdown", startPress, eventOptions);
4050
+ if (target instanceof HTMLElement) {
4051
+ target.addEventListener("focus", (event) => enableKeyboardPress(event, eventOptions));
4052
+ if (!isElementKeyboardAccessible(target) &&
4053
+ !target.hasAttribute("tabindex")) {
4054
+ target.tabIndex = 0;
4054
4055
  }
4055
4056
  }
4056
- this.clearAnimation();
4057
+ });
4058
+ return cancelEvents;
4059
+ }
4060
+
4061
+ function getComputedStyle$2(element, name) {
4062
+ const computedStyle = window.getComputedStyle(element);
4063
+ return isCSSVar(name)
4064
+ ? computedStyle.getPropertyValue(name)
4065
+ : computedStyle[name];
4066
+ }
4067
+
4068
+ function observeTimeline(update, timeline) {
4069
+ let prevProgress;
4070
+ const onFrame = () => {
4071
+ const { currentTime } = timeline;
4072
+ const percentage = currentTime === null ? 0 : currentTime.value;
4073
+ const progress = percentage / 100;
4074
+ if (prevProgress !== progress) {
4075
+ update(progress);
4076
+ }
4077
+ prevProgress = progress;
4078
+ };
4079
+ frame.preUpdate(onFrame, true);
4080
+ return () => cancelFrame(onFrame);
4081
+ }
4082
+
4083
+ function record() {
4084
+ const { value } = statsBuffer;
4085
+ if (value === null) {
4086
+ cancelFrame(record);
4087
+ return;
4057
4088
  }
4058
- /**
4059
- * Returns `true` if this value is currently animating.
4060
- *
4061
- * @public
4062
- */
4063
- isAnimating() {
4064
- return !!this.animation;
4089
+ value.frameloop.rate.push(frameData.delta);
4090
+ value.animations.mainThread.push(activeAnimations.mainThread);
4091
+ value.animations.waapi.push(activeAnimations.waapi);
4092
+ value.animations.layout.push(activeAnimations.layout);
4093
+ }
4094
+ function mean(values) {
4095
+ return values.reduce((acc, value) => acc + value, 0) / values.length;
4096
+ }
4097
+ function summarise(values, calcAverage = mean) {
4098
+ if (values.length === 0) {
4099
+ return {
4100
+ min: 0,
4101
+ max: 0,
4102
+ avg: 0,
4103
+ };
4065
4104
  }
4066
- clearAnimation() {
4067
- delete this.animation;
4105
+ return {
4106
+ min: Math.min(...values),
4107
+ max: Math.max(...values),
4108
+ avg: calcAverage(values),
4109
+ };
4110
+ }
4111
+ const msToFps = (ms) => Math.round(1000 / ms);
4112
+ function clearStatsBuffer() {
4113
+ statsBuffer.value = null;
4114
+ statsBuffer.addProjectionMetrics = null;
4115
+ }
4116
+ function reportStats() {
4117
+ const { value } = statsBuffer;
4118
+ if (!value) {
4119
+ throw new Error("Stats are not being measured");
4068
4120
  }
4121
+ clearStatsBuffer();
4122
+ cancelFrame(record);
4123
+ const summary = {
4124
+ frameloop: {
4125
+ setup: summarise(value.frameloop.setup),
4126
+ rate: summarise(value.frameloop.rate),
4127
+ read: summarise(value.frameloop.read),
4128
+ resolveKeyframes: summarise(value.frameloop.resolveKeyframes),
4129
+ preUpdate: summarise(value.frameloop.preUpdate),
4130
+ update: summarise(value.frameloop.update),
4131
+ preRender: summarise(value.frameloop.preRender),
4132
+ render: summarise(value.frameloop.render),
4133
+ postRender: summarise(value.frameloop.postRender),
4134
+ },
4135
+ animations: {
4136
+ mainThread: summarise(value.animations.mainThread),
4137
+ waapi: summarise(value.animations.waapi),
4138
+ layout: summarise(value.animations.layout),
4139
+ },
4140
+ layoutProjection: {
4141
+ nodes: summarise(value.layoutProjection.nodes),
4142
+ calculatedTargetDeltas: summarise(value.layoutProjection.calculatedTargetDeltas),
4143
+ calculatedProjections: summarise(value.layoutProjection.calculatedProjections),
4144
+ },
4145
+ };
4069
4146
  /**
4070
- * Destroy and clean up subscribers to this `MotionValue`.
4071
- *
4072
- * The `MotionValue` hooks like `useMotionValue` and `useTransform` automatically
4073
- * handle the lifecycle of the returned `MotionValue`, so this method is only necessary if you've manually
4074
- * created a `MotionValue` via the `motionValue` function.
4075
- *
4076
- * @public
4147
+ * Convert the rate to FPS
4077
4148
  */
4078
- destroy() {
4079
- this.events.destroy?.notify();
4080
- this.clearListeners();
4081
- this.stop();
4082
- if (this.stopPassiveEffect) {
4083
- this.stopPassiveEffect();
4084
- }
4149
+ const { rate } = summary.frameloop;
4150
+ rate.min = msToFps(rate.min);
4151
+ rate.max = msToFps(rate.max);
4152
+ rate.avg = msToFps(rate.avg);
4153
+ [rate.min, rate.max] = [rate.max, rate.min];
4154
+ return summary;
4155
+ }
4156
+ function recordStats() {
4157
+ if (statsBuffer.value) {
4158
+ clearStatsBuffer();
4159
+ throw new Error("Stats are already being measured");
4085
4160
  }
4161
+ const newStatsBuffer = statsBuffer;
4162
+ newStatsBuffer.value = {
4163
+ frameloop: {
4164
+ setup: [],
4165
+ rate: [],
4166
+ read: [],
4167
+ resolveKeyframes: [],
4168
+ preUpdate: [],
4169
+ update: [],
4170
+ preRender: [],
4171
+ render: [],
4172
+ postRender: [],
4173
+ },
4174
+ animations: {
4175
+ mainThread: [],
4176
+ waapi: [],
4177
+ layout: [],
4178
+ },
4179
+ layoutProjection: {
4180
+ nodes: [],
4181
+ calculatedTargetDeltas: [],
4182
+ calculatedProjections: [],
4183
+ },
4184
+ };
4185
+ newStatsBuffer.addProjectionMetrics = (metrics) => {
4186
+ const { layoutProjection } = newStatsBuffer.value;
4187
+ layoutProjection.nodes.push(metrics.nodes);
4188
+ layoutProjection.calculatedTargetDeltas.push(metrics.calculatedTargetDeltas);
4189
+ layoutProjection.calculatedProjections.push(metrics.calculatedProjections);
4190
+ };
4191
+ frame.postRender(record, true);
4192
+ return reportStats;
4086
4193
  }
4087
- function motionValue(init, options) {
4088
- return new MotionValue(init, options);
4194
+
4195
+ function transform(...args) {
4196
+ const useImmediate = !Array.isArray(args[0]);
4197
+ const argOffset = useImmediate ? 0 : -1;
4198
+ const inputValue = args[0 + argOffset];
4199
+ const inputRange = args[1 + argOffset];
4200
+ const outputRange = args[2 + argOffset];
4201
+ const options = args[3 + argOffset];
4202
+ const interpolator = interpolate(inputRange, outputRange, options);
4203
+ return useImmediate ? interpolator(inputValue) : interpolator;
4089
4204
  }
4090
4205
 
4091
4206
  function subscribeValue(inputValues, outputValue, getLatest) {
@@ -4180,15 +4295,6 @@
4180
4295
  */
4181
4296
  const findValueType = (v) => valueTypes.find(testValueType(v));
4182
4297
 
4183
- /**
4184
- * Provided a value and a ValueType, returns the value as that value type.
4185
- */
4186
- const getValueAsType = (value, type) => {
4187
- return type && typeof value === "number"
4188
- ? type.transform(value)
4189
- : value;
4190
- };
4191
-
4192
4298
  function chooseLayerType(valueName) {
4193
4299
  if (valueName === "layout")
4194
4300
  return "group";
@@ -6872,9 +6978,9 @@
6872
6978
  handler.notify();
6873
6979
  };
6874
6980
  const listener = () => {
6875
- frame.read(measureAll, false, true);
6876
- frame.read(updateAll, false, true);
6877
- frame.preUpdate(notifyAll, false, true);
6981
+ frame.read(measureAll);
6982
+ frame.read(updateAll);
6983
+ frame.preUpdate(notifyAll);
6878
6984
  };
6879
6985
  scrollListeners.set(container, listener);
6880
6986
  const target = getEventTarget(container);
@@ -6883,6 +6989,7 @@
6883
6989
  resizeListeners.set(container, resize(container, listener));
6884
6990
  }
6885
6991
  target.addEventListener("scroll", listener, { passive: true });
6992
+ listener();
6886
6993
  }
6887
6994
  const listener = scrollListeners.get(container);
6888
6995
  frame.read(listener, false, true);