motion-dom 12.9.6 → 12.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3072,420 +3072,6 @@
3072
3072
  return Array.from(elementOrSelector);
3073
3073
  }
3074
3074
 
3075
- function styleEffect(subject, values) {
3076
- const elements = resolveElements(subject);
3077
- const subscriptions = [];
3078
- for (let i = 0; i < elements.length; i++) {
3079
- const element = elements[i];
3080
- for (const key in values) {
3081
- const value = values[key];
3082
- /**
3083
- * TODO: Get specific setters for combined props (like x)
3084
- * or values with default types (like color)
3085
- *
3086
- * TODO: CSS variable support
3087
- */
3088
- const updateStyle = () => {
3089
- element.style[key] = value.get();
3090
- };
3091
- const scheduleUpdate = () => frame.render(updateStyle);
3092
- const cancel = value.on("change", scheduleUpdate);
3093
- scheduleUpdate();
3094
- subscriptions.push(() => {
3095
- cancel();
3096
- cancelFrame(updateStyle);
3097
- });
3098
- }
3099
- }
3100
- return () => {
3101
- for (const cancel of subscriptions) {
3102
- cancel();
3103
- }
3104
- };
3105
- }
3106
-
3107
- const { schedule: microtask, cancel: cancelMicrotask } =
3108
- /* @__PURE__ */ createRenderBatcher(queueMicrotask, false);
3109
-
3110
- const isDragging = {
3111
- x: false,
3112
- y: false,
3113
- };
3114
- function isDragActive() {
3115
- return isDragging.x || isDragging.y;
3116
- }
3117
-
3118
- function setDragLock(axis) {
3119
- if (axis === "x" || axis === "y") {
3120
- if (isDragging[axis]) {
3121
- return null;
3122
- }
3123
- else {
3124
- isDragging[axis] = true;
3125
- return () => {
3126
- isDragging[axis] = false;
3127
- };
3128
- }
3129
- }
3130
- else {
3131
- if (isDragging.x || isDragging.y) {
3132
- return null;
3133
- }
3134
- else {
3135
- isDragging.x = isDragging.y = true;
3136
- return () => {
3137
- isDragging.x = isDragging.y = false;
3138
- };
3139
- }
3140
- }
3141
- }
3142
-
3143
- function setupGesture(elementOrSelector, options) {
3144
- const elements = resolveElements(elementOrSelector);
3145
- const gestureAbortController = new AbortController();
3146
- const eventOptions = {
3147
- passive: true,
3148
- ...options,
3149
- signal: gestureAbortController.signal,
3150
- };
3151
- const cancel = () => gestureAbortController.abort();
3152
- return [elements, eventOptions, cancel];
3153
- }
3154
-
3155
- function isValidHover(event) {
3156
- return !(event.pointerType === "touch" || isDragActive());
3157
- }
3158
- /**
3159
- * Create a hover gesture. hover() is different to .addEventListener("pointerenter")
3160
- * in that it has an easier syntax, filters out polyfilled touch events, interoperates
3161
- * with drag gestures, and automatically removes the "pointerennd" event listener when the hover ends.
3162
- *
3163
- * @public
3164
- */
3165
- function hover(elementOrSelector, onHoverStart, options = {}) {
3166
- const [elements, eventOptions, cancel] = setupGesture(elementOrSelector, options);
3167
- const onPointerEnter = (enterEvent) => {
3168
- if (!isValidHover(enterEvent))
3169
- return;
3170
- const { target } = enterEvent;
3171
- const onHoverEnd = onHoverStart(target, enterEvent);
3172
- if (typeof onHoverEnd !== "function" || !target)
3173
- return;
3174
- const onPointerLeave = (leaveEvent) => {
3175
- if (!isValidHover(leaveEvent))
3176
- return;
3177
- onHoverEnd(leaveEvent);
3178
- target.removeEventListener("pointerleave", onPointerLeave);
3179
- };
3180
- target.addEventListener("pointerleave", onPointerLeave, eventOptions);
3181
- };
3182
- elements.forEach((element) => {
3183
- element.addEventListener("pointerenter", onPointerEnter, eventOptions);
3184
- });
3185
- return cancel;
3186
- }
3187
-
3188
- /**
3189
- * Recursively traverse up the tree to check whether the provided child node
3190
- * is the parent or a descendant of it.
3191
- *
3192
- * @param parent - Element to find
3193
- * @param child - Element to test against parent
3194
- */
3195
- const isNodeOrChild = (parent, child) => {
3196
- if (!child) {
3197
- return false;
3198
- }
3199
- else if (parent === child) {
3200
- return true;
3201
- }
3202
- else {
3203
- return isNodeOrChild(parent, child.parentElement);
3204
- }
3205
- };
3206
-
3207
- const isPrimaryPointer = (event) => {
3208
- if (event.pointerType === "mouse") {
3209
- return typeof event.button !== "number" || event.button <= 0;
3210
- }
3211
- else {
3212
- /**
3213
- * isPrimary is true for all mice buttons, whereas every touch point
3214
- * is regarded as its own input. So subsequent concurrent touch points
3215
- * will be false.
3216
- *
3217
- * Specifically match against false here as incomplete versions of
3218
- * PointerEvents in very old browser might have it set as undefined.
3219
- */
3220
- return event.isPrimary !== false;
3221
- }
3222
- };
3223
-
3224
- const focusableElements = new Set([
3225
- "BUTTON",
3226
- "INPUT",
3227
- "SELECT",
3228
- "TEXTAREA",
3229
- "A",
3230
- ]);
3231
- function isElementKeyboardAccessible(element) {
3232
- return (focusableElements.has(element.tagName) ||
3233
- element.tabIndex !== -1);
3234
- }
3235
-
3236
- const isPressing = new WeakSet();
3237
-
3238
- /**
3239
- * Filter out events that are not "Enter" keys.
3240
- */
3241
- function filterEvents(callback) {
3242
- return (event) => {
3243
- if (event.key !== "Enter")
3244
- return;
3245
- callback(event);
3246
- };
3247
- }
3248
- function firePointerEvent(target, type) {
3249
- target.dispatchEvent(new PointerEvent("pointer" + type, { isPrimary: true, bubbles: true }));
3250
- }
3251
- const enableKeyboardPress = (focusEvent, eventOptions) => {
3252
- const element = focusEvent.currentTarget;
3253
- if (!element)
3254
- return;
3255
- const handleKeydown = filterEvents(() => {
3256
- if (isPressing.has(element))
3257
- return;
3258
- firePointerEvent(element, "down");
3259
- const handleKeyup = filterEvents(() => {
3260
- firePointerEvent(element, "up");
3261
- });
3262
- const handleBlur = () => firePointerEvent(element, "cancel");
3263
- element.addEventListener("keyup", handleKeyup, eventOptions);
3264
- element.addEventListener("blur", handleBlur, eventOptions);
3265
- });
3266
- element.addEventListener("keydown", handleKeydown, eventOptions);
3267
- /**
3268
- * Add an event listener that fires on blur to remove the keydown events.
3269
- */
3270
- element.addEventListener("blur", () => element.removeEventListener("keydown", handleKeydown), eventOptions);
3271
- };
3272
-
3273
- /**
3274
- * Filter out events that are not primary pointer events, or are triggering
3275
- * while a Motion gesture is active.
3276
- */
3277
- function isValidPressEvent(event) {
3278
- return isPrimaryPointer(event) && !isDragActive();
3279
- }
3280
- /**
3281
- * Create a press gesture.
3282
- *
3283
- * Press is different to `"pointerdown"`, `"pointerup"` in that it
3284
- * automatically filters out secondary pointer events like right
3285
- * click and multitouch.
3286
- *
3287
- * It also adds accessibility support for keyboards, where
3288
- * an element with a press gesture will receive focus and
3289
- * trigger on Enter `"keydown"` and `"keyup"` events.
3290
- *
3291
- * This is different to a browser's `"click"` event, which does
3292
- * respond to keyboards but only for the `"click"` itself, rather
3293
- * than the press start and end/cancel. The element also needs
3294
- * to be focusable for this to work, whereas a press gesture will
3295
- * make an element focusable by default.
3296
- *
3297
- * @public
3298
- */
3299
- function press(targetOrSelector, onPressStart, options = {}) {
3300
- const [targets, eventOptions, cancelEvents] = setupGesture(targetOrSelector, options);
3301
- const startPress = (startEvent) => {
3302
- const target = startEvent.currentTarget;
3303
- if (!isValidPressEvent(startEvent) || isPressing.has(target))
3304
- return;
3305
- isPressing.add(target);
3306
- const onPressEnd = onPressStart(target, startEvent);
3307
- const onPointerEnd = (endEvent, success) => {
3308
- window.removeEventListener("pointerup", onPointerUp);
3309
- window.removeEventListener("pointercancel", onPointerCancel);
3310
- if (!isValidPressEvent(endEvent) || !isPressing.has(target)) {
3311
- return;
3312
- }
3313
- isPressing.delete(target);
3314
- if (typeof onPressEnd === "function") {
3315
- onPressEnd(endEvent, { success });
3316
- }
3317
- };
3318
- const onPointerUp = (upEvent) => {
3319
- onPointerEnd(upEvent, target === window ||
3320
- target === document ||
3321
- options.useGlobalTarget ||
3322
- isNodeOrChild(target, upEvent.target));
3323
- };
3324
- const onPointerCancel = (cancelEvent) => {
3325
- onPointerEnd(cancelEvent, false);
3326
- };
3327
- window.addEventListener("pointerup", onPointerUp, eventOptions);
3328
- window.addEventListener("pointercancel", onPointerCancel, eventOptions);
3329
- };
3330
- targets.forEach((target) => {
3331
- const pointerDownTarget = options.useGlobalTarget ? window : target;
3332
- pointerDownTarget.addEventListener("pointerdown", startPress, eventOptions);
3333
- if (target instanceof HTMLElement) {
3334
- target.addEventListener("focus", (event) => enableKeyboardPress(event, eventOptions));
3335
- if (!isElementKeyboardAccessible(target) &&
3336
- !target.hasAttribute("tabindex")) {
3337
- target.tabIndex = 0;
3338
- }
3339
- }
3340
- });
3341
- return cancelEvents;
3342
- }
3343
-
3344
- function getComputedStyle$1(element, name) {
3345
- const computedStyle = window.getComputedStyle(element);
3346
- return isCSSVar(name)
3347
- ? computedStyle.getPropertyValue(name)
3348
- : computedStyle[name];
3349
- }
3350
-
3351
- function observeTimeline(update, timeline) {
3352
- let prevProgress;
3353
- const onFrame = () => {
3354
- const { currentTime } = timeline;
3355
- const percentage = currentTime === null ? 0 : currentTime.value;
3356
- const progress = percentage / 100;
3357
- if (prevProgress !== progress) {
3358
- update(progress);
3359
- }
3360
- prevProgress = progress;
3361
- };
3362
- frame.preUpdate(onFrame, true);
3363
- return () => cancelFrame(onFrame);
3364
- }
3365
-
3366
- function record() {
3367
- const { value } = statsBuffer;
3368
- if (value === null) {
3369
- cancelFrame(record);
3370
- return;
3371
- }
3372
- value.frameloop.rate.push(frameData.delta);
3373
- value.animations.mainThread.push(activeAnimations.mainThread);
3374
- value.animations.waapi.push(activeAnimations.waapi);
3375
- value.animations.layout.push(activeAnimations.layout);
3376
- }
3377
- function mean(values) {
3378
- return values.reduce((acc, value) => acc + value, 0) / values.length;
3379
- }
3380
- function summarise(values, calcAverage = mean) {
3381
- if (values.length === 0) {
3382
- return {
3383
- min: 0,
3384
- max: 0,
3385
- avg: 0,
3386
- };
3387
- }
3388
- return {
3389
- min: Math.min(...values),
3390
- max: Math.max(...values),
3391
- avg: calcAverage(values),
3392
- };
3393
- }
3394
- const msToFps = (ms) => Math.round(1000 / ms);
3395
- function clearStatsBuffer() {
3396
- statsBuffer.value = null;
3397
- statsBuffer.addProjectionMetrics = null;
3398
- }
3399
- function reportStats() {
3400
- const { value } = statsBuffer;
3401
- if (!value) {
3402
- throw new Error("Stats are not being measured");
3403
- }
3404
- clearStatsBuffer();
3405
- cancelFrame(record);
3406
- const summary = {
3407
- frameloop: {
3408
- setup: summarise(value.frameloop.setup),
3409
- rate: summarise(value.frameloop.rate),
3410
- read: summarise(value.frameloop.read),
3411
- resolveKeyframes: summarise(value.frameloop.resolveKeyframes),
3412
- preUpdate: summarise(value.frameloop.preUpdate),
3413
- update: summarise(value.frameloop.update),
3414
- preRender: summarise(value.frameloop.preRender),
3415
- render: summarise(value.frameloop.render),
3416
- postRender: summarise(value.frameloop.postRender),
3417
- },
3418
- animations: {
3419
- mainThread: summarise(value.animations.mainThread),
3420
- waapi: summarise(value.animations.waapi),
3421
- layout: summarise(value.animations.layout),
3422
- },
3423
- layoutProjection: {
3424
- nodes: summarise(value.layoutProjection.nodes),
3425
- calculatedTargetDeltas: summarise(value.layoutProjection.calculatedTargetDeltas),
3426
- calculatedProjections: summarise(value.layoutProjection.calculatedProjections),
3427
- },
3428
- };
3429
- /**
3430
- * Convert the rate to FPS
3431
- */
3432
- const { rate } = summary.frameloop;
3433
- rate.min = msToFps(rate.min);
3434
- rate.max = msToFps(rate.max);
3435
- rate.avg = msToFps(rate.avg);
3436
- [rate.min, rate.max] = [rate.max, rate.min];
3437
- return summary;
3438
- }
3439
- function recordStats() {
3440
- if (statsBuffer.value) {
3441
- clearStatsBuffer();
3442
- throw new Error("Stats are already being measured");
3443
- }
3444
- const newStatsBuffer = statsBuffer;
3445
- newStatsBuffer.value = {
3446
- frameloop: {
3447
- setup: [],
3448
- rate: [],
3449
- read: [],
3450
- resolveKeyframes: [],
3451
- preUpdate: [],
3452
- update: [],
3453
- preRender: [],
3454
- render: [],
3455
- postRender: [],
3456
- },
3457
- animations: {
3458
- mainThread: [],
3459
- waapi: [],
3460
- layout: [],
3461
- },
3462
- layoutProjection: {
3463
- nodes: [],
3464
- calculatedTargetDeltas: [],
3465
- calculatedProjections: [],
3466
- },
3467
- };
3468
- newStatsBuffer.addProjectionMetrics = (metrics) => {
3469
- const { layoutProjection } = newStatsBuffer.value;
3470
- layoutProjection.nodes.push(metrics.nodes);
3471
- layoutProjection.calculatedTargetDeltas.push(metrics.calculatedTargetDeltas);
3472
- layoutProjection.calculatedProjections.push(metrics.calculatedProjections);
3473
- };
3474
- frame.postRender(record, true);
3475
- return reportStats;
3476
- }
3477
-
3478
- function transform(...args) {
3479
- const useImmediate = !Array.isArray(args[0]);
3480
- const argOffset = useImmediate ? 0 : -1;
3481
- const inputValue = args[0 + argOffset];
3482
- const inputRange = args[1 + argOffset];
3483
- const outputRange = args[2 + argOffset];
3484
- const options = args[3 + argOffset];
3485
- const interpolator = interpolate(inputRange, outputRange, options);
3486
- return useImmediate ? interpolator(inputValue) : interpolator;
3487
- }
3488
-
3489
3075
  /**
3490
3076
  * Maximum time between the value of two frames, beyond which we
3491
3077
  * assume the velocity has since been 0.
@@ -3542,6 +3128,11 @@
3542
3128
  // Update update subscribers
3543
3129
  if (this.current !== this.prev) {
3544
3130
  this.events.change?.notify(this.current);
3131
+ if (this.dependents) {
3132
+ for (const dependent of this.dependents) {
3133
+ dependent.dirty();
3134
+ }
3135
+ }
3545
3136
  }
3546
3137
  // Update render subscribers
3547
3138
  if (render) {
@@ -3683,6 +3274,20 @@
3683
3274
  if (this.stopPassiveEffect)
3684
3275
  this.stopPassiveEffect();
3685
3276
  }
3277
+ dirty() {
3278
+ this.events.change?.notify(this.current);
3279
+ }
3280
+ addDependent(dependent) {
3281
+ if (!this.dependents) {
3282
+ this.dependents = new Set();
3283
+ }
3284
+ this.dependents.add(dependent);
3285
+ }
3286
+ removeDependent(dependent) {
3287
+ if (this.dependents) {
3288
+ this.dependents.delete(dependent);
3289
+ }
3290
+ }
3686
3291
  /**
3687
3292
  * Returns the latest state of `MotionValue`
3688
3293
  *
@@ -3747,50 +3352,558 @@
3747
3352
  });
3748
3353
  }
3749
3354
  /**
3750
- * Stop the currently active animation.
3751
- *
3752
- * @public
3355
+ * Stop the currently active animation.
3356
+ *
3357
+ * @public
3358
+ */
3359
+ stop() {
3360
+ if (this.animation) {
3361
+ this.animation.stop();
3362
+ if (this.events.animationCancel) {
3363
+ this.events.animationCancel.notify();
3364
+ }
3365
+ }
3366
+ this.clearAnimation();
3367
+ }
3368
+ /**
3369
+ * Returns `true` if this value is currently animating.
3370
+ *
3371
+ * @public
3372
+ */
3373
+ isAnimating() {
3374
+ return !!this.animation;
3375
+ }
3376
+ clearAnimation() {
3377
+ delete this.animation;
3378
+ }
3379
+ /**
3380
+ * Destroy and clean up subscribers to this `MotionValue`.
3381
+ *
3382
+ * The `MotionValue` hooks like `useMotionValue` and `useTransform` automatically
3383
+ * handle the lifecycle of the returned `MotionValue`, so this method is only necessary if you've manually
3384
+ * created a `MotionValue` via the `motionValue` function.
3385
+ *
3386
+ * @public
3387
+ */
3388
+ destroy() {
3389
+ this.dependents?.clear();
3390
+ this.events.destroy?.notify();
3391
+ this.clearListeners();
3392
+ this.stop();
3393
+ if (this.stopPassiveEffect) {
3394
+ this.stopPassiveEffect();
3395
+ }
3396
+ }
3397
+ }
3398
+ function motionValue(init, options) {
3399
+ return new MotionValue(init, options);
3400
+ }
3401
+
3402
+ /**
3403
+ * Provided a value and a ValueType, returns the value as that value type.
3404
+ */
3405
+ const getValueAsType = (value, type) => {
3406
+ return type && typeof value === "number"
3407
+ ? type.transform(value)
3408
+ : value;
3409
+ };
3410
+
3411
+ class MotionValueState {
3412
+ constructor() {
3413
+ this.latest = {};
3414
+ this.values = new Map();
3415
+ }
3416
+ set(name, value, render, computed) {
3417
+ const existingValue = this.values.get(name);
3418
+ if (existingValue) {
3419
+ existingValue.onRemove();
3420
+ }
3421
+ const onChange = () => {
3422
+ this.latest[name] = getValueAsType(value.get(), numberValueTypes[name]);
3423
+ render && frame.render(render);
3424
+ };
3425
+ onChange();
3426
+ const cancelOnChange = value.on("change", onChange);
3427
+ computed && value.addDependent(computed);
3428
+ const remove = () => {
3429
+ cancelOnChange();
3430
+ render && cancelFrame(render);
3431
+ this.values.delete(name);
3432
+ computed && value.removeDependent(computed);
3433
+ };
3434
+ this.values.set(name, { value, onRemove: remove });
3435
+ return remove;
3436
+ }
3437
+ get(name) {
3438
+ return this.values.get(name)?.value;
3439
+ }
3440
+ destroy() {
3441
+ for (const value of this.values.values()) {
3442
+ value.onRemove();
3443
+ }
3444
+ }
3445
+ }
3446
+
3447
+ const translateAlias = {
3448
+ x: "translateX",
3449
+ y: "translateY",
3450
+ z: "translateZ",
3451
+ transformPerspective: "perspective",
3452
+ };
3453
+ function buildTransform(state) {
3454
+ let transform = "";
3455
+ let transformIsDefault = true;
3456
+ /**
3457
+ * Loop over all possible transforms in order, adding the ones that
3458
+ * are present to the transform string.
3459
+ */
3460
+ for (let i = 0; i < transformPropOrder.length; i++) {
3461
+ const key = transformPropOrder[i];
3462
+ const value = state.latest[key];
3463
+ if (value === undefined)
3464
+ continue;
3465
+ let valueIsDefault = true;
3466
+ if (typeof value === "number") {
3467
+ valueIsDefault = value === (key.startsWith("scale") ? 1 : 0);
3468
+ }
3469
+ else {
3470
+ valueIsDefault = parseFloat(value) === 0;
3471
+ }
3472
+ if (!valueIsDefault) {
3473
+ transformIsDefault = false;
3474
+ const transformName = translateAlias[key] || key;
3475
+ const valueToRender = state.latest[key];
3476
+ transform += `${transformName}(${valueToRender}) `;
3477
+ }
3478
+ }
3479
+ return transformIsDefault ? "none" : transform.trim();
3480
+ }
3481
+
3482
+ const stateMap = new WeakMap();
3483
+ function styleEffect(subject, values) {
3484
+ const elements = resolveElements(subject);
3485
+ const subscriptions = [];
3486
+ for (let i = 0; i < elements.length; i++) {
3487
+ const element = elements[i];
3488
+ const state = stateMap.get(element) ?? new MotionValueState();
3489
+ stateMap.set(element, state);
3490
+ for (const key in values) {
3491
+ const value = values[key];
3492
+ const remove = addValue(element, state, key, value);
3493
+ subscriptions.push(remove);
3494
+ }
3495
+ }
3496
+ return () => {
3497
+ for (const cancel of subscriptions)
3498
+ cancel();
3499
+ };
3500
+ }
3501
+ function addValue(element, state, key, value) {
3502
+ let render = undefined;
3503
+ let computed = undefined;
3504
+ if (transformProps.has(key)) {
3505
+ if (!state.get("transform")) {
3506
+ state.set("transform", new MotionValue("none"), () => {
3507
+ element.style.transform = buildTransform(state);
3508
+ });
3509
+ }
3510
+ computed = state.get("transform");
3511
+ }
3512
+ else if (isCSSVar(key)) {
3513
+ render = () => {
3514
+ element.style.setProperty(key, state.latest[key]);
3515
+ };
3516
+ }
3517
+ else {
3518
+ render = () => {
3519
+ element.style[key] = state.latest[key];
3520
+ };
3521
+ }
3522
+ return state.set(key, value, render, computed);
3523
+ }
3524
+
3525
+ const { schedule: microtask, cancel: cancelMicrotask } =
3526
+ /* @__PURE__ */ createRenderBatcher(queueMicrotask, false);
3527
+
3528
+ const isDragging = {
3529
+ x: false,
3530
+ y: false,
3531
+ };
3532
+ function isDragActive() {
3533
+ return isDragging.x || isDragging.y;
3534
+ }
3535
+
3536
+ function setDragLock(axis) {
3537
+ if (axis === "x" || axis === "y") {
3538
+ if (isDragging[axis]) {
3539
+ return null;
3540
+ }
3541
+ else {
3542
+ isDragging[axis] = true;
3543
+ return () => {
3544
+ isDragging[axis] = false;
3545
+ };
3546
+ }
3547
+ }
3548
+ else {
3549
+ if (isDragging.x || isDragging.y) {
3550
+ return null;
3551
+ }
3552
+ else {
3553
+ isDragging.x = isDragging.y = true;
3554
+ return () => {
3555
+ isDragging.x = isDragging.y = false;
3556
+ };
3557
+ }
3558
+ }
3559
+ }
3560
+
3561
+ function setupGesture(elementOrSelector, options) {
3562
+ const elements = resolveElements(elementOrSelector);
3563
+ const gestureAbortController = new AbortController();
3564
+ const eventOptions = {
3565
+ passive: true,
3566
+ ...options,
3567
+ signal: gestureAbortController.signal,
3568
+ };
3569
+ const cancel = () => gestureAbortController.abort();
3570
+ return [elements, eventOptions, cancel];
3571
+ }
3572
+
3573
+ function isValidHover(event) {
3574
+ return !(event.pointerType === "touch" || isDragActive());
3575
+ }
3576
+ /**
3577
+ * Create a hover gesture. hover() is different to .addEventListener("pointerenter")
3578
+ * in that it has an easier syntax, filters out polyfilled touch events, interoperates
3579
+ * with drag gestures, and automatically removes the "pointerennd" event listener when the hover ends.
3580
+ *
3581
+ * @public
3582
+ */
3583
+ function hover(elementOrSelector, onHoverStart, options = {}) {
3584
+ const [elements, eventOptions, cancel] = setupGesture(elementOrSelector, options);
3585
+ const onPointerEnter = (enterEvent) => {
3586
+ if (!isValidHover(enterEvent))
3587
+ return;
3588
+ const { target } = enterEvent;
3589
+ const onHoverEnd = onHoverStart(target, enterEvent);
3590
+ if (typeof onHoverEnd !== "function" || !target)
3591
+ return;
3592
+ const onPointerLeave = (leaveEvent) => {
3593
+ if (!isValidHover(leaveEvent))
3594
+ return;
3595
+ onHoverEnd(leaveEvent);
3596
+ target.removeEventListener("pointerleave", onPointerLeave);
3597
+ };
3598
+ target.addEventListener("pointerleave", onPointerLeave, eventOptions);
3599
+ };
3600
+ elements.forEach((element) => {
3601
+ element.addEventListener("pointerenter", onPointerEnter, eventOptions);
3602
+ });
3603
+ return cancel;
3604
+ }
3605
+
3606
+ /**
3607
+ * Recursively traverse up the tree to check whether the provided child node
3608
+ * is the parent or a descendant of it.
3609
+ *
3610
+ * @param parent - Element to find
3611
+ * @param child - Element to test against parent
3612
+ */
3613
+ const isNodeOrChild = (parent, child) => {
3614
+ if (!child) {
3615
+ return false;
3616
+ }
3617
+ else if (parent === child) {
3618
+ return true;
3619
+ }
3620
+ else {
3621
+ return isNodeOrChild(parent, child.parentElement);
3622
+ }
3623
+ };
3624
+
3625
+ const isPrimaryPointer = (event) => {
3626
+ if (event.pointerType === "mouse") {
3627
+ return typeof event.button !== "number" || event.button <= 0;
3628
+ }
3629
+ else {
3630
+ /**
3631
+ * isPrimary is true for all mice buttons, whereas every touch point
3632
+ * is regarded as its own input. So subsequent concurrent touch points
3633
+ * will be false.
3634
+ *
3635
+ * Specifically match against false here as incomplete versions of
3636
+ * PointerEvents in very old browser might have it set as undefined.
3637
+ */
3638
+ return event.isPrimary !== false;
3639
+ }
3640
+ };
3641
+
3642
+ const focusableElements = new Set([
3643
+ "BUTTON",
3644
+ "INPUT",
3645
+ "SELECT",
3646
+ "TEXTAREA",
3647
+ "A",
3648
+ ]);
3649
+ function isElementKeyboardAccessible(element) {
3650
+ return (focusableElements.has(element.tagName) ||
3651
+ element.tabIndex !== -1);
3652
+ }
3653
+
3654
+ const isPressing = new WeakSet();
3655
+
3656
+ /**
3657
+ * Filter out events that are not "Enter" keys.
3658
+ */
3659
+ function filterEvents(callback) {
3660
+ return (event) => {
3661
+ if (event.key !== "Enter")
3662
+ return;
3663
+ callback(event);
3664
+ };
3665
+ }
3666
+ function firePointerEvent(target, type) {
3667
+ target.dispatchEvent(new PointerEvent("pointer" + type, { isPrimary: true, bubbles: true }));
3668
+ }
3669
+ const enableKeyboardPress = (focusEvent, eventOptions) => {
3670
+ const element = focusEvent.currentTarget;
3671
+ if (!element)
3672
+ return;
3673
+ const handleKeydown = filterEvents(() => {
3674
+ if (isPressing.has(element))
3675
+ return;
3676
+ firePointerEvent(element, "down");
3677
+ const handleKeyup = filterEvents(() => {
3678
+ firePointerEvent(element, "up");
3679
+ });
3680
+ const handleBlur = () => firePointerEvent(element, "cancel");
3681
+ element.addEventListener("keyup", handleKeyup, eventOptions);
3682
+ element.addEventListener("blur", handleBlur, eventOptions);
3683
+ });
3684
+ element.addEventListener("keydown", handleKeydown, eventOptions);
3685
+ /**
3686
+ * Add an event listener that fires on blur to remove the keydown events.
3753
3687
  */
3754
- stop() {
3755
- if (this.animation) {
3756
- this.animation.stop();
3757
- if (this.events.animationCancel) {
3758
- this.events.animationCancel.notify();
3688
+ element.addEventListener("blur", () => element.removeEventListener("keydown", handleKeydown), eventOptions);
3689
+ };
3690
+
3691
+ /**
3692
+ * Filter out events that are not primary pointer events, or are triggering
3693
+ * while a Motion gesture is active.
3694
+ */
3695
+ function isValidPressEvent(event) {
3696
+ return isPrimaryPointer(event) && !isDragActive();
3697
+ }
3698
+ /**
3699
+ * Create a press gesture.
3700
+ *
3701
+ * Press is different to `"pointerdown"`, `"pointerup"` in that it
3702
+ * automatically filters out secondary pointer events like right
3703
+ * click and multitouch.
3704
+ *
3705
+ * It also adds accessibility support for keyboards, where
3706
+ * an element with a press gesture will receive focus and
3707
+ * trigger on Enter `"keydown"` and `"keyup"` events.
3708
+ *
3709
+ * This is different to a browser's `"click"` event, which does
3710
+ * respond to keyboards but only for the `"click"` itself, rather
3711
+ * than the press start and end/cancel. The element also needs
3712
+ * to be focusable for this to work, whereas a press gesture will
3713
+ * make an element focusable by default.
3714
+ *
3715
+ * @public
3716
+ */
3717
+ function press(targetOrSelector, onPressStart, options = {}) {
3718
+ const [targets, eventOptions, cancelEvents] = setupGesture(targetOrSelector, options);
3719
+ const startPress = (startEvent) => {
3720
+ const target = startEvent.currentTarget;
3721
+ if (!isValidPressEvent(startEvent) || isPressing.has(target))
3722
+ return;
3723
+ isPressing.add(target);
3724
+ const onPressEnd = onPressStart(target, startEvent);
3725
+ const onPointerEnd = (endEvent, success) => {
3726
+ window.removeEventListener("pointerup", onPointerUp);
3727
+ window.removeEventListener("pointercancel", onPointerCancel);
3728
+ if (isPressing.has(target)) {
3729
+ isPressing.delete(target);
3730
+ }
3731
+ if (!isValidPressEvent(endEvent)) {
3732
+ return;
3733
+ }
3734
+ if (typeof onPressEnd === "function") {
3735
+ onPressEnd(endEvent, { success });
3736
+ }
3737
+ };
3738
+ const onPointerUp = (upEvent) => {
3739
+ onPointerEnd(upEvent, target === window ||
3740
+ target === document ||
3741
+ options.useGlobalTarget ||
3742
+ isNodeOrChild(target, upEvent.target));
3743
+ };
3744
+ const onPointerCancel = (cancelEvent) => {
3745
+ onPointerEnd(cancelEvent, false);
3746
+ };
3747
+ window.addEventListener("pointerup", onPointerUp, eventOptions);
3748
+ window.addEventListener("pointercancel", onPointerCancel, eventOptions);
3749
+ };
3750
+ targets.forEach((target) => {
3751
+ const pointerDownTarget = options.useGlobalTarget ? window : target;
3752
+ pointerDownTarget.addEventListener("pointerdown", startPress, eventOptions);
3753
+ if (target instanceof HTMLElement) {
3754
+ target.addEventListener("focus", (event) => enableKeyboardPress(event, eventOptions));
3755
+ if (!isElementKeyboardAccessible(target) &&
3756
+ !target.hasAttribute("tabindex")) {
3757
+ target.tabIndex = 0;
3759
3758
  }
3760
3759
  }
3761
- this.clearAnimation();
3760
+ });
3761
+ return cancelEvents;
3762
+ }
3763
+
3764
+ function getComputedStyle$1(element, name) {
3765
+ const computedStyle = window.getComputedStyle(element);
3766
+ return isCSSVar(name)
3767
+ ? computedStyle.getPropertyValue(name)
3768
+ : computedStyle[name];
3769
+ }
3770
+
3771
+ function observeTimeline(update, timeline) {
3772
+ let prevProgress;
3773
+ const onFrame = () => {
3774
+ const { currentTime } = timeline;
3775
+ const percentage = currentTime === null ? 0 : currentTime.value;
3776
+ const progress = percentage / 100;
3777
+ if (prevProgress !== progress) {
3778
+ update(progress);
3779
+ }
3780
+ prevProgress = progress;
3781
+ };
3782
+ frame.preUpdate(onFrame, true);
3783
+ return () => cancelFrame(onFrame);
3784
+ }
3785
+
3786
+ function record() {
3787
+ const { value } = statsBuffer;
3788
+ if (value === null) {
3789
+ cancelFrame(record);
3790
+ return;
3762
3791
  }
3763
- /**
3764
- * Returns `true` if this value is currently animating.
3765
- *
3766
- * @public
3767
- */
3768
- isAnimating() {
3769
- return !!this.animation;
3792
+ value.frameloop.rate.push(frameData.delta);
3793
+ value.animations.mainThread.push(activeAnimations.mainThread);
3794
+ value.animations.waapi.push(activeAnimations.waapi);
3795
+ value.animations.layout.push(activeAnimations.layout);
3796
+ }
3797
+ function mean(values) {
3798
+ return values.reduce((acc, value) => acc + value, 0) / values.length;
3799
+ }
3800
+ function summarise(values, calcAverage = mean) {
3801
+ if (values.length === 0) {
3802
+ return {
3803
+ min: 0,
3804
+ max: 0,
3805
+ avg: 0,
3806
+ };
3770
3807
  }
3771
- clearAnimation() {
3772
- delete this.animation;
3808
+ return {
3809
+ min: Math.min(...values),
3810
+ max: Math.max(...values),
3811
+ avg: calcAverage(values),
3812
+ };
3813
+ }
3814
+ const msToFps = (ms) => Math.round(1000 / ms);
3815
+ function clearStatsBuffer() {
3816
+ statsBuffer.value = null;
3817
+ statsBuffer.addProjectionMetrics = null;
3818
+ }
3819
+ function reportStats() {
3820
+ const { value } = statsBuffer;
3821
+ if (!value) {
3822
+ throw new Error("Stats are not being measured");
3773
3823
  }
3824
+ clearStatsBuffer();
3825
+ cancelFrame(record);
3826
+ const summary = {
3827
+ frameloop: {
3828
+ setup: summarise(value.frameloop.setup),
3829
+ rate: summarise(value.frameloop.rate),
3830
+ read: summarise(value.frameloop.read),
3831
+ resolveKeyframes: summarise(value.frameloop.resolveKeyframes),
3832
+ preUpdate: summarise(value.frameloop.preUpdate),
3833
+ update: summarise(value.frameloop.update),
3834
+ preRender: summarise(value.frameloop.preRender),
3835
+ render: summarise(value.frameloop.render),
3836
+ postRender: summarise(value.frameloop.postRender),
3837
+ },
3838
+ animations: {
3839
+ mainThread: summarise(value.animations.mainThread),
3840
+ waapi: summarise(value.animations.waapi),
3841
+ layout: summarise(value.animations.layout),
3842
+ },
3843
+ layoutProjection: {
3844
+ nodes: summarise(value.layoutProjection.nodes),
3845
+ calculatedTargetDeltas: summarise(value.layoutProjection.calculatedTargetDeltas),
3846
+ calculatedProjections: summarise(value.layoutProjection.calculatedProjections),
3847
+ },
3848
+ };
3774
3849
  /**
3775
- * Destroy and clean up subscribers to this `MotionValue`.
3776
- *
3777
- * The `MotionValue` hooks like `useMotionValue` and `useTransform` automatically
3778
- * handle the lifecycle of the returned `MotionValue`, so this method is only necessary if you've manually
3779
- * created a `MotionValue` via the `motionValue` function.
3780
- *
3781
- * @public
3850
+ * Convert the rate to FPS
3782
3851
  */
3783
- destroy() {
3784
- this.events.destroy?.notify();
3785
- this.clearListeners();
3786
- this.stop();
3787
- if (this.stopPassiveEffect) {
3788
- this.stopPassiveEffect();
3789
- }
3852
+ const { rate } = summary.frameloop;
3853
+ rate.min = msToFps(rate.min);
3854
+ rate.max = msToFps(rate.max);
3855
+ rate.avg = msToFps(rate.avg);
3856
+ [rate.min, rate.max] = [rate.max, rate.min];
3857
+ return summary;
3858
+ }
3859
+ function recordStats() {
3860
+ if (statsBuffer.value) {
3861
+ clearStatsBuffer();
3862
+ throw new Error("Stats are already being measured");
3790
3863
  }
3864
+ const newStatsBuffer = statsBuffer;
3865
+ newStatsBuffer.value = {
3866
+ frameloop: {
3867
+ setup: [],
3868
+ rate: [],
3869
+ read: [],
3870
+ resolveKeyframes: [],
3871
+ preUpdate: [],
3872
+ update: [],
3873
+ preRender: [],
3874
+ render: [],
3875
+ postRender: [],
3876
+ },
3877
+ animations: {
3878
+ mainThread: [],
3879
+ waapi: [],
3880
+ layout: [],
3881
+ },
3882
+ layoutProjection: {
3883
+ nodes: [],
3884
+ calculatedTargetDeltas: [],
3885
+ calculatedProjections: [],
3886
+ },
3887
+ };
3888
+ newStatsBuffer.addProjectionMetrics = (metrics) => {
3889
+ const { layoutProjection } = newStatsBuffer.value;
3890
+ layoutProjection.nodes.push(metrics.nodes);
3891
+ layoutProjection.calculatedTargetDeltas.push(metrics.calculatedTargetDeltas);
3892
+ layoutProjection.calculatedProjections.push(metrics.calculatedProjections);
3893
+ };
3894
+ frame.postRender(record, true);
3895
+ return reportStats;
3791
3896
  }
3792
- function motionValue(init, options) {
3793
- return new MotionValue(init, options);
3897
+
3898
+ function transform(...args) {
3899
+ const useImmediate = !Array.isArray(args[0]);
3900
+ const argOffset = useImmediate ? 0 : -1;
3901
+ const inputValue = args[0 + argOffset];
3902
+ const inputRange = args[1 + argOffset];
3903
+ const outputRange = args[2 + argOffset];
3904
+ const options = args[3 + argOffset];
3905
+ const interpolator = interpolate(inputRange, outputRange, options);
3906
+ return useImmediate ? interpolator(inputValue) : interpolator;
3794
3907
  }
3795
3908
 
3796
3909
  function subscribeValue(inputValues, outputValue, getLatest) {
@@ -3885,15 +3998,6 @@
3885
3998
  */
3886
3999
  const findValueType = (v) => valueTypes.find(testValueType(v));
3887
4000
 
3888
- /**
3889
- * Provided a value and a ValueType, returns the value as that value type.
3890
- */
3891
- const getValueAsType = (value, type) => {
3892
- return type && typeof value === "number"
3893
- ? type.transform(value)
3894
- : value;
3895
- };
3896
-
3897
4001
  function chooseLayerType(valueName) {
3898
4002
  if (valueName === "layout")
3899
4003
  return "group";