@atom.io/spa 0.0.2 → 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 +63 -10
- package/README.mdx +26 -5
- 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 +2 -2
- package/dist/{astro-shared-DyzbUi6c.d.ts → astro-shared-BkWvVZWZ.d.ts} +2 -2
- package/dist/{astro-shared-DyzbUi6c.d.ts.map → astro-shared-BkWvVZWZ.d.ts.map} +1 -1
- package/dist/astro.d.ts +1 -1
- package/dist/capabilities-BkQ2i0vV.js +63 -0
- package/dist/capabilities-BkQ2i0vV.js.map +1 -0
- package/dist/{capabilities-yC9TsHDI.d.ts → capabilities-CFP-HIuY.d.ts} +54 -1
- package/dist/capabilities-CFP-HIuY.d.ts.map +1 -0
- package/dist/client.min.js +1 -1
- package/dist/{capabilities-BuZUQpiD.js → core-DCmqLZQP.js} +16 -59
- 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 +2 -2
- package/dist/development/capabilities-BkQ2i0vV.js +63 -0
- package/dist/development/capabilities-BkQ2i0vV.js.map +1 -0
- package/dist/development/{capabilities-DhOJmeoJ.js → core-p9vciX9C.js} +16 -59
- package/dist/development/core-p9vciX9C.js.map +1 -0
- package/dist/development/{dom-BKc6V_Kz.js → dom-Ci1XvbVJ.js} +16 -7
- package/dist/development/dom-Ci1XvbVJ.js.map +1 -0
- package/dist/development/index.js +3 -3
- package/dist/development/konva.js +25 -3
- package/dist/development/konva.js.map +1 -1
- package/dist/development/motion.js +392 -8
- package/dist/development/motion.js.map +1 -1
- package/dist/development/renderer.js +23 -10
- package/dist/development/renderer.js.map +1 -1
- package/dist/development/server.js +2 -1
- package/dist/development/server.js.map +1 -1
- package/dist/development/{src-Dh-d6w4y.js → src-DG0xjuS5.js} +2 -2
- package/dist/development/{src-Dh-d6w4y.js.map → src-DG0xjuS5.js.map} +1 -1
- package/dist/development/three.js +24 -10
- package/dist/development/three.js.map +1 -1
- package/dist/{dom-B2rm8o2L.js → dom-dHQM0O7Q.js} +16 -7
- package/dist/dom-dHQM0O7Q.js.map +1 -0
- package/dist/{index-BdwxmSKg.d.ts → index-CJJXcKaw.d.ts} +3 -3
- package/dist/{index-BdwxmSKg.d.ts.map → index-CJJXcKaw.d.ts.map} +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/{jsx-runtime-CZU7F8Qz.d.ts → jsx-runtime-CREZYKDC.d.ts} +2 -2
- package/dist/{jsx-runtime-CZU7F8Qz.d.ts.map → jsx-runtime-CREZYKDC.d.ts.map} +1 -1
- package/dist/jsx-runtime.d.ts +1 -1
- package/dist/konva.d.ts +3 -3
- package/dist/konva.d.ts.map +1 -1
- package/dist/konva.js +25 -3
- package/dist/konva.js.map +1 -1
- package/dist/motion.d.ts +2 -1
- package/dist/motion.d.ts.map +1 -1
- package/dist/motion.js +392 -8
- package/dist/motion.js.map +1 -1
- package/dist/renderer.d.ts +3 -3
- package/dist/renderer.d.ts.map +1 -1
- package/dist/renderer.js +23 -10
- package/dist/renderer.js.map +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +2 -1
- package/dist/server.js.map +1 -1
- package/dist/size.json +12 -12
- package/dist/{src-BZxX4xbU.js → src-DCQa6qxs.js} +2 -2
- package/dist/{src-BZxX4xbU.js.map → src-DCQa6qxs.js.map} +1 -1
- package/dist/three.d.ts +3 -3
- package/dist/three.d.ts.map +1 -1
- package/dist/three.js +24 -10
- package/dist/three.js.map +1 -1
- package/package.json +8 -6
- package/src/astro-application.ts +2 -2
- package/src/capabilities.ts +21 -2
- package/src/core.ts +27 -2
- package/src/dom.ts +35 -4
- package/src/konva.ts +41 -14
- package/src/motion-tween.ts +106 -0
- package/src/motion.ts +122 -9
- package/src/presence.ts +324 -0
- package/src/renderer.ts +51 -14
- package/src/three-events.ts +13 -1
- package/src/three.ts +38 -15
- package/dist/capabilities-BuZUQpiD.js.map +0 -1
- package/dist/capabilities-yC9TsHDI.d.ts.map +0 -1
- package/dist/development/capabilities-DhOJmeoJ.js.map +0 -1
- package/dist/development/dom-BKc6V_Kz.js.map +0 -1
- package/dist/dom-B2rm8o2L.js.map +0 -1
package/src/astro-application.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ReadableToken } from "atom.io"
|
|
2
|
-
import { IMPLICIT, withdraw } from "atom.io/internal"
|
|
2
|
+
import { IMPLICIT, subscribeToStateSubject, withdraw } from "atom.io/internal"
|
|
3
3
|
|
|
4
4
|
import { applicationContext } from "./astro-context.ts"
|
|
5
5
|
|
|
@@ -31,7 +31,7 @@ export function browserApplication(
|
|
|
31
31
|
const state = withdraw(store, token)
|
|
32
32
|
subscriptions.set(
|
|
33
33
|
token.key,
|
|
34
|
-
state
|
|
34
|
+
subscribeToStateSubject(state, `spa:bootstrap`, changed),
|
|
35
35
|
)
|
|
36
36
|
}
|
|
37
37
|
for (const state of [
|
package/src/capabilities.ts
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
|
-
import type { Cleanup, Scope } from "./core.ts"
|
|
1
|
+
import type { Cleanup, Root, Scope } from "./core.ts"
|
|
2
|
+
import type { ExitHandle, PresenceCoordinator } from "./presence.ts"
|
|
2
3
|
|
|
3
4
|
/** Native renderers expose property access without importing capability engines. */
|
|
4
5
|
export interface CapabilityTarget {
|
|
5
6
|
kind: string
|
|
6
7
|
node: unknown
|
|
8
|
+
suspend?(): Cleanup
|
|
7
9
|
read?(key: string): unknown
|
|
8
10
|
write?(key: string, value: unknown): void
|
|
9
11
|
listen?(event: string, listener: () => void): Cleanup
|
|
10
12
|
}
|
|
11
13
|
export interface CapabilityBinding {
|
|
14
|
+
exit?(props: Record<string, unknown>): ExitHandle | undefined
|
|
15
|
+
resume?(): void
|
|
12
16
|
update(props: Record<string, unknown>): void
|
|
13
17
|
dispose(): void
|
|
14
18
|
}
|
|
15
19
|
export interface Capability {
|
|
16
20
|
namespace: string
|
|
21
|
+
presence?(root: Root): PresenceCoordinator
|
|
17
22
|
scheduleFrame?(callback: (time: number) => void): Cleanup
|
|
18
23
|
attach(target: CapabilityTarget): CapabilityBinding
|
|
19
24
|
}
|
|
@@ -41,8 +46,11 @@ export function validateCapabilities(
|
|
|
41
46
|
export function capabilityProps(
|
|
42
47
|
scope: Scope,
|
|
43
48
|
target: CapabilityTarget,
|
|
49
|
+
read?: () => Record<string, any>,
|
|
44
50
|
): (props: Record<string, any>) => Record<string, any> {
|
|
45
51
|
const bindings = new Map<string, CapabilityBinding>()
|
|
52
|
+
|
|
53
|
+
scope._root._presence?.target(scope, target)
|
|
46
54
|
const cleanup: Cleanup = () => {
|
|
47
55
|
const errors: unknown[] = []
|
|
48
56
|
for (const binding of [...bindings.values()].reverse()) {
|
|
@@ -63,7 +71,11 @@ export function capabilityProps(
|
|
|
63
71
|
for (const key of Object.keys(props)) {
|
|
64
72
|
const namespace = capabilityNamespace(key)
|
|
65
73
|
if (!namespace) {
|
|
66
|
-
|
|
74
|
+
const value = props[key]
|
|
75
|
+
native[key] =
|
|
76
|
+
key.startsWith(`on`) && typeof value === `function`
|
|
77
|
+
? (scope._root._presence?.event(scope, value) ?? value)
|
|
78
|
+
: value
|
|
67
79
|
continue
|
|
68
80
|
}
|
|
69
81
|
const group = groups.get(namespace) ?? {}
|
|
@@ -75,12 +87,19 @@ export function capabilityProps(
|
|
|
75
87
|
if (!binding) {
|
|
76
88
|
binding = scope._root._capabilities.get(namespace)!.attach(target)
|
|
77
89
|
bindings.set(namespace, binding)
|
|
90
|
+
scope._root._presence?.bind(
|
|
91
|
+
scope,
|
|
92
|
+
binding,
|
|
93
|
+
read ?? (() => props),
|
|
94
|
+
namespace,
|
|
95
|
+
)
|
|
78
96
|
}
|
|
79
97
|
binding.update(values)
|
|
80
98
|
}
|
|
81
99
|
for (const [namespace, binding] of bindings)
|
|
82
100
|
if (!groups.has(namespace)) {
|
|
83
101
|
bindings.delete(namespace)
|
|
102
|
+
scope._root._presence?.unbind(binding)
|
|
84
103
|
binding.dispose()
|
|
85
104
|
}
|
|
86
105
|
return native
|
package/src/core.ts
CHANGED
|
@@ -31,6 +31,11 @@ import {
|
|
|
31
31
|
} from "atom.io/internal"
|
|
32
32
|
|
|
33
33
|
import type { Capability } from "./capabilities.ts"
|
|
34
|
+
import type {
|
|
35
|
+
ExitParticipant,
|
|
36
|
+
PresenceCoordinator,
|
|
37
|
+
PresenceTarget,
|
|
38
|
+
} from "./presence.ts"
|
|
34
39
|
|
|
35
40
|
export type Cleanup = () => void
|
|
36
41
|
// A callable interface permits the recursive Child union. A type alias does not.
|
|
@@ -61,6 +66,9 @@ export interface RootOptions {
|
|
|
61
66
|
onError?: (error: unknown) => void
|
|
62
67
|
}
|
|
63
68
|
export interface Scope {
|
|
69
|
+
_exiting?: boolean
|
|
70
|
+
_exits?: Set<ExitParticipant>
|
|
71
|
+
_targets?: Set<PresenceTarget>
|
|
64
72
|
_root: Root
|
|
65
73
|
_parent?: Scope | undefined
|
|
66
74
|
_cleanups: Set<Cleanup | Scope>
|
|
@@ -68,6 +76,7 @@ export interface Scope {
|
|
|
68
76
|
_dead: boolean
|
|
69
77
|
}
|
|
70
78
|
export interface Root extends Scope {
|
|
79
|
+
_presence?: PresenceCoordinator
|
|
71
80
|
_capabilities: Map<string, Capability>
|
|
72
81
|
_store: RootStore
|
|
73
82
|
_prefix: string
|
|
@@ -124,6 +133,11 @@ export function dispose(scope: Scope): void {
|
|
|
124
133
|
if (scope._dead) return
|
|
125
134
|
scope._dead = true
|
|
126
135
|
const errors: unknown[] = []
|
|
136
|
+
try {
|
|
137
|
+
scope._root._presence?.forget(scope)
|
|
138
|
+
} catch (error) {
|
|
139
|
+
errors.push(error)
|
|
140
|
+
}
|
|
127
141
|
for (const cleanup of [...scope._cleanups].reverse()) {
|
|
128
142
|
try {
|
|
129
143
|
typeof cleanup === `function` ? cleanup() : dispose(cleanup)
|
|
@@ -142,7 +156,7 @@ export function invalidate(scope: Scope): void {
|
|
|
142
156
|
if (typeof child !== `function`) invalidate(child)
|
|
143
157
|
}
|
|
144
158
|
function enqueue(scope: Scope, run: Cleanup): void {
|
|
145
|
-
if (scope
|
|
159
|
+
if (!isPresent(scope)) return
|
|
146
160
|
const root = scope._root
|
|
147
161
|
if (!root._queue.size)
|
|
148
162
|
queueMicrotask(() => {
|
|
@@ -176,6 +190,11 @@ export function createRoot(options: RootOptions = {}, server = false): Root {
|
|
|
176
190
|
((error) => {
|
|
177
191
|
throw error
|
|
178
192
|
})
|
|
193
|
+
for (const capability of capabilities.values())
|
|
194
|
+
if (capability.presence) {
|
|
195
|
+
if (root._presence) throw new Error(`spa: multiple presence coordinators`)
|
|
196
|
+
root._presence = capability.presence(root)
|
|
197
|
+
}
|
|
179
198
|
root.flush = () => {
|
|
180
199
|
try {
|
|
181
200
|
let passes = 0
|
|
@@ -219,7 +238,7 @@ export function observe<T>(
|
|
|
219
238
|
): void {
|
|
220
239
|
const subscriptions = new Map<Subscribe, Cleanup>()
|
|
221
240
|
const run = () => {
|
|
222
|
-
if (scope
|
|
241
|
+
if (!isPresent(scope)) return
|
|
223
242
|
const used = new Set<Subscribe>()
|
|
224
243
|
const previous = tracking
|
|
225
244
|
tracking = (subscribe) => {
|
|
@@ -394,3 +413,9 @@ export function plain(value: Child): string {
|
|
|
394
413
|
// oxlint-disable-next-line typescript/no-base-to-string -- Valid text children are primitives here; development rejects malformed object children.
|
|
395
414
|
return String(value)
|
|
396
415
|
}
|
|
416
|
+
|
|
417
|
+
export function isPresent(scope: Scope): boolean {
|
|
418
|
+
for (let at: Scope | undefined = scope; at; at = at._parent)
|
|
419
|
+
if (at._dead || at._exiting) return false
|
|
420
|
+
return true
|
|
421
|
+
}
|
package/src/dom.ts
CHANGED
|
@@ -129,6 +129,21 @@ function mount(
|
|
|
129
129
|
comment(`{`, parent, before, owner, cursor)
|
|
130
130
|
let end: Node | null = null
|
|
131
131
|
let records = new Map<unknown, RecordNode>()
|
|
132
|
+
const presence = owner._root._presence?.collection<RecordNode, Child>(
|
|
133
|
+
(record) => record._scope,
|
|
134
|
+
(record, child) =>
|
|
135
|
+
typeof record._value === typeof child &&
|
|
136
|
+
(record._value as VNode | null)?.type === (child as VNode | null)?.type,
|
|
137
|
+
)
|
|
138
|
+
const retire = (
|
|
139
|
+
record: RecordNode,
|
|
140
|
+
key: unknown,
|
|
141
|
+
eligible: boolean,
|
|
142
|
+
index: number,
|
|
143
|
+
) => {
|
|
144
|
+
if (presence) presence.remove(record, key, eligible, index, () => {})
|
|
145
|
+
else dispose(record._scope)
|
|
146
|
+
}
|
|
132
147
|
let initial = cursor
|
|
133
148
|
observe(
|
|
134
149
|
owner,
|
|
@@ -150,9 +165,15 @@ function mount(
|
|
|
150
165
|
(typeof record._value !== typeof child ||
|
|
151
166
|
(record._value as VNode | null)?.type !== vnode?.type)
|
|
152
167
|
) {
|
|
153
|
-
|
|
168
|
+
retire(
|
|
169
|
+
record,
|
|
170
|
+
key,
|
|
171
|
+
child == null || typeof child === `boolean`,
|
|
172
|
+
[...records.keys()].indexOf(key),
|
|
173
|
+
)
|
|
154
174
|
record = undefined
|
|
155
175
|
}
|
|
176
|
+
record ??= presence?.take(key, child)
|
|
156
177
|
if (!record) {
|
|
157
178
|
const childOwner = createScope(owner)
|
|
158
179
|
record = {
|
|
@@ -180,15 +201,20 @@ function mount(
|
|
|
180
201
|
)
|
|
181
202
|
} else {
|
|
182
203
|
record._value = child
|
|
204
|
+
owner._root._presence?.resume(record._scope)
|
|
183
205
|
invalidate(record._scope)
|
|
184
206
|
}
|
|
185
207
|
kept.set(key, record)
|
|
186
208
|
}
|
|
187
209
|
for (const [key, record] of records)
|
|
188
|
-
if (!kept.has(key))
|
|
210
|
+
if (!kept.has(key))
|
|
211
|
+
retire(record, key, true, [...records.keys()].indexOf(key))
|
|
189
212
|
if (!initial) {
|
|
190
213
|
let at = end ?? before
|
|
191
|
-
|
|
214
|
+
const rendered = presence?.merge([...kept.values()]) ?? [
|
|
215
|
+
...kept.values(),
|
|
216
|
+
]
|
|
217
|
+
for (const record of rendered.reverse()) {
|
|
192
218
|
if (record._end.nextSibling !== at) {
|
|
193
219
|
const range = parent.ownerDocument!.createRange()
|
|
194
220
|
range.setStartBefore(record._start)
|
|
@@ -199,6 +225,7 @@ function mount(
|
|
|
199
225
|
}
|
|
200
226
|
if (
|
|
201
227
|
focus?.isConnected &&
|
|
228
|
+
!focus.closest(`[inert]`) &&
|
|
202
229
|
focus !== parent.ownerDocument!.activeElement
|
|
203
230
|
)
|
|
204
231
|
focus.focus({ preventScroll: true })
|
|
@@ -291,7 +318,11 @@ function mount(
|
|
|
291
318
|
import.meta.env.DEV ? `spa: hydration mismatch` : undefined,
|
|
292
319
|
)
|
|
293
320
|
}
|
|
294
|
-
const nativeProps = capabilityProps(
|
|
321
|
+
const nativeProps = capabilityProps(
|
|
322
|
+
owner,
|
|
323
|
+
{ kind: `dom`, node: element },
|
|
324
|
+
() => ({ ...props }),
|
|
325
|
+
)
|
|
295
326
|
let previous: Record<string, any> = {}
|
|
296
327
|
owner._cleanups.add(() => {
|
|
297
328
|
for (const key in previous)
|
package/src/konva.ts
CHANGED
|
@@ -30,6 +30,17 @@ const constructors = {
|
|
|
30
30
|
Star: Konva.Star,
|
|
31
31
|
Transformer: Konva.Transformer,
|
|
32
32
|
}
|
|
33
|
+
function suspendNode(node: Konva.Node): () => void {
|
|
34
|
+
const listening = node.listening()
|
|
35
|
+
const draggable = node.draggable()
|
|
36
|
+
node.listening(false)
|
|
37
|
+
// The public setter clears both pending and active native drag records.
|
|
38
|
+
node.draggable(false)
|
|
39
|
+
return () => {
|
|
40
|
+
node.listening(listening)
|
|
41
|
+
node.draggable(draggable)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
33
44
|
function updateNode(
|
|
34
45
|
node: Konva.Node,
|
|
35
46
|
props: Record<string, any>,
|
|
@@ -50,6 +61,9 @@ const renderer = createSceneRenderer<Konva.Node>({
|
|
|
50
61
|
return {
|
|
51
62
|
kind: `konva`,
|
|
52
63
|
node,
|
|
64
|
+
suspend() {
|
|
65
|
+
return suspendNode(node)
|
|
66
|
+
},
|
|
53
67
|
read: (key) => node.getAttr(key),
|
|
54
68
|
write: (key, value) => {
|
|
55
69
|
node.setAttr(key, value)
|
|
@@ -95,8 +109,14 @@ export function Stage(
|
|
|
95
109
|
): VNode {
|
|
96
110
|
let container: HTMLDivElement | null = null
|
|
97
111
|
onMount(() => {
|
|
112
|
+
// Konva clears its container; preserve SPA's markers in the outer element.
|
|
113
|
+
const surface = container!.ownerDocument.createElement(`div`)
|
|
114
|
+
container!.append(surface)
|
|
115
|
+
onCleanup(() => {
|
|
116
|
+
surface.remove()
|
|
117
|
+
})
|
|
98
118
|
const stage = new Konva.Stage({
|
|
99
|
-
container:
|
|
119
|
+
container: surface,
|
|
100
120
|
width: props.width ?? 300,
|
|
101
121
|
height: props.height ?? 150,
|
|
102
122
|
})
|
|
@@ -104,20 +124,27 @@ export function Stage(
|
|
|
104
124
|
stage.off()
|
|
105
125
|
stage.destroy()
|
|
106
126
|
})
|
|
107
|
-
const nativeProps = capabilityProps(
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
stage
|
|
112
|
-
|
|
113
|
-
stage
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
127
|
+
const nativeProps = capabilityProps(
|
|
128
|
+
owner!,
|
|
129
|
+
{
|
|
130
|
+
kind: `konva`,
|
|
131
|
+
node: stage,
|
|
132
|
+
suspend() {
|
|
133
|
+
return suspendNode(stage)
|
|
134
|
+
},
|
|
135
|
+
listen(event, listener) {
|
|
136
|
+
stage.on(event, listener)
|
|
137
|
+
return () => {
|
|
138
|
+
stage.off(event, listener)
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
read: (key) => stage.getAttr(key),
|
|
142
|
+
write: (key, value) => {
|
|
143
|
+
stage.setAttr(key, value)
|
|
144
|
+
},
|
|
119
145
|
},
|
|
120
|
-
|
|
146
|
+
() => ({ ...props }),
|
|
147
|
+
)
|
|
121
148
|
let previous: Record<string, any> = {}
|
|
122
149
|
bindScene(
|
|
123
150
|
() => ({ ...props }),
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { MotionValue, ValueAnimationTransition } from "motion"
|
|
2
|
+
import { animate, keyframes } from "motion"
|
|
3
|
+
|
|
4
|
+
import type { MotionScalar } from "./motion.ts"
|
|
5
|
+
|
|
6
|
+
const trajectories = new WeakMap<
|
|
7
|
+
MotionValue<MotionScalar>,
|
|
8
|
+
() => { position: number; velocity: number }
|
|
9
|
+
>()
|
|
10
|
+
const scalar = /^(-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?)([a-z%]*)$/i
|
|
11
|
+
const brake = (progress: number) => progress * (2 - progress)
|
|
12
|
+
const returnToTarget = (progress: number) =>
|
|
13
|
+
progress * progress * (3 - 2 * progress)
|
|
14
|
+
|
|
15
|
+
/** Retarget a moving tween without changing its position or velocity at the join. */
|
|
16
|
+
export function animateTween(
|
|
17
|
+
value: MotionValue<MotionScalar>,
|
|
18
|
+
goal: MotionScalar,
|
|
19
|
+
transition: ValueAnimationTransition,
|
|
20
|
+
interrupted: boolean,
|
|
21
|
+
): ReturnType<typeof animate> {
|
|
22
|
+
if (interrupted) value.stop()
|
|
23
|
+
const current = value.get()
|
|
24
|
+
const from =
|
|
25
|
+
typeof current === `number` ? [``, current, ``] : scalar.exec(current)
|
|
26
|
+
const to = typeof goal === `number` ? [``, goal, ``] : scalar.exec(goal)
|
|
27
|
+
const previous = trajectories.get(value)?.()
|
|
28
|
+
const velocity =
|
|
29
|
+
previous && from && Math.abs(previous.position - Number(from[1])) < 0.001
|
|
30
|
+
? previous.velocity
|
|
31
|
+
: value.getVelocity()
|
|
32
|
+
trajectories.delete(value)
|
|
33
|
+
const duration = transition.duration ?? 0.3
|
|
34
|
+
// A duration/ease without an explicit type is also a Motion tween. Springs keep
|
|
35
|
+
// Motion's own velocity-aware integration; discrete values keep native mixing.
|
|
36
|
+
const tween =
|
|
37
|
+
transition.type === `tween` ||
|
|
38
|
+
(transition.type === undefined &&
|
|
39
|
+
(transition.duration !== undefined || transition.ease !== undefined))
|
|
40
|
+
const moving =
|
|
41
|
+
interrupted &&
|
|
42
|
+
tween &&
|
|
43
|
+
from &&
|
|
44
|
+
to &&
|
|
45
|
+
from[2] === to[2] &&
|
|
46
|
+
Number.isFinite(velocity) &&
|
|
47
|
+
velocity !== 0 &&
|
|
48
|
+
duration > 0
|
|
49
|
+
const braking = 0.2
|
|
50
|
+
const turn = from ? Number(from[1]) + (velocity * duration * braking) / 2 : 0
|
|
51
|
+
const waypoint = typeof current === `number` ? turn : `${turn}${from?.[2]}`
|
|
52
|
+
const frames = moving ? [current, waypoint, goal] : [current, goal]
|
|
53
|
+
// Constant deceleration: dx/dt at t=0 is v, and at the waypoint is zero.
|
|
54
|
+
// The return starts and ends at rest. Keyframes also handle goal === current.
|
|
55
|
+
const next = moving
|
|
56
|
+
? {
|
|
57
|
+
...transition,
|
|
58
|
+
type: `tween` as const,
|
|
59
|
+
duration,
|
|
60
|
+
delay: 0,
|
|
61
|
+
times: [0, braking, 1],
|
|
62
|
+
ease: [brake, returnToTarget],
|
|
63
|
+
}
|
|
64
|
+
: transition
|
|
65
|
+
// Values are already resolved. Synchronous numeric tweens also keep cancelled,
|
|
66
|
+
// not-yet-started resolver work from being revived by a timeline read.
|
|
67
|
+
const controls = animate(
|
|
68
|
+
value,
|
|
69
|
+
frames,
|
|
70
|
+
tween && (moving || current !== goal) ? { ...next, isSync: true } : next,
|
|
71
|
+
)
|
|
72
|
+
if (
|
|
73
|
+
tween &&
|
|
74
|
+
(moving || current !== goal) &&
|
|
75
|
+
from &&
|
|
76
|
+
to &&
|
|
77
|
+
from[2] === to[2] &&
|
|
78
|
+
duration > 0
|
|
79
|
+
) {
|
|
80
|
+
const curve = keyframes({
|
|
81
|
+
keyframes: moving
|
|
82
|
+
? [Number(from[1]), turn, Number(to[1])]
|
|
83
|
+
: [Number(from[1]), Number(to[1])],
|
|
84
|
+
duration: duration * 1000,
|
|
85
|
+
times: moving ? [0, braking, 1] : [0, 1],
|
|
86
|
+
ease: next.ease ?? `easeOut`,
|
|
87
|
+
})
|
|
88
|
+
const sample = (at: number) => curve.next(at * 1000).value
|
|
89
|
+
|
|
90
|
+
// Use the tween's own timeline. Frame-estimated MotionValue velocity becomes
|
|
91
|
+
// noisy when stop/resume happen several times within the same render frame.
|
|
92
|
+
trajectories.set(value, () => {
|
|
93
|
+
const at = Math.max(0, Math.min(duration, controls.time))
|
|
94
|
+
const before = Math.max(0, at - 0.0001),
|
|
95
|
+
after = Math.min(duration, at + 0.0001)
|
|
96
|
+
return {
|
|
97
|
+
position: sample(at),
|
|
98
|
+
velocity:
|
|
99
|
+
at <= 0 || at >= duration
|
|
100
|
+
? 0
|
|
101
|
+
: (sample(after) - sample(before)) / (after - before),
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
return controls
|
|
106
|
+
}
|
package/src/motion.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { MotionValue, ValueAnimationTransition } from "motion"
|
|
1
|
+
import type { animate, MotionValue, ValueAnimationTransition } from "motion"
|
|
2
2
|
import {
|
|
3
|
-
animate,
|
|
4
3
|
cancelFrame,
|
|
5
4
|
frame,
|
|
6
5
|
hover,
|
|
@@ -15,6 +14,9 @@ import type {
|
|
|
15
14
|
CapabilityBinding,
|
|
16
15
|
CapabilityTarget,
|
|
17
16
|
} from "./capabilities.ts"
|
|
17
|
+
import { animateTween } from "./motion-tween.ts"
|
|
18
|
+
import type { ExitHandle } from "./presence.ts"
|
|
19
|
+
import { createPresence } from "./presence.ts"
|
|
18
20
|
|
|
19
21
|
export type MotionScalar = number | string
|
|
20
22
|
export type MotionDOMTarget = Partial<
|
|
@@ -78,6 +80,7 @@ export interface MotionTransition {
|
|
|
78
80
|
export interface MotionAttributes<Kind extends string = `dom`> {
|
|
79
81
|
[`motion:initial`]?: MotionTarget<Kind> | false
|
|
80
82
|
[`motion:animate`]?: MotionTarget<Kind>
|
|
83
|
+
[`motion:exit`]?: MotionTarget<Kind>
|
|
81
84
|
[`motion:transition`]?: MotionTransition
|
|
82
85
|
[`motion:whileHover`]?: MotionTarget<Kind>
|
|
83
86
|
[`motion:whileTap`]?: MotionTarget<Kind>
|
|
@@ -108,6 +111,7 @@ const transform =
|
|
|
108
111
|
const allowed = new Set([
|
|
109
112
|
`initial`,
|
|
110
113
|
`animate`,
|
|
114
|
+
`exit`,
|
|
111
115
|
`transition`,
|
|
112
116
|
`whileHover`,
|
|
113
117
|
`whileTap`,
|
|
@@ -140,6 +144,8 @@ function attach(
|
|
|
140
144
|
let tapped = false
|
|
141
145
|
let started = false
|
|
142
146
|
let dead = false
|
|
147
|
+
let exiting = false
|
|
148
|
+
let exitHandle: ExitHandle | undefined
|
|
143
149
|
let gestures: (() => void)[] = []
|
|
144
150
|
const preference =
|
|
145
151
|
options.reducedMotion !== `never` && typeof matchMedia === `function`
|
|
@@ -230,8 +236,8 @@ function attach(
|
|
|
230
236
|
properties.set(key, property)
|
|
231
237
|
return property
|
|
232
238
|
}
|
|
233
|
-
const refresh = () => {
|
|
234
|
-
if (dead) return
|
|
239
|
+
const refresh = (force = false) => {
|
|
240
|
+
if (dead || exiting) return
|
|
235
241
|
const initial = (props[`initial`] ?? {}) as TargetValues | false
|
|
236
242
|
const animateTo = (props[`animate`] ?? {}) as TargetValues
|
|
237
243
|
const hoverTo = (props[`whileHover`] ?? {}) as TargetValues
|
|
@@ -268,14 +274,20 @@ function attach(
|
|
|
268
274
|
goals[key] ??
|
|
269
275
|
(initial === false ? undefined : initial[key]) ??
|
|
270
276
|
property.rest
|
|
271
|
-
if (equal(property.target, goal)) continue
|
|
277
|
+
if (!force && equal(property.target, goal)) continue
|
|
272
278
|
property.target = Array.isArray(goal) ? [...goal] : goal
|
|
273
279
|
const next = Array.isArray(goal) ? goal : [goal]
|
|
274
280
|
if (next.length !== property.values.length)
|
|
275
281
|
throw new Error(`spa/motion: cannot change the shape of ${key}`)
|
|
276
282
|
property.values.forEach((value, index) => {
|
|
277
283
|
if (reduced() || (!started && initial === false)) value.jump(next[index])
|
|
278
|
-
else
|
|
284
|
+
else
|
|
285
|
+
animateTween(
|
|
286
|
+
value,
|
|
287
|
+
next[index],
|
|
288
|
+
transition,
|
|
289
|
+
force || value.isAnimating(),
|
|
290
|
+
)
|
|
279
291
|
})
|
|
280
292
|
}
|
|
281
293
|
started = true
|
|
@@ -351,8 +363,107 @@ function attach(
|
|
|
351
363
|
}
|
|
352
364
|
}
|
|
353
365
|
}
|
|
354
|
-
|
|
366
|
+
const finishExit = () => {
|
|
367
|
+
exitHandle?.cancel()
|
|
368
|
+
}
|
|
369
|
+
const mediaChanged = () => {
|
|
370
|
+
preferenceChanged()
|
|
371
|
+
if (reduced() && exiting) finishExit()
|
|
372
|
+
}
|
|
373
|
+
preference?.addEventListener(`change`, mediaChanged)
|
|
355
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
|
+
},
|
|
356
467
|
update(next) {
|
|
357
468
|
if (dead) return
|
|
358
469
|
for (const key of Object.keys(next))
|
|
@@ -363,14 +474,15 @@ function attach(
|
|
|
363
474
|
Boolean(next[`whileHover`]) !== Boolean(props[`whileHover`]) ||
|
|
364
475
|
Boolean(next[`whileTap`]) !== Boolean(props[`whileTap`])
|
|
365
476
|
props = next
|
|
366
|
-
if (gestureChange) installGestures()
|
|
477
|
+
if (gestureChange && !exiting) installGestures()
|
|
367
478
|
refresh()
|
|
368
479
|
},
|
|
369
480
|
dispose() {
|
|
370
481
|
if (dead) return
|
|
371
482
|
dead = true
|
|
372
483
|
clearGestures()
|
|
373
|
-
|
|
484
|
+
exitHandle?.cancel()
|
|
485
|
+
preference?.removeEventListener(`change`, mediaChanged)
|
|
374
486
|
styles?.()
|
|
375
487
|
for (const [key, property] of properties) {
|
|
376
488
|
property.release()
|
|
@@ -392,6 +504,7 @@ export function motionCapability(
|
|
|
392
504
|
): Capability {
|
|
393
505
|
return {
|
|
394
506
|
namespace: `motion`,
|
|
507
|
+
presence: createPresence,
|
|
395
508
|
attach: (target) => attach(target, options),
|
|
396
509
|
scheduleFrame(callback) {
|
|
397
510
|
const run = ({ timestamp }: { timestamp: number }) => {
|