@hozo/primitives 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/CHANGELOG.md +71 -0
  2. package/README.md +18 -1
  3. package/dist/button-host-attributes.d.ts +20 -0
  4. package/dist/button-host-attributes.d.ts.map +1 -0
  5. package/dist/button-host-attributes.js +47 -0
  6. package/dist/button-host-attributes.js.map +1 -0
  7. package/dist/button-host-props.native.d.ts +8 -0
  8. package/dist/button-host-props.native.d.ts.map +1 -0
  9. package/dist/button-host-props.native.js +20 -0
  10. package/dist/button-host-props.native.js.map +1 -0
  11. package/dist/foundation.d.ts +27 -6
  12. package/dist/foundation.d.ts.map +1 -1
  13. package/dist/foundation.js +72 -19
  14. package/dist/foundation.js.map +1 -1
  15. package/dist/foundation.native.d.ts +27 -7
  16. package/dist/foundation.native.d.ts.map +1 -1
  17. package/dist/foundation.native.js +18 -3
  18. package/dist/foundation.native.js.map +1 -1
  19. package/dist/generated/button-dom-props.d.ts +11 -0
  20. package/dist/generated/button-dom-props.d.ts.map +1 -0
  21. package/dist/generated/button-dom-props.js +132 -0
  22. package/dist/generated/button-dom-props.js.map +1 -0
  23. package/dist/generated/button-props.d.ts +3 -0
  24. package/dist/generated/button-props.d.ts.map +1 -0
  25. package/dist/generated/button-props.js +5 -0
  26. package/dist/generated/button-props.js.map +1 -0
  27. package/dist/generated/button-props.native.d.ts +2 -0
  28. package/dist/generated/button-props.native.d.ts.map +1 -0
  29. package/dist/generated/button-props.native.js +2 -0
  30. package/dist/generated/button-props.native.js.map +1 -0
  31. package/dist/generated/inherited-label.d.ts +2 -0
  32. package/dist/generated/inherited-label.d.ts.map +1 -0
  33. package/dist/generated/inherited-label.js +3 -0
  34. package/dist/generated/inherited-label.js.map +1 -0
  35. package/dist/generated/inherited-pressable.d.ts +2 -0
  36. package/dist/generated/inherited-pressable.d.ts.map +1 -0
  37. package/dist/generated/inherited-pressable.js +3 -0
  38. package/dist/generated/inherited-pressable.js.map +1 -0
  39. package/dist/generated/inherited-relative-text.d.ts +2 -0
  40. package/dist/generated/inherited-relative-text.d.ts.map +1 -0
  41. package/dist/generated/inherited-relative-text.js +3 -0
  42. package/dist/generated/inherited-relative-text.js.map +1 -0
  43. package/dist/generated/inherited-text.d.ts +2 -0
  44. package/dist/generated/inherited-text.d.ts.map +1 -0
  45. package/dist/generated/inherited-text.js +3 -0
  46. package/dist/generated/inherited-text.js.map +1 -0
  47. package/dist/generated/inherited-view.d.ts +2 -0
  48. package/dist/generated/inherited-view.d.ts.map +1 -0
  49. package/dist/generated/inherited-view.js +3 -0
  50. package/dist/generated/inherited-view.js.map +1 -0
  51. package/dist/generated/native-disabled.d.ts +2 -0
  52. package/dist/generated/native-disabled.d.ts.map +1 -0
  53. package/dist/generated/native-disabled.js +2 -0
  54. package/dist/generated/native-disabled.js.map +1 -0
  55. package/dist/generated/text-input-dom-props.d.ts +7 -0
  56. package/dist/generated/text-input-dom-props.d.ts.map +1 -0
  57. package/dist/generated/text-input-dom-props.js +130 -0
  58. package/dist/generated/text-input-dom-props.js.map +1 -0
  59. package/dist/generated/text-input.native.d.ts +1 -1
  60. package/dist/generated/text-input.native.d.ts.map +1 -1
  61. package/dist/generated/text-input.native.js +1 -1
  62. package/dist/generated/text-input.native.js.map +1 -1
  63. package/dist/generated/view-dom-props.d.ts +8 -0
  64. package/dist/generated/view-dom-props.d.ts.map +1 -0
  65. package/dist/generated/view-dom-props.js +241 -0
  66. package/dist/generated/view-dom-props.js.map +1 -0
  67. package/dist/inherited-label.native.d.ts +8 -0
  68. package/dist/inherited-label.native.d.ts.map +1 -0
  69. package/dist/inherited-label.native.js +12 -0
  70. package/dist/inherited-label.native.js.map +1 -0
  71. package/dist/inherited-pressable.native.d.ts +11 -0
  72. package/dist/inherited-pressable.native.d.ts.map +1 -0
  73. package/dist/inherited-pressable.native.js +75 -0
  74. package/dist/inherited-pressable.native.js.map +1 -0
  75. package/dist/inherited-relative-text.native.d.ts +13 -0
  76. package/dist/inherited-relative-text.native.d.ts.map +1 -0
  77. package/dist/inherited-relative-text.native.js +16 -0
  78. package/dist/inherited-relative-text.native.js.map +1 -0
  79. package/dist/inherited-text-scope.d.ts +8 -0
  80. package/dist/inherited-text-scope.d.ts.map +1 -0
  81. package/dist/inherited-text-scope.js +10 -0
  82. package/dist/inherited-text-scope.js.map +1 -0
  83. package/dist/inherited-text-style.native.d.ts +5 -0
  84. package/dist/inherited-text-style.native.d.ts.map +1 -0
  85. package/dist/inherited-text-style.native.js +30 -0
  86. package/dist/inherited-text-style.native.js.map +1 -0
  87. package/dist/inherited-text.native.d.ts +5 -0
  88. package/dist/inherited-text.native.d.ts.map +1 -0
  89. package/dist/inherited-text.native.js +18 -0
  90. package/dist/inherited-text.native.js.map +1 -0
  91. package/dist/inherited-view.native.d.ts +9 -0
  92. package/dist/inherited-view.native.d.ts.map +1 -0
  93. package/dist/inherited-view.native.js +17 -0
  94. package/dist/inherited-view.native.js.map +1 -0
  95. package/dist/link.d.ts +4 -3
  96. package/dist/link.d.ts.map +1 -1
  97. package/dist/link.js +9 -1
  98. package/dist/link.js.map +1 -1
  99. package/dist/link.native.d.ts +16 -7
  100. package/dist/link.native.d.ts.map +1 -1
  101. package/dist/link.native.js +15 -5
  102. package/dist/link.native.js.map +1 -1
  103. package/dist/native-disabled.d.ts +13 -0
  104. package/dist/native-disabled.d.ts.map +1 -0
  105. package/dist/native-disabled.js +13 -0
  106. package/dist/native-disabled.js.map +1 -0
  107. package/dist/presence.native.d.ts.map +1 -1
  108. package/dist/presence.native.js +1 -1
  109. package/dist/presence.native.js.map +1 -1
  110. package/dist/pressable.d.ts +1 -1
  111. package/dist/pressable.d.ts.map +1 -1
  112. package/dist/pressable.js +15 -5
  113. package/dist/pressable.js.map +1 -1
  114. package/dist/pressable.native.d.ts +10 -1
  115. package/dist/pressable.native.d.ts.map +1 -1
  116. package/dist/pressable.native.js +206 -221
  117. package/dist/pressable.native.js.map +1 -1
  118. package/dist/runtime-container.native.d.ts +11 -9
  119. package/dist/runtime-container.native.d.ts.map +1 -1
  120. package/dist/runtime-container.native.js +8 -6
  121. package/dist/runtime-container.native.js.map +1 -1
  122. package/dist/text-color-transition.native.d.ts +12 -0
  123. package/dist/text-color-transition.native.d.ts.map +1 -0
  124. package/dist/text-color-transition.native.js +87 -0
  125. package/dist/text-color-transition.native.js.map +1 -0
  126. package/dist/text-input-props.native.d.ts +10 -0
  127. package/dist/text-input-props.native.d.ts.map +1 -0
  128. package/dist/text-input-props.native.js +15 -0
  129. package/dist/text-input-props.native.js.map +1 -0
  130. package/dist/text-input.native.d.ts +19 -0
  131. package/dist/text-input.native.d.ts.map +1 -0
  132. package/dist/text-input.native.js +32 -0
  133. package/dist/text-input.native.js.map +1 -0
  134. package/dist/view.d.ts +1 -285
  135. package/dist/view.d.ts.map +1 -1
  136. package/dist/view.js +5 -4
  137. package/dist/view.js.map +1 -1
  138. package/package.json +51 -7
  139. package/src/button-host-attributes.ts +56 -0
  140. package/src/button-host-props.native.ts +22 -0
  141. package/src/foundation.native.tsx +45 -6
  142. package/src/foundation.tsx +139 -28
  143. package/src/generated/button-dom-props.ts +127 -0
  144. package/src/generated/button-props.native.ts +1 -0
  145. package/src/generated/button-props.ts +4 -0
  146. package/src/generated/inherited-label.ts +2 -0
  147. package/src/generated/inherited-pressable.ts +2 -0
  148. package/src/generated/inherited-relative-text.ts +2 -0
  149. package/src/generated/inherited-text.ts +2 -0
  150. package/src/generated/inherited-view.ts +2 -0
  151. package/src/generated/native-disabled.ts +1 -0
  152. package/src/generated/text-input-dom-props.ts +128 -0
  153. package/src/generated/text-input.native.ts +1 -4
  154. package/src/generated/view-dom-props.ts +267 -0
  155. package/src/inherited-label.native.tsx +28 -0
  156. package/src/inherited-pressable.native.tsx +152 -0
  157. package/src/inherited-relative-text.native.tsx +22 -0
  158. package/src/inherited-text-scope.tsx +27 -0
  159. package/src/inherited-text-style.native.ts +35 -0
  160. package/src/inherited-text.native.tsx +32 -0
  161. package/src/inherited-view.native.tsx +37 -0
  162. package/src/link.native.tsx +43 -28
  163. package/src/link.ts +12 -0
  164. package/src/native-disabled.ts +23 -0
  165. package/src/presence.native.tsx +1 -1
  166. package/src/pressable.native.tsx +298 -262
  167. package/src/pressable.ts +34 -17
  168. package/src/runtime-container.native.tsx +30 -15
  169. package/src/text-color-transition.native.ts +96 -0
  170. package/src/text-input-props.native.ts +26 -0
  171. package/src/text-input.native.tsx +57 -0
  172. package/src/view.ts +34 -25
  173. package/dist/generated/animated.d.ts +0 -2
  174. package/dist/generated/animated.d.ts.map +0 -1
  175. package/dist/generated/animated.js +0 -3
  176. package/dist/generated/animated.js.map +0 -1
  177. package/dist/presence-context.d.ts +0 -15
  178. package/dist/presence-context.d.ts.map +0 -1
  179. package/dist/presence-context.js +0 -11
  180. package/dist/presence-context.js.map +0 -1
  181. package/dist/runtime-transition.native.d.ts +0 -30
  182. package/dist/runtime-transition.native.d.ts.map +0 -1
  183. package/dist/runtime-transition.native.js +0 -183
  184. package/dist/runtime-transition.native.js.map +0 -1
  185. package/src/generated/animated.ts +0 -6
  186. package/src/presence-context.ts +0 -27
  187. package/src/runtime-transition.native.tsx +0 -244
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hozo/primitives",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Canonical universal UI primitives for Hozo applications.",
5
5
  "keywords": [
6
6
  "react-native",
@@ -32,14 +32,26 @@
32
32
  "types": "./dist/index.d.ts",
33
33
  "default": "./dist/index.js"
34
34
  },
