@nativue/core 0.3.0 → 0.4.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.
- package/README.md +16 -5
- package/android/build.gradle.kts +2 -2
- package/android/src/main/java/dev/nativue/Bridge.kt +76 -24
- package/android/src/main/java/dev/nativue/Gestures.kt +2 -0
- package/android/src/main/java/dev/nativue/ModalHost.kt +8 -0
- package/android/src/main/java/dev/nativue/Navigation.kt +5 -2
- package/android/src/main/java/dev/nativue/Style.kt +1 -1
- package/android/src/main/java/dev/nativue/SvgView.kt +1 -1
- package/android/src/main/java/dev/nativue/Views.kt +40 -1
- package/cli/nativue.mjs +21 -5
- package/cli/run.mjs +1 -1
- package/ios/Bridge.swift +44 -20
- package/ios/ImageView.swift +1 -1
- package/ios/KeyValueStorage.swift +1 -1
- package/ios/KeyboardAvoidingView.swift +12 -5
- package/ios/ModalController.swift +27 -6
- package/ios/Plugins.swift +2 -1
- package/ios/PressableView.swift +1 -1
- package/ios/Style.swift +3 -1
- package/ios/SvgView.swift +1 -1
- package/ios/TextInputView.swift +1 -1
- package/ios/VueScrollView.swift +50 -1
- package/package.json +1 -1
- package/src/components.ts +47 -11
- package/src/fetch.ts +6 -3
- package/src/native-components.ts +7 -0
- package/src/renderer.ts +54 -11
- package/src/router.ts +37 -7
package/src/renderer.ts
CHANGED
|
@@ -39,22 +39,28 @@ interface Node {
|
|
|
39
39
|
children: Node[]
|
|
40
40
|
text: string
|
|
41
41
|
style?: Style | null
|
|
42
|
+
/** <TextInput focused-style>: merged over `style` while the input has focus. */
|
|
43
|
+
focusedStyle?: Style | null
|
|
42
44
|
className?: string
|
|
43
|
-
/**
|
|
45
|
+
/** The states its class's styles may depend on (Tailwind's active:, focus:, disabled:). */
|
|
44
46
|
pressed?: boolean
|
|
47
|
+
focused?: boolean
|
|
48
|
+
disabled?: boolean
|
|
45
49
|
classStyle?: Style
|
|
46
50
|
}
|
|
47
51
|
|
|
52
|
+
type ElementState = Pick<Node, 'pressed' | 'focused' | 'disabled'>
|
|
53
|
+
|
|
48
54
|
type EventHandler = (payload: unknown) => void
|
|
49
|
-
type ClassStyleResolver = (className: string, state:
|
|
55
|
+
type ClassStyleResolver = (className: string, state: Required<ElementState>) => Style
|
|
50
56
|
|
|
51
57
|
const native = globalThis.__native
|
|
52
58
|
let nextNodeId = 1
|
|
53
|
-
// ponytail: handlers of removed nodes are never cleaned up; clean them on remove when it matters.
|
|
54
59
|
const eventHandlersByNodeId: Record<number, Record<string, EventHandler | undefined>> = {}
|
|
55
60
|
let resolveClassStyle: ClassStyleResolver | null = null
|
|
56
|
-
// ponytail: a removed node leaves the set, its descendants stay; track them if apps churn many.
|
|
57
61
|
const nodesWithClass = new Set<Node>()
|
|
62
|
+
// For their focus state (focus:), which arrives as events by id.
|
|
63
|
+
const textInputsById = new Map<number, Node>()
|
|
58
64
|
|
|
59
65
|
/**
|
|
60
66
|
* Gives `class` its styles, for a nativue.config plugin (@nativue/tailwind). Class styles may depend
|
|
@@ -73,6 +79,19 @@ function isNativeNode(node: Node): boolean {
|
|
|
73
79
|
return !node.type.startsWith('#')
|
|
74
80
|
}
|
|
75
81
|
|
|
82
|
+
// Vue removes only the root of an unmounted subtree: its descendants go with it.
|
|
83
|
+
function forgetSubtree(node: Node) {
|
|
84
|
+
nodesWithClass.delete(node)
|
|
85
|
+
textInputsById.delete(node.id)
|
|
86
|
+
delete eventHandlersByNodeId[node.id]
|
|
87
|
+
node.children.forEach(forgetSubtree)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// As the native side reads boolean props: a bare attribute (`disabled`) is '', true.
|
|
91
|
+
function isTruthyAttribute(value: unknown): boolean {
|
|
92
|
+
return typeof value === 'string' ? value !== 'false' : value === true
|
|
93
|
+
}
|
|
94
|
+
|
|
76
95
|
function isEventProp(key: string): boolean {
|
|
77
96
|
return /^on[A-Z]/.test(key)
|
|
78
97
|
}
|
|
@@ -143,19 +162,27 @@ function withoutAnimatedProperties(
|
|
|
143
162
|
|
|
144
163
|
// The class's styles come first, so the style prop overrides them, as inline styles do on the web.
|
|
145
164
|
function combinedStyle(element: Node): Style | null {
|
|
146
|
-
|
|
165
|
+
const focusedStyle = element.focused ? element.focusedStyle : null
|
|
166
|
+
if (!element.classStyle && !focusedStyle) return element.style ?? null
|
|
147
167
|
|
|
148
|
-
return { ...element.classStyle, ...element.style }
|
|
168
|
+
return { ...element.classStyle, ...element.style, ...focusedStyle }
|
|
149
169
|
}
|
|
150
170
|
|
|
151
171
|
function classStyleOf(element: Node): Style | undefined {
|
|
152
172
|
if (!element.className || !resolveClassStyle) return undefined
|
|
153
173
|
|
|
154
|
-
return resolveClassStyle(element.className, {
|
|
174
|
+
return resolveClassStyle(element.className, {
|
|
175
|
+
pressed: element.pressed ?? false,
|
|
176
|
+
focused: element.focused ?? false,
|
|
177
|
+
disabled: element.disabled ?? false,
|
|
178
|
+
})
|
|
155
179
|
}
|
|
156
180
|
|
|
157
|
-
/** Applies a change to what makes up the style (style, class,
|
|
158
|
-
function updateStyle(
|
|
181
|
+
/** Applies a change to what makes up the style (style, class, state), sending only the diff. */
|
|
182
|
+
function updateStyle(
|
|
183
|
+
element: Node,
|
|
184
|
+
change: Pick<Node, 'style' | 'focusedStyle' | 'className'> & ElementState,
|
|
185
|
+
) {
|
|
159
186
|
const previousStyle = combinedStyle(element)
|
|
160
187
|
|
|
161
188
|
Object.assign(element, change)
|
|
@@ -231,6 +258,7 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
|
231
258
|
createElement(type) {
|
|
232
259
|
const element = createNode(type)
|
|
233
260
|
native.create(element.id, type)
|
|
261
|
+
if (type === 'TextInput') textInputsById.set(element.id, element)
|
|
234
262
|
|
|
235
263
|
return element
|
|
236
264
|
},
|
|
@@ -269,7 +297,7 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
|
269
297
|
|
|
270
298
|
remove(child) {
|
|
271
299
|
const parent = detachFromParent(child)
|
|
272
|
-
|
|
300
|
+
forgetSubtree(child)
|
|
273
301
|
|
|
274
302
|
if (isNativeNode(child)) native.remove(child.id)
|
|
275
303
|
else syncTextContent(parent)
|
|
@@ -289,6 +317,11 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
|
289
317
|
if (key === 'style') updateStyle(element, { style: nextValue })
|
|
290
318
|
else if (key === 'class') updateStyle(element, { className: nextValue ?? undefined })
|
|
291
319
|
else if (key === 'pressed') updateStyle(element, { pressed: nextValue })
|
|
320
|
+
else if (camelize(key) === 'focusedStyle') updateStyle(element, { focusedStyle: nextValue })
|
|
321
|
+
else if (key === 'disabled') {
|
|
322
|
+
updateStyle(element, { disabled: isTruthyAttribute(nextValue) })
|
|
323
|
+
native.setProp(element.id, key, nextValue ?? null)
|
|
324
|
+
}
|
|
292
325
|
// :on-pan="..." (v-bind, used for Animated.event) arrives kebab-case.
|
|
293
326
|
else if (isEventProp(camelize(key))) {
|
|
294
327
|
patchEventHandler(element, camelize(key), previousValue, nextValue, component ?? null)
|
|
@@ -304,6 +337,8 @@ const MODEL_EVENT_BY_NATIVE_EVENT: Record<string, string> = {
|
|
|
304
337
|
}
|
|
305
338
|
|
|
306
339
|
globalThis.__dispatch = (nodeId, eventName, payload) => {
|
|
340
|
+
updateFocus(nodeId, eventName)
|
|
341
|
+
|
|
307
342
|
const handlers = eventHandlersByNodeId[nodeId]
|
|
308
343
|
if (!handlers) return
|
|
309
344
|
|
|
@@ -311,6 +346,14 @@ globalThis.__dispatch = (nodeId, eventName, payload) => {
|
|
|
311
346
|
handlers[MODEL_EVENT_BY_NATIVE_EVENT[eventName]]?.(payload)
|
|
312
347
|
}
|
|
313
348
|
|
|
349
|
+
// A <TextInput>'s focus, for its class's focus: styles and its focused-style.
|
|
350
|
+
function updateFocus(nodeId: number, eventName: string) {
|
|
351
|
+
const textInput = textInputsById.get(nodeId)
|
|
352
|
+
if (!textInput || (eventName !== 'focus' && eventName !== 'blur')) return
|
|
353
|
+
|
|
354
|
+
updateStyle(textInput, { focused: eventName === 'focus' })
|
|
355
|
+
}
|
|
356
|
+
|
|
314
357
|
const LAYOUT_ANIMATION_PRESETS = {
|
|
315
358
|
easeInEaseOut: { type: 'easeInEaseOut', duration: 300 },
|
|
316
359
|
linear: { type: 'linear', duration: 500 },
|
|
@@ -352,7 +395,7 @@ export const Keyboard = {
|
|
|
352
395
|
|
|
353
396
|
// Replaces Vue's DOM-only vModelText, which the template compiler emits for v-model on
|
|
354
397
|
// <TextInput> and <Switch>.
|
|
355
|
-
//
|
|
398
|
+
// Limitation: v-model modifiers (.trim, .number, .lazy) are not supported yet.
|
|
356
399
|
export const vModelText: Directive<Node> = {
|
|
357
400
|
created(element, { value }) {
|
|
358
401
|
native.setProp(element.id, 'value', value ?? '')
|
package/src/router.ts
CHANGED
|
@@ -250,19 +250,29 @@ function matchSegments(segments: Segment[], pathParts: string[]): Params | null
|
|
|
250
250
|
const part = pathParts[index]
|
|
251
251
|
if (part === undefined) return null
|
|
252
252
|
|
|
253
|
-
if (segment.type === 'param') params[segment.name] =
|
|
253
|
+
if (segment.type === 'param') params[segment.name] = decodeURIComponentSafely(part)
|
|
254
254
|
else if (segment.name !== part) return null
|
|
255
255
|
}
|
|
256
256
|
|
|
257
257
|
return segments.length === pathParts.length ? params : null
|
|
258
258
|
}
|
|
259
259
|
|
|
260
|
+
// Links come from other apps too: a malformed escape (%E0%A4) is kept as it is, not thrown.
|
|
261
|
+
function decodeURIComponentSafely(text: string): string {
|
|
262
|
+
try {
|
|
263
|
+
return decodeURIComponent(text.replaceAll('+', ' '))
|
|
264
|
+
} catch {
|
|
265
|
+
return text
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
260
269
|
function parseQuery(query = ''): Params {
|
|
261
270
|
const params: Params = {}
|
|
262
271
|
|
|
263
272
|
for (const pair of query.split('&').filter(Boolean)) {
|
|
264
|
-
const
|
|
265
|
-
|
|
273
|
+
const separatorIndex = pair.includes('=') ? pair.indexOf('=') : pair.length // ?token=abc== keeps its ==
|
|
274
|
+
const key = pair.slice(0, separatorIndex)
|
|
275
|
+
params[decodeURIComponentSafely(key)] = decodeURIComponentSafely(pair.slice(separatorIndex + 1))
|
|
266
276
|
}
|
|
267
277
|
|
|
268
278
|
return params
|
|
@@ -304,7 +314,10 @@ function childNodesOf(layout: RouteNode): RouteNode[] {
|
|
|
304
314
|
// Where a layout starts when opened without a destination: its index page.
|
|
305
315
|
function initialRouteOf(layout: LayoutNode): Route {
|
|
306
316
|
const descendants = pages.filter((page) => page.layouts.includes(layout))
|
|
307
|
-
const page =
|
|
317
|
+
const page =
|
|
318
|
+
descendants.find(
|
|
319
|
+
(candidate) => candidate.layouts.at(-1) === layout && candidate.name === 'index',
|
|
320
|
+
) ?? descendants[0]
|
|
308
321
|
|
|
309
322
|
return {
|
|
310
323
|
chain: [...page.layouts.slice(page.layouts.indexOf(layout) + 1), page],
|
|
@@ -467,13 +480,30 @@ export function useRouter() {
|
|
|
467
480
|
return router
|
|
468
481
|
}
|
|
469
482
|
|
|
470
|
-
|
|
483
|
+
function injectPageEntry(hookName: string): Entry {
|
|
484
|
+
const entry = inject(ENTRY_KEY, null)
|
|
485
|
+
if (!entry) throw new Error(`${hookName}() works in a page, not in a _layout.vue`)
|
|
486
|
+
|
|
487
|
+
return entry
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// Type the params you expect: useLocalSearchParams<{ id: string }>(). Reactive: a tab opened again
|
|
491
|
+
// with other params (router.navigate('/search?q=shoes')) updates it.
|
|
471
492
|
export function useLocalSearchParams<RouteParams extends Params = Params>(): RouteParams {
|
|
472
|
-
|
|
493
|
+
const entry = injectPageEntry('useLocalSearchParams')
|
|
494
|
+
|
|
495
|
+
// Reads through to the entry's current params, which navigation may replace.
|
|
496
|
+
return new Proxy({} as RouteParams, {
|
|
497
|
+
get: (_target, key) => Reflect.get(entry.params, key),
|
|
498
|
+
has: (_target, key) => Reflect.has(entry.params, key),
|
|
499
|
+
ownKeys: () => Reflect.ownKeys(entry.params),
|
|
500
|
+
getOwnPropertyDescriptor: (_target, key) => Reflect.getOwnPropertyDescriptor(entry.params, key),
|
|
501
|
+
})
|
|
473
502
|
}
|
|
474
503
|
|
|
504
|
+
// Limitation: The path when the page opened; make it a ref if pages need to follow in-place navigation.
|
|
475
505
|
export function usePathname(): string {
|
|
476
|
-
return
|
|
506
|
+
return injectPageEntry('usePathname').href.split('?')[0]
|
|
477
507
|
}
|
|
478
508
|
|
|
479
509
|
// Whether this screen is the one on screen: on top of its stack (or the selected tab), inside a
|