@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/src/motion.ts
ADDED
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
import type { animate, MotionValue, ValueAnimationTransition } from "motion"
|
|
2
|
+
import {
|
|
3
|
+
cancelFrame,
|
|
4
|
+
frame,
|
|
5
|
+
hover,
|
|
6
|
+
motionValue,
|
|
7
|
+
press,
|
|
8
|
+
readStyleValue,
|
|
9
|
+
styleEffect,
|
|
10
|
+
} from "motion"
|
|
11
|
+
|
|
12
|
+
import type {
|
|
13
|
+
Capability,
|
|
14
|
+
CapabilityBinding,
|
|
15
|
+
CapabilityTarget,
|
|
16
|
+
} from "./capabilities.ts"
|
|
17
|
+
import { animateTween } from "./motion-tween.ts"
|
|
18
|
+
import type { ExitHandle } from "./presence.ts"
|
|
19
|
+
import { createPresence } from "./presence.ts"
|
|
20
|
+
|
|
21
|
+
export type MotionScalar = number | string
|
|
22
|
+
export type MotionDOMTarget = Partial<
|
|
23
|
+
Record<
|
|
24
|
+
| keyof CSSStyleDeclaration
|
|
25
|
+
| `x`
|
|
26
|
+
| `y`
|
|
27
|
+
| `z`
|
|
28
|
+
| `scale`
|
|
29
|
+
| `scaleX`
|
|
30
|
+
| `scaleY`
|
|
31
|
+
| `rotate`
|
|
32
|
+
| `rotateX`
|
|
33
|
+
| `rotateY`
|
|
34
|
+
| `rotateZ`
|
|
35
|
+
| `skewX`
|
|
36
|
+
| `skewY`
|
|
37
|
+
| `--${string}`,
|
|
38
|
+
MotionScalar
|
|
39
|
+
>
|
|
40
|
+
>
|
|
41
|
+
export interface MotionThreeTarget {
|
|
42
|
+
position?: readonly [number, number, number]
|
|
43
|
+
rotation?: readonly [number, number, number]
|
|
44
|
+
scale?: number | readonly [number, number, number]
|
|
45
|
+
opacity?: number
|
|
46
|
+
intensity?: number
|
|
47
|
+
color?: string
|
|
48
|
+
}
|
|
49
|
+
export type MotionTarget<Kind extends string> = Kind extends `dom`
|
|
50
|
+
? MotionDOMTarget
|
|
51
|
+
: Kind extends `three`
|
|
52
|
+
? MotionThreeTarget
|
|
53
|
+
: Record<string, MotionScalar>
|
|
54
|
+
/** Supported transition options; keep public declarations independent of optional peer types. */
|
|
55
|
+
export interface MotionTransition {
|
|
56
|
+
type?: `spring` | `tween`
|
|
57
|
+
stiffness?: number
|
|
58
|
+
damping?: number
|
|
59
|
+
mass?: number
|
|
60
|
+
velocity?: number
|
|
61
|
+
restSpeed?: number
|
|
62
|
+
restDelta?: number
|
|
63
|
+
bounce?: number
|
|
64
|
+
visualDuration?: number
|
|
65
|
+
duration?: number
|
|
66
|
+
delay?: number
|
|
67
|
+
ease?:
|
|
68
|
+
| `linear`
|
|
69
|
+
| `easeIn`
|
|
70
|
+
| `easeOut`
|
|
71
|
+
| `easeInOut`
|
|
72
|
+
| `circIn`
|
|
73
|
+
| `circOut`
|
|
74
|
+
| `circInOut`
|
|
75
|
+
| `backIn`
|
|
76
|
+
| `backOut`
|
|
77
|
+
| `backInOut`
|
|
78
|
+
| readonly [number, number, number, number]
|
|
79
|
+
}
|
|
80
|
+
export interface MotionAttributes<Kind extends string = `dom`> {
|
|
81
|
+
[`motion:initial`]?: MotionTarget<Kind> | false
|
|
82
|
+
[`motion:animate`]?: MotionTarget<Kind>
|
|
83
|
+
[`motion:exit`]?: MotionTarget<Kind>
|
|
84
|
+
[`motion:transition`]?: MotionTransition
|
|
85
|
+
[`motion:whileHover`]?: MotionTarget<Kind>
|
|
86
|
+
[`motion:whileTap`]?: MotionTarget<Kind>
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// eslint-disable-next-line quotes -- TypeScript module augmentation requires a string literal.
|
|
90
|
+
declare module "@atom.io/spa/jsx-runtime" {
|
|
91
|
+
// oxlint-disable-next-line typescript/no-empty-interface -- Declaration merging adds optional motion attributes.
|
|
92
|
+
interface CapabilityAttributes<
|
|
93
|
+
Kind extends string,
|
|
94
|
+
> extends MotionAttributes<Kind> {}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface MotionCapabilityOptions {
|
|
98
|
+
/** Respect the user's reduced-motion preference by default. */
|
|
99
|
+
reducedMotion?: `user` | `always` | `never`
|
|
100
|
+
}
|
|
101
|
+
type TargetValues = Record<string, MotionScalar | readonly number[]>
|
|
102
|
+
interface AnimatedProperty {
|
|
103
|
+
values: MotionValue<MotionScalar>[]
|
|
104
|
+
base: MotionScalar | readonly number[]
|
|
105
|
+
rest: MotionScalar | readonly number[]
|
|
106
|
+
target: MotionScalar | readonly number[]
|
|
107
|
+
release(): void
|
|
108
|
+
}
|
|
109
|
+
const transform =
|
|
110
|
+
/^(x|y|z|translate.*|scale.*|rotate.*|skew.*|transformPerspective)$/
|
|
111
|
+
const allowed = new Set([
|
|
112
|
+
`initial`,
|
|
113
|
+
`animate`,
|
|
114
|
+
`exit`,
|
|
115
|
+
`transition`,
|
|
116
|
+
`whileHover`,
|
|
117
|
+
`whileTap`,
|
|
118
|
+
])
|
|
119
|
+
function equal(a: unknown, b: unknown): boolean {
|
|
120
|
+
return (
|
|
121
|
+
Object.is(a, b) ||
|
|
122
|
+
(Array.isArray(a) &&
|
|
123
|
+
Array.isArray(b) &&
|
|
124
|
+
a.length === b.length &&
|
|
125
|
+
a.every((value, index) => Object.is(value, b[index])))
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function attach(
|
|
130
|
+
target: CapabilityTarget,
|
|
131
|
+
options: MotionCapabilityOptions,
|
|
132
|
+
): CapabilityBinding {
|
|
133
|
+
if (target.kind !== `dom` && (!target.read || !target.write))
|
|
134
|
+
throw new Error(
|
|
135
|
+
`spa/motion: renderer ${target.kind} does not support motion`,
|
|
136
|
+
)
|
|
137
|
+
const element =
|
|
138
|
+
target.kind === `dom` ? (target.node as HTMLElement | SVGElement) : undefined
|
|
139
|
+
const properties = new Map<string, AnimatedProperty>()
|
|
140
|
+
const originalStyles = new Map<string, { value: string; priority: string }>()
|
|
141
|
+
let styles: (() => void) | undefined
|
|
142
|
+
let props: Record<string, unknown> = {}
|
|
143
|
+
let hovered = false
|
|
144
|
+
let tapped = false
|
|
145
|
+
let started = false
|
|
146
|
+
let dead = false
|
|
147
|
+
let exiting = false
|
|
148
|
+
let exitHandle: ExitHandle | undefined
|
|
149
|
+
let gestures: (() => void)[] = []
|
|
150
|
+
const preference =
|
|
151
|
+
options.reducedMotion !== `never` && typeof matchMedia === `function`
|
|
152
|
+
? matchMedia(`(prefers-reduced-motion: reduce)`)
|
|
153
|
+
: undefined
|
|
154
|
+
const reduced = () =>
|
|
155
|
+
options.reducedMotion === `always` ||
|
|
156
|
+
(options.reducedMotion !== `never` && preference?.matches)
|
|
157
|
+
const read = (key: string, fallback: unknown): any => {
|
|
158
|
+
const value = element ? readStyleValue(element, key) : target.read!(key)
|
|
159
|
+
return value === undefined || value === ``
|
|
160
|
+
? typeof fallback === `number`
|
|
161
|
+
? key.startsWith(`scale`) || key === `opacity`
|
|
162
|
+
? 1
|
|
163
|
+
: 0
|
|
164
|
+
: fallback
|
|
165
|
+
: value
|
|
166
|
+
}
|
|
167
|
+
const bindStyles = () => {
|
|
168
|
+
styles?.()
|
|
169
|
+
if (element)
|
|
170
|
+
styles = styleEffect(
|
|
171
|
+
[element],
|
|
172
|
+
Object.fromEntries(
|
|
173
|
+
[...properties].map(([key, property]) => [key, property.values[0]]),
|
|
174
|
+
) as any,
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
const ensure = (
|
|
178
|
+
key: string,
|
|
179
|
+
goal: MotionScalar | readonly number[],
|
|
180
|
+
initial: TargetValues | false,
|
|
181
|
+
): AnimatedProperty => {
|
|
182
|
+
const existing = properties.get(key)
|
|
183
|
+
if (existing) {
|
|
184
|
+
if (existing.values.length !== (Array.isArray(goal) ? goal.length : 1))
|
|
185
|
+
throw new Error(`spa/motion: cannot change the shape of ${key}`)
|
|
186
|
+
return existing
|
|
187
|
+
}
|
|
188
|
+
const base = read(key, goal) as MotionScalar | readonly number[]
|
|
189
|
+
const rest = !Array.isArray(goal) && Array.isArray(base) ? base[0]! : base
|
|
190
|
+
let start = initial === false ? goal : (initial[key] ?? rest)
|
|
191
|
+
if (!Array.isArray(goal) && Array.isArray(start)) start = start[0]!
|
|
192
|
+
if (
|
|
193
|
+
Array.isArray(goal) &&
|
|
194
|
+
(!Array.isArray(start) || start.length !== goal.length)
|
|
195
|
+
)
|
|
196
|
+
throw new Error(`spa/motion: ${key} requires matching numeric tuples`)
|
|
197
|
+
const values = (Array.isArray(start) ? start : [start]).map((value) =>
|
|
198
|
+
motionValue(value),
|
|
199
|
+
) as MotionValue<MotionScalar>[]
|
|
200
|
+
const unsubscribers: (() => void)[] = []
|
|
201
|
+
if (element) {
|
|
202
|
+
if (Array.isArray(goal))
|
|
203
|
+
throw new Error(
|
|
204
|
+
`spa/motion: DOM targets must be scalar styles or transforms`,
|
|
205
|
+
)
|
|
206
|
+
const cssKey = transform.test(key)
|
|
207
|
+
? `transform`
|
|
208
|
+
: key.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)
|
|
209
|
+
if (!originalStyles.has(cssKey))
|
|
210
|
+
originalStyles.set(cssKey, {
|
|
211
|
+
value: element.style.getPropertyValue(cssKey),
|
|
212
|
+
priority: element.style.getPropertyPriority(cssKey),
|
|
213
|
+
})
|
|
214
|
+
} else {
|
|
215
|
+
const write = () => {
|
|
216
|
+
target.write!(
|
|
217
|
+
key,
|
|
218
|
+
Array.isArray(goal)
|
|
219
|
+
? values.map((value) => value.get())
|
|
220
|
+
: values[0].get(),
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
for (const value of values) unsubscribers.push(value.on(`change`, write))
|
|
224
|
+
write()
|
|
225
|
+
}
|
|
226
|
+
const property: AnimatedProperty = {
|
|
227
|
+
values,
|
|
228
|
+
base,
|
|
229
|
+
rest,
|
|
230
|
+
target: start,
|
|
231
|
+
release() {
|
|
232
|
+
for (const release of unsubscribers) release()
|
|
233
|
+
for (const value of values) value.destroy()
|
|
234
|
+
},
|
|
235
|
+
}
|
|
236
|
+
properties.set(key, property)
|
|
237
|
+
return property
|
|
238
|
+
}
|
|
239
|
+
const refresh = (force = false) => {
|
|
240
|
+
if (dead || exiting) return
|
|
241
|
+
const initial = (props[`initial`] ?? {}) as TargetValues | false
|
|
242
|
+
const animateTo = (props[`animate`] ?? {}) as TargetValues
|
|
243
|
+
const hoverTo = (props[`whileHover`] ?? {}) as TargetValues
|
|
244
|
+
const tapTo = (props[`whileTap`] ?? {}) as TargetValues
|
|
245
|
+
const goals = {
|
|
246
|
+
...animateTo,
|
|
247
|
+
...(hovered ? hoverTo : {}),
|
|
248
|
+
...(tapped ? tapTo : {}),
|
|
249
|
+
}
|
|
250
|
+
const keys = new Set([
|
|
251
|
+
...Object.keys(initial || {}),
|
|
252
|
+
...Object.keys(animateTo),
|
|
253
|
+
...Object.keys(hoverTo),
|
|
254
|
+
...Object.keys(tapTo),
|
|
255
|
+
])
|
|
256
|
+
let added = false
|
|
257
|
+
for (const key of keys) {
|
|
258
|
+
const sample =
|
|
259
|
+
goals[key] ??
|
|
260
|
+
(initial === false ? undefined : initial[key]) ??
|
|
261
|
+
hoverTo[key] ??
|
|
262
|
+
tapTo[key]
|
|
263
|
+
if (sample === undefined) continue
|
|
264
|
+
const had = properties.has(key)
|
|
265
|
+
ensure(key, sample, started ? {} : initial)
|
|
266
|
+
if (!had) added = true
|
|
267
|
+
}
|
|
268
|
+
if (added) bindStyles()
|
|
269
|
+
const transition = (props[`transition`] ?? {
|
|
270
|
+
type: `spring`,
|
|
271
|
+
}) as ValueAnimationTransition
|
|
272
|
+
for (const [key, property] of properties) {
|
|
273
|
+
const goal =
|
|
274
|
+
goals[key] ??
|
|
275
|
+
(initial === false ? undefined : initial[key]) ??
|
|
276
|
+
property.rest
|
|
277
|
+
if (!force && equal(property.target, goal)) continue
|
|
278
|
+
property.target = Array.isArray(goal) ? [...goal] : goal
|
|
279
|
+
const next = Array.isArray(goal) ? goal : [goal]
|
|
280
|
+
if (next.length !== property.values.length)
|
|
281
|
+
throw new Error(`spa/motion: cannot change the shape of ${key}`)
|
|
282
|
+
property.values.forEach((value, index) => {
|
|
283
|
+
if (reduced() || (!started && initial === false)) value.jump(next[index])
|
|
284
|
+
else
|
|
285
|
+
animateTween(
|
|
286
|
+
value,
|
|
287
|
+
next[index],
|
|
288
|
+
transition,
|
|
289
|
+
force || value.isAnimating(),
|
|
290
|
+
)
|
|
291
|
+
})
|
|
292
|
+
}
|
|
293
|
+
started = true
|
|
294
|
+
}
|
|
295
|
+
const clearGestures = () => {
|
|
296
|
+
for (const release of gestures) release()
|
|
297
|
+
gestures = []
|
|
298
|
+
}
|
|
299
|
+
const installGestures = () => {
|
|
300
|
+
clearGestures()
|
|
301
|
+
if (element) {
|
|
302
|
+
if (props[`whileHover`])
|
|
303
|
+
gestures.push(
|
|
304
|
+
hover([element], () => {
|
|
305
|
+
hovered = true
|
|
306
|
+
refresh()
|
|
307
|
+
return () => {
|
|
308
|
+
hovered = false
|
|
309
|
+
refresh()
|
|
310
|
+
}
|
|
311
|
+
}),
|
|
312
|
+
)
|
|
313
|
+
if (props[`whileTap`])
|
|
314
|
+
gestures.push(
|
|
315
|
+
press([element], () => {
|
|
316
|
+
tapped = true
|
|
317
|
+
refresh()
|
|
318
|
+
return () => {
|
|
319
|
+
tapped = false
|
|
320
|
+
refresh()
|
|
321
|
+
}
|
|
322
|
+
}),
|
|
323
|
+
)
|
|
324
|
+
} else if (props[`whileHover`] || props[`whileTap`]) {
|
|
325
|
+
if (!target.listen)
|
|
326
|
+
throw new Error(
|
|
327
|
+
`spa/motion: ${target.kind} gestures require a renderer event adapter`,
|
|
328
|
+
)
|
|
329
|
+
const listen = (event: string, run: () => void) =>
|
|
330
|
+
gestures.push(target.listen!(event, run))
|
|
331
|
+
listen(`pointerenter`, () => {
|
|
332
|
+
hovered = true
|
|
333
|
+
refresh()
|
|
334
|
+
})
|
|
335
|
+
listen(`pointerleave`, () => {
|
|
336
|
+
hovered = false
|
|
337
|
+
tapped = false
|
|
338
|
+
refresh()
|
|
339
|
+
})
|
|
340
|
+
listen(`pointerdown`, () => {
|
|
341
|
+
tapped = true
|
|
342
|
+
refresh()
|
|
343
|
+
})
|
|
344
|
+
listen(`pointerup`, () => {
|
|
345
|
+
tapped = false
|
|
346
|
+
refresh()
|
|
347
|
+
})
|
|
348
|
+
listen(`pointercancel`, () => {
|
|
349
|
+
tapped = false
|
|
350
|
+
refresh()
|
|
351
|
+
})
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
const preferenceChanged = () => {
|
|
355
|
+
if (reduced()) {
|
|
356
|
+
for (const property of properties.values()) {
|
|
357
|
+
const goal = Array.isArray(property.target)
|
|
358
|
+
? property.target
|
|
359
|
+
: [property.target]
|
|
360
|
+
property.values.forEach((value, i) => {
|
|
361
|
+
value.jump(goal[i])
|
|
362
|
+
})
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
const finishExit = () => {
|
|
367
|
+
exitHandle?.cancel()
|
|
368
|
+
}
|
|
369
|
+
const mediaChanged = () => {
|
|
370
|
+
preferenceChanged()
|
|
371
|
+
if (reduced() && exiting) finishExit()
|
|
372
|
+
}
|
|
373
|
+
preference?.addEventListener(`change`, mediaChanged)
|
|
374
|
+
return {
|
|
375
|
+
exit(next) {
|
|
376
|
+
const goals = next[`exit`] as TargetValues | undefined
|
|
377
|
+
exiting = true
|
|
378
|
+
hovered = tapped = false
|
|
379
|
+
clearGestures()
|
|
380
|
+
const moving = new Set<MotionValue<MotionScalar>>()
|
|
381
|
+
for (const property of properties.values())
|
|
382
|
+
for (const value of property.values) {
|
|
383
|
+
if (value.isAnimating()) moving.add(value)
|
|
384
|
+
value.stop()
|
|
385
|
+
}
|
|
386
|
+
if (!goals || !Object.keys(goals).length || reduced()) return undefined
|
|
387
|
+
const transition = (next[`transition`] ?? {
|
|
388
|
+
type: `spring`,
|
|
389
|
+
}) as MotionTransition
|
|
390
|
+
for (const key of [
|
|
391
|
+
`duration`,
|
|
392
|
+
`delay`,
|
|
393
|
+
`visualDuration`,
|
|
394
|
+
`velocity`,
|
|
395
|
+
`bounce`,
|
|
396
|
+
`stiffness`,
|
|
397
|
+
`damping`,
|
|
398
|
+
`mass`,
|
|
399
|
+
`restDelta`,
|
|
400
|
+
`restSpeed`,
|
|
401
|
+
] as const) {
|
|
402
|
+
const value = transition[key]
|
|
403
|
+
if (
|
|
404
|
+
value !== undefined &&
|
|
405
|
+
(!Number.isFinite(value) ||
|
|
406
|
+
(key !== `velocity` && value < 0) ||
|
|
407
|
+
(key === `bounce` && value >= 1) ||
|
|
408
|
+
([`stiffness`, `damping`, `mass`, `restDelta`, `restSpeed`].includes(
|
|
409
|
+
key,
|
|
410
|
+
) &&
|
|
411
|
+
value === 0))
|
|
412
|
+
)
|
|
413
|
+
throw new Error(
|
|
414
|
+
`spa/motion: exit requires a finite settling transition`,
|
|
415
|
+
)
|
|
416
|
+
}
|
|
417
|
+
let added = false
|
|
418
|
+
for (const [key, goal] of Object.entries(goals)) {
|
|
419
|
+
const had = properties.has(key)
|
|
420
|
+
ensure(key, goal, {})
|
|
421
|
+
if (!had) added = true
|
|
422
|
+
}
|
|
423
|
+
if (added) bindStyles()
|
|
424
|
+
let settle!: () => void
|
|
425
|
+
let reject!: (error: unknown) => void
|
|
426
|
+
const finished = new Promise<void>((resolve, fail) => {
|
|
427
|
+
settle = resolve
|
|
428
|
+
reject = fail
|
|
429
|
+
})
|
|
430
|
+
const controls: ReturnType<typeof animate>[] = []
|
|
431
|
+
for (const [key, goal] of Object.entries(goals)) {
|
|
432
|
+
const property = properties.get(key)!
|
|
433
|
+
property.target = Array.isArray(goal) ? [...goal] : goal
|
|
434
|
+
const values = Array.isArray(goal) ? goal : [goal]
|
|
435
|
+
property.values.forEach((value, index) =>
|
|
436
|
+
controls.push(
|
|
437
|
+
animateTween(
|
|
438
|
+
value,
|
|
439
|
+
values[index],
|
|
440
|
+
transition as ValueAnimationTransition,
|
|
441
|
+
moving.has(value),
|
|
442
|
+
),
|
|
443
|
+
),
|
|
444
|
+
)
|
|
445
|
+
}
|
|
446
|
+
void Promise.all(controls.map((control) => Promise.resolve(control))).then(
|
|
447
|
+
settle,
|
|
448
|
+
reject,
|
|
449
|
+
)
|
|
450
|
+
exitHandle = {
|
|
451
|
+
finished,
|
|
452
|
+
cancel() {
|
|
453
|
+
for (const control of controls) control.stop()
|
|
454
|
+
settle()
|
|
455
|
+
},
|
|
456
|
+
}
|
|
457
|
+
return exitHandle
|
|
458
|
+
},
|
|
459
|
+
resume() {
|
|
460
|
+
if (dead) return
|
|
461
|
+
exitHandle?.cancel()
|
|
462
|
+
exitHandle = undefined
|
|
463
|
+
exiting = false
|
|
464
|
+
installGestures()
|
|
465
|
+
refresh(true)
|
|
466
|
+
},
|
|
467
|
+
update(next) {
|
|
468
|
+
if (dead) return
|
|
469
|
+
for (const key of Object.keys(next))
|
|
470
|
+
if (!allowed.has(key))
|
|
471
|
+
throw new Error(`spa/motion: unknown property motion:${key}`)
|
|
472
|
+
const gestureChange =
|
|
473
|
+
!started ||
|
|
474
|
+
Boolean(next[`whileHover`]) !== Boolean(props[`whileHover`]) ||
|
|
475
|
+
Boolean(next[`whileTap`]) !== Boolean(props[`whileTap`])
|
|
476
|
+
props = next
|
|
477
|
+
if (gestureChange && !exiting) installGestures()
|
|
478
|
+
refresh()
|
|
479
|
+
},
|
|
480
|
+
dispose() {
|
|
481
|
+
if (dead) return
|
|
482
|
+
dead = true
|
|
483
|
+
clearGestures()
|
|
484
|
+
exitHandle?.cancel()
|
|
485
|
+
preference?.removeEventListener(`change`, mediaChanged)
|
|
486
|
+
styles?.()
|
|
487
|
+
for (const [key, property] of properties) {
|
|
488
|
+
property.release()
|
|
489
|
+
if (!element) target.write!(key, property.base)
|
|
490
|
+
}
|
|
491
|
+
for (const [key, value] of originalStyles) {
|
|
492
|
+
if (value.value)
|
|
493
|
+
element!.style.setProperty(key, value.value, value.priority)
|
|
494
|
+
else element!.style.removeProperty(key)
|
|
495
|
+
}
|
|
496
|
+
properties.clear()
|
|
497
|
+
},
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/** Explicit, root-scoped motion:* integration. Importing it never registers globally. */
|
|
502
|
+
export function motionCapability(
|
|
503
|
+
options: MotionCapabilityOptions = {},
|
|
504
|
+
): Capability {
|
|
505
|
+
return {
|
|
506
|
+
namespace: `motion`,
|
|
507
|
+
presence: createPresence,
|
|
508
|
+
attach: (target) => attach(target, options),
|
|
509
|
+
scheduleFrame(callback) {
|
|
510
|
+
const run = ({ timestamp }: { timestamp: number }) => {
|
|
511
|
+
callback(timestamp)
|
|
512
|
+
}
|
|
513
|
+
frame.render(run, true)
|
|
514
|
+
return () => {
|
|
515
|
+
cancelFrame(run)
|
|
516
|
+
}
|
|
517
|
+
},
|
|
518
|
+
}
|
|
519
|
+
}
|