@nativue/core 0.2.0 → 0.4.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/android/consumer-rules.pro +3 -0
- package/android/nativue.settings.gradle.kts +56 -0
- package/android/src/main/java/dev/nativue/Bridge.kt +125 -29
- package/android/src/main/java/dev/nativue/ModalHost.kt +10 -2
- package/android/src/main/java/dev/nativue/NativueActivity.kt +8 -0
- package/android/src/main/java/dev/nativue/Navigation.kt +37 -26
- package/android/src/main/java/dev/nativue/Plugins.kt +75 -0
- package/android/src/main/java/dev/nativue/Style.kt +5 -1
- package/android/src/main/java/dev/nativue/Theme.kt +61 -0
- package/android/src/main/java/dev/nativue/Views.kt +53 -2
- package/cli/config.d.mts +19 -0
- package/cli/config.mjs +4 -0
- package/cli/link.mjs +129 -0
- package/cli/nativue.mjs +67 -5
- package/ios/Bridge.swift +111 -15
- package/ios/KeyboardAvoidingView.swift +12 -5
- package/ios/ModalController.swift +27 -6
- package/ios/NativeNode.swift +1 -0
- package/ios/NativueView.swift +10 -3
- package/ios/Plugins.swift +46 -0
- package/ios/PressableView.swift +1 -1
- package/ios/Style.swift +14 -1
- package/ios/VueScrollView.swift +49 -0
- package/package.json +7 -2
- package/src/appearance.ts +49 -0
- package/src/components.ts +47 -9
- package/src/entry.ts +2 -1
- package/src/fetch.ts +4 -1
- package/src/globals.ts +3 -0
- package/src/native-components.ts +7 -0
- package/src/native-functions.ts +41 -0
- package/src/renderer.ts +102 -2
- package/src/router.ts +37 -7
- package/src/types.ts +2 -0
- package/src/virtual-modules.d.ts +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nativue/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Vue rendering real native views on iOS and Android",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vue",
|
|
@@ -25,7 +25,11 @@
|
|
|
25
25
|
"exports": {
|
|
26
26
|
".": "./src/renderer.ts",
|
|
27
27
|
"./router": "./src/router.ts",
|
|
28
|
-
"./tsconfig.base.json": "./tsconfig.base.json"
|
|
28
|
+
"./tsconfig.base.json": "./tsconfig.base.json",
|
|
29
|
+
"./config": {
|
|
30
|
+
"types": "./cli/config.d.mts",
|
|
31
|
+
"default": "./cli/config.mjs"
|
|
32
|
+
}
|
|
29
33
|
},
|
|
30
34
|
"scripts": {
|
|
31
35
|
"test": "(cd ../../examples/demo && nativue build) && node test/test.mjs",
|
|
@@ -50,6 +54,7 @@
|
|
|
50
54
|
"tsconfig.base.json",
|
|
51
55
|
"android/build.gradle.kts",
|
|
52
56
|
"android/consumer-rules.pro",
|
|
57
|
+
"android/nativue.settings.gradle.kts",
|
|
53
58
|
"android/src"
|
|
54
59
|
],
|
|
55
60
|
"publishConfig": {
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// The platform the app runs on, and the system's light or dark appearance.
|
|
2
|
+
import { readonly, shallowRef, type Ref } from '@vue/runtime-core'
|
|
3
|
+
|
|
4
|
+
export type ColorScheme = 'light' | 'dark'
|
|
5
|
+
export type PlatformOS = 'ios' | 'android'
|
|
6
|
+
type ColorSchemeListener = (colorScheme: ColorScheme) => void
|
|
7
|
+
|
|
8
|
+
const colorSchemeListeners = new Set<ColorSchemeListener>()
|
|
9
|
+
let colorScheme: ColorScheme = __native.colorScheme()
|
|
10
|
+
const colorSchemeRef = shallowRef(colorScheme)
|
|
11
|
+
|
|
12
|
+
// Platform.OS === 'ios'
|
|
13
|
+
// Platform.select({ ios: 17, android: 16 })
|
|
14
|
+
export const Platform = {
|
|
15
|
+
OS: __native.platform(),
|
|
16
|
+
|
|
17
|
+
select<Value>(values: { ios?: Value; android?: Value; default?: Value }): Value | undefined {
|
|
18
|
+
return values[Platform.OS] ?? values.default
|
|
19
|
+
},
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Appearance.getColorScheme() -> 'light' | 'dark'
|
|
23
|
+
// const stop = Appearance.addChangeListener((colorScheme) => …)
|
|
24
|
+
export const Appearance = {
|
|
25
|
+
getColorScheme(): ColorScheme {
|
|
26
|
+
return colorScheme
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
/** Returns a function that removes the listener. */
|
|
30
|
+
addChangeListener(listener: ColorSchemeListener): () => void {
|
|
31
|
+
colorSchemeListeners.add(listener)
|
|
32
|
+
|
|
33
|
+
return () => colorSchemeListeners.delete(listener)
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// const colorScheme = useColorScheme() 'light' | 'dark', follows the system
|
|
38
|
+
export function useColorScheme(): Readonly<Ref<ColorScheme>> {
|
|
39
|
+
return readonly(colorSchemeRef)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Native calls it when the system switches between light and dark.
|
|
43
|
+
globalThis.__colorSchemeChanged = (nextColorScheme) => {
|
|
44
|
+
if (nextColorScheme === colorScheme) return
|
|
45
|
+
|
|
46
|
+
colorScheme = nextColorScheme
|
|
47
|
+
colorSchemeRef.value = nextColorScheme
|
|
48
|
+
colorSchemeListeners.forEach((listener) => listener(nextColorScheme))
|
|
49
|
+
}
|
package/src/components.ts
CHANGED
|
@@ -3,7 +3,9 @@ import {
|
|
|
3
3
|
computed,
|
|
4
4
|
defineComponent,
|
|
5
5
|
h,
|
|
6
|
+
onBeforeUnmount,
|
|
6
7
|
ref,
|
|
8
|
+
watch,
|
|
7
9
|
type PropType,
|
|
8
10
|
type SlotsType,
|
|
9
11
|
type VNode,
|
|
@@ -12,24 +14,56 @@ import type { ScrollEvent, Style } from './types'
|
|
|
12
14
|
|
|
13
15
|
type Listener = ((payload: unknown) => void) | undefined
|
|
14
16
|
|
|
17
|
+
const MINIMUM_PRESSED_TIME = 130 // ms
|
|
18
|
+
|
|
15
19
|
// Vue's template compiler normalizes `:style`, so it can't be a function of the pressed state: the pressed
|
|
16
|
-
// look goes in `pressed-style`, merged over `style` while pressed. The slot also receives { pressed }
|
|
20
|
+
// look goes in `pressed-style`, merged over `style` while pressed. The slot also receives { pressed },
|
|
21
|
+
// and the class its pressed styles (Tailwind's active:). A disabled one ignores touches.
|
|
17
22
|
export const Pressable = defineComponent({
|
|
18
23
|
name: 'Pressable',
|
|
19
24
|
inheritAttrs: false,
|
|
20
|
-
props: {
|
|
25
|
+
props: {
|
|
26
|
+
style: Object as PropType<Style>,
|
|
27
|
+
pressedStyle: Object as PropType<Style>,
|
|
28
|
+
disabled: Boolean,
|
|
29
|
+
},
|
|
21
30
|
slots: Object as SlotsType<{ default: { pressed: boolean } }>,
|
|
22
31
|
|
|
23
32
|
setup(props, { attrs, slots }) {
|
|
24
33
|
const pressed = ref(false)
|
|
34
|
+
let pressedAt = 0
|
|
35
|
+
let releaseTimeout: ReturnType<typeof setTimeout> | undefined
|
|
25
36
|
|
|
26
|
-
function
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
listener?.(payload)
|
|
30
|
-
}
|
|
37
|
+
function release() {
|
|
38
|
+
clearTimeout(releaseTimeout)
|
|
39
|
+
pressed.value = false
|
|
31
40
|
}
|
|
32
41
|
|
|
42
|
+
// Disabled mid-press, the native view may never send the pressout.
|
|
43
|
+
watch(
|
|
44
|
+
() => props.disabled,
|
|
45
|
+
(disabled) => {
|
|
46
|
+
if (disabled) release()
|
|
47
|
+
},
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
function handlePressIn(payload: unknown) {
|
|
51
|
+
clearTimeout(releaseTimeout)
|
|
52
|
+
pressedAt = Date.now()
|
|
53
|
+
pressed.value = true
|
|
54
|
+
;(attrs.onPressIn as Listener)?.(payload)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// A quick tap would show the pressed style for less than a frame: keep it for a moment.
|
|
58
|
+
function handlePressOut(payload: unknown) {
|
|
59
|
+
const remainingTime = MINIMUM_PRESSED_TIME - (Date.now() - pressedAt)
|
|
60
|
+
|
|
61
|
+
releaseTimeout = setTimeout(release, Math.max(0, remainingTime))
|
|
62
|
+
;(attrs.onPressOut as Listener)?.(payload)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
onBeforeUnmount(() => clearTimeout(releaseTimeout))
|
|
66
|
+
|
|
33
67
|
return () => {
|
|
34
68
|
const style = pressed.value ? { ...props.style, ...props.pressedStyle } : props.style
|
|
35
69
|
|
|
@@ -38,8 +72,10 @@ export const Pressable = defineComponent({
|
|
|
38
72
|
{
|
|
39
73
|
...attrs,
|
|
40
74
|
style,
|
|
41
|
-
|
|
42
|
-
|
|
75
|
+
pressed: pressed.value,
|
|
76
|
+
disabled: props.disabled || undefined, // unset, nothing to send
|
|
77
|
+
onPressIn: handlePressIn,
|
|
78
|
+
onPressOut: handlePressOut,
|
|
43
79
|
},
|
|
44
80
|
slots.default?.({ pressed: pressed.value }),
|
|
45
81
|
)
|
|
@@ -55,6 +91,8 @@ const OFFSCREEN_ROWS_RENDERED = 10
|
|
|
55
91
|
// variable heights are ever needed. Vertical only. The window ignores the header's height, which the
|
|
56
92
|
// offscreen rows absorb while the header is shorter than them; measure it if tall headers show gaps.
|
|
57
93
|
// ponytail: items are `any` in the #item slot; make FlatList generic if typed rows are wanted.
|
|
94
|
+
// ponytail: an Animated.event `:on-scroll` replaces the window's own scroll handler, and a mapped event
|
|
95
|
+
// reaches JS only when a scroll starts and ends; report frames to both natively if that's needed.
|
|
58
96
|
export const FlatList = defineComponent({
|
|
59
97
|
name: 'FlatList',
|
|
60
98
|
props: {
|
package/src/entry.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// The app's entry, added by the CLI: every .vue file under the project's src/app/ is a route, so
|
|
2
|
-
// projects have no main.js.
|
|
2
|
+
// projects have no main.js. The runtime modules of nativue.config's plugins run first.
|
|
3
|
+
import 'virtual:nativue/plugin-runtimes'
|
|
3
4
|
import { createRouterApp } from './router'
|
|
4
5
|
|
|
5
6
|
createRouterApp(import.meta.glob('/src/app/**/*.vue', { eager: true })).mount()
|
package/src/fetch.ts
CHANGED
|
@@ -96,7 +96,10 @@ function fetch(
|
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
globalThis.__fetchFinished = (requestId, response, errorMessage) => {
|
|
99
|
-
const
|
|
99
|
+
const request = pendingRequestsById.get(requestId)
|
|
100
|
+
if (!request) return
|
|
101
|
+
|
|
102
|
+
const { resolve, reject, url } = request
|
|
100
103
|
pendingRequestsById.delete(requestId)
|
|
101
104
|
|
|
102
105
|
if (errorMessage || !response) reject(new TypeError(`Network request failed: ${errorMessage}`))
|
package/src/globals.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// What the JavaScript engine has globally: the bridge, the hooks native calls into, and the web APIs
|
|
2
2
|
// prelude.ts (timers, console) and fetch.ts provide. No DOM.
|
|
3
|
+
import type { ColorScheme } from './appearance'
|
|
3
4
|
import type { NativeModule } from './types'
|
|
4
5
|
|
|
5
6
|
declare global {
|
|
@@ -7,6 +8,8 @@ declare global {
|
|
|
7
8
|
var __dispatch: (nodeId: number, eventName: string, payload: unknown) => void
|
|
8
9
|
var __openURL: (url: string) => void
|
|
9
10
|
var __fireTimer: (timerId: number) => void
|
|
11
|
+
var __colorSchemeChanged: (colorScheme: ColorScheme) => void
|
|
12
|
+
var __asyncFunctionFinished: (callId: number, value: unknown, errorMessage: string | null) => void
|
|
10
13
|
var __animationFinished: (animationId: number, finished: boolean) => void
|
|
11
14
|
var __fetchFinished: (
|
|
12
15
|
requestId: number,
|
package/src/native-components.ts
CHANGED
|
@@ -36,6 +36,8 @@ declare module '@vue/runtime-core' {
|
|
|
36
36
|
keyboardDismissMode?: 'none' | 'on-drag' | 'interactive'
|
|
37
37
|
keyboardShouldPersistTaps?: 'never' | 'always' | 'handled'
|
|
38
38
|
contentInsetAdjustmentBehavior?: 'automatic' | 'scrollableAxes' | 'never' | 'always'
|
|
39
|
+
/** The keyboard insets the content, and the focused input scrolls above it. */
|
|
40
|
+
automaticallyAdjustKeyboardInsets?: boolean
|
|
39
41
|
onScroll?: EventHandler<ScrollEvent>
|
|
40
42
|
onRefresh?: () => void
|
|
41
43
|
}>
|
|
@@ -46,7 +48,12 @@ declare module '@vue/runtime-core' {
|
|
|
46
48
|
keyboardType?: 'default' | 'numeric' | 'decimal-pad' | 'email-address' | 'phone-pad' | 'url'
|
|
47
49
|
secureTextEntry?: boolean
|
|
48
50
|
onChangeText?: (text: string) => void
|
|
51
|
+
disabled?: boolean
|
|
49
52
|
onSubmitEditing?: (text: string) => void
|
|
53
|
+
onFocus?: () => void
|
|
54
|
+
onBlur?: () => void
|
|
55
|
+
/** Merged over `style` while the input has focus. */
|
|
56
|
+
focusedStyle?: Style
|
|
50
57
|
}
|
|
51
58
|
>
|
|
52
59
|
Image: NativeComponent<{
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// The functions packages add natively, for their JS side to call:
|
|
2
|
+
// const impact = requireNativeFunction<(style: string) => void>('haptics.impact')
|
|
3
|
+
// const takePhoto = requireNativeFunction<() => Promise<string>>('camera.takePhoto')
|
|
4
|
+
// Native addFunction ones return synchronously, addAsyncFunction ones return a Promise.
|
|
5
|
+
type AnyFunction = (...args: never[]) => unknown
|
|
6
|
+
type NativeFunctions = Record<string, ((...args: unknown[]) => unknown) | undefined>
|
|
7
|
+
type PendingCall = { resolve: (value: unknown) => void; reject: (error: Error) => void }
|
|
8
|
+
|
|
9
|
+
const pendingCalls = new Map<number, PendingCall>()
|
|
10
|
+
let nextCallId = 1
|
|
11
|
+
|
|
12
|
+
globalThis.__asyncFunctionFinished = (callId, value, errorMessage) => {
|
|
13
|
+
const call = pendingCalls.get(callId)
|
|
14
|
+
if (!call) return
|
|
15
|
+
|
|
16
|
+
pendingCalls.delete(callId)
|
|
17
|
+
if (errorMessage === null) call.resolve(value)
|
|
18
|
+
else call.reject(new Error(errorMessage))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function asyncCaller(nativeFunction: (...args: unknown[]) => unknown) {
|
|
22
|
+
return (...args: unknown[]) =>
|
|
23
|
+
new Promise((resolve, reject) => {
|
|
24
|
+
const callId = nextCallId++
|
|
25
|
+
pendingCalls.set(callId, { resolve, reject })
|
|
26
|
+
nativeFunction(callId, ...args)
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function requireNativeFunction<Function extends AnyFunction>(name: string): Function {
|
|
31
|
+
const nativeFunctions = __native as unknown as NativeFunctions
|
|
32
|
+
const syncFunction = nativeFunctions[`plugin:${name}`]
|
|
33
|
+
const asyncFunction = nativeFunctions[`pluginAsync:${name}`]
|
|
34
|
+
|
|
35
|
+
if (syncFunction) return syncFunction as unknown as Function
|
|
36
|
+
if (asyncFunction) return asyncCaller(asyncFunction) as unknown as Function
|
|
37
|
+
|
|
38
|
+
throw new Error(
|
|
39
|
+
`The native function "${name}" isn't in the app. A package's native code is added when the app is built: rebuild it (npx nativue run ios / android).`,
|
|
40
|
+
)
|
|
41
|
+
}
|
package/src/renderer.ts
CHANGED
|
@@ -14,11 +14,20 @@ import {
|
|
|
14
14
|
type Directive,
|
|
15
15
|
} from '@vue/runtime-core'
|
|
16
16
|
import { animatedEventMapping, animatedStyleBinding, isAnimatedEvent } from './animated'
|
|
17
|
+
import { Appearance } from './appearance'
|
|
17
18
|
import { COLOR_PROPERTIES, processColor } from './colors'
|
|
18
19
|
import { FlatList, Pressable, Swipeable } from './components'
|
|
19
20
|
import type { LayoutAnimationConfig, Style } from './types'
|
|
20
21
|
export * from '@vue/runtime-core'
|
|
21
22
|
export { Animated } from './animated'
|
|
23
|
+
export {
|
|
24
|
+
Appearance,
|
|
25
|
+
Platform,
|
|
26
|
+
useColorScheme,
|
|
27
|
+
type ColorScheme,
|
|
28
|
+
type PlatformOS,
|
|
29
|
+
} from './appearance'
|
|
30
|
+
export { requireNativeFunction } from './native-functions'
|
|
22
31
|
export { AsyncStorage } from './storage'
|
|
23
32
|
export type * from './types'
|
|
24
33
|
export type { SvgElement, SvgIcon } from './native-components'
|
|
@@ -29,14 +38,37 @@ interface Node {
|
|
|
29
38
|
parent: Node | null
|
|
30
39
|
children: Node[]
|
|
31
40
|
text: string
|
|
41
|
+
style?: Style | null
|
|
42
|
+
/** <TextInput focused-style>: merged over `style` while the input has focus. */
|
|
43
|
+
focusedStyle?: Style | null
|
|
44
|
+
className?: string
|
|
45
|
+
/** The states its class's styles may depend on (Tailwind's active:, focus:, disabled:). */
|
|
46
|
+
pressed?: boolean
|
|
47
|
+
focused?: boolean
|
|
48
|
+
disabled?: boolean
|
|
49
|
+
classStyle?: Style
|
|
32
50
|
}
|
|
33
51
|
|
|
52
|
+
type ElementState = Pick<Node, 'pressed' | 'focused' | 'disabled'>
|
|
53
|
+
|
|
34
54
|
type EventHandler = (payload: unknown) => void
|
|
55
|
+
type ClassStyleResolver = (className: string, state: Required<ElementState>) => Style
|
|
35
56
|
|
|
36
57
|
const native = globalThis.__native
|
|
37
58
|
let nextNodeId = 1
|
|
38
|
-
// ponytail: handlers of removed nodes are never cleaned up; clean them on remove when it matters.
|
|
39
59
|
const eventHandlersByNodeId: Record<number, Record<string, EventHandler | undefined>> = {}
|
|
60
|
+
let resolveClassStyle: ClassStyleResolver | null = null
|
|
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>()
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Gives `class` its styles, for a nativue.config plugin (@nativue/tailwind). Class styles may depend
|
|
67
|
+
* on the color scheme: they're resolved again when it changes.
|
|
68
|
+
*/
|
|
69
|
+
export function setClassStyleResolver(resolver: ClassStyleResolver): void {
|
|
70
|
+
resolveClassStyle = resolver
|
|
71
|
+
}
|
|
40
72
|
|
|
41
73
|
function createNode(type: string): Node {
|
|
42
74
|
return { id: nextNodeId++, type, parent: null, children: [], text: '' }
|
|
@@ -47,6 +79,19 @@ function isNativeNode(node: Node): boolean {
|
|
|
47
79
|
return !node.type.startsWith('#')
|
|
48
80
|
}
|
|
49
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
|
+
|
|
50
95
|
function isEventProp(key: string): boolean {
|
|
51
96
|
return /^on[A-Z]/.test(key)
|
|
52
97
|
}
|
|
@@ -115,6 +160,42 @@ function withoutAnimatedProperties(
|
|
|
115
160
|
return staticStyle
|
|
116
161
|
}
|
|
117
162
|
|
|
163
|
+
// The class's styles come first, so the style prop overrides them, as inline styles do on the web.
|
|
164
|
+
function combinedStyle(element: Node): Style | null {
|
|
165
|
+
const focusedStyle = element.focused ? element.focusedStyle : null
|
|
166
|
+
if (!element.classStyle && !focusedStyle) return element.style ?? null
|
|
167
|
+
|
|
168
|
+
return { ...element.classStyle, ...element.style, ...focusedStyle }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function classStyleOf(element: Node): Style | undefined {
|
|
172
|
+
if (!element.className || !resolveClassStyle) return undefined
|
|
173
|
+
|
|
174
|
+
return resolveClassStyle(element.className, {
|
|
175
|
+
pressed: element.pressed ?? false,
|
|
176
|
+
focused: element.focused ?? false,
|
|
177
|
+
disabled: element.disabled ?? false,
|
|
178
|
+
})
|
|
179
|
+
}
|
|
180
|
+
|
|
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
|
+
) {
|
|
186
|
+
const previousStyle = combinedStyle(element)
|
|
187
|
+
|
|
188
|
+
Object.assign(element, change)
|
|
189
|
+
element.classStyle = classStyleOf(element)
|
|
190
|
+
|
|
191
|
+
if (element.className) nodesWithClass.add(element)
|
|
192
|
+
else nodesWithClass.delete(element)
|
|
193
|
+
|
|
194
|
+
patchStyle(element, previousStyle, combinedStyle(element))
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
Appearance.addChangeListener(() => nodesWithClass.forEach((node) => updateStyle(node, {})))
|
|
198
|
+
|
|
118
199
|
function patchStyle(element: Node, previousStyle: Style | null, nextStyle: Style | null) {
|
|
119
200
|
const previousBinding = animatedStyleBinding(previousStyle)
|
|
120
201
|
const nextBinding = animatedStyleBinding(nextStyle)
|
|
@@ -177,6 +258,7 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
|
177
258
|
createElement(type) {
|
|
178
259
|
const element = createNode(type)
|
|
179
260
|
native.create(element.id, type)
|
|
261
|
+
if (type === 'TextInput') textInputsById.set(element.id, element)
|
|
180
262
|
|
|
181
263
|
return element
|
|
182
264
|
},
|
|
@@ -215,6 +297,7 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
|
215
297
|
|
|
216
298
|
remove(child) {
|
|
217
299
|
const parent = detachFromParent(child)
|
|
300
|
+
forgetSubtree(child)
|
|
218
301
|
|
|
219
302
|
if (isNativeNode(child)) native.remove(child.id)
|
|
220
303
|
else syncTextContent(parent)
|
|
@@ -231,7 +314,14 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
|
231
314
|
},
|
|
232
315
|
|
|
233
316
|
patchProp(element, key, previousValue, nextValue, _namespace, component) {
|
|
234
|
-
if (key === 'style')
|
|
317
|
+
if (key === 'style') updateStyle(element, { style: nextValue })
|
|
318
|
+
else if (key === 'class') updateStyle(element, { className: nextValue ?? undefined })
|
|
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
|
+
}
|
|
235
325
|
// :on-pan="..." (v-bind, used for Animated.event) arrives kebab-case.
|
|
236
326
|
else if (isEventProp(camelize(key))) {
|
|
237
327
|
patchEventHandler(element, camelize(key), previousValue, nextValue, component ?? null)
|
|
@@ -247,6 +337,8 @@ const MODEL_EVENT_BY_NATIVE_EVENT: Record<string, string> = {
|
|
|
247
337
|
}
|
|
248
338
|
|
|
249
339
|
globalThis.__dispatch = (nodeId, eventName, payload) => {
|
|
340
|
+
updateFocus(nodeId, eventName)
|
|
341
|
+
|
|
250
342
|
const handlers = eventHandlersByNodeId[nodeId]
|
|
251
343
|
if (!handlers) return
|
|
252
344
|
|
|
@@ -254,6 +346,14 @@ globalThis.__dispatch = (nodeId, eventName, payload) => {
|
|
|
254
346
|
handlers[MODEL_EVENT_BY_NATIVE_EVENT[eventName]]?.(payload)
|
|
255
347
|
}
|
|
256
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
|
+
|
|
257
357
|
const LAYOUT_ANIMATION_PRESETS = {
|
|
258
358
|
easeInEaseOut: { type: 'easeInEaseOut', duration: 300 },
|
|
259
359
|
linear: { type: 'linear', duration: 500 },
|
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
|
+
// ponytail: 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
|
package/src/types.ts
CHANGED
|
@@ -104,6 +104,8 @@ export interface NativeModule {
|
|
|
104
104
|
log(message: string): void
|
|
105
105
|
reportError(message: string, details: string): void
|
|
106
106
|
windowSize(): { width: number; height: number }
|
|
107
|
+
platform(): 'ios' | 'android'
|
|
108
|
+
colorScheme(): 'light' | 'dark'
|
|
107
109
|
create(id: number, type: string): void
|
|
108
110
|
insert(parentId: number, childId: number, index: number): void
|
|
109
111
|
remove(id: number): void
|