@getcortexlabs/mavis-core 0.1.0-alpha.8 → 0.1.0-rc.11.1

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 (282) hide show
  1. package/README.md +2 -2
  2. package/dist/capabilities.d.ts +7 -7
  3. package/dist/capabilities.d.ts.map +1 -1
  4. package/dist/capabilities.js +30 -4
  5. package/dist/capabilities.js.map +1 -1
  6. package/dist/chart.d.ts +43 -0
  7. package/dist/chart.d.ts.map +1 -0
  8. package/dist/chart.js +105 -0
  9. package/dist/chart.js.map +1 -0
  10. package/dist/collections.d.ts +46 -0
  11. package/dist/collections.d.ts.map +1 -0
  12. package/dist/collections.js +48 -0
  13. package/dist/collections.js.map +1 -0
  14. package/dist/contracts.d.ts +20 -0
  15. package/dist/contracts.d.ts.map +1 -1
  16. package/dist/contracts.js +2 -0
  17. package/dist/contracts.js.map +1 -1
  18. package/dist/date-input.d.ts +10 -0
  19. package/dist/date-input.d.ts.map +1 -0
  20. package/dist/date-input.js +58 -0
  21. package/dist/date-input.js.map +1 -0
  22. package/dist/date-time.d.ts +19 -0
  23. package/dist/date-time.d.ts.map +1 -0
  24. package/dist/date-time.js +27 -0
  25. package/dist/date-time.js.map +1 -0
  26. package/dist/discovery.d.ts +49 -0
  27. package/dist/discovery.d.ts.map +1 -0
  28. package/dist/discovery.js +80 -0
  29. package/dist/discovery.js.map +1 -0
  30. package/dist/experimental-regions.d.ts +10 -0
  31. package/dist/experimental-regions.d.ts.map +1 -1
  32. package/dist/experimental-regions.js +23 -0
  33. package/dist/experimental-regions.js.map +1 -1
  34. package/dist/experimental.d.ts +5 -0
  35. package/dist/experimental.d.ts.map +1 -1
  36. package/dist/experimental.js +29 -0
  37. package/dist/experimental.js.map +1 -1
  38. package/dist/grid.d.ts +3 -0
  39. package/dist/grid.d.ts.map +1 -0
  40. package/dist/grid.js +16 -0
  41. package/dist/grid.js.map +1 -0
  42. package/dist/index.d.ts +17 -3
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +13 -1
  45. package/dist/index.js.map +1 -1
  46. package/dist/input.d.ts +4 -0
  47. package/dist/input.d.ts.map +1 -1
  48. package/dist/input.js.map +1 -1
  49. package/dist/interactive.d.ts +36 -0
  50. package/dist/interactive.d.ts.map +1 -0
  51. package/dist/interactive.js +61 -0
  52. package/dist/interactive.js.map +1 -0
  53. package/dist/link.d.ts +24 -0
  54. package/dist/link.d.ts.map +1 -1
  55. package/dist/link.js +41 -0
  56. package/dist/link.js.map +1 -1
  57. package/dist/motion.d.ts +8 -0
  58. package/dist/motion.d.ts.map +1 -1
  59. package/dist/motion.js +11 -0
  60. package/dist/motion.js.map +1 -1
  61. package/dist/navigation-transition.d.ts +10 -0
  62. package/dist/navigation-transition.d.ts.map +1 -0
  63. package/dist/navigation-transition.js +10 -0
  64. package/dist/navigation-transition.js.map +1 -0
  65. package/dist/number-flow-format.d.ts +16 -0
  66. package/dist/number-flow-format.d.ts.map +1 -0
  67. package/dist/number-flow-format.js +49 -0
  68. package/dist/number-flow-format.js.map +1 -0
  69. package/dist/popover.d.ts +17 -0
  70. package/dist/popover.d.ts.map +1 -0
  71. package/dist/popover.js +19 -0
  72. package/dist/popover.js.map +1 -0
  73. package/dist/section-scroll.d.ts +14 -0
  74. package/dist/section-scroll.d.ts.map +1 -0
  75. package/dist/section-scroll.js +28 -0
  76. package/dist/section-scroll.js.map +1 -0
  77. package/dist/sheet.d.ts +7 -0
  78. package/dist/sheet.d.ts.map +1 -1
  79. package/dist/time.d.ts +34 -0
  80. package/dist/time.d.ts.map +1 -0
  81. package/dist/time.js +78 -0
  82. package/dist/time.js.map +1 -0
  83. package/dist/vue/carousel.d.ts +13 -0
  84. package/dist/vue/carousel.d.ts.map +1 -0
  85. package/dist/vue/carousel.js +136 -0
  86. package/dist/vue/carousel.js.map +1 -0
  87. package/dist/vue/chart.d.ts +194 -0
  88. package/dist/vue/chart.d.ts.map +1 -0
  89. package/dist/vue/chart.js +245 -0
  90. package/dist/vue/chart.js.map +1 -0
  91. package/dist/vue/command-palette.d.ts +129 -0
  92. package/dist/vue/command-palette.d.ts.map +1 -0
  93. package/dist/vue/command-palette.js +190 -0
  94. package/dist/vue/command-palette.js.map +1 -0
  95. package/dist/vue/component-motion.d.ts +17 -0
  96. package/dist/vue/component-motion.d.ts.map +1 -0
  97. package/dist/vue/component-motion.js +83 -0
  98. package/dist/vue/component-motion.js.map +1 -0
  99. package/dist/vue/component-props.d.ts +1505 -402
  100. package/dist/vue/component-props.d.ts.map +1 -1
  101. package/dist/vue/component-props.js +28 -0
  102. package/dist/vue/component-props.js.map +1 -1
  103. package/dist/vue/configurator-props.d.ts +21 -0
  104. package/dist/vue/configurator-props.d.ts.map +1 -1
  105. package/dist/vue/configurator-props.js +6 -0
  106. package/dist/vue/configurator-props.js.map +1 -1
  107. package/dist/vue/configurator.d.ts +3 -3
  108. package/dist/vue/configurator.d.ts.map +1 -1
  109. package/dist/vue/configurator.js +3 -1
  110. package/dist/vue/configurator.js.map +1 -1
  111. package/dist/vue/data-table.d.ts +17 -0
  112. package/dist/vue/data-table.d.ts.map +1 -0
  113. package/dist/vue/data-table.js +133 -0
  114. package/dist/vue/data-table.js.map +1 -0
  115. package/dist/vue/date-calendar.d.ts +2 -0
  116. package/dist/vue/date-calendar.d.ts.map +1 -1
  117. package/dist/vue/date-calendar.js +20 -1
  118. package/dist/vue/date-calendar.js.map +1 -1
  119. package/dist/vue/date-picker.d.ts +48 -13
  120. package/dist/vue/date-picker.d.ts.map +1 -1
  121. package/dist/vue/date-picker.js +67 -74
  122. package/dist/vue/date-picker.js.map +1 -1
  123. package/dist/vue/date-props.d.ts +15 -0
  124. package/dist/vue/date-props.d.ts.map +1 -1
  125. package/dist/vue/date-props.js +4 -0
  126. package/dist/vue/date-props.js.map +1 -1
  127. package/dist/vue/discovery-components.d.ts +50 -0
  128. package/dist/vue/discovery-components.d.ts.map +1 -0
  129. package/dist/vue/discovery-components.js +2 -0
  130. package/dist/vue/discovery-components.js.map +1 -0
  131. package/dist/vue/discovery-controls.d.ts +197 -0
  132. package/dist/vue/discovery-controls.d.ts.map +1 -0
  133. package/dist/vue/discovery-controls.js +185 -0
  134. package/dist/vue/discovery-controls.js.map +1 -0
  135. package/dist/vue/discovery-props.d.ts +249 -0
  136. package/dist/vue/discovery-props.d.ts.map +1 -0
  137. package/dist/vue/discovery-props.js +87 -0
  138. package/dist/vue/discovery-props.js.map +1 -0
  139. package/dist/vue/global-components.d.ts +134 -2
  140. package/dist/vue/global-components.d.ts.map +1 -1
  141. package/dist/vue/index.d.ts +27 -1
  142. package/dist/vue/index.d.ts.map +1 -1
  143. package/dist/vue/index.js +23 -1
  144. package/dist/vue/index.js.map +1 -1
  145. package/dist/vue/interactive-components.d.ts +31 -0
  146. package/dist/vue/interactive-components.d.ts.map +1 -0
  147. package/dist/vue/interactive-components.js +2 -0
  148. package/dist/vue/interactive-components.js.map +1 -0
  149. package/dist/vue/interactive-props.d.ts +254 -0
  150. package/dist/vue/interactive-props.d.ts.map +1 -0
  151. package/dist/vue/interactive-props.js +85 -0
  152. package/dist/vue/interactive-props.js.map +1 -0
  153. package/dist/vue/list-motion.d.ts +7 -0
  154. package/dist/vue/list-motion.d.ts.map +1 -0
  155. package/dist/vue/list-motion.js +28 -0
  156. package/dist/vue/list-motion.js.map +1 -0
  157. package/dist/vue/menu.d.ts +122 -0
  158. package/dist/vue/menu.d.ts.map +1 -0
  159. package/dist/vue/menu.js +79 -0
  160. package/dist/vue/menu.js.map +1 -0
  161. package/dist/vue/navigation-controls.d.ts +125 -0
  162. package/dist/vue/navigation-controls.d.ts.map +1 -0
  163. package/dist/vue/navigation-controls.js +130 -0
  164. package/dist/vue/navigation-controls.js.map +1 -0
  165. package/dist/vue/navigation-props.d.ts +283 -0
  166. package/dist/vue/navigation-props.d.ts.map +1 -0
  167. package/dist/vue/navigation-props.js +82 -0
  168. package/dist/vue/navigation-props.js.map +1 -0
  169. package/dist/vue/navigation.d.ts +7 -0
  170. package/dist/vue/navigation.d.ts.map +1 -0
  171. package/dist/vue/navigation.js +15 -0
  172. package/dist/vue/navigation.js.map +1 -0
  173. package/dist/vue/number-flow.d.ts +75 -0
  174. package/dist/vue/number-flow.d.ts.map +1 -0
  175. package/dist/vue/number-flow.js +57 -0
  176. package/dist/vue/number-flow.js.map +1 -0
  177. package/dist/vue/partition.d.ts +15 -0
  178. package/dist/vue/partition.d.ts.map +1 -0
  179. package/dist/vue/partition.js +42 -0
  180. package/dist/vue/partition.js.map +1 -0
  181. package/dist/vue/platform.d.ts +37 -0
  182. package/dist/vue/platform.d.ts.map +1 -0
  183. package/dist/vue/platform.js +46 -0
  184. package/dist/vue/platform.js.map +1 -0
  185. package/dist/vue/props.d.ts +233 -36
  186. package/dist/vue/props.d.ts.map +1 -1
  187. package/dist/vue/props.js +15 -1
  188. package/dist/vue/props.js.map +1 -1
  189. package/dist/vue/sortable-list.d.ts +24 -0
  190. package/dist/vue/sortable-list.d.ts.map +1 -0
  191. package/dist/vue/sortable-list.js +251 -0
  192. package/dist/vue/sortable-list.js.map +1 -0
  193. package/dist/vue/swipe-actions.d.ts +96 -0
  194. package/dist/vue/swipe-actions.d.ts.map +1 -0
  195. package/dist/vue/swipe-actions.js +174 -0
  196. package/dist/vue/swipe-actions.js.map +1 -0
  197. package/dist/vue/time-picker.d.ts +488 -0
  198. package/dist/vue/time-picker.d.ts.map +1 -0
  199. package/dist/vue/time-picker.js +190 -0
  200. package/dist/vue/time-picker.js.map +1 -0
  201. package/dist/vue/tree-view.d.ts +111 -0
  202. package/dist/vue/tree-view.d.ts.map +1 -0
  203. package/dist/vue/tree-view.js +204 -0
  204. package/dist/vue/tree-view.js.map +1 -0
  205. package/dist/vue/virtual-list.d.ts +26 -0
  206. package/dist/vue/virtual-list.d.ts.map +1 -0
  207. package/dist/vue/virtual-list.js +2 -0
  208. package/dist/vue/virtual-list.js.map +1 -0
  209. package/dist/vue/wayfinding-controls.d.ts +199 -0
  210. package/dist/vue/wayfinding-controls.d.ts.map +1 -0
  211. package/dist/vue/wayfinding-controls.js +330 -0
  212. package/dist/vue/wayfinding-controls.js.map +1 -0
  213. package/dist/vue/wayfinding-props.d.ts +221 -0
  214. package/dist/vue/wayfinding-props.d.ts.map +1 -0
  215. package/dist/vue/wayfinding-props.js +86 -0
  216. package/dist/vue/wayfinding-props.js.map +1 -0
  217. package/dist/vue/workflows.d.ts +2 -2
  218. package/dist/vue/workflows.d.ts.map +1 -1
  219. package/dist/vue/workflows.js +17 -4
  220. package/dist/vue/workflows.js.map +1 -1
  221. package/dist/wayfinding.d.ts +50 -0
  222. package/dist/wayfinding.d.ts.map +1 -0
  223. package/dist/wayfinding.js +54 -0
  224. package/dist/wayfinding.js.map +1 -0
  225. package/package.json +2 -2
  226. package/src/capabilities.ts +31 -4
  227. package/src/chart.ts +151 -0
  228. package/src/collections.ts +87 -0
  229. package/src/contracts.ts +2 -0
  230. package/src/date-input.ts +87 -0
  231. package/src/date-time.ts +37 -0
  232. package/src/discovery.ts +139 -0
  233. package/src/experimental-regions.ts +29 -0
  234. package/src/experimental.ts +40 -0
  235. package/src/grid.ts +19 -0
  236. package/src/index.ts +24 -3
  237. package/src/input.ts +5 -0
  238. package/src/interactive.ts +91 -0
  239. package/src/link.ts +63 -0
  240. package/src/motion.ts +14 -0
  241. package/src/navigation-transition.ts +14 -0
  242. package/src/number-flow-format.ts +68 -0
  243. package/src/popover.ts +38 -0
  244. package/src/section-scroll.ts +46 -0
  245. package/src/sheet.ts +11 -0
  246. package/src/time.ts +83 -0
  247. package/src/vue/carousel.ts +198 -0
  248. package/src/vue/chart.ts +337 -0
  249. package/src/vue/command-palette.ts +235 -0
  250. package/src/vue/component-motion.ts +109 -0
  251. package/src/vue/component-props.ts +57 -0
  252. package/src/vue/configurator-props.ts +6 -0
  253. package/src/vue/configurator.ts +3 -1
  254. package/src/vue/data-table.ts +190 -0
  255. package/src/vue/date-calendar.ts +30 -1
  256. package/src/vue/date-picker.ts +81 -85
  257. package/src/vue/date-props.ts +5 -0
  258. package/src/vue/discovery-components.ts +60 -0
  259. package/src/vue/discovery-controls.ts +241 -0
  260. package/src/vue/discovery-props.ts +92 -0
  261. package/src/vue/global-components.ts +124 -2
  262. package/src/vue/index.ts +46 -0
  263. package/src/vue/interactive-components.ts +29 -0
  264. package/src/vue/interactive-props.ts +90 -0
  265. package/src/vue/list-motion.ts +31 -0
  266. package/src/vue/menu.ts +100 -0
  267. package/src/vue/navigation-controls.ts +159 -0
  268. package/src/vue/navigation-props.ts +87 -0
  269. package/src/vue/navigation.ts +16 -0
  270. package/src/vue/number-flow.ts +83 -0
  271. package/src/vue/partition.ts +49 -0
  272. package/src/vue/platform.ts +73 -0
  273. package/src/vue/props.ts +16 -1
  274. package/src/vue/sortable-list.ts +308 -0
  275. package/src/vue/swipe-actions.ts +226 -0
  276. package/src/vue/time-picker.ts +227 -0
  277. package/src/vue/tree-view.ts +259 -0
  278. package/src/vue/virtual-list.ts +28 -0
  279. package/src/vue/wayfinding-controls.ts +430 -0
  280. package/src/vue/wayfinding-props.ts +97 -0
  281. package/src/vue/workflows.ts +17 -4
  282. package/src/wayfinding.ts +85 -0
