@getcortexlabs/mavis 0.1.0-rc.13 → 0.1.0-rc.15

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 (190) hide show
  1. package/README.md +7 -5
  2. package/dist/components/bindings.d.ts +14 -0
  3. package/dist/components/bindings.d.ts.map +1 -0
  4. package/dist/components/bindings.js +21 -0
  5. package/dist/components/bindings.js.map +1 -0
  6. package/dist/components/contracts.d.ts +28 -0
  7. package/dist/components/contracts.d.ts.map +1 -0
  8. package/dist/components/contracts.js +27 -0
  9. package/dist/components/contracts.js.map +1 -0
  10. package/dist/components/controller.d.ts +56 -0
  11. package/dist/components/controller.d.ts.map +1 -0
  12. package/dist/components/controller.js +508 -0
  13. package/dist/components/controller.js.map +1 -0
  14. package/dist/components/index.d.ts +53 -0
  15. package/dist/components/index.d.ts.map +1 -0
  16. package/dist/components/index.js +95 -0
  17. package/dist/components/index.js.map +1 -0
  18. package/dist/components/style.d.ts +52 -0
  19. package/dist/components/style.d.ts.map +1 -0
  20. package/dist/components/style.js +68 -0
  21. package/dist/components/style.js.map +1 -0
  22. package/dist/components/theme.d.ts +12 -0
  23. package/dist/components/theme.d.ts.map +1 -0
  24. package/dist/components/theme.js +29 -0
  25. package/dist/components/theme.js.map +1 -0
  26. package/dist/native/box.ios.d.ts +1 -0
  27. package/dist/native/box.ios.d.ts.map +1 -1
  28. package/dist/native/box.ios.js +21 -1
  29. package/dist/native/box.ios.js.map +1 -1
  30. package/dist/native/flex-layout.ios.d.ts.map +1 -1
  31. package/dist/native/flex-layout.ios.js +4 -0
  32. package/dist/native/flex-layout.ios.js.map +1 -1
  33. package/dist/native/motion-binding.d.ts +11 -0
  34. package/dist/native/motion-binding.d.ts.map +1 -0
  35. package/dist/native/motion-binding.js +132 -0
  36. package/dist/native/motion-binding.js.map +1 -0
  37. package/dist/native/motion-region.d.ts.map +1 -1
  38. package/dist/native/motion-region.js +1 -0
  39. package/dist/native/motion-region.js.map +1 -1
  40. package/dist/native/motion-surface.d.ts +1 -1
  41. package/dist/native/motion-surface.d.ts.map +1 -1
  42. package/dist/native/motion-surface.js +40 -8
  43. package/dist/native/motion-surface.js.map +1 -1
  44. package/dist/native/navigation.d.ts +4 -0
  45. package/dist/native/navigation.d.ts.map +1 -0
  46. package/dist/native/navigation.js +16 -0
  47. package/dist/native/navigation.js.map +1 -0
  48. package/dist/native/pointer-availability.d.ts +7 -0
  49. package/dist/native/pointer-availability.d.ts.map +1 -0
  50. package/dist/native/pointer-availability.js +43 -0
  51. package/dist/native/pointer-availability.js.map +1 -0
  52. package/dist/native/scroll-motion.d.ts +1 -1
  53. package/dist/native/scroll-motion.d.ts.map +1 -1
  54. package/dist/native/scroll-motion.js +55 -16
  55. package/dist/native/scroll-motion.js.map +1 -1
  56. package/dist/native/section-scroll.d.ts +1 -1
  57. package/dist/native/section-scroll.d.ts.map +1 -1
  58. package/dist/native/section-scroll.js +19 -6
  59. package/dist/native/section-scroll.js.map +1 -1
  60. package/dist/vue/card-input.d.ts +4 -0
  61. package/dist/vue/card-input.d.ts.map +1 -0
  62. package/dist/vue/card-input.js +37 -0
  63. package/dist/vue/card-input.js.map +1 -0
  64. package/dist/vue/components/ChoiceControl.d.ts +3 -3
  65. package/dist/vue/components/LoadingDecoration.d.ts +5 -5
  66. package/dist/vue/components/MAccordion.d.ts +3 -3
  67. package/dist/vue/components/MAlert.d.ts +4 -4
  68. package/dist/vue/components/MAvatar.d.ts +4 -4
  69. package/dist/vue/components/MBadge.d.ts +3 -3
  70. package/dist/vue/components/MButton.d.ts +20 -20
  71. package/dist/vue/components/MCard.d.ts +11 -11
  72. package/dist/vue/components/MCard.d.ts.map +1 -1
  73. package/dist/vue/components/MCard.js +3 -2
  74. package/dist/vue/components/MCard.js.map +1 -1
  75. package/dist/vue/components/MCheckbox.d.ts +3 -3
  76. package/dist/vue/components/MChip.d.ts +4 -4
  77. package/dist/vue/components/MCombobox.d.ts +5 -5
  78. package/dist/vue/components/MDatePicker.d.ts +96 -96
  79. package/dist/vue/components/MDatePicker.d.ts.map +1 -1
  80. package/dist/vue/components/MDialog.d.ts +3 -3
  81. package/dist/vue/components/MEmptyState.d.ts +3 -3
  82. package/dist/vue/components/MGrid.d.ts +7 -7
  83. package/dist/vue/components/MGrid.d.ts.map +1 -1
  84. package/dist/vue/components/MGrid.js +5 -1
  85. package/dist/vue/components/MGrid.js.map +1 -1
  86. package/dist/vue/components/MIcon.d.ts +2 -2
  87. package/dist/vue/components/MImage.d.ts +7 -7
  88. package/dist/vue/components/MImageCompare.d.ts +1 -1
  89. package/dist/vue/components/MInput.d.ts +22 -22
  90. package/dist/vue/components/MLink.d.ts +5 -5
  91. package/dist/vue/components/MListItem.d.ts +5 -5
  92. package/dist/vue/components/MPinInput.d.ts +1 -1
  93. package/dist/vue/components/MPopover.d.ts +3 -3
  94. package/dist/vue/components/MProgress.d.ts +5 -5
  95. package/dist/vue/components/MRadioGroup.d.ts +3 -3
  96. package/dist/vue/components/MRangeSlider.d.ts +1 -1
  97. package/dist/vue/components/MResizablePanels.d.ts +3 -3
  98. package/dist/vue/components/MReveal.d.ts +17 -17
  99. package/dist/vue/components/MScreen.d.ts +3 -3
  100. package/dist/vue/components/MScreen.d.ts.map +1 -1
  101. package/dist/vue/components/MScreen.js +11 -1
  102. package/dist/vue/components/MScreen.js.map +1 -1
  103. package/dist/vue/components/MSegmentedControl.d.ts +3 -3
  104. package/dist/vue/components/MSelect.d.ts +4 -4
  105. package/dist/vue/components/MSheet.d.ts +7 -7
  106. package/dist/vue/components/MSkeleton.d.ts +3 -3
  107. package/dist/vue/components/MSlider.d.ts +1 -1
  108. package/dist/vue/components/MStack.d.ts +19 -19
  109. package/dist/vue/components/MStack.d.ts.map +1 -1
  110. package/dist/vue/components/MStack.js +12 -2
  111. package/dist/vue/components/MStack.js.map +1 -1
  112. package/dist/vue/components/MSwitch.d.ts +3 -3
  113. package/dist/vue/components/MTabs.d.ts +3 -3
  114. package/dist/vue/components/MText.d.ts +8 -8
  115. package/dist/vue/components/MText.d.ts.map +1 -1
  116. package/dist/vue/components/MText.js +4 -1
  117. package/dist/vue/components/MText.js.map +1 -1
  118. package/dist/vue/components/MTimePicker.d.ts +40 -40
  119. package/dist/vue/components/MTimePicker.d.ts.map +1 -1
  120. package/dist/vue/components/MTooltip.d.ts +2 -2
  121. package/dist/vue/components/MVirtualList.d.ts.map +1 -1
  122. package/dist/vue/components/MVirtualList.js +88 -67
  123. package/dist/vue/components/MVirtualList.js.map +1 -1
  124. package/dist/vue/components/advanced.d.ts +34 -34
  125. package/dist/vue/components/configurator.d.ts +33 -33
  126. package/dist/vue/components/creative.d.ts +20 -20
  127. package/dist/vue/components/dashboard.d.ts +12 -12
  128. package/dist/vue/components/dashboard.d.ts.map +1 -1
  129. package/dist/vue/components/dashboard.js +185 -69
  130. package/dist/vue/components/dashboard.js.map +1 -1
  131. package/dist/vue/components/discovery.d.ts +10 -10
  132. package/dist/vue/components/interactive.d.ts +6 -6
  133. package/dist/vue/components/layout.d.ts +45 -45
  134. package/dist/vue/components/layout.d.ts.map +1 -1
  135. package/dist/vue/components/layout.js +8 -2
  136. package/dist/vue/components/layout.js.map +1 -1
  137. package/dist/vue/components/navigation.d.ts +4 -4
  138. package/dist/vue/components/screen-components.d.ts +23 -23
  139. package/dist/vue/components/screen-components.d.ts.map +1 -1
  140. package/dist/vue/components/screen-components.js +15 -0
  141. package/dist/vue/components/screen-components.js.map +1 -1
  142. package/dist/vue/components/wayfinding.d.ts +11 -11
  143. package/dist/vue/components/workflows.d.ts +5 -5
  144. package/dist/vue/layout-context.d.ts +4 -0
  145. package/dist/vue/layout-context.d.ts.map +1 -0
  146. package/dist/vue/layout-context.js +3 -0
  147. package/dist/vue/layout-context.js.map +1 -0
  148. package/dist/vue/navigation.d.ts +1 -3
  149. package/dist/vue/navigation.d.ts.map +1 -1
  150. package/dist/vue/navigation.js +1 -15
  151. package/dist/vue/navigation.js.map +1 -1
  152. package/dist/vue/screen-overlay.d.ts +9 -0
  153. package/dist/vue/screen-overlay.d.ts.map +1 -0
  154. package/dist/vue/screen-overlay.js +17 -0
  155. package/dist/vue/screen-overlay.js.map +1 -0
  156. package/dist/vue/use-motion-surface.d.ts +2 -3
  157. package/dist/vue/use-motion-surface.d.ts.map +1 -1
  158. package/dist/vue/use-motion-surface.js +9 -134
  159. package/dist/vue/use-motion-surface.js.map +1 -1
  160. package/package.json +8 -4
  161. package/platforms/android/java/com/mavis/ui/Box.java +31 -0
  162. package/src/components/bindings.ts +32 -0
  163. package/src/components/contracts.ts +60 -0
  164. package/src/components/controller.ts +542 -0
  165. package/src/components/index.ts +136 -0
  166. package/src/components/style.ts +84 -0
  167. package/src/components/theme.ts +25 -0
  168. package/src/native/box.ios.ts +31 -1
  169. package/src/native/flex-layout.ios.ts +4 -0
  170. package/src/native/motion-binding.ts +143 -0
  171. package/src/native/motion-region.ts +1 -0
  172. package/src/native/motion-surface.ts +41 -8
  173. package/src/native/navigation.ts +15 -0
  174. package/src/native/pointer-availability.ts +44 -0
  175. package/src/native/scroll-motion.ts +60 -19
  176. package/src/native/section-scroll.ts +16 -6
  177. package/src/vue/card-input.ts +44 -0
  178. package/src/vue/components/MCard.ts +4 -3
  179. package/src/vue/components/MGrid.ts +10 -1
  180. package/src/vue/components/MScreen.ts +12 -1
  181. package/src/vue/components/MStack.ts +17 -2
  182. package/src/vue/components/MText.ts +4 -1
  183. package/src/vue/components/MVirtualList.ts +104 -78
  184. package/src/vue/components/dashboard.ts +192 -69
  185. package/src/vue/components/layout.ts +8 -0
  186. package/src/vue/components/screen-components.ts +14 -0
  187. package/src/vue/layout-context.ts +4 -0
  188. package/src/vue/navigation.ts +1 -15
  189. package/src/vue/screen-overlay.ts +20 -0
  190. package/src/vue/use-motion-surface.ts +10 -137
