@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
package/src/files.ts ADDED
@@ -0,0 +1,408 @@
1
+ /** Serializable metadata only. Local handles and previews never enter this descriptor. */
2
+ export interface FileItem {
3
+ readonly id: string
4
+ readonly name: string
5
+ readonly size?: number
6
+ readonly type?: string
7
+ readonly status: 'selected' | 'uploading' | 'uploaded' | 'failed' | 'cancelled'
8
+ /** A fraction from zero to one; absent means the total is unknown. */
9
+ readonly progress?: number
10
+ readonly error?: string
11
+ }
12
+
13
+ /** Platform-owned, session-only identity. Use a platform accessor inside host transport. */
14
+ export interface FileHandle {
15
+ readonly __mavisFileHandle: never
16
+ }
17
+
18
+ export interface FileCancellation {
19
+ readonly cancelled: boolean
20
+ onCancel(callback: () => void): () => void
21
+ }
22
+
23
+ export interface SelectedFile {
24
+ readonly name: string
25
+ readonly size?: number
26
+ readonly type?: string
27
+ readonly handle: FileHandle
28
+ /** Renderer-specific image source, owned by this resource. Failure keeps the file. */
29
+ preview?(): Promise<unknown>
30
+ release(): void
31
+ }
32
+
33
+ export interface FileSelectionRequest {
34
+ readonly multiple: boolean
35
+ readonly accept: readonly string[]
36
+ }
37
+
38
+ export interface FilePickerAdapter {
39
+ /** Must be called directly from user activation, before any asynchronous work. */
40
+ pick(
41
+ request: FileSelectionRequest,
42
+ cancellation: FileCancellation,
43
+ ): Promise<readonly SelectedFile[] | null>
44
+ }
45
+
46
+ export interface FileUploadContext {
47
+ readonly item: FileItem
48
+ readonly handle: FileHandle
49
+ readonly cancellation: FileCancellation
50
+ /** Omit total when unknown; a reported fraction never completes an upload. */
51
+ progress(loaded: number, total?: number): void
52
+ }
53
+ export type FileUpload = (context: FileUploadContext) => Promise<void>
54
+
55
+ export interface FileSelectionOptions {
56
+ /** Metadata from the host, including SSR. Initial entries have no local resource. */
57
+ initial?: readonly FileItem[]
58
+ multiple?: boolean
59
+ /** Extensions (.pdf), exact MIME types, or MIME families (image/*). */
60
+ accept?: readonly string[]
61
+ maxCount?: number
62
+ /** Per-file limit in bytes. Unknown sizes cannot be checked locally. */
63
+ maxSize?: number
64
+ upload?: FileUpload
65
+ }
66
+
67
+ export type FileSelectionErrorCode = 'picker' | 'type' | 'size' | 'count'
68
+ export interface FileSelectionState {
69
+ readonly items: readonly FileItem[]
70
+ readonly picking: boolean
71
+ readonly error?: { readonly code: FileSelectionErrorCode; readonly message: string }
72
+ }
73
+
74
+ export interface FileSelectionController {
75
+ readonly state: FileSelectionState
76
+ readonly canUpload: boolean
77
+ subscribe(listener: (state: FileSelectionState) => void): () => void
78
+ select(picker: FilePickerAdapter): Promise<void>
79
+ cancelSelection(): void
80
+ hasFile(id: string): boolean
81
+ preview(id: string): unknown
82
+ discardPreview(id: string): void
83
+ upload(id: string): Promise<void>
84
+ retry(id: string): Promise<void>
85
+ cancel(id: string): void
86
+ remove(id: string): void
87
+ clear(): void
88
+ /** Cancels active operations, retaining selected files for an explicit retry. */
89
+ pause(): void
90
+ /** Idempotent. Cancels work and releases every locally owned resource. */
91
+ dispose(): void
92
+ }
93
+
94
+ function cancellation() {
95
+ let cancelled = false
96
+ const callbacks = new Set<() => void>()
97
+ const token: FileCancellation = {
98
+ get cancelled() {
99
+ return cancelled
100
+ },
101
+ onCancel(callback) {
102
+ if (cancelled) callback()
103
+ else callbacks.add(callback)
104
+ return () => {
105
+ callbacks.delete(callback)
106
+ }
107
+ },
108
+ }
109
+ return {
110
+ token,
111
+ cancel() {
112
+ if (cancelled) return
113
+ cancelled = true
114
+ const pending = [...callbacks]
115
+ callbacks.clear()
116
+ for (const callback of pending) {
117
+ // A broken host abort callback must not retain the other resources.
118
+ try {
119
+ callback()
120
+ } catch {
121
+ /* cancellation remains authoritative */
122
+ }
123
+ }
124
+ },
125
+ }
126
+ }
127
+
128
+ function release(file: SelectedFile) {
129
+ try {
130
+ file.release()
131
+ } catch {
132
+ /* release the remaining resources as well */
133
+ }
134
+ }
135
+
136
+ export function acceptsFile(file: Pick<FileItem, 'name' | 'type'>, accept: readonly string[]) {
137
+ if (!accept.length) return true
138
+ const name = file.name.toLowerCase()
139
+ const type = file.type?.toLowerCase()
140
+ return accept.some((hint) => {
141
+ const value = hint.toLowerCase()
142
+ if (value.startsWith('.')) return name.endsWith(value)
143
+ if (value.endsWith('/*')) return Boolean(type?.startsWith(value.slice(0, -1)))
144
+ return type === value
145
+ })
146
+ }
147
+
148
+ /** Pure controller: no DOM, native APIs, timers, request globals, or automatic transfers. */
149
+ export function createFileSelection(options: FileSelectionOptions = {}): FileSelectionController {
150
+ const multiple = options.multiple ?? false
151
+ const accept = Object.freeze([...(options.accept ?? [])])
152
+ const maxCount = options.maxCount ?? (multiple ? 10 : 1)
153
+ const maxSize = options.maxSize
154
+ if (!Number.isInteger(maxCount) || maxCount < 1)
155
+ throw new RangeError('File maxCount must be a positive integer')
156
+ if (maxSize !== undefined && (!Number.isFinite(maxSize) || maxSize < 0))
157
+ throw new RangeError('File maxSize must be finite and nonnegative')
158
+ if (accept.some((value) => !/^(\.[\w.-]+|[\w!#$&^.+-]+\/(?:[\w!#$&^.+-]+|\*))$/.test(value)))
159
+ throw new TypeError('File accept must contain extensions or MIME types')
160
+ let nextId = 0
161
+ let disposed = false
162
+ let picking: ReturnType<typeof cancellation> | undefined
163
+ const listeners = new Set<(state: FileSelectionState) => void>()
164
+ const resources = new Map<string, SelectedFile>()
165
+ const previews = new Map<string, unknown>()
166
+ const operations = new Map<string, ReturnType<typeof cancellation>>()
167
+ const ids = new Set<string>()
168
+ const initial = (options.initial ?? []).map((item) => {
169
+ if (!item.id || ids.has(item.id) || !item.name.trim())
170
+ throw new TypeError('File descriptors need unique IDs and readable names')
171
+ if (item.size !== undefined && (!Number.isFinite(item.size) || item.size < 0))
172
+ throw new RangeError('File size must be finite and nonnegative')
173
+ if (!['selected', 'uploading', 'uploaded', 'failed', 'cancelled'].includes(item.status))
174
+ throw new TypeError('Invalid file status')
175
+ if (
176
+ item.progress !== undefined &&
177
+ (!Number.isFinite(item.progress) || item.progress < 0 || item.progress > 1)
178
+ )
179
+ throw new RangeError('File progress must be between zero and one')
180
+ ids.add(item.id)
181
+ // Copy only portable fields; do not retain extra properties from a host object.
182
+ return Object.freeze({
183
+ id: item.id,
184
+ name: item.name,
185
+ size: item.size,
186
+ type: item.type,
187
+ status: item.status,
188
+ progress: item.progress,
189
+ error: item.error,
190
+ })
191
+ })
192
+ if (initial.length > (multiple ? maxCount : 1))
193
+ throw new RangeError('Initial files exceed the selection count')
194
+ let state: FileSelectionState = Object.freeze({ items: Object.freeze(initial), picking: false })
195
+
196
+ function publish(patch: Partial<FileSelectionState> = {}) {
197
+ if (disposed) return
198
+ state = Object.freeze({ ...state, ...patch })
199
+ for (const listener of [...listeners]) listener(state)
200
+ }
201
+ function update(id: string, patch: Partial<FileItem>) {
202
+ publish({
203
+ items: Object.freeze(
204
+ state.items.map((item) => (item.id === id ? Object.freeze({ ...item, ...patch }) : item)),
205
+ ),
206
+ })
207
+ }
208
+ function cancel(id: string) {
209
+ const operation = operations.get(id)
210
+ if (!operation) return
211
+ operations.delete(id)
212
+ operation.cancel()
213
+ update(id, { status: 'cancelled', progress: undefined, error: undefined })
214
+ }
215
+ function forget(id: string) {
216
+ cancel(id)
217
+ const file = resources.get(id)
218
+ resources.delete(id)
219
+ previews.delete(id)
220
+ if (file) release(file)
221
+ }
222
+ function cancelSelection() {
223
+ const previous = picking
224
+ picking = undefined
225
+ previous?.cancel()
226
+ if (previous) publish({ picking: false })
227
+ }
228
+ async function upload(id: string) {
229
+ const file = resources.get(id)
230
+ const item = state.items.find((entry) => entry.id === id)
231
+ if (
232
+ disposed ||
233
+ !file ||
234
+ !item ||
235
+ !options.upload ||
236
+ operations.has(id) ||
237
+ item.status === 'uploaded'
238
+ )
239
+ return
240
+ const operation = cancellation()
241
+ operations.set(id, operation)
242
+ const current = () =>
243
+ !disposed && operations.get(id) === operation && resources.get(id) === file
244
+ update(id, { status: 'uploading', progress: undefined, error: undefined })
245
+ try {
246
+ await options.upload({
247
+ item,
248
+ handle: file.handle,
249
+ cancellation: operation.token,
250
+ progress(loaded, total) {
251
+ if (!current()) return
252
+ if (
253
+ !Number.isFinite(loaded) ||
254
+ loaded < 0 ||
255
+ (total !== undefined && (!Number.isFinite(total) || total <= 0))
256
+ )
257
+ throw new RangeError(
258
+ 'Upload progress needs nonnegative loaded bytes and a positive total',
259
+ )
260
+ update(id, { progress: total === undefined ? undefined : Math.min(1, loaded / total) })
261
+ },
262
+ })
263
+ if (current()) update(id, { status: 'uploaded', progress: 1, error: undefined })
264
+ } catch (error) {
265
+ if (current())
266
+ update(id, {
267
+ status: 'failed',
268
+ error: error instanceof Error ? error.message : 'Upload failed',
269
+ })
270
+ } finally {
271
+ if (operations.get(id) === operation) operations.delete(id)
272
+ }
273
+ }
274
+ const controller: FileSelectionController = {
275
+ get state() {
276
+ return state
277
+ },
278
+ get canUpload() {
279
+ return Boolean(options.upload)
280
+ },
281
+ subscribe(listener) {
282
+ if (!disposed) listeners.add(listener)
283
+ return () => {
284
+ listeners.delete(listener)
285
+ }
286
+ },
287
+ async select(picker) {
288
+ if (disposed) return
289
+ cancelSelection()
290
+ const operation = cancellation()
291
+ picking = operation
292
+ publish({ picking: true, error: undefined })
293
+ let files: readonly SelectedFile[] | null = null
294
+ let retained = false
295
+ try {
296
+ // Keep the actual picker launch in the same user activation stack.
297
+ files = await picker.pick({ multiple, accept }, operation.token)
298
+ if (disposed || picking !== operation || operation.token.cancelled || !files?.length) return
299
+ const count = multiple ? state.items.length + files.length : files.length
300
+ let error: FileSelectionState['error']
301
+ if (count > (multiple ? maxCount : 1))
302
+ error = { code: 'count', message: `Choose at most ${multiple ? maxCount : 1} files` }
303
+ else if (files.some((file) => !file.name.trim() || !acceptsFile(file, accept)))
304
+ error = { code: 'type', message: 'This file type is not accepted' }
305
+ else if (
306
+ files.some(
307
+ (file) =>
308
+ file.size !== undefined &&
309
+ (!Number.isFinite(file.size) ||
310
+ file.size < 0 ||
311
+ (maxSize !== undefined && file.size > maxSize)),
312
+ )
313
+ )
314
+ error = { code: 'size', message: 'A file exceeds the size limit' }
315
+ if (error) {
316
+ publish({ error })
317
+ return
318
+ }
319
+ if (!multiple) for (const item of state.items) forget(item.id)
320
+ const additions = files.map((file) => {
321
+ let id: string
322
+ do {
323
+ id = `file-${++nextId}`
324
+ } while (ids.has(id))
325
+ ids.add(id)
326
+ resources.set(id, file)
327
+ return Object.freeze({
328
+ id,
329
+ name: file.name,
330
+ size: file.size,
331
+ type: file.type,
332
+ status: 'selected' as const,
333
+ })
334
+ })
335
+ retained = true
336
+ publish({ items: Object.freeze(multiple ? [...state.items, ...additions] : additions) })
337
+ for (const item of additions) {
338
+ const file = resources.get(item.id)!
339
+ if (file.preview)
340
+ void Promise.resolve()
341
+ .then(() =>
342
+ !disposed && resources.get(item.id) === file ? file.preview!() : undefined,
343
+ )
344
+ .then((preview) => {
345
+ if (!disposed && resources.get(item.id) === file && preview != null) {
346
+ previews.set(item.id, preview)
347
+ publish()
348
+ }
349
+ })
350
+ .catch(() => {
351
+ /* Preview failure never rejects an otherwise usable file. */
352
+ })
353
+ }
354
+ } catch (error) {
355
+ if (!disposed && picking === operation && !operation.token.cancelled)
356
+ publish({
357
+ error: {
358
+ code: 'picker',
359
+ message: error instanceof Error ? error.message : 'Unable to select files',
360
+ },
361
+ })
362
+ } finally {
363
+ if (!retained && files) for (const file of files) release(file)
364
+ if (picking === operation) {
365
+ picking = undefined
366
+ publish({ picking: false })
367
+ }
368
+ }
369
+ },
370
+ cancelSelection,
371
+ hasFile: (id) => resources.has(id),
372
+ preview: (id) => previews.get(id),
373
+ discardPreview(id) {
374
+ if (previews.delete(id)) publish()
375
+ },
376
+ upload,
377
+ retry: upload,
378
+ cancel,
379
+ remove(id) {
380
+ if (disposed) return
381
+ forget(id)
382
+ publish({
383
+ items: Object.freeze(state.items.filter((item) => item.id !== id)),
384
+ error: undefined,
385
+ })
386
+ },
387
+ clear() {
388
+ cancelSelection()
389
+ for (const item of state.items) forget(item.id)
390
+ publish({ items: Object.freeze([]), error: undefined })
391
+ },
392
+ pause() {
393
+ cancelSelection()
394
+ for (const id of [...operations.keys()]) cancel(id)
395
+ },
396
+ dispose() {
397
+ if (disposed) return
398
+ disposed = true
399
+ cancelSelection()
400
+ for (const id of [...operations.keys()]) cancel(id)
401
+ for (const file of resources.values()) release(file)
402
+ resources.clear()
403
+ previews.clear()
404
+ listeners.clear()
405
+ },
406
+ }
407
+ return Object.freeze(controller)
408
+ }
package/src/form.ts ADDED
@@ -0,0 +1,190 @@
1
+ import type { InputHandle } from './input.js'
2
+
3
+ export interface FormError {
4
+ field: string
5
+ message: string
6
+ }
7
+ export interface FormField {
8
+ name: string
9
+ label: string
10
+ target: () => InputHandle | undefined | null
11
+ enabled?: () => boolean
12
+ /** Open the containing section before resolving its mounted input handle. */
13
+ reveal?: () => void | Promise<void>
14
+ }
15
+ export interface FormValidationContext {
16
+ readonly current: boolean
17
+ onCancel(dispose: () => void): void
18
+ }
19
+ export interface FormState {
20
+ readonly errors: readonly FormError[]
21
+ readonly validating: boolean
22
+ }
23
+ export interface FormController {
24
+ readonly state: FormState
25
+ readonly disposed: boolean
26
+ register(field: FormField): () => void
27
+ label(field: string): string | undefined
28
+ has(field: string): boolean
29
+ setErrors(errors: readonly FormError[]): void
30
+ subscribe(listener: (state: FormState) => void): () => void
31
+ focusError(field: string): Promise<boolean>
32
+ focusFirstError(): Promise<boolean>
33
+ /** Invalidate pending validation when the host draft changes. */
34
+ invalidate(): void
35
+ validate(
36
+ task: (context: FormValidationContext) => Promise<readonly FormError[]>,
37
+ ): Promise<'valid' | 'invalid' | 'cancelled' | 'busy'>
38
+ dispose(): void
39
+ }
40
+
41
+ function errorsCopy(errors: readonly FormError[]) {
42
+ return Object.freeze(
43
+ errors.map((error) => {
44
+ if (!error || !error.field?.trim() || !error.message?.trim())
45
+ throw new TypeError('Form errors require a field name and a non-empty message.')
46
+ return Object.freeze({ field: error.field, message: error.message })
47
+ }),
48
+ )
49
+ }
50
+
51
+ /** Coordinates public focus handles and validation results; never owns the field values. */
52
+ export function createFormController(initial: readonly FormError[] = []): FormController {
53
+ const fields = new Map<string, FormField>()
54
+ const listeners = new Set<(state: FormState) => void>()
55
+ let state: FormState = Object.freeze({ errors: errorsCopy(initial), validating: false })
56
+ let disposed = false
57
+ let generation = 0
58
+ let focusGeneration = 0
59
+ let cancellations = new Set<() => void>()
60
+ const publish = (next = state) => {
61
+ state = Object.freeze(next)
62
+ for (const listener of [...listeners]) listener(state)
63
+ }
64
+ const invalidate = () => {
65
+ generation++
66
+ focusGeneration++
67
+ const release = cancellations
68
+ cancellations = new Set()
69
+ const failures: unknown[] = []
70
+ for (const cancel of release) {
71
+ try {
72
+ cancel()
73
+ } catch (error) {
74
+ failures.push(error)
75
+ }
76
+ }
77
+ if (state.validating) publish({ ...state, validating: false })
78
+ if (failures.length) throw new AggregateError(failures, 'Form validation cancellation failed.')
79
+ }
80
+ const focus = async (name: string, request: number) => {
81
+ const field = fields.get(name)
82
+ if (disposed || !field || field.enabled?.() === false) return false
83
+ await field.reveal?.()
84
+ if (
85
+ disposed ||
86
+ request !== focusGeneration ||
87
+ fields.get(name) !== field ||
88
+ field.enabled?.() === false
89
+ )
90
+ return false
91
+ const target = field.target()
92
+ if (!target) return false
93
+ target.focus()
94
+ return true
95
+ }
96
+ const controller: FormController = {
97
+ get state() {
98
+ return state
99
+ },
100
+ get disposed() {
101
+ return disposed
102
+ },
103
+ register(field) {
104
+ if (disposed) throw new Error('Form controller is disposed.')
105
+ if (!field.name?.trim() || !field.label?.trim() || typeof field.target !== 'function')
106
+ throw new TypeError('Form fields require a name, label and public handle getter.')
107
+ if (fields.has(field.name)) throw new RangeError(`Duplicate form field: ${field.name}`)
108
+ fields.set(field.name, field)
109
+ publish()
110
+ return () => {
111
+ if (fields.get(field.name) !== field) return
112
+ fields.delete(field.name)
113
+ publish()
114
+ }
115
+ },
116
+ label(name) {
117
+ return fields.get(name)?.label
118
+ },
119
+ has(name) {
120
+ return fields.has(name)
121
+ },
122
+ setErrors(errors) {
123
+ if (disposed) return
124
+ const next = errorsCopy(errors)
125
+ invalidate()
126
+ publish({ errors: next, validating: false })
127
+ },
128
+ subscribe(listener) {
129
+ if (disposed) return () => {}
130
+ listeners.add(listener)
131
+ return () => {
132
+ listeners.delete(listener)
133
+ }
134
+ },
135
+ focusError(name) {
136
+ return focus(name, ++focusGeneration)
137
+ },
138
+ async focusFirstError() {
139
+ const request = ++focusGeneration
140
+ for (const error of state.errors) {
141
+ if (request !== focusGeneration || disposed) return false
142
+ if (await focus(error.field, request)) return true
143
+ }
144
+ return false
145
+ },
146
+ invalidate,
147
+ async validate(task) {
148
+ if (disposed) return 'cancelled'
149
+ if (state.validating) return 'busy'
150
+ const request = ++generation
151
+ const release = new Set<() => void>()
152
+ cancellations = release
153
+ const current = () => !disposed && generation === request
154
+ publish({ ...state, validating: true })
155
+ try {
156
+ const result = await task({
157
+ get current() {
158
+ return current()
159
+ },
160
+ onCancel(cancel) {
161
+ if (current()) release.add(cancel)
162
+ else cancel()
163
+ },
164
+ })
165
+ if (!current()) return 'cancelled'
166
+ const errors = errorsCopy(result)
167
+ focusGeneration++
168
+ publish({ errors, validating: false })
169
+ return state.errors.length ? 'invalid' : 'valid'
170
+ } catch (error) {
171
+ if (!current()) return 'cancelled'
172
+ throw error
173
+ } finally {
174
+ release.clear()
175
+ if (current() && state.validating) publish({ ...state, validating: false })
176
+ }
177
+ },
178
+ dispose() {
179
+ if (disposed) return
180
+ disposed = true
181
+ try {
182
+ invalidate()
183
+ } finally {
184
+ fields.clear()
185
+ listeners.clear()
186
+ }
187
+ },
188
+ }
189
+ return controller
190
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,12 @@
1
1
  export { defineTheme, mavis, midnight } from './theme/themes.js'
2
2
  export { resolveSpace } from './theme/resolve.js'
3
+ export { pressMotion, revealMotion } from './motion.js'
3
4
  export type {
5
+ FontFamily,
6
+ FontWeight,
7
+ Elevation,
8
+ Shadow,
9
+ ComponentTheme,
4
10
  MavisTheme,
5
11
  ThemeOptions,
6
12
  Palette,
@@ -12,13 +18,68 @@ export type {
12
18
  ButtonVariant,
13
19
  Size,
14
20
  } from './theme/types.js'
21
+ export { listItemLabel } from './list-item.js'
22
+ export { avatarSizes, avatarInitials } from './avatar.js'
23
+ export type { AvatarSize } from './avatar.js'
24
+ export { badgeColors } from './badge.js'
25
+ export type { BadgeTone } from './badge.js'
15
26
  export { sliderScale } from './slider.js'
16
27
  export { resolveDimension, resolveRadius, nonNegative } from './layout.js'
17
28
  export type { Dimension } from './layout.js'
18
- export { validateInput } from './input.js'
29
+ export { validateInput, inputActionSize } from './input.js'
19
30
  export { icons, iconFontVersion } from './icons.js'
20
31
  export { iconGlyph, iconFontFamily, iconSizes, resolveIconSize, checkIconOnly } from './icon.js'
21
32
  export type { IconName, IconSize } from './icon.js'
22
- export type { InputHandle } from './input.js'
33
+ export type { InputHandle, InputAction } from './input.js'
23
34
  export { checkSheetName, sheetCloseReasons } from './sheet.js'
24
- export type { SheetCloseReason } from './sheet.js'
35
+ export type { SheetCloseReason, SheetName } from './sheet.js'
36
+ export { isChoiceValue, choiceKey, validateChoices, canChoose } from './choice.js'
37
+ export type { ChoiceValue, ChoiceOption } from './choice.js'
38
+
39
+ export { createToastController, connectToast, disposeToast } from './toast.js'
40
+ export type {
41
+ ToastTone,
42
+ ToastOptions,
43
+ ToastMessage,
44
+ ToastController,
45
+ ToastSnapshot,
46
+ ToastConnection,
47
+ } from './toast.js'
48
+
49
+ export { checkDialogLabels, dialogCloseReasons } from './dialog.js'
50
+ export type { DialogCloseReason } from './dialog.js'
51
+
52
+ export { validateTabs, tabFocusTarget, tabScrollOffset } from './tabs.js'
53
+ export type { TabOption, TabPanelProps } from './tabs.js'
54
+
55
+ export { progressSizes, progressMotion, progressState, checkProgressLabels } from './progress.js'
56
+ export type { ProgressSize, ProgressTone } from './progress.js'
57
+ export { alertAppearance, checkAlertLabels } from './alert.js'
58
+ export type { AlertTone } from './alert.js'
59
+
60
+ export { validateAccordion, toggleAccordion, accordionFocusTarget } from './accordion.js'
61
+ export type { AccordionItem, AccordionPanelProps } from './accordion.js'
62
+ export { validateSegments, segmentedFocusTarget, segmentedMotion } from './segmented.js'
63
+ export type { SegmentedOption } from './segmented.js'
64
+
65
+ export { checkChipLabels, chipColors, type ChipTone } from './chip.js'
66
+ export { checkEmptyStateTitle } from './empty-state.js'
67
+
68
+ export { fontFamily, shadowStyle, validateWeight } from './theme/typography.js'
69
+
70
+ export { componentContracts, describeContract } from './contracts.js'
71
+ export type {
72
+ ContractProps,
73
+ PropertyValue,
74
+ InputEvents,
75
+ ButtonEvents,
76
+ TextInputHandle,
77
+ } from './contracts.js'
78
+ export { targetCapabilities, componentNames, themeContract } from './capabilities.js'
79
+ export type { ComponentName, CapabilityStatus } from './capabilities.js'
80
+
81
+ export * from './date.js'
82
+ export * from './combobox.js'
83
+ export * from './files.js'
84
+ export * from './form.js'
85
+ export * from './navigation.js'