@studiometa/ui-motion 1.11.0 → 2.0.0-alpha.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/Motion.d.ts +17 -7
- package/dist/Motion.js +16 -29
- package/dist/Motion.js.map +1 -1
- package/dist/MotionScrollTimeline.d.ts +8 -9
- package/dist/MotionScrollTimeline.js +16 -12
- package/dist/MotionScrollTimeline.js.map +1 -1
- package/dist/MotionSequence.d.ts +19 -4
- package/dist/MotionSequence.js +24 -2
- package/dist/MotionSequence.js.map +1 -1
- package/dist/MotionView.d.ts +40 -17
- package/dist/MotionView.js +46 -42
- package/dist/MotionView.js.map +1 -1
- package/dist/dependencies.d.ts +6 -4
- package/dist/dependencies.js +6 -4
- package/dist/dependencies.js.map +1 -1
- package/dist/manifest.js +4 -26
- package/dist/manifest.js.map +1 -1
- package/dist/scripts/manifest-types.d.ts +3 -3
- package/package.json +3 -3
package/dist/Motion.d.ts
CHANGED
|
@@ -18,6 +18,17 @@ interface MotionProps extends BaseProps {
|
|
|
18
18
|
inViewAmount: string;
|
|
19
19
|
once: boolean;
|
|
20
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* The playback lifecycle. None of these events carries a payload — a listener
|
|
23
|
+
* that needs the animation reads it from the component.
|
|
24
|
+
*/
|
|
25
|
+
$emits: {
|
|
26
|
+
'motion-play': void;
|
|
27
|
+
'motion-pause': void;
|
|
28
|
+
'motion-complete': void;
|
|
29
|
+
'motion-cancel': void;
|
|
30
|
+
'motion-stop': void;
|
|
31
|
+
};
|
|
21
32
|
}
|
|
22
33
|
/**
|
|
23
34
|
* Motion class.
|
|
@@ -102,10 +113,14 @@ declare class Motion<T extends BaseProps = BaseProps> extends Base<MotionProps &
|
|
|
102
113
|
*/
|
|
103
114
|
get __hasDeclaredAnimation(): boolean;
|
|
104
115
|
/**
|
|
105
|
-
* Stop the current animation and release the gesture bindings on
|
|
116
|
+
* Stop the current animation and release the gesture bindings on unmount,
|
|
106
117
|
* keeping the styles the animation reached.
|
|
118
|
+
*
|
|
119
|
+
* `unmounted()` rather than a cleanup returned by `mounted()`: the current
|
|
120
|
+
* animation is not owned by the mount — any playback call creates it — so
|
|
121
|
+
* the teardown does not pair with a piece of setup.
|
|
107
122
|
*/
|
|
108
|
-
|
|
123
|
+
unmounted(): void;
|
|
109
124
|
/**
|
|
110
125
|
* Play the animation declared by the `animate` and `transition` options
|
|
111
126
|
* forward, creating it on first play — or recreating it when an imperative
|
|
@@ -228,11 +243,6 @@ declare class Motion<T extends BaseProps = BaseProps> extends Base<MotionProps &
|
|
|
228
243
|
* @private
|
|
229
244
|
*/
|
|
230
245
|
__watchCompletion(controls: AnimationPlaybackControlsWithThen): Promise<void>;
|
|
231
|
-
/**
|
|
232
|
-
* Dispatch a bubbling event so an ancestor `Action` can catch and route it.
|
|
233
|
-
* @private
|
|
234
|
-
*/
|
|
235
|
-
__dispatch(name: string, ...detail: unknown[]): void;
|
|
236
246
|
}
|
|
237
247
|
//#endregion
|
|
238
248
|
export { Motion, Motion as default, MotionProps };
|
package/dist/Motion.js
CHANGED
|
@@ -27,13 +27,6 @@ var Motion = class extends Base {
|
|
|
27
27
|
*/
|
|
28
28
|
static config = {
|
|
29
29
|
name: "Motion",
|
|
30
|
-
emits: [
|
|
31
|
-
"motion-play",
|
|
32
|
-
"motion-pause",
|
|
33
|
-
"motion-complete",
|
|
34
|
-
"motion-cancel",
|
|
35
|
-
"motion-stop"
|
|
36
|
-
],
|
|
37
30
|
options: {
|
|
38
31
|
initial: Object,
|
|
39
32
|
animate: Object,
|
|
@@ -124,10 +117,14 @@ var Motion = class extends Base {
|
|
|
124
117
|
return Object.keys(this.$options.animate).length > 0;
|
|
125
118
|
}
|
|
126
119
|
/**
|
|
127
|
-
* Stop the current animation and release the gesture bindings on
|
|
120
|
+
* Stop the current animation and release the gesture bindings on unmount,
|
|
128
121
|
* keeping the styles the animation reached.
|
|
122
|
+
*
|
|
123
|
+
* `unmounted()` rather than a cleanup returned by `mounted()`: the current
|
|
124
|
+
* animation is not owned by the mount — any playback call creates it — so
|
|
125
|
+
* the teardown does not pair with a piece of setup.
|
|
129
126
|
*/
|
|
130
|
-
|
|
127
|
+
unmounted() {
|
|
131
128
|
this.__completionToken += 1;
|
|
132
129
|
this.__controls?.stop();
|
|
133
130
|
this.__controls = null;
|
|
@@ -149,7 +146,7 @@ var Motion = class extends Base {
|
|
|
149
146
|
}
|
|
150
147
|
controls.speed = Math.abs(controls.speed || 1);
|
|
151
148
|
controls.play();
|
|
152
|
-
this
|
|
149
|
+
this.$emit("motion-play");
|
|
153
150
|
return this.__watchCompletion(controls);
|
|
154
151
|
}
|
|
155
152
|
/**
|
|
@@ -168,7 +165,7 @@ var Motion = class extends Base {
|
|
|
168
165
|
}
|
|
169
166
|
controls.speed = -Math.abs(controls.speed || 1);
|
|
170
167
|
controls.play();
|
|
171
|
-
this
|
|
168
|
+
this.$emit("motion-play");
|
|
172
169
|
return this.__watchCompletion(controls);
|
|
173
170
|
}
|
|
174
171
|
/**
|
|
@@ -177,7 +174,7 @@ var Motion = class extends Base {
|
|
|
177
174
|
pause() {
|
|
178
175
|
if (!this.__controls) return;
|
|
179
176
|
this.__controls.pause();
|
|
180
|
-
this
|
|
177
|
+
this.$emit("motion-pause");
|
|
181
178
|
}
|
|
182
179
|
/**
|
|
183
180
|
* Animate to the given keyframes, replacing the current animation. The
|
|
@@ -194,7 +191,7 @@ var Motion = class extends Base {
|
|
|
194
191
|
});
|
|
195
192
|
this.__controls = controls;
|
|
196
193
|
this.__fromOptions = false;
|
|
197
|
-
this
|
|
194
|
+
this.$emit("motion-play");
|
|
198
195
|
return this.__watchCompletion(controls);
|
|
199
196
|
}
|
|
200
197
|
/**
|
|
@@ -219,7 +216,7 @@ var Motion = class extends Base {
|
|
|
219
216
|
this.__completionToken += 1;
|
|
220
217
|
this.__controls.stop();
|
|
221
218
|
this.__controls = null;
|
|
222
|
-
this
|
|
219
|
+
this.$emit("motion-stop");
|
|
223
220
|
}
|
|
224
221
|
/**
|
|
225
222
|
* Cancel the current animation, reverting the element to its pre-animation
|
|
@@ -230,7 +227,7 @@ var Motion = class extends Base {
|
|
|
230
227
|
this.__completionToken += 1;
|
|
231
228
|
this.__controls.cancel();
|
|
232
229
|
this.__controls = null;
|
|
233
|
-
this
|
|
230
|
+
this.$emit("motion-cancel");
|
|
234
231
|
}
|
|
235
232
|
/**
|
|
236
233
|
* Jump the current animation to its end state.
|
|
@@ -336,7 +333,7 @@ var Motion = class extends Base {
|
|
|
336
333
|
* @private
|
|
337
334
|
*/
|
|
338
335
|
__warnMissingGesture(name) {
|
|
339
|
-
this.$warn(`The resolved motion module has no \`${name}()\` (e.g. \`motion/mini\`). Provide the full \`motion\` entry to use the \`${name}\` option.`);
|
|
336
|
+
this.$warn("motion.missing-gesture", `The resolved motion module has no \`${name}()\` (e.g. \`motion/mini\`). Provide the full \`motion\` entry to use the \`${name}\` option.`);
|
|
340
337
|
}
|
|
341
338
|
/**
|
|
342
339
|
* Create the animation from the `animate` and `transition` options and make
|
|
@@ -364,8 +361,8 @@ var Motion = class extends Base {
|
|
|
364
361
|
* current state is the point.
|
|
365
362
|
*/
|
|
366
363
|
get keyframes() {
|
|
367
|
-
const
|
|
368
|
-
const keyframes = { ...animate };
|
|
364
|
+
const initial = this.$options.initial;
|
|
365
|
+
const keyframes = { ...this.$options.animate };
|
|
369
366
|
for (const [property, to] of Object.entries(keyframes)) if (!Array.isArray(to) && property in initial) keyframes[property] = [initial[property], to];
|
|
370
367
|
return keyframes;
|
|
371
368
|
}
|
|
@@ -380,21 +377,11 @@ var Motion = class extends Base {
|
|
|
380
377
|
const token = ++this.__completionToken;
|
|
381
378
|
return new Promise((resolve) => {
|
|
382
379
|
controls.then(() => {
|
|
383
|
-
if (token === this.__completionToken && this.__controls === controls) this
|
|
380
|
+
if (token === this.__completionToken && this.__controls === controls) this.$emit("motion-complete");
|
|
384
381
|
resolve();
|
|
385
382
|
}, resolve);
|
|
386
383
|
});
|
|
387
384
|
}
|
|
388
|
-
/**
|
|
389
|
-
* Dispatch a bubbling event so an ancestor `Action` can catch and route it.
|
|
390
|
-
* @private
|
|
391
|
-
*/
|
|
392
|
-
__dispatch(name, ...detail) {
|
|
393
|
-
this.$emit(new CustomEvent(name, {
|
|
394
|
-
detail,
|
|
395
|
-
bubbles: true
|
|
396
|
-
}));
|
|
397
|
-
}
|
|
398
385
|
};
|
|
399
386
|
//#endregion
|
|
400
387
|
export { Motion, Motion as default };
|
package/dist/Motion.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Motion.js","names":[],"sources":["../src/Motion.ts"],"sourcesContent":["import { Base } from '@studiometa/js-toolkit/Base';\nimport type { BaseProps, BaseConfig } from '@studiometa/js-toolkit';\nimport type {\n AnimationOptions,\n AnimationPlaybackControlsWithThen,\n DOMKeyframesDefinition,\n} from 'motion';\nimport { getMotion, resolveMotion } from './dependencies.js';\nimport type { MotionModule } from './dependencies.js';\n\nexport interface MotionProps extends BaseProps {\n $options: {\n initial: DOMKeyframesDefinition;\n animate: DOMKeyframesDefinition;\n transition: AnimationOptions;\n autoplay: boolean;\n at: string;\n hover: DOMKeyframesDefinition;\n press: DOMKeyframesDefinition;\n inView: DOMKeyframesDefinition;\n inViewMargin: string;\n inViewAmount: string;\n once: boolean;\n };\n}\n\n/**\n * Motion class.\n *\n * Animate the component's root element declaratively with the\n * [Motion](https://motion.dev) library. The `initial` styles are applied on\n * mount, then the `animate` keyframes play when playback on mount is enabled\n * with `data-option-autoplay`.\n *\n * The component holds a single current animation. `play()` and `reverse()`\n * always drive the animation declared by the options — recreating it when an\n * imperative {@link Motion.animate} call superseded it — while the other\n * playback methods (`pause`, `seek`, `stop`, ...) act on whichever animation\n * is current. An `Action` can control it from any interaction:\n * `data-on:click=\"Motion->target.play()\"`.\n *\n * Events bubble, so an ancestor `Action` can catch and route them; use the\n * `.stop` event modifier to contain them in nested setups.\n *\n * @link https://ui.studiometa.dev/reference/items/Motion/\n */\nexport class Motion<T extends BaseProps = BaseProps> extends Base<MotionProps & T> {\n /**\n * Config.\n */\n static config: BaseConfig = {\n name: 'Motion',\n emits: ['motion-play', 'motion-pause', 'motion-complete', 'motion-cancel', 'motion-stop'],\n options: {\n initial: Object,\n animate: Object,\n transition: Object,\n autoplay: Boolean,\n at: String,\n hover: Object,\n press: Object,\n inView: Object,\n inViewMargin: String,\n inViewAmount: String,\n once: Boolean,\n },\n };\n\n /**\n * The current animation playback controls, or `null` when idle.\n * @private\n */\n __controls: AnimationPlaybackControlsWithThen | null = null;\n\n /**\n * Monotonic token invalidating stale completion watchers whenever the\n * current playback changes (new play, stop, cancel, destroy).\n * @private\n */\n __completionToken = 0;\n\n /**\n * The stop function of each gesture binding, released on destroy.\n * @private\n */\n __gestureStops: VoidFunction[] = [];\n\n /**\n * The base value of every property a gesture animated, captured the first\n * time a gesture touched it — the values a gesture returns to when it ends.\n * @private\n */\n __baseValues: Record<string, string | number> = {};\n\n /**\n * Whether the current animation was built from the `animate` and\n * `transition` options, as opposed to an imperative `animate()` call.\n * @private\n */\n __fromOptions = false;\n\n /**\n * The current animation playback controls, exposed for advanced use from\n * `Action` effects (e.g. `Motion->target.controls.speed = 2`).\n */\n get controls(): AnimationPlaybackControlsWithThen | null {\n return this.__controls;\n }\n\n /**\n * The current playback time in seconds.\n */\n get time(): number {\n return this.__controls?.time ?? 0;\n }\n\n /**\n * The current animation duration in seconds.\n */\n get duration(): number {\n return this.__controls?.duration ?? 0;\n }\n\n /**\n * The current playback progress, from 0 to 1.\n */\n get progress(): number {\n const duration = this.duration;\n return duration > 0 ? this.time / duration : 0;\n }\n\n /**\n * Apply the `initial` styles, play the `animate` keyframes when `autoplay`\n * is enabled, then bind the gesture options.\n */\n async mounted() {\n const { initial, autoplay } = this.$options;\n const motion = await resolveMotion();\n\n if (!this.$isMounted) {\n return;\n }\n\n if (Object.keys(initial).length > 0) {\n motion.animate(this.$el, initial, { duration: 0 });\n }\n\n if (autoplay && this.__hasDeclaredAnimation) {\n this.play();\n }\n\n this.__bindGestures(motion);\n }\n\n /**\n * Whether the options declare an animation to play — the gate for\n * `autoplay`. Subclasses building their animation from another source\n * (e.g. `MotionSequence` from its children) override this.\n * @protected\n */\n get __hasDeclaredAnimation(): boolean {\n return Object.keys(this.$options.animate).length > 0;\n }\n\n /**\n * Stop the current animation and release the gesture bindings on destroy,\n * keeping the styles the animation reached.\n */\n destroyed() {\n this.__completionToken += 1;\n this.__controls?.stop();\n this.__controls = null;\n\n for (const stop of this.__gestureStops) {\n stop();\n }\n this.__gestureStops = [];\n }\n\n /**\n * Play the animation declared by the `animate` and `transition` options\n * forward, creating it on first play — or recreating it when an imperative\n * `animate()` call superseded it. Restarts a finished animation. The\n * returned promise resolves when the animation settles.\n */\n async play(): Promise<void> {\n await resolveMotion();\n let controls = this.__controls;\n\n if (!controls || !this.__fromOptions) {\n controls?.stop();\n controls = this.__createControls();\n }\n\n controls.speed = Math.abs(controls.speed || 1);\n controls.play();\n this.__dispatch('motion-play');\n return this.__watchCompletion(controls);\n }\n\n /**\n * Play the animation declared by the options backward. When nothing has\n * played yet — or when an imperative `animate()` call superseded the\n * declared animation — it is created at its end so it plays back to its\n * starting styles. The returned promise resolves when the animation settles.\n */\n async reverse(): Promise<void> {\n await resolveMotion();\n let controls = this.__controls;\n\n if (!controls || !this.__fromOptions) {\n controls?.stop();\n controls = this.__createControls();\n controls.time = controls.duration;\n }\n\n controls.speed = -Math.abs(controls.speed || 1);\n controls.play();\n this.__dispatch('motion-play');\n return this.__watchCompletion(controls);\n }\n\n /**\n * Pause the current animation in place.\n */\n pause() {\n if (!this.__controls) {\n return;\n }\n\n this.__controls.pause();\n this.__dispatch('motion-pause');\n }\n\n /**\n * Animate to the given keyframes, replacing the current animation. The\n * options are merged over the `transition` option. The returned promise\n * resolves when the animation settles.\n */\n async animate(keyframes: DOMKeyframesDefinition, options?: AnimationOptions): Promise<void> {\n const motion = await resolveMotion();\n this.__completionToken += 1;\n this.__controls?.stop();\n const controls = motion.animate(this.$el, keyframes, {\n ...this.$options.transition,\n ...options,\n });\n this.__controls = controls;\n this.__fromOptions = false;\n this.__dispatch('motion-play');\n return this.__watchCompletion(controls);\n }\n\n /**\n * Seek the current animation to the given progress (0 to 1), creating it\n * paused when nothing has played yet.\n */\n async seek(progress: number) {\n await resolveMotion();\n let controls = this.__controls;\n\n if (!controls) {\n controls = this.__createControls();\n controls.pause();\n }\n\n controls.time = Math.min(Math.max(progress, 0), 1) * controls.duration;\n }\n\n /**\n * Stop the current animation, committing the styles it reached. The next\n * `play()` creates a fresh animation.\n */\n stop() {\n if (!this.__controls) {\n return;\n }\n\n this.__completionToken += 1;\n this.__controls.stop();\n this.__controls = null;\n this.__dispatch('motion-stop');\n }\n\n /**\n * Cancel the current animation, reverting the element to its pre-animation\n * styles. The next `play()` creates a fresh animation.\n */\n cancel() {\n if (!this.__controls) {\n return;\n }\n\n this.__completionToken += 1;\n this.__controls.cancel();\n this.__controls = null;\n this.__dispatch('motion-cancel');\n }\n\n /**\n * Jump the current animation to its end state.\n */\n complete() {\n this.__controls?.complete();\n }\n\n /**\n * Bind the `hover`, `press` and `inView` gesture options with Motion's own\n * gesture functions (touch-filtered, keyboard-accessible press, real\n * IntersectionObserver). Nothing binds when an option is empty, so idle\n * elements pay nothing. Gesture animations are transient: they never become\n * the current animation and emit no lifecycle events.\n * @private\n */\n __bindGestures(motion: MotionModule) {\n const { hover, press, inView, inViewMargin, inViewAmount, once } = this.$options;\n\n if (Object.keys(hover).length > 0) {\n if (motion.hover) {\n this.__gestureStops.push(\n motion.hover(this.$el, () => {\n this.__startGesture(hover);\n return () => this.__revertGesture(hover);\n }),\n );\n } else {\n this.__warnMissingGesture('hover');\n }\n }\n\n if (Object.keys(press).length > 0) {\n if (motion.press) {\n this.__gestureStops.push(\n motion.press(this.$el, () => {\n this.__startGesture(press);\n return () => this.__revertGesture(press);\n }),\n );\n } else {\n this.__warnMissingGesture('press');\n }\n }\n\n if (Object.keys(inView).length > 0) {\n if (motion.inView) {\n const options: { margin?: string; amount?: 'some' | 'all' | number } = {};\n if (inViewMargin) {\n options.margin = inViewMargin;\n }\n if (inViewAmount) {\n const numeric = Number(inViewAmount);\n options.amount = Number.isNaN(numeric) ? (inViewAmount as 'some' | 'all') : numeric;\n }\n this.__gestureStops.push(\n motion.inView(\n this.$el,\n () => {\n this.__startGesture(inView);\n // With no leave handler returned, `inView()` fires once and the\n // reached styles persist.\n if (once) {\n return undefined;\n }\n return () => this.__revertGesture(inView);\n },\n options as Parameters<NonNullable<MotionModule['inView']>>[2],\n ),\n );\n } else {\n this.__warnMissingGesture('inView');\n }\n }\n }\n\n /**\n * Animate to a gesture state, alongside — not replacing — the current\n * animation, capturing the base value of every property it touches first.\n * @private\n */\n __startGesture(keyframes: DOMKeyframesDefinition): AnimationPlaybackControlsWithThen {\n this.__captureBaseValues(keyframes);\n return getMotion().animate(this.$el, keyframes, this.$options.transition);\n }\n\n /**\n * Capture the element's current value for every property a gesture is about\n * to animate, once per property and never refreshed afterwards.\n *\n * The read mirrors Motion's own `VisualElement.readValueFromInstance()`: the\n * parsed transform matrix for a transform property, the computed style\n * otherwise — both through Motion's own readers, so the values come back in\n * the shape its animations expect. Motion React hydrates the same snapshot\n * (its `baseTarget`) the first time it reads a value and keeps it, which is\n * what makes a return immune to a second gesture starting while the previous\n * one is still returning.\n *\n * A property the readers cannot resolve is left out, so nothing is invented:\n * the return animation simply does not touch it.\n * @private\n */\n __captureBaseValues(keyframes: DOMKeyframesDefinition) {\n const { transformProps, readTransformValue, getComputedStyle } = getMotion();\n\n for (const property of Object.keys(keyframes)) {\n if (property in this.__baseValues) {\n continue;\n }\n\n if (readTransformValue && transformProps?.has(property)) {\n this.__baseValues[property] = readTransformValue(this.$el, property);\n continue;\n }\n\n const computed = getComputedStyle?.(this.$el, property);\n\n if (typeof computed === 'string' && computed !== '') {\n const numeric = Number(computed);\n this.__baseValues[property] = Number.isNaN(numeric) ? computed : numeric;\n }\n }\n }\n\n /**\n * Return the element to its base values when a gesture ends, animating\n * forward with the `transition` option — the gesture animation is never\n * rewound.\n *\n * This is what Motion React does: ending a gesture deactivates its variant\n * (`node.animationState.setActive(\"whileHover\", false)`), which recomputes\n * the target of every property the variant owned and animates to it. Playing\n * the gesture animation backward instead would run its easing or spring\n * curve in reverse — a different feel — and could never reach a base value\n * the element did not hold when the gesture started.\n * @private\n */\n __revertGesture(keyframes: DOMKeyframesDefinition) {\n const base: Record<string, string | number> = {};\n\n for (const property of Object.keys(keyframes)) {\n if (property in this.__baseValues) {\n base[property] = this.__baseValues[property];\n }\n }\n\n if (Object.keys(base).length === 0) {\n return;\n }\n\n getMotion().animate(this.$el, base as DOMKeyframesDefinition, this.$options.transition);\n }\n\n /**\n * Warn about a gesture option the resolved motion module cannot honor.\n * @private\n */\n __warnMissingGesture(name: string) {\n this.$warn(\n `The resolved motion module has no \\`${name}()\\` (e.g. \\`motion/mini\\`). Provide the full \\`motion\\` entry to use the \\`${name}\\` option.`,\n );\n }\n\n /**\n * Create the animation from the `animate` and `transition` options and make\n * it the current one. Subclasses override this to build the current\n * animation from another source.\n * @protected\n */\n __createControls(): AnimationPlaybackControlsWithThen {\n const { transition } = this.$options;\n const controls = getMotion().animate(this.$el, this.keyframes, transition);\n this.__controls = controls;\n this.__fromOptions = true;\n return controls;\n }\n\n /**\n * The keyframes of the declared animation, with the `initial` styles folded\n * in as the starting point of every property they both describe.\n *\n * Motion reads a lone target value as \"from wherever the element is now\", so\n * a settled animation would have nothing left to move on the next `play()`.\n * Pairing each such value with its `initial` counterpart pins the start, and\n * the animation replays identically for as long as the options stand. A\n * property already written as a `[from, to]` array is left untouched, and so\n * is one the `initial` styles say nothing about — there, animating from the\n * current state is the point.\n */\n get keyframes(): DOMKeyframesDefinition {\n const { initial, animate } = this.$options;\n const keyframes: Record<string, unknown> = { ...animate };\n\n for (const [property, to] of Object.entries(keyframes)) {\n if (!Array.isArray(to) && property in initial) {\n keyframes[property] = [initial[property], to];\n }\n }\n\n return keyframes as DOMKeyframesDefinition;\n }\n\n /**\n * Emit `motion-complete` when the given animation finishes, unless another\n * playback superseded it in the meantime. The returned promise resolves when\n * the animation settles and never rejects, so playback methods can safely\n * hand it to callers that do not catch.\n * @private\n */\n __watchCompletion(controls: AnimationPlaybackControlsWithThen): Promise<void> {\n const token = ++this.__completionToken;\n return new Promise((resolve) => {\n controls.then(() => {\n if (token === this.__completionToken && this.__controls === controls) {\n this.__dispatch('motion-complete');\n }\n resolve();\n }, resolve);\n });\n }\n\n /**\n * Dispatch a bubbling event so an ancestor `Action` can catch and route it.\n * @private\n */\n __dispatch(name: string, ...detail: unknown[]) {\n this.$emit(new CustomEvent(name, { detail, bubbles: true }));\n }\n}\n\nexport default Motion;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA8CA,IAAa,SAAb,cAA6D,KAAsB;;;;CAIjF,OAAO,SAAqB;EAC1B,MAAM;EACN,OAAO;GAAC;GAAe;GAAgB;GAAmB;GAAiB;GAAc;EACzF,SAAS;GACP,SAAS;GACT,SAAS;GACT,YAAY;GACZ,UAAU;GACV,IAAI;GACJ,OAAO;GACP,OAAO;GACP,QAAQ;GACR,cAAc;GACd,cAAc;GACd,MAAM;GACP;EACF;;;;;CAMD,aAAuD;;;;;;CAOvD,oBAAoB;;;;;CAMpB,iBAAiC,EAAE;;;;;;CAOnC,eAAgD,EAAE;;;;;;CAOlD,gBAAgB;;;;;CAMhB,IAAI,WAAqD;AACvD,SAAO,KAAK;;;;;CAMd,IAAI,OAAe;AACjB,SAAO,KAAK,YAAY,QAAQ;;;;;CAMlC,IAAI,WAAmB;AACrB,SAAO,KAAK,YAAY,YAAY;;;;;CAMtC,IAAI,WAAmB;EACrB,MAAM,WAAW,KAAK;AACtB,SAAO,WAAW,IAAI,KAAK,OAAO,WAAW;;;;;;CAO/C,MAAM,UAAU;EACd,MAAM,EAAE,SAAS,aAAa,KAAK;EACnC,MAAM,SAAS,MAAM,eAAe;AAEpC,MAAI,CA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,MAAgC,KAAK,SAAS,WAAW;;;;;;CAOzF,qBAAqB,MAAc;AACjC,OAAK,MACH,uCAAuC,KAAK,8EAA8E,KAAK,YAChI;;;;;;;;CASH,mBAAsD;EACpD,MAAM,EAAE,eAAe,KAAK;EAC5B,MAAM,WAAW,WAAW,CAAC,QAAQ,KAAK,KAAK,KAAK,WAAW,WAAW;AAC1E,OAAK,aAAa;AAClB,OAAK,gBAAgB;AACrB,SAAO;;;;;;;;;;;;;;CAeT,IAAI,YAAoC;EACtC,MAAM,EAAE,SAAS,YAAY,KAAK;EAClC,MAAM,YAAqC,EAAE,GAAG,SAAS;AAEzD,OAAK,MAAM,CAAC,UAAU,OAAO,OAAO,QAAQ,UAAU,CACpD,KAAI,CAAC,MAAM,QAAQ,GAAG,IAAI,YAAY,QACpC,WAAU,YAAY,CAAC,QAAQ,WAAW,GAAG;AAIjD,SAAO;;;;;;;;;CAUT,kBAAkB,UAA4D;EAC5E,MAAM,QAAQ,EAAE,KAAK;AACrB,SAAO,IAAI,SAAS,YAAY;AAC9B,YAAS,WAAW;AAClB,QAAI,UAAU,KAAK,qBAAqB,KAAK,eAAe,SAC1D,MAAK,WAAW,kBAAkB;AAEpC,aAAS;MACR,QAAQ;IACX;;;;;;CAOJ,WAAW,MAAc,GAAG,QAAmB;AAC7C,OAAK,MAAM,IAAI,YAAY,MAAM;GAAE;GAAQ,SAAS;GAAM,CAAC,CAAC"}
|
|
1
|
+
{"version":3,"file":"Motion.js","names":[],"sources":["../src/Motion.ts"],"sourcesContent":["import { Base } from '@studiometa/js-toolkit/Base';\nimport type { BaseProps, BaseConfig } from '@studiometa/js-toolkit';\nimport type {\n AnimationOptions,\n AnimationPlaybackControlsWithThen,\n DOMKeyframesDefinition,\n} from 'motion';\nimport { getMotion, resolveMotion } from './dependencies.js';\nimport type { MotionModule } from './dependencies.js';\n\nexport interface MotionProps extends BaseProps {\n $options: {\n initial: DOMKeyframesDefinition;\n animate: DOMKeyframesDefinition;\n transition: AnimationOptions;\n autoplay: boolean;\n at: string;\n hover: DOMKeyframesDefinition;\n press: DOMKeyframesDefinition;\n inView: DOMKeyframesDefinition;\n inViewMargin: string;\n inViewAmount: string;\n once: boolean;\n };\n /**\n * The playback lifecycle. None of these events carries a payload — a listener\n * that needs the animation reads it from the component.\n */\n $emits: {\n 'motion-play': void;\n 'motion-pause': void;\n 'motion-complete': void;\n 'motion-cancel': void;\n 'motion-stop': void;\n };\n}\n\n/**\n * Motion class.\n *\n * Animate the component's root element declaratively with the\n * [Motion](https://motion.dev) library. The `initial` styles are applied on\n * mount, then the `animate` keyframes play when playback on mount is enabled\n * with `data-option-autoplay`.\n *\n * The component holds a single current animation. `play()` and `reverse()`\n * always drive the animation declared by the options — recreating it when an\n * imperative {@link Motion.animate} call superseded it — while the other\n * playback methods (`pause`, `seek`, `stop`, ...) act on whichever animation\n * is current. An `Action` can control it from any interaction:\n * `data-on:click=\"Motion->target.play()\"`.\n *\n * Events bubble, so an ancestor `Action` can catch and route them; use the\n * `.stop` event modifier to contain them in nested setups.\n *\n * @link https://ui.studiometa.dev/reference/items/Motion/\n */\nexport class Motion<T extends BaseProps = BaseProps> extends Base<MotionProps & T> {\n /**\n * Config.\n */\n static config: BaseConfig = {\n name: 'Motion',\n options: {\n initial: Object,\n animate: Object,\n transition: Object,\n autoplay: Boolean,\n at: String,\n hover: Object,\n press: Object,\n inView: Object,\n inViewMargin: String,\n inViewAmount: String,\n once: Boolean,\n },\n };\n\n /**\n * The current animation playback controls, or `null` when idle.\n * @private\n */\n __controls: AnimationPlaybackControlsWithThen | null = null;\n\n /**\n * Monotonic token invalidating stale completion watchers whenever the\n * current playback changes (new play, stop, cancel, destroy).\n * @private\n */\n __completionToken = 0;\n\n /**\n * The stop function of each gesture binding, released on destroy.\n * @private\n */\n __gestureStops: VoidFunction[] = [];\n\n /**\n * The base value of every property a gesture animated, captured the first\n * time a gesture touched it — the values a gesture returns to when it ends.\n * @private\n */\n __baseValues: Record<string, string | number> = {};\n\n /**\n * Whether the current animation was built from the `animate` and\n * `transition` options, as opposed to an imperative `animate()` call.\n * @private\n */\n __fromOptions = false;\n\n /**\n * The current animation playback controls, exposed for advanced use from\n * `Action` effects (e.g. `Motion->target.controls.speed = 2`).\n */\n get controls(): AnimationPlaybackControlsWithThen | null {\n return this.__controls;\n }\n\n /**\n * The current playback time in seconds.\n */\n get time(): number {\n return this.__controls?.time ?? 0;\n }\n\n /**\n * The current animation duration in seconds.\n */\n get duration(): number {\n return this.__controls?.duration ?? 0;\n }\n\n /**\n * The current playback progress, from 0 to 1.\n */\n get progress(): number {\n const duration = this.duration;\n return duration > 0 ? this.time / duration : 0;\n }\n\n /**\n * Apply the `initial` styles, play the `animate` keyframes when `autoplay`\n * is enabled, then bind the gesture options.\n */\n async mounted() {\n const { initial, autoplay } = this.$options;\n const motion = await resolveMotion();\n\n if (!this.$isMounted) {\n return;\n }\n\n if (Object.keys(initial).length > 0) {\n motion.animate(this.$el, initial, { duration: 0 });\n }\n\n if (autoplay && this.__hasDeclaredAnimation) {\n this.play();\n }\n\n this.__bindGestures(motion);\n }\n\n /**\n * Whether the options declare an animation to play — the gate for\n * `autoplay`. Subclasses building their animation from another source\n * (e.g. `MotionSequence` from its children) override this.\n * @protected\n */\n get __hasDeclaredAnimation(): boolean {\n return Object.keys(this.$options.animate).length > 0;\n }\n\n /**\n * Stop the current animation and release the gesture bindings on unmount,\n * keeping the styles the animation reached.\n *\n * `unmounted()` rather than a cleanup returned by `mounted()`: the current\n * animation is not owned by the mount — any playback call creates it — so\n * the teardown does not pair with a piece of setup.\n */\n unmounted() {\n this.__completionToken += 1;\n this.__controls?.stop();\n this.__controls = null;\n\n for (const stop of this.__gestureStops) {\n stop();\n }\n this.__gestureStops = [];\n }\n\n /**\n * Play the animation declared by the `animate` and `transition` options\n * forward, creating it on first play — or recreating it when an imperative\n * `animate()` call superseded it. Restarts a finished animation. The\n * returned promise resolves when the animation settles.\n */\n async play(): Promise<void> {\n await resolveMotion();\n let controls = this.__controls;\n\n if (!controls || !this.__fromOptions) {\n controls?.stop();\n controls = this.__createControls();\n }\n\n controls.speed = Math.abs(controls.speed || 1);\n controls.play();\n this.$emit('motion-play');\n return this.__watchCompletion(controls);\n }\n\n /**\n * Play the animation declared by the options backward. When nothing has\n * played yet — or when an imperative `animate()` call superseded the\n * declared animation — it is created at its end so it plays back to its\n * starting styles. The returned promise resolves when the animation settles.\n */\n async reverse(): Promise<void> {\n await resolveMotion();\n let controls = this.__controls;\n\n if (!controls || !this.__fromOptions) {\n controls?.stop();\n controls = this.__createControls();\n controls.time = controls.duration;\n }\n\n controls.speed = -Math.abs(controls.speed || 1);\n controls.play();\n this.$emit('motion-play');\n return this.__watchCompletion(controls);\n }\n\n /**\n * Pause the current animation in place.\n */\n pause() {\n if (!this.__controls) {\n return;\n }\n\n this.__controls.pause();\n this.$emit('motion-pause');\n }\n\n /**\n * Animate to the given keyframes, replacing the current animation. The\n * options are merged over the `transition` option. The returned promise\n * resolves when the animation settles.\n */\n async animate(keyframes: DOMKeyframesDefinition, options?: AnimationOptions): Promise<void> {\n const motion = await resolveMotion();\n this.__completionToken += 1;\n this.__controls?.stop();\n const controls = motion.animate(this.$el, keyframes, {\n ...this.$options.transition,\n ...options,\n });\n this.__controls = controls;\n this.__fromOptions = false;\n this.$emit('motion-play');\n return this.__watchCompletion(controls);\n }\n\n /**\n * Seek the current animation to the given progress (0 to 1), creating it\n * paused when nothing has played yet.\n */\n async seek(progress: number) {\n await resolveMotion();\n let controls = this.__controls;\n\n if (!controls) {\n controls = this.__createControls();\n controls.pause();\n }\n\n controls.time = Math.min(Math.max(progress, 0), 1) * controls.duration;\n }\n\n /**\n * Stop the current animation, committing the styles it reached. The next\n * `play()` creates a fresh animation.\n */\n stop() {\n if (!this.__controls) {\n return;\n }\n\n this.__completionToken += 1;\n this.__controls.stop();\n this.__controls = null;\n this.$emit('motion-stop');\n }\n\n /**\n * Cancel the current animation, reverting the element to its pre-animation\n * styles. The next `play()` creates a fresh animation.\n */\n cancel() {\n if (!this.__controls) {\n return;\n }\n\n this.__completionToken += 1;\n this.__controls.cancel();\n this.__controls = null;\n this.$emit('motion-cancel');\n }\n\n /**\n * Jump the current animation to its end state.\n */\n complete() {\n this.__controls?.complete();\n }\n\n /**\n * Bind the `hover`, `press` and `inView` gesture options with Motion's own\n * gesture functions (touch-filtered, keyboard-accessible press, real\n * IntersectionObserver). Nothing binds when an option is empty, so idle\n * elements pay nothing. Gesture animations are transient: they never become\n * the current animation and emit no lifecycle events.\n * @private\n */\n __bindGestures(motion: MotionModule) {\n const { hover, press, inView, inViewMargin, inViewAmount, once } = this.$options;\n\n if (Object.keys(hover).length > 0) {\n if (motion.hover) {\n this.__gestureStops.push(\n motion.hover(this.$el, () => {\n this.__startGesture(hover);\n return () => this.__revertGesture(hover);\n }),\n );\n } else {\n this.__warnMissingGesture('hover');\n }\n }\n\n if (Object.keys(press).length > 0) {\n if (motion.press) {\n this.__gestureStops.push(\n motion.press(this.$el, () => {\n this.__startGesture(press);\n return () => this.__revertGesture(press);\n }),\n );\n } else {\n this.__warnMissingGesture('press');\n }\n }\n\n if (Object.keys(inView).length > 0) {\n if (motion.inView) {\n const options: { margin?: string; amount?: 'some' | 'all' | number } = {};\n if (inViewMargin) {\n options.margin = inViewMargin;\n }\n if (inViewAmount) {\n const numeric = Number(inViewAmount);\n options.amount = Number.isNaN(numeric) ? (inViewAmount as 'some' | 'all') : numeric;\n }\n this.__gestureStops.push(\n motion.inView(\n this.$el,\n () => {\n this.__startGesture(inView);\n // With no leave handler returned, `inView()` fires once and the\n // reached styles persist.\n if (once) {\n return undefined;\n }\n return () => this.__revertGesture(inView);\n },\n options as Parameters<NonNullable<MotionModule['inView']>>[2],\n ),\n );\n } else {\n this.__warnMissingGesture('inView');\n }\n }\n }\n\n /**\n * Animate to a gesture state, alongside — not replacing — the current\n * animation, capturing the base value of every property it touches first.\n * @private\n */\n __startGesture(keyframes: DOMKeyframesDefinition): AnimationPlaybackControlsWithThen {\n this.__captureBaseValues(keyframes);\n return getMotion().animate(this.$el, keyframes, this.$options.transition);\n }\n\n /**\n * Capture the element's current value for every property a gesture is about\n * to animate, once per property and never refreshed afterwards.\n *\n * The read mirrors Motion's own `VisualElement.readValueFromInstance()`: the\n * parsed transform matrix for a transform property, the computed style\n * otherwise — both through Motion's own readers, so the values come back in\n * the shape its animations expect. Motion React hydrates the same snapshot\n * (its `baseTarget`) the first time it reads a value and keeps it, which is\n * what makes a return immune to a second gesture starting while the previous\n * one is still returning.\n *\n * A property the readers cannot resolve is left out, so nothing is invented:\n * the return animation simply does not touch it.\n * @private\n */\n __captureBaseValues(keyframes: DOMKeyframesDefinition) {\n const { transformProps, readTransformValue, getComputedStyle } = getMotion();\n\n for (const property of Object.keys(keyframes)) {\n if (property in this.__baseValues) {\n continue;\n }\n\n if (readTransformValue && transformProps?.has(property)) {\n this.__baseValues[property] = readTransformValue(this.$el, property);\n continue;\n }\n\n const computed = getComputedStyle?.(this.$el, property);\n\n if (typeof computed === 'string' && computed !== '') {\n const numeric = Number(computed);\n this.__baseValues[property] = Number.isNaN(numeric) ? computed : numeric;\n }\n }\n }\n\n /**\n * Return the element to its base values when a gesture ends, animating\n * forward with the `transition` option — the gesture animation is never\n * rewound.\n *\n * This is what Motion React does: ending a gesture deactivates its variant\n * (`node.animationState.setActive(\"whileHover\", false)`), which recomputes\n * the target of every property the variant owned and animates to it. Playing\n * the gesture animation backward instead would run its easing or spring\n * curve in reverse — a different feel — and could never reach a base value\n * the element did not hold when the gesture started.\n * @private\n */\n __revertGesture(keyframes: DOMKeyframesDefinition) {\n const base: Record<string, string | number> = {};\n\n for (const property of Object.keys(keyframes)) {\n if (property in this.__baseValues) {\n base[property] = this.__baseValues[property];\n }\n }\n\n if (Object.keys(base).length === 0) {\n return;\n }\n\n getMotion().animate(this.$el, base as DOMKeyframesDefinition, this.$options.transition);\n }\n\n /**\n * Warn about a gesture option the resolved motion module cannot honor.\n * @private\n */\n __warnMissingGesture(name: string) {\n this.$warn(\n 'motion.missing-gesture',\n `The resolved motion module has no \\`${name}()\\` (e.g. \\`motion/mini\\`). Provide the full \\`motion\\` entry to use the \\`${name}\\` option.`,\n );\n }\n\n /**\n * Create the animation from the `animate` and `transition` options and make\n * it the current one. Subclasses override this to build the current\n * animation from another source.\n * @protected\n */\n __createControls(): AnimationPlaybackControlsWithThen {\n const { transition } = this.$options;\n const controls = getMotion().animate(this.$el, this.keyframes, transition);\n this.__controls = controls;\n this.__fromOptions = true;\n return controls;\n }\n\n /**\n * The keyframes of the declared animation, with the `initial` styles folded\n * in as the starting point of every property they both describe.\n *\n * Motion reads a lone target value as \"from wherever the element is now\", so\n * a settled animation would have nothing left to move on the next `play()`.\n * Pairing each such value with its `initial` counterpart pins the start, and\n * the animation replays identically for as long as the options stand. A\n * property already written as a `[from, to]` array is left untouched, and so\n * is one the `initial` styles say nothing about — there, animating from the\n * current state is the point.\n */\n get keyframes(): DOMKeyframesDefinition {\n const initial = this.$options.initial as Record<string, unknown>;\n const keyframes: Record<string, unknown> = { ...(this.$options.animate as object) };\n\n for (const [property, to] of Object.entries(keyframes)) {\n if (!Array.isArray(to) && property in initial) {\n keyframes[property] = [initial[property], to];\n }\n }\n\n return keyframes as DOMKeyframesDefinition;\n }\n\n /**\n * Emit `motion-complete` when the given animation finishes, unless another\n * playback superseded it in the meantime. The returned promise resolves when\n * the animation settles and never rejects, so playback methods can safely\n * hand it to callers that do not catch.\n * @private\n */\n __watchCompletion(controls: AnimationPlaybackControlsWithThen): Promise<void> {\n const token = ++this.__completionToken;\n return new Promise((resolve) => {\n controls.then(() => {\n if (token === this.__completionToken && this.__controls === controls) {\n this.$emit('motion-complete');\n }\n resolve();\n }, resolve);\n });\n }\n}\n\nexport default Motion;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAyDA,IAAa,SAAb,cAA6D,KAAsB;;;;CAIjF,OAAO,SAAqB;EAC1B,MAAM;EACN,SAAS;GACP,SAAS;GACT,SAAS;GACT,YAAY;GACZ,UAAU;GAC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B,QAAI,UAAU,KAAK,qBAAqB,KAAK,eAAe,SAC1D,MAAK,MAAM,kBAAkB;AAE/B,aAAS;MACR,QAAQ;IACX"}
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import { Motion } from "./Motion.js";
|
|
2
2
|
import { Base } from "@studiometa/js-toolkit/Base";
|
|
3
|
-
import { BaseConfig, BaseProps } from "@studiometa/js-toolkit";
|
|
3
|
+
import { BaseConfig, BaseProps, ChildrenCollection } from "@studiometa/js-toolkit";
|
|
4
4
|
import { scroll } from "motion";
|
|
5
5
|
|
|
6
6
|
//#region src/MotionScrollTimeline.d.ts
|
|
7
7
|
interface MotionScrollTimelineProps extends BaseProps {
|
|
8
|
-
$children: {
|
|
9
|
-
Motion: Motion[];
|
|
10
|
-
};
|
|
11
8
|
$options: {
|
|
12
9
|
offset: string[];
|
|
13
10
|
axis: 'x' | 'y';
|
|
@@ -45,18 +42,20 @@ declare class MotionScrollTimeline<T extends BaseProps = BaseProps> extends Base
|
|
|
45
42
|
*/
|
|
46
43
|
static config: BaseConfig;
|
|
47
44
|
/**
|
|
48
|
-
* The stop function of each `scroll()` link, released on
|
|
45
|
+
* The stop function of each `scroll()` link, released on unmount.
|
|
49
46
|
* @private
|
|
50
47
|
*/
|
|
51
48
|
__stops: VoidFunction[];
|
|
52
49
|
/**
|
|
53
|
-
*
|
|
50
|
+
* The mounted `Motion` descendants, live and in DOM order.
|
|
51
|
+
* @private
|
|
54
52
|
*/
|
|
55
|
-
|
|
53
|
+
__children: ChildrenCollection<Motion>;
|
|
56
54
|
/**
|
|
57
|
-
*
|
|
55
|
+
* Bind every `Motion` child to this element's scroll progress, and release
|
|
56
|
+
* every link when the mount cycle ends.
|
|
58
57
|
*/
|
|
59
|
-
|
|
58
|
+
mounted(): Promise<() => void>;
|
|
60
59
|
/**
|
|
61
60
|
* Bind one child's declared animation to the timeline: `seek(0)` builds it
|
|
62
61
|
* paused when nothing has played yet, then `scroll()` takes over its
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { resolveMotion } from "./dependencies.js";
|
|
2
2
|
import { Motion } from "./Motion.js";
|
|
3
3
|
import { Base } from "@studiometa/js-toolkit/Base";
|
|
4
|
+
import { whenDOMSettled } from "@studiometa/js-toolkit/whenDOMSettled";
|
|
4
5
|
//#region src/MotionScrollTimeline.ts
|
|
5
6
|
/**
|
|
6
7
|
* MotionScrollTimeline class.
|
|
@@ -47,28 +48,31 @@ var MotionScrollTimeline = class extends Base {
|
|
|
47
48
|
}
|
|
48
49
|
};
|
|
49
50
|
/**
|
|
50
|
-
* The stop function of each `scroll()` link, released on
|
|
51
|
+
* The stop function of each `scroll()` link, released on unmount.
|
|
51
52
|
* @private
|
|
52
53
|
*/
|
|
53
54
|
__stops = [];
|
|
54
55
|
/**
|
|
55
|
-
*
|
|
56
|
+
* The mounted `Motion` descendants, live and in DOM order.
|
|
57
|
+
* @private
|
|
58
|
+
*/
|
|
59
|
+
__children = this.$watchChildren("Motion");
|
|
60
|
+
/**
|
|
61
|
+
* Bind every `Motion` child to this element's scroll progress, and release
|
|
62
|
+
* every link when the mount cycle ends.
|
|
56
63
|
*/
|
|
57
64
|
async mounted() {
|
|
58
|
-
const motion = await resolveMotion();
|
|
65
|
+
const [motion] = await Promise.all([resolveMotion(), whenDOMSettled()]);
|
|
59
66
|
if (!this.$isMounted) return;
|
|
60
67
|
if (!motion.scroll) {
|
|
61
|
-
this.$warn("The resolved motion module has no `scroll()` (e.g. `motion/mini`). Provide the full `motion` entry to use MotionScrollTimeline.");
|
|
68
|
+
this.$warn("motion-scroll-timeline.missing-scroll", "The resolved motion module has no `scroll()` (e.g. `motion/mini`). Provide the full `motion` entry to use MotionScrollTimeline.");
|
|
62
69
|
return;
|
|
63
70
|
}
|
|
64
|
-
for (const child of this
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
destroyed() {
|
|
70
|
-
for (const stop of this.__stops) stop();
|
|
71
|
-
this.__stops = [];
|
|
71
|
+
for (const child of this.__children) this.__link(motion.scroll, child);
|
|
72
|
+
return () => {
|
|
73
|
+
for (const stop of this.__stops) stop();
|
|
74
|
+
this.__stops = [];
|
|
75
|
+
};
|
|
72
76
|
}
|
|
73
77
|
/**
|
|
74
78
|
* Bind one child's declared animation to the timeline: `seek(0)` builds it
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MotionScrollTimeline.js","names":[],"sources":["../src/MotionScrollTimeline.ts"],"sourcesContent":["import { Base } from '@studiometa/js-toolkit/Base';\nimport type { BaseProps, BaseConfig } from '@studiometa/js-toolkit';\nimport type { scroll } from 'motion';\nimport { Motion } from './Motion.js';\nimport { resolveMotion } from './dependencies.js';\n\n/**\n * The options `scroll()` accepts, widened with `trackContentSize`.\n *\n * `scroll()` forwards every option it does not read itself to `scrollInfo()`,\n * which declares `trackContentSize` on its own `ScrollInfoOptions` — Motion's\n * public `ScrollOptions` interface simply omits it.\n */\ntype ScrollOptions = NonNullable<Parameters<typeof scroll>[1]> & {\n trackContentSize?: boolean;\n};\n\nexport interface MotionScrollTimelineProps extends BaseProps {\n $
|
|
1
|
+
{"version":3,"file":"MotionScrollTimeline.js","names":[],"sources":["../src/MotionScrollTimeline.ts"],"sourcesContent":["import { Base } from '@studiometa/js-toolkit/Base';\nimport { whenDOMSettled } from '@studiometa/js-toolkit/whenDOMSettled';\nimport type { BaseProps, BaseConfig, ChildrenCollection } from '@studiometa/js-toolkit';\nimport type { scroll } from 'motion';\nimport { Motion } from './Motion.js';\nimport { resolveMotion } from './dependencies.js';\n\n/**\n * The options `scroll()` accepts, widened with `trackContentSize`.\n *\n * `scroll()` forwards every option it does not read itself to `scrollInfo()`,\n * which declares `trackContentSize` on its own `ScrollInfoOptions` — Motion's\n * public `ScrollOptions` interface simply omits it.\n */\ntype ScrollOptions = NonNullable<Parameters<typeof scroll>[1]> & {\n trackContentSize?: boolean;\n};\n\nexport interface MotionScrollTimelineProps extends BaseProps {\n $options: {\n offset: string[];\n axis: 'x' | 'y';\n trackContentSize: boolean;\n };\n}\n\n/**\n * MotionScrollTimeline class.\n *\n * The scroll driver for a group of animations: this element's traversal of the\n * viewport defines the timeline, and every `Motion` child it contains is bound\n * to that progress with Motion's `scroll()` — hardware-accelerated where the\n * browser supports `ScrollTimeline`. The children declare their keyframes as\n * usual and keep their whole playback surface; the timeline takes over their\n * playback on mount — leave their `autoplay` off (its default) so nothing\n * plays before the link lands.\n *\n * `scroll()` is not part of `motion/mini`: when the injected module lacks it,\n * the timeline warns and leaves its children untouched.\n *\n * @example\n * ```html\n * <section data-component=\"MotionScrollTimeline\" class=\"h-[300vh]\">\n * <div data-component=\"Motion\" data-option-animate='{ \"opacity\": [0, 1, 0] }'>\n * Content\n * </div>\n * </section>\n * ```\n *\n * @link https://ui.studiometa.dev/reference/items/MotionScrollTimeline/\n */\nexport class MotionScrollTimeline<T extends BaseProps = BaseProps> extends Base<\n MotionScrollTimelineProps & T\n> {\n /**\n * Config.\n */\n static config: BaseConfig = {\n name: 'MotionScrollTimeline',\n components: { Motion },\n options: {\n offset: { type: Array, default: () => ['start end', 'end start'] },\n axis: { type: String, default: 'y' },\n trackContentSize: Boolean,\n },\n };\n\n /**\n * The stop function of each `scroll()` link, released on unmount.\n * @private\n */\n __stops: VoidFunction[] = [];\n\n /**\n * The mounted `Motion` descendants, live and in DOM order.\n * @private\n */\n __children: ChildrenCollection<Motion> = this.$watchChildren<Motion>('Motion');\n\n /**\n * Bind every `Motion` child to this element's scroll progress, and release\n * every link when the mount cycle ends.\n */\n async mounted() {\n // Mount order is not guaranteed and `$watchChildren()` seeds its collection\n // in a microtask, so the timeline can reach `mounted()` before a single\n // child exists. `whenDOMSettled()` waits for the mutation batch that brought\n // this subtree in to finish mounting everything eager in it.\n const [motion] = await Promise.all([resolveMotion(), whenDOMSettled()]);\n\n if (!this.$isMounted) {\n return;\n }\n\n if (!motion.scroll) {\n this.$warn(\n 'motion-scroll-timeline.missing-scroll',\n 'The resolved motion module has no `scroll()` (e.g. `motion/mini`). Provide the full `motion` entry to use MotionScrollTimeline.',\n );\n return;\n }\n\n for (const child of this.__children) {\n this.__link(motion.scroll, child);\n }\n\n return () => {\n for (const stop of this.__stops) {\n stop();\n }\n this.__stops = [];\n };\n }\n\n /**\n * Bind one child's declared animation to the timeline: `seek(0)` builds it\n * paused when nothing has played yet, then `scroll()` takes over its\n * playback.\n * @private\n */\n async __link(scrollFn: typeof scroll, child: Motion) {\n await child.seek(0);\n const { controls } = child;\n\n if (!controls) {\n return;\n }\n\n const { offset, axis, trackContentSize } = this.$options;\n const options: ScrollOptions = { target: this.$el, axis };\n if (offset.length > 0) {\n options.offset = offset as ScrollOptions['offset'];\n }\n if (trackContentSize) {\n options.trackContentSize = true;\n }\n this.__stops.push(scrollFn(controls, options));\n }\n}\n\nexport default MotionScrollTimeline;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmDA,IAAa,uBAAb,cAA2E,KAEzE;;;;CAIA,OAAO,SAAqB;EAC1B,MAAM;EACN,YAAY,EAAE,QAAQ;EACtB,SAAS;GACP,QAAQ;IAAE,MAAM;IAAO,eAAe,CAAC,aAAa,YAAY;IAAE;GAClE,MAAM;IAAE,MAAM;IAAQ,SAAS;IAAK;GACpC,kBAAkB;GACnB;EACF;;;;;CAMD,UAA0B,EAAE;;;;;CAM5B,aAAyC,KAAK,eAAuB,SAAS;;;;;CAM9E,MAAM,UAAU;EAKd,MAAM,CAAC,UAAU,MAAM,QAAQ,IAAI,CAAC,eAAe,EAAE,gBAAgB,CAAC,CAAC;AAEvE,MAAI,CAAC,KAAK,WACR;AAGF,MAAI,CAAC,OAAO,QAAQ;AAClB,QAAK,MACH,yCACA,kIACD;AACD;;AAGF,OAAK,MAAM,SAAS,KAAK,WACvB,MAAK,OAAO,OAAO,QAAQ,MAAM;AAGnC,eAAa;AACX,QAAK,MAAM,QAAQ,KAAK,QACtB,OAAM;AAER,QAAK,UAAU,EAAE;;;;;;;;;CAUrB,MAAM,OAAO,UAAyB,OAAe;AACnD,QAAM,MAAM,KAAK,EAAE;EACnB,MAAM,EAAE,aAAa;AAErB,MAAI,CAAC,SACH;EAGF,MAAM,EAAE,QAAQ,MAAM,qBAAqB,KAAK;EAChD,MAAM,UAAyB;GAAE,QAAQ,KAAK;GAAK;GAAM;AACzD,MAAI,OAAO,SAAS,EAClB,SAAQ,SAAS;AAEnB,MAAI,iBACF,SAAQ,mBAAmB;AAE7B,OAAK,QAAQ,KAAK,SAAS,UAAU,QAAQ,CAAC"}
|
package/dist/MotionSequence.d.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { Motion } from "./Motion.js";
|
|
2
|
-
import { BaseConfig, BaseProps } from "@studiometa/js-toolkit";
|
|
2
|
+
import { BaseConfig, BaseProps, ChildrenCollection } from "@studiometa/js-toolkit";
|
|
3
3
|
import { AnimationPlaybackControlsWithThen } from "motion";
|
|
4
4
|
|
|
5
5
|
//#region src/MotionSequence.d.ts
|
|
6
6
|
interface MotionSequenceProps extends BaseProps {
|
|
7
|
-
$children: {
|
|
8
|
-
Motion: Motion[];
|
|
9
|
-
};
|
|
10
7
|
$options: {
|
|
11
8
|
stagger: number;
|
|
12
9
|
};
|
|
@@ -46,6 +43,24 @@ declare class MotionSequence<T extends BaseProps = BaseProps> extends Motion<Mot
|
|
|
46
43
|
* Config.
|
|
47
44
|
*/
|
|
48
45
|
static config: BaseConfig;
|
|
46
|
+
/**
|
|
47
|
+
* The mounted `Motion` descendants, live and in DOM order. The collection
|
|
48
|
+
* updates itself as children mount and unmount, so a child appended to the
|
|
49
|
+
* sequence after mount is part of the next animation it builds.
|
|
50
|
+
* @private
|
|
51
|
+
*/
|
|
52
|
+
__children: ChildrenCollection<Motion>;
|
|
53
|
+
/**
|
|
54
|
+
* Wait for the children before letting `Motion.mounted()` decide whether
|
|
55
|
+
* there is anything to autoplay.
|
|
56
|
+
*
|
|
57
|
+
* Mount order is not guaranteed and `$watchChildren()` seeds its collection
|
|
58
|
+
* in a microtask, so the sequence can reach `mounted()` with an empty
|
|
59
|
+
* collection and skip its own autoplay. `whenDOMSettled()` waits for the
|
|
60
|
+
* mutation batch that brought this subtree in to finish mounting everything
|
|
61
|
+
* eager in it.
|
|
62
|
+
*/
|
|
63
|
+
mounted(): Promise<void>;
|
|
49
64
|
/**
|
|
50
65
|
* The sequence has an animation to autoplay as soon as it has children
|
|
51
66
|
* declaring keyframes.
|
package/dist/MotionSequence.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { getMotion } from "./dependencies.js";
|
|
2
2
|
import { Motion } from "./Motion.js";
|
|
3
|
+
import { whenDOMSettled } from "@studiometa/js-toolkit/whenDOMSettled";
|
|
3
4
|
//#region src/MotionSequence.ts
|
|
4
5
|
/**
|
|
5
6
|
* MotionSequence class.
|
|
@@ -41,6 +42,28 @@ var MotionSequence = class extends Motion {
|
|
|
41
42
|
options: { stagger: Number }
|
|
42
43
|
};
|
|
43
44
|
/**
|
|
45
|
+
* The mounted `Motion` descendants, live and in DOM order. The collection
|
|
46
|
+
* updates itself as children mount and unmount, so a child appended to the
|
|
47
|
+
* sequence after mount is part of the next animation it builds.
|
|
48
|
+
* @private
|
|
49
|
+
*/
|
|
50
|
+
__children = this.$watchChildren("Motion");
|
|
51
|
+
/**
|
|
52
|
+
* Wait for the children before letting `Motion.mounted()` decide whether
|
|
53
|
+
* there is anything to autoplay.
|
|
54
|
+
*
|
|
55
|
+
* Mount order is not guaranteed and `$watchChildren()` seeds its collection
|
|
56
|
+
* in a microtask, so the sequence can reach `mounted()` with an empty
|
|
57
|
+
* collection and skip its own autoplay. `whenDOMSettled()` waits for the
|
|
58
|
+
* mutation batch that brought this subtree in to finish mounting everything
|
|
59
|
+
* eager in it.
|
|
60
|
+
*/
|
|
61
|
+
async mounted() {
|
|
62
|
+
await whenDOMSettled();
|
|
63
|
+
if (!this.$isMounted) return;
|
|
64
|
+
await super.mounted();
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
44
67
|
* The sequence has an animation to autoplay as soon as it has children
|
|
45
68
|
* declaring keyframes.
|
|
46
69
|
* @protected
|
|
@@ -53,8 +76,7 @@ var MotionSequence = class extends Motion {
|
|
|
53
76
|
* @private
|
|
54
77
|
*/
|
|
55
78
|
get __sequencedChildren() {
|
|
56
|
-
|
|
57
|
-
return children.filter((child) => Object.keys(child.$options.animate).length > 0);
|
|
79
|
+
return this.__children.items.filter((child) => Object.keys(child.$options.animate).length > 0);
|
|
58
80
|
}
|
|
59
81
|
/**
|
|
60
82
|
* Build the sequence from the children declarations and make it the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MotionSequence.js","names":[],"sources":["../src/MotionSequence.ts"],"sourcesContent":["import type { BaseProps, BaseConfig } from '@studiometa/js-toolkit';\nimport type { AnimationPlaybackControlsWithThen, AnimationSequence, SequenceOptions } from 'motion';\nimport { Motion } from './Motion.js';\nimport { getMotion } from './dependencies.js';\n\nexport interface MotionSequenceProps extends BaseProps {\n $
|
|
1
|
+
{"version":3,"file":"MotionSequence.js","names":[],"sources":["../src/MotionSequence.ts"],"sourcesContent":["import { whenDOMSettled } from '@studiometa/js-toolkit/whenDOMSettled';\nimport type { BaseProps, BaseConfig, ChildrenCollection } from '@studiometa/js-toolkit';\nimport type { AnimationPlaybackControlsWithThen, AnimationSequence, SequenceOptions } from 'motion';\nimport { Motion } from './Motion.js';\nimport { getMotion } from './dependencies.js';\n\nexport interface MotionSequenceProps extends BaseProps {\n $options: {\n stagger: number;\n };\n}\n\n/**\n * MotionSequence class.\n *\n * Orchestrate the `Motion` children as one animation sequence: each child\n * declares its keyframes as usual, and the sequence composes them — in DOM\n * order — into a single timeline with Motion's [sequence support](https://motion.dev/docs/animate#timeline-sequencing).\n * The whole `Motion` playback surface applies to the sequence: an `Action`\n * can `play()`, `reverse()` or `seek()` the entire choreography, and a\n * `MotionScrollTimeline` can scrub it.\n *\n * By default segments run one after another; a child's `at` option positions\n * its segment explicitly (a time in seconds, a relative offset like `\"-0.2\"`,\n * or `\"<\"` for \"with the previous\"), and the `stagger` option spreads the\n * children automatically. The sequence owns the children's playback — leave\n * their `autoplay` off (its default) and enable it on the sequence itself\n * with `data-option-autoplay` to play on mount.\n *\n * Sequences need the full `motion` entry: `motion/mini`'s `animate()` does\n * not support them.\n *\n * @example\n * ```html\n * <ul data-component=\"MotionSequence\" data-option-stagger=\"0.1\" data-option-autoplay>\n * <li data-component=\"Motion\" data-option-animate='{ \"opacity\": 1, \"y\": 0 }' data-option-initial='{ \"opacity\": 0, \"y\": 16 }'>…</li>\n * <li data-component=\"Motion\" data-option-animate='{ \"opacity\": 1, \"y\": 0 }' data-option-initial='{ \"opacity\": 0, \"y\": 16 }'>…</li>\n * </ul>\n * ```\n *\n * @link https://ui.studiometa.dev/reference/items/MotionSequence/\n */\nexport class MotionSequence<T extends BaseProps = BaseProps> extends Motion<\n MotionSequenceProps & T\n> {\n /**\n * Config.\n */\n static config: BaseConfig = {\n name: 'MotionSequence',\n components: { Motion },\n options: {\n stagger: Number,\n },\n };\n\n /**\n * The mounted `Motion` descendants, live and in DOM order. The collection\n * updates itself as children mount and unmount, so a child appended to the\n * sequence after mount is part of the next animation it builds.\n * @private\n */\n __children: ChildrenCollection<Motion> = this.$watchChildren<Motion>('Motion');\n\n /**\n * Wait for the children before letting `Motion.mounted()` decide whether\n * there is anything to autoplay.\n *\n * Mount order is not guaranteed and `$watchChildren()` seeds its collection\n * in a microtask, so the sequence can reach `mounted()` with an empty\n * collection and skip its own autoplay. `whenDOMSettled()` waits for the\n * mutation batch that brought this subtree in to finish mounting everything\n * eager in it.\n */\n async mounted(): Promise<void> {\n await whenDOMSettled();\n\n if (!this.$isMounted) {\n return;\n }\n\n await super.mounted();\n }\n\n /**\n * The sequence has an animation to autoplay as soon as it has children\n * declaring keyframes.\n * @protected\n */\n get __hasDeclaredAnimation(): boolean {\n return this.__sequencedChildren.length > 0;\n }\n\n /**\n * The `Motion` children carrying keyframes, in DOM order.\n * @private\n */\n get __sequencedChildren(): Motion[] {\n return this.__children.items.filter((child) => Object.keys(child.$options.animate).length > 0);\n }\n\n /**\n * Build the sequence from the children declarations and make it the\n * current animation. Each segment merges the child's `transition` and its\n * `at` position; without an explicit `at`, the `stagger` option spreads the\n * segments, and with neither they run one after another (Motion's default).\n * @protected\n */\n __createControls(): AnimationPlaybackControlsWithThen {\n const motion = getMotion();\n const { stagger, transition } = this.$options;\n\n const sequence = this.__sequencedChildren.map((child, index) => {\n const { transition: childTransition, at } = child.$options;\n const options: Record<string, unknown> = { ...childTransition };\n\n if (at !== '') {\n const numeric = Number(at);\n options.at = Number.isNaN(numeric) ? at : numeric;\n } else if (stagger > 0) {\n options.at = index * stagger;\n }\n\n // `keyframes`, not the raw `animate` option: each segment starts from\n // the child's `initial` styles, so the sequence replays identically.\n return [child.$el, child.keyframes, options];\n });\n\n const controls = motion.animate(\n sequence as AnimationSequence,\n Object.keys(transition).length > 0 ? (transition as SequenceOptions) : undefined,\n );\n this.__controls = controls;\n this.__fromOptions = true;\n return controls;\n }\n}\n\nexport default MotionSequence;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,IAAa,iBAAb,cAAqE,OAEnE;;;;CAIA,OAAO,SAAqB;EAC1B,MAAM;EACN,YAAY,EAAE,QAAQ;EACtB,SAAS,EACP,SAAS,QACV;EACF;;;;;;;CAQD,aAAyC,KAAK,eAAuB,SAAS;;;;;;;;;;;CAY9E,MAAM,UAAyB;AAC7B,QAAM,gBAAgB;AAEtB,MAAI,CAAC,KAAK,WACR;AAGF,QAAM,MAAM,SAAS;;;;;;;CAQvB,IAAI,yBAAkC;AACpC,SAAO,KAAK,oBAAoB,SAAS;;;;;;CAO3C,IAAI,sBAAgC;AAClC,SAAO,KAAK,WAAW,MAAM,QAAQ,UAAU,OAAO,KAAK,MAAM,SAAS,QAAQ,CAAC,SAAS,EAAE;;;;;;;;;CAUhG,mBAAsD;EACpD,MAAM,SAAS,WAAW;EAC1B,MAAM,EAAE,SAAS,eAAe,KAAK;EAErC,MAAM,WAAW,KAAK,oBAAoB,KAAK,OAAO,UAAU;GAC9D,MAAM,EAAE,YAAY,iBAAiB,OAAO,MAAM;GAClD,MAAM,UAAmC,EAAE,GAAG,iBAAiB;AAE/D,OAAI,OAAO,IAAI;IACb,MAAM,UAAU,OAAO,GAAG;AAC1B,YAAQ,KAAK,OAAO,MAAM,QAAQ,GAAG,KAAK;cACjC,UAAU,EACnB,SAAQ,KAAK,QAAQ;AAKvB,UAAO;IAAC,MAAM;IAAK,MAAM;IAAW;IAAQ;IAC5C;EAEF,MAAM,WAAW,OAAO,QACtB,UACA,OAAO,KAAK,WAAW,CAAC,SAAS,IAAK,aAAiC,KAAA,EACxE;AACD,OAAK,aAAa;AAClB,OAAK,gBAAgB;AACrB,SAAO"}
|
package/dist/MotionView.d.ts
CHANGED
|
@@ -18,6 +18,18 @@ interface MotionViewProps extends BaseProps {
|
|
|
18
18
|
layout: boolean;
|
|
19
19
|
auto: boolean;
|
|
20
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* The transition lifecycle. The names mirror `ViewTransition`'s, which is
|
|
23
|
+
* what makes the two interchangeable.
|
|
24
|
+
*/
|
|
25
|
+
$emits: {
|
|
26
|
+
enter: void;
|
|
27
|
+
'enter-start': void;
|
|
28
|
+
'enter-end': void;
|
|
29
|
+
leave: void;
|
|
30
|
+
'leave-start': void;
|
|
31
|
+
'leave-end': void;
|
|
32
|
+
};
|
|
21
33
|
}
|
|
22
34
|
/**
|
|
23
35
|
* MotionView class.
|
|
@@ -35,9 +47,9 @@ interface MotionViewProps extends BaseProps {
|
|
|
35
47
|
* component then warns and applies updates directly.
|
|
36
48
|
*
|
|
37
49
|
* Containment is the wiring: with the `auto` option (on by default), the
|
|
38
|
-
* component wraps any
|
|
39
|
-
*
|
|
40
|
-
*
|
|
50
|
+
* component wraps any DOM update announced by a mutating component inside its
|
|
51
|
+
* subtree, and joins the open/close lifecycle of a containing `Dialog` through
|
|
52
|
+
* the extendable events' `waitUntil()`. Explicit `Action` wiring
|
|
41
53
|
* (`event.detail.wrap(target)`) remains for cross-subtree topologies.
|
|
42
54
|
*
|
|
43
55
|
* @example
|
|
@@ -60,17 +72,26 @@ declare class MotionView<T extends BaseProps = BaseProps> extends Base<MotionVie
|
|
|
60
72
|
*/
|
|
61
73
|
state: 'entering' | 'leaving' | null;
|
|
62
74
|
/**
|
|
63
|
-
* Wrap
|
|
64
|
-
* the bubbling event reaches the root element, and passing the
|
|
65
|
-
* `detail.wrap()` lets the emitter run its mutation through
|
|
75
|
+
* Wrap an `EVENTS.dom.update` announced by a mutating component inside the
|
|
76
|
+
* subtree: the bubbling event reaches the root element, and passing the
|
|
77
|
+
* instance to `detail.wrap()` lets the emitter run its mutation through
|
|
78
|
+
* `update()` — the duck-typed method `domUpdate()` looks for is `update`,
|
|
79
|
+
* which this component already owns.
|
|
66
80
|
* @private
|
|
67
81
|
*/
|
|
68
82
|
__onDomUpdate: (event: Event) => void;
|
|
69
83
|
/**
|
|
70
84
|
* Join the open/close lifecycle of a containing `Dialog`: its extendable
|
|
71
|
-
* events bubble up past the root element to the document, and
|
|
72
|
-
*
|
|
73
|
-
*
|
|
85
|
+
* events bubble up past the root element to the document, and registering
|
|
86
|
+
* with `detail.waitUntil()` holds the dialog's choreography open until this
|
|
87
|
+
* component's transition settles.
|
|
88
|
+
*
|
|
89
|
+
* Registered as a **function**, not as `this`. `emitExtendable()` duck-types
|
|
90
|
+
* an object registration on the name of the event — it would look for
|
|
91
|
+
* `open()` and `close()`, which a transition component has no business
|
|
92
|
+
* owning. The function form is what the primitive documents for a pair of
|
|
93
|
+
* method names that differ from the event names, and it maps `open` onto
|
|
94
|
+
* `enter()` and `close` onto `leave()` explicitly.
|
|
74
95
|
* @private
|
|
75
96
|
*/
|
|
76
97
|
__onDialogPhase: (event: Event) => void;
|
|
@@ -80,15 +101,11 @@ declare class MotionView<T extends BaseProps = BaseProps> extends Base<MotionVie
|
|
|
80
101
|
get target(): HTMLElement;
|
|
81
102
|
/**
|
|
82
103
|
* Assign the configured `view-transition-name` to the target element and,
|
|
83
|
-
* with the `auto` option, listen for ambient wiring events:
|
|
84
|
-
* from descendant mutators on the root element, and the
|
|
85
|
-
* `open`/`close` events of a containing `Dialog` on the document.
|
|
86
|
-
*/
|
|
87
|
-
mounted(): void;
|
|
88
|
-
/**
|
|
89
|
-
* Remove the ambient wiring listeners.
|
|
104
|
+
* with the `auto` option, listen for ambient wiring events:
|
|
105
|
+
* `EVENTS.dom.update` from descendant mutators on the root element, and the
|
|
106
|
+
* extendable `open`/`close` events of a containing `Dialog` on the document.
|
|
90
107
|
*/
|
|
91
|
-
|
|
108
|
+
mounted(): () => void;
|
|
92
109
|
/**
|
|
93
110
|
* Run the given mutation as a view transition built from the options: the
|
|
94
111
|
* `add` selector picks the animated subjects within the root element (the
|
|
@@ -111,6 +128,12 @@ declare class MotionView<T extends BaseProps = BaseProps> extends Base<MotionVie
|
|
|
111
128
|
* Defaults to the enter transition if no transition has been triggered yet.
|
|
112
129
|
*/
|
|
113
130
|
toggle(): Promise<void>;
|
|
131
|
+
/**
|
|
132
|
+
* Swap one space separated class list for another on the target, the way
|
|
133
|
+
* `ViewTransition` does it.
|
|
134
|
+
* @private
|
|
135
|
+
*/
|
|
136
|
+
__toggleClasses(remove: string, add: string): void;
|
|
114
137
|
}
|
|
115
138
|
//#endregion
|
|
116
139
|
export { MotionView, MotionView as default, MotionViewProps };
|
package/dist/MotionView.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { resolveMotion } from "./dependencies.js";
|
|
2
2
|
import { Base } from "@studiometa/js-toolkit/Base";
|
|
3
|
-
import {
|
|
4
|
-
import { removeClass } from "@studiometa/js-toolkit/utils/removeClass";
|
|
3
|
+
import { EVENTS } from "@studiometa/js-toolkit/EVENTS";
|
|
5
4
|
//#region src/MotionView.ts
|
|
6
5
|
/**
|
|
7
6
|
* MotionView class.
|
|
@@ -19,9 +18,9 @@ import { removeClass } from "@studiometa/js-toolkit/utils/removeClass";
|
|
|
19
18
|
* component then warns and applies updates directly.
|
|
20
19
|
*
|
|
21
20
|
* Containment is the wiring: with the `auto` option (on by default), the
|
|
22
|
-
* component wraps any
|
|
23
|
-
*
|
|
24
|
-
*
|
|
21
|
+
* component wraps any DOM update announced by a mutating component inside its
|
|
22
|
+
* subtree, and joins the open/close lifecycle of a containing `Dialog` through
|
|
23
|
+
* the extendable events' `waitUntil()`. Explicit `Action` wiring
|
|
25
24
|
* (`event.detail.wrap(target)`) remains for cross-subtree topologies.
|
|
26
25
|
*
|
|
27
26
|
* @example
|
|
@@ -40,15 +39,6 @@ var MotionView = class extends Base {
|
|
|
40
39
|
*/
|
|
41
40
|
static config = {
|
|
42
41
|
name: "MotionView",
|
|
43
|
-
emits: [
|
|
44
|
-
"enter",
|
|
45
|
-
"enter-start",
|
|
46
|
-
"enter-end",
|
|
47
|
-
"leave",
|
|
48
|
-
"leave-start",
|
|
49
|
-
"leave-end",
|
|
50
|
-
"toggle"
|
|
51
|
-
],
|
|
52
42
|
options: {
|
|
53
43
|
viewTransitionName: String,
|
|
54
44
|
enterTo: String,
|
|
@@ -71,9 +61,11 @@ var MotionView = class extends Base {
|
|
|
71
61
|
*/
|
|
72
62
|
state = null;
|
|
73
63
|
/**
|
|
74
|
-
* Wrap
|
|
75
|
-
* the bubbling event reaches the root element, and passing the
|
|
76
|
-
* `detail.wrap()` lets the emitter run its mutation through
|
|
64
|
+
* Wrap an `EVENTS.dom.update` announced by a mutating component inside the
|
|
65
|
+
* subtree: the bubbling event reaches the root element, and passing the
|
|
66
|
+
* instance to `detail.wrap()` lets the emitter run its mutation through
|
|
67
|
+
* `update()` — the duck-typed method `domUpdate()` looks for is `update`,
|
|
68
|
+
* which this component already owns.
|
|
77
69
|
* @private
|
|
78
70
|
*/
|
|
79
71
|
__onDomUpdate = (event) => {
|
|
@@ -82,15 +74,22 @@ var MotionView = class extends Base {
|
|
|
82
74
|
};
|
|
83
75
|
/**
|
|
84
76
|
* Join the open/close lifecycle of a containing `Dialog`: its extendable
|
|
85
|
-
* events bubble up past the root element to the document, and
|
|
86
|
-
*
|
|
87
|
-
*
|
|
77
|
+
* events bubble up past the root element to the document, and registering
|
|
78
|
+
* with `detail.waitUntil()` holds the dialog's choreography open until this
|
|
79
|
+
* component's transition settles.
|
|
80
|
+
*
|
|
81
|
+
* Registered as a **function**, not as `this`. `emitExtendable()` duck-types
|
|
82
|
+
* an object registration on the name of the event — it would look for
|
|
83
|
+
* `open()` and `close()`, which a transition component has no business
|
|
84
|
+
* owning. The function form is what the primitive documents for a pair of
|
|
85
|
+
* method names that differ from the event names, and it maps `open` onto
|
|
86
|
+
* `enter()` and `close` onto `leave()` explicitly.
|
|
88
87
|
* @private
|
|
89
88
|
*/
|
|
90
89
|
__onDialogPhase = (event) => {
|
|
91
90
|
const { target } = event;
|
|
92
91
|
const { waitUntil } = event.detail ?? {};
|
|
93
|
-
if (target instanceof Element && target !== this.$el && target.contains(this.$el) && typeof waitUntil === "function") waitUntil(this);
|
|
92
|
+
if (target instanceof Element && target !== this.$el && target.contains(this.$el) && typeof waitUntil === "function") waitUntil(event.type === "open" ? () => this.enter() : () => this.leave());
|
|
94
93
|
};
|
|
95
94
|
/**
|
|
96
95
|
* Get the transition target.
|
|
@@ -100,26 +99,22 @@ var MotionView = class extends Base {
|
|
|
100
99
|
}
|
|
101
100
|
/**
|
|
102
101
|
* Assign the configured `view-transition-name` to the target element and,
|
|
103
|
-
* with the `auto` option, listen for ambient wiring events:
|
|
104
|
-
* from descendant mutators on the root element, and the
|
|
105
|
-
* `open`/`close` events of a containing `Dialog` on the document.
|
|
102
|
+
* with the `auto` option, listen for ambient wiring events:
|
|
103
|
+
* `EVENTS.dom.update` from descendant mutators on the root element, and the
|
|
104
|
+
* extendable `open`/`close` events of a containing `Dialog` on the document.
|
|
106
105
|
*/
|
|
107
106
|
mounted() {
|
|
108
107
|
const { viewTransitionName, auto } = this.$options;
|
|
109
108
|
if (viewTransitionName) this.target.style.setProperty("view-transition-name", viewTransitionName);
|
|
110
|
-
if (auto)
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
destroyed() {
|
|
120
|
-
this.$el.removeEventListener("dom-update", this.__onDomUpdate);
|
|
121
|
-
document.removeEventListener("open", this.__onDialogPhase);
|
|
122
|
-
document.removeEventListener("close", this.__onDialogPhase);
|
|
109
|
+
if (!auto) return;
|
|
110
|
+
this.$el.addEventListener(EVENTS.dom.update, this.__onDomUpdate);
|
|
111
|
+
document.addEventListener("open", this.__onDialogPhase);
|
|
112
|
+
document.addEventListener("close", this.__onDialogPhase);
|
|
113
|
+
return () => {
|
|
114
|
+
this.$el.removeEventListener(EVENTS.dom.update, this.__onDomUpdate);
|
|
115
|
+
document.removeEventListener("open", this.__onDialogPhase);
|
|
116
|
+
document.removeEventListener("close", this.__onDialogPhase);
|
|
117
|
+
};
|
|
123
118
|
}
|
|
124
119
|
/**
|
|
125
120
|
* Run the given mutation as a view transition built from the options: the
|
|
@@ -132,7 +127,7 @@ var MotionView = class extends Base {
|
|
|
132
127
|
async update(mutate) {
|
|
133
128
|
const motion = await resolveMotion();
|
|
134
129
|
if (!motion.animateView) {
|
|
135
|
-
this.$warn("The resolved motion module has no `animateView()` (e.g. `motion/mini`). Provide the full `motion` entry to animate updates with MotionView.");
|
|
130
|
+
this.$warn("motion-view.missing-animate-view", "The resolved motion module has no `animateView()` (e.g. `motion/mini`). Provide the full `motion` entry to animate updates with MotionView.");
|
|
136
131
|
await mutate();
|
|
137
132
|
return;
|
|
138
133
|
}
|
|
@@ -159,8 +154,7 @@ var MotionView = class extends Base {
|
|
|
159
154
|
this.$emit("enter");
|
|
160
155
|
this.$emit("enter-start");
|
|
161
156
|
await this.update(() => {
|
|
162
|
-
|
|
163
|
-
addClass(this.target, this.$options.enterTo);
|
|
157
|
+
this.__toggleClasses(this.$options.leaveTo, this.$options.enterTo);
|
|
164
158
|
});
|
|
165
159
|
this.$emit("enter-end");
|
|
166
160
|
}
|
|
@@ -172,8 +166,7 @@ var MotionView = class extends Base {
|
|
|
172
166
|
this.$emit("leave");
|
|
173
167
|
this.$emit("leave-start");
|
|
174
168
|
await this.update(() => {
|
|
175
|
-
|
|
176
|
-
addClass(this.target, this.$options.leaveTo);
|
|
169
|
+
this.__toggleClasses(this.$options.enterTo, this.$options.leaveTo);
|
|
177
170
|
});
|
|
178
171
|
this.$emit("leave-end");
|
|
179
172
|
}
|
|
@@ -184,6 +177,17 @@ var MotionView = class extends Base {
|
|
|
184
177
|
toggle() {
|
|
185
178
|
return this.state === "entering" ? this.leave() : this.enter();
|
|
186
179
|
}
|
|
180
|
+
/**
|
|
181
|
+
* Swap one space separated class list for another on the target, the way
|
|
182
|
+
* `ViewTransition` does it.
|
|
183
|
+
* @private
|
|
184
|
+
*/
|
|
185
|
+
__toggleClasses(remove, add) {
|
|
186
|
+
const removed = remove.split(" ").filter(Boolean);
|
|
187
|
+
const added = add.split(" ").filter(Boolean);
|
|
188
|
+
if (removed.length > 0) this.target.classList.remove(...removed);
|
|
189
|
+
if (added.length > 0) this.target.classList.add(...added);
|
|
190
|
+
}
|
|
187
191
|
};
|
|
188
192
|
//#endregion
|
|
189
193
|
export { MotionView, MotionView as default };
|
package/dist/MotionView.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MotionView.js","names":[],"sources":["../src/MotionView.ts"],"sourcesContent":["import { Base } from '@studiometa/js-toolkit/Base';\nimport type { BaseProps, BaseConfig } from '@studiometa/js-toolkit';\nimport { addClass } from '@studiometa/js-toolkit/utils/addClass';\nimport { removeClass } from '@studiometa/js-toolkit/utils/removeClass';\nimport type { animateView, DOMKeyframesDefinition } from 'motion';\nimport { resolveMotion } from './dependencies.js';\n\ntype ViewTransitionOptions = NonNullable<Parameters<typeof animateView>[1]>;\n\nexport interface MotionViewProps extends BaseProps {\n $options: {\n viewTransitionName: string;\n enterTo: string;\n leaveTo: string;\n transition: ViewTransitionOptions;\n add: string;\n new: DOMKeyframesDefinition;\n old: DOMKeyframesDefinition;\n enter: DOMKeyframesDefinition;\n exit: DOMKeyframesDefinition;\n layout: boolean;\n auto: boolean;\n };\n}\n\n/**\n * MotionView class.\n *\n * Wrap DOM updates in Motion's [`animateView()`](https://motion.dev/docs/animate-view)\n * so the mutation plays as a view transition. A drop-in alternative to the\n * `ViewTransition` component — same `enter()`/`leave()`/`toggle()` surface,\n * `state` property and options — but the animation is declared with Motion\n * keyframes and transitions (including springs) instead of CSS\n * pseudo-elements. The `update()` method is the underlying primitive: hand it\n * any mutation and it animates.\n *\n * The mutation is never lost: without browser support the update applies\n * without animation, and `animateView()` is not part of `motion/mini` — the\n * component then warns and applies updates directly.\n *\n * Containment is the wiring: with the `auto` option (on by default), the\n * component wraps any `dom-update` announced by a mutating component inside\n * its subtree, and joins the open/close lifecycle of a containing `Dialog`\n * through the extendable events' `waitUntil()`. Explicit `Action` wiring\n * (`event.detail.wrap(target)`) remains for cross-subtree topologies.\n *\n * @example\n * ```html\n * <div data-component=\"MotionView\" data-option-enter-to=\"is-open\" data-option-transition='{ \"type\": \"spring\", \"bounce\": 0.3 }'>\n * Content\n * </div>\n * <button data-component=\"Action\" data-on:click=\"MotionView->target.toggle()\">Toggle</button>\n * ```\n *\n * @link https://ui.studiometa.dev/reference/items/MotionView/\n */\nexport class MotionView<T extends BaseProps = BaseProps> extends Base<MotionViewProps & T> {\n /**\n * Config.\n */\n static config: BaseConfig = {\n name: 'MotionView',\n emits: ['enter', 'enter-start', 'enter-end', 'leave', 'leave-start', 'leave-end', 'toggle'],\n options: {\n viewTransitionName: String,\n enterTo: String,\n leaveTo: String,\n transition: Object,\n add: String,\n new: Object,\n old: Object,\n enter: Object,\n exit: Object,\n layout: Boolean,\n auto: {\n type: Boolean,\n default: true,\n },\n },\n };\n\n /**\n * Current state.\n */\n state: 'entering' | 'leaving' | null = null;\n\n /**\n * Wrap a `dom-update` announced by a mutating component inside the subtree:\n * the bubbling event reaches the root element, and passing the instance to\n * `detail.wrap()` lets the emitter run its mutation through `update()`.\n * @private\n */\n __onDomUpdate = (event: Event) => {\n const { wrap } = (event as CustomEvent).detail ?? {};\n if (typeof wrap === 'function') {\n wrap(this);\n }\n };\n\n /**\n * Join the open/close lifecycle of a containing `Dialog`: its extendable\n * events bubble up past the root element to the document, and handing the\n * instance to `detail.waitUntil()` lets the dialog call `enter()` on open\n * and `leave()` on close.\n * @private\n */\n __onDialogPhase = (event: Event) => {\n const { target } = event;\n const { waitUntil } = (event as CustomEvent).detail ?? {};\n if (\n target instanceof Element &&\n target !== this.$el &&\n target.contains(this.$el) &&\n typeof waitUntil === 'function'\n ) {\n waitUntil(this);\n }\n };\n\n /**\n * Get the transition target.\n */\n get target(): HTMLElement {\n return this.$el;\n }\n\n /**\n * Assign the configured `view-transition-name` to the target element and,\n * with the `auto` option, listen for ambient wiring events: `dom-update`\n * from descendant mutators on the root element, and the extendable\n * `open`/`close` events of a containing `Dialog` on the document.\n */\n mounted() {\n const { viewTransitionName, auto } = this.$options;\n if (viewTransitionName) {\n this.target.style.setProperty('view-transition-name', viewTransitionName);\n }\n\n if (auto) {\n this.$el.addEventListener('dom-update', this.__onDomUpdate);\n document.addEventListener('open', this.__onDialogPhase);\n document.addEventListener('close', this.__onDialogPhase);\n }\n }\n\n /**\n * Remove the ambient wiring listeners.\n */\n destroyed() {\n this.$el.removeEventListener('dom-update', this.__onDomUpdate);\n document.removeEventListener('open', this.__onDialogPhase);\n document.removeEventListener('close', this.__onDialogPhase);\n }\n\n /**\n * Run the given mutation as a view transition built from the options: the\n * `add` selector picks the animated subjects within the root element (the\n * element itself by default), the `new`, `old`, `enter` and `exit` keyframes\n * apply to each subject's layers, and `layout` enables the morph transition.\n * The returned promise resolves when the animation settles and never\n * rejects; when the animation cannot run, the mutation still applies.\n */\n async update(mutate: () => void | Promise<void>): Promise<void> {\n const motion = await resolveMotion();\n\n if (!motion.animateView) {\n this.$warn(\n 'The resolved motion module has no `animateView()` (e.g. `motion/mini`). Provide the full `motion` entry to animate updates with MotionView.',\n );\n await mutate();\n return;\n }\n\n const {\n transition,\n add,\n new: newKeyframes,\n old: oldKeyframes,\n enter: enterKeyframes,\n exit: exitKeyframes,\n layout,\n } = this.$options;\n\n const builder = motion.animateView(\n mutate,\n Object.keys(transition).length > 0 ? transition : undefined,\n );\n\n const targets: Element[] = add ? Array.from(this.$el.querySelectorAll(add)) : [this.$el];\n for (const target of targets) {\n builder.add(target);\n if (Object.keys(newKeyframes).length > 0) {\n builder.new(newKeyframes);\n }\n if (Object.keys(oldKeyframes).length > 0) {\n builder.old(oldKeyframes);\n }\n if (Object.keys(enterKeyframes).length > 0) {\n builder.enter(enterKeyframes);\n }\n if (Object.keys(exitKeyframes).length > 0) {\n builder.exit(exitKeyframes);\n }\n if (layout) {\n builder.layout();\n }\n }\n\n // Browsers without the View Transitions API may reject or settle early:\n // the mutation has already run, so degrade silently instead of throwing.\n try {\n const animation = (await builder) as { finished: Promise<unknown> };\n await animation.finished;\n } catch {\n // Graceful degradation: the update ran, only the animation could not.\n }\n }\n\n /**\n * Trigger the enter transition.\n */\n async enter(): Promise<void> {\n this.state = 'entering';\n this.$emit('enter');\n this.$emit('enter-start');\n await this.update(() => {\n removeClass(this.target, this.$options.leaveTo);\n addClass(this.target, this.$options.enterTo);\n });\n this.$emit('enter-end');\n }\n\n /**\n * Trigger the leave transition.\n */\n async leave(): Promise<void> {\n this.state = 'leaving';\n this.$emit('leave');\n this.$emit('leave-start');\n await this.update(() => {\n removeClass(this.target, this.$options.enterTo);\n addClass(this.target, this.$options.leaveTo);\n });\n this.$emit('leave-end');\n }\n\n /**\n * Toggle between the enter and leave transitions.\n * Defaults to the enter transition if no transition has been triggered yet.\n */\n toggle(): Promise<void> {\n return this.state === 'entering' ? this.leave() : this.enter();\n }\n}\n\nexport default MotionView;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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K,OAAO"}
|
|
1
|
+
{"version":3,"file":"MotionView.js","names":[],"sources":["../src/MotionView.ts"],"sourcesContent":["import { Base } from '@studiometa/js-toolkit/Base';\nimport { EVENTS } from '@studiometa/js-toolkit/EVENTS';\nimport type { BaseProps, BaseConfig } from '@studiometa/js-toolkit';\nimport type { animateView, DOMKeyframesDefinition } from 'motion';\nimport { resolveMotion } from './dependencies.js';\n\ntype ViewTransitionOptions = NonNullable<Parameters<typeof animateView>[1]>;\n\nexport interface MotionViewProps extends BaseProps {\n $options: {\n viewTransitionName: string;\n enterTo: string;\n leaveTo: string;\n transition: ViewTransitionOptions;\n add: string;\n new: DOMKeyframesDefinition;\n old: DOMKeyframesDefinition;\n enter: DOMKeyframesDefinition;\n exit: DOMKeyframesDefinition;\n layout: boolean;\n auto: boolean;\n };\n /**\n * The transition lifecycle. The names mirror `ViewTransition`'s, which is\n * what makes the two interchangeable.\n */\n $emits: {\n enter: void;\n 'enter-start': void;\n 'enter-end': void;\n leave: void;\n 'leave-start': void;\n 'leave-end': void;\n };\n}\n\n/**\n * MotionView class.\n *\n * Wrap DOM updates in Motion's [`animateView()`](https://motion.dev/docs/animate-view)\n * so the mutation plays as a view transition. A drop-in alternative to the\n * `ViewTransition` component — same `enter()`/`leave()`/`toggle()` surface,\n * `state` property and options — but the animation is declared with Motion\n * keyframes and transitions (including springs) instead of CSS\n * pseudo-elements. The `update()` method is the underlying primitive: hand it\n * any mutation and it animates.\n *\n * The mutation is never lost: without browser support the update applies\n * without animation, and `animateView()` is not part of `motion/mini` — the\n * component then warns and applies updates directly.\n *\n * Containment is the wiring: with the `auto` option (on by default), the\n * component wraps any DOM update announced by a mutating component inside its\n * subtree, and joins the open/close lifecycle of a containing `Dialog` through\n * the extendable events' `waitUntil()`. Explicit `Action` wiring\n * (`event.detail.wrap(target)`) remains for cross-subtree topologies.\n *\n * @example\n * ```html\n * <div data-component=\"MotionView\" data-option-enter-to=\"is-open\" data-option-transition='{ \"type\": \"spring\", \"bounce\": 0.3 }'>\n * Content\n * </div>\n * <button data-component=\"Action\" data-on:click=\"MotionView->target.toggle()\">Toggle</button>\n * ```\n *\n * @link https://ui.studiometa.dev/reference/items/MotionView/\n */\nexport class MotionView<T extends BaseProps = BaseProps> extends Base<MotionViewProps & T> {\n /**\n * Config.\n */\n static config: BaseConfig = {\n name: 'MotionView',\n options: {\n viewTransitionName: String,\n enterTo: String,\n leaveTo: String,\n transition: Object,\n add: String,\n new: Object,\n old: Object,\n enter: Object,\n exit: Object,\n layout: Boolean,\n auto: {\n type: Boolean,\n default: true,\n },\n },\n };\n\n /**\n * Current state.\n */\n state: 'entering' | 'leaving' | null = null;\n\n /**\n * Wrap an `EVENTS.dom.update` announced by a mutating component inside the\n * subtree: the bubbling event reaches the root element, and passing the\n * instance to `detail.wrap()` lets the emitter run its mutation through\n * `update()` — the duck-typed method `domUpdate()` looks for is `update`,\n * which this component already owns.\n * @private\n */\n __onDomUpdate = (event: Event) => {\n const { wrap } = (event as CustomEvent).detail ?? {};\n if (typeof wrap === 'function') {\n wrap(this);\n }\n };\n\n /**\n * Join the open/close lifecycle of a containing `Dialog`: its extendable\n * events bubble up past the root element to the document, and registering\n * with `detail.waitUntil()` holds the dialog's choreography open until this\n * component's transition settles.\n *\n * Registered as a **function**, not as `this`. `emitExtendable()` duck-types\n * an object registration on the name of the event — it would look for\n * `open()` and `close()`, which a transition component has no business\n * owning. The function form is what the primitive documents for a pair of\n * method names that differ from the event names, and it maps `open` onto\n * `enter()` and `close` onto `leave()` explicitly.\n * @private\n */\n __onDialogPhase = (event: Event) => {\n const { target } = event;\n const { waitUntil } = (event as CustomEvent).detail ?? {};\n if (\n target instanceof Element &&\n target !== this.$el &&\n target.contains(this.$el) &&\n typeof waitUntil === 'function'\n ) {\n waitUntil(event.type === 'open' ? () => this.enter() : () => this.leave());\n }\n };\n\n /**\n * Get the transition target.\n */\n get target(): HTMLElement {\n return this.$el;\n }\n\n /**\n * Assign the configured `view-transition-name` to the target element and,\n * with the `auto` option, listen for ambient wiring events:\n * `EVENTS.dom.update` from descendant mutators on the root element, and the\n * extendable `open`/`close` events of a containing `Dialog` on the document.\n */\n mounted() {\n const { viewTransitionName, auto } = this.$options;\n if (viewTransitionName) {\n this.target.style.setProperty('view-transition-name', viewTransitionName);\n }\n\n if (!auto) {\n return;\n }\n\n this.$el.addEventListener(EVENTS.dom.update, this.__onDomUpdate);\n document.addEventListener('open', this.__onDialogPhase);\n document.addEventListener('close', this.__onDialogPhase);\n\n // Returning the teardown keeps it paired with the `auto` branch that\n // installed it.\n return () => {\n this.$el.removeEventListener(EVENTS.dom.update, this.__onDomUpdate);\n document.removeEventListener('open', this.__onDialogPhase);\n document.removeEventListener('close', this.__onDialogPhase);\n };\n }\n\n /**\n * Run the given mutation as a view transition built from the options: the\n * `add` selector picks the animated subjects within the root element (the\n * element itself by default), the `new`, `old`, `enter` and `exit` keyframes\n * apply to each subject's layers, and `layout` enables the morph transition.\n * The returned promise resolves when the animation settles and never\n * rejects; when the animation cannot run, the mutation still applies.\n */\n async update(mutate: () => void | Promise<void>): Promise<void> {\n const motion = await resolveMotion();\n\n if (!motion.animateView) {\n this.$warn(\n 'motion-view.missing-animate-view',\n 'The resolved motion module has no `animateView()` (e.g. `motion/mini`). Provide the full `motion` entry to animate updates with MotionView.',\n );\n await mutate();\n return;\n }\n\n const {\n transition,\n add,\n new: newKeyframes,\n old: oldKeyframes,\n enter: enterKeyframes,\n exit: exitKeyframes,\n layout,\n } = this.$options;\n\n const builder = motion.animateView(\n mutate,\n Object.keys(transition).length > 0 ? transition : undefined,\n );\n\n const targets: Element[] = add ? Array.from(this.$el.querySelectorAll(add)) : [this.$el];\n for (const target of targets) {\n builder.add(target);\n if (Object.keys(newKeyframes).length > 0) {\n builder.new(newKeyframes);\n }\n if (Object.keys(oldKeyframes).length > 0) {\n builder.old(oldKeyframes);\n }\n if (Object.keys(enterKeyframes).length > 0) {\n builder.enter(enterKeyframes);\n }\n if (Object.keys(exitKeyframes).length > 0) {\n builder.exit(exitKeyframes);\n }\n if (layout) {\n builder.layout();\n }\n }\n\n // Browsers without the View Transitions API may reject or settle early:\n // the mutation has already run, so degrade silently instead of throwing.\n try {\n const animation = (await builder) as { finished: Promise<unknown> };\n await animation.finished;\n } catch {\n // Graceful degradation: the update ran, only the animation could not.\n }\n }\n\n /**\n * Trigger the enter transition.\n */\n async enter(): Promise<void> {\n this.state = 'entering';\n this.$emit('enter');\n this.$emit('enter-start');\n await this.update(() => {\n this.__toggleClasses(this.$options.leaveTo, this.$options.enterTo);\n });\n this.$emit('enter-end');\n }\n\n /**\n * Trigger the leave transition.\n */\n async leave(): Promise<void> {\n this.state = 'leaving';\n this.$emit('leave');\n this.$emit('leave-start');\n await this.update(() => {\n this.__toggleClasses(this.$options.enterTo, this.$options.leaveTo);\n });\n this.$emit('leave-end');\n }\n\n /**\n * Toggle between the enter and leave transitions.\n * Defaults to the enter transition if no transition has been triggered yet.\n */\n toggle(): Promise<void> {\n return this.state === 'entering' ? this.leave() : this.enter();\n }\n\n /**\n * Swap one space separated class list for another on the target, the way\n * `ViewTransition` does it.\n * @private\n */\n __toggleClasses(remove: string, add: string): void {\n const removed = remove.split(' ').filter(Boolean);\n const added = add.split(' ').filter(Boolean);\n\n if (removed.length > 0) {\n this.target.classList.remove(...removed);\n }\n\n if (added.length > 0) {\n this.target.classList.add(...added);\n }\n }\n}\n\nexport default MotionView;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmEA,IAAa,aAAb,cAAiE,KAA0B;;;;CAIzF,OAAO,SAAqB;EAC1B,MAAM;EACN,SAAS;GACP,oBAAoB;GACpB,SAAS;GACT,SAAS;GACT,YAAY;GACZ,KAAK;GACL,KAAK;GACL,KAAK;GACL,OAAO;GACP,MAAM;GACN,QAAQ;GACR,MAAM;IACJ,MAAM;IACN,SAAS;IACV;GACF;EACF;;;;CAKD,QAAuC;;;;;;;;;CAUvC,iBAAiB,UAAiB;EAChC,MAAM,EAAE,SAAU,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|
29
29
|
/**
|
|
30
30
|
* Inject the `motion` module the components should use.
|
|
31
31
|
*
|
|
32
|
-
* Call once, before the components mount
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
32
|
+
* Call once, before the components mount — e.g. right before the
|
|
33
|
+
* `registerComponent()` or `registerManifest()` call that puts them on the
|
|
34
|
+
* page. Once provided, `@studiometa/ui-motion` never imports `motion` by
|
|
35
|
+
* specifier — it uses this instance — so a host can supply its own build (a
|
|
36
|
+
* specific version, the smaller `motion/mini` entry, an import-map or CDN
|
|
37
|
+
* module).
|
|
36
38
|
*
|
|
37
39
|
* @param {MotionModule} instance The `motion` module namespace.
|
|
38
40
|
*/
|
package/dist/dependencies.js
CHANGED
|
@@ -5,10 +5,12 @@ let motionProvided = false;
|
|
|
5
5
|
/**
|
|
6
6
|
* Inject the `motion` module the components should use.
|
|
7
7
|
*
|
|
8
|
-
* Call once, before the components mount
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
8
|
+
* Call once, before the components mount — e.g. right before the
|
|
9
|
+
* `registerComponent()` or `registerManifest()` call that puts them on the
|
|
10
|
+
* page. Once provided, `@studiometa/ui-motion` never imports `motion` by
|
|
11
|
+
* specifier — it uses this instance — so a host can supply its own build (a
|
|
12
|
+
* specific version, the smaller `motion/mini` entry, an import-map or CDN
|
|
13
|
+
* module).
|
|
12
14
|
*
|
|
13
15
|
* @param {MotionModule} instance The `motion` module namespace.
|
|
14
16
|
*/
|
package/dist/dependencies.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dependencies.js","names":[],"sources":["../src/dependencies.ts"],"sourcesContent":["import type {\n animate,\n animateView,\n scroll,\n hover,\n press,\n inView,\n transformProps,\n readTransformValue,\n getComputedStyle,\n} from 'motion';\n\n/**\n * The subset of the `motion` module the components consume. Declared\n * structurally so a host can inject the full `motion` entry, the smaller\n * `motion/mini` entry or any compatible build — only the members listed here\n * are ever read. Everything but `animate` is optional because `motion/mini`\n * does not ship it: `animateView` powers `MotionView`, `scroll` powers\n * `MotionScrollTimeline`, and `hover`, `press` and `inView` power the\n * matching `Motion` gesture options.\n *\n * `transformProps`, `readTransformValue` and `getComputedStyle` are Motion's\n * own value readers. `Motion` uses them to capture the base value a gesture\n * returns to, the way Motion's `VisualElement.readValueFromInstance()` does.\n * They ship alongside the gesture functions in the full entry.\n */\nexport interface MotionModule {\n animate: typeof animate;\n animateView?: typeof animateView;\n scroll?: typeof scroll;\n hover?: typeof hover;\n press?: typeof press;\n inView?: typeof inView;\n transformProps?: typeof transformProps;\n readTransformValue?: typeof readTransformValue;\n getComputedStyle?: typeof getComputedStyle;\n}\n\nlet motionInstance: MotionModule | undefined;\nlet motionPromise: Promise<MotionModule> | undefined;\nlet motionProvided = false;\n\n/**\n * Inject the `motion` module the components should use.\n *\n * Call once, before the components mount
|
|
1
|
+
{"version":3,"file":"dependencies.js","names":[],"sources":["../src/dependencies.ts"],"sourcesContent":["import type {\n animate,\n animateView,\n scroll,\n hover,\n press,\n inView,\n transformProps,\n readTransformValue,\n getComputedStyle,\n} from 'motion';\n\n/**\n * The subset of the `motion` module the components consume. Declared\n * structurally so a host can inject the full `motion` entry, the smaller\n * `motion/mini` entry or any compatible build — only the members listed here\n * are ever read. Everything but `animate` is optional because `motion/mini`\n * does not ship it: `animateView` powers `MotionView`, `scroll` powers\n * `MotionScrollTimeline`, and `hover`, `press` and `inView` power the\n * matching `Motion` gesture options.\n *\n * `transformProps`, `readTransformValue` and `getComputedStyle` are Motion's\n * own value readers. `Motion` uses them to capture the base value a gesture\n * returns to, the way Motion's `VisualElement.readValueFromInstance()` does.\n * They ship alongside the gesture functions in the full entry.\n */\nexport interface MotionModule {\n animate: typeof animate;\n animateView?: typeof animateView;\n scroll?: typeof scroll;\n hover?: typeof hover;\n press?: typeof press;\n inView?: typeof inView;\n transformProps?: typeof transformProps;\n readTransformValue?: typeof readTransformValue;\n getComputedStyle?: typeof getComputedStyle;\n}\n\nlet motionInstance: MotionModule | undefined;\nlet motionPromise: Promise<MotionModule> | undefined;\nlet motionProvided = false;\n\n/**\n * Inject the `motion` module the components should use.\n *\n * Call once, before the components mount — e.g. right before the\n * `registerComponent()` or `registerManifest()` call that puts them on the\n * page. Once provided, `@studiometa/ui-motion` never imports `motion` by\n * specifier — it uses this instance — so a host can supply its own build (a\n * specific version, the smaller `motion/mini` entry, an import-map or CDN\n * module).\n *\n * @param {MotionModule} instance The `motion` module namespace.\n */\nexport function provideMotion(instance: MotionModule): void {\n motionProvided = true;\n motionInstance = instance;\n motionPromise = Promise.resolve(instance);\n}\n\n/**\n * Resolve the `motion` module: the injected instance when one was provided\n * through {@link provideMotion}, otherwise a lazily loaded `import('motion')`.\n *\n * The result is memoized, so `motion` loads at most once and repeated calls\n * are cheap. The resolved instance is also cached for {@link getMotion}.\n *\n * @returns {Promise<MotionModule>}\n */\nexport function resolveMotion(): Promise<MotionModule> {\n if (!motionPromise) {\n motionPromise = import('motion').then((module) => {\n const instance = module as MotionModule;\n // A provideMotion() call may have won the race while this import was in\n // flight: never let the fallback overwrite an injected instance. Returning\n // the authoritative instance also keeps callers that awaited this promise\n // before the injection consistent with the provided one.\n if (!motionProvided) {\n motionInstance = instance;\n }\n return motionInstance as MotionModule;\n });\n }\n\n return motionPromise;\n}\n\n/**\n * Synchronously read the resolved `motion` module.\n *\n * Only valid after {@link resolveMotion} (or {@link provideMotion}) has run;\n * it throws otherwise. Components read it on synchronous hot paths that are\n * only ever reached once their animation — and therefore `motion` — is ready.\n *\n * @returns {MotionModule}\n */\nexport function getMotion(): MotionModule {\n if (!motionInstance) {\n throw new Error(\n '[@studiometa/ui-motion] motion has not been resolved yet. Await resolveMotion() (or call provideMotion()) before reading it synchronously.',\n );\n }\n\n return motionInstance;\n}\n"],"mappings":";AAsCA,IAAI;AACJ,IAAI;AACJ,IAAI,iBAAiB;;;;;;;;;;;;;AAcrB,SAAgB,cAAc,UAA8B;AAC1D,kBAAiB;AACjB,kBAAiB;AACjB,iBAAgB,QAAQ,QAAQ,SAAS;;;;;;;;;;;AAY3C,SAAgB,gBAAuC;AACrD,KAAI,CAAC,cACH,iBAAgB,OAAO,UAAU,MAAM,WAAW;EAChD,MAAM,WAAW;AAKjB,MAAI,CAAC,eACH,kBAAiB;AAEnB,SAAO;GACP;AAGJ,QAAO;;;;;;;;;;;AAYT,SAAgB,YAA0B;AACxC,KAAI,CAAC,eACH,OAAM,IAAI,MACR,6IACD;AAGH,QAAO"}
|
package/dist/manifest.js
CHANGED
|
@@ -1,41 +1,19 @@
|
|
|
1
1
|
//#region src/manifest.ts
|
|
2
2
|
const manifest = {
|
|
3
3
|
Motion: {
|
|
4
|
-
|
|
5
|
-
packageName: "@studiometa/ui-motion",
|
|
6
|
-
subpath: "Motion",
|
|
7
|
-
exportName: "Motion",
|
|
8
|
-
strategy: "visible",
|
|
9
|
-
group: "motion",
|
|
4
|
+
mountStrategy: "visible",
|
|
10
5
|
load: () => import("./Motion.js").then(({ Motion }) => Motion)
|
|
11
6
|
},
|
|
12
7
|
MotionScrollTimeline: {
|
|
13
|
-
|
|
14
|
-
packageName: "@studiometa/ui-motion",
|
|
15
|
-
subpath: "MotionScrollTimeline",
|
|
16
|
-
exportName: "MotionScrollTimeline",
|
|
17
|
-
strategy: "visible",
|
|
18
|
-
group: "motion",
|
|
19
|
-
children: ["Motion"],
|
|
8
|
+
mountStrategy: "visible",
|
|
20
9
|
load: () => import("./MotionScrollTimeline.js").then(({ MotionScrollTimeline }) => MotionScrollTimeline)
|
|
21
10
|
},
|
|
22
11
|
MotionSequence: {
|
|
23
|
-
|
|
24
|
-
packageName: "@studiometa/ui-motion",
|
|
25
|
-
subpath: "MotionSequence",
|
|
26
|
-
exportName: "MotionSequence",
|
|
27
|
-
strategy: "visible",
|
|
28
|
-
group: "motion",
|
|
29
|
-
children: ["Motion"],
|
|
12
|
+
mountStrategy: "visible",
|
|
30
13
|
load: () => import("./MotionSequence.js").then(({ MotionSequence }) => MotionSequence)
|
|
31
14
|
},
|
|
32
15
|
MotionView: {
|
|
33
|
-
|
|
34
|
-
packageName: "@studiometa/ui-motion",
|
|
35
|
-
subpath: "MotionView",
|
|
36
|
-
exportName: "MotionView",
|
|
37
|
-
strategy: "visible",
|
|
38
|
-
group: "motion",
|
|
16
|
+
mountStrategy: "visible",
|
|
39
17
|
load: () => import("./MotionView.js").then(({ MotionView }) => MotionView)
|
|
40
18
|
}
|
|
41
19
|
};
|
package/dist/manifest.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.js","names":[],"sources":["../src/manifest.ts"],"sourcesContent":["// This file is generated by scripts/generate-manifests.ts. Do not edit.\nimport type { ComponentManifest } from '@studiometa/js-toolkit';\n\nexport const manifest: ComponentManifest = {\n Motion: {\n
|
|
1
|
+
{"version":3,"file":"manifest.js","names":[],"sources":["../src/manifest.ts"],"sourcesContent":["// This file is generated by scripts/generate-manifests.ts. Do not edit.\nimport type { ComponentManifest } from '@studiometa/js-toolkit';\n\nexport const manifest: ComponentManifest = {\n Motion: {\n mountStrategy: 'visible',\n load: () => import('./Motion.js').then(({ Motion }) => Motion),\n },\n MotionScrollTimeline: {\n mountStrategy: 'visible',\n load: () =>\n import('./MotionScrollTimeline.js').then(({ MotionScrollTimeline }) => MotionScrollTimeline),\n },\n MotionSequence: {\n mountStrategy: 'visible',\n load: () => import('./MotionSequence.js').then(({ MotionSequence }) => MotionSequence),\n },\n MotionView: {\n mountStrategy: 'visible',\n load: () => import('./MotionView.js').then(({ MotionView }) => MotionView),\n },\n};\n"],"mappings":";AAGA,MAAa,WAA8B;CACzC,QAAQ;EACN,eAAe;EACf,YAAY,OAAO,eAAe,MAAM,EAAE,aAAa,OAAO;EAC/D;CACD,sBAAsB;EACpB,eAAe;EACf,YACE,OAAO,6BAA6B,MAAM,EAAE,2BAA2B,qBAAqB;EAC/F;CACD,gBAAgB;EACd,eAAe;EACf,YAAY,OAAO,uBAAuB,MAAM,EAAE,qBAAqB,eAAe;EACvF;CACD,YAAY;EACV,eAAe;EACf,YAAY,OAAO,mBAAmB,MAAM,EAAE,iBAAiB,WAAW;EAC3E;CACF"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MountStrategy } from "@studiometa/js-toolkit";
|
|
2
2
|
|
|
3
3
|
//#region ../../scripts/manifest-types.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* Authoring types for the component manifest generator (`scripts/generate-manifests.ts`).
|
|
6
6
|
*
|
|
7
7
|
* The runtime manifest types (`ComponentManifest`, `ComponentManifestEntry`,
|
|
8
|
-
* `
|
|
8
|
+
* `MountStrategy`) now live in `@studiometa/js-toolkit`. These two authoring types are
|
|
9
9
|
* repository-local tooling contracts: they describe the hand-authored catalogs the generator reads
|
|
10
10
|
* (`packages/ui/catalog.ts`, `packages/ui-mapbox/src/catalog.ts`), never a published runtime surface.
|
|
11
11
|
*/
|
|
@@ -28,7 +28,7 @@ interface CuratedComponentMetadata {
|
|
|
28
28
|
*/
|
|
29
29
|
interface ComponentCatalog {
|
|
30
30
|
packageName: string;
|
|
31
|
-
strategy:
|
|
31
|
+
strategy: MountStrategy;
|
|
32
32
|
components: readonly CuratedComponentMetadata[];
|
|
33
33
|
abstractExports: readonly string[];
|
|
34
34
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@studiometa/ui-motion",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-alpha.0",
|
|
4
4
|
"description": "Vanilla js-toolkit components to animate elements declaratively with Motion",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -79,11 +79,11 @@
|
|
|
79
79
|
},
|
|
80
80
|
"homepage": "https://github.com/studiometa/ui#readme",
|
|
81
81
|
"peerDependencies": {
|
|
82
|
-
"@studiometa/js-toolkit": "^
|
|
82
|
+
"@studiometa/js-toolkit": "^4.0.0-alpha.1",
|
|
83
83
|
"motion": "^13.0.0"
|
|
84
84
|
},
|
|
85
85
|
"devDependencies": {
|
|
86
|
-
"@studiometa/js-toolkit": "^
|
|
86
|
+
"@studiometa/js-toolkit": "^4.0.0-alpha.1",
|
|
87
87
|
"motion": "^13.0.0",
|
|
88
88
|
"tsdown": "0.21.5"
|
|
89
89
|
},
|