@@ -0,0 +1,542 @@
1
+ import {
2
+ GridLayout,
3
+ Label,
4
+ FormattedString,
5
+ Span,
6
+ isAndroid,
7
+ type View,
8
+ type EventData,
9
+ type PropertyChangeData,
10
+ type TextField,
11
+ type TextView,
12
+ } from '@nativescript/core'
13
+ import {
14
+ fontFamily,
15
+ iconFontFamily,
16
+ iconGlyph,
17
+ iconSizes,
18
+ checkIconOnly,
19
+ inputActionSize,
20
+ resolveDimension,
21
+ resolveRadius,
22
+ resolveSpace,
23
+ nonNegative,
24
+ validateInput,
25
+ type MavisTheme,
26
+ } from '@getcortexlabs/mavis-core'
27
+ import { resolvePerspective } from '@getcortexlabs/mavis-core/experimental'
28
+ import { MavisButton } from '../native/button'
29
+ import { MavisTextField } from '../native/text-field'
30
+ import { MavisTextView } from '../native/text-view'
31
+ import { attachPressFeedback } from '../native/press.js'
32
+ import { createNativeMotionSurface } from '../native/motion-binding.js'
33
+ import { attachNativeMotionRegion } from '../native/motion-region.js'
34
+ import { nativeDestination, navigateNative } from '../native/navigation.js'
35
+ import { keepEditingOn } from '../native/input-action'
36
+ import { MavisThemeScope } from './theme.js'
37
+ import {
38
+ normalizeComponentProps,
39
+ type NativeComponentName,
40
+ type NativeComponentProps,
41
+ type NativeComponentEvents,
42
+ type MBoxProps,
43
+ type MRowProps,
44
+ type MColumnProps,
45
+ type MButtonProps,
46
+ type MInputProps,
47
+ type MTextProps,
48
+ } from './contracts.js'
49
+ import { nativeBoxStyle, nativeButtonStyle, nativeTextStyle } from './style.js'
50
+
51
+ const controllers = new WeakMap<View, NativeComponentController>()
52
+ const alignment = { start: 'flex-start', center: 'center', end: 'flex-end', stretch: 'stretch' }
53
+ const distribution = {
54
+ start: 'flex-start',
55
+ center: 'center',
56
+ end: 'flex-end',
57
+ between: 'space-between',
58
+ around: 'space-around',
59
+ }
60
+ const style = (view: View, values: object) => Object.assign(view.style, values)
61
+
62
+ /** Owns only a view's presentation and native subscriptions, never application state. */
63
+ export class NativeComponentController {
64
+ props: Record<string, unknown> = {}
65
+ events: NativeComponentEvents = {}
66
+ private contentText = ''
67
+ private defaultScope = new MavisThemeScope()
68
+ private scope = this.defaultScope
69
+ private offTheme?: () => void
70
+ private press?: ReturnType<typeof attachPressFeedback>
71
+ private offRegion?: () => void
72
+ private offAction?: () => void
73
+ private ready = false
74
+ private loaded = false
75
+ private disposed = false
76
+ private painting = false
77
+ private focused = false
78
+ private focusFeedback?: ReturnType<typeof setTimeout>
79
+ private editor?: TextField | TextView
80
+ private title?: Label
81
+ private message?: Label
82
+ private leading?: Label
83
+ private trailing?: Label
84
+ private action?: InstanceType<typeof MavisButton>
85
+ private subscriptions: Array<() => void> = []
86
+ private editorSubscriptions: Array<() => void> = []
87
+ private motion = createNativeMotionSurface(
88
+ () => (this.props as MBoxProps).motionSurface,
89
+ () => this.enabled,
90
+ () => this.name === 'MText',
91
+ false,
92
+ () => resolvePerspective((this.props as MBoxProps).perspective),
93
+ )
94
+
95
+ constructor(
96
+ readonly view: View,
97
+ readonly name: NativeComponentName,
98
+ ) {
99
+ controllers.set(view, this)
100
+ this.listen(view, 'loaded', this.load)
101
+ this.listen(view, 'unloaded', this.unload)
102
+ if (name === 'MButton') this.listen(view, 'tap', this.activate)
103
+ }
104
+
105
+ get enabled() {
106
+ return !this.props.disabled && !this.props.loading
107
+ }
108
+ get theme() {
109
+ return this.scope.theme
110
+ }
111
+ get themeScope() {
112
+ return this.scope
113
+ }
114
+ setContentText(text: string) {
115
+ this.contentText = text
116
+ this.render()
117
+ }
118
+
119
+ setTheme(scope: MavisThemeScope | undefined) {
120
+ scope ??= this.defaultScope
121
+ if (scope === this.scope) return
122
+ this.offTheme?.()
123
+ this.scope = scope
124
+ this.offTheme = this.loaded ? this.scope.subscribe(this.render) : undefined
125
+ this.render()
126
+ }
127
+
128
+ setProps(props: NativeComponentProps<NativeComponentName>) {
129
+ if (this.disposed) throw new Error('Cannot update a disposed Mavis component')
130
+ const previous = this.props
131
+ const next = normalizeComponentProps(this.name, props) as Record<string, unknown>
132
+ if (this.name === 'MInput') validateInput(next as Required<MInputProps>)
133
+ // These are separate component compositions, not aliases for a plain button.
134
+ if (this.name === 'MButton' && next.reorderHandle)
135
+ throw new TypeError(
136
+ 'Native foundation MButton does not implement reorderHandle; use the declared component capability',
137
+ )
138
+ if (this.name === 'MButton' && next.loadingIndicator !== 'none')
139
+ throw new TypeError(
140
+ 'Native foundation MButton supports loading text; loadingIndicator must be none',
141
+ )
142
+ this.props = next
143
+ this.ready = true
144
+ if (this.name === 'MInput' && (this.props.disabled || this.props.readonly)) this.blur()
145
+ this.render()
146
+ if (this.loaded) {
147
+ if (previous.motionSurface !== this.props.motionSurface) this.motion.refresh()
148
+ if (previous.motionRegion !== this.props.motionRegion) this.bindRegion()
149
+ this.motion.refreshEnabled()
150
+ this.motion.refreshPerspective()
151
+ }
152
+ }
153
+
154
+ private listen(view: View, event: string, listener: (event: EventData) => void) {
155
+ view.on(event, listener)
156
+ this.subscriptions.push(() => view.off(event, listener))
157
+ }
158
+
159
+ private load = () => {
160
+ if (this.disposed || this.loaded) return
161
+ this.loaded = true
162
+ this.offTheme = this.scope.subscribe(this.render)
163
+ this.render()
164
+ this.bindRegion()
165
+ this.motion.onLoaded({ object: this.view, eventName: 'loaded' })
166
+ }
167
+
168
+ private bindRegion() {
169
+ this.offRegion?.()
170
+ this.offRegion = undefined
171
+ const region = (this.props as MBoxProps).motionRegion
172
+ if (region && this.loaded) this.offRegion = attachNativeMotionRegion(region, this.view)
173
+ }
174
+
175
+ private unload = () => {
176
+ this.loaded = false
177
+ this.offTheme?.()
178
+ this.offTheme = undefined
179
+ this.press?.()
180
+ this.press = undefined
181
+ this.offRegion?.()
182
+ this.offRegion = undefined
183
+ this.offAction?.()
184
+ this.offAction = undefined
185
+ this.motion.onUnloaded()
186
+ clearTimeout(this.focusFeedback)
187
+ this.focusFeedback = undefined
188
+ this.focused = false
189
+ }
190
+
191
+ dispose() {
192
+ if (this.disposed) return
193
+ this.unload()
194
+ this.disposed = true
195
+ for (const off of this.subscriptions.splice(0)) off()
196
+ for (const off of this.editorSubscriptions.splice(0)) off()
197
+ this.events = {}
198
+ controllers.delete(this.view)
199
+ }
200
+
201
+ focus() {
202
+ if (this.loaded && this.editor && !this.props.disabled && !this.props.readonly)
203
+ this.editor.focus()
204
+ }
205
+ blur() {
206
+ this.editor?.dismissSoftInput()
207
+ this.editor?.android?.clearFocus()
208
+ }
209
+ focusAction() {
210
+ if (this.loaded && this.action?.isEnabled) this.action.focus()
211
+ }
212
+
213
+ private activate = () => {
214
+ if (!this.loaded || !this.enabled || !this.motion.allowsActivation()) return
215
+ this.events.activate?.()
216
+ const props = this.props as MButtonProps
217
+ if (props.href !== undefined)
218
+ void navigateNative(props).catch((error) => this.events.error?.(error))
219
+ }
220
+
221
+ private shrink() {
222
+ const parent = this.view.parent as View | undefined
223
+ return parent && controllers.get(parent)?.name === 'MRow' ? 1 : 0
224
+ }
225
+
226
+ private render = () => {
227
+ if (!this.ready || this.disposed || this.painting) return
228
+ this.painting = true
229
+ try {
230
+ const props = this.props
231
+ const theme = this.theme
232
+ this.view.id = (props.id as string | undefined) ?? ''
233
+ this.view.accessibilityHidden = (props.accessibilityHidden as boolean | undefined) ?? false
234
+ this.view.automationText = (props.id as string | undefined) ?? ''
235
+ if (this.name === 'MBox' || this.name === 'MRow' || this.name === 'MColumn') {
236
+ style(this.view, nativeBoxStyle(theme, props as MBoxProps, this.shrink()))
237
+ this.view.accessibilityLabel = (props.accessibilityLabel as string | undefined) ?? ''
238
+ if (this.name !== 'MBox') {
239
+ const layout = props as Required<MRowProps | MColumnProps>
240
+ style(this.view, {
241
+ flexDirection: this.name === 'MRow' ? 'row' : 'column',
242
+ alignItems: alignment[layout.align],
243
+ justifyContent: distribution[layout.justify],
244
+ flexWrap: layout.wrap ? 'wrap' : 'nowrap',
245
+ gap: resolveSpace(theme, layout.gap ?? 'none'),
246
+ })
247
+ }
248
+ } else if (this.name === 'MText') {
249
+ const text = props as Required<MTextProps>
250
+ const label = this.view as Label
251
+ label.text = text.text ?? this.contentText
252
+ label.textWrap = text.wrap
253
+ label.accessibilityLabel = text.accessibilityLabel ?? label.text
254
+ label.accessibilityRole = /^h[1-6]$/.test(text.as) ? 'header' : 'text'
255
+ style(label, {
256
+ ...nativeTextStyle(theme, text, this.shrink()),
257
+ verticalAlignment:
258
+ controllers.get(label.parent as View)?.name === 'MBox' ? 'top' : 'stretch',
259
+ })
260
+ } else if (this.name === 'MButton') {
261
+ this.paintButton(theme)
262
+ } else this.paintInput(theme)
263
+ } finally {
264
+ this.painting = false
265
+ }
266
+ }
267
+
268
+ private paintButton(theme: MavisTheme) {
269
+ const props = this.props as Required<MButtonProps>
270
+ if (props.href !== undefined) nativeDestination(props)
271
+ const button = this.view as InstanceType<typeof MavisButton>
272
+ const text = props.loading ? props.loadingText : (props.text ?? this.contentText)
273
+ checkIconOnly(text, !!(props.icon || props.trailingIcon), props.accessibilityLabel)
274
+ button.text = text
275
+ button.textWrap = true
276
+ button.isEnabled = this.enabled
277
+ button.accessibilityRole = props.href === undefined ? 'button' : 'link'
278
+ button.accessibilityLabel = props.accessibilityLabel ?? text
279
+ button.accessibilityValue = props.accessibilityLabel ? text : ''
280
+ button.accessibilityState = this.enabled ? '' : 'disabled'
281
+ style(button, nativeButtonStyle(theme, props, this.shrink()))
282
+ if (props.icon || props.trailingIcon) {
283
+ const formatted = new FormattedString()
284
+ const add = (text: string, icon = false) => {
285
+ const span = new Span()
286
+ span.text = text
287
+ if (icon) {
288
+ span.fontFamily = iconFontFamily
289
+ span.fontSize = iconSizes[props.size]
290
+ }
291
+ formatted.spans.push(span)
292
+ }
293
+ if (props.icon) add(iconGlyph(props.icon), true)
294
+ if (text) add(`${props.icon ? ' ' : ''}${text}${props.trailingIcon ? ' ' : ''}`)
295
+ if (props.trailingIcon) add(iconGlyph(props.trailingIcon), true)
296
+ button.formattedText = formatted
297
+ } else button.formattedText = null as unknown as FormattedString
298
+ if (this.loaded && this.enabled) {
299
+ if (this.press) this.press.update(theme.motion)
300
+ else this.press = attachPressFeedback(button, theme.motion)
301
+ } else {
302
+ this.press?.()
303
+ this.press = undefined
304
+ }
305
+ }
306
+
307
+ private createEditor(multiline: boolean) {
308
+ this.offAction?.()
309
+ this.offAction = undefined
310
+ for (const off of this.editorSubscriptions.splice(0)) off()
311
+ const root = this.view as GridLayout
312
+ if (this.editor) root.removeChild(this.editor)
313
+ const editor = multiline ? new MavisTextView() : new MavisTextField()
314
+ this.editor = editor
315
+ const listen = (event: string, fn: (event: EventData) => void) => {
316
+ editor.on(event, fn)
317
+ this.editorSubscriptions.push(() => editor.off(event, fn))
318
+ }
319
+ GridLayout.setRow(editor, 1)
320
+ root.addChild(editor)
321
+ listen('textChange', (event: EventData) => {
322
+ if (this.painting || !this.loaded || this.props.disabled || this.props.readonly) return
323
+ const value = String((event as PropertyChangeData).value ?? '')
324
+ if (value === this.props.modelValue) return
325
+ this.events.valueChange?.(value)
326
+ // A declined controlled edit must not become independent application state.
327
+ void Promise.resolve().then(() => {
328
+ if (this.loaded && this.editor === editor) this.render()
329
+ })
330
+ })
331
+ listen('focus', () => {
332
+ if (!this.loaded) return
333
+ clearTimeout(this.focusFeedback)
334
+ if (editor.android) {
335
+ this.focusFeedback = setTimeout(() => {
336
+ if (this.loaded && editor.android?.hasFocus()) {
337
+ this.focused = true
338
+ this.render()
339
+ }
340
+ }, 0)
341
+ } else {
342
+ this.focused = true
343
+ this.render()
344
+ }
345
+ this.events.focus?.()
346
+ })
347
+ listen('blur', () => {
348
+ clearTimeout(this.focusFeedback)
349
+ this.focused = false
350
+ this.render()
351
+ if (this.loaded) this.events.blur?.()
352
+ })
353
+ if (!multiline)
354
+ listen('returnPress', () => {
355
+ if (
356
+ this.loaded &&
357
+ !this.props.disabled &&
358
+ !this.props.readonly &&
359
+ !editor.ios?.markedTextRange
360
+ )
361
+ this.events.submit?.(editor.text ?? '')
362
+ })
363
+ }
364
+
365
+ private paintInput(theme: MavisTheme) {
366
+ const props = this.props as Required<MInputProps>
367
+ const root = this.view as GridLayout
368
+ if (!this.title) {
369
+ root.rows = 'auto,auto,auto'
370
+ this.title = new Label()
371
+ this.title.accessibilityHidden = true
372
+ this.message = new Label()
373
+ this.message.textWrap = true
374
+ GridLayout.setRow(this.message, 2)
375
+ root.addChild(this.title)
376
+ root.addChild(this.message)
377
+ }
378
+ if (!this.editor || this.editor instanceof MavisTextView !== props.multiline)
379
+ this.createEditor(props.multiline)
380
+ const editor = this.editor!
381
+ const tokens = theme.components.input
382
+ const fontSize = tokens.fontSize ?? theme.typography.body.fontSize
383
+ const message = props.error || props.helperText
384
+ this.title.text = props.label
385
+ style(this.title, { ...nativeTextStyle(theme, { variant: 'caption' }), fontWeight: '600' })
386
+ this.message!.text = message
387
+ this.message!.visibility = message ? 'visible' : 'collapse'
388
+ style(this.message!, {
389
+ ...nativeTextStyle(theme, { variant: 'caption', tone: props.error ? 'danger' : 'muted' }),
390
+ marginTop: theme.space.sm,
391
+ })
392
+ style(root, {
393
+ width: resolveDimension(props.width) ?? 'auto',
394
+ flexGrow: nonNegative(props.grow, 'grow'),
395
+ flexShrink: nonNegative(props.shrink ?? this.shrink(), 'shrink'),
396
+ minWidth: 0,
397
+ opacity: props.disabled ? 0.5 : 1,
398
+ })
399
+ if (editor.text !== props.modelValue) editor.text = props.modelValue
400
+ editor.id = props.id ?? ''
401
+ editor.automationText = props.id ?? ''
402
+ root.id = props.id ? `${props.id}-field` : ''
403
+ root.automationText = root.id
404
+ editor.hint = props.placeholder
405
+ editor.editable = !props.disabled && !props.readonly
406
+ editor.isEnabled = !props.disabled
407
+ editor.isUserInteractionEnabled = !props.disabled
408
+ editor.accessibilityLabel = props.label
409
+ editor.accessibilityHint = message
410
+ editor.accessibilityHidden = props.accessibilityHidden ?? false
411
+ editor.updateTextTrigger = 'textChanged'
412
+ if (!props.multiline) {
413
+ const field = editor as TextField
414
+ field.secure = props.type === 'password'
415
+ field.closeOnReturn = false
416
+ field.returnKeyType = props.returnKey
417
+ }
418
+ editor.set(
419
+ 'keyboardType',
420
+ props.inputmode === 'decimal'
421
+ ? 'decimal'
422
+ : props.inputmode === 'numeric'
423
+ ? 'number'
424
+ : props.type === 'tel'
425
+ ? 'phone'
426
+ : props.type === 'email' || props.type === 'url'
427
+ ? props.type
428
+ : undefined,
429
+ )
430
+ const text = props.type === 'text' || props.type === 'search'
431
+ const capitalize = props.autocapitalize ?? (text ? 'sentences' : 'none')
432
+ editor.autocapitalizationType = capitalize === 'characters' ? 'allcharacters' : capitalize
433
+ editor.autocorrect = props.autocorrect ?? text
434
+ editor.maxLength = props.maxLength ?? Infinity
435
+ const inset = tokens.paddingX + iconSizes.md + 10
436
+ style(editor, {
437
+ minWidth: 0,
438
+ minHeight: props.multiline
439
+ ? props.rows * fontSize * tokens.lineHeight + tokens.paddingY * 2 + 2
440
+ : Math.max(theme.control.md.height, props.trailingAction ? inputActionSize + 8 : 0),
441
+ paddingTop: tokens.paddingY,
442
+ paddingBottom: tokens.paddingY,
443
+ paddingLeft: props.icon ? inset : tokens.paddingX,
444
+ paddingRight: props.trailingAction
445
+ ? Math.max(inputActionSize + 12, tokens.paddingX)
446
+ : props.trailingIcon
447
+ ? inset
448
+ : tokens.paddingX,
449
+ margin: 0,
450
+ marginTop: theme.space.sm,
451
+ fontSize,
452
+ fontFamily: fontFamily(
453
+ tokens.fontFamily ?? theme.typography.body.fontFamily,
454
+ isAndroid ? 'android' : 'ios',
455
+ ),
456
+ fontWeight: tokens.fontWeight,
457
+ lineHeight: fontSize * (tokens.lineHeight - 1),
458
+ color: theme.colors.text,
459
+ placeholderColor: theme.colors.muted,
460
+ backgroundColor: props.readonly ? theme.colors.elevated : theme.colors.surface,
461
+ borderColor: props.error
462
+ ? theme.colors.danger
463
+ : this.focused
464
+ ? theme.colors.primary
465
+ : theme.colors.border,
466
+ borderWidth: 2,
467
+ borderRadius: resolveRadius(theme, tokens.radius),
468
+ })
469
+ const icon = (key: 'leading' | 'trailing', name: MInputProps['icon']) => {
470
+ let view = this[key]
471
+ if (!view && name) {
472
+ view = this[key] = new Label()
473
+ view.accessibilityHidden = true
474
+ view.isUserInteractionEnabled = false
475
+ GridLayout.setRow(view, 1)
476
+ root.addChild(view)
477
+ }
478
+ if (!view) return
479
+ view.visibility = name ? 'visible' : 'collapse'
480
+ view.text = name ? iconGlyph(name) : ''
481
+ style(view, {
482
+ fontFamily: iconFontFamily,
483
+ fontSize: iconSizes.md,
484
+ color: props.error ? theme.colors.danger : theme.colors.muted,
485
+ horizontalAlignment: key === 'leading' ? 'left' : 'right',
486
+ verticalAlignment: props.multiline ? 'top' : 'middle',
487
+ marginLeft: key === 'leading' ? tokens.paddingX : 0,
488
+ marginRight: key === 'trailing' ? tokens.paddingX : 0,
489
+ marginTop: theme.space.sm + (props.multiline ? 14 : 0),
490
+ })
491
+ }
492
+ icon('leading', props.icon)
493
+ icon('trailing', props.trailingIcon)
494
+ const action = props.trailingAction
495
+ if (action && !this.action) {
496
+ this.action = new MavisButton()
497
+ GridLayout.setRow(this.action, 1)
498
+ root.addChild(this.action)
499
+ this.listen(this.action, 'tap', () => {
500
+ if (
501
+ this.loaded &&
502
+ !this.props.disabled &&
503
+ !this.props.readonly &&
504
+ !!(this.props as MInputProps).trailingAction &&
505
+ !(this.props as MInputProps).trailingAction?.disabled
506
+ )
507
+ this.events.action?.()
508
+ })
509
+ }
510
+ if (this.action) {
511
+ this.action.visibility = action ? 'visible' : 'collapse'
512
+ this.action.isEnabled = !!action && !props.disabled && !props.readonly && !action.disabled
513
+ this.action.accessibilityLabel = action?.label ?? ''
514
+ this.action.text = action ? iconGlyph(action.icon) : ''
515
+ style(this.action, {
516
+ fontFamily: iconFontFamily,
517
+ fontSize: iconSizes.md,
518
+ color: theme.colors.text,
519
+ backgroundColor: 'transparent',
520
+ borderWidth: 0,
521
+ width: inputActionSize,
522
+ minHeight: inputActionSize,
523
+ padding: 0,
524
+ margin: 0,
525
+ marginRight: 4,
526
+ marginTop: theme.space.sm + (props.multiline ? 4 : 0),
527
+ horizontalAlignment: 'right',
528
+ verticalAlignment: props.multiline ? 'top' : 'middle',
529
+ })
530
+ if (this.loaded && action && !this.offAction)
531
+ this.offAction = keepEditingOn(this.action, () => this.editor)
532
+ if (!action) {
533
+ this.offAction?.()
534
+ this.offAction = undefined
535
+ }
536
+ }
537
+ }
538
+ }
539
+
540
+ export function nativeComponentController(view: View) {
541
+ return controllers.get(view)
542
+ }
@@ -0,0 +1,136 @@
1
+ import { GridLayout, View, type LayoutBase, type Label, type Button } from '@nativescript/core'
2
+ import { MavisFlexLayout } from '../native/flex-layout'
3
+ import { MavisBox } from '../native/box'
4
+ import { MavisButton } from '../native/button'
5
+ import { MavisLabel } from '../native/label'
6
+ import { NativeComponentController } from './controller.js'
7
+ import { createMavisTheme, MavisThemeScope } from './theme.js'
8
+ import type {
9
+ NativeComponentName,
10
+ NativeComponentProps,
11
+ NativeComponentEvents,
12
+ NativeComponentOptions,
13
+ } from './contracts.js'
14
+ import type { MavisTheme } from '@getcortexlabs/mavis-core'
15
+
16
+ export * from './contracts.js'
17
+ export { componentEvents, plainComponentText, type ComponentCallbacks } from './bindings.js'
18
+ export { createMavisTheme, MavisThemeScope } from './theme.js'
19
+
20
+ /** Framework bridges assign an immutable props snapshot; native view attributes stay separate. */
21
+ export interface MavisNativeView extends View {
22
+ mavisProps: NativeComponentProps<NativeComponentName>
23
+ mavisTheme: MavisThemeScope | undefined
24
+ mavisEvents: NativeComponentEvents
25
+ mavisContentText: string
26
+ disposeMavis(): void
27
+ focusInput(): void
28
+ blurInput(): void
29
+ focusInputAction(): void
30
+ }
31
+
32
+ export type NativeViewFor<N extends NativeComponentName> = MavisNativeView &
33
+ (N extends 'MBox' | 'MRow' | 'MColumn' | 'MInput'
34
+ ? LayoutBase
35
+ : N extends 'MText'
36
+ ? Label
37
+ : Button)
38
+
39
+ function componentClass(
40
+ name: NativeComponentName,
41
+ Base: new () => View,
42
+ ): new () => MavisNativeView {
43
+ return class extends Base {
44
+ private mavisController = new NativeComponentController(this, name)
45
+ set mavisProps(props: NativeComponentProps<NativeComponentName>) {
46
+ this.mavisController.setProps(props)
47
+ }
48
+ get mavisProps() {
49
+ return this.mavisController.props as NativeComponentProps<NativeComponentName>
50
+ }
51
+ set mavisTheme(scope: MavisThemeScope | undefined) {
52
+ this.mavisController.setTheme(scope)
53
+ }
54
+ get mavisTheme(): MavisThemeScope {
55
+ return this.mavisController.themeScope
56
+ }
57
+ set mavisEvents(events: NativeComponentEvents) {
58
+ this.mavisController.events = events ?? {}
59
+ }
60
+ get mavisEvents() {
61
+ return this.mavisController.events
62
+ }
63
+ set mavisContentText(text: string) {
64
+ this.mavisController.setContentText(text)
65
+ }
66
+ disposeMavis() {
67
+ this.mavisController.dispose()
68
+ }
69
+ focusInput() {
70
+ this.mavisController.focus()
71
+ }
72
+ blurInput() {
73
+ this.mavisController.blur()
74
+ }
75
+ focusInputAction() {
76
+ this.mavisController.focusAction()
77
+ }
78
+ }
79
+ }
80
+
81
+ export const nativeComponents = {
82
+ MBox: componentClass('MBox', MavisBox),
83
+ MRow: componentClass('MRow', MavisFlexLayout),
84
+ MColumn: componentClass('MColumn', MavisFlexLayout),
85
+ MText: componentClass('MText', MavisLabel),
86
+ MButton: componentClass('MButton', MavisButton),
87
+ MInput: componentClass('MInput', GridLayout),
88
+ } as const
89
+
90
+ /** Direct JS/TS integration. The owner decides whether to accept controlled value requests. */
91
+ export function createNativeComponent<N extends NativeComponentName>(
92
+ name: N,
93
+ props: NativeComponentProps<N>,
94
+ options: NativeComponentOptions = {},
95
+ ) {
96
+ const view = new nativeComponents[name]() as NativeViewFor<N>
97
+ view.mavisTheme = options.theme
98
+ view.mavisEvents = options.events ?? {}
99
+ view.mavisProps = props
100
+ return {
101
+ view,
102
+ update(next: NativeComponentProps<N>) {
103
+ view.mavisProps = next
104
+ },
105
+ setEvents(events: NativeComponentEvents) {
106
+ view.mavisEvents = events
107
+ },
108
+ focus() {
109
+ view.focusInput()
110
+ },
111
+ blur() {
112
+ view.blurInput()
113
+ },
114
+ focusAction() {
115
+ view.focusInputAction()
116
+ },
117
+ dispose() {
118
+ view.disposeMavis()
119
+ },
120
+ }
121
+ }
122
+
123
+ export function createMavis(options: { theme?: MavisTheme } = {}) {
124
+ const scope = createMavisTheme(options.theme)
125
+ return {
126
+ theme: scope,
127
+ setTheme: (theme: MavisTheme) => scope.setTheme(theme),
128
+ create<N extends NativeComponentName>(
129
+ name: N,
130
+ props: NativeComponentProps<N>,
131
+ events?: NativeComponentEvents,
132
+ ) {
133
+ return createNativeComponent(name, props, { theme: scope, events })
134
+ },
135
+ }
136
+ }