@getcortexlabs/mavis-core 0.1.0-alpha.3 → 0.1.0-alpha.5

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.
Files changed (242) hide show
  1. package/README.md +2 -2
  2. package/dist/accordion.d.ts +17 -0
  3. package/dist/accordion.d.ts.map +1 -0
  4. package/dist/accordion.js +42 -0
  5. package/dist/accordion.js.map +1 -0
  6. package/dist/alert.d.ts +21 -0
  7. package/dist/alert.d.ts.map +1 -0
  8. package/dist/alert.js +13 -0
  9. package/dist/alert.js.map +1 -0
  10. package/dist/avatar.d.ts +10 -0
  11. package/dist/avatar.d.ts.map +1 -0
  12. package/dist/avatar.js +11 -0
  13. package/dist/avatar.js.map +1 -0
  14. package/dist/badge.d.ts +7 -0
  15. package/dist/badge.d.ts.map +1 -0
  16. package/dist/badge.js +11 -0
  17. package/dist/badge.js.map +1 -0
  18. package/dist/capabilities.d.ts +119 -0
  19. package/dist/capabilities.d.ts.map +1 -0
  20. package/dist/capabilities.js +142 -0
  21. package/dist/capabilities.js.map +1 -0
  22. package/dist/chip.d.ts +9 -0
  23. package/dist/chip.d.ts.map +1 -0
  24. package/dist/chip.js +16 -0
  25. package/dist/chip.js.map +1 -0
  26. package/dist/choice.d.ts +15 -0
  27. package/dist/choice.d.ts.map +1 -0
  28. package/dist/choice.js +27 -0
  29. package/dist/choice.js.map +1 -0
  30. package/dist/combobox.d.ts +42 -0
  31. package/dist/combobox.d.ts.map +1 -0
  32. package/dist/combobox.js +159 -0
  33. package/dist/combobox.js.map +1 -0
  34. package/dist/contracts.d.ts +1201 -0
  35. package/dist/contracts.d.ts.map +1 -0
  36. package/dist/contracts.js +242 -0
  37. package/dist/contracts.js.map +1 -0
  38. package/dist/date.d.ts +96 -0
  39. package/dist/date.d.ts.map +1 -0
  40. package/dist/date.js +235 -0
  41. package/dist/date.js.map +1 -0
  42. package/dist/dialog.d.ts +4 -0
  43. package/dist/dialog.d.ts.map +1 -0
  44. package/dist/dialog.js +20 -0
  45. package/dist/dialog.js.map +1 -0
  46. package/dist/empty-state.d.ts +2 -0
  47. package/dist/empty-state.d.ts.map +1 -0
  48. package/dist/empty-state.js +5 -0
  49. package/dist/empty-state.js.map +1 -0
  50. package/dist/experimental-regions.d.ts +142 -0
  51. package/dist/experimental-regions.d.ts.map +1 -0
  52. package/dist/experimental-regions.js +315 -0
  53. package/dist/experimental-regions.js.map +1 -0
  54. package/dist/experimental.d.ts +134 -0
  55. package/dist/experimental.d.ts.map +1 -0
  56. package/dist/experimental.js +502 -0
  57. package/dist/experimental.js.map +1 -0
  58. package/dist/files.d.ts +87 -0
  59. package/dist/files.d.ts.map +1 -0
  60. package/dist/files.js +328 -0
  61. package/dist/files.js.map +1 -0
  62. package/dist/form.d.ts +39 -0
  63. package/dist/form.d.ts.map +1 -0
  64. package/dist/form.js +170 -0
  65. package/dist/form.js.map +1 -0
  66. package/dist/index.d.ts +38 -4
  67. package/dist/index.d.ts.map +1 -1
  68. package/dist/index.js +23 -1
  69. package/dist/index.js.map +1 -1
  70. package/dist/input.d.ts +10 -0
  71. package/dist/input.d.ts.map +1 -1
  72. package/dist/input.js +12 -0
  73. package/dist/input.js.map +1 -1
  74. package/dist/list-item.d.ts +3 -0
  75. package/dist/list-item.d.ts.map +1 -0
  76. package/dist/list-item.js +7 -0
  77. package/dist/list-item.js.map +1 -0
  78. package/dist/motion.d.ts +33 -0
  79. package/dist/motion.d.ts.map +1 -0
  80. package/dist/motion.js +21 -0
  81. package/dist/motion.js.map +1 -0
  82. package/dist/navigation.d.ts +22 -0
  83. package/dist/navigation.d.ts.map +1 -0
  84. package/dist/navigation.js +54 -0
  85. package/dist/navigation.js.map +1 -0
  86. package/dist/progress.d.ts +19 -0
  87. package/dist/progress.d.ts.map +1 -0
  88. package/dist/progress.js +19 -0
  89. package/dist/progress.js.map +1 -0
  90. package/dist/segmented.d.ts +11 -0
  91. package/dist/segmented.d.ts.map +1 -0
  92. package/dist/segmented.js +30 -0
  93. package/dist/segmented.js.map +1 -0
  94. package/dist/sheet.d.ts +8 -1
  95. package/dist/sheet.d.ts.map +1 -1
  96. package/dist/sheet.js +4 -3
  97. package/dist/sheet.js.map +1 -1
  98. package/dist/tabs.d.ts +11 -0
  99. package/dist/tabs.d.ts.map +1 -0
  100. package/dist/tabs.js +28 -0
  101. package/dist/tabs.js.map +1 -0
  102. package/dist/theme/themes.d.ts.map +1 -1
  103. package/dist/theme/themes.js +84 -11
  104. package/dist/theme/themes.js.map +1 -1
  105. package/dist/theme/types.d.ts +52 -1
  106. package/dist/theme/types.d.ts.map +1 -1
  107. package/dist/theme/typography.d.ts +10 -0
  108. package/dist/theme/typography.d.ts.map +1 -0
  109. package/dist/theme/typography.js +90 -0
  110. package/dist/theme/typography.js.map +1 -0
  111. package/dist/toast.d.ts +43 -0
  112. package/dist/toast.d.ts.map +1 -0
  113. package/dist/toast.js +228 -0
  114. package/dist/toast.js.map +1 -0
  115. package/dist/vue/button-content.d.ts +10 -0
  116. package/dist/vue/button-content.d.ts.map +1 -0
  117. package/dist/vue/button-content.js +54 -0
  118. package/dist/vue/button-content.js.map +1 -0
  119. package/dist/vue/cached-activity.d.ts +3 -0
  120. package/dist/vue/cached-activity.d.ts.map +1 -0
  121. package/dist/vue/cached-activity.js +13 -0
  122. package/dist/vue/cached-activity.js.map +1 -0
  123. package/dist/vue/combobox.d.ts +155 -0
  124. package/dist/vue/combobox.d.ts.map +1 -0
  125. package/dist/vue/combobox.js +195 -0
  126. package/dist/vue/combobox.js.map +1 -0
  127. package/dist/vue/component-props.d.ts +1989 -0
  128. package/dist/vue/component-props.d.ts.map +1 -0
  129. package/dist/vue/component-props.js +18 -0
  130. package/dist/vue/component-props.js.map +1 -0
  131. package/dist/vue/contracts.d.ts +18 -0
  132. package/dist/vue/contracts.d.ts.map +1 -0
  133. package/dist/vue/contracts.js +18 -0
  134. package/dist/vue/contracts.js.map +1 -0
  135. package/dist/vue/date-calendar.d.ts +64 -0
  136. package/dist/vue/date-calendar.d.ts.map +1 -0
  137. package/dist/vue/date-calendar.js +65 -0
  138. package/dist/vue/date-calendar.js.map +1 -0
  139. package/dist/vue/date-picker.d.ts +927 -0
  140. package/dist/vue/date-picker.d.ts.map +1 -0
  141. package/dist/vue/date-picker.js +319 -0
  142. package/dist/vue/date-picker.js.map +1 -0
  143. package/dist/vue/date-props.d.ts +254 -0
  144. package/dist/vue/date-props.d.ts.map +1 -0
  145. package/dist/vue/date-props.js +74 -0
  146. package/dist/vue/date-props.js.map +1 -0
  147. package/dist/vue/dialog.d.ts +21 -0
  148. package/dist/vue/dialog.d.ts.map +1 -0
  149. package/dist/vue/dialog.js +58 -0
  150. package/dist/vue/dialog.js.map +1 -0
  151. package/dist/vue/files.d.ts +53 -0
  152. package/dist/vue/files.d.ts.map +1 -0
  153. package/dist/vue/files.js +40 -0
  154. package/dist/vue/files.js.map +1 -0
  155. package/dist/vue/forms.d.ts +4 -0
  156. package/dist/vue/forms.d.ts.map +1 -0
  157. package/dist/vue/forms.js +15 -0
  158. package/dist/vue/forms.js.map +1 -0
  159. package/dist/vue/global-components.d.ts +129 -3
  160. package/dist/vue/global-components.d.ts.map +1 -1
  161. package/dist/vue/index.d.ts +28 -2
  162. package/dist/vue/index.d.ts.map +1 -1
  163. package/dist/vue/index.js +24 -1
  164. package/dist/vue/index.js.map +1 -1
  165. package/dist/vue/inline-content.d.ts +54 -0
  166. package/dist/vue/inline-content.d.ts.map +1 -0
  167. package/dist/vue/inline-content.js +64 -0
  168. package/dist/vue/inline-content.js.map +1 -0
  169. package/dist/vue/props.d.ts +2654 -637
  170. package/dist/vue/props.d.ts.map +1 -1
  171. package/dist/vue/props.js +228 -86
  172. package/dist/vue/props.js.map +1 -1
  173. package/dist/vue/theme-provider.d.ts +58 -0
  174. package/dist/vue/theme-provider.d.ts.map +1 -0
  175. package/dist/vue/theme-provider.js +69 -0
  176. package/dist/vue/theme-provider.js.map +1 -0
  177. package/dist/vue/theme.d.ts +962 -12
  178. package/dist/vue/theme.d.ts.map +1 -1
  179. package/dist/vue/theme.js +17 -7
  180. package/dist/vue/theme.js.map +1 -1
  181. package/dist/vue/toast.d.ts +29 -0
  182. package/dist/vue/toast.d.ts.map +1 -0
  183. package/dist/vue/toast.js +79 -0
  184. package/dist/vue/toast.js.map +1 -0
  185. package/dist/vue/workflow-props.d.ts +110 -0
  186. package/dist/vue/workflow-props.d.ts.map +1 -0
  187. package/dist/vue/workflow-props.js +37 -0
  188. package/dist/vue/workflow-props.js.map +1 -0
  189. package/dist/vue/workflows.d.ts +267 -0
  190. package/dist/vue/workflows.d.ts.map +1 -0
  191. package/dist/vue/workflows.js +200 -0
  192. package/dist/vue/workflows.js.map +1 -0
  193. package/package.json +5 -1
  194. package/src/accordion.ts +66 -0
  195. package/src/alert.ts +16 -0
  196. package/src/avatar.ts +11 -0
  197. package/src/badge.ts +11 -0
  198. package/src/capabilities.ts +145 -0
  199. package/src/chip.ts +20 -0
  200. package/src/choice.ts +41 -0
  201. package/src/combobox.ts +198 -0
  202. package/src/contracts.ts +295 -0
  203. package/src/date.ts +280 -0
  204. package/src/dialog.ts +20 -0
  205. package/src/empty-state.ts +4 -0
  206. package/src/experimental-regions.ts +463 -0
  207. package/src/experimental.ts +643 -0
  208. package/src/files.ts +408 -0
  209. package/src/form.ts +190 -0
  210. package/src/index.ts +64 -3
  211. package/src/input.ts +22 -0
  212. package/src/list-item.ts +5 -0
  213. package/src/motion.ts +22 -0
  214. package/src/navigation.ts +61 -0
  215. package/src/progress.ts +22 -0
  216. package/src/segmented.ts +39 -0
  217. package/src/sheet.ts +6 -3
  218. package/src/tabs.ts +35 -0
  219. package/src/theme/themes.ts +88 -11
  220. package/src/theme/types.ts +42 -1
  221. package/src/theme/typography.ts +84 -0
  222. package/src/toast.ts +249 -0
  223. package/src/vue/button-content.ts +60 -0
  224. package/src/vue/cached-activity.ts +13 -0
  225. package/src/vue/combobox.ts +238 -0
  226. package/src/vue/component-props.ts +24 -0
  227. package/src/vue/contracts.ts +33 -0
  228. package/src/vue/date-calendar.ts +76 -0
  229. package/src/vue/date-picker.ts +387 -0
  230. package/src/vue/date-props.ts +81 -0
  231. package/src/vue/dialog.ts +63 -0
  232. package/src/vue/files.ts +53 -0
  233. package/src/vue/forms.ts +20 -0
  234. package/src/vue/global-components.ts +120 -3
  235. package/src/vue/index.ts +46 -2
  236. package/src/vue/inline-content.ts +71 -0
  237. package/src/vue/props.ts +258 -95
  238. package/src/vue/theme-provider.ts +74 -0
  239. package/src/vue/theme.ts +17 -6
  240. package/src/vue/toast.ts +99 -0
  241. package/src/vue/workflow-props.ts +41 -0
  242. package/src/vue/workflows.ts +257 -0