35
- "./generated/animated": {
36
- "types": "./dist/generated/animated.d.ts",
37
- "default": "./dist/generated/animated.js"
35
+ "./windowing": {
36
+ "types": "./dist/windowing.d.ts",
37
+ "default": "./dist/windowing.js"
38
38
  },
39
39
  "./generated/backdrop-filter": {
40
40
  "types": "./dist/generated/backdrop-filter.d.ts",
41
41
  "default": "./dist/generated/backdrop-filter.js"
42
42
  },
43
+ "./generated/button-dom-props": {
44
+ "types": "./dist/generated/button-dom-props.d.ts",
45
+ "default": "./dist/generated/button-dom-props.js"
46
+ },
47
+ "./generated/button-props": {
48
+ "react-native": {
49
+ "types": "./dist/generated/button-props.native.d.ts",
50
+ "default": "./dist/generated/button-props.native.js"
51
+ },
52
+ "types": "./dist/generated/button-props.d.ts",
53
+ "default": "./dist/generated/button-props.js"
54
+ },
43
55
  "./generated/container": {
44
56
  "types": "./dist/generated/container.d.ts",
45
57
  "default": "./dist/generated/container.js"
@@ -56,6 +68,26 @@
56
68
  "types": "./dist/generated/grid.d.ts",
57
69
  "default": "./dist/generated/grid.js"
58
70
  },
71
+ "./generated/inherited-label": {
72
+ "types": "./dist/generated/inherited-label.d.ts",
73
+ "default": "./dist/generated/inherited-label.js"
74
+ },
75
+ "./generated/inherited-pressable": {
76
+ "types": "./dist/generated/inherited-pressable.d.ts",
77
+ "default": "./dist/generated/inherited-pressable.js"
78
+ },
79
+ "./generated/inherited-relative-text": {
80
+ "types": "./dist/generated/inherited-relative-text.d.ts",
81
+ "default": "./dist/generated/inherited-relative-text.js"
82
+ },
83
+ "./generated/inherited-text": {
84
+ "types": "./dist/generated/inherited-text.d.ts",
85
+ "default": "./dist/generated/inherited-text.js"
86
+ },
87
+ "./generated/inherited-view": {
88
+ "types": "./dist/generated/inherited-view.d.ts",
89
+ "default": "./dist/generated/inherited-view.js"
90
+ },
59
91
  "./generated/link": {
60
92
  "react-native": {
61
93
  "types": "./dist/generated/link.native.d.ts",
@@ -64,6 +96,10 @@
64
96
  "types": "./dist/generated/link.d.ts",
65
97
  "default": "./dist/generated/link.js"
66
98
  },
99
+ "./generated/native-disabled": {
100
+ "types": "./dist/generated/native-disabled.d.ts",
101
+ "default": "./dist/generated/native-disabled.js"
102
+ },
67
103
  "./generated/pressable": {
68
104
  "react-native": {
69
105
  "types": "./dist/generated/pressable.native.d.ts",
@@ -104,6 +140,10 @@
104
140
  "types": "./dist/generated/text-input.d.ts",
105
141
  "default": "./dist/generated/text-input.js"
106
142
  },
143
+ "./generated/text-input-dom-props": {
144
+ "types": "./dist/generated/text-input-dom-props.d.ts",
145
+ "default": "./dist/generated/text-input-dom-props.js"
146
+ },
107
147
  "./generated/text-size": {
108
148
  "types": "./dist/generated/text-size.d.ts",
109
149
  "default": "./dist/generated/text-size.js"
@@ -116,6 +156,10 @@
116
156
  "types": "./dist/generated/view.d.ts",
117
157
  "default": "./dist/generated/view.js"
118
158
  },
159
+ "./generated/view-dom-props": {
160
+ "types": "./dist/generated/view-dom-props.d.ts",
161
+ "default": "./dist/generated/view-dom-props.js"
162
+ },
119
163
  "./package.json": "./package.json"
120
164
  },
121
165
  "files": [
@@ -131,12 +175,12 @@
131
175
  "provenance": true
132
176
  },
133
177
  "dependencies": {
134
- "@hozo/behaviors": "^0.2.0",
135
- "@hozo/engine": "^0.2.0"
178
+ "@hozo/behaviors": "^0.4.0",
179
+ "@hozo/engine": "^0.4.0"
136
180
  },
137
181
  "peerDependencies": {
138
182
  "react": "^19.0.0",
139
- "react-native": ">=0.86"
183
+ "react-native": ">=0.87"
140
184
  },
141
185
  "peerDependenciesMeta": {
142
186
  "react-native": {
@@ -0,0 +1,56 @@
1
+ /** The documented DOM metadata channel; never forward arbitrary leftover props. */
2
+ export interface ButtonDataProps {
3
+ [name: `data-${string}`]: string | number | boolean | undefined
4
+ }
5
+
6
+ export function buttonDataProps(props: ButtonDataProps): ButtonDataProps {
7
+ const data: ButtonDataProps = {}
8
+ for (const name in props) {
9
+ if (Object.hasOwn(props, name) && name.startsWith('data-')) {
10
+ data[name as `data-${string}`] = props[name as `data-${string}`]
11
+ }
12
+ }
13
+ return data
14
+ }
15
+
16
+ /**
17
+ * What a wrapper hands a Button to make it a trigger, by platform.
18
+ *
19
+ * `Tooltip` clones its child with a ref to position against, the handlers
20
+ * that open it, and -- on the Web -- `aria-describedby` naming the bubble.
21
+ * A Button that dropped them was a trigger nothing could hear (#1137).
22
+ * `Toolbar` hands its items a roving `tabIndex` on the Web, and a Button
23
+ * that dropped it left every item a tab stop of its own.
24
+ * Still a list rather than "every leftover prop": the rule above stands,
25
+ * and these are the ones a known wrapper writes.
26
+ */
27
+ export const BUTTON_TRIGGER_PROPS_WEB = [
28
+ 'ref',
29
+ 'onFocus',
30
+ 'onBlur',
31
+ 'onKeyDown',
32
+ 'onPointerEnter',
33
+ 'onPointerLeave',
34
+ 'aria-describedby',
35
+ 'tabIndex',
36
+ ] as const
37
+
38
+ export const BUTTON_TRIGGER_PROPS_NATIVE = [
39
+ 'ref',
40
+ 'onFocus',
41
+ 'onBlur',
42
+ 'onHoverIn',
43
+ 'onHoverOut',
44
+ 'onLongPress',
45
+ ] as const
46
+
47
+ export function buttonTriggerProps(
48
+ props: Record<string, unknown>,
49
+ names: readonly string[],
50
+ ): Record<string, unknown> {
51
+ const out: Record<string, unknown> = {}
52
+ for (const name of names) {
53
+ if (Object.hasOwn(props, name)) out[name] = props[name]
54
+ }
55
+ return out
56
+ }
@@ -0,0 +1,22 @@
1
+ let warned = false
2
+
3
+ /**
4
+ * Preserve evaluation/identity of authored props, but name the platform
5
+ * boundary: RN has no DOM dataset. Metadata is not an accessibility label
6
+ * or nativeID. Do not fabricate either from an application's data channel.
7
+ * The compiler uses this for opaque spreads; the fallback uses it too.
8
+ */
9
+ export function hozoButtonProps<T>(props: T): T {
10
+ if (!warned && props !== null && typeof props === 'object') {
11
+ for (const name in props) {
12
+ if (Object.hasOwn(props, name) && name.startsWith('data-')) {
13
+ warned = true
14
+ console.warn(
15
+ '[hozo] Button data-* metadata requires a Web DOM dataset; React Native does not consume it. nativeID remains available on both platforms. No data values are mapped to accessibility props.',
16
+ )
17
+ break
18
+ }
19
+ }
20
+ }
21
+ return props
22
+ }
@@ -1,19 +1,28 @@
1
1
  import { hozoTextChildren } from '@hozo/behaviors'
2
2
  import { HozoTextSizeContext } from '@hozo/engine'
3
- import React, { type ComponentType, type ReactNode, type Ref } from 'react'
3
+ import React, { type ComponentRef, type ComponentType, type ReactNode, type Ref } from 'react'
4
4
  import {
5
5
  type AccessibilityRole,
6
- type HostInstance,
7
6
  Pressable as RNPressable,
8
7
  type PressableProps as RNPressableProps,
9
8
  Text as RNText,
9
+ TextInput as RNTextInput,
10
10
  type TextProps as RNTextProps,
11
11
  type StyleProp,
12
12
  StyleSheet,
13
13
  type TextStyle,
14
14
  type ViewStyle,
15
15
  } from 'react-native'
16
+ import {
17
+ BUTTON_TRIGGER_PROPS_NATIVE,
18
+ type ButtonDataProps,
19
+ buttonDataProps,
20
+ buttonTriggerProps,
21
+ } from './button-host-attributes.ts'
22
+ import { hozoButtonProps } from './button-host-props.native.ts'
16
23
  import { HozoLink } from './link.native.tsx'
24
+ import { nativeDisabledProps } from './native-disabled.ts'
25
+ import { type NativeTextInputProps, nativeTextInputProps } from './text-input-props.native.ts'
17
26
 
18
27
  export type {
19
28
  HozoResponderEvent,
@@ -24,10 +33,15 @@ export type {
24
33
  } from '@hozo/engine'
25
34
  export type {
26
35
  ImageProps,
27
- TextInputProps,
28
36
  ViewProps,
29
37
  } from 'react-native'
30
- export { Image, TextInput, View } from 'react-native'
38
+ export { Image, View } from 'react-native'
39
+ export type TextInputProps = NativeTextInputProps & { ref?: Ref<ComponentRef<typeof RNTextInput>> }
40
+
41
+ /** The canonical disabled contract also applies outside compiler-owned JSX. */
42
+ export function TextInput({ ref, ...props }: TextInputProps) {
43
+ return <RNTextInput {...nativeTextInputProps(props)} ref={ref} />
44
+ }
31
45
  export type {
32
46
  HozoImageSource,
33
47
  HozoImageSourceObject,
@@ -60,7 +74,11 @@ export interface PressableProps extends RNPressableProps {
60
74
  * A caller needs it to hand the control to something else: `Dialog`'s
61
75
  * `restoreFocusTo` is where this came up (#462).
62
76
  */
63
- ref?: Ref<HostInstance>
77
+ // Derived from the installed peer's Pressable rather than spelled out:
78
+ // its ref type has changed between minors (0.86 a View instance, later a
79
+ // HostInstance), and this stays exactly what that Pressable accepts (and
80
+ // Dialog can restore).
81
+ ref?: Ref<ComponentRef<typeof RNPressable>>
64
82
  href?: string
65
83
  external?: boolean
66
84
  replace?: boolean
@@ -80,6 +98,7 @@ export function Pressable({
80
98
  download: _download,
81
99
  ...props
82
100
  }: PressableProps) {
101
+ props = nativeDisabledProps(props)
83
102
  if (href === undefined) return <RNPressable {...props} />
84
103
 
85
104
  const Link = HozoLink as unknown as ComponentType<
@@ -99,6 +118,7 @@ export interface LinkProps {
99
118
  rel?: string
100
119
  download?: boolean | string
101
120
  onPress?: (event: { defaultPrevented?: boolean }) => void
121
+ disabled?: boolean
102
122
  style?: StyleProp<TextStyle>
103
123
  testID?: string
104
124
  nativeID?: string
@@ -128,7 +148,7 @@ export function Link({
128
148
  )
129
149
  }
130
150
 
131
- export interface ButtonProps {
151
+ export interface ButtonProps extends ButtonDataProps {
132
152
  className?: string
133
153
  children?: ReactNode
134
154
  onPress?: RNPressableProps['onPress']
@@ -144,6 +164,18 @@ export interface ButtonProps {
144
164
  download?: boolean | string
145
165
  style?: StyleProp<ViewStyle>
146
166
  testID?: string
167
+ nativeID?: string
168
+ /**
169
+ * What a wrapper writes to make this a trigger -- `Tooltip` measures the
170
+ * ref and opens on hover or a long press (#1137). Forwarded to the
171
+ * Pressable as given.
172
+ */
173
+ ref?: Ref<ComponentRef<typeof RNPressable>>
174
+ onFocus?: RNPressableProps['onFocus']
175
+ onBlur?: RNPressableProps['onBlur']
176
+ onHoverIn?: RNPressableProps['onHoverIn']
177
+ onHoverOut?: RNPressableProps['onHoverOut']
178
+ onLongPress?: RNPressableProps['onLongPress']
147
179
  }
148
180
 
149
181
  export type ButtonNativeProps = ButtonProps
@@ -160,14 +192,19 @@ export function Button({
160
192
  prefetch,
161
193
  style,
162
194
  testID,
195
+ nativeID,
196
+ ...metadata
163
197
  }: ButtonProps) {
164
198
  const shared = {
199
+ ...hozoButtonProps(buttonDataProps(metadata)),
200
+ ...buttonTriggerProps(metadata, BUTTON_TRIGGER_PROPS_NATIVE),
165
201
  disabled,
166
202
  accessibilityLabel,
167
203
  accessibilityHint,
168
204
  accessibilityState: disabled === undefined ? undefined : { disabled },
169
205
  style,
170
206
  testID,
207
+ nativeID,
171
208
  }
172
209
  if (href !== undefined) {
173
210
  const NativeLink = HozoLink as unknown as ComponentType<
@@ -192,3 +229,5 @@ export function Button({
192
229
  </RNPressable>
193
230
  )
194
231
  }
232
+ // A label a pressable can hand its text styles to (#876); see `hozoTextStyle`.
233
+ ;(Text as { hozoLabelText?: boolean }).hozoLabelText = true
@@ -6,18 +6,31 @@
6
6
  import {
7
7
  type HozoDomStyle,
8
8
  hozoDomStyle,
9
+ hozoViewBase,
10
+ hozoViewClassName,
9
11
  type ResponderProps,
10
12
  useResponderDomProps,
11
13
  } from '@hozo/engine'
12
14
  import {
13
15
  type AriaRole,
14
16
  type CSSProperties,
17
+ type FocusEventHandler,
18
+ type KeyboardEventHandler,
15
19
  type MouseEventHandler,
20
+ type PointerEventHandler,
16
21
  type ReactNode,
22
+ type Ref,
23
+ useCallback,
17
24
  useEffect,
18
25
  useRef,
19
26
  useState,
20
27
  } from 'react'
28
+ import {
29
+ BUTTON_TRIGGER_PROPS_WEB,
30
+ type ButtonDataProps,
31
+ buttonDataProps,
32
+ buttonTriggerProps,
33
+ } from './button-host-attributes.ts'
21
34
  import { HozoLink } from './link.ts'
22
35
  import { HozoPressable, type HozoPressableProps } from './pressable.ts'
23
36
  import { HozoTextInput, type HozoTextInputProps } from './text-input.ts'
@@ -89,8 +102,29 @@ function universalDomProps(props: UniversalProps) {
89
102
  } as const
90
103
  }
91
104
 
92
- function useLayoutRef<T extends HTMLElement>(onLayout?: (event: HozoLayoutEvent) => void) {
105
+ /**
106
+ * The element, for `onLayout` and the responder, and the caller's `ref` too.
107
+ *
108
+ * React Native's `View` takes a `ref`, and the compiled `<div>` passes one
109
+ * straight through. This fallback held its own ref for `onLayout` and gave
110
+ * the caller nothing, so whether `ref` worked depended on whether the
111
+ * compiler had run. `setRef` hands the node to both.
112
+ */
113
+ function useLayoutRef<T extends HTMLElement>(
114
+ onLayout?: (event: HozoLayoutEvent) => void,
115
+ forwarded?: Ref<T>,
116
+ ) {
93
117
  const elementRef = useRef<T>(null)
118
+ // A new function only when the caller's ref changes, so React detaches the
119
+ // old one and attaches the new rather than re-running both every render.
120
+ const setRef = useCallback(
121
+ (node: T | null) => {
122
+ elementRef.current = node
123
+ if (typeof forwarded === 'function') forwarded(node)
124
+ else if (forwarded) forwarded.current = node
125
+ },
126
+ [forwarded],
127
+ )
94
128
  const callbackRef = useRef(onLayout)
95
129
  callbackRef.current = onLayout
96
130
 
@@ -120,33 +154,43 @@ function useLayoutRef<T extends HTMLElement>(onLayout?: (event: HozoLayoutEvent)
120
154
  return () => observer.disconnect()
121
155
  }, [])
122
156
 
123
- return elementRef
157
+ return { elementRef, setRef }
124
158
  }
125
159
 
126
160
  export interface ViewProps extends UniversalProps, ResponderProps {
127
161
  className?: string
128
162
  children?: ReactNode
163
+ ref?: Ref<HTMLDivElement>
129
164
  }
130
165
 
131
- export function View({ className, children, onLayout, ...universal }: ViewProps) {
132
- const ref = useLayoutRef<HTMLDivElement>(onLayout)
133
- const responder = useResponderDomProps(ref, universal)
166
+ export function View({ className, children, onLayout, ref, ...universal }: ViewProps) {
167
+ const { elementRef, setRef } = useLayoutRef<HTMLDivElement>(onLayout, ref)
168
+ const responder = useResponderDomProps(elementRef, universal)
134
169
  return (
135
- <div ref={ref} className={className} {...universalDomProps(universal)} {...responder}>
136
- {children}
137
- </div>
170
+ <>
171
+ {hozoViewBase()}
172
+ <div
173
+ ref={setRef}
174
+ className={hozoViewClassName(className)}
175
+ {...universalDomProps(universal)}
176
+ {...responder}
177
+ >
178
+ {children}
179
+ </div>
180
+ </>
138
181
  )
139
182
  }
140
183
 
141
184
  export interface TextProps extends UniversalProps {
142
185
  className?: string
143
186
  children?: ReactNode
187
+ ref?: Ref<HTMLSpanElement>
144
188
  }
145
189
 
146
- export function Text({ className, children, onLayout, ...universal }: TextProps) {
147
- const ref = useLayoutRef<HTMLSpanElement>(onLayout)
190
+ export function Text({ className, children, onLayout, ref, ...universal }: TextProps) {
191
+ const { setRef } = useLayoutRef<HTMLSpanElement>(onLayout, ref)
148
192
  return (
149
- <span ref={ref} className={className} {...universalDomProps(universal)}>
193
+ <span ref={setRef} className={className} {...universalDomProps(universal)}>
150
194
  {children}
151
195
  </span>
152
196
  )
@@ -160,6 +204,7 @@ export interface ImageProps extends UniversalProps {
160
204
  accessibilityLabel?: string
161
205
  onLoad?: (event: unknown) => void
162
206
  onError?: (event: unknown) => void
207
+ ref?: Ref<HTMLImageElement>
163
208
  }
164
209
 
165
210
  export interface HozoImageSourceObject {
@@ -200,15 +245,49 @@ export function Image({
200
245
  onLoad,
201
246
  onError,
202
247
  onLayout,
248
+ ref,
203
249
  ...universal
204
250
  }: ImageProps) {
205
- const ref = useLayoutRef<HTMLImageElement>(onLayout)
251
+ const { setRef } = useLayoutRef<HTMLImageElement>(onLayout, ref)
206
252
  const [failedSource, setFailedSource] = useState<HozoImageSource>()
207
- const webSrc =
208
- (failedSource === src ? undefined : webImageSource(src)) ?? webImageSource(defaultSource)
253
+ const primary = webImageSource(src)
254
+ const fallback = webImageSource(defaultSource)
255
+ const webSrc = (failedSource === src ? undefined : primary) ?? fallback
256
+ // A server-rendered `<img>` starts loading before React hydrates, so its
257
+ // `error` event may already have fired when `onError` is attached (#1156).
258
+ // `complete && naturalWidth === 0` spots that, and `decode()` separates a
259
+ // broken load from a widthless SVG that some browsers report as 0px wide.
260
+ // The caller's `onError` is not synthesized here because the native error
261
+ // event has already come and gone before hydration.
262
+ const stateRef = useRef({ src, primary, fallback, failedSource })
263
+ stateRef.current = { src, primary, fallback, failedSource }
264
+ const imageRef = useCallback(
265
+ (node: HTMLImageElement | null) => {
266
+ setRef(node)
267
+ const current = stateRef.current
268
+ if (
269
+ node &&
270
+ current.primary &&
271
+ current.fallback &&
272
+ current.failedSource !== current.src &&
273
+ node.complete &&
274
+ node.naturalWidth === 0
275
+ ) {
276
+ const failed = current.src
277
+ if (typeof node.decode === 'function') {
278
+ node.decode().then(undefined, () => {
279
+ if (stateRef.current.src === failed) setFailedSource(failed)
280
+ })
281
+ } else {
282
+ setFailedSource(failed)
283
+ }
284
+ }
285
+ },
286
+ [setRef],
287
+ )
209
288
  return (
210
289
  <img
211
- ref={ref}
290
+ ref={imageRef}
212
291
  className={className}
213
292
  src={webSrc}
214
293
  alt={alt ?? accessibilityLabel ?? ''}
@@ -246,13 +325,14 @@ export interface LinkProps {
246
325
  replace?: boolean
247
326
  prefetch?: boolean
248
327
  onPress?: MouseEventHandler<HTMLAnchorElement>
328
+ disabled?: boolean
249
329
  }
250
330
 
251
331
  export function Link({ children, ...props }: LinkProps) {
252
332
  return <HozoLink {...props}>{children}</HozoLink>
253
333
  }
254
334
 
255
- export interface ButtonProps {
335
+ export interface ButtonProps extends ButtonDataProps {
256
336
  className?: string
257
337
  children?: ReactNode
258
338
  onPress?: MouseEventHandler<HTMLElement>
@@ -267,6 +347,22 @@ export interface ButtonProps {
267
347
  rel?: string
268
348
  download?: boolean | string
269
349
  testID?: string
350
+ /** Stable host ID: DOM id on Web, nativeID on React Native. */
351
+ nativeID?: string
352
+ /**
353
+ * What a wrapper writes to make this a trigger -- `Tooltip` positions
354
+ * against the ref, opens on these handlers, and names its bubble with
355
+ * `aria-describedby` (#1137). Forwarded to the element as given.
356
+ */
357
+ ref?: Ref<HTMLElement>
358
+ onFocus?: FocusEventHandler<HTMLElement>
359
+ onBlur?: FocusEventHandler<HTMLElement>
360
+ onKeyDown?: KeyboardEventHandler<HTMLElement>
361
+ onPointerEnter?: PointerEventHandler<HTMLElement>
362
+ onPointerLeave?: PointerEventHandler<HTMLElement>
363
+ 'aria-describedby'?: string
364
+ /** A roving tab stop, as `Toolbar` hands its items. */
365
+ tabIndex?: number
270
366
  }
271
367
 
272
368
  export function Button({
@@ -284,10 +380,17 @@ export function Button({
284
380
  rel,
285
381
  download,
286
382
  testID,
383
+ nativeID,
384
+ ...metadata
287
385
  }: ButtonProps) {
386
+ const data = {
387
+ ...buttonDataProps(metadata),
388
+ ...buttonTriggerProps(metadata, BUTTON_TRIGGER_PROPS_WEB),
389
+ }
288
390
  if (href != null) {
289
391
  return (
290
392
  <HozoLink
393
+ {...data}
291
394
  href={href}
292
395
  external={external}
293
396
  replace={replace}
@@ -300,6 +403,7 @@ export function Button({
300
403
  accessibilityHint={accessibilityHint}
301
404
  disabled={disabled}
302
405
  testID={testID}
406
+ nativeID={nativeID}
303
407
  onPress={onPress as MouseEventHandler<HTMLAnchorElement>}
304
408
  >
305
409
  {children}
@@ -307,17 +411,24 @@ export function Button({
307
411
  )
308
412
  }
309
413
  return (
310
- <button
311
- type="button"
312
- className={className}
313
- disabled={disabled}
314
- data-hozo-disabled={disabled ? '' : undefined}
315
- aria-label={accessibilityLabel}
316
- aria-description={accessibilityHint}
317
- data-testid={testID}
318
- onClick={onPress as MouseEventHandler<HTMLButtonElement>}
319
- >
320
- {children}
321
- </button>
414
+ <>
415
+ {hozoViewBase()}
416
+ <button
417
+ {...data}
418
+ type="button"
419
+ className={className}
420
+ disabled={disabled}
421
+ data-hozo-disabled={disabled ? '' : undefined}
422
+ aria-disabled={disabled}
423
+ data-hozo-pressable={onPress && !disabled ? '' : undefined}
424
+ aria-label={accessibilityLabel}
425
+ aria-description={accessibilityHint}
426
+ data-testid={testID}
427
+ id={nativeID}
428
+ onClick={onPress as MouseEventHandler<HTMLButtonElement>}
429
+ >
430
+ {children}
431
+ </button>
432
+ </>
322
433
  )
323
434
  }