@@ -0,0 +1,227 @@
1
+ import {
2
+ computed,
3
+ defineComponent,
4
+ h,
5
+ mergeProps,
6
+ nextTick,
7
+ onBeforeUnmount,
8
+ ref,
9
+ watch,
10
+ type Component,
11
+ } from '@vue/runtime-core'
12
+ import {
13
+ formatTime,
14
+ localTime,
15
+ parseTime,
16
+ timeOptions,
17
+ validateTime,
18
+ type LocalTime,
19
+ type TimeValidation,
20
+ } from '../time.js'
21
+ import type { InputActionHandle, InputHandle } from '../input.js'
22
+ import { timePickerProps } from './navigation-props.js'
23
+ import { useButtonAction } from './button-content.js'
24
+
25
+ export function createTimePicker(
26
+ ui: {
27
+ Input: Component
28
+ Column: Component
29
+ Row: Component
30
+ Button: Component
31
+ Text: Component
32
+ Sheet: Component
33
+ Picker: Component
34
+ beforeOpen?: () => void
35
+ },
36
+ platform: 'web' | 'native',
37
+ ) {
38
+ const TimePicker = defineComponent({
39
+ name: 'MTimePicker',
40
+ inheritAttrs: false,
41
+ props: timePickerProps,
42
+ emits: {
43
+ 'update:modelValue': (_value: LocalTime | null) => true,
44
+ 'update:draft': (_value: string) => true,
45
+ validityChange: (_value: TimeValidation) => true,
46
+ focus: () => true,
47
+ blur: () => true,
48
+ },
49
+ setup(props, { attrs, emit, expose }) {
50
+ useButtonAction()
51
+ const editor = ref<InputActionHandle>()
52
+ const local = ref(props.draft ?? formatTime(props.modelValue, props))
53
+ const draft = computed(() => props.draft ?? local.value)
54
+ const validation = computed(() => parseTime(draft.value, props))
55
+ watch(validation, (value) => emit('validityChange', value), { immediate: true })
56
+ const open = ref(false)
57
+ const picked = ref<LocalTime>('00:00')
58
+ const touched = ref(false)
59
+ let alive = true
60
+ let revision = 0
61
+ let requested: LocalTime | null | undefined
62
+ onBeforeUnmount(() => {
63
+ alive = false
64
+ })
65
+ const setDraft = (value: string) => {
66
+ local.value = value
67
+ emit('update:draft', value)
68
+ }
69
+ watch(
70
+ () => [props.modelValue, props.locale, props.hourCycle] as const,
71
+ (value, previous) => {
72
+ if (value[0] !== requested || value[1] !== previous[1] || value[2] !== previous[2])
73
+ setDraft(formatTime(value[0], props))
74
+ requested = undefined
75
+ },
76
+ )
77
+ watch(
78
+ () => props.draft,
79
+ (value) => {
80
+ if (value !== undefined) local.value = value
81
+ },
82
+ )
83
+ watch(
84
+ () => props.disabled || props.readonly,
85
+ (blocked) => {
86
+ if (blocked) open.value = false
87
+ },
88
+ )
89
+ function publish(value: LocalTime | null) {
90
+ const current = ++revision
91
+ requested = value
92
+ emit('update:modelValue', value)
93
+ void nextTick(() => {
94
+ if (!alive || current !== revision) return
95
+ if (props.modelValue !== value) setDraft(formatTime(props.modelValue, props))
96
+ requested = undefined
97
+ })
98
+ }
99
+ function edit(value: string) {
100
+ if (props.disabled || props.readonly) return
101
+ revision++
102
+ setDraft(value)
103
+ const next = parseTime(value, props)
104
+ if (next.status === 'valid' || next.status === 'empty') publish(next.value)
105
+ }
106
+ function close() {
107
+ open.value = false
108
+ if (platform === 'native')
109
+ void nextTick(() => {
110
+ if (alive && !props.disabled) editor.value?.focusAction()
111
+ })
112
+ }
113
+ function choose() {
114
+ if (props.disabled || props.readonly) return
115
+ const options = timeOptions(props)
116
+ const value =
117
+ validation.value.status === 'valid' ? validation.value.value : props.modelValue
118
+ picked.value =
119
+ validateTime(value, props).status === 'valid'
120
+ ? value!
121
+ : localTime(Math.ceil(options.min / options.step) * options.step)
122
+ ui.beforeOpen?.()
123
+ editor.value?.focusAction()
124
+ open.value = true
125
+ }
126
+ expose({
127
+ focus: () => editor.value?.focus(),
128
+ blur: () => editor.value?.blur(),
129
+ } satisfies InputHandle)
130
+ return () => {
131
+ const options = timeOptions(props)
132
+ if (!props.label.trim()) throw new TypeError('TimePicker requires a label.')
133
+ const issue =
134
+ !['valid', 'empty'].includes(validation.value.status) &&
135
+ (validation.value.status !== 'incomplete' || touched.value)
136
+ const selected = validateTime(picked.value, props)
137
+ return h(ui.Column, { width: props.width, grow: props.grow, shrink: props.shrink }, () => [
138
+ h(
139
+ ui.Input,
140
+ mergeProps(attrs, {
141
+ ref: editor,
142
+ id: props.id,
143
+ label: props.label,
144
+ modelValue: draft.value,
145
+ placeholder: props.placeholder ?? (options.cycle === 'h23' ? 'HH:mm' : 'h:mm AM/PM'),
146
+ disabled: props.disabled,
147
+ readonly: props.readonly,
148
+ required: props.required,
149
+ helperText: props.helperText,
150
+ error: props.error || (issue ? props.invalidLabel : ''),
151
+ autocapitalize: 'none',
152
+ autocorrect: false,
153
+ trailingAction: {
154
+ icon: 'clock',
155
+ label: props.chooseLabel,
156
+ disabled: props.disabled || props.readonly,
157
+ },
158
+ 'onUpdate:modelValue': edit,
159
+ onAction: choose,
160
+ onFocus: () => emit('focus'),
161
+ onBlur: () => {
162
+ touched.value = true
163
+ emit('blur')
164
+ },
165
+ }),
166
+ ),
167
+ platform === 'web' && props.name
168
+ ? h('input', {
169
+ type: 'hidden',
170
+ name: props.name,
171
+ disabled: props.disabled,
172
+ value: props.modelValue ?? '',
173
+ })
174
+ : null,
175
+ h(
176
+ ui.Sheet,
177
+ {
178
+ open: open.value,
179
+ title: props.label,
180
+ closeLabel: props.cancelLabel,
181
+ 'onUpdate:open': (value: boolean) => {
182
+ if (!value) close()
183
+ },
184
+ },
185
+ () => [
186
+ h(ui.Picker, {
187
+ ...props,
188
+ modelValue: picked.value,
189
+ 'onUpdate:modelValue': (value: LocalTime) => {
190
+ picked.value = value
191
+ },
192
+ }),
193
+ selected.status !== 'valid'
194
+ ? h(ui.Text, { text: props.invalidLabel, tone: 'danger' })
195
+ : null,
196
+ h(ui.Row, { gap: 'sm', wrap: true }, () => [
197
+ h(ui.Button, {
198
+ text: props.confirmLabel,
199
+ disabled: selected.status !== 'valid' || props.disabled || props.readonly,
200
+ onClick: () => {
201
+ const next = validateTime(picked.value, props)
202
+ if (props.disabled || props.readonly || next.status !== 'valid') return
203
+ setDraft(formatTime(next.value, props))
204
+ publish(next.value)
205
+ close()
206
+ },
207
+ }),
208
+ h(ui.Button, {
209
+ text: props.clearLabel,
210
+ variant: 'ghost',
211
+ disabled: props.disabled || props.readonly || props.required,
212
+ onClick: () => {
213
+ if (props.disabled || props.readonly || props.required) return
214
+ setDraft('')
215
+ publish(null)
216
+ close()
217
+ },
218
+ }),
219
+ ]),
220
+ ],
221
+ ),
222
+ ])
223
+ }
224
+ },
225
+ })
226
+ return TimePicker as typeof TimePicker & { new (): InstanceType<typeof TimePicker> & InputHandle }
227
+ }
@@ -0,0 +1,259 @@
1
+ import { useComponentLayout, useComponentState } from './component-motion.js'
2
+ import {
3
+ computed,
4
+ defineComponent,
5
+ h,
6
+ mergeProps,
7
+ nextTick,
8
+ ref,
9
+ watch,
10
+ useId,
11
+ type Component,
12
+ type PropType,
13
+ } from '@vue/runtime-core'
14
+ import { choiceKey, type ChoiceValue } from '../choice.js'
15
+ import { treeBranch, treeEntries, type TreeNode } from '../discovery.js'
16
+ import { treeViewProps } from './discovery-props.js'
17
+ import { useMavisTheme } from './theme.js'
18
+
19
+ export function createTreeView(
20
+ ui: {
21
+ Column: Component
22
+ Row: Component
23
+ Text: Component
24
+ Button: Component
25
+ Reveal: Component
26
+ Icon: Component
27
+ focus?: (id: string) => void
28
+ containsFocus?: (id: string) => boolean
29
+ },
30
+ platform: 'web' | 'native',
31
+ ) {
32
+ const BranchChildren = defineComponent({
33
+ props: { nodes: { type: Array as PropType<readonly TreeNode[]>, required: true } },
34
+ setup(props, { slots }) {
35
+ const region = useComponentLayout(() => props.nodes.map((node) => node.key))
36
+ return () =>
37
+ h(
38
+ ui.Column,
39
+ { gap: 'xs', motionRegion: region, ...(platform === 'web' ? { role: 'group' } : {}) },
40
+ slots,
41
+ )
42
+ },
43
+ })
44
+ const BranchToggle = defineComponent({
45
+ props: { expanded: Boolean },
46
+ setup(props, { attrs }) {
47
+ const surface = useComponentState(() => ({ rotate: props.expanded ? 90 : 0 }))
48
+ return () =>
49
+ h(
50
+ ui.Button,
51
+ { ...attrs, variant: 'ghost' },
52
+ {
53
+ leading: () =>
54
+ h(ui.Icon, {
55
+ name: 'chevron-right',
56
+ size: 18,
57
+ motionSurface: surface,
58
+ accessibilityHidden: true,
59
+ }),
60
+ },
61
+ )
62
+ },
63
+ })
64
+ return defineComponent({
65
+ name: 'MTreeView',
66
+ inheritAttrs: false,
67
+ props: treeViewProps,
68
+ emits: {
69
+ 'update:modelValue': (_key: ChoiceValue) => true,
70
+ 'update:expanded': (_keys: ChoiceValue[]) => true,
71
+ load: (_node: TreeNode) => true,
72
+ },
73
+ setup(props, { attrs, emit }) {
74
+ const { theme } = useMavisTheme()
75
+ const layout = useComponentLayout(() => props.nodes.map((node) => node.key))
76
+ const treeId = `m-tree-${useId()}`
77
+ const entries = computed(() => treeEntries(props.nodes, props.expanded))
78
+ const focused = ref<ChoiceValue | null>(props.modelValue)
79
+ const idFor = (key: ChoiceValue) => `${treeId}-${encodeURIComponent(choiceKey(key))}`
80
+ function isOpen(key: ChoiceValue) {
81
+ return props.expanded.includes(key)
82
+ }
83
+ function select(node: TreeNode) {
84
+ if (!props.disabled && !node.disabled) emit('update:modelValue', node.key)
85
+ }
86
+ function toggle(node: TreeNode, open = !isOpen(node.key)) {
87
+ if (
88
+ props.disabled ||
89
+ node.disabled ||
90
+ !treeBranch(node) ||
91
+ props.loadingKeys.includes(node.key)
92
+ )
93
+ return
94
+ if (open === isOpen(node.key)) return
95
+ emit(
96
+ 'update:expanded',
97
+ open ? [...props.expanded, node.key] : props.expanded.filter((key) => key !== node.key),
98
+ )
99
+ if (open && node.children === undefined) emit('load', node)
100
+ }
101
+ function focus(key: ChoiceValue) {
102
+ focused.value = key
103
+ void nextTick(() => ui.focus?.(idFor(key)))
104
+ }
105
+ watch(
106
+ entries,
107
+ (visible, previous) => {
108
+ if (visible.some((entry) => entry.node.key === focused.value)) return
109
+ const restore = focused.value !== null && ui.containsFocus?.(idFor(focused.value))
110
+ // Prefer the collapsed ancestor; never reset a retained logical node by array index.
111
+ let parent = previous?.find((entry) => entry.node.key === focused.value)?.parent
112
+ while (parent != null && !visible.some((entry) => entry.node.key === parent))
113
+ parent = previous?.find((entry) => entry.node.key === parent)?.parent
114
+ focused.value = parent ?? visible.find((entry) => !entry.node.disabled)?.node.key ?? null
115
+ if (restore && focused.value !== null) focus(focused.value)
116
+ },
117
+ { immediate: true },
118
+ )
119
+ function keydown(event: KeyboardEvent, node: TreeNode) {
120
+ if (event.altKey || event.ctrlKey || event.metaKey || event.isComposing || props.disabled)
121
+ return
122
+ const visible = entries.value.filter((entry) => !entry.node.disabled)
123
+ const index = visible.findIndex((entry) => entry.node.key === node.key)
124
+ let next: ChoiceValue | undefined
125
+ if (event.key === 'ArrowDown')
126
+ next = visible[Math.min(visible.length - 1, index + 1)]?.node.key
127
+ else if (event.key === 'ArrowUp') next = visible[Math.max(0, index - 1)]?.node.key
128
+ else if (event.key === 'Home') next = visible[0]?.node.key
129
+ else if (event.key === 'End') next = visible.at(-1)?.node.key
130
+ else if (event.key === 'ArrowRight') {
131
+ if (!isOpen(node.key)) toggle(node, true)
132
+ else next = node.children?.find((child) => !child.disabled)?.key
133
+ } else if (event.key === 'ArrowLeft') {
134
+ if (isOpen(node.key)) toggle(node, false)
135
+ else
136
+ next = entries.value.find((entry) => entry.node.key === node.key)?.parent ?? undefined
137
+ } else if (event.key === 'Enter' || event.key === ' ') select(node)
138
+ else if (event.key.length === 1) {
139
+ const order = [...visible.slice(index + 1), ...visible.slice(0, index + 1)]
140
+ next = order.find((entry) =>
141
+ entry.node.label.toLocaleLowerCase().startsWith(event.key.toLocaleLowerCase()),
142
+ )?.node.key
143
+ } else return
144
+ event.preventDefault()
145
+ if (next !== undefined) focus(next)
146
+ }
147
+ function render(nodes: readonly TreeNode[], level = 1): ReturnType<typeof h>[] {
148
+ return nodes.map((node, index) => {
149
+ const branch = treeBranch(node),
150
+ open = isOpen(node.key),
151
+ selected = props.modelValue === node.key
152
+ const busy = props.loadingKeys.includes(node.key)
153
+ const unavailable = props.disabled || node.disabled
154
+ return h(ui.Column, { key: choiceKey(node.key), gap: 'none' }, () => [
155
+ h(
156
+ ui.Row,
157
+ {
158
+ id: idFor(node.key),
159
+ gap: 'xs',
160
+ padding: 'xs',
161
+ radius: 'md',
162
+ backgroundColor: selected ? theme.value.colors.primarySoft : 'transparent',
163
+ ...(platform === 'web'
164
+ ? {
165
+ role: 'treeitem',
166
+ tabindex: !unavailable && focused.value === node.key ? 0 : -1,
167
+ 'aria-level': level,
168
+ 'aria-posinset': index + 1,
169
+ 'aria-setsize': nodes.length,
170
+ 'aria-selected': selected,
171
+ 'aria-expanded': branch ? open : undefined,
172
+ 'aria-disabled': unavailable || undefined,
173
+ 'aria-busy': busy || undefined,
174
+ 'aria-label': node.label,
175
+ onFocus: () => {
176
+ focused.value = node.key
177
+ },
178
+ onKeydown: (event: KeyboardEvent) => keydown(event, node),
179
+ }
180
+ : {}),
181
+ },
182
+ () => [
183
+ branch
184
+ ? h(BranchToggle, {
185
+ expanded: open,
186
+ variant: 'ghost',
187
+ accessibilityLabel: `${node.label}: ${open ? props.expandedLabel : props.collapsedLabel}`,
188
+ disabled: unavailable || busy,
189
+ ...(platform === 'web' ? { tabindex: -1 } : {}),
190
+ onClick: () => toggle(node),
191
+ })
192
+ : h(ui.Icon, { name: 'file', tone: 'muted', size: 18, style: { margin: 12 } }),
193
+ h(ui.Button, {
194
+ text: node.label,
195
+ style:
196
+ platform === 'web'
197
+ ? { justifyContent: 'flex-start', textAlign: 'start' }
198
+ : { textAlignment: 'left' },
199
+ variant: 'ghost',
200
+ grow: platform === 'web' ? 1 : 0,
201
+ shrink: 1,
202
+ disabled: unavailable,
203
+ ...(platform === 'web'
204
+ ? { tabindex: -1 }
205
+ : { accessibilityValue: selected ? 'Selected' : '' }),
206
+ onClick: () => select(node),
207
+ }),
208
+ busy
209
+ ? h(ui.Text, { text: props.loadingLabel, variant: 'caption', tone: 'muted' })
210
+ : null,
211
+ ],
212
+ ),
213
+ branch
214
+ ? h(
215
+ ui.Reveal,
216
+ {
217
+ show: open,
218
+ style:
219
+ platform === 'web'
220
+ ? { paddingInlineStart: `${theme.value.space.lg}px` }
221
+ : { paddingLeft: theme.value.space.lg },
222
+ },
223
+ () =>
224
+ h(BranchChildren, { nodes: node.children ?? [] }, () =>
225
+ render(node.children ?? [], level + 1),
226
+ ),
227
+ )
228
+ : null,
229
+ ])
230
+ })
231
+ }
232
+ return () => {
233
+ void entries.value
234
+ return h(
235
+ ui.Column,
236
+ mergeProps(
237
+ { id: props.id, gap: 'sm' },
238
+ attrs,
239
+ platform === 'web' ? { class: 'm-tree-view' } : {},
240
+ ),
241
+ () => [
242
+ h(ui.Text, { as: 'h2', variant: 'heading', text: props.label }),
243
+ props.nodes.length
244
+ ? h(
245
+ ui.Column,
246
+ {
247
+ gap: 'xs',
248
+ motionRegion: layout,
249
+ ...(platform === 'web' ? { role: 'tree', 'aria-label': props.label } : {}),
250
+ },
251
+ () => render(props.nodes),
252
+ )
253
+ : h(ui.Text, { text: props.emptyLabel, tone: 'muted' }),
254
+ ],
255
+ )
256
+ }
257
+ },
258
+ })
259
+ }
@@ -0,0 +1,28 @@
1
+ import type { VNode, VNodeChild, PublicProps } from '@vue/runtime-core'
2
+ import type { ChoiceValue } from '../choice.js'
3
+ import type { VirtualItemSlot, VirtualListHandle } from '../collections.js'
4
+ import type { ComponentProps } from './component-props.js'
5
+
6
+ /** Both renderers expose the same item type to template slots and key callbacks. */
7
+ type VirtualListProps<T> = PublicProps &
8
+ Omit<ComponentProps['MVirtualList'], 'items' | 'itemKey'> & {
9
+ items: readonly T[]
10
+ itemKey?: Extract<keyof T, string> | ((item: T) => ChoiceValue)
11
+ }
12
+ type VirtualListContext<T> = {
13
+ props: VirtualListProps<T>
14
+ attrs: Record<string, unknown>
15
+ emit: (event: string, ...args: unknown[]) => void
16
+ slots: { default?: (scope: VirtualItemSlot<T>) => VNodeChild; empty?: () => VNodeChild }
17
+ expose(exposed: VirtualListHandle): void
18
+ }
19
+ export interface VirtualListComponent {
20
+ <T>(
21
+ props: NonNullable<Awaited<typeof setup>>['props'],
22
+ context?: Pick<NonNullable<Awaited<typeof setup>>, 'attrs' | 'emit' | 'slots'>,
23
+ exposed?: NonNullable<Awaited<typeof setup>>['expose'],
24
+ setup?: Promise<VirtualListContext<T>>,
25
+ ): VNode & {
26
+ __ctx?: NonNullable<Awaited<typeof setup>>
27
+ }
28
+ }