@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,372 @@
1
+ import {
2
+ type PointerEventHandler,
3
+ type PointerEvent as ReactPointerEvent,
4
+ type RefObject,
5
+ useEffect,
6
+ useRef,
7
+ } from 'react'
8
+
9
+ export interface HozoResponderTouch {
10
+ identifier: number
11
+ locationX: number
12
+ locationY: number
13
+ pageX: number
14
+ pageY: number
15
+ /**
16
+ * What was touched, in whichever platform's currency.
17
+ *
18
+ * A DOM node in the browser and a native view tag -- a number -- on
19
+ * device, with `undefined` because React Native's own touch declares it
20
+ * optional. This said `EventTarget | null` and nothing else, which is
21
+ * the browser's answer written into a shape whose whole purpose is to
22
+ * be platform-free.
23
+ *
24
+ * That was invisible for the same reason the missing `className` was:
25
+ * the `react-native` export condition carried no `types`, so `tsc` in a
26
+ * React Native app never asked React Native's declarations whether these
27
+ * handlers fit. Asked, it says no -- `{...pan.panHandlers}` on a native
28
+ * `View` does not type-check, because a handler taking this event is not
29
+ * one React Native may call.
30
+ */
31
+ target: EventTarget | number | null | undefined
32
+ timestamp: number
33
+ }
34
+
35
+ export interface HozoResponderEvent {
36
+ nativeEvent: HozoResponderTouch & {
37
+ // `readonly`, because React Native's are, and a `readonly T[]` is not
38
+ // assignable to a `T[]`. Nothing here mutates them.
39
+ changedTouches: readonly HozoResponderTouch[]
40
+ touches: readonly HozoResponderTouch[]
41
+ }
42
+ touchHistory: HozoTouchHistory
43
+ preventDefault(): void
44
+ stopPropagation(): void
45
+ }
46
+
47
+ export interface HozoTouchTrack {
48
+ touchActive: boolean
49
+ currentPageX: number
50
+ currentPageY: number
51
+ currentTimeStamp: number
52
+ previousPageX: number
53
+ previousPageY: number
54
+ previousTimeStamp: number
55
+ }
56
+
57
+ export interface HozoTouchHistory {
58
+ /** `readonly` for the same reason the touch lists are. */
59
+ touchBank: readonly HozoTouchTrack[]
60
+ numberActiveTouches: number
61
+ indexOfSingleActiveTouch: number
62
+ mostRecentTimeStamp: number
63
+ }
64
+
65
+ export interface ResponderProps {
66
+ onStartShouldSetResponder?: (event: HozoResponderEvent) => boolean
67
+ onStartShouldSetResponderCapture?: (event: HozoResponderEvent) => boolean
68
+ onMoveShouldSetResponder?: (event: HozoResponderEvent) => boolean
69
+ onMoveShouldSetResponderCapture?: (event: HozoResponderEvent) => boolean
70
+ onResponderGrant?: (event: HozoResponderEvent) => void
71
+ onResponderStart?: (event: HozoResponderEvent) => void
72
+ onResponderMove?: (event: HozoResponderEvent) => void
73
+ onResponderEnd?: (event: HozoResponderEvent) => void
74
+ onResponderRelease?: (event: HozoResponderEvent) => void
75
+ onResponderReject?: (event: HozoResponderEvent) => void
76
+ onResponderTerminate?: (event: HozoResponderEvent) => void
77
+ onResponderTerminationRequest?: (event: HozoResponderEvent) => boolean
78
+ }
79
+
80
+ interface Registration {
81
+ element: HTMLElement
82
+ pointerIds: Set<number>
83
+ props: RefObject<ResponderProps>
84
+ }
85
+
86
+ let activeResponder: Registration | undefined
87
+
88
+ interface PointerSnapshot {
89
+ identifier: number
90
+ clientX: number
91
+ clientY: number
92
+ pageX: number
93
+ pageY: number
94
+ target: EventTarget | null
95
+ timestamp: number
96
+ }
97
+
98
+ const activePointers = new Map<number, PointerSnapshot>()
99
+ const pointerHistory = new Map<number, HozoTouchTrack>()
100
+ const trackedEvents = new WeakSet<object>()
101
+ let globalCleanupInstalled = false
102
+
103
+ function endPointer(pointerId: number, pageX?: number, pageY?: number, timestamp?: number) {
104
+ activePointers.delete(pointerId)
105
+ const track = pointerHistory.get(pointerId)
106
+ if (track) {
107
+ track.touchActive = false
108
+ if (pageX !== undefined && pageY !== undefined && timestamp !== undefined) {
109
+ track.previousPageX = track.currentPageX
110
+ track.previousPageY = track.currentPageY
111
+ track.previousTimeStamp = track.currentTimeStamp
112
+ track.currentPageX = pageX
113
+ track.currentPageY = pageY
114
+ track.currentTimeStamp = timestamp
115
+ }
116
+ }
117
+ }
118
+
119
+ function ensureGlobalPointerCleanup() {
120
+ if (globalCleanupInstalled || typeof window === 'undefined') return
121
+ globalCleanupInstalled = true
122
+ const remove = (event: PointerEvent) => {
123
+ endPointer(event.pointerId, event.pageX, event.pageY, event.timeStamp)
124
+ }
125
+ window.addEventListener('pointerup', remove)
126
+ window.addEventListener('pointercancel', remove)
127
+ }
128
+
129
+ function eventKey(event: ReactPointerEvent<HTMLElement>): object {
130
+ return typeof event.nativeEvent === 'object' && event.nativeEvent !== null
131
+ ? event.nativeEvent
132
+ : event
133
+ }
134
+
135
+ function trackPointer(
136
+ event: ReactPointerEvent<HTMLElement>,
137
+ phase: 'start' | 'move' | 'end',
138
+ ): boolean {
139
+ const key = eventKey(event)
140
+ if (trackedEvents.has(key)) return false
141
+ trackedEvents.add(key)
142
+ if (phase === 'end') {
143
+ endPointer(event.pointerId, event.pageX, event.pageY, event.timeStamp)
144
+ } else {
145
+ ensureGlobalPointerCleanup()
146
+ if (phase === 'start' && activePointers.size === 0) pointerHistory.clear()
147
+ activePointers.set(event.pointerId, {
148
+ identifier: event.pointerId,
149
+ clientX: event.clientX,
150
+ clientY: event.clientY,
151
+ pageX: event.pageX,
152
+ pageY: event.pageY,
153
+ target: event.target,
154
+ timestamp: event.timeStamp,
155
+ })
156
+ const previous = pointerHistory.get(event.pointerId)
157
+ pointerHistory.set(event.pointerId, {
158
+ touchActive: true,
159
+ currentPageX: event.pageX,
160
+ currentPageY: event.pageY,
161
+ currentTimeStamp: event.timeStamp,
162
+ previousPageX: phase === 'move' && previous ? previous.currentPageX : event.pageX,
163
+ previousPageY: phase === 'move' && previous ? previous.currentPageY : event.pageY,
164
+ previousTimeStamp: phase === 'move' && previous ? previous.currentTimeStamp : event.timeStamp,
165
+ })
166
+ }
167
+ return true
168
+ }
169
+
170
+ function releaseRegistration(props: RefObject<ResponderProps>) {
171
+ const incumbent = activeResponder
172
+ if (!incumbent || incumbent.props !== props) return
173
+ activeResponder = undefined
174
+ for (const pointerId of incumbent.pointerIds) {
175
+ if (incumbent.element.hasPointerCapture?.(pointerId)) {
176
+ incumbent.element.releasePointerCapture?.(pointerId)
177
+ }
178
+ }
179
+ }
180
+
181
+ function responderEvent(event: ReactPointerEvent<HTMLElement>, ended = false): HozoResponderEvent {
182
+ const rect = event.currentTarget.getBoundingClientRect()
183
+ const toTouch = (pointer: PointerSnapshot): HozoResponderTouch => ({
184
+ identifier: pointer.identifier,
185
+ locationX: pointer.clientX - rect.left,
186
+ locationY: pointer.clientY - rect.top,
187
+ pageX: pointer.pageX,
188
+ pageY: pointer.pageY,
189
+ target: pointer.target,
190
+ timestamp: pointer.timestamp,
191
+ })
192
+ const changed = toTouch({
193
+ identifier: event.pointerId,
194
+ clientX: event.clientX,
195
+ clientY: event.clientY,
196
+ pageX: event.pageX,
197
+ pageY: event.pageY,
198
+ target: event.target,
199
+ timestamp: event.timeStamp,
200
+ })
201
+ const touches = [...activePointers.values()].map(toTouch)
202
+ const touchBank = [...pointerHistory.values()]
203
+ const activeIndices = touchBank
204
+ .map((track, index) => (track.touchActive ? index : -1))
205
+ .filter((index) => index >= 0)
206
+ return {
207
+ nativeEvent: {
208
+ ...changed,
209
+ changedTouches: [changed],
210
+ touches: ended ? touches.filter((touch) => touch.identifier !== event.pointerId) : touches,
211
+ },
212
+ touchHistory: {
213
+ touchBank,
214
+ numberActiveTouches: activePointers.size,
215
+ indexOfSingleActiveTouch: activeIndices.length === 1 ? activeIndices[0]! : -1,
216
+ mostRecentTimeStamp: touchBank.reduce(
217
+ (latest, track) => Math.max(latest, track.currentTimeStamp),
218
+ 0,
219
+ ),
220
+ },
221
+ preventDefault: () => event.preventDefault(),
222
+ stopPropagation: () => event.stopPropagation(),
223
+ }
224
+ }
225
+
226
+ function claim(
227
+ element: HTMLElement,
228
+ props: RefObject<ResponderProps>,
229
+ event: ReactPointerEvent<HTMLElement>,
230
+ ): boolean {
231
+ if (!event.isPrimary || activeResponder?.element === element) return false
232
+
233
+ const value = responderEvent(event)
234
+ if (activeResponder) {
235
+ const incumbent = activeResponder
236
+ const allowsTermination = incumbent.props.current.onResponderTerminationRequest?.(value) ?? true
237
+ if (!allowsTermination) {
238
+ props.current.onResponderReject?.(value)
239
+ return false
240
+ }
241
+ activeResponder = undefined
242
+ incumbent.props.current.onResponderTerminate?.(value)
243
+ for (const pointerId of incumbent.pointerIds) {
244
+ if (incumbent.element.hasPointerCapture?.(pointerId)) {
245
+ incumbent.element.releasePointerCapture?.(pointerId)
246
+ }
247
+ }
248
+ }
249
+
250
+ activeResponder = { element, pointerIds: new Set([event.pointerId]), props }
251
+ element.setPointerCapture?.(event.pointerId)
252
+ props.current.onResponderGrant?.(value)
253
+ props.current.onResponderStart?.(value)
254
+ return true
255
+ }
256
+
257
+ function finish(element: HTMLElement, event: ReactPointerEvent<HTMLElement>, terminated: boolean) {
258
+ const incumbent = activeResponder
259
+ if (!incumbent || incumbent.element !== element || !incumbent.pointerIds.has(event.pointerId))
260
+ return
261
+ incumbent.pointerIds.delete(event.pointerId)
262
+ const value = responderEvent(event, true)
263
+ incumbent.props.current.onResponderEnd?.(value)
264
+ if (element.hasPointerCapture?.(event.pointerId)) element.releasePointerCapture?.(event.pointerId)
265
+ if (!terminated && incumbent.pointerIds.size > 0) return
266
+ activeResponder = undefined
267
+ if (terminated) incumbent.props.current.onResponderTerminate?.(value)
268
+ else incumbent.props.current.onResponderRelease?.(value)
269
+ }
270
+
271
+ export function useResponderDomProps<T extends HTMLElement>(
272
+ elementRef: RefObject<T | null>,
273
+ props: ResponderProps,
274
+ enabled = true,
275
+ ) {
276
+ const propsRef = useRef(props)
277
+ propsRef.current = props
278
+ // `enabled` is not read in the effect and is not there for that: the
279
+ // cleanup is the body, and it has to run when a responder is turned off
280
+ // rather than only when it unmounts. Dropping the dependency leaves a
281
+ // disabled element holding the pointer capture.
282
+ // biome-ignore lint/correctness/useExhaustiveDependencies: turning it off has to release
283
+ useEffect(() => () => releaseRegistration(propsRef), [enabled])
284
+ return createResponderDomProps(elementRef, propsRef, enabled)
285
+ }
286
+
287
+ export function createResponderDomProps<T extends HTMLElement>(
288
+ elementRef: RefObject<T | null>,
289
+ propsRef: RefObject<ResponderProps>,
290
+ enabled = true,
291
+ ) {
292
+ if (!enabled) return {}
293
+
294
+ const negotiate = (
295
+ shouldSet: ((event: HozoResponderEvent) => boolean) | undefined,
296
+ event: ReactPointerEvent<T>,
297
+ ) => {
298
+ const element = elementRef.current
299
+ if (!element || activeResponder?.element === element || !shouldSet?.(responderEvent(event)))
300
+ return false
301
+ return claim(element, propsRef, event)
302
+ }
303
+
304
+ const onPointerDown: PointerEventHandler<T> = (event) => {
305
+ const isNewPointer = trackPointer(event, 'start')
306
+ const element = elementRef.current
307
+ const incumbent = activeResponder
308
+ if (isNewPointer && element && incumbent?.element === element) {
309
+ incumbent.pointerIds.add(event.pointerId)
310
+ element.setPointerCapture?.(event.pointerId)
311
+ propsRef.current.onResponderStart?.(responderEvent(event))
312
+ return
313
+ }
314
+ // The responder negotiation bubbles deepest-first. Once a child wins,
315
+ // ancestors must not make a second claim from the same pointer start.
316
+ if (negotiate(propsRef.current.onStartShouldSetResponder, event)) event.stopPropagation()
317
+ }
318
+ const onPointerDownCapture: PointerEventHandler<T> = (event) => {
319
+ const isNewPointer = trackPointer(event, 'start')
320
+ const element = elementRef.current
321
+ const incumbent = activeResponder
322
+ if (isNewPointer && element && incumbent?.element === element) {
323
+ incumbent.pointerIds.add(event.pointerId)
324
+ element.setPointerCapture?.(event.pointerId)
325
+ propsRef.current.onResponderStart?.(responderEvent(event))
326
+ event.stopPropagation()
327
+ return
328
+ }
329
+ if (negotiate(propsRef.current.onStartShouldSetResponderCapture, event)) event.stopPropagation()
330
+ }
331
+ const onPointerMove: PointerEventHandler<T> = (event) => {
332
+ trackPointer(event, 'move')
333
+ const element = elementRef.current
334
+ const incumbent = activeResponder
335
+ if (
336
+ element &&
337
+ incumbent &&
338
+ incumbent.element === element &&
339
+ incumbent.pointerIds.has(event.pointerId)
340
+ ) {
341
+ propsRef.current.onResponderMove?.(responderEvent(event))
342
+ } else {
343
+ if (negotiate(propsRef.current.onMoveShouldSetResponder, event)) event.stopPropagation()
344
+ }
345
+ }
346
+ const onPointerMoveCapture: PointerEventHandler<T> = (event) => {
347
+ trackPointer(event, 'move')
348
+ if (negotiate(propsRef.current.onMoveShouldSetResponderCapture, event)) event.stopPropagation()
349
+ }
350
+ const onPointerUp: PointerEventHandler<T> = (event) => {
351
+ trackPointer(event, 'end')
352
+ finish(event.currentTarget, event, false)
353
+ }
354
+ const onPointerCancel: PointerEventHandler<T> = (event) => {
355
+ trackPointer(event, 'end')
356
+ finish(event.currentTarget, event, true)
357
+ }
358
+ const onLostPointerCapture: PointerEventHandler<T> = (event) => {
359
+ trackPointer(event, 'end')
360
+ finish(event.currentTarget, event, true)
361
+ }
362
+
363
+ return {
364
+ onPointerDown,
365
+ onPointerDownCapture,
366
+ onPointerMove,
367
+ onPointerMoveCapture,
368
+ onPointerUp,
369
+ onPointerCancel,
370
+ onLostPointerCapture,
371
+ }
372
+ }
@@ -0,0 +1,87 @@
1
+ // The insets a notch, a home indicator and a status bar take out of the
2
+ // window, read from the package every React Native app already has.
3
+ //
4
+ // `pt-[env(safe-area-inset-top)]` is a class Tailwind itself produces, and
5
+ // on Web it needs nothing from Hozo: the browser resolves `env()`. On React
6
+ // Native there is no cascade to resolve it in, and -- this is the part that
7
+ // decided the design -- no way to read the real number from React Native's
8
+ // core either. Core exposes a deprecated iOS-only `SafeAreaView`, which
9
+ // applies padding rather than reporting a value, and an Android-only
10
+ // `StatusBar.currentHeight`, which is the status bar and not the cutout.
11
+ // `safeAreaInsets` appears nowhere in its JavaScript.
12
+ //
13
+ // So the value needs native code: `UIView.safeAreaInsets` on iOS,
14
+ // `WindowInsets` on Android. Hozo could ship that itself, and chose not to.
15
+ // `@hozo/*` is a compiler and JavaScript today, which means a project can
16
+ // add it without a rebuild, without autolinking, and without leaving Expo
17
+ // Go; shipping a native module to avoid one dependency would trade that
18
+ // away for every user in order to save one `pnpm add` for some of them.
19
+ //
20
+ // `react-native-safe-area-context` is that dependency, and it is not an
21
+ // obscure one: `@react-navigation` pulls it in, so most applications with
22
+ // more than one screen already have it.
23
+
24
+ /** The four insets, in the shape the compiled style reads them. */
25
+ export interface HozoSafeAreaInsets {
26
+ top: number
27
+ right: number
28
+ bottom: number
29
+ left: number
30
+ }
31
+
32
+ type InsetsHook = () => HozoSafeAreaInsets
33
+
34
+ const NONE: HozoSafeAreaInsets = { top: 0, right: 0, bottom: 0, left: 0 }
35
+
36
+ /**
37
+ * The package's hook, or a hook-shaped constant when it is not installed.
38
+ *
39
+ * Resolved at module load, which is both the simple answer and the correct
40
+ * one. Simple, because a hook chosen once is a hook called unconditionally
41
+ * afterwards -- selecting per render would be calling a hook inside a
42
+ * branch, which is the rule React does not bend even when the branch is
43
+ * decided by the bundle and never changes.
44
+ *
45
+ * Correct, because this module is only loaded when the compiler emitted a
46
+ * safe-area class. A project that writes none never imports it, never
47
+ * resolves anything, and never sees the warning; a project that writes one
48
+ * sees it exactly once, at startup, when there is still time to act.
49
+ */
50
+ function resolveInsetsHook(): InsetsHook {
51
+ try {
52
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
53
+ const module = require('react-native-safe-area-context') as {
54
+ useSafeAreaInsets?: InsetsHook
55
+ }
56
+ if (typeof module.useSafeAreaInsets === 'function') return module.useSafeAreaInsets
57
+ } catch {
58
+ // Not installed. Fall through to the warning: an optional dependency
59
+ // that throws on absence is not optional.
60
+ }
61
+ console.warn(
62
+ '[hozo] a safe-area class needs `react-native-safe-area-context`, which is not installed. ' +
63
+ 'Insets are zero until it is: add it, then wrap the app in its `<SafeAreaProvider>`. ' +
64
+ 'Hozo ships no native code of its own, and the real inset is `UIView.safeAreaInsets` on ' +
65
+ 'iOS and `WindowInsets` on Android -- neither of which React Native exposes to JavaScript.',
66
+ )
67
+ // Zeros rather than a throw, because of what each costs: a missing inset
68
+ // draws content under a notch, which is ugly and recoverable, and a
69
+ // thrown error is a blank app. The warning is what makes the ugly one
70
+ // findable.
71
+ return () => NONE
72
+ }
73
+
74
+ const useInsets = resolveInsetsHook()
75
+
76
+ /**
77
+ * The current safe-area insets. Drives `pt-[env(safe-area-inset-top)]` and
78
+ * the other three sides, the way `useHozoViewport` drives `h-screen`.
79
+ *
80
+ * A wrapper with no logic in it is the point: the number belongs to the
81
+ * platform, this package's job is to make one spelling reach both, and
82
+ * anything clever here would be Hozo having an opinion about a value it
83
+ * cannot measure.
84
+ */
85
+ export function useHozoSafeArea(): HozoSafeAreaInsets {
86
+ return useInsets()
87
+ }
@@ -0,0 +1,119 @@
1
+ // Keyboard access for a scroll container, decided where the answer exists.
2
+ //
3
+ // A `div` with `overflow: auto` scrolls under a pointer and is unreachable
4
+ // from a keyboard unless it is focusable itself or holds something that
5
+ // is. React Native's `ScrollView` is scrolled by touch and the platform
6
+ // handles reaching it, so the gap opens on Web only -- see #99.
7
+ //
8
+ // Not decided at compile time, because none of it is a compile-time fact.
9
+ // axe's rule, which is the one browsers and auditors agree on, is:
10
+ //
11
+ // matches scrollWidth > clientWidth + 13
12
+ // or scrollHeight > clientHeight + 13
13
+ // on an axis that actually scrolls
14
+ // any focusable-content OR focusable-element
15
+ //
16
+ // Overflow depends on CSS, content and viewport; whether a subtree holds
17
+ // anything focusable depends on what a runtime expression rendered. A
18
+ // compiler emitting `tabIndex={0}` for every `ScrollView` would add a tab
19
+ // stop to every list that happens to fit, and one emitting a diagnostic
20
+ // would warn about containers that will never overflow. Both teach people
21
+ // to ignore them.
22
+ //
23
+ // So this mirrors the same three conditions at the moment they are true,
24
+ // and re-checks when any of them can have changed.
25
+
26
+ /**
27
+ * The attribute marking a tab stop this file added.
28
+ *
29
+ * Load-bearing in two places: nothing removes a stop it did not add, and
30
+ * nothing reads its own stop as proof that one was already there.
31
+ */
32
+ const MARK = 'data-hozo-scroll-stop'
33
+
34
+ /** What a browser gives focus to without being asked. */
35
+ const FOCUSABLE =
36
+ 'a[href], button, input, select, textarea, summary, iframe, audio[controls],' +
37
+ ' video[controls], [contenteditable], [tabindex]'
38
+
39
+ /**
40
+ * Whether the element scrolls *and* has nothing focusable to reach.
41
+ *
42
+ * The 13-pixel allowance is axe's, not a guess: a container can exceed its
43
+ * content box by a rounding error or a scrollbar's own width without being
44
+ * scrollable in any useful sense.
45
+ */
46
+ function needsTabStop(element: HTMLElement): boolean {
47
+ const style = getComputedStyle(element)
48
+ const scrollsX =
49
+ element.scrollWidth > element.clientWidth + 13 &&
50
+ (style.overflowX === 'auto' || style.overflowX === 'scroll')
51
+ const scrollsY =
52
+ element.scrollHeight > element.clientHeight + 13 &&
53
+ (style.overflowY === 'auto' || style.overflowY === 'scroll')
54
+ if (!scrollsX && !scrollsY) return false
55
+
56
+ // `focusable-element`, but only when the stop is somebody else's. A
57
+ // tab stop this function added is not evidence that one was already
58
+ // needed, and reading it as such made the answer oscillate: the first
59
+ // call added the attribute, the observer's initial callback saw it and
60
+ // took it straight back off. Two calls, decisions `true` then `false`,
61
+ // and nothing left on the element.
62
+ if (element.hasAttribute('tabindex') && !element.hasAttribute(MARK)) return false
63
+ return element.querySelector(FOCUSABLE) === null
64
+ }
65
+
66
+ /**
67
+ * Props for a compiled `ScrollView`, from `{...hozoScrollable()}`.
68
+ *
69
+ * A ref callback rather than state: this sets one attribute on one element
70
+ * and has no business re-rendering the tree to do it. The observers are
71
+ * torn down when React hands back `null`.
72
+ *
73
+ * Not emitted at all when the author wrote their own `tabIndex` -- that
74
+ * precedence is settled by the compiler, which can see the attribute, so
75
+ * there is no branch here to get wrong. An author saying `tabIndex={-1}`
76
+ * means it, and nothing in this file will argue.
77
+ */
78
+ export function hozoScrollable() {
79
+ let observer: ResizeObserver | undefined
80
+ let mutations: MutationObserver | undefined
81
+
82
+ const apply = (element: HTMLElement) => {
83
+ // `data-hozo-scroll-stop` rather than a bare `tabindex`, so a later
84
+ // read can tell a stop this added from one an author wrote. Without it
85
+ // the removal path could not tell them apart and would take away
86
+ // somebody else's.
87
+ if (needsTabStop(element)) {
88
+ element.setAttribute('tabindex', '0')
89
+ element.setAttribute(MARK, '')
90
+ } else if (element.hasAttribute(MARK)) {
91
+ element.removeAttribute('tabindex')
92
+ element.removeAttribute(MARK)
93
+ }
94
+ }
95
+
96
+ return {
97
+ ref: (element: HTMLElement | null) => {
98
+ observer?.disconnect()
99
+ mutations?.disconnect()
100
+ observer = undefined
101
+ mutations = undefined
102
+ if (!element) return
103
+
104
+ apply(element)
105
+ // Resize covers the viewport changing and the content growing;
106
+ // mutation covers a child arriving that is focusable, which changes
107
+ // the answer without changing any size.
108
+ if (typeof ResizeObserver !== 'undefined') {
109
+ observer = new ResizeObserver(() => apply(element))
110
+ observer.observe(element)
111
+ for (const child of element.children) observer.observe(child)
112
+ }
113
+ if (typeof MutationObserver !== 'undefined') {
114
+ mutations = new MutationObserver(() => apply(element))
115
+ mutations.observe(element, { childList: true, subtree: true })
116
+ }
117
+ },
118
+ }
119
+ }
@@ -0,0 +1,25 @@
1
+ // The base a relative text size scales against.
2
+ //
3
+ // Here rather than beside the components that use it because a React
4
+ // context is not a platform concept, and because `@hozo/typography`
5
+ // reads it: a package resolves this one's types through the Web entry
6
+ // whichever platform it is building for, so a native-only export is one
7
+ // its type checker cannot see.
8
+
9
+ import { createContext, useContext } from 'react'
10
+
11
+ /**
12
+ * React Native's own default, from `RCTFont.mm`:
13
+ * `const CGFloat defaultFontSize = 14`.
14
+ *
15
+ * The base when nothing above has named one, which is what React Native
16
+ * itself would have drawn at.
17
+ */
18
+ export const HOZO_DEFAULT_FONT_SIZE = 14
19
+
20
+ export const HozoTextSizeContext = createContext(HOZO_DEFAULT_FONT_SIZE)
21
+
22
+ /** The size in force here, for a component sizing itself against it. */
23
+ export function useHozoTextSize() {
24
+ return useContext(HozoTextSizeContext)
25
+ }