framer-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.
@@ -1442,7 +1442,7 @@
1442
1442
  const frameloopDriver = (update) => {
1443
1443
  const passTimestamp = ({ timestamp }) => update(timestamp);
1444
1444
  return {
1445
- start: () => frame.update(passTimestamp, true),
1445
+ start: (keepAlive = true) => frame.update(passTimestamp, keepAlive),
1446
1446
  stop: () => cancelFrame(passTimestamp),
1447
1447
  /**
1448
1448
  * If we're processing this frame we can use the
@@ -2269,6 +2269,7 @@
2269
2269
  else if (this.driver) {
2270
2270
  this.startTime = this.driver.now() - newTime / this.playbackSpeed;
2271
2271
  }
2272
+ this.driver?.start(false);
2272
2273
  }
2273
2274
  get speed() {
2274
2275
  return this.playbackSpeed;
@@ -2358,6 +2359,7 @@
2358
2359
  this.options.ease = "linear";
2359
2360
  this.initAnimation();
2360
2361
  }
2362
+ this.driver?.stop();
2361
2363
  return timeline.observe(this);
2362
2364
  }
2363
2365
  }
@@ -3825,420 +3827,6 @@
3825
3827
  return Array.from(elementOrSelector);
3826
3828
  }
3827
3829
 
3828
- function styleEffect(subject, values) {
3829
- const elements = resolveElements(subject);
3830
- const subscriptions = [];
3831
- for (let i = 0; i < elements.length; i++) {
3832
- const element = elements[i];
3833
- for (const key in values) {
3834
- const value = values[key];
3835
- /**
3836
- * TODO: Get specific setters for combined props (like x)
3837
- * or values with default types (like color)
3838
- *
3839
- * TODO: CSS variable support
3840
- */
3841
- const updateStyle = () => {
3842
- element.style[key] = value.get();
3843
- };
3844
- const scheduleUpdate = () => frame.render(updateStyle);
3845
- const cancel = value.on("change", scheduleUpdate);
3846
- scheduleUpdate();
3847
- subscriptions.push(() => {
3848
- cancel();
3849
- cancelFrame(updateStyle);
3850
- });
3851
- }
3852
- }
3853
- return () => {
3854
- for (const cancel of subscriptions) {
3855
- cancel();
3856
- }
3857
- };
3858
- }
3859
-
3860
- const { schedule: microtask, cancel: cancelMicrotask } =
3861
- /* @__PURE__ */ createRenderBatcher(queueMicrotask, false);
3862
-
3863
- const isDragging = {
3864
- x: false,
3865
- y: false,
3866
- };
3867
- function isDragActive() {
3868
- return isDragging.x || isDragging.y;
3869
- }
3870
-
3871
- function setDragLock(axis) {
3872
- if (axis === "x" || axis === "y") {
3873
- if (isDragging[axis]) {
3874
- return null;
3875
- }
3876
- else {
3877
- isDragging[axis] = true;
3878
- return () => {
3879
- isDragging[axis] = false;
3880
- };
3881
- }
3882
- }
3883
- else {
3884
- if (isDragging.x || isDragging.y) {
3885
- return null;
3886
- }
3887
- else {
3888
- isDragging.x = isDragging.y = true;
3889
- return () => {
3890
- isDragging.x = isDragging.y = false;
3891
- };
3892
- }
3893
- }
3894
- }
3895
-
3896
- function setupGesture(elementOrSelector, options) {
3897
- const elements = resolveElements(elementOrSelector);
3898
- const gestureAbortController = new AbortController();
3899
- const eventOptions = {
3900
- passive: true,
3901
- ...options,
3902
- signal: gestureAbortController.signal,
3903
- };
3904
- const cancel = () => gestureAbortController.abort();
3905
- return [elements, eventOptions, cancel];
3906
- }
3907
-
3908
- function isValidHover(event) {
3909
- return !(event.pointerType === "touch" || isDragActive());
3910
- }
3911
- /**
3912
- * Create a hover gesture. hover() is different to .addEventListener("pointerenter")
3913
- * in that it has an easier syntax, filters out polyfilled touch events, interoperates
3914
- * with drag gestures, and automatically removes the "pointerennd" event listener when the hover ends.
3915
- *
3916
- * @public
3917
- */
3918
- function hover(elementOrSelector, onHoverStart, options = {}) {
3919
- const [elements, eventOptions, cancel] = setupGesture(elementOrSelector, options);
3920
- const onPointerEnter = (enterEvent) => {
3921
- if (!isValidHover(enterEvent))
3922
- return;
3923
- const { target } = enterEvent;
3924
- const onHoverEnd = onHoverStart(target, enterEvent);
3925
- if (typeof onHoverEnd !== "function" || !target)
3926
- return;
3927
- const onPointerLeave = (leaveEvent) => {
3928
- if (!isValidHover(leaveEvent))
3929
- return;
3930
- onHoverEnd(leaveEvent);
3931
- target.removeEventListener("pointerleave", onPointerLeave);
3932
- };
3933
- target.addEventListener("pointerleave", onPointerLeave, eventOptions);
3934
- };
3935
- elements.forEach((element) => {
3936
- element.addEventListener("pointerenter", onPointerEnter, eventOptions);
3937
- });
3938
- return cancel;
3939
- }
3940
-
3941
- /**
3942
- * Recursively traverse up the tree to check whether the provided child node
3943
- * is the parent or a descendant of it.
3944
- *
3945
- * @param parent - Element to find
3946
- * @param child - Element to test against parent
3947
- */
3948
- const isNodeOrChild = (parent, child) => {
3949
- if (!child) {
3950
- return false;
3951
- }
3952
- else if (parent === child) {
3953
- return true;
3954
- }
3955
- else {
3956
- return isNodeOrChild(parent, child.parentElement);
3957
- }
3958
- };
3959
-
3960
- const isPrimaryPointer = (event) => {
3961
- if (event.pointerType === "mouse") {
3962
- return typeof event.button !== "number" || event.button <= 0;
3963
- }
3964
- else {
3965
- /**
3966
- * isPrimary is true for all mice buttons, whereas every touch point
3967
- * is regarded as its own input. So subsequent concurrent touch points
3968
- * will be false.
3969
- *
3970
- * Specifically match against false here as incomplete versions of
3971
- * PointerEvents in very old browser might have it set as undefined.
3972
- */
3973
- return event.isPrimary !== false;
3974
- }
3975
- };
3976
-
3977
- const focusableElements = new Set([
3978
- "BUTTON",
3979
- "INPUT",
3980
- "SELECT",
3981
- "TEXTAREA",
3982
- "A",
3983
- ]);
3984
- function isElementKeyboardAccessible(element) {
3985
- return (focusableElements.has(element.tagName) ||
3986
- element.tabIndex !== -1);
3987
- }
3988
-
3989
- const isPressing = new WeakSet();
3990
-
3991
- /**
3992
- * Filter out events that are not "Enter" keys.
3993
- */
3994
- function filterEvents(callback) {
3995
- return (event) => {
3996
- if (event.key !== "Enter")
3997
- return;
3998
- callback(event);
3999
- };
4000
- }
4001
- function firePointerEvent(target, type) {
4002
- target.dispatchEvent(new PointerEvent("pointer" + type, { isPrimary: true, bubbles: true }));
4003
- }
4004
- const enableKeyboardPress = (focusEvent, eventOptions) => {
4005
- const element = focusEvent.currentTarget;
4006
- if (!element)
4007
- return;
4008
- const handleKeydown = filterEvents(() => {
4009
- if (isPressing.has(element))
4010
- return;
4011
- firePointerEvent(element, "down");
4012
- const handleKeyup = filterEvents(() => {
4013
- firePointerEvent(element, "up");
4014
- });
4015
- const handleBlur = () => firePointerEvent(element, "cancel");
4016
- element.addEventListener("keyup", handleKeyup, eventOptions);
4017
- element.addEventListener("blur", handleBlur, eventOptions);
4018
- });
4019
- element.addEventListener("keydown", handleKeydown, eventOptions);
4020
- /**
4021
- * Add an event listener that fires on blur to remove the keydown events.
4022
- */
4023
- element.addEventListener("blur", () => element.removeEventListener("keydown", handleKeydown), eventOptions);
4024
- };
4025
-
4026
- /**
4027
- * Filter out events that are not primary pointer events, or are triggering
4028
- * while a Motion gesture is active.
4029
- */
4030
- function isValidPressEvent(event) {
4031
- return isPrimaryPointer(event) && !isDragActive();
4032
- }
4033
- /**
4034
- * Create a press gesture.
4035
- *
4036
- * Press is different to `"pointerdown"`, `"pointerup"` in that it
4037
- * automatically filters out secondary pointer events like right
4038
- * click and multitouch.
4039
- *
4040
- * It also adds accessibility support for keyboards, where
4041
- * an element with a press gesture will receive focus and
4042
- * trigger on Enter `"keydown"` and `"keyup"` events.
4043
- *
4044
- * This is different to a browser's `"click"` event, which does
4045
- * respond to keyboards but only for the `"click"` itself, rather
4046
- * than the press start and end/cancel. The element also needs
4047
- * to be focusable for this to work, whereas a press gesture will
4048
- * make an element focusable by default.
4049
- *
4050
- * @public
4051
- */
4052
- function press(targetOrSelector, onPressStart, options = {}) {
4053
- const [targets, eventOptions, cancelEvents] = setupGesture(targetOrSelector, options);
4054
- const startPress = (startEvent) => {
4055
- const target = startEvent.currentTarget;
4056
- if (!isValidPressEvent(startEvent) || isPressing.has(target))
4057
- return;
4058
- isPressing.add(target);
4059
- const onPressEnd = onPressStart(target, startEvent);
4060
- const onPointerEnd = (endEvent, success) => {
4061
- window.removeEventListener("pointerup", onPointerUp);
4062
- window.removeEventListener("pointercancel", onPointerCancel);
4063
- if (!isValidPressEvent(endEvent) || !isPressing.has(target)) {
4064
- return;
4065
- }
4066
- isPressing.delete(target);
4067
- if (typeof onPressEnd === "function") {
4068
- onPressEnd(endEvent, { success });
4069
- }
4070
- };
4071
- const onPointerUp = (upEvent) => {
4072
- onPointerEnd(upEvent, target === window ||
4073
- target === document ||
4074
- options.useGlobalTarget ||
4075
- isNodeOrChild(target, upEvent.target));
4076
- };
4077
- const onPointerCancel = (cancelEvent) => {
4078
- onPointerEnd(cancelEvent, false);
4079
- };
4080
- window.addEventListener("pointerup", onPointerUp, eventOptions);
4081
- window.addEventListener("pointercancel", onPointerCancel, eventOptions);
4082
- };
4083
- targets.forEach((target) => {
4084
- const pointerDownTarget = options.useGlobalTarget ? window : target;
4085
- pointerDownTarget.addEventListener("pointerdown", startPress, eventOptions);
4086
- if (target instanceof HTMLElement) {
4087
- target.addEventListener("focus", (event) => enableKeyboardPress(event, eventOptions));
4088
- if (!isElementKeyboardAccessible(target) &&
4089
- !target.hasAttribute("tabindex")) {
4090
- target.tabIndex = 0;
4091
- }
4092
- }
4093
- });
4094
- return cancelEvents;
4095
- }
4096
-
4097
- function getComputedStyle$2(element, name) {
4098
- const computedStyle = window.getComputedStyle(element);
4099
- return isCSSVar(name)
4100
- ? computedStyle.getPropertyValue(name)
4101
- : computedStyle[name];
4102
- }
4103
-
4104
- function observeTimeline(update, timeline) {
4105
- let prevProgress;
4106
- const onFrame = () => {
4107
- const { currentTime } = timeline;
4108
- const percentage = currentTime === null ? 0 : currentTime.value;
4109
- const progress = percentage / 100;
4110
- if (prevProgress !== progress) {
4111
- update(progress);
4112
- }
4113
- prevProgress = progress;
4114
- };
4115
- frame.preUpdate(onFrame, true);
4116
- return () => cancelFrame(onFrame);
4117
- }
4118
-
4119
- function record() {
4120
- const { value } = statsBuffer;
4121
- if (value === null) {
4122
- cancelFrame(record);
4123
- return;
4124
- }
4125
- value.frameloop.rate.push(frameData.delta);
4126
- value.animations.mainThread.push(activeAnimations.mainThread);
4127
- value.animations.waapi.push(activeAnimations.waapi);
4128
- value.animations.layout.push(activeAnimations.layout);
4129
- }
4130
- function mean(values) {
4131
- return values.reduce((acc, value) => acc + value, 0) / values.length;
4132
- }
4133
- function summarise(values, calcAverage = mean) {
4134
- if (values.length === 0) {
4135
- return {
4136
- min: 0,
4137
- max: 0,
4138
- avg: 0,
4139
- };
4140
- }
4141
- return {
4142
- min: Math.min(...values),
4143
- max: Math.max(...values),
4144
- avg: calcAverage(values),
4145
- };
4146
- }
4147
- const msToFps = (ms) => Math.round(1000 / ms);
4148
- function clearStatsBuffer() {
4149
- statsBuffer.value = null;
4150
- statsBuffer.addProjectionMetrics = null;
4151
- }
4152
- function reportStats() {
4153
- const { value } = statsBuffer;
4154
- if (!value) {
4155
- throw new Error("Stats are not being measured");
4156
- }
4157
- clearStatsBuffer();
4158
- cancelFrame(record);
4159
- const summary = {
4160
- frameloop: {
4161
- setup: summarise(value.frameloop.setup),
4162
- rate: summarise(value.frameloop.rate),
4163
- read: summarise(value.frameloop.read),
4164
- resolveKeyframes: summarise(value.frameloop.resolveKeyframes),
4165
- preUpdate: summarise(value.frameloop.preUpdate),
4166
- update: summarise(value.frameloop.update),
4167
- preRender: summarise(value.frameloop.preRender),
4168
- render: summarise(value.frameloop.render),
4169
- postRender: summarise(value.frameloop.postRender),
4170
- },
4171
- animations: {
4172
- mainThread: summarise(value.animations.mainThread),
4173
- waapi: summarise(value.animations.waapi),
4174
- layout: summarise(value.animations.layout),
4175
- },
4176
- layoutProjection: {
4177
- nodes: summarise(value.layoutProjection.nodes),
4178
- calculatedTargetDeltas: summarise(value.layoutProjection.calculatedTargetDeltas),
4179
- calculatedProjections: summarise(value.layoutProjection.calculatedProjections),
4180
- },
4181
- };
4182
- /**
4183
- * Convert the rate to FPS
4184
- */
4185
- const { rate } = summary.frameloop;
4186
- rate.min = msToFps(rate.min);
4187
- rate.max = msToFps(rate.max);
4188
- rate.avg = msToFps(rate.avg);
4189
- [rate.min, rate.max] = [rate.max, rate.min];
4190
- return summary;
4191
- }
4192
- function recordStats() {
4193
- if (statsBuffer.value) {
4194
- clearStatsBuffer();
4195
- throw new Error("Stats are already being measured");
4196
- }
4197
- const newStatsBuffer = statsBuffer;
4198
- newStatsBuffer.value = {
4199
- frameloop: {
4200
- setup: [],
4201
- rate: [],
4202
- read: [],
4203
- resolveKeyframes: [],
4204
- preUpdate: [],
4205
- update: [],
4206
- preRender: [],
4207
- render: [],
4208
- postRender: [],
4209
- },
4210
- animations: {
4211
- mainThread: [],
4212
- waapi: [],
4213
- layout: [],
4214
- },
4215
- layoutProjection: {
4216
- nodes: [],
4217
- calculatedTargetDeltas: [],
4218
- calculatedProjections: [],
4219
- },
4220
- };
4221
- newStatsBuffer.addProjectionMetrics = (metrics) => {
4222
- const { layoutProjection } = newStatsBuffer.value;
4223
- layoutProjection.nodes.push(metrics.nodes);
4224
- layoutProjection.calculatedTargetDeltas.push(metrics.calculatedTargetDeltas);
4225
- layoutProjection.calculatedProjections.push(metrics.calculatedProjections);
4226
- };
4227
- frame.postRender(record, true);
4228
- return reportStats;
4229
- }
4230
-
4231
- function transform(...args) {
4232
- const useImmediate = !Array.isArray(args[0]);
4233
- const argOffset = useImmediate ? 0 : -1;
4234
- const inputValue = args[0 + argOffset];
4235
- const inputRange = args[1 + argOffset];
4236
- const outputRange = args[2 + argOffset];
4237
- const options = args[3 + argOffset];
4238
- const interpolator = interpolate(inputRange, outputRange, options);
4239
- return useImmediate ? interpolator(inputValue) : interpolator;
4240
- }
4241
-
4242
3830
  /**
4243
3831
  * Maximum time between the value of two frames, beyond which we
4244
3832
  * assume the velocity has since been 0.
@@ -4257,293 +3845,820 @@
4257
3845
  */
