@hozo/primitives 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 (218) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +54 -0
  3. package/dist/flat-list.native.d.ts +17 -0
  4. package/dist/flat-list.native.d.ts.map +1 -0
  5. package/dist/flat-list.native.js +89 -0
  6. package/dist/flat-list.native.js.map +1 -0
  7. package/dist/foundation.d.ts +104 -0
  8. package/dist/foundation.d.ts.map +1 -0
  9. package/dist/foundation.js +115 -0
  10. package/dist/foundation.js.map +1 -0
  11. package/dist/foundation.native.d.ts +59 -0
  12. package/dist/foundation.native.d.ts.map +1 -0
  13. package/dist/foundation.native.js +40 -0
  14. package/dist/foundation.native.js.map +1 -0
  15. package/dist/generated/animated.d.ts +2 -0
  16. package/dist/generated/animated.d.ts.map +1 -0
  17. package/dist/generated/animated.js +3 -0
  18. package/dist/generated/animated.js.map +1 -0
  19. package/dist/generated/backdrop-filter.d.ts +2 -0
  20. package/dist/generated/backdrop-filter.d.ts.map +1 -0
  21. package/dist/generated/backdrop-filter.js +3 -0
  22. package/dist/generated/backdrop-filter.js.map +1 -0
  23. package/dist/generated/container.d.ts +2 -0
  24. package/dist/generated/container.d.ts.map +1 -0
  25. package/dist/generated/container.js +3 -0
  26. package/dist/generated/container.js.map +1 -0
  27. package/dist/generated/flat-list.d.ts +2 -0
  28. package/dist/generated/flat-list.d.ts.map +1 -0
  29. package/dist/generated/flat-list.js +2 -0
  30. package/dist/generated/flat-list.js.map +1 -0
  31. package/dist/generated/flat-list.native.d.ts +3 -0
  32. package/dist/generated/flat-list.native.d.ts.map +1 -0
  33. package/dist/generated/flat-list.native.js +2 -0
  34. package/dist/generated/flat-list.native.js.map +1 -0
  35. package/dist/generated/grid.d.ts +3 -0
  36. package/dist/generated/grid.d.ts.map +1 -0
  37. package/dist/generated/grid.js +4 -0
  38. package/dist/generated/grid.js.map +1 -0
  39. package/dist/generated/link.d.ts +2 -0
  40. package/dist/generated/link.d.ts.map +1 -0
  41. package/dist/generated/link.js +2 -0
  42. package/dist/generated/link.js.map +1 -0
  43. package/dist/generated/link.native.d.ts +2 -0
  44. package/dist/generated/link.native.d.ts.map +1 -0
  45. package/dist/generated/link.native.js +2 -0
  46. package/dist/generated/link.native.js.map +1 -0
  47. package/dist/generated/pressable.d.ts +2 -0
  48. package/dist/generated/pressable.d.ts.map +1 -0
  49. package/dist/generated/pressable.js +2 -0
  50. package/dist/generated/pressable.js.map +1 -0
  51. package/dist/generated/pressable.native.d.ts +2 -0
  52. package/dist/generated/pressable.native.d.ts.map +1 -0
  53. package/dist/generated/pressable.native.js +2 -0
  54. package/dist/generated/pressable.native.js.map +1 -0
  55. package/dist/generated/refresh-control.d.ts +2 -0
  56. package/dist/generated/refresh-control.d.ts.map +1 -0
  57. package/dist/generated/refresh-control.js +2 -0
  58. package/dist/generated/refresh-control.js.map +1 -0
  59. package/dist/generated/refresh-control.native.d.ts +2 -0
  60. package/dist/generated/refresh-control.native.d.ts.map +1 -0
  61. package/dist/generated/refresh-control.native.js +2 -0
  62. package/dist/generated/refresh-control.native.js.map +1 -0
  63. package/dist/generated/scroll-view.d.ts +2 -0
  64. package/dist/generated/scroll-view.d.ts.map +1 -0
  65. package/dist/generated/scroll-view.js +2 -0
  66. package/dist/generated/scroll-view.js.map +1 -0
  67. package/dist/generated/scroll-view.native.d.ts +2 -0
  68. package/dist/generated/scroll-view.native.d.ts.map +1 -0
  69. package/dist/generated/scroll-view.native.js +2 -0
  70. package/dist/generated/scroll-view.native.js.map +1 -0
  71. package/dist/generated/spaced.d.ts +2 -0
  72. package/dist/generated/spaced.d.ts.map +1 -0
  73. package/dist/generated/spaced.js +3 -0
  74. package/dist/generated/spaced.js.map +1 -0
  75. package/dist/generated/text-input.d.ts +2 -0
  76. package/dist/generated/text-input.d.ts.map +1 -0
  77. package/dist/generated/text-input.js +2 -0
  78. package/dist/generated/text-input.js.map +1 -0
  79. package/dist/generated/text-input.native.d.ts +2 -0
  80. package/dist/generated/text-input.native.d.ts.map +1 -0
  81. package/dist/generated/text-input.native.js +2 -0
  82. package/dist/generated/text-input.native.js.map +1 -0
  83. package/dist/generated/text-size.d.ts +2 -0
  84. package/dist/generated/text-size.d.ts.map +1 -0
  85. package/dist/generated/text-size.js +3 -0
  86. package/dist/generated/text-size.js.map +1 -0
  87. package/dist/generated/text.d.ts +2 -0
  88. package/dist/generated/text.d.ts.map +1 -0
  89. package/dist/generated/text.js +5 -0
  90. package/dist/generated/text.js.map +1 -0
  91. package/dist/generated/view.d.ts +2 -0
  92. package/dist/generated/view.d.ts.map +1 -0
  93. package/dist/generated/view.js +2 -0
  94. package/dist/generated/view.js.map +1 -0
  95. package/dist/generated/view.native.d.ts +2 -0
  96. package/dist/generated/view.native.d.ts.map +1 -0
  97. package/dist/generated/view.native.js +2 -0
  98. package/dist/generated/view.native.js.map +1 -0
  99. package/dist/index.d.ts +188 -0
  100. package/dist/index.d.ts.map +1 -0
  101. package/dist/index.js +718 -0
  102. package/dist/index.js.map +1 -0
  103. package/dist/index.native.d.ts +22 -0
  104. package/dist/index.native.d.ts.map +1 -0
  105. package/dist/index.native.js +8 -0
  106. package/dist/index.native.js.map +1 -0
  107. package/dist/link.d.ts +320 -0
  108. package/dist/link.d.ts.map +1 -0
  109. package/dist/link.js +69 -0
  110. package/dist/link.js.map +1 -0
  111. package/dist/link.native.d.ts +33 -0
  112. package/dist/link.native.d.ts.map +1 -0
  113. package/dist/link.native.js +44 -0
  114. package/dist/link.native.js.map +1 -0
  115. package/dist/pressable.d.ts +65 -0
  116. package/dist/pressable.d.ts.map +1 -0
  117. package/dist/pressable.js +202 -0
  118. package/dist/pressable.js.map +1 -0
  119. package/dist/pressable.native.d.ts +55 -0
  120. package/dist/pressable.native.d.ts.map +1 -0
  121. package/dist/pressable.native.js +405 -0
  122. package/dist/pressable.native.js.map +1 -0
  123. package/dist/runtime-backdrop.native.d.ts +8 -0
  124. package/dist/runtime-backdrop.native.d.ts.map +1 -0
  125. package/dist/runtime-backdrop.native.js +12 -0
  126. package/dist/runtime-backdrop.native.js.map +1 -0
  127. package/dist/runtime-container.native.d.ts +41 -0
  128. package/dist/runtime-container.native.d.ts.map +1 -0
  129. package/dist/runtime-container.native.js +61 -0
  130. package/dist/runtime-container.native.js.map +1 -0
  131. package/dist/runtime-grid.d.ts +37 -0
  132. package/dist/runtime-grid.d.ts.map +1 -0
  133. package/dist/runtime-grid.js +139 -0
  134. package/dist/runtime-grid.js.map +1 -0
  135. package/dist/runtime-grid.native.d.ts +26 -0
  136. package/dist/runtime-grid.native.d.ts.map +1 -0
  137. package/dist/runtime-grid.native.js +76 -0
  138. package/dist/runtime-grid.native.js.map +1 -0
  139. package/dist/runtime-measured.d.ts +20 -0
  140. package/dist/runtime-measured.d.ts.map +1 -0
  141. package/dist/runtime-measured.js +42 -0
  142. package/dist/runtime-measured.js.map +1 -0
  143. package/dist/runtime-spacing.d.ts +9 -0
  144. package/dist/runtime-spacing.d.ts.map +1 -0
  145. package/dist/runtime-spacing.js +35 -0
  146. package/dist/runtime-spacing.js.map +1 -0
  147. package/dist/runtime-spacing.native.d.ts +12 -0
  148. package/dist/runtime-spacing.native.d.ts.map +1 -0
  149. package/dist/runtime-spacing.native.js +64 -0
  150. package/dist/runtime-spacing.native.js.map +1 -0
  151. package/dist/runtime-text-size.native.d.ts +29 -0
  152. package/dist/runtime-text-size.native.d.ts.map +1 -0
  153. package/dist/runtime-text-size.native.js +51 -0
  154. package/dist/runtime-text-size.native.js.map +1 -0
  155. package/dist/runtime-transition.native.d.ts +15 -0
  156. package/dist/runtime-transition.native.d.ts.map +1 -0
  157. package/dist/runtime-transition.native.js +156 -0
  158. package/dist/runtime-transition.native.js.map +1 -0
  159. package/dist/text-input.d.ts +40 -0
  160. package/dist/text-input.d.ts.map +1 -0
  161. package/dist/text-input.js +54 -0
  162. package/dist/text-input.js.map +1 -0
  163. package/dist/view.d.ts +42 -0
  164. package/dist/view.d.ts.map +1 -0
  165. package/dist/view.js +84 -0
  166. package/dist/view.js.map +1 -0
  167. package/dist/view.native.d.ts +8 -0
  168. package/dist/view.native.d.ts.map +1 -0
  169. package/dist/view.native.js +5 -0
  170. package/dist/view.native.js.map +1 -0
  171. package/dist/windowing.d.ts +149 -0
  172. package/dist/windowing.d.ts.map +1 -0
  173. package/dist/windowing.js +336 -0
  174. package/dist/windowing.js.map +1 -0
  175. package/package.json +160 -0
  176. package/src/flat-list.native.tsx +158 -0
  177. package/src/foundation.native.tsx +179 -0
  178. package/src/foundation.tsx +323 -0
  179. package/src/generated/animated.ts +6 -0
  180. package/src/generated/backdrop-filter.ts +5 -0
  181. package/src/generated/container.ts +7 -0
  182. package/src/generated/flat-list.native.ts +6 -0
  183. package/src/generated/flat-list.ts +7 -0
  184. package/src/generated/grid.ts +4 -0
  185. package/src/generated/link.native.ts +1 -0
  186. package/src/generated/link.ts +1 -0
  187. package/src/generated/pressable.native.ts +5 -0
  188. package/src/generated/pressable.ts +5 -0
  189. package/src/generated/refresh-control.native.ts +4 -0
  190. package/src/generated/refresh-control.ts +1 -0
  191. package/src/generated/scroll-view.native.ts +4 -0
  192. package/src/generated/scroll-view.ts +1 -0
  193. package/src/generated/spaced.ts +2 -0
  194. package/src/generated/text-input.native.ts +4 -0
  195. package/src/generated/text-input.ts +1 -0
  196. package/src/generated/text-size.ts +7 -0
  197. package/src/generated/text.ts +8 -0
  198. package/src/generated/view.native.ts +1 -0
  199. package/src/generated/view.ts +1 -0
  200. package/src/index.native.ts +24 -0
  201. package/src/index.ts +1111 -0
  202. package/src/link.native.tsx +77 -0
  203. package/src/link.ts +125 -0
  204. package/src/pressable.native.tsx +538 -0
  205. package/src/pressable.ts +358 -0
  206. package/src/runtime-backdrop.native.tsx +22 -0
  207. package/src/runtime-container.native.tsx +105 -0
  208. package/src/runtime-grid.native.tsx +143 -0
  209. package/src/runtime-grid.ts +198 -0
  210. package/src/runtime-measured.ts +42 -0
  211. package/src/runtime-spacing.native.tsx +75 -0
  212. package/src/runtime-spacing.ts +37 -0
  213. package/src/runtime-text-size.native.tsx +79 -0
  214. package/src/runtime-transition.native.tsx +183 -0
  215. package/src/text-input.ts +129 -0
  216. package/src/view.native.ts +7 -0
  217. package/src/view.ts +165 -0
  218. package/src/windowing.ts +415 -0
