@getcortexlabs/nitroui-vue 0.0.0-stage → 0.1.0
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.
- package/README.md +33 -2
- package/package.json +23 -4
- package/src/components.ts +38 -0
- package/src/index.ts +17 -0
- package/src/layout.ts +34 -0
- package/src/renderer.ts +170 -0
package/README.md
CHANGED
|
@@ -1,3 +1,34 @@
|
|
|
1
|
-
#
|
|
1
|
+
# NitroUI Vue
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A community Vue 3 renderer for NitroUI. Use `View`, `Text` and `TextInput` to build native iOS and Android screens.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
In a NitroUI 0.3.0 app:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
bun add @getcortexlabs/nitroui-vue @vue/runtime-core@3.5.46
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Mount a screen
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { defineComponent, h, ref } from '@vue/runtime-core'
|
|
17
|
+
import { registerApp, Text, TextInput, View } from '@getcortexlabs/nitroui-vue'
|
|
18
|
+
|
|
19
|
+
const App = defineComponent(() => {
|
|
20
|
+
const name = ref('')
|
|
21
|
+
return () => h(View, { layout: { padding: 24, gap: 12 } }, () => [
|
|
22
|
+
h(Text, null, () => name.value ? `Hello, ${name.value}!` : 'Hello, Vue!'),
|
|
23
|
+
h(TextInput, {
|
|
24
|
+
placeholder: 'Your name',
|
|
25
|
+
modelValue: name.value,
|
|
26
|
+
'onUpdate:modelValue': (value: string) => { name.value = value },
|
|
27
|
+
}),
|
|
28
|
+
])
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
registerApp(App)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
For a Vue template with `<script setup>` and `v-model`, see the [single-file component and compiler setup](https://github.com/getcortexlabs/nitroui-vue/tree/main/example). The package uses the public NitroUI native bindings and ships TypeScript for the NitroUI bundler.
|
package/package.json
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@getcortexlabs/nitroui-vue",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Experimental Vue 3 renderer for NitroUI.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": "./src/index.ts",
|
|
7
|
+
"peerDependencies": {
|
|
8
|
+
"@vue/runtime-core": "^3.5.46",
|
|
9
|
+
"nitro-ui-layout": "0.3.0",
|
|
10
|
+
"nitro-ui-renderer": "0.3.0",
|
|
11
|
+
"nitro-ui-runtime": "0.3.0"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src"
|
|
15
|
+
],
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/getcortexlabs/nitroui-vue.git",
|
|
22
|
+
"directory": "packages/nitroui-vue"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/getcortexlabs/nitroui-vue#readme"
|
|
25
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { defineComponent, h, type FunctionalComponent, type PropType } from '@vue/runtime-core'
|
|
2
|
+
import type { Layout } from './layout'
|
|
3
|
+
|
|
4
|
+
interface ViewProps {
|
|
5
|
+
layout?: Layout
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface TextProps extends ViewProps {
|
|
9
|
+
fontSize?: number
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function hostProps(props: ViewProps) {
|
|
13
|
+
// Read reactive layout fields during render and retain their previous values for patching.
|
|
14
|
+
return props.layout ? { ...props, layout: { ...props.layout } } : props
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const View: FunctionalComponent<ViewProps> = (props, { slots }) =>
|
|
18
|
+
h('nitro-view', hostProps(props), slots.default?.())
|
|
19
|
+
|
|
20
|
+
export const Text: FunctionalComponent<TextProps> = (props, { slots }) =>
|
|
21
|
+
h('nitro-text', hostProps(props), slots.default?.())
|
|
22
|
+
|
|
23
|
+
View.inheritAttrs = false
|
|
24
|
+
Text.inheritAttrs = false
|
|
25
|
+
|
|
26
|
+
export const TextInput = defineComponent({
|
|
27
|
+
name: 'TextInput',
|
|
28
|
+
props: {
|
|
29
|
+
modelValue: String,
|
|
30
|
+
placeholder: String,
|
|
31
|
+
layout: Object as PropType<Layout>,
|
|
32
|
+
},
|
|
33
|
+
emits: ['update:modelValue'],
|
|
34
|
+
setup(props, { emit }) {
|
|
35
|
+
const onEdit = (text: string) => emit('update:modelValue', text)
|
|
36
|
+
return () => h('nitro-input', { ...hostProps(props), onEdit })
|
|
37
|
+
},
|
|
38
|
+
})
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import 'nitro-ui-runtime'
|
|
2
|
+
import type { Component } from '@vue/runtime-core'
|
|
3
|
+
import { Application, ViewController, Window } from 'nitro-ui-renderer/hybrids'
|
|
4
|
+
import { mount } from './renderer'
|
|
5
|
+
|
|
6
|
+
export { Text, TextInput, View } from './components'
|
|
7
|
+
export type { Layout } from './layout'
|
|
8
|
+
export { mount }
|
|
9
|
+
|
|
10
|
+
export function registerApp(component: Component) {
|
|
11
|
+
Application.registerRoot(scene => {
|
|
12
|
+
const controller = new ViewController()
|
|
13
|
+
controller.view.setBackgroundColor(0xffffffff)
|
|
14
|
+
mount(component, controller)
|
|
15
|
+
return new Window(scene, controller)
|
|
16
|
+
})
|
|
17
|
+
}
|
package/src/layout.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { NLNodeRef } from 'nitro-ui-layout'
|
|
2
|
+
|
|
3
|
+
export interface Layout {
|
|
4
|
+
width?: number
|
|
5
|
+
height?: number
|
|
6
|
+
padding?: number
|
|
7
|
+
gap?: number
|
|
8
|
+
flexGrow?: number
|
|
9
|
+
justifyContent?: 'flex-start' | 'center' | 'flex-end'
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function updateLayout(node: NLNodeRef, next: Layout = {}, previous: Layout = {}) {
|
|
13
|
+
const api = nitroLayout
|
|
14
|
+
if (next.width !== previous.width) api.NLSetWidth(node, next.width ?? NaN, api.NLPoint)
|
|
15
|
+
if (next.height !== previous.height) api.NLSetHeight(node, next.height ?? NaN, api.NLPoint)
|
|
16
|
+
if (next.flexGrow !== previous.flexGrow) api.NLSetFlexGrow(node, next.flexGrow ?? 0)
|
|
17
|
+
if (next.gap !== previous.gap) {
|
|
18
|
+
api.NLSetColumnGap(node, next.gap ?? 0, api.NLPoint)
|
|
19
|
+
api.NLSetRowGap(node, next.gap ?? 0, api.NLPoint)
|
|
20
|
+
}
|
|
21
|
+
if (next.padding !== previous.padding) {
|
|
22
|
+
for (const edge of [api.NLLeft, api.NLTop, api.NLRight, api.NLBottom]) {
|
|
23
|
+
api.NLSetPadding(node, edge, next.padding ?? 0, api.NLPoint)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (next.justifyContent !== previous.justifyContent) {
|
|
27
|
+
const positions = {
|
|
28
|
+
'flex-start': api.NLJustifyStart,
|
|
29
|
+
'center': api.NLJustifyCenter,
|
|
30
|
+
'flex-end': api.NLJustifyEnd,
|
|
31
|
+
}
|
|
32
|
+
api.NLSetJustifyContent(node, positions[next.justifyContent ?? 'flex-start'])
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/renderer.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { createRenderer, type Component } from '@vue/runtime-core'
|
|
2
|
+
import { Application, Label, type TextInput, type View, type ViewController } from 'nitro-ui-renderer/hybrids'
|
|
3
|
+
import { updateLayout } from './layout'
|
|
4
|
+
|
|
5
|
+
type Element = {
|
|
6
|
+
parent: Element | null
|
|
7
|
+
children: HostNode[]
|
|
8
|
+
} & (
|
|
9
|
+
| { kind: 'view'; native: View }
|
|
10
|
+
| { kind: 'label'; native: Label }
|
|
11
|
+
| { kind: 'input'; native: TextInput; stopObserving: () => void; onEdit?: (text: string) => void }
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
type Marker = { kind: 'text' | 'comment'; value: string; parent: Element | null }
|
|
15
|
+
type HostNode = Element | Marker
|
|
16
|
+
|
|
17
|
+
function isElement(node: HostNode): node is Element {
|
|
18
|
+
return 'native' in node
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function layoutNode(element: Element) {
|
|
22
|
+
return nitroLayout.NLNodeFromHandle(element.native.layoutNodePtr)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function syncLabel(parent: Element) {
|
|
26
|
+
if (parent.kind === 'label') {
|
|
27
|
+
parent.native.text = parent.children.map(child => child.kind === 'text' ? child.value : '').join('')
|
|
28
|
+
parent.native.afterUpdate()
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function detach(child: HostNode) {
|
|
33
|
+
const parent = child.parent
|
|
34
|
+
if (!parent) return
|
|
35
|
+
if (isElement(child)) {
|
|
36
|
+
parent.native.removeChild(child.native)
|
|
37
|
+
nitroLayout.NLRemoveChild(layoutNode(parent), layoutNode(child))
|
|
38
|
+
}
|
|
39
|
+
parent.children.splice(parent.children.indexOf(child), 1)
|
|
40
|
+
child.parent = null
|
|
41
|
+
syncLabel(parent)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function dispose(node: HostNode) {
|
|
45
|
+
if (!isElement(node)) return
|
|
46
|
+
if (node.kind === 'input') node.stopObserving()
|
|
47
|
+
node.children.forEach(dispose)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Mount a Vue tree into an existing native controller. Call app.unmount() to detach it. */
|
|
51
|
+
export function mount(component: Component, controller: ViewController) {
|
|
52
|
+
const root: Element = { kind: 'view', native: controller.view, parent: null, children: [] }
|
|
53
|
+
let queued = false
|
|
54
|
+
let active = true
|
|
55
|
+
function requestLayout() {
|
|
56
|
+
if (queued || !active) return
|
|
57
|
+
queued = true
|
|
58
|
+
queueMicrotask(() => {
|
|
59
|
+
queued = false
|
|
60
|
+
if (!active) return
|
|
61
|
+
const { width, height } = root.native.getFrame()
|
|
62
|
+
const node = layoutNode(root)
|
|
63
|
+
nitroLayout.NLSolve(node, width, height)
|
|
64
|
+
nitroLayout.NLApplyLayout(node)
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const renderer = createRenderer<HostNode, Element>({
|
|
69
|
+
createElement(tag) {
|
|
70
|
+
const base = { parent: null, children: [] }
|
|
71
|
+
switch (tag) {
|
|
72
|
+
case 'nitro-view': return { ...base, kind: 'view', native: Application.createView() }
|
|
73
|
+
case 'nitro-text': return { ...base, kind: 'label', native: new Label() }
|
|
74
|
+
case 'nitro-input': {
|
|
75
|
+
const native = Application.createTextInput(false)
|
|
76
|
+
const size = native.addOnLayoutInvalidatedListener(requestLayout)
|
|
77
|
+
const element: Element & { kind: 'input' } = {
|
|
78
|
+
...base, kind: 'input', native,
|
|
79
|
+
stopObserving() {
|
|
80
|
+
size.remove()
|
|
81
|
+
edits.remove()
|
|
82
|
+
element.onEdit = undefined
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
const edits = native.addOnTextChangedListener(change => element.onEdit?.(change.text))
|
|
86
|
+
return element
|
|
87
|
+
}
|
|
88
|
+
default: throw new Error(`Unsupported NitroUI element: ${tag}`)
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
createText: value => ({ kind: 'text', value, parent: null }),
|
|
92
|
+
createComment: value => ({ kind: 'comment', value, parent: null }),
|
|
93
|
+
parentNode: node => node.parent,
|
|
94
|
+
nextSibling(node) {
|
|
95
|
+
const siblings = node.parent?.children
|
|
96
|
+
return siblings?.[siblings.indexOf(node) + 1] ?? null
|
|
97
|
+
},
|
|
98
|
+
insert(child, parent, anchor) {
|
|
99
|
+
if (anchor === child) return
|
|
100
|
+
if (isElement(child) && parent.kind !== 'view') {
|
|
101
|
+
throw new Error('Only View can contain native elements.')
|
|
102
|
+
}
|
|
103
|
+
if (child.kind === 'text' && child.value.trim() && parent.kind !== 'label') {
|
|
104
|
+
throw new Error('Wrap text in a Text component.')
|
|
105
|
+
}
|
|
106
|
+
detach(child)
|
|
107
|
+
// Vue uses empty text nodes and comments as fragment and v-if anchors.
|
|
108
|
+
const index = anchor ? parent.children.indexOf(anchor) : parent.children.length
|
|
109
|
+
if (isElement(child)) {
|
|
110
|
+
const next = parent.children.slice(index).find(isElement)
|
|
111
|
+
if (next) {
|
|
112
|
+
parent.native.insertBefore(child.native, next.native)
|
|
113
|
+
nitroLayout.NLInsertBefore(layoutNode(parent), layoutNode(child), layoutNode(next))
|
|
114
|
+
} else {
|
|
115
|
+
parent.native.appendChild(child.native)
|
|
116
|
+
nitroLayout.NLAppendChild(layoutNode(parent), layoutNode(child))
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
parent.children.splice(index, 0, child)
|
|
120
|
+
child.parent = parent
|
|
121
|
+
syncLabel(parent)
|
|
122
|
+
requestLayout()
|
|
123
|
+
},
|
|
124
|
+
remove(node) {
|
|
125
|
+
detach(node)
|
|
126
|
+
dispose(node)
|
|
127
|
+
requestLayout()
|
|
128
|
+
},
|
|
129
|
+
setText(node, text) {
|
|
130
|
+
if (isElement(node)) throw new Error('Expected a Vue text node.')
|
|
131
|
+
node.value = text
|
|
132
|
+
if (node.parent) syncLabel(node.parent)
|
|
133
|
+
requestLayout()
|
|
134
|
+
},
|
|
135
|
+
setElementText(node, text) {
|
|
136
|
+
for (const child of [...node.children]) {
|
|
137
|
+
detach(child)
|
|
138
|
+
dispose(child)
|
|
139
|
+
}
|
|
140
|
+
if (node.kind === 'label') {
|
|
141
|
+
node.children.push({ kind: 'text', value: text, parent: node })
|
|
142
|
+
syncLabel(node)
|
|
143
|
+
} else if (text.trim()) {
|
|
144
|
+
throw new Error('Wrap text in a Text component.')
|
|
145
|
+
}
|
|
146
|
+
requestLayout()
|
|
147
|
+
},
|
|
148
|
+
patchProp(node, key, previous, next) {
|
|
149
|
+
if (key === 'layout') updateLayout(layoutNode(node), next ?? {}, previous ?? {})
|
|
150
|
+
else if (key === 'fontSize' && node.kind === 'label') node.native.fontSize = next ?? 17
|
|
151
|
+
else if (key === 'modelValue' && node.kind === 'input') node.native.setText(next ?? '')
|
|
152
|
+
else if (key === 'placeholder' && node.kind === 'input') node.native.placeholder = next ?? undefined
|
|
153
|
+
else if (key === 'onEdit' && node.kind === 'input') {
|
|
154
|
+
node.onEdit = next ?? undefined
|
|
155
|
+
return
|
|
156
|
+
} else return
|
|
157
|
+
node.native.afterUpdate()
|
|
158
|
+
requestLayout()
|
|
159
|
+
},
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
const app = renderer.createApp(component)
|
|
163
|
+
const size = controller.addOnSizeChangedListener(requestLayout)
|
|
164
|
+
app.onUnmount(() => {
|
|
165
|
+
active = false
|
|
166
|
+
size.remove()
|
|
167
|
+
})
|
|
168
|
+
app.mount(root)
|
|
169
|
+
return app
|
|
170
|
+
}
|