@hozo/engine 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.
Files changed (168) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +78 -0
  3. package/dist/activate.d.ts +14 -0
  4. package/dist/activate.d.ts.map +1 -0
  5. package/dist/activate.js +38 -0
  6. package/dist/activate.js.map +1 -0
  7. package/dist/ambient.d.ts +79 -0
  8. package/dist/ambient.d.ts.map +1 -0
  9. package/dist/ambient.js +117 -0
  10. package/dist/ambient.js.map +1 -0
  11. package/dist/backdrop.d.ts +36 -0
  12. package/dist/backdrop.d.ts.map +1 -0
  13. package/dist/backdrop.js +41 -0
  14. package/dist/backdrop.js.map +1 -0
  15. package/dist/class-name.native.d.ts +20 -0
  16. package/dist/class-name.native.d.ts.map +1 -0
  17. package/dist/class-name.native.js +34 -0
  18. package/dist/class-name.native.js.map +1 -0
  19. package/dist/class-resolver.d.ts +26 -0
  20. package/dist/class-resolver.d.ts.map +1 -0
  21. package/dist/class-resolver.js +56 -0
  22. package/dist/class-resolver.js.map +1 -0
  23. package/dist/color-transition.d.ts +3 -0
  24. package/dist/color-transition.d.ts.map +1 -0
  25. package/dist/color-transition.js +31 -0
  26. package/dist/color-transition.js.map +1 -0
  27. package/dist/dom-style.d.ts +10 -0
  28. package/dist/dom-style.d.ts.map +1 -0
  29. package/dist/dom-style.js +145 -0
  30. package/dist/dom-style.js.map +1 -0
  31. package/dist/dom-style.native.d.ts +7 -0
  32. package/dist/dom-style.native.d.ts.map +1 -0
  33. package/dist/dom-style.native.js +9 -0
  34. package/dist/dom-style.native.js.map +1 -0
  35. package/dist/external-link.d.ts +6 -0
  36. package/dist/external-link.d.ts.map +1 -0
  37. package/dist/external-link.js +9 -0
  38. package/dist/external-link.js.map +1 -0
  39. package/dist/generated/animation.d.ts +2 -0
  40. package/dist/generated/animation.d.ts.map +1 -0
  41. package/dist/generated/animation.js +3 -0
  42. package/dist/generated/animation.js.map +1 -0
  43. package/dist/generated/class-resolver.d.ts +2 -0
  44. package/dist/generated/class-resolver.d.ts.map +1 -0
  45. package/dist/generated/class-resolver.js +3 -0
  46. package/dist/generated/class-resolver.js.map +1 -0
  47. package/dist/generated/dom-style.d.ts +2 -0
  48. package/dist/generated/dom-style.d.ts.map +1 -0
  49. package/dist/generated/dom-style.js +2 -0
  50. package/dist/generated/dom-style.js.map +1 -0
  51. package/dist/generated/dom-style.native.d.ts +2 -0
  52. package/dist/generated/dom-style.native.d.ts.map +1 -0
  53. package/dist/generated/dom-style.native.js +2 -0
  54. package/dist/generated/dom-style.native.js.map +1 -0
  55. package/dist/generated/environment.d.ts +2 -0
  56. package/dist/generated/environment.d.ts.map +1 -0
  57. package/dist/generated/environment.js +5 -0
  58. package/dist/generated/environment.js.map +1 -0
  59. package/dist/generated/image-source.d.ts +2 -0
  60. package/dist/generated/image-source.d.ts.map +1 -0
  61. package/dist/generated/image-source.js +3 -0
  62. package/dist/generated/image-source.js.map +1 -0
  63. package/dist/generated/interactive.d.ts +2 -0
  64. package/dist/generated/interactive.d.ts.map +1 -0
  65. package/dist/generated/interactive.js +2 -0
  66. package/dist/generated/interactive.js.map +1 -0
  67. package/dist/generated/safe-area.d.ts +2 -0
  68. package/dist/generated/safe-area.d.ts.map +1 -0
  69. package/dist/generated/safe-area.js +4 -0
  70. package/dist/generated/safe-area.js.map +1 -0
  71. package/dist/generated/scrollable.d.ts +2 -0
  72. package/dist/generated/scrollable.d.ts.map +1 -0
  73. package/dist/generated/scrollable.js +2 -0
  74. package/dist/generated/scrollable.js.map +1 -0
  75. package/dist/hooks.native.d.ts +92 -0
  76. package/dist/hooks.native.d.ts.map +1 -0
  77. package/dist/hooks.native.js +284 -0
  78. package/dist/hooks.native.js.map +1 -0
  79. package/dist/image-source.native.d.ts +5 -0
  80. package/dist/image-source.native.d.ts.map +1 -0
  81. package/dist/image-source.native.js +5 -0
  82. package/dist/image-source.native.js.map +1 -0
  83. package/dist/index.d.ts +11 -0
  84. package/dist/index.d.ts.map +1 -0
  85. package/dist/index.js +30 -0
  86. package/dist/index.js.map +1 -0
  87. package/dist/index.native.d.ts +8 -0
  88. package/dist/index.native.d.ts.map +1 -0
  89. package/dist/index.native.js +28 -0
  90. package/dist/index.native.js.map +1 -0
  91. package/dist/interactive.d.ts +31 -0
  92. package/dist/interactive.d.ts.map +1 -0
  93. package/dist/interactive.js +64 -0
  94. package/dist/interactive.js.map +1 -0
  95. package/dist/navigation-context.d.ts +11 -0
  96. package/dist/navigation-context.d.ts.map +1 -0
  97. package/dist/navigation-context.js +12 -0
  98. package/dist/navigation-context.js.map +1 -0
  99. package/dist/navigation-entry.d.ts +3 -0
  100. package/dist/navigation-entry.d.ts.map +1 -0
  101. package/dist/navigation-entry.js +3 -0
  102. package/dist/navigation-entry.js.map +1 -0
  103. package/dist/navigation-entry.native.d.ts +2 -0
  104. package/dist/navigation-entry.native.d.ts.map +1 -0
  105. package/dist/navigation-entry.native.js +6 -0
  106. package/dist/navigation-entry.native.js.map +1 -0
  107. package/dist/navigation.d.ts +34 -0
  108. package/dist/navigation.d.ts.map +1 -0
  109. package/dist/navigation.js +27 -0
  110. package/dist/navigation.js.map +1 -0
  111. package/dist/project.d.ts +10 -0
  112. package/dist/project.d.ts.map +1 -0
  113. package/dist/project.js +10 -0
  114. package/dist/project.js.map +1 -0
  115. package/dist/project.native.d.ts +2 -0
  116. package/dist/project.native.d.ts.map +1 -0
  117. package/dist/project.native.js +2 -0
  118. package/dist/project.native.js.map +1 -0
  119. package/dist/responder.d.ts +100 -0
  120. package/dist/responder.d.ts.map +1 -0
  121. package/dist/responder.js +256 -0
  122. package/dist/responder.js.map +1 -0
  123. package/dist/safe-area.native.d.ts +18 -0
  124. package/dist/safe-area.native.d.ts.map +1 -0
  125. package/dist/safe-area.native.js +72 -0
  126. package/dist/safe-area.native.js.map +1 -0
  127. package/dist/scrollable.d.ts +16 -0
  128. package/dist/scrollable.d.ts.map +1 -0
  129. package/dist/scrollable.js +114 -0
  130. package/dist/scrollable.js.map +1 -0
  131. package/dist/text-size.d.ts +12 -0
  132. package/dist/text-size.d.ts.map +1 -0
  133. package/dist/text-size.js +22 -0
  134. package/dist/text-size.js.map +1 -0
  135. package/package.json +131 -0
  136. package/src/activate.ts +45 -0
  137. package/src/ambient.ts +199 -0
  138. package/src/backdrop.ts +66 -0
  139. package/src/class-name.native.ts +53 -0
  140. package/src/class-resolver.ts +74 -0
  141. package/src/color-transition.ts +27 -0
  142. package/src/dom-style.native.ts +13 -0
  143. package/src/dom-style.ts +173 -0
  144. package/src/external-link.ts +12 -0
  145. package/src/generated/animation.ts +2 -0
  146. package/src/generated/class-resolver.ts +2 -0
  147. package/src/generated/dom-style.native.ts +1 -0
  148. package/src/generated/dom-style.ts +1 -0
  149. package/src/generated/environment.ts +10 -0
  150. package/src/generated/image-source.ts +2 -0
  151. package/src/generated/interactive.ts +1 -0
  152. package/src/generated/safe-area.ts +3 -0
  153. package/src/generated/scrollable.ts +1 -0
  154. package/src/hooks.native.ts +408 -0
  155. package/src/image-source.native.ts +4 -0
  156. package/src/index.native.ts +38 -0
  157. package/src/index.ts +50 -0
  158. package/src/interactive.ts +68 -0
  159. package/src/navigation-context.tsx +20 -0
  160. package/src/navigation-entry.native.ts +5 -0
  161. package/src/navigation-entry.ts +12 -0
  162. package/src/navigation.ts +55 -0
  163. package/src/project.native.ts +1 -0
  164. package/src/project.ts +9 -0
  165. package/src/responder.ts +372 -0
  166. package/src/safe-area.native.ts +87 -0
  167. package/src/scrollable.ts +119 -0
  168. package/src/text-size.ts +25 -0
