motion-dom 12.9.4 → 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.
- package/dist/cjs/index.js +727 -623
- package/dist/es/animation/keyframes/DOMKeyframesResolver.mjs +1 -1
- package/dist/es/effects/MotionValueState.mjs +41 -0
- package/dist/es/effects/style/index.mjs +51 -0
- package/dist/es/effects/style/transform.mjs +38 -0
- package/dist/es/gestures/press/index.mjs +4 -2
- package/dist/es/index.mjs +1 -1
- package/dist/es/value/index.mjs +20 -0
- package/dist/index.d.ts +8 -0
- package/dist/motion-dom.dev.js +727 -623
- package/dist/motion-dom.js +1 -1
- package/dist/size-rollup-motion-value.js +1 -0
- package/dist/size-rollup-style-effect.js +1 -0
- package/package.json +14 -3
- package/dist/es/effects/style-effect.mjs +0 -36
package/dist/motion-dom.dev.js
CHANGED
|
@@ -2919,7 +2919,7 @@
|
|
|
2919
2919
|
}
|
|
2920
2920
|
}
|
|
2921
2921
|
}
|
|
2922
|
-
else {
|
|
2922
|
+
else if (positionalValues[name]) {
|
|
2923
2923
|
/**
|
|
2924
2924
|
* Else, the only way to resolve this is by measuring the element.
|
|
2925
2925
|
*/
|
|
@@ -3072,597 +3072,188 @@
|
|
|
3072
3072
|
return Array.from(elementOrSelector);
|
|
3073
3073
|
}
|
|
3074
3074
|
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3075
|
+
/**
|
|
3076
|
+
* Maximum time between the value of two frames, beyond which we
|
|
3077
|
+
* assume the velocity has since been 0.
|
|
3078
|
+
*/
|
|
3079
|
+
const MAX_VELOCITY_DELTA = 30;
|
|
3080
|
+
const isFloat = (value) => {
|
|
3081
|
+
return !isNaN(parseFloat(value));
|
|
3082
|
+
};
|
|
3083
|
+
const collectMotionValues = {
|
|
3084
|
+
current: undefined,
|
|
3085
|
+
};
|
|
3086
|
+
/**
|
|
3087
|
+
* `MotionValue` is used to track the state and velocity of motion values.
|
|
3088
|
+
*
|
|
3089
|
+
* @public
|
|
3090
|
+
*/
|
|
3091
|
+
class MotionValue {
|
|
3092
|
+
/**
|
|
3093
|
+
* @param init - The initiating value
|
|
3094
|
+
* @param config - Optional configuration options
|
|
3095
|
+
*
|
|
3096
|
+
* - `transformer`: A function to transform incoming values with.
|
|
3097
|
+
*/
|
|
3098
|
+
constructor(init, options = {}) {
|
|
3099
|
+
/**
|
|
3100
|
+
* This will be replaced by the build step with the latest version number.
|
|
3101
|
+
* When MotionValues are provided to motion components, warn if versions are mixed.
|
|
3102
|
+
*/
|
|
3103
|
+
this.version = "__VERSION__";
|
|
3104
|
+
/**
|
|
3105
|
+
* Tracks whether this value can output a velocity. Currently this is only true
|
|
3106
|
+
* if the value is numerical, but we might be able to widen the scope here and support
|
|
3107
|
+
* other value types.
|
|
3108
|
+
*
|
|
3109
|
+
* @internal
|
|
3110
|
+
*/
|
|
3111
|
+
this.canTrackVelocity = null;
|
|
3112
|
+
/**
|
|
3113
|
+
* An object containing a SubscriptionManager for each active event.
|
|
3114
|
+
*/
|
|
3115
|
+
this.events = {};
|
|
3116
|
+
this.updateAndNotify = (v, render = true) => {
|
|
3117
|
+
const currentTime = time.now();
|
|
3082
3118
|
/**
|
|
3083
|
-
*
|
|
3084
|
-
*
|
|
3085
|
-
*
|
|
3086
|
-
* TODO: CSS variable support
|
|
3119
|
+
* If we're updating the value during another frame or eventloop
|
|
3120
|
+
* than the previous frame, then the we set the previous frame value
|
|
3121
|
+
* to current.
|
|
3087
3122
|
*/
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
}
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3123
|
+
if (this.updatedAt !== currentTime) {
|
|
3124
|
+
this.setPrevFrameValue();
|
|
3125
|
+
}
|
|
3126
|
+
this.prev = this.current;
|
|
3127
|
+
this.setCurrent(v);
|
|
3128
|
+
// Update update subscribers
|
|
3129
|
+
if (this.current !== this.prev) {
|
|
3130
|
+
this.events.change?.notify(this.current);
|
|
3131
|
+
if (this.dependents) {
|
|
3132
|
+
for (const dependent of this.dependents) {
|
|
3133
|
+
dependent.dirty();
|
|
3134
|
+
}
|
|
3135
|
+
}
|
|
3136
|
+
}
|
|
3137
|
+
// Update render subscribers
|
|
3138
|
+
if (render) {
|
|
3139
|
+
this.events.renderRequest?.notify(this.current);
|
|
3140
|
+
}
|
|
3141
|
+
};
|
|
3142
|
+
this.hasAnimated = false;
|
|
3143
|
+
this.setCurrent(init);
|
|
3144
|
+
this.owner = options.owner;
|
|
3145
|
+
}
|
|
3146
|
+
setCurrent(current) {
|
|
3147
|
+
this.current = current;
|
|
3148
|
+
this.updatedAt = time.now();
|
|
3149
|
+
if (this.canTrackVelocity === null && current !== undefined) {
|
|
3150
|
+
this.canTrackVelocity = isFloat(this.current);
|
|
3098
3151
|
}
|
|
3099
3152
|
}
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3153
|
+
setPrevFrameValue(prevFrameValue = this.current) {
|
|
3154
|
+
this.prevFrameValue = prevFrameValue;
|
|
3155
|
+
this.prevUpdatedAt = this.updatedAt;
|
|
3156
|
+
}
|
|
3157
|
+
/**
|
|
3158
|
+
* Adds a function that will be notified when the `MotionValue` is updated.
|
|
3159
|
+
*
|
|
3160
|
+
* It returns a function that, when called, will cancel the subscription.
|
|
3161
|
+
*
|
|
3162
|
+
* When calling `onChange` inside a React component, it should be wrapped with the
|
|
3163
|
+
* `useEffect` hook. As it returns an unsubscribe function, this should be returned
|
|
3164
|
+
* from the `useEffect` function to ensure you don't add duplicate subscribers..
|
|
3165
|
+
*
|
|
3166
|
+
* ```jsx
|
|
3167
|
+
* export const MyComponent = () => {
|
|
3168
|
+
* const x = useMotionValue(0)
|
|
3169
|
+
* const y = useMotionValue(0)
|
|
3170
|
+
* const opacity = useMotionValue(1)
|
|
3171
|
+
*
|
|
3172
|
+
* useEffect(() => {
|
|
3173
|
+
* function updateOpacity() {
|
|
3174
|
+
* const maxXY = Math.max(x.get(), y.get())
|
|
3175
|
+
* const newOpacity = transform(maxXY, [0, 100], [1, 0])
|
|
3176
|
+
* opacity.set(newOpacity)
|
|
3177
|
+
* }
|
|
3178
|
+
*
|
|
3179
|
+
* const unsubscribeX = x.on("change", updateOpacity)
|
|
3180
|
+
* const unsubscribeY = y.on("change", updateOpacity)
|
|
3181
|
+
*
|
|
3182
|
+
* return () => {
|
|
3183
|
+
* unsubscribeX()
|
|
3184
|
+
* unsubscribeY()
|
|
3185
|
+
* }
|
|
3186
|
+
* }, [])
|
|
3187
|
+
*
|
|
3188
|
+
* return <motion.div style={{ x }} />
|
|
3189
|
+
* }
|
|
3190
|
+
* ```
|
|
3191
|
+
*
|
|
3192
|
+
* @param subscriber - A function that receives the latest value.
|
|
3193
|
+
* @returns A function that, when called, will cancel this subscription.
|
|
3194
|
+
*
|
|
3195
|
+
* @deprecated
|
|
3196
|
+
*/
|
|
3197
|
+
onChange(subscription) {
|
|
3198
|
+
{
|
|
3199
|
+
motionUtils.warnOnce(false, `value.onChange(callback) is deprecated. Switch to value.on("change", callback).`);
|
|
3103
3200
|
}
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
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;
|
|
3201
|
+
return this.on("change", subscription);
|
|
3202
|
+
}
|
|
3203
|
+
on(eventName, callback) {
|
|
3204
|
+
if (!this.events[eventName]) {
|
|
3205
|
+
this.events[eventName] = new motionUtils.SubscriptionManager();
|
|
3122
3206
|
}
|
|
3123
|
-
|
|
3124
|
-
|
|
3207
|
+
const unsubscribe = this.events[eventName].add(callback);
|
|
3208
|
+
if (eventName === "change") {
|
|
3125
3209
|
return () => {
|
|
3126
|
-
|
|
3210
|
+
unsubscribe();
|
|
3211
|
+
/**
|
|
3212
|
+
* If we have no more change listeners by the start
|
|
3213
|
+
* of the next frame, stop active animations.
|
|
3214
|
+
*/
|
|
3215
|
+
frame.read(() => {
|
|
3216
|
+
if (!this.events.change.getSize()) {
|
|
3217
|
+
this.stop();
|
|
3218
|
+
}
|
|
3219
|
+
});
|
|
3127
3220
|
};
|
|
3128
3221
|
}
|
|
3222
|
+
return unsubscribe;
|
|
3129
3223
|
}
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
|
|
3224
|
+
clearListeners() {
|
|
3225
|
+
for (const eventManagers in this.events) {
|
|
3226
|
+
this.events[eventManagers].clear();
|
|
3227
|
+
}
|
|
3228
|
+
}
|
|
3229
|
+
/**
|
|
3230
|
+
* Attaches a passive effect to the `MotionValue`.
|
|
3231
|
+
*/
|
|
3232
|
+
attach(passiveEffect, stopPassiveEffect) {
|
|
3233
|
+
this.passiveEffect = passiveEffect;
|
|
3234
|
+
this.stopPassiveEffect = stopPassiveEffect;
|
|
3235
|
+
}
|
|
3236
|
+
/**
|
|
3237
|
+
* Sets the state of the `MotionValue`.
|
|
3238
|
+
*
|
|
3239
|
+
* @remarks
|
|
3240
|
+
*
|
|
3241
|
+
* ```jsx
|
|
3242
|
+
* const x = useMotionValue(0)
|
|
3243
|
+
* x.set(10)
|
|
3244
|
+
* ```
|
|
3245
|
+
*
|
|
3246
|
+
* @param latest - Latest value to set.
|
|
3247
|
+
* @param render - Whether to notify render subscribers. Defaults to `true`
|
|
3248
|
+
*
|
|
3249
|
+
* @public
|
|
3250
|
+
*/
|
|
3251
|
+
set(v, render = true) {
|
|
3252
|
+
if (!render || !this.passiveEffect) {
|
|
3253
|
+
this.updateAndNotify(v, render);
|
|
3133
3254
|
}
|
|
3134
3255
|
else {
|
|
3135
|
-
|
|
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
|
-
/**
|
|
3490
|
-
* Maximum time between the value of two frames, beyond which we
|
|
3491
|
-
* assume the velocity has since been 0.
|
|
3492
|
-
*/
|
|
3493
|
-
const MAX_VELOCITY_DELTA = 30;
|
|
3494
|
-
const isFloat = (value) => {
|
|
3495
|
-
return !isNaN(parseFloat(value));
|
|
3496
|
-
};
|
|
3497
|
-
const collectMotionValues = {
|
|
3498
|
-
current: undefined,
|
|
3499
|
-
};
|
|
3500
|
-
/**
|
|
3501
|
-
* `MotionValue` is used to track the state and velocity of motion values.
|
|
3502
|
-
*
|
|
3503
|
-
* @public
|
|
3504
|
-
*/
|
|
3505
|
-
class MotionValue {
|
|
3506
|
-
/**
|
|
3507
|
-
* @param init - The initiating value
|
|
3508
|
-
* @param config - Optional configuration options
|
|
3509
|
-
*
|
|
3510
|
-
* - `transformer`: A function to transform incoming values with.
|
|
3511
|
-
*/
|
|
3512
|
-
constructor(init, options = {}) {
|
|
3513
|
-
/**
|
|
3514
|
-
* This will be replaced by the build step with the latest version number.
|
|
3515
|
-
* When MotionValues are provided to motion components, warn if versions are mixed.
|
|
3516
|
-
*/
|
|
3517
|
-
this.version = "__VERSION__";
|
|
3518
|
-
/**
|
|
3519
|
-
* Tracks whether this value can output a velocity. Currently this is only true
|
|
3520
|
-
* if the value is numerical, but we might be able to widen the scope here and support
|
|
3521
|
-
* other value types.
|
|
3522
|
-
*
|
|
3523
|
-
* @internal
|
|
3524
|
-
*/
|
|
3525
|
-
this.canTrackVelocity = null;
|
|
3526
|
-
/**
|
|
3527
|
-
* An object containing a SubscriptionManager for each active event.
|
|
3528
|
-
*/
|
|
3529
|
-
this.events = {};
|
|
3530
|
-
this.updateAndNotify = (v, render = true) => {
|
|
3531
|
-
const currentTime = time.now();
|
|
3532
|
-
/**
|
|
3533
|
-
* If we're updating the value during another frame or eventloop
|
|
3534
|
-
* than the previous frame, then the we set the previous frame value
|
|
3535
|
-
* to current.
|
|
3536
|
-
*/
|
|
3537
|
-
if (this.updatedAt !== currentTime) {
|
|
3538
|
-
this.setPrevFrameValue();
|
|
3539
|
-
}
|
|
3540
|
-
this.prev = this.current;
|
|
3541
|
-
this.setCurrent(v);
|
|
3542
|
-
// Update update subscribers
|
|
3543
|
-
if (this.current !== this.prev) {
|
|
3544
|
-
this.events.change?.notify(this.current);
|
|
3545
|
-
}
|
|
3546
|
-
// Update render subscribers
|
|
3547
|
-
if (render) {
|
|
3548
|
-
this.events.renderRequest?.notify(this.current);
|
|
3549
|
-
}
|
|
3550
|
-
};
|
|
3551
|
-
this.hasAnimated = false;
|
|
3552
|
-
this.setCurrent(init);
|
|
3553
|
-
this.owner = options.owner;
|
|
3554
|
-
}
|
|
3555
|
-
setCurrent(current) {
|
|
3556
|
-
this.current = current;
|
|
3557
|
-
this.updatedAt = time.now();
|
|
3558
|
-
if (this.canTrackVelocity === null && current !== undefined) {
|
|
3559
|
-
this.canTrackVelocity = isFloat(this.current);
|
|
3560
|
-
}
|
|
3561
|
-
}
|
|
3562
|
-
setPrevFrameValue(prevFrameValue = this.current) {
|
|
3563
|
-
this.prevFrameValue = prevFrameValue;
|
|
3564
|
-
this.prevUpdatedAt = this.updatedAt;
|
|
3565
|
-
}
|
|
3566
|
-
/**
|
|
3567
|
-
* Adds a function that will be notified when the `MotionValue` is updated.
|
|
3568
|
-
*
|
|
3569
|
-
* It returns a function that, when called, will cancel the subscription.
|
|
3570
|
-
*
|
|
3571
|
-
* When calling `onChange` inside a React component, it should be wrapped with the
|
|
3572
|
-
* `useEffect` hook. As it returns an unsubscribe function, this should be returned
|
|
3573
|
-
* from the `useEffect` function to ensure you don't add duplicate subscribers..
|
|
3574
|
-
*
|
|
3575
|
-
* ```jsx
|
|
3576
|
-
* export const MyComponent = () => {
|
|
3577
|
-
* const x = useMotionValue(0)
|
|
3578
|
-
* const y = useMotionValue(0)
|
|
3579
|
-
* const opacity = useMotionValue(1)
|
|
3580
|
-
*
|
|
3581
|
-
* useEffect(() => {
|
|
3582
|
-
* function updateOpacity() {
|
|
3583
|
-
* const maxXY = Math.max(x.get(), y.get())
|
|
3584
|
-
* const newOpacity = transform(maxXY, [0, 100], [1, 0])
|
|
3585
|
-
* opacity.set(newOpacity)
|
|
3586
|
-
* }
|
|
3587
|
-
*
|
|
3588
|
-
* const unsubscribeX = x.on("change", updateOpacity)
|
|
3589
|
-
* const unsubscribeY = y.on("change", updateOpacity)
|
|
3590
|
-
*
|
|
3591
|
-
* return () => {
|
|
3592
|
-
* unsubscribeX()
|
|
3593
|
-
* unsubscribeY()
|
|
3594
|
-
* }
|
|
3595
|
-
* }, [])
|
|
3596
|
-
*
|
|
3597
|
-
* return <motion.div style={{ x }} />
|
|
3598
|
-
* }
|
|
3599
|
-
* ```
|
|
3600
|
-
*
|
|
3601
|
-
* @param subscriber - A function that receives the latest value.
|
|
3602
|
-
* @returns A function that, when called, will cancel this subscription.
|
|
3603
|
-
*
|
|
3604
|
-
* @deprecated
|
|
3605
|
-
*/
|
|
3606
|
-
onChange(subscription) {
|
|
3607
|
-
{
|
|
3608
|
-
motionUtils.warnOnce(false, `value.onChange(callback) is deprecated. Switch to value.on("change", callback).`);
|
|
3609
|
-
}
|
|
3610
|
-
return this.on("change", subscription);
|
|
3611
|
-
}
|
|
3612
|
-
on(eventName, callback) {
|
|
3613
|
-
if (!this.events[eventName]) {
|
|
3614
|
-
this.events[eventName] = new motionUtils.SubscriptionManager();
|
|
3615
|
-
}
|
|
3616
|
-
const unsubscribe = this.events[eventName].add(callback);
|
|
3617
|
-
if (eventName === "change") {
|
|
3618
|
-
return () => {
|
|
3619
|
-
unsubscribe();
|
|
3620
|
-
/**
|
|
3621
|
-
* If we have no more change listeners by the start
|
|
3622
|
-
* of the next frame, stop active animations.
|
|
3623
|
-
*/
|
|
3624
|
-
frame.read(() => {
|
|
3625
|
-
if (!this.events.change.getSize()) {
|
|
3626
|
-
this.stop();
|
|
3627
|
-
}
|
|
3628
|
-
});
|
|
3629
|
-
};
|
|
3630
|
-
}
|
|
3631
|
-
return unsubscribe;
|
|
3632
|
-
}
|
|
3633
|
-
clearListeners() {
|
|
3634
|
-
for (const eventManagers in this.events) {
|
|
3635
|
-
this.events[eventManagers].clear();
|
|
3636
|
-
}
|
|
3637
|
-
}
|
|
3638
|
-
/**
|
|
3639
|
-
* Attaches a passive effect to the `MotionValue`.
|
|
3640
|
-
*/
|
|
3641
|
-
attach(passiveEffect, stopPassiveEffect) {
|
|
3642
|
-
this.passiveEffect = passiveEffect;
|
|
3643
|
-
this.stopPassiveEffect = stopPassiveEffect;
|
|
3644
|
-
}
|
|
3645
|
-
/**
|
|
3646
|
-
* Sets the state of the `MotionValue`.
|
|
3647
|
-
*
|
|
3648
|
-
* @remarks
|
|
3649
|
-
*
|
|
3650
|
-
* ```jsx
|
|
3651
|
-
* const x = useMotionValue(0)
|
|
3652
|
-
* x.set(10)
|
|
3653
|
-
* ```
|
|
3654
|
-
*
|
|
3655
|
-
* @param latest - Latest value to set.
|
|
3656
|
-
* @param render - Whether to notify render subscribers. Defaults to `true`
|
|
3657
|
-
*
|
|
3658
|
-
* @public
|
|
3659
|
-
*/
|
|
3660
|
-
set(v, render = true) {
|
|
3661
|
-
if (!render || !this.passiveEffect) {
|
|
3662
|
-
this.updateAndNotify(v, render);
|
|
3663
|
-
}
|
|
3664
|
-
else {
|
|
3665
|
-
this.passiveEffect(v, this.updateAndNotify);
|
|
3256
|
+
this.passiveEffect(v, this.updateAndNotify);
|
|
3666
3257
|
}
|
|
3667
3258
|
}
|
|
3668
3259
|
setWithVelocity(prev, current, delta) {
|
|
@@ -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
|
-
|
|
3755
|
-
|
|
3756
|
-
|
|
3757
|
-
|
|
3758
|
-
|
|
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
|
-
|
|
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
|
-
|
|
3765
|
-
|
|
3766
|
-
|
|
3767
|
-
|
|
3768
|
-
|
|
3769
|
-
|
|
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
|
-
|
|
3772
|
-
|
|
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
|
-
*
|
|
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
|
-
|
|
3784
|
-
|
|
3785
|
-
|
|
3786
|
-
|
|
3787
|
-
|
|
3788
|
-
|
|
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
|
-
|
|
3793
|
-
|
|
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";
|