motion-dom 12.9.6 → 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.
@@ -2,7 +2,7 @@ import { invariant, pipe, clamp, millisecondsToSeconds, secondsToMilliseconds }
2
2
  import { time } from '../frameloop/sync-time.mjs';
3
3
  import { activeAnimations } from '../stats/animation-count.mjs';
4
4
  import { mix } from '../utils/mix/index.mjs';
5
- import { frameloopDriver } from './drivers/driver-frameloop.mjs';
5
+ import { frameloopDriver } from './drivers/frame.mjs';
6
6
  import { inertia } from './generators/inertia.mjs';
7
7
  import { keyframes } from './generators/keyframes.mjs';
8
8
  import { calcGeneratorDuration } from './generators/utils/calc-duration.mjs';
@@ -244,6 +244,7 @@ class JSAnimation extends WithPromise {
244
244
  else if (this.driver) {
245
245
  this.startTime = this.driver.now() - newTime / this.playbackSpeed;
246
246
  }
247
+ this.driver?.start(false);
247
248
  }
248
249
  get speed() {
249
250
  return this.playbackSpeed;
@@ -333,6 +334,7 @@ class JSAnimation extends WithPromise {
333
334
  this.options.ease = "linear";
334
335
  this.initAnimation();
335
336
  }
337
+ this.driver?.stop();
336
338
  return timeline.observe(this);
337
339
  }
338
340
  }
