@studiometa/ui-motion 0.0.1 → 1.11.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.
@@ -0,0 +1,70 @@
1
+ import { Motion } from "./Motion.js";
2
+ import { BaseConfig, BaseProps } from "@studiometa/js-toolkit";
3
+ import { AnimationPlaybackControlsWithThen } from "motion";
4
+
5
+ //#region src/MotionSequence.d.ts
6
+ interface MotionSequenceProps extends BaseProps {
7
+ $children: {
8
+ Motion: Motion[];
9
+ };
10
+ $options: {
11
+ stagger: number;
12
+ };
13
+ }
14
+ /**
15
+ * MotionSequence class.
16
+ *
17
+ * Orchestrate the `Motion` children as one animation sequence: each child
18
+ * declares its keyframes as usual, and the sequence composes them — in DOM
19
+ * order — into a single timeline with Motion's [sequence support](https://motion.dev/docs/animate#timeline-sequencing).
20
+ * The whole `Motion` playback surface applies to the sequence: an `Action`
21
+ * can `play()`, `reverse()` or `seek()` the entire choreography, and a
22
+ * `MotionScrollTimeline` can scrub it.
23
+ *
24
+ * By default segments run one after another; a child's `at` option positions
25
+ * its segment explicitly (a time in seconds, a relative offset like `"-0.2"`,
26
+ * or `"<"` for "with the previous"), and the `stagger` option spreads the
27
+ * children automatically. The sequence owns the children's playback — leave
28
+ * their `autoplay` off (its default) and enable it on the sequence itself
29
+ * with `data-option-autoplay` to play on mount.
30
+ *
31
+ * Sequences need the full `motion` entry: `motion/mini`'s `animate()` does
32
+ * not support them.
33
+ *
34
+ * @example
35
+ * ```html
36
+ * <ul data-component="MotionSequence" data-option-stagger="0.1" data-option-autoplay>
37
+ * <li data-component="Motion" data-option-animate='{ "opacity": 1, "y": 0 }' data-option-initial='{ "opacity": 0, "y": 16 }'>…</li>
38
+ * <li data-component="Motion" data-option-animate='{ "opacity": 1, "y": 0 }' data-option-initial='{ "opacity": 0, "y": 16 }'>…</li>
39
+ * </ul>
40
+ * ```
41
+ *
42
+ * @link https://ui.studiometa.dev/reference/items/MotionSequence/
43
+ */
44
+ declare class MotionSequence<T extends BaseProps = BaseProps> extends Motion<MotionSequenceProps & T> {
45
+ /**
46
+ * Config.
47
+ */
48
+ static config: BaseConfig;
49
+ /**
50
+ * The sequence has an animation to autoplay as soon as it has children
51
+ * declaring keyframes.
52
+ * @protected
53
+ */
54
+ get __hasDeclaredAnimation(): boolean;
55
+ /**
56
+ * The `Motion` children carrying keyframes, in DOM order.
57
+ * @private
58
+ */
59
+ get __sequencedChildren(): Motion[];
60
+ /**
61
+ * Build the sequence from the children declarations and make it the
62
+ * current animation. Each segment merges the child's `transition` and its
63
+ * `at` position; without an explicit `at`, the `stagger` option spreads the
64
+ * segments, and with neither they run one after another (Motion's default).
65
+ * @protected
66
+ */
67
+ __createControls(): AnimationPlaybackControlsWithThen;
68
+ }
69
+ //#endregion
70
+ export { MotionSequence, MotionSequence as default, MotionSequenceProps };
@@ -0,0 +1,91 @@
1
+ import { getMotion } from "./dependencies.js";
2
+ import { Motion } from "./Motion.js";
3
+ //#region src/MotionSequence.ts
4
+ /**
5
+ * MotionSequence class.
6
+ *
7
+ * Orchestrate the `Motion` children as one animation sequence: each child
8
+ * declares its keyframes as usual, and the sequence composes them — in DOM
9
+ * order — into a single timeline with Motion's [sequence support](https://motion.dev/docs/animate#timeline-sequencing).
10
+ * The whole `Motion` playback surface applies to the sequence: an `Action`
11
+ * can `play()`, `reverse()` or `seek()` the entire choreography, and a
12
+ * `MotionScrollTimeline` can scrub it.
13
+ *
14
+ * By default segments run one after another; a child's `at` option positions
15
+ * its segment explicitly (a time in seconds, a relative offset like `"-0.2"`,
16
+ * or `"<"` for "with the previous"), and the `stagger` option spreads the
17
+ * children automatically. The sequence owns the children's playback — leave
18
+ * their `autoplay` off (its default) and enable it on the sequence itself
19
+ * with `data-option-autoplay` to play on mount.
20
+ *
21
+ * Sequences need the full `motion` entry: `motion/mini`'s `animate()` does
22
+ * not support them.
23
+ *
24
+ * @example
25
+ * ```html
26
+ * <ul data-component="MotionSequence" data-option-stagger="0.1" data-option-autoplay>
27
+ * <li data-component="Motion" data-option-animate='{ "opacity": 1, "y": 0 }' data-option-initial='{ "opacity": 0, "y": 16 }'>…</li>
28
+ * <li data-component="Motion" data-option-animate='{ "opacity": 1, "y": 0 }' data-option-initial='{ "opacity": 0, "y": 16 }'>…</li>
29
+ * </ul>
30
+ * ```
31
+ *
32
+ * @link https://ui.studiometa.dev/reference/items/MotionSequence/
33
+ */
34
+ var MotionSequence = class extends Motion {
35
+ /**
36
+ * Config.
37
+ */
38
+ static config = {
39
+ name: "MotionSequence",
40
+ components: { Motion },
41
+ options: { stagger: Number }
42
+ };
43
+ /**
44
+ * The sequence has an animation to autoplay as soon as it has children
45
+ * declaring keyframes.
46
+ * @protected
47
+ */
48
+ get __hasDeclaredAnimation() {
49
+ return this.__sequencedChildren.length > 0;
50
+ }
51
+ /**
52
+ * The `Motion` children carrying keyframes, in DOM order.
53
+ * @private
54
+ */
55
+ get __sequencedChildren() {
56
+ const { Motion: children = [] } = this.$children;
57
+ return children.filter((child) => Object.keys(child.$options.animate).length > 0);
58
+ }
59
+ /**
60
+ * Build the sequence from the children declarations and make it the
61
+ * current animation. Each segment merges the child's `transition` and its
62
+ * `at` position; without an explicit `at`, the `stagger` option spreads the
63
+ * segments, and with neither they run one after another (Motion's default).
64
+ * @protected
65
+ */
66
+ __createControls() {
67
+ const motion = getMotion();
68
+ const { stagger, transition } = this.$options;
69
+ const sequence = this.__sequencedChildren.map((child, index) => {
70
+ const { transition: childTransition, at } = child.$options;
71
+ const options = { ...childTransition };
72
+ if (at !== "") {
73
+ const numeric = Number(at);
74
+ options.at = Number.isNaN(numeric) ? at : numeric;
75
+ } else if (stagger > 0) options.at = index * stagger;
76
+ return [
77
+ child.$el,
78
+ child.keyframes,
79
+ options
80
+ ];
81
+ });
82
+ const controls = motion.animate(sequence, Object.keys(transition).length > 0 ? transition : void 0);
83
+ this.__controls = controls;
84
+ this.__fromOptions = true;
85
+ return controls;
86
+ }
87
+ };
88
+ //#endregion
89
+ export { MotionSequence, MotionSequence as default };
90
+
91
+ //# sourceMappingURL=MotionSequence.js.map
@@ -0,0 +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 $children: {\n Motion: Motion[];\n };\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 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 const { Motion: children = [] } = this.$children;\n return children.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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CA,IAAa,iBAAb,cAAqE,OAEnE;;;;CAIA,OAAO,SAAqB;EAC1B,MAAM;EACN,YAAY,EAAE,QAAQ;EACtB,SAAS,EACP,SAAS,QACV;EACF;;;;;;CAOD,IAAI,yBAAkC;AACpC,SAAO,KAAK,oBAAoB,SAAS;;;;;;CAO3C,IAAI,sBAAgC;EAClC,MAAM,EAAE,QAAQ,WAAW,EAAE,KAAK,KAAK;AACvC,SAAO,SAAS,QAAQ,UAAU,OAAO,KAAK,MAAM,SAAS,QAAQ,CAAC,SAAS,EAAE;;;;;;;;;CAUnF,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"}
@@ -0,0 +1,116 @@
1
+ import { Base } from "@studiometa/js-toolkit/Base";
2
+ import { BaseConfig, BaseProps } from "@studiometa/js-toolkit";
3
+ import { DOMKeyframesDefinition, animateView } from "motion";
4
+
5
+ //#region src/MotionView.d.ts
6
+ type ViewTransitionOptions = NonNullable<Parameters<typeof animateView>[1]>;
7
+ interface MotionViewProps extends BaseProps {
8
+ $options: {
9
+ viewTransitionName: string;
10
+ enterTo: string;
11
+ leaveTo: string;
12
+ transition: ViewTransitionOptions;
13
+ add: string;
14
+ new: DOMKeyframesDefinition;
15
+ old: DOMKeyframesDefinition;
16
+ enter: DOMKeyframesDefinition;
17
+ exit: DOMKeyframesDefinition;
18
+ layout: boolean;
19
+ auto: boolean;
20
+ };
21
+ }
22
+ /**
23
+ * MotionView class.
24
+ *
25
+ * Wrap DOM updates in Motion's [`animateView()`](https://motion.dev/docs/animate-view)
26
+ * so the mutation plays as a view transition. A drop-in alternative to the
27
+ * `ViewTransition` component — same `enter()`/`leave()`/`toggle()` surface,
28
+ * `state` property and options — but the animation is declared with Motion
29
+ * keyframes and transitions (including springs) instead of CSS
30
+ * pseudo-elements. The `update()` method is the underlying primitive: hand it
31
+ * any mutation and it animates.
32
+ *
33
+ * The mutation is never lost: without browser support the update applies
34
+ * without animation, and `animateView()` is not part of `motion/mini` — the
35
+ * component then warns and applies updates directly.
36
+ *
37
+ * Containment is the wiring: with the `auto` option (on by default), the
38
+ * component wraps any `dom-update` announced by a mutating component inside
39
+ * its subtree, and joins the open/close lifecycle of a containing `Dialog`
40
+ * through the extendable events' `waitUntil()`. Explicit `Action` wiring
41
+ * (`event.detail.wrap(target)`) remains for cross-subtree topologies.
42
+ *
43
+ * @example
44
+ * ```html
45
+ * <div data-component="MotionView" data-option-enter-to="is-open" data-option-transition='{ "type": "spring", "bounce": 0.3 }'>
46
+ * Content
47
+ * </div>
48
+ * <button data-component="Action" data-on:click="MotionView->target.toggle()">Toggle</button>
49
+ * ```
50
+ *
51
+ * @link https://ui.studiometa.dev/reference/items/MotionView/
52
+ */
53
+ declare class MotionView<T extends BaseProps = BaseProps> extends Base<MotionViewProps & T> {
54
+ /**
55
+ * Config.
56
+ */
57
+ static config: BaseConfig;
58
+ /**
59
+ * Current state.
60
+ */
61
+ state: 'entering' | 'leaving' | null;
62
+ /**
63
+ * Wrap a `dom-update` announced by a mutating component inside the subtree:
64
+ * the bubbling event reaches the root element, and passing the instance to
65
+ * `detail.wrap()` lets the emitter run its mutation through `update()`.
66
+ * @private
67
+ */
68
+ __onDomUpdate: (event: Event) => void;
69
+ /**
70
+ * Join the open/close lifecycle of a containing `Dialog`: its extendable
71
+ * events bubble up past the root element to the document, and handing the
72
+ * instance to `detail.waitUntil()` lets the dialog call `enter()` on open
73
+ * and `leave()` on close.
74
+ * @private
75
+ */
76
+ __onDialogPhase: (event: Event) => void;
77
+ /**
78
+ * Get the transition target.
79
+ */
80
+ get target(): HTMLElement;
81
+ /**
82
+ * Assign the configured `view-transition-name` to the target element and,
83
+ * with the `auto` option, listen for ambient wiring events: `dom-update`
84
+ * from descendant mutators on the root element, and the extendable
85
+ * `open`/`close` events of a containing `Dialog` on the document.
86
+ */
87
+ mounted(): void;
88
+ /**
89
+ * Remove the ambient wiring listeners.
90
+ */
91
+ destroyed(): void;
92
+ /**
93
+ * Run the given mutation as a view transition built from the options: the
94
+ * `add` selector picks the animated subjects within the root element (the
95
+ * element itself by default), the `new`, `old`, `enter` and `exit` keyframes
96
+ * apply to each subject's layers, and `layout` enables the morph transition.
97
+ * The returned promise resolves when the animation settles and never
98
+ * rejects; when the animation cannot run, the mutation still applies.
99
+ */
100
+ update(mutate: () => void | Promise<void>): Promise<void>;
101
+ /**
102
+ * Trigger the enter transition.
103
+ */
104
+ enter(): Promise<void>;
105
+ /**
106
+ * Trigger the leave transition.
107
+ */
108
+ leave(): Promise<void>;
109
+ /**
110
+ * Toggle between the enter and leave transitions.
111
+ * Defaults to the enter transition if no transition has been triggered yet.
112
+ */
113
+ toggle(): Promise<void>;
114
+ }
115
+ //#endregion
116
+ export { MotionView, MotionView as default, MotionViewProps };
@@ -0,0 +1,191 @@
1
+ import { resolveMotion } from "./dependencies.js";
2
+ import { Base } from "@studiometa/js-toolkit/Base";
3
+ import { addClass } from "@studiometa/js-toolkit/utils/addClass";
4
+ import { removeClass } from "@studiometa/js-toolkit/utils/removeClass";
5
+ //#region src/MotionView.ts
6
+ /**
7
+ * MotionView class.
8
+ *
9
+ * Wrap DOM updates in Motion's [`animateView()`](https://motion.dev/docs/animate-view)
10
+ * so the mutation plays as a view transition. A drop-in alternative to the
11
+ * `ViewTransition` component — same `enter()`/`leave()`/`toggle()` surface,
12
+ * `state` property and options — but the animation is declared with Motion
13
+ * keyframes and transitions (including springs) instead of CSS
14
+ * pseudo-elements. The `update()` method is the underlying primitive: hand it
15
+ * any mutation and it animates.
16
+ *
17
+ * The mutation is never lost: without browser support the update applies
18
+ * without animation, and `animateView()` is not part of `motion/mini` — the
19
+ * component then warns and applies updates directly.
20
+ *
21
+ * Containment is the wiring: with the `auto` option (on by default), the
22
+ * component wraps any `dom-update` announced by a mutating component inside
23
+ * its subtree, and joins the open/close lifecycle of a containing `Dialog`
24
+ * through the extendable events' `waitUntil()`. Explicit `Action` wiring
25
+ * (`event.detail.wrap(target)`) remains for cross-subtree topologies.
26
+ *
27
+ * @example
28
+ * ```html
29
+ * <div data-component="MotionView" data-option-enter-to="is-open" data-option-transition='{ "type": "spring", "bounce": 0.3 }'>
30
+ * Content
31
+ * </div>
32
+ * <button data-component="Action" data-on:click="MotionView->target.toggle()">Toggle</button>
33
+ * ```
34
+ *
35
+ * @link https://ui.studiometa.dev/reference/items/MotionView/
36
+ */
37
+ var MotionView = class extends Base {
38
+ /**
39
+ * Config.
40
+ */
41
+ static config = {
42
+ name: "MotionView",
43
+ emits: [
44
+ "enter",
45
+ "enter-start",
46
+ "enter-end",
47
+ "leave",
48
+ "leave-start",
49
+ "leave-end",
50
+ "toggle"
51
+ ],
52
+ options: {
53
+ viewTransitionName: String,
54
+ enterTo: String,
55
+ leaveTo: String,
56
+ transition: Object,
57
+ add: String,
58
+ new: Object,
59
+ old: Object,
60
+ enter: Object,
61
+ exit: Object,
62
+ layout: Boolean,
63
+ auto: {
64
+ type: Boolean,
65
+ default: true
66
+ }
67
+ }
68
+ };
69
+ /**
70
+ * Current state.
71
+ */
72
+ state = null;
73
+ /**
74
+ * Wrap a `dom-update` announced by a mutating component inside the subtree:
75
+ * the bubbling event reaches the root element, and passing the instance to
76
+ * `detail.wrap()` lets the emitter run its mutation through `update()`.
77
+ * @private
78
+ */
79
+ __onDomUpdate = (event) => {
80
+ const { wrap } = event.detail ?? {};
81
+ if (typeof wrap === "function") wrap(this);
82
+ };
83
+ /**
84
+ * Join the open/close lifecycle of a containing `Dialog`: its extendable
85
+ * events bubble up past the root element to the document, and handing the
86
+ * instance to `detail.waitUntil()` lets the dialog call `enter()` on open
87
+ * and `leave()` on close.
88
+ * @private
89
+ */
90
+ __onDialogPhase = (event) => {
91
+ const { target } = event;
92
+ const { waitUntil } = event.detail ?? {};
93
+ if (target instanceof Element && target !== this.$el && target.contains(this.$el) && typeof waitUntil === "function") waitUntil(this);
94
+ };
95
+ /**
96
+ * Get the transition target.
97
+ */
98
+ get target() {
99
+ return this.$el;
100
+ }
101
+ /**
102
+ * Assign the configured `view-transition-name` to the target element and,
103
+ * with the `auto` option, listen for ambient wiring events: `dom-update`
104
+ * from descendant mutators on the root element, and the extendable
105
+ * `open`/`close` events of a containing `Dialog` on the document.
106
+ */
107
+ mounted() {
108
+ const { viewTransitionName, auto } = this.$options;
109
+ if (viewTransitionName) this.target.style.setProperty("view-transition-name", viewTransitionName);
110
+ if (auto) {
111
+ this.$el.addEventListener("dom-update", this.__onDomUpdate);
112
+ document.addEventListener("open", this.__onDialogPhase);
113
+ document.addEventListener("close", this.__onDialogPhase);
114
+ }
115
+ }
116
+ /**
117
+ * Remove the ambient wiring listeners.
118
+ */
119
+ destroyed() {
120
+ this.$el.removeEventListener("dom-update", this.__onDomUpdate);
121
+ document.removeEventListener("open", this.__onDialogPhase);
122
+ document.removeEventListener("close", this.__onDialogPhase);
123
+ }
124
+ /**
125
+ * Run the given mutation as a view transition built from the options: the
126
+ * `add` selector picks the animated subjects within the root element (the
127
+ * element itself by default), the `new`, `old`, `enter` and `exit` keyframes
128
+ * apply to each subject's layers, and `layout` enables the morph transition.
129
+ * The returned promise resolves when the animation settles and never
130
+ * rejects; when the animation cannot run, the mutation still applies.
131
+ */
132
+ async update(mutate) {
133
+ const motion = await resolveMotion();
134
+ 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.");
136
+ await mutate();
137
+ return;
138
+ }
139
+ const { transition, add, new: newKeyframes, old: oldKeyframes, enter: enterKeyframes, exit: exitKeyframes, layout } = this.$options;
140
+ const builder = motion.animateView(mutate, Object.keys(transition).length > 0 ? transition : void 0);
141
+ const targets = add ? Array.from(this.$el.querySelectorAll(add)) : [this.$el];
142
+ for (const target of targets) {
143
+ builder.add(target);
144
+ if (Object.keys(newKeyframes).length > 0) builder.new(newKeyframes);
145
+ if (Object.keys(oldKeyframes).length > 0) builder.old(oldKeyframes);
146
+ if (Object.keys(enterKeyframes).length > 0) builder.enter(enterKeyframes);
147
+ if (Object.keys(exitKeyframes).length > 0) builder.exit(exitKeyframes);
148
+ if (layout) builder.layout();
149
+ }
150
+ try {
151
+ await (await builder).finished;
152
+ } catch {}
153
+ }
154
+ /**
155
+ * Trigger the enter transition.
156
+ */
157
+ async enter() {
158
+ this.state = "entering";
159
+ this.$emit("enter");
160
+ this.$emit("enter-start");
161
+ await this.update(() => {
162
+ removeClass(this.target, this.$options.leaveTo);
163
+ addClass(this.target, this.$options.enterTo);
164
+ });
165
+ this.$emit("enter-end");
166
+ }
167
+ /**
168
+ * Trigger the leave transition.
169
+ */
170
+ async leave() {
171
+ this.state = "leaving";
172
+ this.$emit("leave");
173
+ this.$emit("leave-start");
174
+ await this.update(() => {
175
+ removeClass(this.target, this.$options.enterTo);
176
+ addClass(this.target, this.$options.leaveTo);
177
+ });
178
+ this.$emit("leave-end");
179
+ }
180
+ /**
181
+ * Toggle between the enter and leave transitions.
182
+ * Defaults to the enter transition if no transition has been triggered yet.
183
+ */
184
+ toggle() {
185
+ return this.state === "entering" ? this.leave() : this.enter();
186
+ }
187
+ };
188
+ //#endregion
189
+ export { MotionView, MotionView as default };
190
+
191
+ //# sourceMappingURL=MotionView.js.map
@@ -0,0 +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();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;;CAOzB,SAAwB;AACtB,SAAO,KAAK,UAAU,aAAa,KAAK,OAAO,GAAG,KAAK,OAAO"}
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,7 @@
1
+ import { manifest } from "./manifest.js";
2
+ import { registerManifest } from "@studiometa/js-toolkit/registerManifest";
3
+ //#region src/autoload.ts
4
+ if (typeof document !== "undefined") registerManifest(manifest);
5
+ //#endregion
6
+
7
+ //# sourceMappingURL=autoload.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"autoload.js","names":[],"sources":["../src/autoload.ts"],"sourcesContent":["import { registerManifest } from '@studiometa/js-toolkit/registerManifest';\nimport { manifest } from './manifest.js';\n\n// Side-effect entry: importing this module registers the `@studiometa/ui-motion` manifest with the\n// shared js-toolkit autoload runtime and ensures the autoloader starts. There is nothing to export\n// — `import` is the contract. Importing it alongside `@studiometa/ui/autoload` coalesces into a\n// single loader over the composed set.\nif (typeof document !== 'undefined') {\n registerManifest(manifest);\n}\n"],"mappings":";;;AAOA,IAAI,OAAO,aAAa,YACtB,kBAAiB,SAAS"}
@@ -0,0 +1,7 @@
1
+ import { ComponentCatalog } from "./scripts/manifest-types.js";
2
+
3
+ //#region src/catalog.d.ts
4
+ /** The autoload catalog for every declarative `@studiometa/ui-motion` component. */
5
+ declare const catalog: ComponentCatalog;
6
+ //#endregion
7
+ export { catalog };
@@ -0,0 +1,30 @@
1
+ /** The autoload catalog for every declarative `@studiometa/ui-motion` component. */
2
+ const catalog = {
3
+ packageName: "@studiometa/ui-motion",
4
+ strategy: "visible",
5
+ components: [
6
+ {
7
+ token: "Motion",
8
+ group: "motion"
9
+ },
10
+ {
11
+ token: "MotionScrollTimeline",
12
+ group: "motion",
13
+ children: ["Motion"]
14
+ },
15
+ {
16
+ token: "MotionSequence",
17
+ group: "motion",
18
+ children: ["Motion"]
19
+ },
20
+ {
21
+ token: "MotionView",
22
+ group: "motion"
23
+ }
24
+ ],
25
+ abstractExports: []
26
+ };
27
+ //#endregion
28
+ export { catalog };
29
+
30
+ //# sourceMappingURL=catalog.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catalog.js","names":[],"sources":["../src/catalog.ts"],"sourcesContent":["import type { ComponentCatalog, CuratedComponentMetadata } from '../../../scripts/manifest-types.js';\n\n// Motion is external (import-map resolved) and never bundled, so the component\n// declares neither a CDN-served stylesheet nor a bundled integration chunk —\n// consumers load the Motion JavaScript from the source their import map points at.\nconst components: readonly CuratedComponentMetadata[] = [\n { token: 'Motion', group: 'motion' },\n { token: 'MotionScrollTimeline', group: 'motion', children: ['Motion'] },\n { token: 'MotionSequence', group: 'motion', children: ['Motion'] },\n { token: 'MotionView', group: 'motion' },\n];\n\n/** The autoload catalog for every declarative `@studiometa/ui-motion` component. */\nexport const catalog: ComponentCatalog = {\n packageName: '@studiometa/ui-motion',\n strategy: 'visible',\n components,\n abstractExports: [],\n};\n"],"mappings":";AAaA,MAAa,UAA4B;CACvC,aAAa;CACb,UAAU;CACV,YAXsD;EACtD;GAAE,OAAO;GAAU,OAAO;GAAU;EACpC;GAAE,OAAO;GAAwB,OAAO;GAAU,UAAU,CAAC,SAAS;GAAE;EACxE;GAAE,OAAO;GAAkB,OAAO;GAAU,UAAU,CAAC,SAAS;GAAE;EAClE;GAAE,OAAO;GAAc,OAAO;GAAU;EACzC;CAOC,iBAAiB,EAAE;CACpB"}
@@ -0,0 +1,61 @@
1
+ import { animate, animateView, getComputedStyle, hover, inView, press, readTransformValue, scroll, transformProps } from "motion";
2
+
3
+ //#region src/dependencies.d.ts
4
+ /**
5
+ * The subset of the `motion` module the components consume. Declared
6
+ * structurally so a host can inject the full `motion` entry, the smaller
7
+ * `motion/mini` entry or any compatible build — only the members listed here
8
+ * are ever read. Everything but `animate` is optional because `motion/mini`
9
+ * does not ship it: `animateView` powers `MotionView`, `scroll` powers
10
+ * `MotionScrollTimeline`, and `hover`, `press` and `inView` power the
11
+ * matching `Motion` gesture options.
12
+ *
13
+ * `transformProps`, `readTransformValue` and `getComputedStyle` are Motion's
14
+ * own value readers. `Motion` uses them to capture the base value a gesture
15
+ * returns to, the way Motion's `VisualElement.readValueFromInstance()` does.
16
+ * They ship alongside the gesture functions in the full entry.
17
+ */
18
+ interface MotionModule {
19
+ animate: typeof animate;
20
+ animateView?: typeof animateView;
21
+ scroll?: typeof scroll;
22
+ hover?: typeof hover;
23
+ press?: typeof press;
24
+ inView?: typeof inView;
25
+ transformProps?: typeof transformProps;
26
+ readTransformValue?: typeof readTransformValue;
27
+ getComputedStyle?: typeof getComputedStyle;
28
+ }
29
+ /**
30
+ * Inject the `motion` module the components should use.
31
+ *
32
+ * Call once, before the components mount (e.g. right before `createApp`). Once
33
+ * provided, `@studiometa/ui-motion` never imports `motion` by specifier — it
34
+ * uses this instance — so a host can supply its own build (a specific version,
35
+ * the smaller `motion/mini` entry, an import-map or CDN module).
36
+ *
37
+ * @param {MotionModule} instance The `motion` module namespace.
38
+ */
39
+ declare function provideMotion(instance: MotionModule): void;
40
+ /**
41
+ * Resolve the `motion` module: the injected instance when one was provided
42
+ * through {@link provideMotion}, otherwise a lazily loaded `import('motion')`.
43
+ *
44
+ * The result is memoized, so `motion` loads at most once and repeated calls
45
+ * are cheap. The resolved instance is also cached for {@link getMotion}.
46
+ *
47
+ * @returns {Promise<MotionModule>}
48
+ */
49
+ declare function resolveMotion(): Promise<MotionModule>;
50
+ /**
51
+ * Synchronously read the resolved `motion` module.
52
+ *
53
+ * Only valid after {@link resolveMotion} (or {@link provideMotion}) has run;
54
+ * it throws otherwise. Components read it on synchronous hot paths that are
55
+ * only ever reached once their animation — and therefore `motion` — is ready.
56
+ *
57
+ * @returns {MotionModule}
58
+ */
59
+ declare function getMotion(): MotionModule;
60
+ //#endregion
61
+ export { MotionModule, getMotion, provideMotion, resolveMotion };