@@ -0,0 +1,358 @@
1
+ import {
2
+ type HozoDomStyle,
3
+ hozoDomStyle,
4
+ hozoInteractive,
5
+ type ResponderProps,
6
+ useResponderDomProps,
7
+ } from '@hozo/engine'
8
+ import {
9
+ createElement,
10
+ forwardRef,
11
+ type HTMLAttributes,
12
+ type KeyboardEvent,
13
+ type MouseEvent,
14
+ type PointerEvent,
15
+ type ReactNode,
16
+ type Ref,
17
+ useCallback,
18
+ useEffect,
19
+ useRef,
20
+ useState,
21
+ } from 'react'
22
+ import { HozoLink, type HozoLinkProps } from './link.ts'
23
+ import type { HozoLayoutEvent } from './view.ts'
24
+
25
+ export interface HozoPressableState {
26
+ pressed: boolean
27
+ hovered: boolean
28
+ focused: boolean
29
+ }
30
+
31
+ type DivProps = Omit<
32
+ HTMLAttributes<HTMLDivElement>,
33
+ | 'children'
34
+ | 'onBlur'
35
+ | 'onClick'
36
+ | 'onFocus'
37
+ | 'onPointerCancel'
38
+ | 'onPointerDown'
39
+ | 'onPointerEnter'
40
+ | 'onPointerLeave'
41
+ | 'onPointerUp'
42
+ | 'style'
43
+ >
44
+
45
+ export interface HozoPressableProps extends DivProps, ResponderProps {
46
+ children?: ReactNode | ((state: HozoPressableState) => ReactNode)
47
+ style?: HozoDomStyle | ((state: HozoPressableState) => HozoDomStyle)
48
+ onPress?: (event: MouseEvent<HTMLElement>) => void
49
+ onClick?: (event: MouseEvent<HTMLElement>) => void
50
+ onPressIn?: (event: PointerEvent<HTMLElement> | KeyboardEvent<HTMLElement>) => void
51
+ onPressOut?: (event: PointerEvent<HTMLElement> | KeyboardEvent<HTMLElement>) => void
52
+ onPointerCancel?: (event: PointerEvent<HTMLElement>) => void
53
+ onPointerDown?: (event: PointerEvent<HTMLElement>) => void
54
+ onPointerEnter?: (event: PointerEvent<HTMLElement>) => void
55
+ onPointerLeave?: (event: PointerEvent<HTMLElement>) => void
56
+ onPointerUp?: (event: PointerEvent<HTMLElement>) => void
57
+ onHoverIn?: (event: PointerEvent<HTMLElement>) => void
58
+ onHoverOut?: (event: PointerEvent<HTMLElement>) => void
59
+ onFocus?: (event: React.FocusEvent<HTMLElement>) => void
60
+ onBlur?: (event: React.FocusEvent<HTMLElement>) => void
61
+ disabled?: boolean
62
+ accessibilityRole?: string
63
+ accessibilityLabel?: string
64
+ accessibilityHint?: string
65
+ accessibilityState?: {
66
+ disabled?: boolean
67
+ selected?: boolean
68
+ checked?: boolean | 'mixed'
69
+ busy?: boolean
70
+ expanded?: boolean
71
+ }
72
+ accessibilityValue?: { min?: number; max?: number; now?: number; text?: string }
73
+ accessibilityLiveRegion?: 'none' | 'polite' | 'assertive'
74
+ testID?: string
75
+ nativeID?: string
76
+ pointerEvents?: 'auto' | 'none' | 'box-none' | 'box-only'
77
+ onLayout?: (event: HozoLayoutEvent) => void
78
+ dataSet?: Record<string, string | number | boolean | undefined>
79
+ href?: string
80
+ external?: boolean
81
+ replace?: boolean
82
+ prefetch?: boolean
83
+ target?: string
84
+ rel?: string
85
+ download?: boolean | string
86
+ accessible?: boolean
87
+ accessibilityActions?: readonly unknown[]
88
+ onAccessibilityAction?: (event: unknown) => void
89
+ android_ripple?: unknown
90
+ hitSlop?: unknown
91
+ }
92
+
93
+ function assignRef<T>(ref: Ref<T> | undefined, value: T | null) {
94
+ if (typeof ref === 'function') ref(value)
95
+ else if (ref) ref.current = value
96
+ }
97
+
98
+ function dataAttributes(dataSet: HozoPressableProps['dataSet']) {
99
+ return Object.fromEntries(
100
+ Object.entries(dataSet ?? {}).map(([key, value]) => [`data-${key}`, value]),
101
+ )
102
+ }
103
+
104
+ /** A value-level Pressable for component props and animation wrappers. */
105
+ export const HozoPressable = forwardRef<HTMLElement, HozoPressableProps>(function HozoPressable(
106
+ {
107
+ accessibilityActions: _accessibilityActions,
108
+ accessibilityHint,
109
+ accessibilityLabel,
110
+ accessibilityLiveRegion,
111
+ accessibilityRole,
112
+ accessibilityState,
113
+ accessibilityValue,
114
+ accessible: _accessible,
115
+ android_ripple: _androidRipple,
116
+ children,
117
+ dataSet,
118
+ disabled,
119
+ download,
120
+ external,
121
+ hitSlop: _hitSlop,
122
+ href,
123
+ nativeID,
124
+ onAccessibilityAction: _onAccessibilityAction,
125
+ onBlur,
126
+ onClick,
127
+ onFocus,
128
+ onHoverIn,
129
+ onHoverOut,
130
+ onPointerCancel,
131
+ onPointerDown,
132
+ onPointerEnter,
133
+ onPointerLeave,
134
+ onPointerUp,
135
+ onPress,
136
+ onPressIn,
137
+ onPressOut,
138
+ onKeyDown,
139
+ onKeyUp,
140
+ onLostPointerCapture,
141
+ onLayout,
142
+ onMoveShouldSetResponder,
143
+ onMoveShouldSetResponderCapture,
144
+ onResponderEnd,
145
+ onResponderGrant,
146
+ onResponderMove,
147
+ onResponderReject,
148
+ onResponderRelease,
149
+ onResponderStart,
150
+ onResponderTerminate,
151
+ onResponderTerminationRequest,
152
+ onStartShouldSetResponder,
153
+ onStartShouldSetResponderCapture,
154
+ pointerEvents,
155
+ prefetch,
156
+ rel,
157
+ replace,
158
+ role,
159
+ style,
160
+ target,
161
+ testID,
162
+ ...props
163
+ },
164
+ forwardedRef,
165
+ ) {
166
+ const elementRef = useRef<HTMLElement>(null)
167
+ const layoutCallback = useRef(onLayout)
168
+ layoutCallback.current = onLayout
169
+ const pressedRef = useRef(false)
170
+ const [state, setState] = useState<HozoPressableState>({
171
+ pressed: false,
172
+ hovered: false,
173
+ focused: false,
174
+ })
175
+ const unavailable = disabled || accessibilityState?.disabled === true
176
+ const setRef = useCallback(
177
+ (element: HTMLElement | null) => {
178
+ elementRef.current = element
179
+ assignRef(forwardedRef, element)
180
+ },
181
+ [forwardedRef],
182
+ )
183
+ const responder = useResponderDomProps(
184
+ elementRef,
185
+ {
186
+ onMoveShouldSetResponder,
187
+ onMoveShouldSetResponderCapture,
188
+ onResponderEnd,
189
+ onResponderGrant,
190
+ onResponderMove,
191
+ onResponderReject,
192
+ onResponderRelease,
193
+ onResponderStart,
194
+ onResponderTerminate,
195
+ onResponderTerminationRequest,
196
+ onStartShouldSetResponder,
197
+ onStartShouldSetResponderCapture,
198
+ },
199
+ !unavailable,
200
+ )
201
+ useEffect(() => {
202
+ const element = elementRef.current
203
+ if (!element || !layoutCallback.current) return
204
+ let previous = ''
205
+ const emit = () => {
206
+ const rect = element.getBoundingClientRect()
207
+ const layout = {
208
+ x: element.offsetLeft,
209
+ y: element.offsetTop,
210
+ width: rect.width,
211
+ height: rect.height,
212
+ }
213
+ const key = `${layout.x}:${layout.y}:${layout.width}:${layout.height}`
214
+ if (key === previous) return
215
+ previous = key
216
+ layoutCallback.current?.({ nativeEvent: { layout } })
217
+ }
218
+ emit()
219
+ if (typeof ResizeObserver === 'undefined') return
220
+ const observer = new ResizeObserver(emit)
221
+ observer.observe(element)
222
+ return () => observer.disconnect()
223
+ }, [])
224
+ const activate =
225
+ onClick || onPress
226
+ ? (event: MouseEvent<HTMLElement>) => {
227
+ onClick?.(event)
228
+ onPress?.(event)
229
+ }
230
+ : undefined
231
+ const interaction = activate
232
+ ? hozoInteractive(activate as never, unavailable)
233
+ : {
234
+ 'aria-disabled': unavailable || undefined,
235
+ 'data-hozo-disabled': unavailable ? '' : undefined,
236
+ tabIndex: undefined,
237
+ onClick: undefined,
238
+ onKeyDown: undefined,
239
+ onKeyUp: undefined,
240
+ }
241
+ const pressIn = (event: PointerEvent<HTMLElement> | KeyboardEvent<HTMLElement>) => {
242
+ if (unavailable || pressedRef.current) return
243
+ pressedRef.current = true
244
+ setState((current) => ({ ...current, pressed: true }))
245
+ onPressIn?.(event)
246
+ }
247
+ const pressOut = (event: PointerEvent<HTMLElement> | KeyboardEvent<HTMLElement>) => {
248
+ if (!pressedRef.current) return
249
+ pressedRef.current = false
250
+ setState((current) => ({ ...current, pressed: false }))
251
+ onPressOut?.(event)
252
+ }
253
+ const resolvedChildren = typeof children === 'function' ? children(state) : children
254
+ const resolvedStyle = hozoDomStyle(typeof style === 'function' ? style(state) : style)
255
+ const value = accessibilityValue
256
+ const common = {
257
+ ...props,
258
+ ...dataAttributes(dataSet),
259
+ ref: setRef,
260
+ role: accessibilityRole ?? role,
261
+ style: resolvedStyle,
262
+ id: nativeID,
263
+ 'data-testid': testID,
264
+ 'data-hozo-pointer-events': pointerEvents,
265
+ 'aria-label': accessibilityLabel,
266
+ 'aria-description': accessibilityHint,
267
+ 'aria-selected': accessibilityState?.selected,
268
+ 'aria-checked': accessibilityState?.checked,
269
+ 'aria-busy': accessibilityState?.busy,
270
+ 'aria-expanded': accessibilityState?.expanded,
271
+ 'aria-valuemin': value?.min,
272
+ 'aria-valuemax': value?.max,
273
+ 'aria-valuenow': value?.now,
274
+ 'aria-valuetext': value?.text,
275
+ 'aria-live': accessibilityLiveRegion === 'none' ? undefined : accessibilityLiveRegion,
276
+ ...responder,
277
+ onPointerDown: (event: PointerEvent<HTMLElement>) => {
278
+ if (event.isPrimary) pressIn(event)
279
+ responder.onPointerDown?.(event)
280
+ onPointerDown?.(event as never)
281
+ },
282
+ onPointerUp: (event: PointerEvent<HTMLElement>) => {
283
+ pressOut(event)
284
+ responder.onPointerUp?.(event)
285
+ onPointerUp?.(event as never)
286
+ },
287
+ onPointerCancel: (event: PointerEvent<HTMLElement>) => {
288
+ pressOut(event)
289
+ responder.onPointerCancel?.(event)
290
+ onPointerCancel?.(event as never)
291
+ },
292
+ onPointerEnter: (event: PointerEvent<HTMLElement>) => {
293
+ if (!unavailable) setState((current) => ({ ...current, hovered: true }))
294
+ onHoverIn?.(event)
295
+ onPointerEnter?.(event as never)
296
+ },
297
+ onPointerLeave: (event: PointerEvent<HTMLElement>) => {
298
+ pressOut(event)
299
+ setState((current) => ({ ...current, hovered: false }))
300
+ onHoverOut?.(event)
301
+ onPointerLeave?.(event as never)
302
+ },
303
+ onFocus: (event: React.FocusEvent<HTMLElement>) => {
304
+ if (!unavailable) setState((current) => ({ ...current, focused: true }))
305
+ onFocus?.(event)
306
+ },
307
+ onBlur: (event: React.FocusEvent<HTMLElement>) => {
308
+ pressOut(event as unknown as KeyboardEvent<HTMLElement>)
309
+ setState((current) => ({ ...current, focused: false }))
310
+ onBlur?.(event)
311
+ },
312
+ onLostPointerCapture: (event: PointerEvent<HTMLElement>) => {
313
+ pressOut(event)
314
+ responder.onLostPointerCapture?.(event)
315
+ onLostPointerCapture?.(event as never)
316
+ },
317
+ }
318
+ if (href != null) {
319
+ return createElement(
320
+ HozoLink,
321
+ {
322
+ ...common,
323
+ ref: setRef as Ref<HTMLAnchorElement>,
324
+ href,
325
+ external,
326
+ replace,
327
+ prefetch,
328
+ target,
329
+ rel,
330
+ download,
331
+ disabled: unavailable,
332
+ accessibilityRole,
333
+ onPress: unavailable ? undefined : (activate as never),
334
+ onKeyDown,
335
+ onKeyUp,
336
+ } as unknown as HozoLinkProps,
337
+ resolvedChildren,
338
+ )
339
+ }
340
+ return createElement(
341
+ 'div',
342
+ {
343
+ ...common,
344
+ ...interaction,
345
+ onKeyDown: (event: KeyboardEvent<HTMLElement>) => {
346
+ if (event.key === 'Enter' || event.key === ' ') pressIn(event)
347
+ interaction.onKeyDown?.(event)
348
+ onKeyDown?.(event as never)
349
+ },
350
+ onKeyUp: (event: KeyboardEvent<HTMLElement>) => {
351
+ if (event.key === 'Enter' || event.key === ' ') pressOut(event)
352
+ interaction.onKeyUp?.(event)
353
+ onKeyUp?.(event as never)
354
+ },
355
+ } as unknown as HTMLAttributes<HTMLDivElement>,
356
+ resolvedChildren,
357
+ )
358
+ })
@@ -0,0 +1,22 @@
1
+ import { hozoBackdropFilterAdapter } from '@hozo/engine'
2
+ import { createElement, type ReactElement } from 'react'
3
+ import type { ViewProps } from 'react-native'
4
+
5
+ export interface HozoBackdropFilterProps extends ViewProps {
6
+ hozoBlurRadius: number
7
+ }
8
+
9
+ /** Native component boundary for the opt-in backdrop-filter adapter. */
10
+ export function HozoBackdropFilter({
11
+ hozoBlurRadius,
12
+ ...props
13
+ }: HozoBackdropFilterProps): ReactElement {
14
+ const Adapter = hozoBackdropFilterAdapter()
15
+ if (!Adapter) {
16
+ throw new Error(
17
+ '[hozo] backdropFilter needs a Native adapter. Configure one with ' +
18
+ 'configureHozoBackdropFilter(createExpoBlurAdapter(BlurView, options)).',
19
+ )
20
+ }
21
+ return createElement(Adapter, { ...props, blurRadius: hozoBlurRadius })
22
+ }
@@ -0,0 +1,105 @@
1
+ // Container queries on React Native, which has none.
2
+ //
3
+ // The width a container query asks about is the one thing the runtime
4
+ // cannot already know. A window has one width and `useHozoViewport`
5
+ // reports it; a container's width is whatever layout gave that particular
6
+ // element, so the element has to measure itself and say.
7
+ //
8
+ // Two pieces, and the split is forced by React rather than chosen. A
9
+ // component cannot read a context it renders the provider for, so the
10
+ // element that *is* the container and the element that *queries* it
11
+ // cannot be hooks in the same function body. `HozoContainer` provides;
12
+ // `HozoContainerQuery` consumes through a render prop, which is the same
13
+ // shape `group-…:` already borrows from Pressable's `style` callback.
14
+ //
15
+ // `HozoContainerQuery` renders no element of its own. It exists to put a
16
+ // component boundary between the provider and the read, and adding a View
17
+ // to do that would change the layout the query is measuring.
18
+
19
+ import { createContext, type ReactNode, useContext, useMemo, useState } from 'react'
20
+ import { type LayoutChangeEvent, View, type ViewProps } from 'react-native'
21
+
22
+ import { keepIfSettled } from './runtime-measured.ts'
23
+
24
+ /**
25
+ * Container widths in scope, by name.
26
+ *
27
+ * The empty key is the nearest container whatever it is called, which is
28
+ * what an unnamed `@sm:` asks about. A named container registers under
29
+ * both, so `@sm:` inside `@container/main` reads the same width as
30
+ * `@sm/main:`.
31
+ *
32
+ * `undefined` for a name means no container in scope answers to it --
33
+ * distinct from a container that measured zero, and the distinction is
34
+ * load-bearing. CSS says a query with no container matches nothing, in
35
+ * *either* direction, so `@max-md:` must not fire just because there is
36
+ * no width to compare.
37
+ */
38
+ export type HozoContainerWidths = Readonly<Record<string, number | undefined>>
39
+
40
+ const HozoContainerContext = createContext<HozoContainerWidths>({})
41
+
42
+ export interface HozoContainerProps extends ViewProps {
43
+ /** `@container/main` -- the name `@sm/main:` addresses. */
44
+ hozoContainerName?: string
45
+ children?: ReactNode
46
+ }
47
+
48
+ /**
49
+ * A View that measures itself and hands its width to its subtree.
50
+ *
51
+ * The compiler renders an element here instead of a View when its classes
52
+ * declared it a container.
53
+ */
54
+ export function HozoContainer({
55
+ hozoContainerName,
56
+ onLayout,
57
+ children,
58
+ ...rest
59
+ }: HozoContainerProps): ReactNode {
60
+ const [width, setWidth] = useState<number | undefined>(undefined)
61
+ const outer = useContext(HozoContainerContext)
62
+
63
+ const widths = useMemo(
64
+ () => ({
65
+ ...outer,
66
+ '': width,
67
+ ...(hozoContainerName ? { [hozoContainerName]: width } : {}),
68
+ }),
69
+ [outer, width, hozoContainerName],
70
+ )
71
+
72
+ // The author's own `onLayout` still runs. Taking the prop and dropping
73
+ // it would be the quiet kind of breakage this compiler exists to avoid.
74
+ const measure = (event: LayoutChangeEvent) => {
75
+ const measured = event.nativeEvent.layout.width
76
+ // A tolerance rather than `===`, for the reason `measured.ts` gives.
77
+ // This one has not been seen looping, and it is the same shape as the
78
+ // one that was: a stored width, a render that depends on it, and a
79
+ // float coming back from Yoga.
80
+ setWidth((current) => keepIfSettled(current, measured))
81
+ onLayout?.(event)
82
+ }
83
+
84
+ return (
85
+ <View {...rest} onLayout={measure}>
86
+ <HozoContainerContext.Provider value={widths}>{children}</HozoContainerContext.Provider>
87
+ </View>
88
+ )
89
+ }
90
+
91
+ /**
92
+ * Hands the widths in scope to the element that queries them.
93
+ *
94
+ * A render prop rather than a hook, because the querying element is
95
+ * usually in the same component as the container and a hook there would
96
+ * read the context from *outside* the provider. This puts a component
97
+ * boundary in the way, and renders nothing else.
98
+ */
99
+ export function HozoContainerQuery({
100
+ children,
101
+ }: {
102
+ children: (widths: HozoContainerWidths) => ReactNode
103
+ }): ReactNode {
104
+ return children(useContext(HozoContainerContext))
105
+ }
@@ -0,0 +1,143 @@
1
+ import { Children, isValidElement, type ReactNode, useState } from 'react'
2
+ import { type LayoutChangeEvent, View } from 'react-native'
3
+ import {
4
+ type GridTrack,
5
+ gridCellStyle,
6
+ gridLayout,
7
+ gridRowSizes,
8
+ gridRows,
9
+ gridTrackSizes,
10
+ } from './runtime-grid.ts'
11
+ import { keepIfSettled } from './runtime-measured.ts'
12
+
13
+ interface Props {
14
+ tracks: readonly GridTrack[]
15
+ rowTracks?: readonly GridTrack[]
16
+ columnGap?: number
17
+ rowGap?: number
18
+ children?: ReactNode
19
+ }
20
+
21
+ /**
22
+ * The first renderer for Hozo's grid solver boundary. It needs no
23
+ * measurement: fixed tracks and fr tracks are solved by one Yoga flex row.
24
+ * Empty cells preserve track widths on the final row.
25
+ */
26
+ export function HozoGrid({
27
+ tracks,
28
+ rowTracks = [],
29
+ columnGap = 0,
30
+ rowGap = 0,
31
+ children,
32
+ }: Props): ReactNode {
33
+ const [width, setWidth] = useState(0)
34
+ const [heights, setHeights] = useState<number[]>([])
35
+ const list = Children.toArray(children)
36
+ const placements = list.map((child) =>
37
+ isValidElement<ItemProps>(child) && child.type === HozoGridItem
38
+ ? {
39
+ span: child.props.columnSpan ?? 1,
40
+ columnStart: child.props.columnStart,
41
+ rowSpan: child.props.rowSpan,
42
+ rowStart: child.props.rowStart,
43
+ }
44
+ : { span: 1 },
45
+ )
46
+ const layout = gridLayout(placements, tracks.length)
47
+ const measured =
48
+ rowTracks.length > 0 ||
49
+ placements.some((item) => (item.rowSpan ?? 1) > 1 || item.rowStart !== undefined)
50
+ if (measured) {
51
+ const columns = gridTrackSizes(tracks, width, columnGap)
52
+ const rows = gridRowSizes(layout, heights, rowGap, rowTracks)
53
+ const offsets = (sizes: readonly number[], gap: number) =>
54
+ sizes.map((_, index) => sizes.slice(0, index).reduce((a, b) => a + b, 0) + index * gap)
55
+ const left = offsets(columns, columnGap)
56
+ const top = offsets(rows, rowGap)
57
+ const totalHeight = rows.reduce((a, b) => a + b, 0) + Math.max(0, rows.length - 1) * rowGap
58
+ const rememberHeight = (child: number) => (event: LayoutChangeEvent) => {
59
+ const height = event.nativeEvent.layout.height
60
+ setHeights((current) => {
61
+ const kept = keepIfSettled(current[child], height)
62
+ return kept === current[child] ? current : Object.assign([...current], { [child]: kept })
63
+ })
64
+ }
65
+ return (
66
+ <View
67
+ style={{ position: 'relative', alignSelf: 'stretch', height: totalHeight }}
68
+ // Guarded, and its sibling above always was. This one was not,
69
+ // and that asymmetry is the whole bug: the container height is
70
+ // derived from the measured heights, so every height change
71
+ // relaid the container out and fired this again. With the same
72
+ // integer width React bails and it stops; with a width that
73
+ // wobbles in the last decimal it never does.
74
+ onLayout={(event) => {
75
+ const next = event.nativeEvent.layout.width
76
+ setWidth((current) => keepIfSettled(current, next))
77
+ }}
78
+ >
79
+ {layout.map((item) => {
80
+ const cellWidth =
81
+ columns.slice(item.column, item.column + item.columnSpan).reduce((a, b) => a + b, 0) +
82
+ (item.columnSpan - 1) * columnGap
83
+ const cellHeight =
84
+ rows.slice(item.row, item.row + item.rowSpan).reduce((a, b) => a + b, 0) +
85
+ (item.rowSpan - 1) * rowGap
86
+ return (
87
+ <View
88
+ key={item.child}
89
+ onLayout={rememberHeight(item.child)}
90
+ style={{
91
+ position: 'absolute',
92
+ left: left[item.column],
93
+ top: top[item.row],
94
+ width: cellWidth,
95
+ ...(cellHeight > 0 ? { height: cellHeight } : {}),
96
+ }}
97
+ >
98
+ {unwrapGridItem(list[item.child])}
99
+ </View>
100
+ )
101
+ })}
102
+ </View>
103
+ )
104
+ }
105
+ const rows = gridRows(placements, tracks)
106
+
107
+ return rows.map((cells, row) => (
108
+ // A grid cell is its coordinates. Rows and columns are generated from the
109
+ // track counts rather than from data, so there is nothing else for one to
110
+ // be, and two cells never trade places.
111
+ // biome-ignore lint/suspicious/noArrayIndexKey: a cell is its coordinates
112
+ <View key={row} style={{ flexDirection: 'row', columnGap }}>
113
+ {cells.map((cell, column) => (
114
+ // A grid cell is its coordinates. Rows and columns are generated from the
115
+ // track counts rather than from data, so there is nothing else for one to
116
+ // be, and two cells never trade places.
117
+ // biome-ignore lint/suspicious/noArrayIndexKey: a cell is its coordinates
118
+ <View key={column} style={gridCellStyle(cell.tracks, columnGap)}>
119
+ {cell.child === null ? null : unwrapGridItem(list[cell.child])}
120
+ </View>
121
+ ))}
122
+ </View>
123
+ ))
124
+ }
125
+
126
+ interface ItemProps {
127
+ columnSpan?: number
128
+ columnStart?: number
129
+ rowSpan?: number
130
+ rowStart?: number
131
+ children?: ReactNode
132
+ }
133
+
134
+ /** Compiler marker consumed by HozoGrid; outside one it is an identity wrapper. */
135
+ export function HozoGridItem({ children }: ItemProps): ReactNode {
136
+ return children
137
+ }
138
+
139
+ function unwrapGridItem(child: ReactNode): ReactNode {
140
+ return isValidElement<ItemProps>(child) && child.type === HozoGridItem
141
+ ? child.props.children
142
+ : child
143
+ }