@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,463 @@
1
+ import {
2
+ motionSurfaceAdapter,
3
+ motionScrollAdapter,
4
+ type MotionSurface,
5
+ type MotionSurfaceAdapter,
6
+ type MotionBinding,
7
+ type MotionReadable,
8
+ type MotionFrames,
9
+ type MotionOptions,
10
+ type MotionSpringOptions,
11
+ type MotionScrollSource,
12
+ type MotionScrollAdapter,
13
+ } from './experimental.js'
14
+
15
+ export interface MotionPointerOptions {
16
+ axis?: 'x' | 'y' | 'both'
17
+ coordinates?: 'normalized' | 'pixels'
18
+ range?: { x?: readonly [number, number]; y?: readonly [number, number] }
19
+ rest?: { x?: number; y?: number }
20
+ spring?: MotionSpringOptions | false
21
+ disabled?: boolean
22
+ }
23
+ export interface MotionPointer extends MotionBinding {
24
+ readonly x: MotionReadable
25
+ readonly y: MotionReadable
26
+ readonly active: boolean
27
+ readonly execution: 'hover' | 'static'
28
+ update(state: { disabled: boolean }): void
29
+ refresh(): void
30
+ }
31
+ export type MotionPointerAdapter = Omit<MotionPointer, 'disposed' | 'x' | 'y'> & {
32
+ x: Pick<MotionReadable, 'get' | 'dispose'>
33
+ y: Pick<MotionReadable, 'get' | 'dispose'>
34
+ }
35
+ export interface MotionViewportOptions {
36
+ frames?: MotionFrames
37
+ transition?: MotionOptions
38
+ stagger?: number
39
+ root?: MotionScrollSource
40
+ /** CSS pixels/DIPs; positive expands the observed viewport. */
41
+ margin?: number
42
+ threshold?: number
43
+ once?: boolean
44
+ disabled?: boolean
45
+ }
46
+ export interface MotionViewport extends MotionBinding {
47
+ readonly execution: 'intersection-observer' | 'native-layout' | 'static'
48
+ readonly pending: number
49
+ readonly completed: number
50
+ update(state: { disabled: boolean }): void
51
+ refresh(): void
52
+ }
53
+ export interface MotionSharedResult {
54
+ status: 'completed' | 'cancelled' | 'unavailable'
55
+ reason?:
56
+ | 'interrupted'
57
+ | 'cancelled'
58
+ | 'unloaded'
59
+ | 'suspended'
60
+ | 'reduced-motion'
61
+ | 'missing-source'
62
+ | 'missing-target'
63
+ | 'unsupported-target'
64
+ | 'layout-timeout'
65
+ | 'geometry-changed'
66
+ }
67
+ export interface MotionSharedPlayback {
68
+ readonly finished: Promise<MotionSharedResult>
69
+ cancel(): void
70
+ stop(): void
71
+ }
72
+ export interface MotionSharedOptions {
73
+ transition?: MotionOptions
74
+ layoutTimeout?: number
75
+ }
76
+ export interface MotionSharedLayout extends MotionBinding {
77
+ readonly active: boolean
78
+ /** Explicit identity; follows replacement of this declared surface. */
79
+ attach(surface: MotionSurface, identity: string): () => void
80
+ /** Host mutation is synchronous; Jolt waits for renderer layout. */
81
+ transition(change: () => void, options?: MotionOptions): MotionSharedPlayback
82
+ cancel(): void
83
+ }
84
+ export interface MotionDiagnosticsOptions {
85
+ development: true
86
+ identity: string
87
+ maxEvents?: number
88
+ }
89
+ export interface MotionDiagnosticSnapshot {
90
+ identity: string
91
+ execution: 'jolt-web' | 'lifecycle-only'
92
+ disposed: boolean
93
+ resources: Record<string, number | boolean>
94
+ targets: {
95
+ id: number
96
+ execution: string
97
+ channels: {
98
+ property: string
99
+ layer?: string
100
+ kind: 'animation' | 'value' | 'rest'
101
+ owner?: number
102
+ paused?: boolean
103
+ }[]
104
+ }[]
105
+ events: {
106
+ sequence: number
107
+ time: number
108
+ reason: string
109
+ owner?: number
110
+ target?: number
111
+ scope?: number
112
+ property?: string
113
+ layer?: string
114
+ }[]
115
+ }
116
+ export interface MotionDiagnostics extends MotionBinding {
117
+ snapshot(): MotionDiagnosticSnapshot
118
+ clear(): void
119
+ }
120
+ type Available = {
121
+ readonly available: boolean
122
+ onAvailable(setup: () => void | (() => void)): () => void
123
+ }
124
+ export interface MotionRegion extends Available {
125
+ viewportEnter(surfaces: readonly MotionSurface[], options?: MotionViewportOptions): MotionViewport
126
+ sharedLayout(options?: MotionSharedOptions): MotionSharedLayout
127
+ diagnostics(options: MotionDiagnosticsOptions): MotionDiagnostics
128
+ }
129
+ export interface MotionRegionAdapter {
130
+ readonly kind: 'web' | 'native'
131
+ viewportEnter(
132
+ surfaces: readonly MotionSurfaceAdapter[],
133
+ options: Omit<MotionViewportOptions, 'root'> & {
134
+ root?: MotionScrollAdapter
135
+ },
136
+ ): Omit<MotionViewport, 'disposed'>
137
+ sharedLayout(options: MotionSharedOptions): Omit<MotionSharedLayout, 'disposed' | 'attach'> & {
138
+ attach(surface: MotionSurfaceAdapter, identity: string): () => void
139
+ }
140
+ diagnostics?(options: MotionDiagnosticsOptions): Omit<MotionDiagnostics, 'disposed'>
141
+ }
142
+ type Resource = { dispose(): void }
143
+ type RegionState = {
144
+ adapter: MotionRegionAdapter
145
+ resources: Set<Resource>
146
+ cleanup: Set<() => void>
147
+ valid: boolean
148
+ }
149
+ const regions = new WeakMap<MotionRegion, { state?: RegionState; setups: Set<() => () => void> }>()
150
+ export function regionState(region: MotionRegion): RegionState {
151
+ const state = regions.get(region)?.state
152
+ if (!state?.valid) throw new Error('Motion region is unavailable. Mount its MBox first.')
153
+ return state
154
+ }
155
+
156
+ /** Subscribe to a fixed set of explicit, potentially replacing surfaces. */
157
+ export function whenMotionAvailable(
158
+ sources: readonly Available[],
159
+ setup: () => void | (() => void),
160
+ ) {
161
+ let cleanup: void | (() => void),
162
+ active = false
163
+ const clear = () => {
164
+ const previous = cleanup
165
+ cleanup = undefined
166
+ active = false
167
+ previous?.()
168
+ }
169
+ const start = () => {
170
+ if (!active && sources.every((source) => source.available)) {
171
+ active = true
172
+ try {
173
+ cleanup = setup()
174
+ } catch (error) {
175
+ active = false
176
+ throw error
177
+ }
178
+ }
179
+ return clear
180
+ }
181
+ const subscriptions: (() => void)[] = []
182
+ try {
183
+ for (const source of new Set(sources)) subscriptions.push(source.onAvailable(start))
184
+ } catch (error) {
185
+ for (const stop of subscriptions) stop()
186
+ clear()
187
+ throw error
188
+ }
189
+ return () => {
190
+ for (const stop of subscriptions) stop()
191
+ clear()
192
+ }
193
+ }
194
+
195
+ function owned<T extends Resource>(state: RegionState, raw: T): T & MotionBinding {
196
+ let disposed = false
197
+ const handle = new Proxy(raw, {
198
+ get(target, key) {
199
+ if (key === 'disposed') return disposed || !state.valid
200
+ if (key === 'dispose')
201
+ return () => {
202
+ if (disposed) return
203
+ disposed = true
204
+ state.resources.delete(handle)
205
+ raw.dispose()
206
+ }
207
+ // Diagnostics retain a final, detached snapshot after teardown.
208
+ if (key !== 'snapshot' && (disposed || !state.valid))
209
+ throw new Error('Motion region resource is disposed.')
210
+ const value = Reflect.get(target, key, target)
211
+ return typeof value === 'function' ? value.bind(target) : value
212
+ },
213
+ }) as T & MotionBinding
214
+ state.resources.add(handle)
215
+ return handle
216
+ }
217
+
218
+ function diagnosticsHandle(
219
+ state: RegionState,
220
+ options: MotionDiagnosticsOptions,
221
+ ): MotionDiagnostics {
222
+ let owner: RegionState | undefined = state
223
+ let raw = state.adapter.diagnostics?.(options)
224
+ let final: MotionDiagnosticSnapshot | undefined
225
+ const read = (): MotionDiagnosticSnapshot =>
226
+ raw?.snapshot() ?? {
227
+ identity: options.identity,
228
+ execution: 'lifecycle-only',
229
+ disposed: !owner,
230
+ resources: { regionResources: owner?.resources.size ?? 0 },
231
+ targets: [],
232
+ events: [],
233
+ }
234
+ const handle: MotionDiagnostics = {
235
+ get disposed() {
236
+ return !owner
237
+ },
238
+ snapshot: () => {
239
+ const snapshot = final ?? read()
240
+ return {
241
+ ...snapshot,
242
+ resources: { ...snapshot.resources },
243
+ targets: snapshot.targets.map((target) => ({
244
+ ...target,
245
+ channels: target.channels.map((channel) => ({ ...channel })),
246
+ })),
247
+ events: snapshot.events.map((event) => ({ ...event })),
248
+ }
249
+ },
250
+ clear() {
251
+ raw?.clear()
252
+ if (final) final.events = []
253
+ },
254
+ dispose() {
255
+ if (!owner) return
256
+ owner.resources.delete(handle)
257
+ owner = undefined
258
+ try {
259
+ raw?.dispose()
260
+ final = { ...read(), disposed: true }
261
+ } finally {
262
+ raw = undefined
263
+ }
264
+ },
265
+ }
266
+ state.resources.add(handle)
267
+ return handle
268
+ }
269
+
270
+ export function createMotionRegion(): MotionRegion {
271
+ const entry: { state?: RegionState; setups: Set<() => () => void> } = { setups: new Set() }
272
+ const region: MotionRegion = {
273
+ get available() {
274
+ return Boolean(entry.state?.valid)
275
+ },
276
+ onAvailable(setup) {
277
+ let cleanup: (() => void) | undefined
278
+ const invoke = () => {
279
+ const state = regionState(region)
280
+ const before = new Set(state.resources)
281
+ let release: void | (() => void)
282
+ try {
283
+ release = setup()
284
+ } catch (error) {
285
+ for (const resource of [...state.resources].reverse())
286
+ if (!before.has(resource)) resource.dispose()
287
+ throw error
288
+ }
289
+ const resources = [...state.resources].filter((value) => !before.has(value))
290
+ let ended = false
291
+ cleanup = () => {
292
+ if (ended) return
293
+ ended = true
294
+ state.cleanup.delete(cleanup!)
295
+ const errors: unknown[] = []
296
+ for (const dispose of [
297
+ () => release?.(),
298
+ ...resources.reverse().map((resource) => () => resource.dispose()),
299
+ ]) {
300
+ try {
301
+ dispose()
302
+ } catch (error) {
303
+ errors.push(error)
304
+ }
305
+ }
306
+ if (errors.length) throw new AggregateError(errors, 'Motion region setup cleanup failed.')
307
+ }
308
+ return cleanup
309
+ }
310
+ entry.setups.add(invoke)
311
+ try {
312
+ if (entry.state?.valid) entry.state.cleanup.add(invoke())
313
+ } catch (error) {
314
+ entry.setups.delete(invoke)
315
+ throw error
316
+ }
317
+ return () => {
318
+ entry.setups.delete(invoke)
319
+ cleanup?.()
320
+ }
321
+ },
322
+ viewportEnter(surfaces, options = {}) {
323
+ const state = regionState(region)
324
+ if (!surfaces.length || surfaces.length > 64 || new Set(surfaces).size !== surfaces.length)
325
+ throw new RangeError('Use 1–64 distinct viewport surfaces in their entrance order.')
326
+ for (const [key, value] of Object.entries({
327
+ margin: options.margin,
328
+ stagger: options.stagger,
329
+ }))
330
+ if (
331
+ value !== undefined &&
332
+ (!Number.isFinite(value) || Math.abs(value) > 10000 || (key === 'stagger' && value < 0))
333
+ )
334
+ throw new RangeError('Viewport margin/stagger must be finite and bounded by 10000.')
335
+ if (
336
+ options.threshold !== undefined &&
337
+ (!Number.isFinite(options.threshold) || options.threshold < 0 || options.threshold > 1)
338
+ )
339
+ throw new RangeError('Viewport threshold must be between zero and one.')
340
+ return owned(
341
+ state,
342
+ state.adapter.viewportEnter(surfaces.map(motionSurfaceAdapter), {
343
+ ...options,
344
+ root: options.root && motionScrollAdapter(options.root),
345
+ }),
346
+ )
347
+ },
348
+ sharedLayout(options = {}) {
349
+ const state = regionState(region)
350
+ const raw = state.adapter.sharedLayout(options)
351
+ const attachments = new Set<() => void>()
352
+ const identities = new Map<string, MotionSurface>()
353
+ return owned<Omit<MotionSharedLayout, 'disposed'>>(state, {
354
+ get active() {
355
+ return raw.active
356
+ },
357
+ attach(surface, identity) {
358
+ if (
359
+ !identity.trim() ||
360
+ identity.length > 100 ||
361
+ identities.has(identity) ||
362
+ [...identities.values()].includes(surface)
363
+ )
364
+ throw new TypeError(
365
+ 'Use a unique nonempty identity and surface (up to 100 characters).',
366
+ )
367
+ identities.set(identity, surface)
368
+ let stop: () => void
369
+ try {
370
+ stop = surface.onAvailable(() => raw.attach(motionSurfaceAdapter(surface), identity))
371
+ } catch (error) {
372
+ identities.delete(identity)
373
+ throw error
374
+ }
375
+ const detach = () => {
376
+ attachments.delete(detach)
377
+ identities.delete(identity)
378
+ stop()
379
+ }
380
+ attachments.add(detach)
381
+ return detach
382
+ },
383
+ transition: (change, options) => raw.transition(change, options),
384
+ cancel: () => raw.cancel(),
385
+ dispose() {
386
+ for (const detach of [...attachments]) detach()
387
+ raw.dispose()
388
+ },
389
+ })
390
+ },
391
+ diagnostics(options) {
392
+ const state = regionState(region)
393
+ if (
394
+ options?.development !== true ||
395
+ !options.identity?.trim() ||
396
+ options.identity.length > 100
397
+ )
398
+ throw new TypeError('Diagnostics need development: true and a short region identity.')
399
+ const max = options.maxEvents ?? 200
400
+ if (!Number.isInteger(max) || max < 1 || max > 2000)
401
+ throw new RangeError('Use 1–2000 diagnostic events.')
402
+ return diagnosticsHandle(state, options)
403
+ },
404
+ }
405
+ regions.set(region, entry)
406
+ return region
407
+ }
408
+
409
+ export function bindMotionRegion(region: MotionRegion, adapter: MotionRegionAdapter): () => void {
410
+ const entry = regions.get(region)
411
+ if (!entry) throw new TypeError('Use createMotionRegion().')
412
+ const clear = (state: RegionState) => {
413
+ if (!state.valid) return
414
+ state.valid = false
415
+ if (entry.state === state) entry.state = undefined
416
+ const errors: unknown[] = []
417
+ for (const release of [
418
+ ...state.cleanup,
419
+ ...[...state.resources].reverse().map((resource) => () => resource.dispose()),
420
+ ])
421
+ try {
422
+ release()
423
+ } catch (error) {
424
+ errors.push(error)
425
+ }
426
+ state.cleanup.clear()
427
+ state.resources.clear()
428
+ if (errors.length) throw new AggregateError(errors, 'Motion region cleanup failed.')
429
+ }
430
+ if (entry.state) clear(entry.state)
431
+ const state: RegionState = { adapter, valid: true, resources: new Set(), cleanup: new Set() }
432
+ entry.state = state
433
+ try {
434
+ for (const setup of [...entry.setups]) state.cleanup.add(setup())
435
+ } catch (error) {
436
+ clear(state)
437
+ throw error
438
+ }
439
+ return () => clear(state)
440
+ }
441
+
442
+ export function validatePointer(options: MotionPointerOptions) {
443
+ if (options.axis !== undefined && !['x', 'y', 'both'].includes(options.axis))
444
+ throw new TypeError('Unknown pointer axis.')
445
+ if (options.coordinates !== undefined && !['normalized', 'pixels'].includes(options.coordinates))
446
+ throw new TypeError('Unknown pointer coordinates.')
447
+ if (options.disabled !== undefined && typeof options.disabled !== 'boolean')
448
+ throw new TypeError('disabled must be boolean.')
449
+ for (const axis of ['x', 'y'] as const) {
450
+ const rest = options.rest?.[axis] ?? 0,
451
+ range = options.range?.[axis]
452
+ if (
453
+ !Number.isFinite(rest) ||
454
+ (range &&
455
+ (range.length !== 2 ||
456
+ !range.every(Number.isFinite) ||
457
+ range[0] >= range[1] ||
458
+ rest < range[0] ||
459
+ rest > range[1]))
460
+ )
461
+ throw new RangeError('Pointer bounds must increase and contain the resting value.')
462
+ }
463
+ }