@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
package/src/motion.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import type { animate, MotionValue
|
|
1
|
+
import type { animate, MotionValue } from "motion"
|
|
2
2
|
import {
|
|
3
3
|
cancelFrame,
|
|
4
4
|
frame,
|
|
5
|
+
handOffElementState,
|
|
5
6
|
hover,
|
|
6
7
|
motionValue,
|
|
7
8
|
press,
|
|
8
9
|
readStyleValue,
|
|
9
10
|
styleEffect,
|
|
11
|
+
visualElementStore,
|
|
10
12
|
} from "motion"
|
|
11
13
|
|
|
12
14
|
import type {
|
|
@@ -14,10 +16,65 @@ import type {
|
|
|
14
16
|
CapabilityBinding,
|
|
15
17
|
CapabilityTarget,
|
|
16
18
|
} from "./capabilities.ts"
|
|
19
|
+
import type { Root } from "./core.ts"
|
|
20
|
+
import { createLayoutController } from "./motion-layout.ts"
|
|
21
|
+
import { finalFrame, propertyTransition } from "./motion-transition.ts"
|
|
17
22
|
import { animateTween } from "./motion-tween.ts"
|
|
18
23
|
import type { ExitHandle } from "./presence.ts"
|
|
19
24
|
import { createPresence } from "./presence.ts"
|
|
20
25
|
|
|
26
|
+
export type MotionKeyframes = readonly [
|
|
27
|
+
MotionScalar | null,
|
|
28
|
+
MotionScalar,
|
|
29
|
+
...MotionScalar[],
|
|
30
|
+
]
|
|
31
|
+
export type MotionEasing =
|
|
32
|
+
| `linear`
|
|
33
|
+
| `easeIn`
|
|
34
|
+
| `easeOut`
|
|
35
|
+
| `easeInOut`
|
|
36
|
+
| `circIn`
|
|
37
|
+
| `circOut`
|
|
38
|
+
| `circInOut`
|
|
39
|
+
| `backIn`
|
|
40
|
+
| `backOut`
|
|
41
|
+
| `backInOut`
|
|
42
|
+
| readonly [number, number, number, number]
|
|
43
|
+
/** Public options remain independent of optional Motion peer declarations. */
|
|
44
|
+
export interface MotionValueTransition {
|
|
45
|
+
type?: `spring` | `tween`
|
|
46
|
+
stiffness?: number
|
|
47
|
+
damping?: number
|
|
48
|
+
mass?: number
|
|
49
|
+
velocity?: number
|
|
50
|
+
restSpeed?: number
|
|
51
|
+
restDelta?: number
|
|
52
|
+
bounce?: number
|
|
53
|
+
visualDuration?: number
|
|
54
|
+
duration?: number
|
|
55
|
+
delay?: number
|
|
56
|
+
times?: readonly number[]
|
|
57
|
+
ease?: MotionEasing | readonly MotionEasing[]
|
|
58
|
+
}
|
|
59
|
+
export interface MotionTransition extends MotionValueTransition {
|
|
60
|
+
default?: MotionValueTransition
|
|
61
|
+
properties?: Readonly<Record<string, MotionValueTransition>>
|
|
62
|
+
}
|
|
63
|
+
export interface MotionLayoutTransition {
|
|
64
|
+
type?: `spring` | `tween`
|
|
65
|
+
duration?: number
|
|
66
|
+
delay?: number
|
|
67
|
+
ease?:
|
|
68
|
+
| `linear`
|
|
69
|
+
| `easeIn`
|
|
70
|
+
| `easeOut`
|
|
71
|
+
| `easeInOut`
|
|
72
|
+
| readonly [number, number, number, number]
|
|
73
|
+
stiffness?: number
|
|
74
|
+
damping?: number
|
|
75
|
+
mass?: number
|
|
76
|
+
}
|
|
77
|
+
|
|
21
78
|
export type MotionScalar = number | string
|
|
22
79
|
export type MotionDOMTarget = Partial<
|
|
23
80
|
Record<
|
|
@@ -35,7 +92,7 @@ export type MotionDOMTarget = Partial<
|
|
|
35
92
|
| `skewX`
|
|
36
93
|
| `skewY`
|
|
37
94
|
| `--${string}`,
|
|
38
|
-
MotionScalar
|
|
95
|
+
MotionScalar | MotionKeyframes
|
|
39
96
|
>
|
|
40
97
|
>
|
|
41
98
|
export interface MotionThreeTarget {
|
|
@@ -51,34 +108,19 @@ export type MotionTarget<Kind extends string> = Kind extends `dom`
|
|
|
51
108
|
: Kind extends `three`
|
|
52
109
|
? MotionThreeTarget
|
|
53
110
|
: 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
111
|
export interface MotionAttributes<Kind extends string = `dom`> {
|
|
81
|
-
[`motion:
|
|
112
|
+
[`motion:layout`]?: Kind extends `dom` ? boolean | `position` | `size` : never
|
|
113
|
+
[`motion:layoutId`]?: Kind extends `dom` ? string | number : never
|
|
114
|
+
[`motion:layoutGroup`]?: Kind extends `dom` ? string | number : never
|
|
115
|
+
[`motion:layoutScroll`]?: Kind extends `dom` ? boolean : never
|
|
116
|
+
[`motion:layoutTransition`]?: Kind extends `dom`
|
|
117
|
+
? MotionLayoutTransition
|
|
118
|
+
: never
|
|
119
|
+
[`motion:initial`]?:
|
|
120
|
+
| (Kind extends `dom`
|
|
121
|
+
? Partial<Record<keyof MotionDOMTarget, MotionScalar>>
|
|
122
|
+
: MotionTarget<Kind>)
|
|
123
|
+
| false
|
|
82
124
|
[`motion:animate`]?: MotionTarget<Kind>
|
|
83
125
|
[`motion:exit`]?: MotionTarget<Kind>
|
|
84
126
|
[`motion:transition`]?: MotionTransition
|
|
@@ -97,13 +139,16 @@ declare module "@atom.io/spa/jsx-runtime" {
|
|
|
97
139
|
export interface MotionCapabilityOptions {
|
|
98
140
|
/** Respect the user's reduced-motion preference by default. */
|
|
99
141
|
reducedMotion?: `user` | `always` | `never`
|
|
142
|
+
/** Default transition for layout projection; animated properties use motion:transition. */
|
|
143
|
+
layoutTransition?: MotionLayoutTransition
|
|
100
144
|
}
|
|
101
|
-
type
|
|
145
|
+
type TargetValue = MotionScalar | readonly (MotionScalar | null)[]
|
|
146
|
+
type TargetValues = Record<string, TargetValue>
|
|
102
147
|
interface AnimatedProperty {
|
|
103
148
|
values: MotionValue<MotionScalar>[]
|
|
104
|
-
base:
|
|
105
|
-
rest:
|
|
106
|
-
target:
|
|
149
|
+
base: TargetValue
|
|
150
|
+
rest: TargetValue
|
|
151
|
+
target: TargetValue
|
|
107
152
|
release(): void
|
|
108
153
|
}
|
|
109
154
|
const transform =
|
|
@@ -116,6 +161,13 @@ const allowed = new Set([
|
|
|
116
161
|
`whileHover`,
|
|
117
162
|
`whileTap`,
|
|
118
163
|
])
|
|
164
|
+
const layoutKeys = {
|
|
165
|
+
layout: `animate`,
|
|
166
|
+
layoutId: `id`,
|
|
167
|
+
layoutGroup: `group`,
|
|
168
|
+
layoutScroll: `scroll`,
|
|
169
|
+
layoutTransition: `transition`,
|
|
170
|
+
} as const
|
|
119
171
|
function equal(a: unknown, b: unknown): boolean {
|
|
120
172
|
return (
|
|
121
173
|
Object.is(a, b) ||
|
|
@@ -126,14 +178,10 @@ function equal(a: unknown, b: unknown): boolean {
|
|
|
126
178
|
)
|
|
127
179
|
}
|
|
128
180
|
|
|
129
|
-
function
|
|
181
|
+
function attachValues(
|
|
130
182
|
target: CapabilityTarget,
|
|
131
183
|
options: MotionCapabilityOptions,
|
|
132
184
|
): 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
185
|
const element =
|
|
138
186
|
target.kind === `dom` ? (target.node as HTMLElement | SVGElement) : undefined
|
|
139
187
|
const properties = new Map<string, AnimatedProperty>()
|
|
@@ -166,43 +214,42 @@ function attach(
|
|
|
166
214
|
}
|
|
167
215
|
const bindStyles = () => {
|
|
168
216
|
styles?.()
|
|
169
|
-
if (element)
|
|
217
|
+
if (element) {
|
|
170
218
|
styles = styleEffect(
|
|
171
219
|
[element],
|
|
172
220
|
Object.fromEntries(
|
|
173
221
|
[...properties].map(([key, property]) => [key, property.values[0]]),
|
|
174
222
|
) as any,
|
|
175
223
|
)
|
|
224
|
+
// Layout projection and late-added/gesture values must render together.
|
|
225
|
+
const visual = visualElementStore.get(element)
|
|
226
|
+
if (visual) handOffElementState(element, visual)
|
|
227
|
+
}
|
|
176
228
|
}
|
|
177
229
|
const ensure = (
|
|
178
230
|
key: string,
|
|
179
|
-
goal:
|
|
231
|
+
goal: TargetValue,
|
|
180
232
|
initial: TargetValues | false,
|
|
181
233
|
): AnimatedProperty => {
|
|
234
|
+
const vector = !element && Array.isArray(goal)
|
|
235
|
+
const sample = element ? finalFrame(goal) : goal
|
|
182
236
|
const existing = properties.get(key)
|
|
183
237
|
if (existing) {
|
|
184
|
-
if (existing.values.length !== (
|
|
238
|
+
if (existing.values.length !== (vector ? goal.length : 1))
|
|
185
239
|
throw new Error(`spa/motion: cannot change the shape of ${key}`)
|
|
186
240
|
return existing
|
|
187
241
|
}
|
|
188
|
-
const base = read(key,
|
|
189
|
-
const rest = !
|
|
190
|
-
let start = initial === false ?
|
|
191
|
-
if (!
|
|
192
|
-
if (
|
|
193
|
-
Array.isArray(goal) &&
|
|
194
|
-
(!Array.isArray(start) || start.length !== goal.length)
|
|
195
|
-
)
|
|
242
|
+
const base = read(key, sample) as TargetValue
|
|
243
|
+
const rest = !vector && Array.isArray(base) ? base[0]! : base
|
|
244
|
+
let start = initial === false ? sample : (initial[key] ?? rest)
|
|
245
|
+
if (!vector && Array.isArray(start)) start = start[0]!
|
|
246
|
+
if (vector && (!Array.isArray(start) || start.length !== goal.length))
|
|
196
247
|
throw new Error(`spa/motion: ${key} requires matching numeric tuples`)
|
|
197
248
|
const values = (Array.isArray(start) ? start : [start]).map((value) =>
|
|
198
249
|
motionValue(value),
|
|
199
250
|
) as MotionValue<MotionScalar>[]
|
|
200
251
|
const unsubscribers: (() => void)[] = []
|
|
201
252
|
if (element) {
|
|
202
|
-
if (Array.isArray(goal))
|
|
203
|
-
throw new Error(
|
|
204
|
-
`spa/motion: DOM targets must be scalar styles or transforms`,
|
|
205
|
-
)
|
|
206
253
|
const cssKey = transform.test(key)
|
|
207
254
|
? `transform`
|
|
208
255
|
: key.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)
|
|
@@ -266,9 +313,6 @@ function attach(
|
|
|
266
313
|
if (!had) added = true
|
|
267
314
|
}
|
|
268
315
|
if (added) bindStyles()
|
|
269
|
-
const transition = (props[`transition`] ?? {
|
|
270
|
-
type: `spring`,
|
|
271
|
-
}) as ValueAnimationTransition
|
|
272
316
|
for (const [key, property] of properties) {
|
|
273
317
|
const goal =
|
|
274
318
|
goals[key] ??
|
|
@@ -276,11 +320,17 @@ function attach(
|
|
|
276
320
|
property.rest
|
|
277
321
|
if (!force && equal(property.target, goal)) continue
|
|
278
322
|
property.target = Array.isArray(goal) ? [...goal] : goal
|
|
279
|
-
const next = Array.isArray(goal) ? goal : [goal]
|
|
323
|
+
const next = !element && Array.isArray(goal) ? goal : [goal]
|
|
324
|
+
const transition = propertyTransition(
|
|
325
|
+
props[`transition`] as MotionTransition | undefined,
|
|
326
|
+
key,
|
|
327
|
+
element ? goal : undefined,
|
|
328
|
+
)
|
|
280
329
|
if (next.length !== property.values.length)
|
|
281
330
|
throw new Error(`spa/motion: cannot change the shape of ${key}`)
|
|
282
331
|
property.values.forEach((value, index) => {
|
|
283
|
-
if (reduced() || (!started && initial === false))
|
|
332
|
+
if (reduced() || (!started && initial === false))
|
|
333
|
+
value.jump(finalFrame(next[index]))
|
|
284
334
|
else
|
|
285
335
|
animateTween(
|
|
286
336
|
value,
|
|
@@ -354,11 +404,12 @@ function attach(
|
|
|
354
404
|
const preferenceChanged = () => {
|
|
355
405
|
if (reduced()) {
|
|
356
406
|
for (const property of properties.values()) {
|
|
357
|
-
const goal =
|
|
358
|
-
|
|
359
|
-
|
|
407
|
+
const goal =
|
|
408
|
+
!element && Array.isArray(property.target)
|
|
409
|
+
? property.target
|
|
410
|
+
: [property.target]
|
|
360
411
|
property.values.forEach((value, i) => {
|
|
361
|
-
value.jump(goal[i])
|
|
412
|
+
value.jump(finalFrame(goal[i]))
|
|
362
413
|
})
|
|
363
414
|
}
|
|
364
415
|
}
|
|
@@ -431,15 +482,15 @@ function attach(
|
|
|
431
482
|
for (const [key, goal] of Object.entries(goals)) {
|
|
432
483
|
const property = properties.get(key)!
|
|
433
484
|
property.target = Array.isArray(goal) ? [...goal] : goal
|
|
434
|
-
const values = Array.isArray(goal) ? goal : [goal]
|
|
485
|
+
const values = !element && Array.isArray(goal) ? goal : [goal]
|
|
486
|
+
const resolved = propertyTransition(
|
|
487
|
+
next[`transition`] as MotionTransition | undefined,
|
|
488
|
+
key,
|
|
489
|
+
element ? goal : undefined,
|
|
490
|
+
)
|
|
435
491
|
property.values.forEach((value, index) =>
|
|
436
492
|
controls.push(
|
|
437
|
-
animateTween(
|
|
438
|
-
value,
|
|
439
|
-
values[index],
|
|
440
|
-
transition as ValueAnimationTransition,
|
|
441
|
-
moving.has(value),
|
|
442
|
-
),
|
|
493
|
+
animateTween(value, values[index], resolved, moving.has(value)),
|
|
443
494
|
),
|
|
444
495
|
)
|
|
445
496
|
}
|
|
@@ -457,7 +508,6 @@ function attach(
|
|
|
457
508
|
return exitHandle
|
|
458
509
|
},
|
|
459
510
|
resume() {
|
|
460
|
-
if (dead) return
|
|
461
511
|
exitHandle?.cancel()
|
|
462
512
|
exitHandle = undefined
|
|
463
513
|
exiting = false
|
|
@@ -465,10 +515,6 @@ function attach(
|
|
|
465
515
|
refresh(true)
|
|
466
516
|
},
|
|
467
517
|
update(next, previousStyle) {
|
|
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
518
|
const gestureChange =
|
|
473
519
|
!started ||
|
|
474
520
|
Boolean(next[`whileHover`]) !== Boolean(props[`whileHover`]) ||
|
|
@@ -488,13 +534,16 @@ function attach(
|
|
|
488
534
|
refresh()
|
|
489
535
|
},
|
|
490
536
|
dispose() {
|
|
491
|
-
if (dead) return
|
|
492
537
|
dead = true
|
|
493
538
|
clearGestures()
|
|
494
539
|
exitHandle?.cancel()
|
|
495
540
|
preference?.removeEventListener(`change`, mediaChanged)
|
|
496
541
|
styles?.()
|
|
542
|
+
const visual = element && visualElementStore.get(element)
|
|
497
543
|
for (const [key, property] of properties) {
|
|
544
|
+
// Projection can retain this renderer after the binding is gone.
|
|
545
|
+
if (visual?.values.get(key) === property.values[0])
|
|
546
|
+
visual.removeValue(key)
|
|
498
547
|
property.release()
|
|
499
548
|
if (!element) target.write!(key, property.base)
|
|
500
549
|
}
|
|
@@ -512,10 +561,89 @@ function attach(
|
|
|
512
561
|
export function motionCapability(
|
|
513
562
|
options: MotionCapabilityOptions = {},
|
|
514
563
|
): Capability {
|
|
564
|
+
const layouts = new WeakMap<Root, ReturnType<typeof createLayoutController>>()
|
|
515
565
|
return {
|
|
516
566
|
namespace: `motion`,
|
|
517
567
|
presence: createPresence,
|
|
518
|
-
|
|
568
|
+
beforeUpdate(root) {
|
|
569
|
+
const layout = layouts.get(root)
|
|
570
|
+
// A layout binding can first appear during the upcoming DOM writes.
|
|
571
|
+
return layout ? layout.beforeUpdate() : () => layouts.get(root)?.commit()
|
|
572
|
+
},
|
|
573
|
+
attach(target, scope) {
|
|
574
|
+
if (target.kind !== `dom` && (!target.read || !target.write))
|
|
575
|
+
throw new Error(
|
|
576
|
+
`spa/motion: renderer ${target.kind} does not support motion`,
|
|
577
|
+
)
|
|
578
|
+
let values: CapabilityBinding | undefined
|
|
579
|
+
let layout: CapabilityBinding | undefined
|
|
580
|
+
let dead = false
|
|
581
|
+
return {
|
|
582
|
+
update(next, previousStyle) {
|
|
583
|
+
if (dead) return
|
|
584
|
+
const animated: Record<string, unknown> = {}
|
|
585
|
+
const projected: Record<string, unknown> = {}
|
|
586
|
+
for (const [key, value] of Object.entries(next)) {
|
|
587
|
+
if (Object.hasOwn(layoutKeys, key))
|
|
588
|
+
projected[layoutKeys[key as keyof typeof layoutKeys]] = value
|
|
589
|
+
else if (allowed.has(key)) animated[key] = value
|
|
590
|
+
else throw new Error(`spa/motion: unknown property motion:${key}`)
|
|
591
|
+
}
|
|
592
|
+
if (Object.keys(projected).length) {
|
|
593
|
+
if (!layout) {
|
|
594
|
+
const element = target.node as HTMLElement
|
|
595
|
+
if (
|
|
596
|
+
target.kind !== `dom` ||
|
|
597
|
+
element?.namespaceURI !== `http://www.w3.org/1999/xhtml` ||
|
|
598
|
+
!scope
|
|
599
|
+
)
|
|
600
|
+
throw new Error(
|
|
601
|
+
`spa/motion: layout requires an owned HTML element`,
|
|
602
|
+
)
|
|
603
|
+
let controller = layouts.get(scope._root)
|
|
604
|
+
if (!controller) {
|
|
605
|
+
controller = createLayoutController(scope._root, options)
|
|
606
|
+
layouts.set(scope._root, controller)
|
|
607
|
+
}
|
|
608
|
+
layout = controller.attach(element, scope)
|
|
609
|
+
}
|
|
610
|
+
layout.update(projected, previousStyle)
|
|
611
|
+
} else {
|
|
612
|
+
layout?.dispose()
|
|
613
|
+
layout = undefined
|
|
614
|
+
}
|
|
615
|
+
if (
|
|
616
|
+
Object.keys(animated).length ||
|
|
617
|
+
(values && !Object.keys(projected).length)
|
|
618
|
+
) {
|
|
619
|
+
values ??= attachValues(target, options)
|
|
620
|
+
values.update(animated, previousStyle)
|
|
621
|
+
} else {
|
|
622
|
+
values?.dispose()
|
|
623
|
+
values = undefined
|
|
624
|
+
}
|
|
625
|
+
},
|
|
626
|
+
exit(next) {
|
|
627
|
+
if (dead) return undefined
|
|
628
|
+
layout?.exit?.(next)
|
|
629
|
+
return values?.exit?.(next)
|
|
630
|
+
},
|
|
631
|
+
resume() {
|
|
632
|
+
if (dead) return
|
|
633
|
+
layout?.resume?.()
|
|
634
|
+
values?.resume?.()
|
|
635
|
+
},
|
|
636
|
+
dispose() {
|
|
637
|
+
if (dead) return
|
|
638
|
+
dead = true
|
|
639
|
+
try {
|
|
640
|
+
values?.dispose()
|
|
641
|
+
} finally {
|
|
642
|
+
layout?.dispose()
|
|
643
|
+
}
|
|
644
|
+
},
|
|
645
|
+
}
|
|
646
|
+
},
|
|
519
647
|
scheduleFrame(callback) {
|
|
520
648
|
const run = ({ timestamp }: { timestamp: number }) => {
|
|
521
649
|
callback(timestamp)
|
package/src/three.ts
CHANGED
|
@@ -54,7 +54,12 @@ const constructors = {
|
|
|
54
54
|
}
|
|
55
55
|
const meshDefaults = new WeakMap<
|
|
56
56
|
THREE.Mesh,
|
|
57
|
-
{
|
|
57
|
+
{
|
|
58
|
+
geometry: THREE.BufferGeometry
|
|
59
|
+
material: THREE.Material | THREE.Material[]
|
|
60
|
+
ownsGeometry: boolean
|
|
61
|
+
ownsMaterial: boolean
|
|
62
|
+
}
|
|
58
63
|
>()
|
|
59
64
|
const borrowedMeshDefaults = new WeakMap<
|
|
60
65
|
THREE.Mesh,
|
|
@@ -112,7 +117,9 @@ const renderer = createSceneRenderer<Resource>({
|
|
|
112
117
|
if (node instanceof THREE.Mesh)
|
|
113
118
|
meshDefaults.set(node, {
|
|
114
119
|
geometry: node.geometry,
|
|
115
|
-
material: node.material
|
|
120
|
+
material: node.material,
|
|
121
|
+
ownsGeometry: props[`args`]?.[0] === undefined,
|
|
122
|
+
ownsMaterial: props[`args`]?.[1] === undefined,
|
|
116
123
|
})
|
|
117
124
|
constructorArgs.set(node, [...(props[`args`] ?? [])])
|
|
118
125
|
defaults.set(node, new Map())
|
|
@@ -230,8 +237,8 @@ const renderer = createSceneRenderer<Resource>({
|
|
|
230
237
|
node.removeFromParent()
|
|
231
238
|
if (node instanceof THREE.Mesh) {
|
|
232
239
|
const owned = meshDefaults.get(node)!
|
|
233
|
-
owned.geometry.dispose()
|
|
234
|
-
owned.material.dispose()
|
|
240
|
+
if (owned.ownsGeometry) owned.geometry.dispose()
|
|
241
|
+
if (owned.ownsMaterial) (owned.material as THREE.Material).dispose()
|
|
235
242
|
}
|
|
236
243
|
}
|
|
237
244
|
} else node.dispose()
|
|
@@ -240,47 +247,122 @@ const renderer = createSceneRenderer<Resource>({
|
|
|
240
247
|
function equalArgs(a: unknown[], b: unknown[] = []): boolean {
|
|
241
248
|
return (
|
|
242
249
|
a.length === b.length &&
|
|
243
|
-
a.every(
|
|
250
|
+
a.every(
|
|
251
|
+
(value, index) =>
|
|
252
|
+
Object.is(value, b[index]) ||
|
|
253
|
+
(Array.isArray(value) &&
|
|
254
|
+
Array.isArray(b[index]) &&
|
|
255
|
+
equalArgs(value, b[index])),
|
|
256
|
+
)
|
|
244
257
|
)
|
|
245
258
|
}
|
|
246
259
|
|
|
260
|
+
export interface CanvasState {
|
|
261
|
+
scene: THREE.Scene
|
|
262
|
+
camera: THREE.Camera
|
|
263
|
+
renderer: THREE.WebGLRenderer
|
|
264
|
+
}
|
|
265
|
+
export interface CanvasResizeState extends CanvasState {
|
|
266
|
+
width: number
|
|
267
|
+
height: number
|
|
268
|
+
dpr: number
|
|
269
|
+
}
|
|
247
270
|
export interface CanvasProps {
|
|
248
271
|
children?: Children
|
|
249
272
|
width?: number
|
|
250
273
|
height?: number
|
|
251
274
|
camera?: THREE.Camera
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
275
|
+
/** Constructor-time options. Canvas always supplies its own DOM canvas. */
|
|
276
|
+
rendererOptions?: Omit<THREE.WebGLRendererParameters, `canvas`>
|
|
277
|
+
/** The returned renderer belongs exclusively to Canvas and is disposed with it. */
|
|
278
|
+
createRenderer?: (
|
|
279
|
+
options: THREE.WebGLRendererParameters,
|
|
280
|
+
) => THREE.WebGLRenderer
|
|
281
|
+
/** A fixed ratio, or a device-pixel-ratio clamp. Defaults to 1. */
|
|
282
|
+
dpr?: number | readonly [min: number, max: number]
|
|
283
|
+
/** Runs after sizing and scene mounting, before the first draw. May return cleanup. */
|
|
284
|
+
onCreated?: (state: CanvasState) => void | (() => void)
|
|
285
|
+
/** Supplied cameras keep their projection; update it explicitly here if desired. */
|
|
286
|
+
onResize?: (state: CanvasResizeState) => void
|
|
287
|
+
onFrame?: (state: CanvasState & { delta: number; time: number }) => void
|
|
259
288
|
}
|
|
260
289
|
/** Browser-only scene boundary. The camera prop is borrowed and never disposed. */
|
|
261
290
|
export function Canvas(props: CanvasProps): SpaElement {
|
|
262
291
|
let canvas: HTMLCanvasElement | null = null
|
|
263
292
|
onMount(() => {
|
|
264
|
-
const
|
|
293
|
+
const parameters: THREE.WebGLRendererParameters = {
|
|
294
|
+
antialias: true,
|
|
295
|
+
...props.rendererOptions,
|
|
296
|
+
canvas: canvas!,
|
|
297
|
+
}
|
|
298
|
+
const webgl = props.createRenderer
|
|
299
|
+
? props.createRenderer(parameters)
|
|
300
|
+
: new THREE.WebGLRenderer(parameters)
|
|
265
301
|
onCleanup(() => {
|
|
266
302
|
webgl.dispose()
|
|
267
303
|
})
|
|
304
|
+
if (webgl.domElement !== canvas)
|
|
305
|
+
throw new Error(`spa/three: renderer must use the supplied Canvas element`)
|
|
268
306
|
const scene = new THREE.Scene()
|
|
269
307
|
const camera = new THREE.PerspectiveCamera(50, 1, 0.1, 1000)
|
|
270
308
|
camera.position.z = 5
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
)
|
|
309
|
+
const view = canvas.ownerDocument.defaultView!
|
|
310
|
+
const size = () => ({
|
|
311
|
+
width: props.width ?? 800,
|
|
312
|
+
height: props.height ?? 600,
|
|
313
|
+
dpr: props.dpr ?? 1,
|
|
314
|
+
active: props.camera ?? camera,
|
|
315
|
+
})
|
|
316
|
+
const resize = ({ width, height, dpr, active }: ReturnType<typeof size>) => {
|
|
317
|
+
const range = typeof dpr === `number` ? [dpr, dpr] : dpr
|
|
318
|
+
if (
|
|
319
|
+
![width, height, ...range].every(
|
|
320
|
+
(value) => Number.isFinite(value) && value > 0,
|
|
321
|
+
) ||
|
|
322
|
+
range[0] > range[1]
|
|
323
|
+
)
|
|
324
|
+
throw new Error(
|
|
325
|
+
`spa/three: dimensions and DPR must be positive with an ordered DPR range`,
|
|
326
|
+
)
|
|
327
|
+
const ratio = Math.max(
|
|
328
|
+
range[0],
|
|
329
|
+
Math.min(range[1], view.devicePixelRatio || 1),
|
|
330
|
+
)
|
|
331
|
+
webgl.setPixelRatio(ratio)
|
|
332
|
+
webgl.setSize(width, height)
|
|
333
|
+
camera.aspect = width / height
|
|
334
|
+
camera.updateProjectionMatrix()
|
|
335
|
+
props.onResize?.({
|
|
336
|
+
scene,
|
|
337
|
+
camera: active,
|
|
338
|
+
renderer: webgl,
|
|
339
|
+
width,
|
|
340
|
+
height,
|
|
341
|
+
dpr: ratio,
|
|
342
|
+
})
|
|
343
|
+
}
|
|
344
|
+
bindScene(size, resize)
|
|
345
|
+
const resized = () => {
|
|
346
|
+
try {
|
|
347
|
+
resize(size())
|
|
348
|
+
} catch (error) {
|
|
349
|
+
try {
|
|
350
|
+
fail(ownerRoot, error)
|
|
351
|
+
} catch (failure) {
|
|
352
|
+
ownerRoot.error(failure)
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
const ownerRoot = owner!._root
|
|
357
|
+
view.addEventListener(`resize`, resized)
|
|
358
|
+
onCleanup(() => {
|
|
359
|
+
view.removeEventListener(`resize`, resized)
|
|
360
|
+
})
|
|
279
361
|
renderer.mount(() => props.children, scene)
|
|
280
362
|
const scope = owner!
|
|
281
363
|
const root = scope._root
|
|
282
364
|
const events = createThreeEvents(
|
|
283
|
-
canvas
|
|
365
|
+
canvas,
|
|
284
366
|
scene,
|
|
285
367
|
() => props.camera ?? camera,
|
|
286
368
|
(error) => {
|
|
@@ -294,22 +376,31 @@ export function Canvas(props: CanvasProps): SpaElement {
|
|
|
294
376
|
onCleanup(() => {
|
|
295
377
|
events.dispose()
|
|
296
378
|
})
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
const
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
379
|
+
// Scene mounting queues child refs and mount hooks; setup must follow them.
|
|
380
|
+
onMount(() => {
|
|
381
|
+
const cleanup = props.onCreated?.({
|
|
382
|
+
scene,
|
|
383
|
+
camera: props.camera ?? camera,
|
|
384
|
+
renderer: webgl,
|
|
385
|
+
})
|
|
386
|
+
if (cleanup) onCleanup(cleanup)
|
|
387
|
+
let last: number | undefined
|
|
388
|
+
onSceneFrame((time) => {
|
|
389
|
+
const active = props.camera ?? camera
|
|
390
|
+
const delta = last === undefined ? 0 : (time - last) / 1000
|
|
391
|
+
last = time
|
|
392
|
+
if (isPresent(scope))
|
|
393
|
+
props.onFrame?.({
|
|
394
|
+
scene,
|
|
395
|
+
camera: active,
|
|
396
|
+
renderer: webgl,
|
|
397
|
+
delta,
|
|
398
|
+
time: time / 1000,
|
|
399
|
+
})
|
|
400
|
+
events.update()
|
|
401
|
+
webgl.render(scene, active)
|
|
402
|
+
}, true)
|
|
403
|
+
})
|
|
313
404
|
})
|
|
314
405
|
return jsx(`canvas`, {
|
|
315
406
|
ref: (element: HTMLCanvasElement | null) => {
|
|
@@ -318,7 +409,11 @@ export function Canvas(props: CanvasProps): SpaElement {
|
|
|
318
409
|
})
|
|
319
410
|
}
|
|
320
411
|
export const Group = sceneComponent<ObjectProps>(`Group`)
|
|
321
|
-
export const Mesh = sceneComponent<
|
|
412
|
+
export const Mesh = sceneComponent<
|
|
413
|
+
ObjectProps<THREE.Mesh> & {
|
|
414
|
+
args?: ConstructorParameters<typeof THREE.Mesh>
|
|
415
|
+
}
|
|
416
|
+
>(`Mesh`)
|
|
322
417
|
export const PerspectiveCamera = sceneComponent<
|
|
323
418
|
ObjectProps<THREE.PerspectiveCamera> & {
|
|
324
419
|
args?: ConstructorParameters<typeof THREE.PerspectiveCamera>
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"capabilities-_zOwNJ-2.d.ts","names":[],"sources":["../src/presence.ts","../src/core.ts","../src/capabilities.ts"],"mappings":";;;UAIiB;EAChB,UAAU;EACV;;UAEgB;EAChB,QAAQ;EACR;;UAEgB;EAChB;EACA;EACA,YAAY;;UAEH,mBAAmB,GAAG;EAC/B,KAAK,cAAc,OAAO,IAAI;EAC9B,OACC,QAAQ,GACR,cACA,mBACA,eACA,MAAM;EAEP,MAAM,SAAS,MAAM;;UAEL;EAChB,WAAW,GAAG,GACb,QAAQ,QAAQ,MAAM,OACtB,aAAa,QAAQ,GAAG,OAAO,gBAC7B,mBAAmB,GAAG;EACzB,OAAO,OAAO,OAAO,QAAQ;EAC7B,KACC,OAAO,OACP,SAAS,mBACT,YAAY,yBACZ;EAED,OAAO,SAAS;EAChB,MAAM,OAAO,OAAO,SAAS,WAAW;EACxC,OAAO,OAAO,OAAO,MAAM;EAC3B,OAAO,OAAO;EACd,OAAO,OAAO;;KCLH;UAGK,SAAS;MACrB;;KAGO,UAAU,YAAY,OAAO,MAAM;;UAE9B;EAChB,eAAe;EACf,OAAO;;;KAGI,WACT,8EAOS,aACT,SAAS;UACK;EAChB,wBAAwB;EACxB,QAAQ;EACR;EACA,WAAW;;UAEK;EAChB;EACA,SAAS,IAAI;EACb,WAAW,IAAI;EACf,OAAO;EACP,UAAU;EACV,WAAW,IAAI,UAAU;EACzB,UAAU;EACV;;UAEgB,aAAa;EAC7B,YAAY,gBAAgB,OAAO;EACnC,YAAY;EACZ,eAAe,YAAY;EAC3B,QAAQ;EACR;EACA;EACA,QAAQ,IAAI;EACZ,SAAS;EACT,OAAO;EACP,QAAQ;;;iBA+HO,YAAY;iBAGZ,UAAU,SAAS;iBAGnB,QAAQ,KAAK;iBASb;;iBAuDA,UAAU,GAAG,WAC5B,OAAO,cAAc,QAAQ,KAC3B,SAAS,OAAO,IAAI;iBACP,UAAU,GAAG,UAAU,WAAW,WACjD,QAAQ,oBAAoB,GAAG,GAAG,IAClC,KAAK,QAAQ,KACX,SAAS,OAAO,IAAI;iBAcP,UAAU,GACzB,OAAO,cAAc,gBAClB,OAAO,IAAI,OAAO;iBACN,UAAU,GAAG,UAAU,WACtC,QAAQ,oBAAoB,GAAG,SAC/B,KAAK,QAAQ,MACV,OAAO,IAAI,OAAO;iBAQN,eAAe,cAAc,qBAC5C,OAAO,iBAAiB,SAClB,MAAM,WAAW,OAAO,WAAW;iBAK1B,OAAO,UAAU,SAChC,OAAO,iBAAiB,aACrB,SAAS;iBACG,OAAO,UAAU,SAAS,UAAU,WACnD,QAAQ,uBAAuB,UAAU,IACzC,KAAK,QAAQ,MACV,SAAS;UAuBI;EAChB;EACA;EACA;EACA;EACA;EACA;IAAoB,OAAO;IAAS,OAAO;;;iBAE5B,YAAY,UAAU,oBACrC,OAAO,cAAc,KACnB;iBACa,YAAY,UAAU,WAAW,UAAU,oBAC1D,QAAQ,oBAAoB,GAAG,IAC/B,KAAK,QAAQ,KACX;;UC9Wc;EAChB;EACA;EACA,YAAY;EACZ,MAAM;EACN,OAAO,aAAa;EACpB,QAAQ,eAAe,uBAAuB;;UAE9B;EAChB,MAAM,OAAO,0BAA0B;EACvC;;EAEA,OAAO,OAAO,yBAAyB;EACvC;;UAEgB;EAChB;EACA,UAAU,MAAM,OAAO;EACvB,eAAe,WAAW,wBAAwB;;EAElD,OAAO,QAAQ,kBAAkB,QAAQ,QAAQ"}
|