@@ -4,7 +4,7 @@ import { frame, cancelFrame, frameData } from '../../frameloop/frame.mjs';
4
4
  const frameloopDriver = (update) => {
5
5
  const passTimestamp = ({ timestamp }) => update(timestamp);
6
6
  return {
7
- start: () => frame.update(passTimestamp, true),
7
+ start: (keepAlive = true) => frame.update(passTimestamp, keepAlive),
8
8
  stop: () => cancelFrame(passTimestamp),
9
9
  /**
10
10
  * If we're processing this frame we can use the
@@ -0,0 +1,41 @@
1
+ import { frame, cancelFrame } from '../frameloop/frame.mjs';
2
+ import { numberValueTypes } from '../value/types/maps/number.mjs';
3
+ import { getValueAsType } from '../value/types/utils/get-as-type.mjs';
4
+
5
+ class MotionValueState {
6
+ constructor() {
7
+ this.latest = {};
8
+ this.values = new Map();
9
+ }
10
+ set(name, value, render, computed) {
11
+ const existingValue = this.values.get(name);
12
+ if (existingValue) {
13
+ existingValue.onRemove();
14
+ }
15
+ const onChange = () => {
16
+ this.latest[name] = getValueAsType(value.get(), numberValueTypes[name]);
17
+ render && frame.render(render);
18
+ };
19
+ onChange();
20
+ const cancelOnChange = value.on("change", onChange);
21
+ computed && value.addDependent(computed);
22
+ const remove = () => {
23
+ cancelOnChange();
24
+ render && cancelFrame(render);
25
+ this.values.delete(name);
26
+ computed && value.removeDependent(computed);
27
+ };
28
+ this.values.set(name, { value, onRemove: remove });
29
+ return remove;
30
+ }
31
+ get(name) {
32
+ return this.values.get(name)?.value;
33
+ }
34
+ destroy() {
35
+ for (const value of this.values.values()) {
36
+ value.onRemove();
37
+ }
38
+ }
39
+ }
40
+
41
+ export { MotionValueState };
@@ -0,0 +1,51 @@
1
+ import { isCSSVar } from '../../render/dom/is-css-var.mjs';
2
+ import { transformProps } from '../../render/utils/keys-transform.mjs';
3
+ import { resolveElements } from '../../utils/resolve-elements.mjs';
4
+ import { MotionValue } from '../../value/index.mjs';
5
+ import { MotionValueState } from '../MotionValueState.mjs';
6
+ import { buildTransform } from './transform.mjs';
7
+
8
+ const stateMap = new WeakMap();
9
+ function styleEffect(subject, values) {
10
+ const elements = resolveElements(subject);
11
+ const subscriptions = [];
12
+ for (let i = 0; i < elements.length; i++) {
13
+ const element = elements[i];
14
+ const state = stateMap.get(element) ?? new MotionValueState();
15
+ stateMap.set(element, state);
16
+ for (const key in values) {
17
+ const value = values[key];
18
+ const remove = addValue(element, state, key, value);
19
+ subscriptions.push(remove);
20
+ }
21
+ }
22
+ return () => {
23
+ for (const cancel of subscriptions)
24
+ cancel();
25
+ };
26
+ }
27
+ function addValue(element, state, key, value) {
28
+ let render = undefined;
29
+ let computed = undefined;
30
+ if (transformProps.has(key)) {
31
+ if (!state.get("transform")) {
32
+ state.set("transform", new MotionValue("none"), () => {
33
+ element.style.transform = buildTransform(state);
34
+ });
35
+ }
36
+ computed = state.get("transform");
37
+ }
38
+ else if (isCSSVar(key)) {
39
+ render = () => {
40
+ element.style.setProperty(key, state.latest[key]);
41
+ };
42
+ }
43
+ else {
44
+ render = () => {
45
+ element.style[key] = state.latest[key];
46
+ };
47
+ }
48
+ return state.set(key, value, render, computed);
49
+ }
50
+
51
+ export { styleEffect };
@@ -0,0 +1,38 @@
1
+ import { transformPropOrder } from '../../render/utils/keys-transform.mjs';
2
+
3
+ const translateAlias = {
4
+ x: "translateX",
5
+ y: "translateY",
6
+ z: "translateZ",
7
+ transformPerspective: "perspective",
8
+ };
9
+ function buildTransform(state) {
10
+ let transform = "";
11
+ let transformIsDefault = true;
12
+ /**
13
+ * Loop over all possible transforms in order, adding the ones that
14
+ * are present to the transform string.
15
+ */
16
+ for (let i = 0; i < transformPropOrder.length; i++) {
17
+ const key = transformPropOrder[i];
18
+ const value = state.latest[key];
19
+ if (value === undefined)
20
+ continue;
21
+ let valueIsDefault = true;
22
+ if (typeof value === "number") {
23
+ valueIsDefault = value === (key.startsWith("scale") ? 1 : 0);
24
+ }
25
+ else {
26
+ valueIsDefault = parseFloat(value) === 0;
27
+ }
28
+ if (!valueIsDefault) {
29
+ transformIsDefault = false;
30
+ const transformName = translateAlias[key] || key;
31
+ const valueToRender = state.latest[key];
32
+ transform += `${transformName}(${valueToRender}) `;
33
+ }
34
+ }
35
+ return transformIsDefault ? "none" : transform.trim();
36
+ }
37
+
38
+ export { buildTransform };
@@ -43,10 +43,12 @@ function press(targetOrSelector, onPressStart, options = {}) {
43
43
  const onPointerEnd = (endEvent, success) => {
44
44
  window.removeEventListener("pointerup", onPointerUp);
45
45
  window.removeEventListener("pointercancel", onPointerCancel);
46
- if (!isValidPressEvent(endEvent) || !isPressing.has(target)) {
46
+ if (isPressing.has(target)) {
47
+ isPressing.delete(target);
48
+ }
49
+ if (!isValidPressEvent(endEvent)) {
47
50
  return;
48
51
  }
49
- isPressing.delete(target);
50
52
  if (typeof onPressEnd === "function") {
51
53
  onPressEnd(endEvent, { success });
52
54
  }
package/dist/es/index.mjs CHANGED
@@ -31,7 +31,7 @@ export { supportsPartialKeyframes } from './animation/waapi/supports/partial-key
31
31
  export { supportsBrowserAnimation } from './animation/waapi/supports/waapi.mjs';
32
32
  export { acceleratedValues } from './animation/waapi/utils/accelerated-values.mjs';
33
33
  export { generateLinearEasing } from './animation/waapi/utils/linear.mjs';
34
- export { styleEffect } from './effects/style-effect.mjs';
34
+ export { styleEffect } from './effects/style/index.mjs';
35
35
  export { createRenderBatcher } from './frameloop/batcher.mjs';
36
36
  export { cancelMicrotask, microtask } from './frameloop/microtask.mjs';
37
37
  export { time } from './frameloop/sync-time.mjs';
@@ -58,6 +58,11 @@ class MotionValue {
58
58
  // Update update subscribers
59
59
  if (this.current !== this.prev) {
60
60
  this.events.change?.notify(this.current);
61
+ if (this.dependents) {
62
+ for (const dependent of this.dependents) {
63
+ dependent.dirty();
64
+ }
65
+ }
61
66
  }
62
67
  // Update render subscribers
63
68
  if (render) {
@@ -199,6 +204,20 @@ class MotionValue {
199
204
  if (this.stopPassiveEffect)
200
205
  this.stopPassiveEffect();
201
206
  }
207
+ dirty() {
208
+ this.events.change?.notify(this.current);
209
+ }
210
+ addDependent(dependent) {
211
+ if (!this.dependents) {
212
+ this.dependents = new Set();
213
+ }
214
+ this.dependents.add(dependent);
215
+ }
216
+ removeDependent(dependent) {
217
+ if (this.dependents) {
218
+ this.dependents.delete(dependent);
219
+ }
220
+ }
202
221
  /**
203
222
  * Returns the latest state of `MotionValue`
204
223
  *
@@ -297,6 +316,7 @@ class MotionValue {
297
316
  * @public
298
317
  */
299
318
  destroy() {
319
+ this.dependents?.clear();
300
320
  this.events.destroy?.notify();
301
321
  this.clearListeners();
302
322
  this.stop();
package/dist/index.d.ts CHANGED
@@ -254,7 +254,7 @@ type Update$1 = (timestamp: number) => void;
254
254
  * could be a synchronous loop, a setInterval, or tied to the device's framerate.
255
255
  */
256
256
  interface DriverControls {
257
- start: () => void;
257
+ start: (keepAlive?: boolean) => void;
258
258
  stop: () => void;
259
259
  now: () => number;
260
260
  }
@@ -580,6 +580,11 @@ declare class MotionValue<V = any> {
580
580
  * A reference to the currently-controlling animation.
581
581
  */
582
582
  animation?: AnimationPlaybackControlsWithThen;
583
+ /**
584
+ * A list of MotionValues whose values are computed from this one.
585
+ * This is a rough start to a proper signal-like dirtying system.
586
+ */
587
+ private dependents;
583
588
  /**
584
589
  * Tracks whether this value should be removed
585
590
  */
@@ -666,6 +671,9 @@ declare class MotionValue<V = any> {
666
671
  * effects, and resets velocity to `0`.
667
672
  */
668
673
  jump(v: V, endAnimation?: boolean): void;
674
+ dirty(): void;
675
+ addDependent(dependent: MotionValue): void;
676
+ removeDependent(dependent: MotionValue): void;
669
677
  updateAndNotify: (v: V, render?: boolean) => void;
670
678
  /**
671
679
  * Returns the latest state of `MotionValue`