@motion.page/sdk 1.2.2 → 1.2.4

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/README.md CHANGED
@@ -19,6 +19,7 @@ A high-performance animation SDK with a declarative API. Scroll-triggered animat
19
19
  - [Motion()](#motion-function)
20
20
  - [Motion Static Methods](#motion-static-methods)
21
21
  - [Motion.context](#motioncontext--dynamic-content--spa)
22
+ - [Motion.responsive](#motionresponsive--responsive-breakpoint-variants)
22
23
  - [Motion.utils](#motionutils)
23
24
  - [Timeline](#timeline)
24
25
  - [Triggers](#triggers)
@@ -284,6 +285,7 @@ Calling `Motion()` with the same name on an already-existing timeline returns it
284
285
  | `Motion.refreshScrollTriggers` | `(): void` | Recalculate all scroll trigger start/end positions (call after layout changes). |
285
286
  | `Motion.cleanup` | `(): void` | Remove ScrollTrigger spacer and marker DOM nodes from the document. |
286
287
  | `Motion.context` | `(fn: () => void): MotionContext` | Create a scoped context that tracks all timelines created during `fn()`. Returns a `MotionContext` with `revert()`, `refresh()`, and `add(fn)` methods. Essential for dynamic content (AJAX filters, SPA navigation, infinite scroll). |
288
+ | `Motion.responsive` | `(name, variants, breakpoints): ResponsiveManager` | Register a reactive timeline whose per-breakpoint variant is live-swapped as the viewport crosses a breakpoint — no page reload. `variants` is a sparse map of `desktop` / `laptop` / `tablet` / `phone` factories; `breakpoints` is `{ laptops, tablets, phones }` in px. Returns a manager with `getActiveTier()` and `destroy()`. |
287
289
 
288
290
  #### Examples
289
291
 
@@ -344,6 +346,53 @@ ctx.add(() => {
344
346
  });
345
347
  ```
346
348
 
349
+ #### Motion.responsive — Responsive Breakpoint Variants
350
+
351
+ Use `Motion.responsive()` to give one timeline a different animation per viewport tier. Instead of compiling a separate block per breakpoint, you register a **sparse map of variant factories** and the SDK live-swaps the active variant as the viewport crosses a breakpoint — it kills the previous tier's timeline and builds the new one, with **no page reload**.
352
+
353
+ ```ts
354
+ Motion.responsive(
355
+ 'hero',
356
+ {
357
+ // Each tier is a factory that builds AND registers its timeline.
358
+ desktop: () =>
359
+ Motion('hero', '.title', {
360
+ from: { opacity: 0, x: -120 },
361
+ duration: 1,
362
+ }).onPageLoad(),
363
+
364
+ phone: () =>
365
+ Motion('hero', '.title', {
366
+ from: { opacity: 0, y: 40 },
367
+ duration: 0.6,
368
+ }).onPageLoad(),
369
+ },
370
+ { laptops: 992, tablets: 768, phones: 576 } // breakpoint boundaries in px
371
+ );
372
+ ```
373
+
374
+ **Tiers & ranges.** There are four tiers — `desktop`, `laptop`, `tablet`, `phone`. The `breakpoints` object sets the boundaries, and each tier owns a width range (with the values above):
375
+
376
+ | Tier | Active range |
377
+ |------|--------------|
378
+ | `desktop` | `≥ 993px` (above `laptops`) |
379
+ | `laptop` | `769px – 992px` |
380
+ | `tablet` | `577px – 768px` |
381
+ | `phone` | `≤ 576px` (at or below `phones`) |
382
+
383
+ **Sparse maps cascade up.** You only define the tiers you care about. A tier with no variant falls back to the nearest **wider** tier that does. In the example above, `laptop` and `tablet` widths both use the `desktop` variant; only `phone` width switches to the phone variant.
384
+
385
+ **Return value — the manager.** `Motion.responsive()` returns a `ResponsiveManager`:
386
+
387
+ ```ts
388
+ const hero = Motion.responsive('hero', { /* variants */ }, { laptops: 992, tablets: 768, phones: 576 });
389
+
390
+ hero.getActiveTier(); // 'desktop' | 'laptop' | 'tablet' | 'phone' | null — the tier currently driving the page
391
+ hero.destroy(); // remove the breakpoint listeners and kill the active variant
392
+ ```
393
+
394
+ > In SSR or environments without `matchMedia`, the `desktop` variant is built once with no live swapping. If the responsive runtime is tree-shaken out of a custom bundle that has no responsive timelines, the call best-effort builds the `desktop` variant and returns `undefined`.
395
+
347
396
  ---
348
397
 
349
398
  ### Motion.utils
@@ -226,6 +226,13 @@ export declare class Animation {
226
226
  * @internal - Use only within sdk package for property inspection (e.g. captureStartValues).
227
227
  */
228
228
  getFirstPropTween(): PropTween | null;
229
+ /**
230
+ * Mark motion-path geometry stale on every prop tween so the geometry-dependent
231
+ * offsets re-resolve on the next render. Called after a layout-shifting event
232
+ * (window.load / resize ScrollTrigger refresh) so paths aligned to an
233
+ * asynchronously-sized target settle onto the correct position.
234
+ */
235
+ invalidatePathGeometry(): void;
229
236
  /**
230
237
  * Add a property tween to the linked list
231
238
  */
@@ -10,7 +10,7 @@ import type { ParsedFilterFunction } from '../utils/FilterParser';
10
10
  import type { ParsedDrawSVG } from '../utils/DrawSVGParser';
11
11
  import type { ParsedClipPath } from '../utils/ClipPathParser';
12
12
  import { type PathPoint } from '../utils/PathParser';
13
- type PropTweenValueType = 'scalar' | 'color' | 'filter' | 'drawSVG' | 'path' | 'clipPath';
13
+ type PropTweenValueType = 'scalar' | 'string' | 'color' | 'filter' | 'drawSVG' | 'path' | 'clipPath';
14
14
  export declare class PropTween {
15
15
  target: AnimationTarget | null;
16
16
  property: string;
@@ -22,6 +22,8 @@ export declare class PropTween {
22
22
  endUnit: string | null;
23
23
  next: PropTween | null;
24
24
  valueType: PropTweenValueType;
25
+ startString: string;
26
+ endString: string;
25
27
  startColor: Float32Array | null;
26
28
  endColor: Float32Array | null;
27
29
  changeColor: Float32Array;
@@ -46,11 +48,22 @@ export declare class PropTween {
46
48
  y: number;
47
49
  } | null;
48
50
  pathLUT: PathPoint[] | null;
51
+ pathAlignTarget: string | Element | null;
52
+ pathAlignAt: [number, number] | null;
53
+ pathTarget: string | Element | null;
54
+ pathScaleX: number;
55
+ pathScaleY: number;
49
56
  private _isElement;
57
+ private _pathGeomResolved;
50
58
  /**
51
59
  * Initialize the PropTween with scalar values
52
60
  */
53
61
  init(target: AnimationTarget, property: string, startValue: number, endValue: number, unit?: string, startUnit?: string, endUnit?: string): this;
62
+ /**
63
+ * Initialize the PropTween with a compound CSS string value
64
+ * (transform-origin, background-position, …).
65
+ */
66
+ initString(target: AnimationTarget, property: string, startString: string, endString: string): this;
54
67
  /**
55
68
  * Initialize the PropTween with color values
56
69
  */
@@ -77,7 +90,36 @@ export declare class PropTween {
77
90
  }, pathOffset: {
78
91
  x: number;
79
92
  y: number;
80
- }): this;
93
+ }, pathScale?: {
94
+ x: number;
95
+ y: number;
96
+ }, alignTarget?: string | Element, alignAt?: [number, number], pathTarget?: string | Element): this;
97
+ /**
98
+ * Sample the path into the lookup table at the given user-unit -> screen-px scale.
99
+ * Releases any previously-held LUT back to the pool before re-sampling.
100
+ */
101
+ private _buildPathLUT;
102
+ /**
103
+ * Resolve the geometry-dependent path offsets (alignOffset/pathOffset/pathScale)
104
+ * against the live, laid-out element.
105
+ *
106
+ * The offsets are first computed at parse time, but a target sized asynchronously
107
+ * (Lottie injecting its <svg>, lazy-loaded <img>, web-font reflow) can report a
108
+ * 0×0 / 0-height box then. That bakes `alignOffset = {x: w/2, y: 0}` — anchoring
109
+ * the element by its TOP edge instead of its CENTER — which is invisible on the
110
+ * straight runs of a path but drifts the element off curves (where it rotates).
111
+ *
112
+ * We therefore defer: each frame until the element has a real (non-degenerate)
113
+ * box, recompute from the live geometry, then lock in. Once resolved there is no
114
+ * per-frame cost. Only aligned DOM-element motion paths participate; raw path-data
115
+ * or plain-object targets keep their parse-time values unchanged.
116
+ */
117
+ private _resolvePathGeometryIfNeeded;
118
+ /**
119
+ * Mark the path geometry stale so it re-resolves on the next render. Called when
120
+ * layout may have shifted (window.load / resize ScrollTrigger refresh).
121
+ */
122
+ invalidatePathGeometry(): void;
81
123
  /**
82
124
  * Render the property at given progress (0-1)
83
125
  */
@@ -385,6 +385,12 @@ export declare class Timeline {
385
385
  * @internal
386
386
  */
387
387
  getFirstChildAnimation(): Animation | null;
388
+ /**
389
+ * Propagate a motion-path geometry invalidation to every descendant animation,
390
+ * so aligned path offsets re-resolve against post-layout geometry on the next
391
+ * render. Called from ScrollTrigger.refresh() (window.load / resize).
392
+ */
393
+ invalidatePathGeometry(): void;
388
394
  /**
389
395
  * Get first child builder (for split-aware trigger target resolution).
390
396
  * When text splitting is active, the builder holds original pre-split targets.