@atom.io/spa 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +254 -6
- package/README.mdx +83 -1
- package/dist/astro-application.d.ts +1 -1
- package/dist/astro-application.js +2 -2
- package/dist/astro-application.js.map +1 -1
- package/dist/astro-client.d.ts +1 -1
- package/dist/astro-client.js +6 -3
- package/dist/astro-client.js.map +1 -1
- package/dist/astro-server.js +2 -1
- package/dist/astro-server.js.map +1 -1
- package/dist/astro-shared-BT45auwR.js.map +1 -1
- package/dist/{astro-shared-DkZaqX7L.d.ts → astro-shared-BkWvVZWZ.d.ts} +4 -2
- package/dist/astro-shared-BkWvVZWZ.d.ts.map +1 -0
- package/dist/astro.d.ts +1 -1
- package/dist/astro.js +1 -1
- package/dist/capabilities-BkQ2i0vV.js +63 -0
- package/dist/capabilities-BkQ2i0vV.js.map +1 -0
- package/dist/capabilities-CFP-HIuY.d.ts +115 -0
- package/dist/capabilities-CFP-HIuY.d.ts.map +1 -0
- package/dist/client.min.js +1 -1
- package/dist/{core-Dvqm7w2y.js → core-DCmqLZQP.js} +23 -4
- package/dist/core-DCmqLZQP.js.map +1 -0
- package/dist/development/astro-application.js +2 -2
- package/dist/development/astro-application.js.map +1 -1
- package/dist/development/astro-client.js +6 -3
- package/dist/development/astro-client.js.map +1 -1
- package/dist/development/astro-server.js +2 -1
- package/dist/development/astro-server.js.map +1 -1
- package/dist/development/astro-shared-BT45auwR.js.map +1 -1
- package/dist/development/astro.js +1 -1
- package/dist/development/capabilities-BkQ2i0vV.js +63 -0
- package/dist/development/capabilities-BkQ2i0vV.js.map +1 -0
- package/dist/development/{core-DB3sQz4h.js → core-p9vciX9C.js} +23 -4
- package/dist/development/core-p9vciX9C.js.map +1 -0
- package/dist/development/{dom-D9cY6l1R.js → dom-Ci1XvbVJ.js} +21 -7
- package/dist/development/dom-Ci1XvbVJ.js.map +1 -0
- package/dist/development/index.js +3 -38
- package/dist/development/jsx-runtime.js.map +1 -1
- package/dist/development/konva.js +143 -0
- package/dist/development/konva.js.map +1 -0
- package/dist/development/motion.js +601 -0
- package/dist/development/motion.js.map +1 -0
- package/dist/development/renderer.js +190 -0
- package/dist/development/renderer.js.map +1 -0
- package/dist/development/server.js +4 -2
- package/dist/development/server.js.map +1 -1
- package/dist/development/src-DG0xjuS5.js +38 -0
- package/dist/development/src-DG0xjuS5.js.map +1 -0
- package/dist/development/three.js +452 -0
- package/dist/development/three.js.map +1 -0
- package/dist/development/vite-CSHoxzXU.js +240 -0
- package/dist/development/vite-CSHoxzXU.js.map +1 -0
- package/dist/development/vite.js +1 -1
- package/dist/{dom-BR9o9wZW.js → dom-dHQM0O7Q.js} +21 -7
- package/dist/dom-dHQM0O7Q.js.map +1 -0
- package/dist/index-CJJXcKaw.d.ts +24 -0
- package/dist/index-CJJXcKaw.d.ts.map +1 -0
- package/dist/index.d.ts +4 -24
- package/dist/index.js +3 -38
- package/dist/{jsx-runtime-BzGV18VO.d.ts → jsx-runtime-CREZYKDC.d.ts} +6 -4
- package/dist/jsx-runtime-CREZYKDC.d.ts.map +1 -0
- package/dist/jsx-runtime.d.ts +2 -2
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/konva.d.ts +32 -0
- package/dist/konva.d.ts.map +1 -0
- package/dist/konva.js +143 -0
- package/dist/konva.js.map +1 -0
- package/dist/motion.d.ts +45 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +601 -0
- package/dist/motion.js.map +1 -0
- package/dist/renderer.d.ts +24 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +190 -0
- package/dist/renderer.js.map +1 -0
- package/dist/server.d.ts +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +4 -2
- package/dist/server.js.map +1 -1
- package/dist/size.json +16 -16
- package/dist/src-DCQa6qxs.js +38 -0
- package/dist/src-DCQa6qxs.js.map +1 -0
- package/dist/three.d.ts +110 -0
- package/dist/three.d.ts.map +1 -0
- package/dist/three.js +452 -0
- package/dist/three.js.map +1 -0
- package/dist/vite-CSHoxzXU.js +240 -0
- package/dist/vite-CSHoxzXU.js.map +1 -0
- package/dist/vite.js +1 -1
- package/package.json +62 -15
- package/src/astro-application.ts +2 -2
- package/src/astro-client.ts +1 -0
- package/src/astro-server.ts +1 -0
- package/src/astro-shared.ts +3 -0
- package/src/capabilities.ts +107 -0
- package/src/compiler.ts +251 -178
- package/src/core.ts +38 -2
- package/src/dom.ts +38 -4
- package/src/index.ts +5 -0
- package/src/jsx-runtime.ts +4 -0
- package/src/jsx-types.d.ts +3 -1
- package/src/konva.ts +186 -0
- package/src/motion-tween.ts +106 -0
- package/src/motion.ts +519 -0
- package/src/presence.ts +324 -0
- package/src/renderer.ts +278 -0
- package/src/server.ts +3 -0
- package/src/three-events.ts +342 -0
- package/src/three.ts +373 -0
- package/dist/astro-shared-DkZaqX7L.d.ts.map +0 -1
- package/dist/core-BhiA9fFi.d.ts +0 -44
- package/dist/core-BhiA9fFi.d.ts.map +0 -1
- package/dist/core-Dvqm7w2y.js.map +0 -1
- package/dist/development/core-DB3sQz4h.js.map +0 -1
- package/dist/development/dom-D9cY6l1R.js.map +0 -1
- package/dist/development/index.js.map +0 -1
- package/dist/development/vite-CQchIQ_-.js +0 -126
- package/dist/development/vite-CQchIQ_-.js.map +0 -1
- package/dist/dom-BR9o9wZW.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/jsx-runtime-BzGV18VO.d.ts.map +0 -1
- package/dist/vite-CQchIQ_-.js +0 -126
- package/dist/vite-CQchIQ_-.js.map +0 -1
package/dist/motion.js
ADDED
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
import { a as invalidate, i as fail, o as isPresent, p as untrack, r as dispose } from "./core-DCmqLZQP.js";
|
|
2
|
+
import { animate, cancelFrame, frame, hover, keyframes, motionValue, press, readStyleValue, styleEffect } from "motion";
|
|
3
|
+
// src/motion-tween.ts
|
|
4
|
+
const trajectories = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
const scalar = /^(-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?)([a-z%]*)$/i;
|
|
6
|
+
const brake = (progress) => progress * (2 - progress);
|
|
7
|
+
const returnToTarget = (progress) => progress * progress * (3 - 2 * progress);
|
|
8
|
+
/** Retarget a moving tween without changing its position or velocity at the join. */
|
|
9
|
+
function animateTween(value, goal, transition, interrupted) {
|
|
10
|
+
if (interrupted) value.stop();
|
|
11
|
+
const current = value.get();
|
|
12
|
+
const from = typeof current === `number` ? [
|
|
13
|
+
``,
|
|
14
|
+
current,
|
|
15
|
+
``
|
|
16
|
+
] : scalar.exec(current);
|
|
17
|
+
const to = typeof goal === `number` ? [
|
|
18
|
+
``,
|
|
19
|
+
goal,
|
|
20
|
+
``
|
|
21
|
+
] : scalar.exec(goal);
|
|
22
|
+
const previous = trajectories.get(value)?.();
|
|
23
|
+
const velocity = previous && from && Math.abs(previous.position - Number(from[1])) < .001 ? previous.velocity : value.getVelocity();
|
|
24
|
+
trajectories.delete(value);
|
|
25
|
+
const duration = transition.duration ?? .3;
|
|
26
|
+
const tween = transition.type === `tween` || transition.type === void 0 && (transition.duration !== void 0 || transition.ease !== void 0);
|
|
27
|
+
const moving = interrupted && tween && from && to && from[2] === to[2] && Number.isFinite(velocity) && velocity !== 0 && duration > 0;
|
|
28
|
+
const braking = .2;
|
|
29
|
+
const turn = from ? Number(from[1]) + velocity * duration * braking / 2 : 0;
|
|
30
|
+
const waypoint = typeof current === `number` ? turn : `${turn}${from?.[2]}`;
|
|
31
|
+
const frames = moving ? [
|
|
32
|
+
current,
|
|
33
|
+
waypoint,
|
|
34
|
+
goal
|
|
35
|
+
] : [current, goal];
|
|
36
|
+
const next = moving ? {
|
|
37
|
+
...transition,
|
|
38
|
+
type: `tween`,
|
|
39
|
+
duration,
|
|
40
|
+
delay: 0,
|
|
41
|
+
times: [
|
|
42
|
+
0,
|
|
43
|
+
braking,
|
|
44
|
+
1
|
|
45
|
+
],
|
|
46
|
+
ease: [brake, returnToTarget]
|
|
47
|
+
} : transition;
|
|
48
|
+
const controls = animate(value, frames, tween && (moving || current !== goal) ? {
|
|
49
|
+
...next,
|
|
50
|
+
isSync: true
|
|
51
|
+
} : next);
|
|
52
|
+
if (tween && (moving || current !== goal) && from && to && from[2] === to[2] && duration > 0) {
|
|
53
|
+
const curve = keyframes({
|
|
54
|
+
keyframes: moving ? [
|
|
55
|
+
Number(from[1]),
|
|
56
|
+
turn,
|
|
57
|
+
Number(to[1])
|
|
58
|
+
] : [Number(from[1]), Number(to[1])],
|
|
59
|
+
duration: duration * 1e3,
|
|
60
|
+
times: moving ? [
|
|
61
|
+
0,
|
|
62
|
+
braking,
|
|
63
|
+
1
|
|
64
|
+
] : [0, 1],
|
|
65
|
+
ease: next.ease ?? `easeOut`
|
|
66
|
+
});
|
|
67
|
+
const sample = (at) => curve.next(at * 1e3).value;
|
|
68
|
+
trajectories.set(value, () => {
|
|
69
|
+
const at = Math.max(0, Math.min(duration, controls.time));
|
|
70
|
+
const before = Math.max(0, at - 1e-4), after = Math.min(duration, at + 1e-4);
|
|
71
|
+
return {
|
|
72
|
+
position: sample(at),
|
|
73
|
+
velocity: at <= 0 || at >= duration ? 0 : (sample(after) - sample(before)) / (after - before)
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
return controls;
|
|
78
|
+
}
|
|
79
|
+
// src/presence.ts
|
|
80
|
+
/** Optional coordinator: only imported by integrations that support retention. */
|
|
81
|
+
function createPresence(root) {
|
|
82
|
+
const releases = /* @__PURE__ */ new Map();
|
|
83
|
+
const handlers = /* @__PURE__ */ new WeakMap();
|
|
84
|
+
const groups = /* @__PURE__ */ new Map();
|
|
85
|
+
const sessions = /* @__PURE__ */ new Set();
|
|
86
|
+
const active = /* @__PURE__ */ new Map();
|
|
87
|
+
const suspended = /* @__PURE__ */ new Map();
|
|
88
|
+
const suspend = (target) => {
|
|
89
|
+
if (target.kind !== `dom`) return target.suspend();
|
|
90
|
+
const node = target.node;
|
|
91
|
+
const inert = node.getAttribute(`inert`), hidden = node.getAttribute(`aria-hidden`);
|
|
92
|
+
const pointer = node.style.getPropertyValue(`pointer-events`), priority = node.style.getPropertyPriority(`pointer-events`);
|
|
93
|
+
const focused = node.ownerDocument.activeElement;
|
|
94
|
+
if (focused && node.contains(focused)) focused.blur();
|
|
95
|
+
node.setAttribute(`inert`, ``);
|
|
96
|
+
node.setAttribute(`aria-hidden`, `true`);
|
|
97
|
+
node.style.setProperty(`pointer-events`, `none`, `important`);
|
|
98
|
+
return () => {
|
|
99
|
+
for (const [key, value] of [[`inert`, inert], [`aria-hidden`, hidden]]) if (value === null) node.removeAttribute(key);
|
|
100
|
+
else node.setAttribute(key, value);
|
|
101
|
+
if (pointer) node.style.setProperty(`pointer-events`, pointer, priority);
|
|
102
|
+
else node.style.removeProperty(`pointer-events`);
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
const walk = (scope, visit) => {
|
|
106
|
+
const descend = (at) => {
|
|
107
|
+
visit(at);
|
|
108
|
+
for (const child of at._cleanups) if (typeof child !== `function`) descend(child);
|
|
109
|
+
};
|
|
110
|
+
descend(scope);
|
|
111
|
+
};
|
|
112
|
+
const ancestor = (scope, parent) => {
|
|
113
|
+
for (let at = scope; at; at = at._parent) if (at === parent) return true;
|
|
114
|
+
return false;
|
|
115
|
+
};
|
|
116
|
+
const report = (error) => {
|
|
117
|
+
if (root._dead) return;
|
|
118
|
+
try {
|
|
119
|
+
fail(root, error);
|
|
120
|
+
} catch (failure) {
|
|
121
|
+
root.error(failure);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
const finish = () => {
|
|
125
|
+
for (const group of [...groups.values()]) {
|
|
126
|
+
if (group.scope._dead || group.sessions.some((session) => !session.done)) continue;
|
|
127
|
+
if ([...groups.keys()].some((scope) => scope !== group.scope && ancestor(group.scope, scope))) continue;
|
|
128
|
+
groups.delete(group.scope);
|
|
129
|
+
try {
|
|
130
|
+
group.done();
|
|
131
|
+
} catch (error) {
|
|
132
|
+
report(error);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
const cancel = (session) => {
|
|
137
|
+
session.cancelled = true;
|
|
138
|
+
sessions.delete(session);
|
|
139
|
+
active.delete(session.participant);
|
|
140
|
+
session.handle.cancel();
|
|
141
|
+
};
|
|
142
|
+
const coordinator = {
|
|
143
|
+
target(scope, target) {
|
|
144
|
+
(scope._targets ??= /* @__PURE__ */ new Set()).add(target);
|
|
145
|
+
scope._cleanups.add(() => {
|
|
146
|
+
scope._targets.delete(target);
|
|
147
|
+
});
|
|
148
|
+
},
|
|
149
|
+
bind(scope, binding, props, namespace) {
|
|
150
|
+
const read = () => Object.fromEntries(Object.entries(props()).filter(([key]) => key.startsWith(`${namespace}:`)).map(([key, value]) => [key.slice(namespace.length + 1), value]));
|
|
151
|
+
if (!binding.exit) return;
|
|
152
|
+
const participant = {
|
|
153
|
+
exit: () => binding.exit(untrack(read)),
|
|
154
|
+
resume() {
|
|
155
|
+
binding.update(untrack(read));
|
|
156
|
+
binding.resume?.();
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
(scope._exits ??= /* @__PURE__ */ new Set()).add(participant);
|
|
160
|
+
const release = () => {
|
|
161
|
+
scope._exits.delete(participant);
|
|
162
|
+
scope._cleanups.delete(release);
|
|
163
|
+
releases.delete(binding);
|
|
164
|
+
};
|
|
165
|
+
releases.set(binding, release);
|
|
166
|
+
scope._cleanups.add(release);
|
|
167
|
+
},
|
|
168
|
+
unbind(binding) {
|
|
169
|
+
releases.get(binding)?.();
|
|
170
|
+
},
|
|
171
|
+
event(scope, handler) {
|
|
172
|
+
const cached = handlers.get(scope) ?? /* @__PURE__ */ new WeakMap();
|
|
173
|
+
handlers.set(scope, cached);
|
|
174
|
+
if (!cached.has(handler)) cached.set(handler, function(...args) {
|
|
175
|
+
if (isPresent(scope)) return handler.apply(this, args);
|
|
176
|
+
});
|
|
177
|
+
return cached.get(handler);
|
|
178
|
+
},
|
|
179
|
+
collection(scopeOf, compatible) {
|
|
180
|
+
let order = [];
|
|
181
|
+
const exiting = /* @__PURE__ */ new Map();
|
|
182
|
+
return {
|
|
183
|
+
take(key, value) {
|
|
184
|
+
if (value == null || typeof value === `boolean`) return void 0;
|
|
185
|
+
for (const [record, state] of exiting) if (state.eligible && state.key === key) {
|
|
186
|
+
if (!compatible(record, value)) {
|
|
187
|
+
state.eligible = false;
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
exiting.delete(record);
|
|
191
|
+
return record;
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
remove(record, key, eligible, index, done) {
|
|
195
|
+
const position = order.indexOf(record);
|
|
196
|
+
const state = {
|
|
197
|
+
key,
|
|
198
|
+
eligible,
|
|
199
|
+
index: position < 0 ? index : position
|
|
200
|
+
};
|
|
201
|
+
exiting.set(record, state);
|
|
202
|
+
let pending = false;
|
|
203
|
+
const release = () => {
|
|
204
|
+
dispose(scopeOf(record));
|
|
205
|
+
exiting.delete(record);
|
|
206
|
+
order = order.filter((item) => item !== record);
|
|
207
|
+
for (const other of exiting.values()) if (other.index > state.index) other.index--;
|
|
208
|
+
if (pending) done();
|
|
209
|
+
};
|
|
210
|
+
pending = coordinator.remove(scopeOf(record), release);
|
|
211
|
+
if (!pending) release();
|
|
212
|
+
},
|
|
213
|
+
merge(records) {
|
|
214
|
+
const rendered = [...records];
|
|
215
|
+
for (const [record, state] of [...exiting].sort((a, b) => a[1].index - b[1].index)) {
|
|
216
|
+
rendered.splice(Math.min(state.index, rendered.length), 0, record);
|
|
217
|
+
state.index = rendered.indexOf(record);
|
|
218
|
+
}
|
|
219
|
+
order = [...rendered];
|
|
220
|
+
return rendered;
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
},
|
|
224
|
+
remove(scope, done) {
|
|
225
|
+
const collected = [];
|
|
226
|
+
walk(scope, (at) => {
|
|
227
|
+
for (const participant of at._exits ?? []) {
|
|
228
|
+
let session = active.get(participant);
|
|
229
|
+
if (!session) {
|
|
230
|
+
const handle = untrack(() => participant.exit());
|
|
231
|
+
if (!handle) continue;
|
|
232
|
+
session = {
|
|
233
|
+
scope: at,
|
|
234
|
+
participant,
|
|
235
|
+
handle,
|
|
236
|
+
done: false,
|
|
237
|
+
cancelled: false
|
|
238
|
+
};
|
|
239
|
+
sessions.add(session);
|
|
240
|
+
active.set(participant, session);
|
|
241
|
+
const held = session;
|
|
242
|
+
handle.finished.then(() => {
|
|
243
|
+
if (held.cancelled || root._dead) return;
|
|
244
|
+
held.done = true;
|
|
245
|
+
finish();
|
|
246
|
+
}, (error) => {
|
|
247
|
+
if (!held.cancelled) report(error);
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
collected.push(session);
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
if (!collected.some((session) => !session.done)) return false;
|
|
254
|
+
scope._exiting = true;
|
|
255
|
+
groups.set(scope, {
|
|
256
|
+
scope,
|
|
257
|
+
sessions: collected,
|
|
258
|
+
done
|
|
259
|
+
});
|
|
260
|
+
walk(scope, (at) => {
|
|
261
|
+
for (const target of at._targets ?? []) if (!suspended.has(target) && (target.kind === `dom` || target.suspend)) suspended.set(target, {
|
|
262
|
+
scope: at,
|
|
263
|
+
restore: suspend(target)
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
return true;
|
|
267
|
+
},
|
|
268
|
+
resume(scope) {
|
|
269
|
+
if (!groups.delete(scope)) return;
|
|
270
|
+
scope._exiting = false;
|
|
271
|
+
for (const session of [...sessions]) if (ancestor(session.scope, scope) && isPresent(session.scope)) cancel(session);
|
|
272
|
+
walk(scope, (at) => {
|
|
273
|
+
if (isPresent(at)) for (const participant of at._exits ?? []) participant.resume();
|
|
274
|
+
});
|
|
275
|
+
for (const [target, state] of suspended) if (isPresent(state.scope)) {
|
|
276
|
+
suspended.delete(target);
|
|
277
|
+
state.restore();
|
|
278
|
+
}
|
|
279
|
+
invalidate(scope);
|
|
280
|
+
queueMicrotask(finish);
|
|
281
|
+
},
|
|
282
|
+
forget(scope) {
|
|
283
|
+
const errors = [];
|
|
284
|
+
for (const session of [...sessions]) if (ancestor(session.scope, scope)) try {
|
|
285
|
+
cancel(session);
|
|
286
|
+
} catch (error) {
|
|
287
|
+
errors.push(error);
|
|
288
|
+
}
|
|
289
|
+
for (const at of groups.keys()) if (ancestor(at, scope)) groups.delete(at);
|
|
290
|
+
for (const [target, state] of suspended) if (ancestor(state.scope, scope)) {
|
|
291
|
+
suspended.delete(target);
|
|
292
|
+
try {
|
|
293
|
+
state.restore();
|
|
294
|
+
} catch (error) {
|
|
295
|
+
errors.push(error);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (errors.length) throw new AggregateError(errors);
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
return coordinator;
|
|
302
|
+
}
|
|
303
|
+
// src/motion.ts
|
|
304
|
+
const transform = /^(x|y|z|translate.*|scale.*|rotate.*|skew.*|transformPerspective)$/;
|
|
305
|
+
const allowed = /* @__PURE__ */ new Set([
|
|
306
|
+
`initial`,
|
|
307
|
+
`animate`,
|
|
308
|
+
`exit`,
|
|
309
|
+
`transition`,
|
|
310
|
+
`whileHover`,
|
|
311
|
+
`whileTap`
|
|
312
|
+
]);
|
|
313
|
+
function equal(a, b) {
|
|
314
|
+
return Object.is(a, b) || Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((value, index) => Object.is(value, b[index]));
|
|
315
|
+
}
|
|
316
|
+
function attach(target, options) {
|
|
317
|
+
if (target.kind !== `dom` && (!target.read || !target.write)) throw new Error(`spa/motion: renderer ${target.kind} does not support motion`);
|
|
318
|
+
const element = target.kind === `dom` ? target.node : void 0;
|
|
319
|
+
const properties = /* @__PURE__ */ new Map();
|
|
320
|
+
const originalStyles = /* @__PURE__ */ new Map();
|
|
321
|
+
let styles;
|
|
322
|
+
let props = {};
|
|
323
|
+
let hovered = false;
|
|
324
|
+
let tapped = false;
|
|
325
|
+
let started = false;
|
|
326
|
+
let dead = false;
|
|
327
|
+
let exiting = false;
|
|
328
|
+
let exitHandle;
|
|
329
|
+
let gestures = [];
|
|
330
|
+
const preference = options.reducedMotion !== `never` && typeof matchMedia === `function` ? matchMedia(`(prefers-reduced-motion: reduce)`) : void 0;
|
|
331
|
+
const reduced = () => options.reducedMotion === `always` || options.reducedMotion !== `never` && preference?.matches;
|
|
332
|
+
const read = (key, fallback) => {
|
|
333
|
+
const value = element ? readStyleValue(element, key) : target.read(key);
|
|
334
|
+
return value === void 0 || value === `` ? typeof fallback === `number` ? key.startsWith(`scale`) || key === `opacity` ? 1 : 0 : fallback : value;
|
|
335
|
+
};
|
|
336
|
+
const bindStyles = () => {
|
|
337
|
+
styles?.();
|
|
338
|
+
if (element) styles = styleEffect([element], Object.fromEntries([...properties].map(([key, property]) => [key, property.values[0]])));
|
|
339
|
+
};
|
|
340
|
+
const ensure = (key, goal, initial) => {
|
|
341
|
+
const existing = properties.get(key);
|
|
342
|
+
if (existing) {
|
|
343
|
+
if (existing.values.length !== (Array.isArray(goal) ? goal.length : 1)) throw new Error(`spa/motion: cannot change the shape of ${key}`);
|
|
344
|
+
return existing;
|
|
345
|
+
}
|
|
346
|
+
const base = read(key, goal);
|
|
347
|
+
const rest = !Array.isArray(goal) && Array.isArray(base) ? base[0] : base;
|
|
348
|
+
let start = initial === false ? goal : initial[key] ?? rest;
|
|
349
|
+
if (!Array.isArray(goal) && Array.isArray(start)) start = start[0];
|
|
350
|
+
if (Array.isArray(goal) && (!Array.isArray(start) || start.length !== goal.length)) throw new Error(`spa/motion: ${key} requires matching numeric tuples`);
|
|
351
|
+
const values = (Array.isArray(start) ? start : [start]).map((value) => motionValue(value));
|
|
352
|
+
const unsubscribers = [];
|
|
353
|
+
if (element) {
|
|
354
|
+
if (Array.isArray(goal)) throw new Error(`spa/motion: DOM targets must be scalar styles or transforms`);
|
|
355
|
+
const cssKey = transform.test(key) ? `transform` : key.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
356
|
+
if (!originalStyles.has(cssKey)) originalStyles.set(cssKey, {
|
|
357
|
+
value: element.style.getPropertyValue(cssKey),
|
|
358
|
+
priority: element.style.getPropertyPriority(cssKey)
|
|
359
|
+
});
|
|
360
|
+
} else {
|
|
361
|
+
const write = () => {
|
|
362
|
+
target.write(key, Array.isArray(goal) ? values.map((value) => value.get()) : values[0].get());
|
|
363
|
+
};
|
|
364
|
+
for (const value of values) unsubscribers.push(value.on(`change`, write));
|
|
365
|
+
write();
|
|
366
|
+
}
|
|
367
|
+
const property = {
|
|
368
|
+
values,
|
|
369
|
+
base,
|
|
370
|
+
rest,
|
|
371
|
+
target: start,
|
|
372
|
+
release() {
|
|
373
|
+
for (const release of unsubscribers) release();
|
|
374
|
+
for (const value of values) value.destroy();
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
properties.set(key, property);
|
|
378
|
+
return property;
|
|
379
|
+
};
|
|
380
|
+
const refresh = (force = false) => {
|
|
381
|
+
if (dead || exiting) return;
|
|
382
|
+
const initial = props[`initial`] ?? {};
|
|
383
|
+
const animateTo = props[`animate`] ?? {};
|
|
384
|
+
const hoverTo = props[`whileHover`] ?? {};
|
|
385
|
+
const tapTo = props[`whileTap`] ?? {};
|
|
386
|
+
const goals = {
|
|
387
|
+
...animateTo,
|
|
388
|
+
...hovered ? hoverTo : {},
|
|
389
|
+
...tapped ? tapTo : {}
|
|
390
|
+
};
|
|
391
|
+
const keys = /* @__PURE__ */ new Set([
|
|
392
|
+
...Object.keys(initial || {}),
|
|
393
|
+
...Object.keys(animateTo),
|
|
394
|
+
...Object.keys(hoverTo),
|
|
395
|
+
...Object.keys(tapTo)
|
|
396
|
+
]);
|
|
397
|
+
let added = false;
|
|
398
|
+
for (const key of keys) {
|
|
399
|
+
const sample = goals[key] ?? (initial === false ? void 0 : initial[key]) ?? hoverTo[key] ?? tapTo[key];
|
|
400
|
+
if (sample === void 0) continue;
|
|
401
|
+
const had = properties.has(key);
|
|
402
|
+
ensure(key, sample, started ? {} : initial);
|
|
403
|
+
if (!had) added = true;
|
|
404
|
+
}
|
|
405
|
+
if (added) bindStyles();
|
|
406
|
+
const transition = props[`transition`] ?? { type: `spring` };
|
|
407
|
+
for (const [key, property] of properties) {
|
|
408
|
+
const goal = goals[key] ?? (initial === false ? void 0 : initial[key]) ?? property.rest;
|
|
409
|
+
if (!force && equal(property.target, goal)) continue;
|
|
410
|
+
property.target = Array.isArray(goal) ? [...goal] : goal;
|
|
411
|
+
const next = Array.isArray(goal) ? goal : [goal];
|
|
412
|
+
if (next.length !== property.values.length) throw new Error(`spa/motion: cannot change the shape of ${key}`);
|
|
413
|
+
property.values.forEach((value, index) => {
|
|
414
|
+
if (reduced() || !started && initial === false) value.jump(next[index]);
|
|
415
|
+
else animateTween(value, next[index], transition, force || value.isAnimating());
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
started = true;
|
|
419
|
+
};
|
|
420
|
+
const clearGestures = () => {
|
|
421
|
+
for (const release of gestures) release();
|
|
422
|
+
gestures = [];
|
|
423
|
+
};
|
|
424
|
+
const installGestures = () => {
|
|
425
|
+
clearGestures();
|
|
426
|
+
if (element) {
|
|
427
|
+
if (props[`whileHover`]) gestures.push(hover([element], () => {
|
|
428
|
+
hovered = true;
|
|
429
|
+
refresh();
|
|
430
|
+
return () => {
|
|
431
|
+
hovered = false;
|
|
432
|
+
refresh();
|
|
433
|
+
};
|
|
434
|
+
}));
|
|
435
|
+
if (props[`whileTap`]) gestures.push(press([element], () => {
|
|
436
|
+
tapped = true;
|
|
437
|
+
refresh();
|
|
438
|
+
return () => {
|
|
439
|
+
tapped = false;
|
|
440
|
+
refresh();
|
|
441
|
+
};
|
|
442
|
+
}));
|
|
443
|
+
} else if (props[`whileHover`] || props[`whileTap`]) {
|
|
444
|
+
if (!target.listen) throw new Error(`spa/motion: ${target.kind} gestures require a renderer event adapter`);
|
|
445
|
+
const listen = (event, run) => gestures.push(target.listen(event, run));
|
|
446
|
+
listen(`pointerenter`, () => {
|
|
447
|
+
hovered = true;
|
|
448
|
+
refresh();
|
|
449
|
+
});
|
|
450
|
+
listen(`pointerleave`, () => {
|
|
451
|
+
hovered = false;
|
|
452
|
+
tapped = false;
|
|
453
|
+
refresh();
|
|
454
|
+
});
|
|
455
|
+
listen(`pointerdown`, () => {
|
|
456
|
+
tapped = true;
|
|
457
|
+
refresh();
|
|
458
|
+
});
|
|
459
|
+
listen(`pointerup`, () => {
|
|
460
|
+
tapped = false;
|
|
461
|
+
refresh();
|
|
462
|
+
});
|
|
463
|
+
listen(`pointercancel`, () => {
|
|
464
|
+
tapped = false;
|
|
465
|
+
refresh();
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
const preferenceChanged = () => {
|
|
470
|
+
if (reduced()) for (const property of properties.values()) {
|
|
471
|
+
const goal = Array.isArray(property.target) ? property.target : [property.target];
|
|
472
|
+
property.values.forEach((value, i) => {
|
|
473
|
+
value.jump(goal[i]);
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
const finishExit = () => {
|
|
478
|
+
exitHandle?.cancel();
|
|
479
|
+
};
|
|
480
|
+
const mediaChanged = () => {
|
|
481
|
+
preferenceChanged();
|
|
482
|
+
if (reduced() && exiting) finishExit();
|
|
483
|
+
};
|
|
484
|
+
preference?.addEventListener(`change`, mediaChanged);
|
|
485
|
+
return {
|
|
486
|
+
exit(next) {
|
|
487
|
+
const goals = next[`exit`];
|
|
488
|
+
exiting = true;
|
|
489
|
+
hovered = tapped = false;
|
|
490
|
+
clearGestures();
|
|
491
|
+
const moving = /* @__PURE__ */ new Set();
|
|
492
|
+
for (const property of properties.values()) for (const value of property.values) {
|
|
493
|
+
if (value.isAnimating()) moving.add(value);
|
|
494
|
+
value.stop();
|
|
495
|
+
}
|
|
496
|
+
if (!goals || !Object.keys(goals).length || reduced()) return void 0;
|
|
497
|
+
const transition = next[`transition`] ?? { type: `spring` };
|
|
498
|
+
for (const key of [
|
|
499
|
+
`duration`,
|
|
500
|
+
`delay`,
|
|
501
|
+
`visualDuration`,
|
|
502
|
+
`velocity`,
|
|
503
|
+
`bounce`,
|
|
504
|
+
`stiffness`,
|
|
505
|
+
`damping`,
|
|
506
|
+
`mass`,
|
|
507
|
+
`restDelta`,
|
|
508
|
+
`restSpeed`
|
|
509
|
+
]) {
|
|
510
|
+
const value = transition[key];
|
|
511
|
+
if (value !== void 0 && (!Number.isFinite(value) || key !== `velocity` && value < 0 || key === `bounce` && value >= 1 || [
|
|
512
|
+
`stiffness`,
|
|
513
|
+
`damping`,
|
|
514
|
+
`mass`,
|
|
515
|
+
`restDelta`,
|
|
516
|
+
`restSpeed`
|
|
517
|
+
].includes(key) && value === 0)) throw new Error(`spa/motion: exit requires a finite settling transition`);
|
|
518
|
+
}
|
|
519
|
+
let added = false;
|
|
520
|
+
for (const [key, goal] of Object.entries(goals)) {
|
|
521
|
+
const had = properties.has(key);
|
|
522
|
+
ensure(key, goal, {});
|
|
523
|
+
if (!had) added = true;
|
|
524
|
+
}
|
|
525
|
+
if (added) bindStyles();
|
|
526
|
+
let settle;
|
|
527
|
+
let reject;
|
|
528
|
+
const finished = new Promise((resolve, fail) => {
|
|
529
|
+
settle = resolve;
|
|
530
|
+
reject = fail;
|
|
531
|
+
});
|
|
532
|
+
const controls = [];
|
|
533
|
+
for (const [key, goal] of Object.entries(goals)) {
|
|
534
|
+
const property = properties.get(key);
|
|
535
|
+
property.target = Array.isArray(goal) ? [...goal] : goal;
|
|
536
|
+
const values = Array.isArray(goal) ? goal : [goal];
|
|
537
|
+
property.values.forEach((value, index) => controls.push(animateTween(value, values[index], transition, moving.has(value))));
|
|
538
|
+
}
|
|
539
|
+
Promise.all(controls.map((control) => Promise.resolve(control))).then(settle, reject);
|
|
540
|
+
exitHandle = {
|
|
541
|
+
finished,
|
|
542
|
+
cancel() {
|
|
543
|
+
for (const control of controls) control.stop();
|
|
544
|
+
settle();
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
return exitHandle;
|
|
548
|
+
},
|
|
549
|
+
resume() {
|
|
550
|
+
if (dead) return;
|
|
551
|
+
exitHandle?.cancel();
|
|
552
|
+
exitHandle = void 0;
|
|
553
|
+
exiting = false;
|
|
554
|
+
installGestures();
|
|
555
|
+
refresh(true);
|
|
556
|
+
},
|
|
557
|
+
update(next) {
|
|
558
|
+
if (dead) return;
|
|
559
|
+
for (const key of Object.keys(next)) if (!allowed.has(key)) throw new Error(`spa/motion: unknown property motion:${key}`);
|
|
560
|
+
const gestureChange = !started || Boolean(next[`whileHover`]) !== Boolean(props[`whileHover`]) || Boolean(next[`whileTap`]) !== Boolean(props[`whileTap`]);
|
|
561
|
+
props = next;
|
|
562
|
+
if (gestureChange && !exiting) installGestures();
|
|
563
|
+
refresh();
|
|
564
|
+
},
|
|
565
|
+
dispose() {
|
|
566
|
+
if (dead) return;
|
|
567
|
+
dead = true;
|
|
568
|
+
clearGestures();
|
|
569
|
+
exitHandle?.cancel();
|
|
570
|
+
preference?.removeEventListener(`change`, mediaChanged);
|
|
571
|
+
styles?.();
|
|
572
|
+
for (const [key, property] of properties) {
|
|
573
|
+
property.release();
|
|
574
|
+
if (!element) target.write(key, property.base);
|
|
575
|
+
}
|
|
576
|
+
for (const [key, value] of originalStyles) if (value.value) element.style.setProperty(key, value.value, value.priority);
|
|
577
|
+
else element.style.removeProperty(key);
|
|
578
|
+
properties.clear();
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
/** Explicit, root-scoped motion:* integration. Importing it never registers globally. */
|
|
583
|
+
function motionCapability(options = {}) {
|
|
584
|
+
return {
|
|
585
|
+
namespace: `motion`,
|
|
586
|
+
presence: createPresence,
|
|
587
|
+
attach: (target) => attach(target, options),
|
|
588
|
+
scheduleFrame(callback) {
|
|
589
|
+
const run = ({ timestamp }) => {
|
|
590
|
+
callback(timestamp);
|
|
591
|
+
};
|
|
592
|
+
frame.render(run, true);
|
|
593
|
+
return () => {
|
|
594
|
+
cancelFrame(run);
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
export { motionCapability };
|
|
600
|
+
|
|
601
|
+
//# sourceMappingURL=motion.js.map
|