@@ -0,0 +1,643 @@
1
+ /** Experimental, single-surface motion contract. No platform objects cross this boundary. */
2
+ import {
3
+ regionState,
4
+ validatePointer,
5
+ type MotionRegion,
6
+ type MotionRegionAdapter,
7
+ type MotionPointerOptions,
8
+ type MotionPointer,
9
+ type MotionPointerAdapter,
10
+ } from './experimental-regions.js'
11
+ export * from './experimental-regions.js'
12
+ export type MotionValues = Partial<
13
+ Record<'scale' | 'scaleX' | 'scaleY' | 'x' | 'y' | 'rotate' | 'opacity', number>
14
+ >
15
+ export type MotionFrames = { [K in keyof MotionValues]?: number | readonly (number | null)[] }
16
+ export interface MotionOptions {
17
+ type?: 'spring' | 'tween'
18
+ duration?: number
19
+ bounce?: number
20
+ ease?: 'linear' | 'easeIn' | 'easeOut' | 'easeInOut'
21
+ delay?: number
22
+ }
23
+ export interface MotionPlayback {
24
+ readonly finished: Promise<boolean>
25
+ stop(): void
26
+ }
27
+ export interface MotionSurface {
28
+ readonly available: boolean
29
+ animate(values: MotionFrames, options?: MotionOptions): MotionPlayback
30
+ set(values: MotionValues): MotionPlayback
31
+ /** Runs once per mount/load. Its resources belong to that presentation. */
32
+ onAvailable(setup: () => void | (() => void)): () => void
33
+ /** A child owner, recreated on mount/load. Late work must use the supplied child. */
34
+ use(setup: (motion: MotionSurface) => void | (() => void)): () => void
35
+ pointerFollow(region: MotionRegion, options?: MotionPointerOptions): MotionPointer
36
+ motionValue(initial: number): MotionValue
37
+ springValue(source: MotionValue, options?: MotionSpringOptions): MotionSpring
38
+ bind(values: MotionBindings): MotionBinding
39
+ drag(options: MotionDragOptions): MotionDrag
40
+ /** Re-read a supported size/pivot change without replacing this presentation. */
41
+ refresh(): void
42
+ scrollLinked(
43
+ source: MotionScrollSource,
44
+ frames: MotionScrollFrames,
45
+ options: MotionScrollOptions,
46
+ ): MotionScrollLink
47
+ }
48
+ export interface MotionBinding {
49
+ readonly disposed: boolean
50
+ dispose(): void
51
+ }
52
+ export interface MotionReadable extends MotionBinding {
53
+ get(): number
54
+ to(
55
+ input: readonly number[],
56
+ output: readonly number[],
57
+ options?: { clamp?: boolean },
58
+ ): MotionReadable
59
+ }
60
+ export interface MotionValue extends MotionReadable {
61
+ set(value: number): void
62
+ }
63
+ export interface MotionSpring extends MotionReadable {
64
+ readonly moving: boolean
65
+ readonly paused: boolean
66
+ getVelocity(): number
67
+ pause(): void
68
+ resume(): void
69
+ }
70
+ export interface MotionSpringOptions {
71
+ stiffness?: number
72
+ damping?: number
73
+ mass?: number
74
+ initial?: number
75
+ restDelta?: number
76
+ restSpeed?: number
77
+ }
78
+ export type MotionBindings = Partial<Record<keyof MotionValues, MotionReadable>>
79
+ export interface MotionDragOptions {
80
+ axis: 'x' | 'y'
81
+ bounds?: { left?: number; right?: number; top?: number; bottom?: number }
82
+ snap: { x?: readonly number[]; y?: readonly number[] }
83
+ }
84
+ export interface MotionDrag extends MotionBinding {
85
+ readonly dragging: boolean
86
+ pause(): void
87
+ resume(): void
88
+ cancel(): void
89
+ }
90
+
91
+ /** Renderer contracts. Values never expose their DOM/native target. */
92
+ export type MotionValueAdapter = Pick<MotionValue, 'get' | 'set' | 'dispose'>
93
+ export type MotionSpringAdapter = Omit<MotionSpring, 'disposed' | 'to'>
94
+ export type MotionDragAdapter = Omit<MotionDrag, 'disposed'>
95
+ export interface MotionSurfaceAdapter {
96
+ /** The renderer borrows its host runtime and owns only this child's resources. */
97
+ use?(setup: (adapter: MotionSurfaceAdapter) => () => void): () => void
98
+ pointerFollow?(region: MotionRegionAdapter, options: MotionPointerOptions): MotionPointerAdapter
99
+ animate(values: MotionFrames, options?: MotionOptions): MotionPlayback
100
+ set(values: MotionValues): MotionPlayback
101
+ motionValue?(initial: number): MotionValueAdapter
102
+ springValue?(source: MotionValueAdapter, options?: MotionSpringOptions): MotionSpringAdapter
103
+ bind?(values: Partial<Record<keyof MotionValues, Pick<MotionReadable, 'get' | 'dispose'>>>): {
104
+ dispose(): void
105
+ }
106
+ drag?(options: MotionDragOptions): MotionDragAdapter
107
+ refresh?(): void
108
+ scrollLinked?(
109
+ source: MotionScrollAdapter,
110
+ frames: MotionScrollFrames,
111
+ options: MotionScrollOptions,
112
+ ): Omit<MotionScrollLink, 'disposed'>
113
+ map?(
114
+ source: Pick<MotionReadable, 'get' | 'dispose'>,
115
+ input: readonly number[],
116
+ output: readonly number[],
117
+ options?: { clamp?: boolean },
118
+ ): Pick<MotionReadable, 'get' | 'dispose'>
119
+ }
120
+
121
+ /** CSS pixels on Web, device-independent pixels on NativeScript. Vertical only. */
122
+ export type MotionScrollFrames = Partial<Record<keyof MotionValues, readonly number[]>>
123
+ export interface MotionScrollOptions {
124
+ range: readonly [number, number]
125
+ execution?: 'auto' | 'scheduler'
126
+ }
127
+ export interface MotionScrollLink extends MotionBinding {
128
+ readonly execution: 'scroll-timeline' | 'scheduler' | 'native' | 'released'
129
+ refresh(): void
130
+ }
131
+ export interface MotionScrollSource {
132
+ readonly available: boolean
133
+ onAvailable(setup: () => void | (() => void)): () => void
134
+ /** Re-read deep overflow changes not reported by layout/font observers. */
135
+ refresh(): void
136
+ }
137
+ /** Renderer identity only. The actual scrolling node stays in the renderer's WeakMap. */
138
+ export interface MotionScrollAdapter {
139
+ readonly kind: 'web' | 'native'
140
+ }
141
+ type ScrollState = {
142
+ adapter: MotionScrollAdapter
143
+ links: Set<MotionScrollLink>
144
+ cleanup: Set<() => void>
145
+ }
146
+ const scrollSources = new WeakMap<MotionScrollSource, ScrollState | undefined>()
147
+ const scrollSetups = new WeakMap<MotionScrollSource, Set<() => void | (() => void)>>()
148
+ export function motionScrollAdapter(source: MotionScrollSource): MotionScrollAdapter {
149
+ const state = scrollSources.get(source)
150
+ if (!state) throw new Error('Scroll source is unavailable.')
151
+ return state.adapter
152
+ }
153
+
154
+ export function createMotionScrollSource(): MotionScrollSource {
155
+ const source: MotionScrollSource = {
156
+ get available() {
157
+ return Boolean(scrollSources.get(source))
158
+ },
159
+ refresh() {
160
+ const state = scrollSources.get(source)
161
+ if (!state) throw new Error('Scroll source is unavailable. Mount a scrolling MScreen first.')
162
+ for (const link of state.links) if (!link.disposed) link.refresh()
163
+ },
164
+ onAvailable(setup) {
165
+ let cleanup: (() => void) | undefined
166
+ const invoke = () => {
167
+ const result = setup()
168
+ let ended = false
169
+ cleanup = () => {
170
+ if (ended) return
171
+ ended = true
172
+ result?.()
173
+ }
174
+ return cleanup
175
+ }
176
+ scrollSetups.get(source)!.add(invoke)
177
+ const state = scrollSources.get(source)
178
+ if (state) {
179
+ try {
180
+ state.cleanup.add(invoke())
181
+ } catch (error) {
182
+ scrollSetups.get(source)!.delete(invoke)
183
+ throw error
184
+ }
185
+ }
186
+ return () => {
187
+ scrollSetups.get(source)!.delete(invoke)
188
+ cleanup?.()
189
+ }
190
+ },
191
+ }
192
+ scrollSources.set(source, undefined)
193
+ scrollSetups.set(source, new Set())
194
+ return source
195
+ }
196
+
197
+ export function bindMotionScrollSource(
198
+ source: MotionScrollSource,
199
+ adapter: MotionScrollAdapter,
200
+ ): () => void {
201
+ if (!scrollSources.has(source)) throw new TypeError('Use createMotionScrollSource().')
202
+ const clear = (state: ScrollState) => {
203
+ if (scrollSources.get(source) === state) scrollSources.set(source, undefined)
204
+ for (const link of [...state.links]) link.dispose()
205
+ for (const cleanup of state.cleanup) cleanup()
206
+ state.links.clear()
207
+ state.cleanup.clear()
208
+ }
209
+ const previous = scrollSources.get(source)
210
+ if (previous) clear(previous)
211
+ const state: ScrollState = { adapter, links: new Set(), cleanup: new Set() }
212
+ scrollSources.set(source, state)
213
+ try {
214
+ for (const setup of scrollSetups.get(source)!) {
215
+ const cleanup = setup()
216
+ if (cleanup) state.cleanup.add(cleanup)
217
+ }
218
+ } catch (error) {
219
+ clear(state)
220
+ throw error
221
+ }
222
+ return () => clear(state)
223
+ }
224
+
225
+ function validateScroll(frames: MotionScrollFrames, options: MotionScrollOptions) {
226
+ const range = options?.range
227
+ if (
228
+ !Array.isArray(range) ||
229
+ range.length !== 2 ||
230
+ !range.every(Number.isFinite) ||
231
+ range[0] < 0 ||
232
+ range[1] <= range[0] ||
233
+ range[1] > 1e9
234
+ )
235
+ throw new RangeError('Scroll range must increase between 0 and 1e9 pixels/DIPs.')
236
+ if (options.execution !== undefined && !['auto', 'scheduler'].includes(options.execution))
237
+ throw new TypeError('Unsupported scroll execution.')
238
+ const entries = Object.entries(frames)
239
+ if (
240
+ !entries.length ||
241
+ entries.some(
242
+ ([key, output]) =>
243
+ !['x', 'y', 'rotate', 'scale', 'scaleX', 'scaleY', 'opacity'].includes(key) ||
244
+ !Array.isArray(output) ||
245
+ output.length < 2 ||
246
+ output.length > 1024 ||
247
+ !output.every(Number.isFinite),
248
+ )
249
+ )
250
+ throw new TypeError('Scroll frames need numeric transform/opacity ranges.')
251
+ }
252
+
253
+ type Resource = { dispose(): void }
254
+ type Binding = {
255
+ adapter: MotionSurfaceAdapter
256
+ active: Set<MotionPlayback>
257
+ resources: Set<Resource>
258
+ cleanup: Set<() => void>
259
+ valid: boolean
260
+ context: object
261
+ }
262
+ const bindings = new WeakMap<MotionSurface, Binding | undefined>()
263
+ /** Renderer bridge; the identity has no platform object in it. */
264
+ export function motionSurfaceAdapter(surface: MotionSurface): MotionSurfaceAdapter {
265
+ const binding = bindings.get(surface)
266
+ if (!binding?.valid) throw new Error('Motion surface is unavailable.')
267
+ return binding.adapter
268
+ }
269
+ const setups = new WeakMap<MotionSurface, Set<() => void | (() => void)>>()
270
+ type ValueState = {
271
+ binding: Binding
272
+ raw: Pick<MotionReadable, 'get' | 'dispose'>
273
+ input: boolean
274
+ depth: number
275
+ dependents: Set<Resource>
276
+ }
277
+ const values = new WeakMap<MotionReadable, ValueState>()
278
+ const dependencies = new WeakMap<Resource, Set<Set<Resource>>>()
279
+ function depend(parent: ValueState, resource: Resource) {
280
+ parent.dependents.add(resource)
281
+ let groups = dependencies.get(resource)
282
+ if (!groups) dependencies.set(resource, (groups = new Set()))
283
+ groups.add(parent.dependents)
284
+ }
285
+
286
+ function own<T extends Resource>(binding: Binding, raw: T): T & MotionBinding {
287
+ let disposed = false
288
+ const handle = new Proxy(raw, {
289
+ get(target, key) {
290
+ if (key === 'disposed') return disposed || !binding.valid
291
+ if (key === 'dispose')
292
+ return () => {
293
+ if (disposed) return
294
+ disposed = true
295
+ binding.resources.delete(handle)
296
+ for (const group of dependencies.get(handle) ?? []) group.delete(handle)
297
+ dependencies.delete(handle)
298
+ const state = values.get(handle as unknown as MotionReadable)
299
+ const errors: unknown[] = []
300
+ for (const dependent of state?.dependents ?? []) {
301
+ try {
302
+ dependent.dispose()
303
+ } catch (error) {
304
+ errors.push(error)
305
+ }
306
+ }
307
+ state?.dependents.clear()
308
+ try {
309
+ target.dispose()
310
+ } catch (error) {
311
+ errors.push(error)
312
+ }
313
+ if (errors.length) throw new AggregateError(errors, 'Motion resource cleanup failed.')
314
+ }
315
+ if (disposed || !binding.valid) throw new Error('This motion resource is disposed.')
316
+ const value = Reflect.get(target, key, target)
317
+ return typeof value === 'function'
318
+ ? (...args: unknown[]) => {
319
+ if (disposed || !binding.valid) throw new Error('This motion resource is disposed.')
320
+ return value.apply(target, args)
321
+ }
322
+ : value
323
+ },
324
+ }) as T & MotionBinding
325
+ binding.resources.add(handle)
326
+ return handle
327
+ }
328
+
329
+ function ownValue<T extends Pick<MotionReadable, 'get' | 'dispose'>>(
330
+ binding: Binding,
331
+ raw: T,
332
+ input: boolean,
333
+ parent?: ValueState,
334
+ ): T & MotionReadable {
335
+ const mapped = Object.assign({}, raw, {
336
+ to(input: readonly number[], output: readonly number[], options?: { clamp?: boolean }) {
337
+ if (!binding.adapter.map) throw new Error('Numeric mappings are unavailable.')
338
+ const state = values.get(value)!
339
+ if (state.depth >= 32)
340
+ throw new RangeError('Motion mappings support at most 32 derived levels.')
341
+ if (
342
+ !Array.isArray(input) ||
343
+ !Array.isArray(output) ||
344
+ input.length < 2 ||
345
+ input.length > 1024 ||
346
+ input.length !== output.length ||
347
+ ![...input, ...output].every(Number.isFinite)
348
+ )
349
+ throw new TypeError('A numeric map needs 2–1024 finite input/output pairs.')
350
+ if (
351
+ options &&
352
+ (Object.keys(options).some((key) => key !== 'clamp') ||
353
+ (options.clamp !== undefined && typeof options.clamp !== 'boolean'))
354
+ )
355
+ throw new TypeError('Mapping options support only a boolean clamp.')
356
+ return ownValue(binding, binding.adapter.map(raw, input, output, options), false, state)
357
+ },
358
+ })
359
+ // Preserve getters such as moving/paused; Object.assign would snapshot them.
360
+ const descriptors = Object.getOwnPropertyDescriptors(raw)
361
+ delete (descriptors as Record<string, PropertyDescriptor>).to
362
+ Object.defineProperties(mapped, descriptors)
363
+ const value = own(binding, mapped)
364
+ values.set(value, {
365
+ binding,
366
+ raw,
367
+ input,
368
+ depth: parent ? parent.depth + 1 : 0,
369
+ dependents: new Set(),
370
+ })
371
+ if (parent) depend(parent, value)
372
+ return value
373
+ }
374
+
375
+ export function createMotionSurface(): MotionSurface {
376
+ const current = () => {
377
+ const binding = bindings.get(surface)
378
+ if (!binding?.valid)
379
+ throw new Error('Motion surface is unavailable. Wait for the component to mount/load.')
380
+ return binding
381
+ }
382
+ const source = (value: MotionReadable, binding: Binding, input = false) => {
383
+ const state = values.get(value)
384
+ if (
385
+ !state ||
386
+ state.binding.context !== binding.context ||
387
+ value.disposed ||
388
+ (input && !state.input)
389
+ )
390
+ throw new TypeError('Use a live value from this surface and presentation.')
391
+ return state
392
+ }
393
+ const run = (invoke: (adapter: MotionSurfaceAdapter) => MotionPlayback): MotionPlayback => {
394
+ const binding = current()
395
+ const playback = invoke(binding.adapter)
396
+ binding.active.add(playback)
397
+ void playback.finished.then(() => binding.active.delete(playback))
398
+ return {
399
+ finished: playback.finished,
400
+ stop() {
401
+ if (binding.valid) playback.stop()
402
+ },
403
+ }
404
+ }
405
+ const surface: MotionSurface = {
406
+ get available() {
407
+ return Boolean(bindings.get(surface)?.valid)
408
+ },
409
+ animate: (values, options) => run((adapter) => adapter.animate(values, options)),
410
+ set: (values) => run((adapter) => adapter.set(values)),
411
+ use(setup) {
412
+ return surface.onAvailable(() => {
413
+ const binding = current()
414
+ const start = (adapter: MotionSurfaceAdapter) => {
415
+ const child = createMotionSurface()
416
+ const unbind = bindMotionSurface(child, adapter, binding.context)
417
+ let cleanup: void | (() => void)
418
+ try {
419
+ cleanup = setup(child)
420
+ if (cleanup !== undefined && typeof cleanup !== 'function')
421
+ throw new TypeError('Motion setup must return a cleanup function, not a promise.')
422
+ } catch (error) {
423
+ unbind()
424
+ throw error
425
+ }
426
+ return () => {
427
+ try {
428
+ cleanup?.()
429
+ } finally {
430
+ unbind()
431
+ }
432
+ }
433
+ }
434
+ return binding.adapter.use?.(start) ?? start(binding.adapter)
435
+ })
436
+ },
437
+ pointerFollow(region, options = {}) {
438
+ const binding = current()
439
+ validatePointer(options)
440
+ const state = regionState(region)
441
+ if (!binding.adapter.pointerFollow) throw new Error('Pointer input is unavailable.')
442
+ const raw = binding.adapter.pointerFollow(state.adapter, options)
443
+ const x = ownValue(binding, raw.x, false)
444
+ const y = ownValue(binding, raw.y, false)
445
+ const control = own(binding, {
446
+ x,
447
+ y,
448
+ execution: raw.execution,
449
+ get active() {
450
+ return raw.active
451
+ },
452
+ update: (state: { disabled: boolean }) => raw.update(state),
453
+ refresh: () => raw.refresh(),
454
+ dispose() {
455
+ state.resources.delete(control)
456
+ x.dispose()
457
+ y.dispose()
458
+ raw.dispose()
459
+ },
460
+ })
461
+ state.resources.add(control)
462
+ return control
463
+ },
464
+ onAvailable(setup) {
465
+ let cleanup: (() => void) | undefined
466
+ const invoke = () => {
467
+ const binding = current()
468
+ const before = new Set(binding.resources)
469
+ const active = new Set(binding.active)
470
+ let release: void | (() => void)
471
+ try {
472
+ release = setup()
473
+ } catch (error) {
474
+ for (const resource of [...binding.resources].reverse())
475
+ if (!before.has(resource)) resource.dispose()
476
+ for (const handle of binding.active) if (!active.has(handle)) handle.stop()
477
+ throw error
478
+ }
479
+ const resources = [...binding.resources].filter((resource) => !before.has(resource))
480
+ const playback = [...binding.active].filter((handle) => !active.has(handle))
481
+ let disposed = false
482
+ cleanup = () => {
483
+ if (disposed) return
484
+ disposed = true
485
+ binding.cleanup.delete(cleanup!)
486
+ const errors: unknown[] = []
487
+ for (const dispose of [
488
+ () => release?.(),
489
+ ...resources.reverse().map((resource) => () => resource.dispose()),
490
+ ...playback.map((handle) => () => {
491
+ if (binding.active.has(handle)) handle.stop()
492
+ }),
493
+ ]) {
494
+ try {
495
+ dispose()
496
+ } catch (error) {
497
+ errors.push(error)
498
+ }
499
+ }
500
+ cleanup = undefined
501
+ if (errors.length) throw new AggregateError(errors, 'Motion setup cleanup failed.')
502
+ }
503
+ binding.cleanup.add(cleanup)
504
+ return cleanup
505
+ }
506
+ setups.get(surface)!.add(invoke)
507
+ if (surface.available) {
508
+ try {
509
+ invoke()
510
+ } catch (error) {
511
+ setups.get(surface)!.delete(invoke)
512
+ throw error
513
+ }
514
+ }
515
+ return () => {
516
+ setups.get(surface)!.delete(invoke)
517
+ cleanup?.()
518
+ cleanup = undefined
519
+ }
520
+ },
521
+ motionValue(initial) {
522
+ const binding = current()
523
+ if (!binding.adapter.motionValue) throw new Error('Continuous motion is unavailable.')
524
+ const raw = binding.adapter.motionValue(initial)
525
+ return ownValue(binding, raw, true)
526
+ },
527
+ springValue(input, options) {
528
+ const binding = current()
529
+ const parent = source(input, binding, true)
530
+ if (!binding.adapter.springValue) throw new Error('Continuous motion is unavailable.')
531
+ const raw = binding.adapter.springValue(parent.raw as MotionValueAdapter, options)
532
+ return ownValue(binding, raw, false, parent)
533
+ },
534
+ bind(input) {
535
+ const binding = current()
536
+ const entries = Object.entries(input).map(
537
+ ([key, value]) => [key, source(value, binding)] as const,
538
+ )
539
+ if (!binding.adapter.bind) throw new Error('Continuous motion is unavailable.')
540
+ const handle = own(
541
+ binding,
542
+ binding.adapter.bind(Object.fromEntries(entries.map(([key, state]) => [key, state.raw]))),
543
+ )
544
+ for (const [, state] of entries) depend(state, handle)
545
+ return handle
546
+ },
547
+ scrollLinked(source, frames, options) {
548
+ const binding = current()
549
+ const state = scrollSources.get(source)
550
+ if (!state) throw new Error('Scroll source is unavailable.')
551
+ validateScroll(frames, options)
552
+ if (!binding.adapter.scrollLinked) throw new Error('Scroll motion is unavailable.')
553
+ const raw = binding.adapter.scrollLinked(state.adapter, frames, options)
554
+ const link = own(binding, {
555
+ get execution() {
556
+ return raw.execution
557
+ },
558
+ refresh: () => raw.refresh(),
559
+ dispose() {
560
+ state.links.delete(link)
561
+ raw.dispose()
562
+ },
563
+ })
564
+ state.links.add(link)
565
+ return link
566
+ },
567
+ refresh() {
568
+ current().adapter.refresh?.()
569
+ },
570
+ drag(options) {
571
+ const binding = current()
572
+ if (!binding.adapter.drag) throw new Error('Drag input is unavailable.')
573
+ return own(binding, binding.adapter.drag(options))
574
+ },
575
+ }
576
+ bindings.set(surface, undefined)
577
+ setups.set(surface, new Set())
578
+ return surface
579
+ }
580
+
581
+ /** Renderer-only binding. Replacement invalidates old handles before exposing the new surface. */
582
+ export function bindMotionSurface(
583
+ surface: MotionSurface,
584
+ adapter: MotionSurfaceAdapter,
585
+ context: object = {},
586
+ ): () => void {
587
+ if (!bindings.has(surface)) throw new TypeError('Use createMotionSurface() to declare a surface.')
588
+ const invalidate = (binding: Binding) => {
589
+ binding.valid = false
590
+ const errors: unknown[] = []
591
+ for (const cleanup of [...binding.cleanup].reverse()) {
592
+ try {
593
+ cleanup()
594
+ } catch (error) {
595
+ errors.push(error)
596
+ }
597
+ }
598
+ binding.cleanup.clear()
599
+ for (const playback of binding.active) {
600
+ try {
601
+ playback.stop()
602
+ } catch (error) {
603
+ errors.push(error)
604
+ }
605
+ }
606
+ binding.active.clear()
607
+ for (const resource of [...binding.resources].reverse()) {
608
+ try {
609
+ resource.dispose()
610
+ } catch (error) {
611
+ errors.push(error)
612
+ }
613
+ }
614
+ binding.resources.clear()
615
+ if (errors.length) throw new AggregateError(errors, 'Motion cleanup failed.')
616
+ }
617
+ const previous = bindings.get(surface)
618
+ if (previous) invalidate(previous)
619
+ const binding: Binding = {
620
+ adapter,
621
+ active: new Set(),
622
+ resources: new Set(),
623
+ cleanup: new Set(),
624
+ valid: true,
625
+ context,
626
+ }
627
+ bindings.set(surface, binding)
628
+ try {
629
+ // A setup can register a child while the surface is already available. That
630
+ // child starts immediately; a live Set iteration would run it a second time.
631
+ for (const setup of [...setups.get(surface)!]) {
632
+ setup()
633
+ }
634
+ } catch (error) {
635
+ invalidate(binding)
636
+ throw error
637
+ }
638
+ return () => {
639
+ if (!binding.valid) return
640
+ invalidate(binding)
641
+ if (bindings.get(surface) === binding) bindings.set(surface, undefined)
642
+ }
643
+ }