@@ -0,0 +1,408 @@
1
+ // The React Native glue for `./ambient.ts`. Deliberately thin -- all the
2
+ // logic worth testing lives there, free of `react`/`react-native` imports
3
+ // so it can be tested without a device or a native module registry. This
4
+ // file is the part that can only be verified by running an app, same
5
+ // division as `@hozo/metro`'s `index.ts`.
6
+ //
7
+ // Generated components call these; nothing here is meant to be imported by
8
+ // hand.
9
+
10
+ import { useEffect, useMemo, useRef, useSyncExternalStore } from 'react'
11
+ import {
12
+ AccessibilityInfo,
13
+ Animated,
14
+ Appearance,
15
+ type ColorSchemeName,
16
+ Dimensions,
17
+ Easing,
18
+ I18nManager,
19
+ type ScaledSize,
20
+ } from 'react-native'
21
+
22
+ import {
23
+ type BreakpointName,
24
+ bucketFor,
25
+ createStore,
26
+ ENVIRONMENT_FACTS,
27
+ type EnvironmentQuery,
28
+ isAtLeast,
29
+ isPortrait,
30
+ sameViewport,
31
+ type Viewport,
32
+ } from './ambient.ts'
33
+
34
+ /// `Dimensions` hands back more than the two numbers wanted here (`scale`,
35
+ /// `fontScale`), and a fresh object every event. Narrowing it is what makes
36
+ /// the store's change check able to see that nothing moved.
37
+ function viewportOf({ width, height }: { width: number; height: number }): Viewport {
38
+ return { width, height }
39
+ }
40
+
41
+ // One subscription per app, not per component. See ./ambient.ts.
42
+
43
+ const colorSchemeStore = createStore<ColorSchemeName | null>(Appearance.getColorScheme())
44
+ Appearance.addChangeListener(({ colorScheme }) => {
45
+ colorSchemeStore.set(colorScheme)
46
+ })
47
+
48
+ function sameScaledSize(left: ScaledSize, right: ScaledSize): boolean {
49
+ return (
50
+ left.width === right.width &&
51
+ left.height === right.height &&
52
+ left.scale === right.scale &&
53
+ left.fontScale === right.fontScale
54
+ )
55
+ }
56
+
57
+ const initialWindow = Dimensions.get('window')
58
+ const breakpointStore = createStore(bucketFor(initialWindow.width))
59
+ // A second store over the same event, holding the size itself rather than
60
+ // the bucket. Kept separate on purpose: a component using only `md:`
61
+ // must not re-render on every resize that doesn't cross a breakpoint, and
62
+ // it wouldn't if these shared one snapshot.
63
+ const viewportStore = createStore(viewportOf(initialWindow), sameViewport)
64
+ const windowDimensionsStore = createStore(initialWindow, sameScaledSize)
65
+ const orientationStore = createStore(isPortrait(initialWindow))
66
+ Dimensions.addEventListener('change', ({ window }: { window: ScaledSize }) => {
67
+ breakpointStore.set(bucketFor(window.width))
68
+ viewportStore.set(viewportOf(window))
69
+ windowDimensionsStore.set(window)
70
+ orientationStore.set(isPortrait(window))
71
+ })
72
+
73
+ /** Whether the OS is in dark mode. Drives `dark:` utilities. */
74
+ export function useHozoDark(): boolean {
75
+ return useSyncExternalStore(
76
+ colorSchemeStore.subscribe,
77
+ () => colorSchemeStore.get() === 'dark',
78
+ () => colorSchemeStore.get() === 'dark',
79
+ )
80
+ }
81
+
82
+ /** React Native's color-scheme hook, backed by Hozo's shared native listener. */
83
+ export function useColorScheme(): ColorSchemeName | null {
84
+ return useSyncExternalStore(
85
+ colorSchemeStore.subscribe,
86
+ colorSchemeStore.get,
87
+ colorSchemeStore.get,
88
+ )
89
+ }
90
+
91
+ /**
92
+ * Whether the viewport is at least as wide as `name`'s breakpoint. Drives
93
+ * `sm:`/`md:`/`lg:`/`xl:`/`2xl:` utilities.
94
+ *
95
+ * Takes the name rather than returning the current bucket so the call
96
+ * reads as the condition it was compiled from: `md:` becomes
97
+ * `useHozoBreakpoint('md')`.
98
+ */
99
+ export function useHozoBreakpoint(name: BreakpointName): boolean {
100
+ const bucket = useSyncExternalStore(
101
+ breakpointStore.subscribe,
102
+ breakpointStore.get,
103
+ breakpointStore.get,
104
+ )
105
+ return isAtLeast(bucket, name)
106
+ }
107
+
108
+ /**
109
+ * Whether the window is at least `px` wide. Drives `min-[500px]:` and,
110
+ * negated, `max-[…]:` and `max-<breakpoint>:`.
111
+ *
112
+ * A threshold rather than a bucket, because an arbitrary one is by
113
+ * definition not one of the five. Just as cheap, though, and for a reason
114
+ * worth stating: the snapshot is the *predicate*, not the width, so React
115
+ * compares a boolean and skips the render on every resize that doesn't
116
+ * cross `px`. Rounding to buckets buys nothing over that -- it is the
117
+ * older of the two ideas, not the cheaper one.
118
+ */
119
+ export function useHozoWidthAtLeast(px: number): boolean {
120
+ return useSyncExternalStore(
121
+ viewportStore.subscribe,
122
+ () => viewportStore.get().width >= px,
123
+ () => viewportStore.get().width >= px,
124
+ )
125
+ }
126
+
127
+ /**
128
+ * The current window size. Drives the viewport-relative sizes -- `h-screen`
129
+ * compiles to `{ height: useHozoViewport().height }`.
130
+ *
131
+ * The window, not the screen: the window excludes system UI the app can't
132
+ * draw under, which is the closer analogue of the Web viewport. It does not
133
+ * subtract a notch or a home indicator, so a full-bleed layout still wants
134
+ * a safe-area inset on top of this -- exactly as it does on Web.
135
+ *
136
+ * Unlike the two above, this re-renders on every window change rather than
137
+ * only when a breakpoint is crossed. That is unavoidable for a size that
138
+ * has to track the window exactly, and it's why this is a separate store
139
+ * rather than the breakpoints being rebuilt on top of it.
140
+ */
141
+ export function useHozoViewport(): Viewport {
142
+ return useSyncExternalStore(viewportStore.subscribe, viewportStore.get, viewportStore.get)
143
+ }
144
+
145
+ /** React Native's window dimensions, backed by Hozo's shared native listener. */
146
+ export function useWindowDimensions(): ScaledSize {
147
+ return useSyncExternalStore(
148
+ windowDimensionsStore.subscribe,
149
+ windowDimensionsStore.get,
150
+ windowDimensionsStore.get,
151
+ )
152
+ }
153
+
154
+ /**
155
+ * Tailwind's four looping animations, as one hook.
156
+ *
157
+ * All four move only `opacity` and `transform`, which is what lets every
158
+ * one of them run on the native driver -- the animation continues on the
159
+ * UI thread while JavaScript is busy, which is exactly the moment a
160
+ * spinner or a skeleton is on screen. An animation that stalls while the
161
+ * work it is reporting on happens is worse than none.
162
+ *
163
+ * One `Animated.Value` running 0 → 1 on a loop, with the shape of each
164
+ * animation in the interpolation rather than in the timing. That keeps the
165
+ * timing identical between them and puts every difference in one place.
166
+ *
167
+ * `reduce-motion` is deliberately not consulted here. Tailwind's answer is
168
+ * `motion-safe:animate-spin`, a variant the compiler already handles, and
169
+ * making the hook second-guess it would mean a class that says animate
170
+ * sometimes not animating for reasons the source does not show.
171
+ */
172
+ export type HozoAnimation = 'spin' | 'pulse' | 'bounce' | 'ping'
173
+
174
+ const DURATIONS: Record<HozoAnimation, number> = {
175
+ spin: 1000,
176
+ // Tailwind's `pulse` is 2s and the others are 1s.
177
+ pulse: 2000,
178
+ bounce: 1000,
179
+ ping: 1000,
180
+ }
181
+
182
+ /**
183
+ * The easing for one animation, built when it is asked for.
184
+ *
185
+ * A function rather than a table, because a table is evaluated at import
186
+ * time and `Easing.bezier` would be called on every native import of this
187
+ * package -- which is how the accessibility settings above ended up
188
+ * needing the same treatment. Nothing here should run for a project that
189
+ * imports the module and never animates.
190
+ */
191
+ function easingFor(name: HozoAnimation): (value: number) => number {
192
+ switch (name) {
193
+ // `cubic-bezier(.4,0,.6,1)`, which is symmetric, so the fade out and
194
+ // back read as one breath rather than two movements.
195
+ case 'pulse':
196
+ return Easing.bezier(0.4, 0, 0.6, 1)
197
+ case 'ping':
198
+ return Easing.bezier(0, 0, 0.2, 1)
199
+ // Linear on purpose for the bounce: its shape is in the interpolation
200
+ // below, and easing it again would ease it twice.
201
+ default:
202
+ return Easing.linear
203
+ }
204
+ }
205
+
206
+ export function useHozoAnimation(name: HozoAnimation) {
207
+ const progress = useRef(new Animated.Value(0)).current
208
+
209
+ useEffect(() => {
210
+ progress.setValue(0)
211
+ const animation = Animated.loop(
212
+ Animated.timing(progress, {
213
+ toValue: 1,
214
+ duration: DURATIONS[name],
215
+ easing: easingFor(name),
216
+ useNativeDriver: true,
217
+ }),
218
+ )
219
+ animation.start()
220
+ return () => animation.stop()
221
+ }, [progress, name])
222
+
223
+ return useMemo(() => {
224
+ switch (name) {
225
+ case 'spin':
226
+ return {
227
+ transform: [
228
+ {
229
+ rotate: progress.interpolate({ inputRange: [0, 1], outputRange: ['0deg', '360deg'] }),
230
+ },
231
+ ],
232
+ }
233
+ case 'pulse':
234
+ return {
235
+ opacity: progress.interpolate({ inputRange: [0, 0.5, 1], outputRange: [1, 0.5, 1] }),
236
+ }
237
+ case 'bounce':
238
+ return {
239
+ transform: [
240
+ {
241
+ // A percentage, because Tailwind's bounce is -25% of the
242
+ // element's own height and a pixel figure would be right at
243
+ // one size only. React Native has taken percentages in
244
+ // `translateY` since 0.76, below this package's floor of 0.86.
245
+ //
246
+ // The two halves are not symmetric: Tailwind eases the rise
247
+ // with `cubic-bezier(.8,0,1,1)` and the fall with
248
+ // `cubic-bezier(0,0,.2,1)`, so it leaves slowly and lands
249
+ // quickly. The extra input points are that asymmetry drawn
250
+ // out, since one `Animated.timing` cannot hold two easings.
251
+ translateY: progress.interpolate({
252
+ inputRange: [0, 0.25, 0.5, 0.75, 1],
253
+ outputRange: ['0%', '-15%', '-25%', '-10%', '0%'],
254
+ }),
255
+ },
256
+ ],
257
+ }
258
+ case 'ping':
259
+ return {
260
+ opacity: progress.interpolate({ inputRange: [0, 1], outputRange: [1, 0] }),
261
+ transform: [{ scale: progress.interpolate({ inputRange: [0, 1], outputRange: [1, 2] }) }],
262
+ }
263
+ }
264
+ }, [progress, name])
265
+ }
266
+
267
+ // The environment queries, on the same one-subscription-per-app footing as
268
+ // `dark:` above. Four facts answer seven queries: `motion-safe` is
269
+ // `motion-reduce` negated, `landscape` is `portrait` negated, `ltr` is
270
+ // `rtl` negated.
271
+
272
+ const reduceMotionStore = createStore(false)
273
+ const invertColorsStore = createStore(false)
274
+ // The four Tailwind has no name for. Three are iOS-only, and React Native
275
+ // resolves those to `false` on Android rather than rejecting them, so
276
+ // nothing here needs a platform check: a style that does not fire where
277
+ // the setting does not exist is correct.
278
+ const reduceTransparencyStore = createStore(false)
279
+ const boldTextStore = createStore(false)
280
+ const grayscaleStore = createStore(false)
281
+ const screenReaderStore = createStore(false)
282
+ // `contrast-more:` alone has two settings behind it, one per platform, and
283
+ // they get a store each rather than sharing one. Sharing would mean two
284
+ // asynchronous reads racing to write the same value: on a device where one
285
+ // method is present and the other resolves `false`, whichever landed last
286
+ // would win and the answer would depend on scheduling. Two stores combined
287
+ // at read time cannot do that.
288
+ const darkerSystemColorsStore = createStore(false)
289
+ const highTextContrastStore = createStore(false)
290
+
291
+ // Asynchronous, unlike `Appearance.getColorScheme()`: these cross to the
292
+ // platform. The store starts at `false` and corrects itself, which is the
293
+ // right way round -- a first frame that under-reports reduced motion
294
+ // animates once, and one that over-reports it silently drops an animation
295
+ // the user asked for.
296
+ void AccessibilityInfo.isReduceMotionEnabled().then((value) => reduceMotionStore.set(value))
297
+ void AccessibilityInfo.isInvertColorsEnabled().then((value) => invertColorsStore.set(value))
298
+ AccessibilityInfo.addEventListener('reduceMotionChanged', (value) => reduceMotionStore.set(value))
299
+ AccessibilityInfo.addEventListener('invertColorsChanged', (value) => invertColorsStore.set(value))
300
+ // Asked for by name rather than called directly, because this runs at
301
+ // import time and a missing method there takes the whole app down before
302
+ // anything renders. These four are newer than the two above and are not
303
+ // all present on every React Native version or every platform's native
304
+ // module -- and a setting Hozo cannot read is a `false`, which is what the
305
+ // store already holds.
306
+ function track(
307
+ read: keyof typeof AccessibilityInfo,
308
+ event: Parameters<typeof AccessibilityInfo.addEventListener>[0],
309
+ store: { set: (value: boolean) => void },
310
+ ) {
311
+ const method = AccessibilityInfo[read]
312
+ if (typeof method !== 'function') return
313
+ void (method as () => Promise<boolean>)
314
+ .call(AccessibilityInfo)
315
+ .then((value) => store.set(value))
316
+ // A rejection is the same answer as a missing method: unknown, and
317
+ // `false` is the safe reading of unknown for all four.
318
+ .catch(() => {})
319
+ AccessibilityInfo.addEventListener(event, (value) => store.set(value as boolean))
320
+ }
321
+
322
+ track('isReduceTransparencyEnabled', 'reduceTransparencyChanged', reduceTransparencyStore)
323
+ track('isBoldTextEnabled', 'boldTextChanged', boldTextStore)
324
+ track('isGrayscaleEnabled', 'grayscaleChanged', grayscaleStore)
325
+ // The one that changes mid-session more than any of the others: someone
326
+ // turning VoiceOver on to read a screen they are already looking at is the
327
+ // ordinary case, not an edge one.
328
+ track('isScreenReaderEnabled', 'screenReaderChanged', screenReaderStore)
329
+ // iOS's Increase Contrast, which is the same OS setting Safari writes
330
+ // `prefers-contrast: more` from -- so a component styled with
331
+ // `contrast-more:` behaves the same in a WebView and in a native screen.
332
+ track('isDarkerSystemColorsEnabled', 'darkerSystemColorsChanged', darkerSystemColorsStore)
333
+ // Android's is narrower: high-contrast *text*, not a general contrast
334
+ // increase. Read as the nearest thing that platform has rather than left
335
+ // unanswered, which would make `contrast-more:` iOS-only in practice.
336
+ track('isHighTextContrastEnabled', 'highTextContrastChanged', highTextContrastStore)
337
+
338
+ /**
339
+ * Whether an environment query holds. Drives `motion-reduce:`, `ltr:` and
340
+ * the rest.
341
+ *
342
+ * `I18nManager.isRTL` has no store because it has no event: React Native
343
+ * requires a reload for a direction change to take effect, so within one
344
+ * run it is a constant.
345
+ */
346
+ export function useHozoEnvironment(query: EnvironmentQuery): boolean {
347
+ const { fact, negate } = ENVIRONMENT_FACTS[query]
348
+ const reduceMotion = useSyncExternalStore(
349
+ reduceMotionStore.subscribe,
350
+ reduceMotionStore.get,
351
+ reduceMotionStore.get,
352
+ )
353
+ const invertColors = useSyncExternalStore(
354
+ invertColorsStore.subscribe,
355
+ invertColorsStore.get,
356
+ invertColorsStore.get,
357
+ )
358
+ const portrait = useSyncExternalStore(
359
+ orientationStore.subscribe,
360
+ orientationStore.get,
361
+ orientationStore.get,
362
+ )
363
+ const reduceTransparency = useSyncExternalStore(
364
+ reduceTransparencyStore.subscribe,
365
+ reduceTransparencyStore.get,
366
+ reduceTransparencyStore.get,
367
+ )
368
+ const boldText = useSyncExternalStore(
369
+ boldTextStore.subscribe,
370
+ boldTextStore.get,
371
+ boldTextStore.get,
372
+ )
373
+ const grayscale = useSyncExternalStore(
374
+ grayscaleStore.subscribe,
375
+ grayscaleStore.get,
376
+ grayscaleStore.get,
377
+ )
378
+ const screenReader = useSyncExternalStore(
379
+ screenReaderStore.subscribe,
380
+ screenReaderStore.get,
381
+ screenReaderStore.get,
382
+ )
383
+ const darkerSystemColors = useSyncExternalStore(
384
+ darkerSystemColorsStore.subscribe,
385
+ darkerSystemColorsStore.get,
386
+ darkerSystemColorsStore.get,
387
+ )
388
+ const highTextContrast = useSyncExternalStore(
389
+ highTextContrastStore.subscribe,
390
+ highTextContrastStore.get,
391
+ highTextContrastStore.get,
392
+ )
393
+ const value = {
394
+ reduceMotion,
395
+ invertColors,
396
+ portrait,
397
+ rtl: I18nManager.isRTL,
398
+ reduceTransparency,
399
+ boldText,
400
+ grayscale,
401
+ screenReader,
402
+ // Either platform's setting answers the query. Only one of the two can
403
+ // be true on a given device -- each method exists on one platform --
404
+ // so the `||` is a union of platforms rather than of settings.
405
+ contrastMore: darkerSystemColors || highTextContrast,
406
+ }[fact]
407
+ return negate ? !value : value
408
+ }
@@ -0,0 +1,4 @@
1
+ /** Normalizes Hozo's universal Image `src` to React Native's source shape. */
2
+ export function hozoImageSource<T extends number | object>(src: string | T): { uri: string } | T {
3
+ return typeof src === 'string' ? { uri: src } : src
4
+ }
@@ -0,0 +1,38 @@
1
+ // The React Native entry point for `@hozo/engine`.
2
+ //
3
+ // Metro resolves a platform extension ahead of the plain file, so an app
4
+ // importing `@hozo/engine` gets this on device and `./index.ts` on Web.
5
+ // That split is what lets the parts needing `react`/`react-native` be
6
+ // exported at all: `./index.ts` is imported by the Web build too, where
7
+ // those modules don't exist.
8
+ //
9
+ // Generated infrastructure such as `useHozoDark` remains reachable here,
10
+ // but compiled output imports it leaf by leaf from `./generated/*` (through
11
+ // `@hozo/core/generated/*`). Compiler-generated React components are owned
12
+ // by domain packages, including `@hozo/primitives/generated/*` for Native
13
+ // layout boundaries.
14
+
15
+ // The `className` prop, for TypeScript only. Imported for its side
16
+ // effect: the file declares nothing at runtime and augments React
17
+ // Native's prop interfaces so an app can be type-checked against the same
18
+ // source the compiler reads. See the file for why it is here rather than
19
+ // in `@hozo/core`.
20
+ import './class-name.native.ts'
21
+
22
+ export type { BreakpointName, Viewport } from './ambient.ts'
23
+ // `index.ts` names the Web implementation explicitly after TypeScript
24
+ // emits it. Override that star export so Metro never sends DOM conversion
25
+ // through a native render path.
26
+ export { type HozoDomStyle, hozoDomProps, hozoDomStyle } from './dom-style.native.ts'
27
+ export {
28
+ type HozoAnimation,
29
+ useHozoAnimation,
30
+ useHozoBreakpoint,
31
+ useHozoDark,
32
+ useHozoEnvironment,
33
+ useHozoViewport,
34
+ useHozoWidthAtLeast,
35
+ } from './hooks.native.ts'
36
+ export { hozoImageSource } from './image-source.native.ts'
37
+ export * from './index.ts'
38
+ export { type HozoSafeAreaInsets, useHozoSafeArea } from './safe-area.native.ts'
package/src/index.ts ADDED
@@ -0,0 +1,50 @@
1
+ // The only thing Hozo ships to a device, and it exists for exactly one
2
+ // reason: React Native has no CSS engine.
3
+ //
4
+ // On Web, a `className` the compiler couldn't read needs no runtime at all
5
+ // -- the class string reaches the DOM, and the browser matches it against
6
+ // the generated candidate stylesheet by itself. Native has nothing to hand
7
+ // the string to, so this closes that one gap and nothing else.
8
+ //
9
+ // One exception, added later: a `<div role="button">` that Hozo put in the
10
+ // tab order needs Enter and Space wired up, and only script can do that.
11
+ // See `./activate.ts`.
12
+ //
13
+ // Compare `react-native-css`, which implements a genuine CSS engine in JS:
14
+ // specificity sorting, media/container query evaluation, CSS variables, a
15
+ // reactive observable graph. It needs all of that because it accepts
16
+ // arbitrary CSS. This resolver handles only *single Tailwind utility
17
+ // classes*, which are all the same specificity -- so "later in the string
18
+ // wins" is the entire cascade, and React Native's own style-array merging
19
+ // already implements that. Hence a lookup, not an engine.
20
+
21
+ export { hozoActivateKeyDown, hozoActivateKeyUp } from './activate.ts'
22
+ export {
23
+ configureHozoBackdropFilter,
24
+ createExpoBlurAdapter,
25
+ type ExpoBlurAdapterOptions,
26
+ type HozoBackdropFilterAdapter,
27
+ type HozoBackdropFilterAdapterProps,
28
+ hozoBackdropFilterAdapter,
29
+ } from './backdrop.ts'
30
+
31
+ export { type ClassResolver, createClassResolver, type StyleObject } from './class-resolver.ts'
32
+ export { blendColor } from './color-transition.ts'
33
+ export { type HozoDomStyle, hozoDomProps, hozoDomStyle } from './dom-style.ts'
34
+ export { externalLinkAttributes } from './external-link.ts'
35
+ export { hozoInteractive } from './interactive.ts'
36
+ export {
37
+ type HozoResponderEvent,
38
+ type HozoResponderTouch,
39
+ type HozoTouchHistory,
40
+ type HozoTouchTrack,
41
+ type ResponderProps,
42
+ useResponderDomProps,
43
+ } from './responder.ts'
44
+ export { hozoScrollable } from './scrollable.ts'
45
+
46
+ export {
47
+ HOZO_DEFAULT_FONT_SIZE,
48
+ HozoTextSizeContext,
49
+ useHozoTextSize,
50
+ } from './text-size.ts'
@@ -0,0 +1,68 @@
1
+ // Everything `disabled` means, in one place.
2
+ //
3
+ // `docs/decisions/001-disabled-and-focus.md` settles it as five things at
4
+ // once: inoperable, announced as disabled, present in the accessibility
5
+ // tree, out of the tab order, and -- for an element Hozo made interactive
6
+ // itself -- not activatable by Enter or Space either.
7
+ //
8
+ // Five things emitted as five independent expressions is exactly how they
9
+ // come apart, and they had. The compiled path announced `aria-disabled`
10
+ // and then ran the handler anyway; the fallback component in `@hozo/core`
11
+ // suppressed the handler but had no keyboard activation to suppress. Two
12
+ // paths, two different wrong answers, from one prop.
13
+ //
14
+ // So both call this instead. A drift between them now needs someone to
15
+ // edit this file and mean it.
16
+ //
17
+ // Only for elements Hozo had to synthesize -- a `<div role="button">`. A
18
+ // real `<button>` takes the `disabled` attribute and the browser does all
19
+ // five, which is why `Button` does not come through here.
20
+
21
+ import { hozoActivateKeyDown, hozoActivateKeyUp } from './activate.ts'
22
+
23
+ /** What React hands a click handler on the element Hozo synthesized. */
24
+ type PressHandler = (event: never) => void
25
+
26
+ /**
27
+ * The props that make an element a control, and unmake it when disabled.
28
+ *
29
+ * `tabIndex` is `-1` rather than absent when disabled: it leaves the tab
30
+ * order, which is the decision, while staying reachable by `focus()`,
31
+ * which is what focus management and roving tabindex need. See 001's
32
+ * rule 1a.
33
+ *
34
+ * Both branches carry the same keys so that spreading this never leaves a
35
+ * stale attribute behind from the previous render.
36
+ */
37
+ export function hozoInteractive(onPress?: PressHandler, disabled?: unknown) {
38
+ return disabled
39
+ ? {
40
+ 'aria-disabled': true,
41
+ // The styling hook, kept separate from how the state is said.
42
+ // `disabled:opacity-50` compiles to `[data-hozo-disabled]`, which
43
+ // works on any element -- `:disabled` matches form controls only,
44
+ // so on the `<div>` a Pressable becomes it matched nothing.
45
+ //
46
+ // An empty string, not `true`: React renders `data-x={false}` as
47
+ // the string `"false"` (unlike `aria-*` and unlike known boolean
48
+ // attributes), and `[data-hozo-disabled]` matches on presence, so
49
+ // a boolean here would have made every control permanently
50
+ // disabled-looking. `undefined` is the only value React omits.
51
+ 'data-hozo-disabled': '',
52
+ tabIndex: -1,
53
+ onClick: undefined,
54
+ onKeyDown: undefined,
55
+ onKeyUp: undefined,
56
+ }
57
+ : {
58
+ // Not `false`: an element that was never disabled says nothing
59
+ // about it, and `aria-disabled="false"` on every control in a page
60
+ // is noise a screen reader has to read past.
61
+ 'aria-disabled': undefined,
62
+ 'data-hozo-disabled': undefined,
63
+ tabIndex: 0,
64
+ onClick: onPress,
65
+ onKeyDown: hozoActivateKeyDown,
66
+ onKeyUp: hozoActivateKeyUp,
67
+ }
68
+ }
@@ -0,0 +1,20 @@
1
+ import { createContext, type ReactNode, useContext } from 'react'
2
+
3
+ import type { HozoNavigationAdapter } from './navigation.ts'
4
+
5
+ const NavigationContext = createContext<HozoNavigationAdapter | null>(null)
6
+
7
+ export interface HozoNavigationProviderProps {
8
+ adapter: HozoNavigationAdapter
9
+ children?: ReactNode
10
+ }
11
+
12
+ /** Internal provider used by the public `@hozo/navigation` integration. */
13
+ export function HozoNavigationProvider({ adapter, children }: HozoNavigationProviderProps) {
14
+ return <NavigationContext value={adapter}>{children}</NavigationContext>
15
+ }
16
+
17
+ /** Returns null when an application deliberately has no router integration. */
18
+ export function useHozoNavigation(): HozoNavigationAdapter | null {
19
+ return useContext(NavigationContext)
20
+ }
@@ -0,0 +1,5 @@
1
+ // Keep the subpath resolvable through package `react-native` conditions.
2
+ // The contract and context are platform-neutral; this file exists because
3
+ // every runtime subpath names its Native target explicitly in the generated
4
+ // package exports map.
5
+ export * from './navigation-entry.ts'
@@ -0,0 +1,12 @@
1
+ export {
2
+ activateHozoNavigation,
3
+ type HozoNavigationAdapter,
4
+ type HozoNavigationFallback,
5
+ type HozoNavigationRequest,
6
+ prefetchHozoNavigation,
7
+ } from './navigation.ts'
8
+ export {
9
+ HozoNavigationProvider,
10
+ type HozoNavigationProviderProps,
11
+ useHozoNavigation,
12
+ } from './navigation-context.tsx'
@@ -0,0 +1,55 @@
1
+ /** A destination-bearing interaction after component-specific events run. */
2
+ export interface HozoNavigationRequest {
3
+ href: string
4
+ /** Bypass application routing and use the platform destination handler. */
5
+ external?: boolean
6
+ /** Replace the current router history entry instead of pushing a new one. */
7
+ replace?: boolean
8
+ }
9
+
10
+ /**
11
+ * The deliberately small seam between universal primitives and a router.
12
+ *
13
+ * `true` means the router accepted the destination. Returning `false` lets
14
+ * the platform fallback handle it instead. The adapter owns route matching;
15
+ * the runtime cannot reliably infer whether an absolute URL belongs to an
16
+ * application, or whether a custom scheme is an internal native route.
17
+ */
18
+ export interface HozoNavigationAdapter {
19
+ navigate(request: HozoNavigationRequest): boolean | Promise<boolean>
20
+ /** Warms an application-owned destination without navigating to it. */
21
+ prefetch?(request: HozoNavigationRequest): unknown | Promise<unknown>
22
+ }
23
+
24
+ export type HozoNavigationFallback = (href: string) => unknown | Promise<unknown>
25
+
26
+ /**
27
+ * Offers an internal destination to the installed router, then falls back.
28
+ *
29
+ * Explicit external links never enter application routing. A declined route
30
+ * and an absent adapter are equivalent, which keeps every primitive useful
31
+ * before `@hozo/navigation` is installed and prevents the adapter from
32
+ * becoming a required global singleton.
33
+ */
34
+ export async function activateHozoNavigation(
35
+ adapter: HozoNavigationAdapter | null,
36
+ request: HozoNavigationRequest,
37
+ fallback: HozoNavigationFallback,
38
+ ): Promise<void> {
39
+ if (!request.external && adapter && (await adapter.navigate(request))) return
40
+ await fallback(request.href)
41
+ }
42
+
43
+ /** Offers a destination to an installed router without a platform fallback. */
44
+ export function prefetchHozoNavigation(
45
+ adapter: HozoNavigationAdapter | null,
46
+ request: HozoNavigationRequest,
47
+ ): void {
48
+ if (request.external) return
49
+ try {
50
+ const result = adapter?.prefetch?.(request)
51
+ if (result !== undefined) void Promise.resolve(result).catch(() => {})
52
+ } catch {
53
+ // Prefetch is speculative. A cache warm-up must never break interaction.
54
+ }
55
+ }
@@ -0,0 +1 @@
1
+ export { hozoPreflight } from './project.ts'
package/src/project.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Project-wide Native defaults when no bundler integration supplies them.
3
+ *
4
+ * `@hozo/metro` redirects this entry to the generated candidate module,
5
+ * whose value is the resolved `preflight` option. Keeping a real entry here
6
+ * makes unconfigured builds and component tests deterministic; Tailwind's
7
+ * reset is the common case and was the fallback before this entry existed.
8
+ */
9
+ export const hozoPreflight = true