4258
3846
  class MotionValue {
4259
3847
  /**
4260
- * @param init - The initiating value
4261
- * @param config - Optional configuration options
3848
+ * @param init - The initiating value
3849
+ * @param config - Optional configuration options
3850
+ *
3851
+ * - `transformer`: A function to transform incoming values with.
3852
+ */
3853
+ constructor(init, options = {}) {
3854
+ /**
3855
+ * This will be replaced by the build step with the latest version number.
3856
+ * When MotionValues are provided to motion components, warn if versions are mixed.
3857
+ */
3858
+ this.version = "__VERSION__";
3859
+ /**
3860
+ * Tracks whether this value can output a velocity. Currently this is only true
3861
+ * if the value is numerical, but we might be able to widen the scope here and support
3862
+ * other value types.
3863
+ *
3864
+ * @internal
3865
+ */
3866
+ this.canTrackVelocity = null;
3867
+ /**
3868
+ * An object containing a SubscriptionManager for each active event.
3869
+ */
3870
+ this.events = {};
3871
+ this.updateAndNotify = (v, render = true) => {
3872
+ const currentTime = time.now();
3873
+ /**
3874
+ * If we're updating the value during another frame or eventloop
3875
+ * than the previous frame, then the we set the previous frame value
3876
+ * to current.
3877
+ */
3878
+ if (this.updatedAt !== currentTime) {
3879
+ this.setPrevFrameValue();
3880
+ }
3881
+ this.prev = this.current;
3882
+ this.setCurrent(v);
3883
+ // Update update subscribers
3884
+ if (this.current !== this.prev) {
3885
+ this.events.change?.notify(this.current);
3886
+ if (this.dependents) {
3887
+ for (const dependent of this.dependents) {
3888
+ dependent.dirty();
3889
+ }
3890
+ }
3891
+ }
3892
+ // Update render subscribers
3893
+ if (render) {
3894
+ this.events.renderRequest?.notify(this.current);
3895
+ }
3896
+ };
3897
+ this.hasAnimated = false;
3898
+ this.setCurrent(init);
3899
+ this.owner = options.owner;
3900
+ }
3901
+ setCurrent(current) {
3902
+ this.current = current;
3903
+ this.updatedAt = time.now();
3904
+ if (this.canTrackVelocity === null && current !== undefined) {
3905
+ this.canTrackVelocity = isFloat(this.current);
3906
+ }
3907
+ }
3908
+ setPrevFrameValue(prevFrameValue = this.current) {
3909
+ this.prevFrameValue = prevFrameValue;
3910
+ this.prevUpdatedAt = this.updatedAt;
3911
+ }
3912
+ /**
3913
+ * Adds a function that will be notified when the `MotionValue` is updated.
3914
+ *
3915
+ * It returns a function that, when called, will cancel the subscription.
3916
+ *
3917
+ * When calling `onChange` inside a React component, it should be wrapped with the
3918
+ * `useEffect` hook. As it returns an unsubscribe function, this should be returned
3919
+ * from the `useEffect` function to ensure you don't add duplicate subscribers..
3920
+ *
3921
+ * ```jsx
3922
+ * export const MyComponent = () => {
3923
+ * const x = useMotionValue(0)
3924
+ * const y = useMotionValue(0)
3925
+ * const opacity = useMotionValue(1)
3926
+ *
3927
+ * useEffect(() => {
3928
+ * function updateOpacity() {
3929
+ * const maxXY = Math.max(x.get(), y.get())
3930
+ * const newOpacity = transform(maxXY, [0, 100], [1, 0])
3931
+ * opacity.set(newOpacity)
3932
+ * }
3933
+ *
3934
+ * const unsubscribeX = x.on("change", updateOpacity)
3935
+ * const unsubscribeY = y.on("change", updateOpacity)
3936
+ *
3937
+ * return () => {
3938
+ * unsubscribeX()
3939
+ * unsubscribeY()
3940
+ * }
3941
+ * }, [])
3942
+ *
3943
+ * return <motion.div style={{ x }} />
3944
+ * }
3945
+ * ```
3946
+ *
3947
+ * @param subscriber - A function that receives the latest value.
3948
+ * @returns A function that, when called, will cancel this subscription.
3949
+ *
3950
+ * @deprecated
3951
+ */
3952
+ onChange(subscription) {
3953
+ {
3954
+ warnOnce(false, `value.onChange(callback) is deprecated. Switch to value.on("change", callback).`);
3955
+ }
3956
+ return this.on("change", subscription);
3957
+ }
3958
+ on(eventName, callback) {
3959
+ if (!this.events[eventName]) {
3960
+ this.events[eventName] = new SubscriptionManager();
3961
+ }
3962
+ const unsubscribe = this.events[eventName].add(callback);
3963
+ if (eventName === "change") {
3964
+ return () => {
3965
+ unsubscribe();
3966
+ /**
3967
+ * If we have no more change listeners by the start
3968
+ * of the next frame, stop active animations.
3969
+ */
3970
+ frame.read(() => {
3971
+ if (!this.events.change.getSize()) {
3972
+ this.stop();
3973
+ }
3974
+ });
3975
+ };
3976
+ }
3977
+ return unsubscribe;
3978
+ }
3979
+ clearListeners() {
3980
+ for (const eventManagers in this.events) {
3981
+ this.events[eventManagers].clear();
3982
+ }
3983
+ }
3984
+ /**
3985
+ * Attaches a passive effect to the `MotionValue`.
3986
+ */
3987
+ attach(passiveEffect, stopPassiveEffect) {
3988
+ this.passiveEffect = passiveEffect;
3989
+ this.stopPassiveEffect = stopPassiveEffect;
3990
+ }
3991
+ /**
3992
+ * Sets the state of the `MotionValue`.
3993
+ *
3994
+ * @remarks
3995
+ *
3996
+ * ```jsx
3997
+ * const x = useMotionValue(0)
3998
+ * x.set(10)
3999
+ * ```
4000
+ *
4001
+ * @param latest - Latest value to set.
4002
+ * @param render - Whether to notify render subscribers. Defaults to `true`
4003
+ *
4004
+ * @public
4005
+ */
4006
+ set(v, render = true) {
4007
+ if (!render || !this.passiveEffect) {
4008
+ this.updateAndNotify(v, render);
4009
+ }
4010
+ else {
4011
+ this.passiveEffect(v, this.updateAndNotify);
4012
+ }
4013
+ }
4014
+ setWithVelocity(prev, current, delta) {
4015
+ this.set(current);
4016
+ this.prev = undefined;
4017
+ this.prevFrameValue = prev;
4018
+ this.prevUpdatedAt = this.updatedAt - delta;
4019
+ }
4020
+ /**
4021
+ * Set the state of the `MotionValue`, stopping any active animations,
4022
+ * effects, and resets velocity to `0`.
4023
+ */
4024
+ jump(v, endAnimation = true) {
4025
+ this.updateAndNotify(v);
4026
+ this.prev = v;
4027
+ this.prevUpdatedAt = this.prevFrameValue = undefined;
4028
+ endAnimation && this.stop();
4029
+ if (this.stopPassiveEffect)
4030
+ this.stopPassiveEffect();
4031
+ }
4032
+ dirty() {
4033
+ this.events.change?.notify(this.current);
4034
+ }
4035
+ addDependent(dependent) {
4036
+ if (!this.dependents) {
4037
+ this.dependents = new Set();
4038
+ }
4039
+ this.dependents.add(dependent);
4040
+ }
4041
+ removeDependent(dependent) {
4042
+ if (this.dependents) {
4043
+ this.dependents.delete(dependent);
4044
+ }
4045
+ }
4046
+ /**
4047
+ * Returns the latest state of `MotionValue`
4262
4048
  *
4263
- * - `transformer`: A function to transform incoming values with.
4049
+ * @returns - The latest state of `MotionValue`
4050
+ *
4051
+ * @public
4264
4052
  */
4265
- constructor(init, options = {}) {
4266
- /**
4267
- * This will be replaced by the build step with the latest version number.
4268
- * When MotionValues are provided to motion components, warn if versions are mixed.
4269
- */
4270
- this.version = "__VERSION__";
4271
- /**
4272
- * Tracks whether this value can output a velocity. Currently this is only true
4273
- * if the value is numerical, but we might be able to widen the scope here and support
4274
- * other value types.
4275
- *
4276
- * @internal
4277
- */
4278
- this.canTrackVelocity = null;
4279
- /**
4280
- * An object containing a SubscriptionManager for each active event.
4281
- */
4282
- this.events = {};
4283
- this.updateAndNotify = (v, render = true) => {
4284
- const currentTime = time.now();
4285
- /**
4286
- * If we're updating the value during another frame or eventloop
4287
- * than the previous frame, then the we set the previous frame value
4288
- * to current.
4289
- */
4290
- if (this.updatedAt !== currentTime) {
4291
- this.setPrevFrameValue();
4292
- }
4293
- this.prev = this.current;
4294
- this.setCurrent(v);
4295
- // Update update subscribers
4296
- if (this.current !== this.prev) {
4297
- this.events.change?.notify(this.current);
4298
- }
4299
- // Update render subscribers
4300
- if (render) {
4301
- this.events.renderRequest?.notify(this.current);
4302
- }
4303
- };
4304
- this.hasAnimated = false;
4305
- this.setCurrent(init);
4306
- this.owner = options.owner;
4307
- }
4308
- setCurrent(current) {
4309
- this.current = current;
4310
- this.updatedAt = time.now();
4311
- if (this.canTrackVelocity === null && current !== undefined) {
4312
- this.canTrackVelocity = isFloat(this.current);
4053
+ get() {
4054
+ if (collectMotionValues.current) {
4055
+ collectMotionValues.current.push(this);
4313
4056
  }
4057
+ return this.current;
4314
4058
  }
4315
- setPrevFrameValue(prevFrameValue = this.current) {
4316
- this.prevFrameValue = prevFrameValue;
4317
- this.prevUpdatedAt = this.updatedAt;
4059
+ /**
4060
+ * @public
4061
+ */
4062
+ getPrevious() {
4063
+ return this.prev;
4318
4064
  }
4319
4065
  /**
4320
- * Adds a function that will be notified when the `MotionValue` is updated.
4066
+ * Returns the latest velocity of `MotionValue`
4321
4067
  *
4322
- * It returns a function that, when called, will cancel the subscription.
4068
+ * @returns - The latest velocity of `MotionValue`. Returns `0` if the state is non-numerical.
4323
4069
  *
4324
- * When calling `onChange` inside a React component, it should be wrapped with the
4325
- * `useEffect` hook. As it returns an unsubscribe function, this should be returned
4326
- * from the `useEffect` function to ensure you don't add duplicate subscribers..
4070
+ * @public
4071
+ */
4072
+ getVelocity() {
4073
+ const currentTime = time.now();
4074
+ if (!this.canTrackVelocity ||
4075
+ this.prevFrameValue === undefined ||
4076
+ currentTime - this.updatedAt > MAX_VELOCITY_DELTA) {
4077
+ return 0;
4078
+ }
4079
+ const delta = Math.min(this.updatedAt - this.prevUpdatedAt, MAX_VELOCITY_DELTA);
4080
+ // Casts because of parseFloat's poor typing
4081
+ return velocityPerSecond(parseFloat(this.current) -
4082
+ parseFloat(this.prevFrameValue), delta);
4083
+ }
4084
+ /**
4085
+ * Registers a new animation to control this `MotionValue`. Only one
4086
+ * animation can drive a `MotionValue` at one time.
4327
4087
  *
4328
4088
  * ```jsx
4329
- * export const MyComponent = () => {
4330
- * const x = useMotionValue(0)
4331
- * const y = useMotionValue(0)
4332
- * const opacity = useMotionValue(1)
4333
- *
4334
- * useEffect(() => {
4335
- * function updateOpacity() {
4336
- * const maxXY = Math.max(x.get(), y.get())
4337
- * const newOpacity = transform(maxXY, [0, 100], [1, 0])
4338
- * opacity.set(newOpacity)
4339
- * }
4089
+ * value.start()
4090
+ * ```
4340
4091
  *
4341
- * const unsubscribeX = x.on("change", updateOpacity)
4342
- * const unsubscribeY = y.on("change", updateOpacity)
4092
+ * @param animation - A function that starts the provided animation
4093
+ */
4094
+ start(startAnimation) {
4095
+ this.stop();
4096
+ return new Promise((resolve) => {
4097
+ this.hasAnimated = true;
4098
+ this.animation = startAnimation(resolve);
4099
+ if (this.events.animationStart) {
4100
+ this.events.animationStart.notify();
4101
+ }
4102
+ }).then(() => {
4103
+ if (this.events.animationComplete) {
4104
+ this.events.animationComplete.notify();
4105
+ }
4106
+ this.clearAnimation();
4107
+ });
4108
+ }
4109
+ /**
4110
+ * Stop the currently active animation.
4343
4111
  *
4344
- * return () => {
4345
- * unsubscribeX()
4346
- * unsubscribeY()
4347
- * }
4348
- * }, [])
4112
+ * @public
4113
+ */
4114
+ stop() {
4115
+ if (this.animation) {
4116
+ this.animation.stop();
4117
+ if (this.events.animationCancel) {
4118
+ this.events.animationCancel.notify();
4119
+ }
4120
+ }
4121
+ this.clearAnimation();
4122
+ }
4123
+ /**
4124
+ * Returns `true` if this value is currently animating.
4349
4125
  *
4350
- * return <motion.div style={{ x }} />
4351
- * }
4352
- * ```
4126
+ * @public
4127
+ */
4128
+ isAnimating() {
4129
+ return !!this.animation;
4130
+ }
4131
+ clearAnimation() {
4132
+ delete this.animation;
4133
+ }
4134
+ /**
4135
+ * Destroy and clean up subscribers to this `MotionValue`.
4353
4136
  *
4354
- * @param subscriber - A function that receives the latest value.
4355
- * @returns A function that, when called, will cancel this subscription.
4137
+ * The `MotionValue` hooks like `useMotionValue` and `useTransform` automatically
4138
+ * handle the lifecycle of the returned `MotionValue`, so this method is only necessary if you've manually
4139
+ * created a `MotionValue` via the `motionValue` function.
4356
4140
  *
4357
- * @deprecated
4141
+ * @public
4358
4142
  */
4359
- onChange(subscription) {
4360
- {
4361
- warnOnce(false, `value.onChange(callback) is deprecated. Switch to value.on("change", callback).`);
4143
+ destroy() {
4144
+ this.dependents?.clear();
4145
+ this.events.destroy?.notify();
4146
+ this.clearListeners();
4147
+ this.stop();
4148
+ if (this.stopPassiveEffect) {
4149
+ this.stopPassiveEffect();
4362
4150
  }
4363
- return this.on("change", subscription);
4364
4151
  }
4365
- on(eventName, callback) {
4366
- if (!this.events[eventName]) {
4367
- this.events[eventName] = new SubscriptionManager();
4152
+ }
4153
+ function motionValue(init, options) {
4154
+ return new MotionValue(init, options);
4155
+ }
4156
+
4157
+ /**
4158
+ * Provided a value and a ValueType, returns the value as that value type.
4159
+ */
4160
+ const getValueAsType = (value, type) => {
4161
+ return type && typeof value === "number"
4162
+ ? type.transform(value)
4163
+ : value;
4164
+ };
4165
+
4166
+ class MotionValueState {
4167
+ constructor() {
4168
+ this.latest = {};
4169
+ this.values = new Map();
4170
+ }
4171
+ set(name, value, render, computed) {
4172
+ const existingValue = this.values.get(name);
4173
+ if (existingValue) {
4174
+ existingValue.onRemove();
4175
+ }
4176
+ const onChange = () => {
4177
+ this.latest[name] = getValueAsType(value.get(), numberValueTypes[name]);
4178
+ render && frame.render(render);
4179
+ };
4180
+ onChange();
4181
+ const cancelOnChange = value.on("change", onChange);
4182
+ computed && value.addDependent(computed);
4183
+ const remove = () => {
4184
+ cancelOnChange();
4185
+ render && cancelFrame(render);
4186
+ this.values.delete(name);
4187
+ computed && value.removeDependent(computed);
4188
+ };
4189
+ this.values.set(name, { value, onRemove: remove });
4190
+ return remove;
4191
+ }
4192
+ get(name) {
4193
+ return this.values.get(name)?.value;
4194
+ }
4195
+ destroy() {
4196
+ for (const value of this.values.values()) {
4197
+ value.onRemove();
4198
+ }
4199
+ }
4200
+ }
4201
+
4202
+ const translateAlias$1 = {
4203
+ x: "translateX",
4204
+ y: "translateY",
4205
+ z: "translateZ",
4206
+ transformPerspective: "perspective",
4207
+ };
4208
+ function buildTransform$1(state) {
4209
+ let transform = "";
4210
+ let transformIsDefault = true;
4211
+ /**
4212
+ * Loop over all possible transforms in order, adding the ones that
4213
+ * are present to the transform string.
4214
+ */
4215
+ for (let i = 0; i < transformPropOrder.length; i++) {
4216
+ const key = transformPropOrder[i];
4217
+ const value = state.latest[key];
4218
+ if (value === undefined)
4219
+ continue;
4220
+ let valueIsDefault = true;
4221
+ if (typeof value === "number") {
4222
+ valueIsDefault = value === (key.startsWith("scale") ? 1 : 0);
4223
+ }
4224
+ else {
4225
+ valueIsDefault = parseFloat(value) === 0;
4226
+ }
4227
+ if (!valueIsDefault) {
4228
+ transformIsDefault = false;
4229
+ const transformName = translateAlias$1[key] || key;
4230
+ const valueToRender = state.latest[key];
4231
+ transform += `${transformName}(${valueToRender}) `;
4232
+ }
4233
+ }
4234
+ return transformIsDefault ? "none" : transform.trim();
4235
+ }
4236
+
4237
+ const stateMap = new WeakMap();
4238
+ function styleEffect(subject, values) {
4239
+ const elements = resolveElements(subject);
4240
+ const subscriptions = [];
4241
+ for (let i = 0; i < elements.length; i++) {
4242
+ const element = elements[i];
4243
+ const state = stateMap.get(element) ?? new MotionValueState();
4244
+ stateMap.set(element, state);
4245
+ for (const key in values) {
4246
+ const value = values[key];
4247
+ const remove = addValue(element, state, key, value);
4248
+ subscriptions.push(remove);
4249
+ }
4250
+ }
4251
+ return () => {
4252
+ for (const cancel of subscriptions)
4253
+ cancel();
4254
+ };
4255
+ }
4256
+ function addValue(element, state, key, value) {
4257
+ let render = undefined;
4258
+ let computed = undefined;
4259
+ if (transformProps.has(key)) {
4260
+ if (!state.get("transform")) {
4261
+ state.set("transform", new MotionValue("none"), () => {
4262
+ element.style.transform = buildTransform$1(state);
4263
+ });
4264
+ }
4265
+ computed = state.get("transform");
4266
+ }
4267
+ else if (isCSSVar(key)) {
4268
+ render = () => {
4269
+ element.style.setProperty(key, state.latest[key]);
4270
+ };
4271
+ }
4272
+ else {
4273
+ render = () => {
4274
+ element.style[key] = state.latest[key];
4275
+ };
4276
+ }
4277
+ return state.set(key, value, render, computed);
4278
+ }
4279
+
4280
+ const { schedule: microtask, cancel: cancelMicrotask } =
4281
+ /* @__PURE__ */ createRenderBatcher(queueMicrotask, false);
4282
+
4283
+ const isDragging = {
4284
+ x: false,
4285
+ y: false,
4286
+ };
4287
+ function isDragActive() {
4288
+ return isDragging.x || isDragging.y;
4289
+ }
4290
+
4291
+ function setDragLock(axis) {
4292
+ if (axis === "x" || axis === "y") {
4293
+ if (isDragging[axis]) {
4294
+ return null;
4368
4295
  }
4369
- const unsubscribe = this.events[eventName].add(callback);
4370
- if (eventName === "change") {
4296
+ else {
4297
+ isDragging[axis] = true;
4371
4298
  return () => {
4372
- unsubscribe();
4373
- /**
4374
- * If we have no more change listeners by the start
4375
- * of the next frame, stop active animations.
4376
- */
4377
- frame.read(() => {
4378
- if (!this.events.change.getSize()) {
4379
- this.stop();
4380
- }
4381
- });
4299
+ isDragging[axis] = false;
4382
4300
  };
4383
4301
  }
4384
- return unsubscribe;
4385
- }
4386
- clearListeners() {
4387
- for (const eventManagers in this.events) {
4388
- this.events[eventManagers].clear();
4389
- }
4390
- }
4391
- /**
4392
- * Attaches a passive effect to the `MotionValue`.
4393
- */
4394
- attach(passiveEffect, stopPassiveEffect) {
4395
- this.passiveEffect = passiveEffect;
4396
- this.stopPassiveEffect = stopPassiveEffect;
4397
4302
  }
4398
- /**
4399
- * Sets the state of the `MotionValue`.
4400
- *
4401
- * @remarks
4402
- *
4403
- * ```jsx
4404
- * const x = useMotionValue(0)
4405
- * x.set(10)
4406
- * ```
4407
- *
4408
- * @param latest - Latest value to set.
4409
- * @param render - Whether to notify render subscribers. Defaults to `true`
4410
- *
4411
- * @public
4412
- */
4413
- set(v, render = true) {
4414
- if (!render || !this.passiveEffect) {
4415
- this.updateAndNotify(v, render);
4303
+ else {
4304
+ if (isDragging.x || isDragging.y) {
4305
+ return null;
4416
4306
  }
4417
4307
  else {
4418
- this.passiveEffect(v, this.updateAndNotify);
4308
+ isDragging.x = isDragging.y = true;
4309
+ return () => {
4310
+ isDragging.x = isDragging.y = false;
4311
+ };
4419
4312
  }
4420
4313
  }
4421
- setWithVelocity(prev, current, delta) {
4422
- this.set(current);
4423
- this.prev = undefined;
4424
- this.prevFrameValue = prev;
4425
- this.prevUpdatedAt = this.updatedAt - delta;
4314
+ }
4315
+
4316
+ function setupGesture(elementOrSelector, options) {
4317
+ const elements = resolveElements(elementOrSelector);
4318
+ const gestureAbortController = new AbortController();
4319
+ const eventOptions = {
4320
+ passive: true,
4321
+ ...options,
4322
+ signal: gestureAbortController.signal,
4323
+ };
4324
+ const cancel = () => gestureAbortController.abort();
4325
+ return [elements, eventOptions, cancel];
4326
+ }
4327
+
4328
+ function isValidHover(event) {
4329
+ return !(event.pointerType === "touch" || isDragActive());
4330
+ }
4331
+ /**
4332
+ * Create a hover gesture. hover() is different to .addEventListener("pointerenter")
4333
+ * in that it has an easier syntax, filters out polyfilled touch events, interoperates
4334
+ * with drag gestures, and automatically removes the "pointerennd" event listener when the hover ends.
4335
+ *
4336
+ * @public
4337
+ */
4338
+ function hover(elementOrSelector, onHoverStart, options = {}) {
4339
+ const [elements, eventOptions, cancel] = setupGesture(elementOrSelector, options);
4340
+ const onPointerEnter = (enterEvent) => {
4341
+ if (!isValidHover(enterEvent))
4342
+ return;
4343
+ const { target } = enterEvent;
4344
+ const onHoverEnd = onHoverStart(target, enterEvent);
4345
+ if (typeof onHoverEnd !== "function" || !target)
4346
+ return;
4347
+ const onPointerLeave = (leaveEvent) => {
4348
+ if (!isValidHover(leaveEvent))
4349
+ return;
4350
+ onHoverEnd(leaveEvent);
4351
+ target.removeEventListener("pointerleave", onPointerLeave);
4352
+ };
4353
+ target.addEventListener("pointerleave", onPointerLeave, eventOptions);
4354
+ };
4355
+ elements.forEach((element) => {
4356
+ element.addEventListener("pointerenter", onPointerEnter, eventOptions);
4357
+ });
4358
+ return cancel;
4359
+ }
4360
+
4361
+ /**
4362
+ * Recursively traverse up the tree to check whether the provided child node
4363
+ * is the parent or a descendant of it.
4364
+ *
4365
+ * @param parent - Element to find
4366
+ * @param child - Element to test against parent
4367
+ */
4368
+ const isNodeOrChild = (parent, child) => {
4369
+ if (!child) {
4370
+ return false;
4426
4371
  }
4427
- /**
4428
- * Set the state of the `MotionValue`, stopping any active animations,
4429
- * effects, and resets velocity to `0`.
4430
- */
4431
- jump(v, endAnimation = true) {
4432
- this.updateAndNotify(v);
4433
- this.prev = v;
4434
- this.prevUpdatedAt = this.prevFrameValue = undefined;
4435
- endAnimation && this.stop();
4436
- if (this.stopPassiveEffect)
4437
- this.stopPassiveEffect();
4372
+ else if (parent === child) {
4373
+ return true;
4438
4374
  }
4439
- /**
4440
- * Returns the latest state of `MotionValue`
4441
- *
4442
- * @returns - The latest state of `MotionValue`
4443
- *
4444
- * @public
4445
- */
4446
- get() {
4447
- if (collectMotionValues.current) {
4448
- collectMotionValues.current.push(this);
4449
- }
4450
- return this.current;
4375
+ else {
4376
+ return isNodeOrChild(parent, child.parentElement);
4451
4377
  }
4452
- /**
4453
- * @public
4454
- */
4455
- getPrevious() {
4456
- return this.prev;
4378
+ };
4379
+
4380
+ const isPrimaryPointer = (event) => {
4381
+ if (event.pointerType === "mouse") {
4382
+ return typeof event.button !== "number" || event.button <= 0;
4457
4383
  }
4458
- /**
4459
- * Returns the latest velocity of `MotionValue`
4460
- *
4461
- * @returns - The latest velocity of `MotionValue`. Returns `0` if the state is non-numerical.
4462
- *
4463
- * @public
4464
- */
4465
- getVelocity() {
4466
- const currentTime = time.now();
4467
- if (!this.canTrackVelocity ||
4468
- this.prevFrameValue === undefined ||
4469
- currentTime - this.updatedAt > MAX_VELOCITY_DELTA) {
4470
- return 0;
4471
- }
4472
- const delta = Math.min(this.updatedAt - this.prevUpdatedAt, MAX_VELOCITY_DELTA);
4473
- // Casts because of parseFloat's poor typing
4474
- return velocityPerSecond(parseFloat(this.current) -
4475
- parseFloat(this.prevFrameValue), delta);
4384
+ else {
4385
+ /**
4386
+ * isPrimary is true for all mice buttons, whereas every touch point
4387
+ * is regarded as its own input. So subsequent concurrent touch points
4388
+ * will be false.
4389
+ *
4390
+ * Specifically match against false here as incomplete versions of
4391
+ * PointerEvents in very old browser might have it set as undefined.
4392
+ */
4393
+ return event.isPrimary !== false;
4476
4394
  }
4477
- /**
4478
- * Registers a new animation to control this `MotionValue`. Only one
4479
- * animation can drive a `MotionValue` at one time.
4480
- *
4481
- * ```jsx
4482
- * value.start()
4483
- * ```
4484
- *
4485
- * @param animation - A function that starts the provided animation
4486
- */
4487
- start(startAnimation) {
4488
- this.stop();
4489
- return new Promise((resolve) => {
4490
- this.hasAnimated = true;
4491
- this.animation = startAnimation(resolve);
4492
- if (this.events.animationStart) {
4493
- this.events.animationStart.notify();
4494
- }
4495
- }).then(() => {
4496
- if (this.events.animationComplete) {
4497
- this.events.animationComplete.notify();
4498
- }
4499
- this.clearAnimation();
4395
+ };
4396
+
4397
+ const focusableElements = new Set([
4398
+ "BUTTON",
4399
+ "INPUT",
4400
+ "SELECT",
4401
+ "TEXTAREA",
4402
+ "A",
4403
+ ]);
4404
+ function isElementKeyboardAccessible(element) {
4405
+ return (focusableElements.has(element.tagName) ||
4406
+ element.tabIndex !== -1);
4407
+ }
4408
+
4409
+ const isPressing = new WeakSet();
4410
+
4411
+ /**
4412
+ * Filter out events that are not "Enter" keys.
4413
+ */
4414
+ function filterEvents(callback) {
4415
+ return (event) => {
4416
+ if (event.key !== "Enter")
4417
+ return;
4418
+ callback(event);
4419
+ };
4420
+ }
4421
+ function firePointerEvent(target, type) {
4422
+ target.dispatchEvent(new PointerEvent("pointer" + type, { isPrimary: true, bubbles: true }));
4423
+ }
4424
+ const enableKeyboardPress = (focusEvent, eventOptions) => {
4425
+ const element = focusEvent.currentTarget;
4426
+ if (!element)
4427
+ return;
4428
+ const handleKeydown = filterEvents(() => {
4429
+ if (isPressing.has(element))
4430
+ return;
4431
+ firePointerEvent(element, "down");
4432
+ const handleKeyup = filterEvents(() => {
4433
+ firePointerEvent(element, "up");
4500
4434
  });
4501
- }
4435
+ const handleBlur = () => firePointerEvent(element, "cancel");
4436
+ element.addEventListener("keyup", handleKeyup, eventOptions);
4437
+ element.addEventListener("blur", handleBlur, eventOptions);
4438
+ });
4439
+ element.addEventListener("keydown", handleKeydown, eventOptions);
4502
4440
  /**
4503
- * Stop the currently active animation.
4504
- *
4505
- * @public
4441
+ * Add an event listener that fires on blur to remove the keydown events.
4506
4442
  */
4507
- stop() {
4508
- if (this.animation) {
4509
- this.animation.stop();
4510
- if (this.events.animationCancel) {
4511
- this.events.animationCancel.notify();
4443
+ element.addEventListener("blur", () => element.removeEventListener("keydown", handleKeydown), eventOptions);
4444
+ };
4445
+
4446
+ /**
4447
+ * Filter out events that are not primary pointer events, or are triggering
4448
+ * while a Motion gesture is active.
4449
+ */
4450
+ function isValidPressEvent(event) {
4451
+ return isPrimaryPointer(event) && !isDragActive();
4452
+ }
4453
+ /**
4454
+ * Create a press gesture.
4455
+ *
4456
+ * Press is different to `"pointerdown"`, `"pointerup"` in that it
4457
+ * automatically filters out secondary pointer events like right
4458
+ * click and multitouch.
4459
+ *
4460
+ * It also adds accessibility support for keyboards, where
4461
+ * an element with a press gesture will receive focus and
4462
+ * trigger on Enter `"keydown"` and `"keyup"` events.
4463
+ *
4464
+ * This is different to a browser's `"click"` event, which does
4465
+ * respond to keyboards but only for the `"click"` itself, rather
4466
+ * than the press start and end/cancel. The element also needs
4467
+ * to be focusable for this to work, whereas a press gesture will
4468
+ * make an element focusable by default.
4469
+ *
4470
+ * @public
4471
+ */
4472
+ function press(targetOrSelector, onPressStart, options = {}) {
4473
+ const [targets, eventOptions, cancelEvents] = setupGesture(targetOrSelector, options);
4474
+ const startPress = (startEvent) => {
4475
+ const target = startEvent.currentTarget;
4476
+ if (!isValidPressEvent(startEvent) || isPressing.has(target))
4477
+ return;
4478
+ isPressing.add(target);
4479
+ const onPressEnd = onPressStart(target, startEvent);
4480
+ const onPointerEnd = (endEvent, success) => {
4481
+ window.removeEventListener("pointerup", onPointerUp);
4482
+ window.removeEventListener("pointercancel", onPointerCancel);
4483
+ if (isPressing.has(target)) {
4484
+ isPressing.delete(target);
4485
+ }
4486
+ if (!isValidPressEvent(endEvent)) {
4487
+ return;
4488
+ }
4489
+ if (typeof onPressEnd === "function") {
4490
+ onPressEnd(endEvent, { success });
4491
+ }
4492
+ };
4493
+ const onPointerUp = (upEvent) => {
4494
+ onPointerEnd(upEvent, target === window ||
4495
+ target === document ||
4496
+ options.useGlobalTarget ||
4497
+ isNodeOrChild(target, upEvent.target));
4498
+ };
4499
+ const onPointerCancel = (cancelEvent) => {
4500
+ onPointerEnd(cancelEvent, false);
4501
+ };
4502
+ window.addEventListener("pointerup", onPointerUp, eventOptions);
4503
+ window.addEventListener("pointercancel", onPointerCancel, eventOptions);
4504
+ };
4505
+ targets.forEach((target) => {
4506
+ const pointerDownTarget = options.useGlobalTarget ? window : target;
4507
+ pointerDownTarget.addEventListener("pointerdown", startPress, eventOptions);
4508
+ if (target instanceof HTMLElement) {
4509
+ target.addEventListener("focus", (event) => enableKeyboardPress(event, eventOptions));
4510
+ if (!isElementKeyboardAccessible(target) &&
4511
+ !target.hasAttribute("tabindex")) {
4512
+ target.tabIndex = 0;
4512
4513
  }
4513
4514
  }
4514
- this.clearAnimation();
4515
+ });
4516
+ return cancelEvents;
4517
+ }
4518
+
4519
+ function getComputedStyle$2(element, name) {
4520
+ const computedStyle = window.getComputedStyle(element);
4521
+ return isCSSVar(name)
4522
+ ? computedStyle.getPropertyValue(name)
4523
+ : computedStyle[name];
4524
+ }
4525
+
4526
+ function observeTimeline(update, timeline) {
4527
+ let prevProgress;
4528
+ const onFrame = () => {
4529
+ const { currentTime } = timeline;
4530
+ const percentage = currentTime === null ? 0 : currentTime.value;
4531
+ const progress = percentage / 100;
4532
+ if (prevProgress !== progress) {
4533
+ update(progress);
4534
+ }
4535
+ prevProgress = progress;
4536
+ };
4537
+ frame.preUpdate(onFrame, true);
4538
+ return () => cancelFrame(onFrame);
4539
+ }
4540
+
4541
+ function record() {
4542
+ const { value } = statsBuffer;
4543
+ if (value === null) {
4544
+ cancelFrame(record);
4545
+ return;
4515
4546
  }
4516
- /**
4517
- * Returns `true` if this value is currently animating.
4518
- *
4519
- * @public
4520
- */
4521
- isAnimating() {
4522
- return !!this.animation;
4547
+ value.frameloop.rate.push(frameData.delta);
4548
+ value.animations.mainThread.push(activeAnimations.mainThread);
4549
+ value.animations.waapi.push(activeAnimations.waapi);
4550
+ value.animations.layout.push(activeAnimations.layout);
4551
+ }
4552
+ function mean(values) {
4553
+ return values.reduce((acc, value) => acc + value, 0) / values.length;
4554
+ }
4555
+ function summarise(values, calcAverage = mean) {
4556
+ if (values.length === 0) {
4557
+ return {
4558
+ min: 0,
4559
+ max: 0,
4560
+ avg: 0,
4561
+ };
4523
4562
  }
4524
- clearAnimation() {
4525
- delete this.animation;
4563
+ return {
4564
+ min: Math.min(...values),
4565
+ max: Math.max(...values),
4566
+ avg: calcAverage(values),
4567
+ };
4568
+ }
4569
+ const msToFps = (ms) => Math.round(1000 / ms);
4570
+ function clearStatsBuffer() {
4571
+ statsBuffer.value = null;
4572
+ statsBuffer.addProjectionMetrics = null;
4573
+ }
4574
+ function reportStats() {
4575
+ const { value } = statsBuffer;
4576
+ if (!value) {
4577
+ throw new Error("Stats are not being measured");
4526
4578
  }
4579
+ clearStatsBuffer();
4580
+ cancelFrame(record);
4581
+ const summary = {
4582
+ frameloop: {
4583
+ setup: summarise(value.frameloop.setup),
4584
+ rate: summarise(value.frameloop.rate),
4585
+ read: summarise(value.frameloop.read),
4586
+ resolveKeyframes: summarise(value.frameloop.resolveKeyframes),
4587
+ preUpdate: summarise(value.frameloop.preUpdate),
4588
+ update: summarise(value.frameloop.update),
4589
+ preRender: summarise(value.frameloop.preRender),
4590
+ render: summarise(value.frameloop.render),
4591
+ postRender: summarise(value.frameloop.postRender),
4592
+ },
4593
+ animations: {
4594
+ mainThread: summarise(value.animations.mainThread),
4595
+ waapi: summarise(value.animations.waapi),
4596
+ layout: summarise(value.animations.layout),
4597
+ },
4598
+ layoutProjection: {
4599
+ nodes: summarise(value.layoutProjection.nodes),
4600
+ calculatedTargetDeltas: summarise(value.layoutProjection.calculatedTargetDeltas),
4601
+ calculatedProjections: summarise(value.layoutProjection.calculatedProjections),
4602
+ },
4603
+ };
4527
4604
  /**
4528
- * Destroy and clean up subscribers to this `MotionValue`.
4529
- *
4530
- * The `MotionValue` hooks like `useMotionValue` and `useTransform` automatically
4531
- * handle the lifecycle of the returned `MotionValue`, so this method is only necessary if you've manually
4532
- * created a `MotionValue` via the `motionValue` function.
4533
- *
4534
- * @public
4605
+ * Convert the rate to FPS
4535
4606
  */
4536
- destroy() {
4537
- this.events.destroy?.notify();
4538
- this.clearListeners();
4539
- this.stop();
4540
- if (this.stopPassiveEffect) {
4541
- this.stopPassiveEffect();
4542
- }
4607
+ const { rate } = summary.frameloop;
4608
+ rate.min = msToFps(rate.min);
4609
+ rate.max = msToFps(rate.max);
4610
+ rate.avg = msToFps(rate.avg);
4611
+ [rate.min, rate.max] = [rate.max, rate.min];
4612
+ return summary;
4613
+ }
4614
+ function recordStats() {
4615
+ if (statsBuffer.value) {
4616
+ clearStatsBuffer();
4617
+ throw new Error("Stats are already being measured");
4543
4618
  }
4619
+ const newStatsBuffer = statsBuffer;
4620
+ newStatsBuffer.value = {
4621
+ frameloop: {
4622
+ setup: [],
4623
+ rate: [],
4624
+ read: [],
4625
+ resolveKeyframes: [],
4626
+ preUpdate: [],
4627
+ update: [],
4628
+ preRender: [],
4629
+ render: [],
4630
+ postRender: [],
4631
+ },
4632
+ animations: {
4633
+ mainThread: [],
4634
+ waapi: [],
4635
+ layout: [],
4636
+ },
4637
+ layoutProjection: {
4638
+ nodes: [],
4639
+ calculatedTargetDeltas: [],
4640
+ calculatedProjections: [],
4641
+ },
4642
+ };
4643
+ newStatsBuffer.addProjectionMetrics = (metrics) => {
4644
+ const { layoutProjection } = newStatsBuffer.value;
4645
+ layoutProjection.nodes.push(metrics.nodes);
4646
+ layoutProjection.calculatedTargetDeltas.push(metrics.calculatedTargetDeltas);
4647
+ layoutProjection.calculatedProjections.push(metrics.calculatedProjections);
4648
+ };
4649
+ frame.postRender(record, true);
4650
+ return reportStats;
4544
4651
  }
4545
- function motionValue(init, options) {
4546
- return new MotionValue(init, options);
4652
+
4653
+ function transform(...args) {
4654
+ const useImmediate = !Array.isArray(args[0]);
4655
+ const argOffset = useImmediate ? 0 : -1;
4656
+ const inputValue = args[0 + argOffset];
4657
+ const inputRange = args[1 + argOffset];
4658
+ const outputRange = args[2 + argOffset];
4659
+ const options = args[3 + argOffset];
4660
+ const interpolator = interpolate(inputRange, outputRange, options);
4661
+ return useImmediate ? interpolator(inputValue) : interpolator;
4547
4662
  }
4548
4663
 
4549
4664
  function subscribeValue(inputValues, outputValue, getLatest) {
@@ -4638,15 +4753,6 @@
4638
4753
  */
4639
4754
  const findValueType = (v) => valueTypes.find(testValueType(v));
4640
4755
 
4641
- /**
4642
- * Provided a value and a ValueType, returns the value as that value type.
4643
- */
4644
- const getValueAsType = (value, type) => {
4645
- return type && typeof value === "number"
4646
- ? type.transform(value)
4647
- : value;
4648
- };
4649
-
4650
4756
  function chooseLayerType(valueName) {
4651
4757
  if (valueName === "layout")
4652
4758
  return "group";
@@ -12429,9 +12535,9 @@
12429
12535
  handler.notify();
12430
12536
  };
12431
12537
  const listener = () => {
12432
- frame.read(measureAll, false, true);
12433
- frame.read(updateAll, false, true);
12434
- frame.preUpdate(notifyAll, false, true);
12538
+ frame.read(measureAll);
12539
+ frame.read(updateAll);
12540
+ frame.preUpdate(notifyAll);
12435
12541
  };
12436
12542
  scrollListeners.set(container, listener);
12437
12543
  const target = getEventTarget(container);
@@ -12440,6 +12546,7 @@
12440
12546
  resizeListeners.set(container, resize(container, listener));
12441
12547
  }
12442
12548
  target.addEventListener("scroll", listener, { passive: true });
12549
+ listener();
12443
12550
  }
12444
12551
  const listener = scrollListeners.get(container);
12445
12552
  frame.read(listener, false, true);