@atom.io/spa 0.0.6 → 0.0.7
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 +282 -10
- package/README.mdx +65 -5
- package/dist/astro-application.d.ts +1 -1
- package/dist/astro-client.d.ts +1 -1
- package/dist/astro-client.js +2 -2
- package/dist/astro-resumability-client.d.ts +1 -1
- package/dist/astro-server.js +1 -1
- package/dist/{astro-shared-B9wvGIkk.d.ts → astro-shared-DZp1x8RQ.d.ts} +2 -2
- package/dist/{astro-shared-B9wvGIkk.d.ts.map → astro-shared-DZp1x8RQ.d.ts.map} +1 -1
- package/dist/astro.d.ts +1 -1
- package/dist/{capabilities-_zOwNJ-2.d.ts → capabilities-DNcnGRv_.d.ts} +7 -3
- package/dist/capabilities-DNcnGRv_.d.ts.map +1 -0
- package/dist/capabilities-nOIitQ-_.js.map +1 -1
- package/dist/client.min.js +1 -1
- package/dist/{core-DjFJ0aIJ.js → core-B4rJ0xdc.js} +22 -12
- package/dist/core-B4rJ0xdc.js.map +1 -0
- package/dist/development/astro-client.js +2 -2
- package/dist/development/astro-server.js +1 -1
- package/dist/development/capabilities-nOIitQ-_.js.map +1 -1
- package/dist/development/{core-DxPiKryn.js → core-BHA7I1nf.js} +24 -14
- package/dist/development/core-BHA7I1nf.js.map +1 -0
- package/dist/development/{dom-BWy4dDss.js → dom-DQ0M3rLL.js} +2 -2
- package/dist/development/{dom-BWy4dDss.js.map → dom-DQ0M3rLL.js.map} +1 -1
- package/dist/development/floating.js +1 -1
- package/dist/development/index.js +4 -4
- package/dist/development/konva.js +1 -1
- package/dist/development/motion.js +413 -24
- package/dist/development/motion.js.map +1 -1
- package/dist/development/renderer.js +1 -1
- package/dist/development/{server-renderer-C7lGVhpo.js → server-renderer-DCLoMdOX.js} +2 -2
- package/dist/development/{server-renderer-C7lGVhpo.js.map → server-renderer-DCLoMdOX.js.map} +1 -1
- package/dist/development/server.js +1 -1
- package/dist/development/{src-WSHJxR9b.js → src-DoQgCX-O.js} +2 -2
- package/dist/development/{src-WSHJxR9b.js.map → src-DoQgCX-O.js.map} +1 -1
- package/dist/development/three.js +76 -23
- package/dist/development/three.js.map +1 -1
- package/dist/{dom-BrPrxO97.js → dom-D8g3roX9.js} +2 -2
- package/dist/{dom-BrPrxO97.js.map → dom-D8g3roX9.js.map} +1 -1
- package/dist/floating.d.ts +1 -1
- package/dist/floating.js +1 -1
- package/dist/{index-BFIjI7gF.d.ts → index-BFnXIfOj.d.ts} +3 -3
- package/dist/{index-BFIjI7gF.d.ts.map → index-BFnXIfOj.d.ts.map} +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- package/dist/{jsx-runtime-BhiCZOYZ.d.ts → jsx-runtime-DQnpyT_g.d.ts} +2 -2
- package/dist/{jsx-runtime-BhiCZOYZ.d.ts.map → jsx-runtime-DQnpyT_g.d.ts.map} +1 -1
- package/dist/jsx-runtime.d.ts +1 -1
- package/dist/konva.d.ts +3 -3
- package/dist/konva.js +1 -1
- package/dist/motion.d.ts +39 -16
- package/dist/motion.d.ts.map +1 -1
- package/dist/motion.js +413 -24
- package/dist/motion.js.map +1 -1
- package/dist/renderer.d.ts +1 -1
- package/dist/renderer.js +1 -1
- package/dist/{server-renderer-D-uRg04V.js → server-renderer-D0rsFL_-.js} +2 -2
- package/dist/{server-renderer-D-uRg04V.js.map → server-renderer-D0rsFL_-.js.map} +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +1 -1
- package/dist/size.json +12 -12
- package/dist/{src-DWIqXCiz.js → src-Bme1kz2b.js} +2 -2
- package/dist/{src-DWIqXCiz.js.map → src-Bme1kz2b.js.map} +1 -1
- package/dist/three.d.ts +27 -8
- package/dist/three.d.ts.map +1 -1
- package/dist/three.js +76 -23
- package/dist/three.js.map +1 -1
- package/package.json +6 -5
- package/src/capabilities.ts +2 -0
- package/src/core.ts +26 -12
- package/src/index.ts +1 -0
- package/src/motion-layout.ts +363 -0
- package/src/motion-transition.ts +92 -0
- package/src/motion-tween.ts +19 -2
- package/src/motion.ts +204 -76
- package/src/three.ts +134 -39
- package/dist/capabilities-_zOwNJ-2.d.ts.map +0 -1
- package/dist/core-DjFJ0aIJ.js.map +0 -1
- package/dist/development/core-DxPiKryn.js.map +0 -1
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import type { IProjectionNode, ValueTransition } from "motion"
|
|
2
|
+
import { acquireProjectionNode, visualElementStore } from "motion"
|
|
3
|
+
|
|
4
|
+
import type { CapabilityBinding } from "./capabilities.ts"
|
|
5
|
+
import type { Cleanup, Root, Scope } from "./core.ts"
|
|
6
|
+
import { fail, onMount, within } from "./core.ts"
|
|
7
|
+
import type {
|
|
8
|
+
MotionCapabilityOptions,
|
|
9
|
+
MotionLayoutTransition,
|
|
10
|
+
} from "./motion.ts"
|
|
11
|
+
|
|
12
|
+
interface Props {
|
|
13
|
+
animate?: boolean | `position` | `size`
|
|
14
|
+
id?: string | number
|
|
15
|
+
group?: string | number
|
|
16
|
+
scroll?: boolean
|
|
17
|
+
transition?: MotionLayoutTransition
|
|
18
|
+
}
|
|
19
|
+
interface LayoutController {
|
|
20
|
+
commit(): void
|
|
21
|
+
beforeUpdate(): Cleanup
|
|
22
|
+
attach(element: HTMLElement, scope: Scope): CapabilityBinding
|
|
23
|
+
}
|
|
24
|
+
interface Entry {
|
|
25
|
+
element: HTMLElement
|
|
26
|
+
scope: Scope
|
|
27
|
+
props: Props
|
|
28
|
+
mounted: boolean
|
|
29
|
+
dead: boolean
|
|
30
|
+
node?: IProjectionNode | undefined
|
|
31
|
+
release?: Cleanup | undefined
|
|
32
|
+
refreshStyles?: Cleanup | undefined
|
|
33
|
+
captureStyles?: Cleanup | undefined
|
|
34
|
+
identity?: string
|
|
35
|
+
size?: string
|
|
36
|
+
}
|
|
37
|
+
let serial = 0
|
|
38
|
+
|
|
39
|
+
export function createLayoutController(
|
|
40
|
+
root: Root,
|
|
41
|
+
options: MotionCapabilityOptions,
|
|
42
|
+
): LayoutController {
|
|
43
|
+
const prefix = serial++
|
|
44
|
+
const entries = new Set<Entry>()
|
|
45
|
+
let dead = false
|
|
46
|
+
let updating = false
|
|
47
|
+
const preference =
|
|
48
|
+
options.reducedMotion !== `never` && typeof matchMedia === `function`
|
|
49
|
+
? matchMedia(`(prefers-reduced-motion: reduce)`)
|
|
50
|
+
: undefined
|
|
51
|
+
const reduced = () =>
|
|
52
|
+
options.reducedMotion === `always` ||
|
|
53
|
+
(options.reducedMotion !== `never` && Boolean(preference?.matches))
|
|
54
|
+
const report = (error: unknown) => {
|
|
55
|
+
try {
|
|
56
|
+
fail(root, error)
|
|
57
|
+
} catch (failure) {
|
|
58
|
+
root.error(failure)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const ordered = () =>
|
|
62
|
+
[...entries]
|
|
63
|
+
.filter(
|
|
64
|
+
(entry) => entry.mounted && !entry.dead && entry.element.isConnected,
|
|
65
|
+
)
|
|
66
|
+
.sort((a, b) =>
|
|
67
|
+
a.element.compareDocumentPosition(b.element) &
|
|
68
|
+
Node.DOCUMENT_POSITION_FOLLOWING
|
|
69
|
+
? -1
|
|
70
|
+
: 1,
|
|
71
|
+
)
|
|
72
|
+
const release = (entry: Entry) => {
|
|
73
|
+
const detach = entry.release
|
|
74
|
+
entry.release = undefined
|
|
75
|
+
entry.node = undefined
|
|
76
|
+
entry.refreshStyles = entry.captureStyles = undefined
|
|
77
|
+
detach?.()
|
|
78
|
+
}
|
|
79
|
+
const configure = (entry: Entry) => {
|
|
80
|
+
const transition = entry.props.transition ??
|
|
81
|
+
options.layoutTransition ?? { duration: 0.3 }
|
|
82
|
+
for (const name of [
|
|
83
|
+
`duration`,
|
|
84
|
+
`delay`,
|
|
85
|
+
`stiffness`,
|
|
86
|
+
`damping`,
|
|
87
|
+
`mass`,
|
|
88
|
+
] as const) {
|
|
89
|
+
const value = transition[name]
|
|
90
|
+
if (
|
|
91
|
+
value !== undefined &&
|
|
92
|
+
(!Number.isFinite(value) ||
|
|
93
|
+
value < 0 ||
|
|
94
|
+
([`stiffness`, `damping`, `mass`].includes(name) && value === 0))
|
|
95
|
+
)
|
|
96
|
+
throw new Error(`spa/motion: transitions must settle in finite time`)
|
|
97
|
+
}
|
|
98
|
+
entry.node!.setOptions({
|
|
99
|
+
layoutScroll: entry.props.scroll ?? false,
|
|
100
|
+
animationType:
|
|
101
|
+
typeof entry.props.animate === `string` ? entry.props.animate : `both`,
|
|
102
|
+
animate: entry.props.animate !== false && !reduced(),
|
|
103
|
+
transition: (reduced() ? { duration: 0 } : transition) as ValueTransition,
|
|
104
|
+
})
|
|
105
|
+
if (reduced()) entry.node!.finishAnimation()
|
|
106
|
+
}
|
|
107
|
+
const prepare = (entry: Entry) => {
|
|
108
|
+
const { element, props } = entry
|
|
109
|
+
const identity = JSON.stringify([
|
|
110
|
+
prefix,
|
|
111
|
+
props.group ?? ``,
|
|
112
|
+
props.id ?? null,
|
|
113
|
+
props.animate ?? true,
|
|
114
|
+
])
|
|
115
|
+
if (entry.node && entry.identity !== identity) release(entry)
|
|
116
|
+
if (!entry.node) {
|
|
117
|
+
const attrs = [`data-layout`, `data-layout-id`].map(
|
|
118
|
+
(name) => [name, element.getAttribute(name)] as const,
|
|
119
|
+
)
|
|
120
|
+
const styleNames = [
|
|
121
|
+
`transform`,
|
|
122
|
+
`transform-origin`,
|
|
123
|
+
`opacity`,
|
|
124
|
+
`visibility`,
|
|
125
|
+
`pointer-events`,
|
|
126
|
+
]
|
|
127
|
+
const style = element.ownerDocument.createElement(`div`).style
|
|
128
|
+
style.cssText = element.style.cssText
|
|
129
|
+
const restore = (name: string) => {
|
|
130
|
+
const value = style.getPropertyValue(name)
|
|
131
|
+
if (value)
|
|
132
|
+
element.style.setProperty(name, value, style.getPropertyPriority(name))
|
|
133
|
+
else element.style.removeProperty(name)
|
|
134
|
+
}
|
|
135
|
+
element.removeAttribute(`data-layout`)
|
|
136
|
+
element.removeAttribute(`data-layout-id`)
|
|
137
|
+
if (props.animate !== false)
|
|
138
|
+
element.setAttribute(
|
|
139
|
+
`data-layout`,
|
|
140
|
+
typeof props.animate === `string` ? props.animate : `true`,
|
|
141
|
+
)
|
|
142
|
+
if (props.id !== undefined && props.animate !== false)
|
|
143
|
+
element.setAttribute(
|
|
144
|
+
`data-layout-id`,
|
|
145
|
+
JSON.stringify([prefix, props.group ?? ``, props.id]),
|
|
146
|
+
)
|
|
147
|
+
const [node, detach] = acquireProjectionNode(element)
|
|
148
|
+
entry.node = node
|
|
149
|
+
entry.identity = identity
|
|
150
|
+
const visual = node.options.visualElement!
|
|
151
|
+
const originalProps = visual.getProps()
|
|
152
|
+
const originalValues = { ...node.latestValues }
|
|
153
|
+
let opacity = `1`
|
|
154
|
+
let visibility = ``
|
|
155
|
+
let pointerEvents = ``
|
|
156
|
+
let transform = ``
|
|
157
|
+
const transformTemplate = (_: unknown, projected: string) =>
|
|
158
|
+
`${projected === `none` ? `` : projected} ${transform}`.trim()
|
|
159
|
+
entry.captureStyles = () => {
|
|
160
|
+
style.cssText = element.style.cssText
|
|
161
|
+
}
|
|
162
|
+
entry.refreshStyles = () => {
|
|
163
|
+
// Resolve authored CSS against today's dimensions, without reading back
|
|
164
|
+
// the opacity and percentage origins written by projection itself.
|
|
165
|
+
const rendered = element.style.cssText
|
|
166
|
+
for (const name of styleNames) restore(name)
|
|
167
|
+
try {
|
|
168
|
+
const authored = getComputedStyle(element)
|
|
169
|
+
opacity = authored.opacity || `1`
|
|
170
|
+
visibility = authored.visibility
|
|
171
|
+
pointerEvents = authored.pointerEvents
|
|
172
|
+
transform = authored.transform
|
|
173
|
+
if (!visual.values.has(`opacity`))
|
|
174
|
+
node.latestValues[`opacity`] = Number(opacity)
|
|
175
|
+
const origin = authored.transformOrigin.split(` `)
|
|
176
|
+
for (const [key, value] of [
|
|
177
|
+
[`originX`, parseFloat(origin[0]) / (element.offsetWidth || 1)],
|
|
178
|
+
[`originY`, parseFloat(origin[1]) / (element.offsetHeight || 1)],
|
|
179
|
+
] as const)
|
|
180
|
+
if (!visual.values.has(key) && Number.isFinite(value))
|
|
181
|
+
node.latestValues[key] = value
|
|
182
|
+
} finally {
|
|
183
|
+
element.style.cssText = rendered
|
|
184
|
+
}
|
|
185
|
+
// Motion-owned transforms compose in projection; return to authored
|
|
186
|
+
// transforms if that Motion binding is subsequently removed.
|
|
187
|
+
const template =
|
|
188
|
+
transform &&
|
|
189
|
+
transform !== `none` &&
|
|
190
|
+
!Object.keys(visual.latestValues).some((key) =>
|
|
191
|
+
/^(x|y|scale|rotate|skew)/.test(key),
|
|
192
|
+
)
|
|
193
|
+
? transformTemplate
|
|
194
|
+
: originalProps.transformTemplate
|
|
195
|
+
if (visual.getProps().transformTemplate !== template) {
|
|
196
|
+
const next = { ...visual.getProps() }
|
|
197
|
+
if (template) next.transformTemplate = template
|
|
198
|
+
else delete next.transformTemplate
|
|
199
|
+
visual.update(next, null)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
const applyProjectionStyles = node.applyProjectionStyles
|
|
203
|
+
node.applyProjectionStyles = (targetStyle, styleProp) => {
|
|
204
|
+
applyProjectionStyles.call(node, targetStyle, {
|
|
205
|
+
...styleProp,
|
|
206
|
+
pointerEvents: styleProp?.[`pointerEvents`] ?? pointerEvents,
|
|
207
|
+
})
|
|
208
|
+
if (!targetStyle.visibility) targetStyle.visibility = visibility
|
|
209
|
+
if (!targetStyle.opacity)
|
|
210
|
+
targetStyle.opacity = String(node.latestValues[`opacity`] ?? opacity)
|
|
211
|
+
}
|
|
212
|
+
entry.release = () => {
|
|
213
|
+
try {
|
|
214
|
+
if (node.isLead()) node.finishAnimation()
|
|
215
|
+
detach()
|
|
216
|
+
} finally {
|
|
217
|
+
node.applyProjectionStyles = applyProjectionStyles
|
|
218
|
+
visual.projection = undefined
|
|
219
|
+
for (const key of [`opacity`, `originX`, `originY`])
|
|
220
|
+
if (!visual.values.has(key)) {
|
|
221
|
+
if (originalValues[key] === undefined)
|
|
222
|
+
Reflect.deleteProperty(node.latestValues, key)
|
|
223
|
+
else node.latestValues[key] = originalValues[key]
|
|
224
|
+
}
|
|
225
|
+
const retained =
|
|
226
|
+
!root._dead && !entry.scope._dead && visual.values.size > 0
|
|
227
|
+
if (!retained) {
|
|
228
|
+
visual.unmount()
|
|
229
|
+
visualElementStore.delete(element)
|
|
230
|
+
} else visual.update(originalProps, null)
|
|
231
|
+
for (const [name, value] of attrs) {
|
|
232
|
+
if (value === null) element.removeAttribute(name)
|
|
233
|
+
else element.setAttribute(name, value)
|
|
234
|
+
}
|
|
235
|
+
for (const name of styleNames) restore(name)
|
|
236
|
+
if (retained) visual.scheduleRender()
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
observer?.observe(element)
|
|
240
|
+
}
|
|
241
|
+
entry.refreshStyles!()
|
|
242
|
+
configure(entry)
|
|
243
|
+
entry.size = `${element.offsetWidth}:${element.offsetHeight}`
|
|
244
|
+
}
|
|
245
|
+
const commit = () => {
|
|
246
|
+
try {
|
|
247
|
+
if (dead) return
|
|
248
|
+
for (const entry of ordered()) prepare(entry)
|
|
249
|
+
const roots = new Set(
|
|
250
|
+
[...entries].flatMap((entry) =>
|
|
251
|
+
entry.node ? [entry.node.root ?? entry.node] : [],
|
|
252
|
+
),
|
|
253
|
+
)
|
|
254
|
+
for (const projection of roots) projection.didUpdate()
|
|
255
|
+
// Mount replacements before dropping outgoing nodes so shared snapshots can hand off.
|
|
256
|
+
for (const entry of entries)
|
|
257
|
+
if (entry.dead) {
|
|
258
|
+
observer?.unobserve(entry.element)
|
|
259
|
+
release(entry)
|
|
260
|
+
entries.delete(entry)
|
|
261
|
+
}
|
|
262
|
+
} finally {
|
|
263
|
+
updating = false
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
const observer =
|
|
267
|
+
typeof ResizeObserver === `function`
|
|
268
|
+
? new ResizeObserver((changes) => {
|
|
269
|
+
if (dead || updating) return
|
|
270
|
+
try {
|
|
271
|
+
for (const change of changes) {
|
|
272
|
+
const entry = [...entries].find(
|
|
273
|
+
(candidate) => candidate.element === change.target,
|
|
274
|
+
)
|
|
275
|
+
if (
|
|
276
|
+
!entry?.node ||
|
|
277
|
+
entry.size ===
|
|
278
|
+
`${entry.element.offsetWidth}:${entry.element.offsetHeight}`
|
|
279
|
+
)
|
|
280
|
+
continue
|
|
281
|
+
const previous = entry.node.layout
|
|
282
|
+
entry.node.willUpdate()
|
|
283
|
+
if (previous) entry.node.snapshot = previous
|
|
284
|
+
}
|
|
285
|
+
commit()
|
|
286
|
+
} catch (error) {
|
|
287
|
+
report(error)
|
|
288
|
+
}
|
|
289
|
+
})
|
|
290
|
+
: undefined
|
|
291
|
+
const mediaChanged = () => {
|
|
292
|
+
try {
|
|
293
|
+
for (const entry of entries) if (entry.node) configure(entry)
|
|
294
|
+
} catch (error) {
|
|
295
|
+
report(error)
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
preference?.addEventListener(`change`, mediaChanged)
|
|
299
|
+
root._cleanups.add(() => {
|
|
300
|
+
dead = true
|
|
301
|
+
observer?.disconnect()
|
|
302
|
+
preference?.removeEventListener(`change`, mediaChanged)
|
|
303
|
+
const errors: unknown[] = []
|
|
304
|
+
for (const entry of entries)
|
|
305
|
+
try {
|
|
306
|
+
release(entry)
|
|
307
|
+
} catch (error) {
|
|
308
|
+
errors.push(error)
|
|
309
|
+
}
|
|
310
|
+
entries.clear()
|
|
311
|
+
if (errors.length) throw new AggregateError(errors)
|
|
312
|
+
})
|
|
313
|
+
return {
|
|
314
|
+
commit,
|
|
315
|
+
beforeUpdate(): Cleanup {
|
|
316
|
+
updating = true
|
|
317
|
+
for (const entry of ordered()) {
|
|
318
|
+
prepare(entry)
|
|
319
|
+
entry.node!.willUpdate()
|
|
320
|
+
}
|
|
321
|
+
return commit
|
|
322
|
+
},
|
|
323
|
+
attach(element: HTMLElement, scope: Scope): CapabilityBinding {
|
|
324
|
+
const entry: Entry = {
|
|
325
|
+
element,
|
|
326
|
+
scope,
|
|
327
|
+
props: {},
|
|
328
|
+
mounted: false,
|
|
329
|
+
dead: false,
|
|
330
|
+
}
|
|
331
|
+
entries.add(entry)
|
|
332
|
+
within(scope, () => {
|
|
333
|
+
onMount(() => {
|
|
334
|
+
entry.mounted = true
|
|
335
|
+
})
|
|
336
|
+
})
|
|
337
|
+
return {
|
|
338
|
+
update(props, previousStyle) {
|
|
339
|
+
entry.props = props
|
|
340
|
+
if (previousStyle !== undefined) entry.captureStyles?.()
|
|
341
|
+
},
|
|
342
|
+
exit() {
|
|
343
|
+
if (entry.node) entry.node.isPresent = false
|
|
344
|
+
return undefined
|
|
345
|
+
},
|
|
346
|
+
resume() {
|
|
347
|
+
if (entry.node) {
|
|
348
|
+
entry.node.isPresent = true
|
|
349
|
+
entry.node.promote()
|
|
350
|
+
}
|
|
351
|
+
},
|
|
352
|
+
dispose() {
|
|
353
|
+
entry.dead = true
|
|
354
|
+
if (!updating) {
|
|
355
|
+
observer?.unobserve(element)
|
|
356
|
+
release(entry)
|
|
357
|
+
entries.delete(entry)
|
|
358
|
+
}
|
|
359
|
+
},
|
|
360
|
+
}
|
|
361
|
+
},
|
|
362
|
+
}
|
|
363
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { ValueAnimationTransition } from "motion"
|
|
2
|
+
|
|
3
|
+
import type { MotionScalar, MotionTransition } from "./motion.ts"
|
|
4
|
+
|
|
5
|
+
export function finalFrame(value: any): MotionScalar {
|
|
6
|
+
return Array.isArray(value) ? value.at(-1) : value
|
|
7
|
+
}
|
|
8
|
+
/** Property options override default options, which override top-level options. */
|
|
9
|
+
export function propertyTransition(
|
|
10
|
+
config: MotionTransition | undefined,
|
|
11
|
+
key: string,
|
|
12
|
+
goal?: unknown,
|
|
13
|
+
): ValueAnimationTransition {
|
|
14
|
+
const { default: fallback, properties, ...base } = config ?? {}
|
|
15
|
+
const transition = { ...base, ...fallback, ...properties?.[key] }
|
|
16
|
+
const sequence = Array.isArray(goal)
|
|
17
|
+
if (sequence) {
|
|
18
|
+
if (
|
|
19
|
+
goal.length < 2 ||
|
|
20
|
+
goal.some(
|
|
21
|
+
(value, index) =>
|
|
22
|
+
!(
|
|
23
|
+
typeof value === `string` ||
|
|
24
|
+
(typeof value === `number` && Number.isFinite(value)) ||
|
|
25
|
+
(index === 0 && value === null)
|
|
26
|
+
),
|
|
27
|
+
)
|
|
28
|
+
)
|
|
29
|
+
throw new Error(
|
|
30
|
+
`spa/motion: keyframes require at least two scalar values; only the first may be null`,
|
|
31
|
+
)
|
|
32
|
+
if (transition.type === `spring` && goal.length > 2)
|
|
33
|
+
throw new Error(
|
|
34
|
+
`spa/motion: multi-keyframe sequences require a tween transition`,
|
|
35
|
+
)
|
|
36
|
+
transition.type ??= `tween`
|
|
37
|
+
}
|
|
38
|
+
if (!sequence && !config) transition.type ??= `spring`
|
|
39
|
+
const count = sequence ? goal.length : 2
|
|
40
|
+
if (
|
|
41
|
+
transition.times &&
|
|
42
|
+
(transition.times.length !== count ||
|
|
43
|
+
transition.times[0] !== 0 ||
|
|
44
|
+
transition.times.at(-1) !== 1 ||
|
|
45
|
+
transition.times.some(
|
|
46
|
+
(at, index, times) =>
|
|
47
|
+
!Number.isFinite(at) ||
|
|
48
|
+
at < 0 ||
|
|
49
|
+
at > 1 ||
|
|
50
|
+
(index > 0 && at < times[index - 1]),
|
|
51
|
+
))
|
|
52
|
+
)
|
|
53
|
+
throw new Error(
|
|
54
|
+
`spa/motion: times must match the keyframes and increase from 0 to 1`,
|
|
55
|
+
)
|
|
56
|
+
if (
|
|
57
|
+
Array.isArray(transition.ease) &&
|
|
58
|
+
typeof transition.ease[0] !== `number` &&
|
|
59
|
+
transition.ease.length !== count - 1
|
|
60
|
+
)
|
|
61
|
+
throw new Error(
|
|
62
|
+
`spa/motion: segment easing must match the keyframe intervals`,
|
|
63
|
+
)
|
|
64
|
+
for (const option of [
|
|
65
|
+
`duration`,
|
|
66
|
+
`delay`,
|
|
67
|
+
`visualDuration`,
|
|
68
|
+
`velocity`,
|
|
69
|
+
`bounce`,
|
|
70
|
+
`stiffness`,
|
|
71
|
+
`damping`,
|
|
72
|
+
`mass`,
|
|
73
|
+
`restDelta`,
|
|
74
|
+
`restSpeed`,
|
|
75
|
+
] as const) {
|
|
76
|
+
const value = transition[option]
|
|
77
|
+
if (
|
|
78
|
+
value !== undefined &&
|
|
79
|
+
(!Number.isFinite(value) ||
|
|
80
|
+
(option !== `velocity` && option !== `delay` && value < 0) ||
|
|
81
|
+
(option === `bounce` && value >= 1) ||
|
|
82
|
+
([`stiffness`, `damping`, `mass`, `restDelta`, `restSpeed`].includes(
|
|
83
|
+
option,
|
|
84
|
+
) &&
|
|
85
|
+
value === 0))
|
|
86
|
+
)
|
|
87
|
+
throw new Error(
|
|
88
|
+
`spa/motion: animation requires a finite settling transition`,
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
return transition as ValueAnimationTransition
|
|
92
|
+
}
|
package/src/motion-tween.ts
CHANGED
|
@@ -15,15 +15,32 @@ const returnToTarget = (progress: number) =>
|
|
|
15
15
|
/** Retarget a moving tween without changing its position or velocity at the join. */
|
|
16
16
|
export function animateTween(
|
|
17
17
|
value: MotionValue<MotionScalar>,
|
|
18
|
-
goal: MotionScalar,
|
|
18
|
+
goal: MotionScalar | readonly (MotionScalar | null)[],
|
|
19
19
|
transition: ValueAnimationTransition,
|
|
20
20
|
interrupted: boolean,
|
|
21
21
|
): ReturnType<typeof animate> {
|
|
22
|
+
if (Array.isArray(goal)) {
|
|
23
|
+
value.stop()
|
|
24
|
+
trajectories.delete(value)
|
|
25
|
+
let frames = goal.map((frame) => frame ?? value.get())
|
|
26
|
+
const units = frames.map((frame) =>
|
|
27
|
+
typeof frame === `number` ? `` : scalar.exec(frame)?.[2],
|
|
28
|
+
)
|
|
29
|
+
const unit = units.find((suffix) => suffix)
|
|
30
|
+
// Synchronous animation bypasses Motion's DOM unit resolver. Keep compatible
|
|
31
|
+
// scalar frames in one unit so numeric mixers never receive CSS strings.
|
|
32
|
+
if (unit && units.every((suffix) => suffix === `` || suffix === unit))
|
|
33
|
+
frames = frames.map((frame, index) =>
|
|
34
|
+
units[index] === `` ? `${frame}${unit}` : frame,
|
|
35
|
+
)
|
|
36
|
+
return animate(value, frames, { ...transition, isSync: true })
|
|
37
|
+
}
|
|
22
38
|
if (interrupted) value.stop()
|
|
23
39
|
const current = value.get()
|
|
24
40
|
const from =
|
|
25
41
|
typeof current === `number` ? [``, current, ``] : scalar.exec(current)
|
|
26
|
-
const to =
|
|
42
|
+
const to =
|
|
43
|
+
typeof goal === `number` ? [``, goal, ``] : scalar.exec(goal as string)
|
|
27
44
|
const previous = trajectories.get(value)?.()
|
|
28
45
|
const velocity =
|
|
29
46
|
previous && from && Math.abs(previous.position - Number(from[1])) < 0.001
|