@nativue/core 0.2.0 → 0.3.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/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,31 @@ interface Node {
29
38
  parent: Node | null
30
39
  children: Node[]
31
40
  text: string
41
+ style?: Style | null
42
+ className?: string
43
+ /** A <Pressable>'s pressed state, for its class's pressed styles (Tailwind's active:). */
44
+ pressed?: boolean
45
+ classStyle?: Style
32
46
  }
33
47
 
34
48
  type EventHandler = (payload: unknown) => void
49
+ type ClassStyleResolver = (className: string, state: { pressed: boolean }) => Style
35
50
 
36
51
  const native = globalThis.__native
37
52
  let nextNodeId = 1
38
53
  // ponytail: handlers of removed nodes are never cleaned up; clean them on remove when it matters.
39
54
  const eventHandlersByNodeId: Record<number, Record<string, EventHandler | undefined>> = {}
55
+ let resolveClassStyle: ClassStyleResolver | null = null
56
+ // ponytail: a removed node leaves the set, its descendants stay; track them if apps churn many.
57
+ const nodesWithClass = new Set<Node>()
58
+
59
+ /**
60
+ * Gives `class` its styles, for a nativue.config plugin (@nativue/tailwind). Class styles may depend
61
+ * on the color scheme: they're resolved again when it changes.
62
+ */
63
+ export function setClassStyleResolver(resolver: ClassStyleResolver): void {
64
+ resolveClassStyle = resolver
65
+ }
40
66
 
41
67
  function createNode(type: string): Node {
42
68
  return { id: nextNodeId++, type, parent: null, children: [], text: '' }
@@ -115,6 +141,34 @@ function withoutAnimatedProperties(
115
141
  return staticStyle
116
142
  }
117
143
 
144
+ // The class's styles come first, so the style prop overrides them, as inline styles do on the web.
145
+ function combinedStyle(element: Node): Style | null {
146
+ if (!element.classStyle) return element.style ?? null
147
+
148
+ return { ...element.classStyle, ...element.style }
149
+ }
150
+
151
+ function classStyleOf(element: Node): Style | undefined {
152
+ if (!element.className || !resolveClassStyle) return undefined
153
+
154
+ return resolveClassStyle(element.className, { pressed: element.pressed ?? false })
155
+ }
156
+
157
+ /** Applies a change to what makes up the style (style, class, pressed state), sending only the diff. */
158
+ function updateStyle(element: Node, change: Pick<Node, 'style' | 'className' | 'pressed'>) {
159
+ const previousStyle = combinedStyle(element)
160
+
161
+ Object.assign(element, change)
162
+ element.classStyle = classStyleOf(element)
163
+
164
+ if (element.className) nodesWithClass.add(element)
165
+ else nodesWithClass.delete(element)
166
+
167
+ patchStyle(element, previousStyle, combinedStyle(element))
168
+ }
169
+
170
+ Appearance.addChangeListener(() => nodesWithClass.forEach((node) => updateStyle(node, {})))
171
+
118
172
  function patchStyle(element: Node, previousStyle: Style | null, nextStyle: Style | null) {
119
173
  const previousBinding = animatedStyleBinding(previousStyle)
120
174
  const nextBinding = animatedStyleBinding(nextStyle)
@@ -215,6 +269,7 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
215
269
 
216
270
  remove(child) {
217
271
  const parent = detachFromParent(child)
272
+ nodesWithClass.delete(child)
218
273
 
219
274
  if (isNativeNode(child)) native.remove(child.id)
220
275
  else syncTextContent(parent)
@@ -231,7 +286,9 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
231
286
  },
232
287
 
233
288
  patchProp(element, key, previousValue, nextValue, _namespace, component) {
234
- if (key === 'style') patchStyle(element, previousValue, nextValue)
289
+ if (key === 'style') updateStyle(element, { style: nextValue })
290
+ else if (key === 'class') updateStyle(element, { className: nextValue ?? undefined })
291
+ else if (key === 'pressed') updateStyle(element, { pressed: nextValue })
235
292
  // :on-pan="..." (v-bind, used for Animated.event) arrives kebab-case.
236
293
  else if (isEventProp(camelize(key))) {
237
294
  patchEventHandler(element, camelize(key), previousValue, nextValue, component ?? null)
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
@@ -0,0 +1,2 @@
1
+ // Generated by the CLI from nativue.config's plugins (cli/nativue.mjs).
2
+ declare module 'virtual:nativue/plugin-runtimes'