@hozo/primitives 0.3.0 → 0.5.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 (136) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/README.md +1 -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 +45 -4
  12. package/dist/foundation.d.ts.map +1 -1
  13. package/dist/foundation.js +52 -6
  14. package/dist/foundation.js.map +1 -1
  15. package/dist/foundation.native.d.ts +34 -7
  16. package/dist/foundation.native.d.ts.map +1 -1
  17. package/dist/foundation.native.js +21 -5
  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 +155 -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-leaves.d.ts +2 -0
  32. package/dist/generated/inherited-leaves.d.ts.map +1 -0
  33. package/dist/generated/inherited-leaves.js +4 -0
  34. package/dist/generated/inherited-leaves.js.map +1 -0
  35. package/dist/generated/native-disabled.d.ts +2 -0
  36. package/dist/generated/native-disabled.d.ts.map +1 -0
  37. package/dist/generated/native-disabled.js +2 -0
  38. package/dist/generated/native-disabled.js.map +1 -0
  39. package/dist/generated/text-input-dom-props.d.ts +7 -0
  40. package/dist/generated/text-input-dom-props.d.ts.map +1 -0
  41. package/dist/generated/text-input-dom-props.js +130 -0
  42. package/dist/generated/text-input-dom-props.js.map +1 -0
  43. package/dist/generated/text-input.native.d.ts +1 -1
  44. package/dist/generated/text-input.native.d.ts.map +1 -1
  45. package/dist/generated/text-input.native.js +1 -1
  46. package/dist/generated/text-input.native.js.map +1 -1
  47. package/dist/generated/text.d.ts +1 -1
  48. package/dist/generated/text.d.ts.map +1 -1
  49. package/dist/generated/text.js +4 -1
  50. package/dist/generated/text.js.map +1 -1
  51. package/dist/generated/view-dom-props.d.ts +8 -0
  52. package/dist/generated/view-dom-props.d.ts.map +1 -0
  53. package/dist/generated/view-dom-props.js +241 -0
  54. package/dist/generated/view-dom-props.js.map +1 -0
  55. package/dist/inherited-leaves.native.d.ts +28 -0
  56. package/dist/inherited-leaves.native.d.ts.map +1 -0
  57. package/dist/inherited-leaves.native.js +49 -0
  58. package/dist/inherited-leaves.native.js.map +1 -0
  59. package/dist/inherited-text-scope.d.ts +8 -0
  60. package/dist/inherited-text-scope.d.ts.map +1 -0
  61. package/dist/inherited-text-scope.js +10 -0
  62. package/dist/inherited-text-scope.js.map +1 -0
  63. package/dist/link.d.ts +4 -3
  64. package/dist/link.d.ts.map +1 -1
  65. package/dist/link.js +9 -1
  66. package/dist/link.js.map +1 -1
  67. package/dist/link.native.d.ts +1 -1
  68. package/dist/link.native.d.ts.map +1 -1
  69. package/dist/link.native.js +7 -1
  70. package/dist/link.native.js.map +1 -1
  71. package/dist/native-disabled.d.ts +13 -0
  72. package/dist/native-disabled.d.ts.map +1 -0
  73. package/dist/native-disabled.js +13 -0
  74. package/dist/native-disabled.js.map +1 -0
  75. package/dist/presence.native.d.ts.map +1 -1
  76. package/dist/presence.native.js +1 -1
  77. package/dist/presence.native.js.map +1 -1
  78. package/dist/pressable.d.ts.map +1 -1
  79. package/dist/pressable.js +8 -1
  80. package/dist/pressable.js.map +1 -1
  81. package/dist/pressable.native.d.ts +23 -2
  82. package/dist/pressable.native.d.ts.map +1 -1
  83. package/dist/pressable.native.js +57 -46
  84. package/dist/pressable.native.js.map +1 -1
  85. package/dist/text-color-transition.native.d.ts +12 -0
  86. package/dist/text-color-transition.native.d.ts.map +1 -0
  87. package/dist/text-color-transition.native.js +87 -0
  88. package/dist/text-color-transition.native.js.map +1 -0
  89. package/dist/text-input-props.native.d.ts +10 -0
  90. package/dist/text-input-props.native.d.ts.map +1 -0
  91. package/dist/text-input-props.native.js +15 -0
  92. package/dist/text-input-props.native.js.map +1 -0
  93. package/dist/text-input.native.d.ts +19 -0
  94. package/dist/text-input.native.d.ts.map +1 -0
  95. package/dist/text-input.native.js +32 -0
  96. package/dist/text-input.native.js.map +1 -0
  97. package/package.json +35 -7
  98. package/src/button-host-attributes.ts +56 -0
  99. package/src/button-host-props.native.ts +22 -0
  100. package/src/foundation.native.tsx +59 -8
  101. package/src/foundation.tsx +119 -5
  102. package/src/generated/button-dom-props.ts +159 -0
  103. package/src/generated/button-props.native.ts +1 -0
  104. package/src/generated/button-props.ts +4 -0
  105. package/src/generated/inherited-leaves.ts +3 -0
  106. package/src/generated/native-disabled.ts +1 -0
  107. package/src/generated/text-input-dom-props.ts +128 -0
  108. package/src/generated/text-input.native.ts +1 -4
  109. package/src/generated/text.ts +5 -0
  110. package/src/generated/view-dom-props.ts +267 -0
  111. package/src/inherited-leaves.native.tsx +83 -0
  112. package/src/inherited-text-scope.tsx +27 -0
  113. package/src/link.native.tsx +17 -13
  114. package/src/link.ts +12 -0
  115. package/src/native-disabled.ts +23 -0
  116. package/src/presence.native.tsx +1 -1
  117. package/src/pressable.native.tsx +90 -42
  118. package/src/pressable.ts +8 -1
  119. package/src/text-color-transition.native.ts +96 -0
  120. package/src/text-input-props.native.ts +26 -0
  121. package/src/text-input.native.tsx +57 -0
  122. package/dist/generated/animated.d.ts +0 -2
  123. package/dist/generated/animated.d.ts.map +0 -1
  124. package/dist/generated/animated.js +0 -3
  125. package/dist/generated/animated.js.map +0 -1
  126. package/dist/presence-context.d.ts +0 -15
  127. package/dist/presence-context.d.ts.map +0 -1
  128. package/dist/presence-context.js +0 -11
  129. package/dist/presence-context.js.map +0 -1
  130. package/dist/runtime-transition.native.d.ts +0 -30
  131. package/dist/runtime-transition.native.d.ts.map +0 -1
  132. package/dist/runtime-transition.native.js +0 -183
  133. package/dist/runtime-transition.native.js.map +0 -1
  134. package/src/generated/animated.ts +0 -6
  135. package/src/presence-context.ts +0 -27
  136. package/src/runtime-transition.native.tsx +0 -244
@@ -0,0 +1,159 @@
1
+ // What a Button's spread means on the Web, once the compiler has decided
2
+ // the element (#1096).
3
+ //
4
+ // `function B({ href, ...rest }) { return <Button {...rest} /> }` lowers to
5
+ // a `<button>` at build time (#656), but `rest` still holds the props in the
6
+ // primitive's vocabulary -- `onPress`, `accessibilityLabel` -- and spread raw
7
+ // onto the DOM they are unknown attributes. The click handler was the one
8
+ // that showed: an `@hozo/ui` Button that did nothing when clicked.
9
+ //
10
+ // So the spread goes through the same translation the uncompiled `Button`
11
+ // applies to its named props, and drops what that `Button` drops. Keys the
12
+ // spread does not hold are not set, because explicit attributes the
13
+ // compiler wrote before the spread must survive it.
14
+
15
+ import { type HozoDomStyle, hozoDomStyle } from '@hozo/engine'
16
+
17
+ type Props = Record<string, unknown>
18
+
19
+ interface AccessibilityState {
20
+ disabled?: boolean
21
+ selected?: boolean
22
+ checked?: boolean | 'mixed'
23
+ busy?: boolean
24
+ expanded?: boolean
25
+ }
26
+
27
+ /** `accessibilityState.disabled` disables a Button as the `disabled` prop does. */
28
+ const stateDisabled = (props: Props) =>
29
+ (props.accessibilityState as AccessibilityState | undefined)?.disabled === true
30
+
31
+ const prevent = (event: { preventDefault(): void }) => event.preventDefault()
32
+
33
+ function common(props: Props, out: Props, name: string, value: unknown): boolean {
34
+ switch (name) {
35
+ case 'accessibilityLabel':
36
+ out['aria-label'] = value
37
+ return true
38
+ case 'accessibilityHint':
39
+ out['aria-description'] = value
40
+ return true
41
+ case 'testID':
42
+ out['data-testid'] = value
43
+ return true
44
+ case 'nativeID':
45
+ out.id = value
46
+ return true
47
+ case 'className':
48
+ case 'children':
49
+ // What a wrapper writes to make the Button a trigger: `Tooltip` clones
50
+ // its child with these (#1137). The uncompiled Button forwards the same
51
+ // list, `BUTTON_TRIGGER_PROPS_WEB`.
52
+ case 'ref':
53
+ case 'onFocus':
54
+ case 'onBlur':
55
+ case 'onKeyDown':
56
+ case 'onPointerEnter':
57
+ case 'onPointerLeave':
58
+ case 'aria-describedby':
59
+ // A disclosure trigger names what it opens (#1704).
60
+ case 'aria-controls':
61
+ out[name] = value
62
+ return true
63
+ case 'accessibilityState': {
64
+ // `disabled` is the element's own business, read by each caller.
65
+ const state = value as AccessibilityState | undefined
66
+ out['aria-selected'] = state?.selected
67
+ out['aria-checked'] = state?.checked
68
+ out['aria-busy'] = state?.busy
69
+ out['aria-expanded'] = state?.expanded
70
+ return true
71
+ }
72
+ case 'style':
73
+ out.style = hozoDomStyle(value as HozoDomStyle)
74
+ return true
75
+ }
76
+ if (name.startsWith('data-')) {
77
+ out[name] = props[name]
78
+ return true
79
+ }
80
+ return false
81
+ }
82
+
83
+ /** A Button's spread onto the `<button>` the compiler chose. */
84
+ export function hozoButtonDomProps<T>(props: T): T {
85
+ if (!props || typeof props !== 'object') return props
86
+ const source = props as Props
87
+ const out: Props = {}
88
+ for (const name in source) {
89
+ if (!Object.hasOwn(source, name)) continue
90
+ const value = source[name]
91
+ if (common(source, out, name, value)) continue
92
+ if (name === 'tabIndex') {
93
+ // A roving tab stop, as `Toolbar` hands its items (BUTTON_TRIGGER_PROPS_WEB).
94
+ out.tabIndex = value
95
+ } else if (name === 'onPress') {
96
+ out.onClick = value
97
+ if (value) out['data-hozo-pressable'] = ''
98
+ } else if (name === 'disabled') {
99
+ out.disabled = value
100
+ out['data-hozo-disabled'] = value ? '' : undefined
101
+ }
102
+ // Anything else -- link-only props with no href, unknown keys -- the
103
+ // uncompiled Button does not forward either.
104
+ }
105
+ if (stateDisabled(source)) {
106
+ out.disabled = true
107
+ out['data-hozo-disabled'] = ''
108
+ }
109
+ return out as T
110
+ }
111
+
112
+ /**
113
+ * A Button's spread onto the `<a>` the compiler chose because it was given
114
+ * an `href`. An anchor has no disabled state, so a press is suppressed
115
+ * while disabled, as the compiler does for a named `onPress`. `disabled` is
116
+ * the one the compiler saw named on the element, if any; one inside the
117
+ * spread counts too.
118
+ */
119
+ export function hozoLinkDomProps<T>(props: T, disabled?: unknown): T {
120
+ if (!props || typeof props !== 'object') return props
121
+ const source = props as Props
122
+ const fromState = stateDisabled(source)
123
+ const off = Boolean(disabled ?? source.disabled) || fromState
124
+ const out: Props = {}
125
+ const disable = (value: unknown) => {
126
+ out['aria-disabled'] = value ? true : undefined
127
+ out['data-hozo-disabled'] = value ? '' : undefined
128
+ if (value) out.tabIndex = -1
129
+ else if (!Object.hasOwn(source, 'tabIndex')) out.tabIndex = undefined
130
+ if (value) {
131
+ out.onClick ??= prevent
132
+ out.onAuxClick = prevent
133
+ }
134
+ }
135
+ for (const name in source) {
136
+ if (!Object.hasOwn(source, name)) continue
137
+ const value = source[name]
138
+ if (common(source, out, name, value)) continue
139
+ switch (name) {
140
+ case 'onPress':
141
+ out.onClick = off ? prevent : value
142
+ break
143
+ case 'tabIndex':
144
+ // A roving tab stop, but never one while disabled.
145
+ out.tabIndex = off ? -1 : value
146
+ break
147
+ case 'disabled':
148
+ disable(value || fromState)
149
+ break
150
+ case 'target':
151
+ case 'rel':
152
+ case 'download':
153
+ out[name] = value
154
+ break
155
+ }
156
+ }
157
+ if (fromState && !Object.hasOwn(source, 'disabled')) disable(true)
158
+ return out as T
159
+ }
@@ -0,0 +1 @@
1
+ export { hozoButtonProps } from '../button-host-props.native.ts'
@@ -0,0 +1,4 @@
1
+ /** Only the Native compiler needs the opaque-spread metadata warning. */
2
+ export function hozoButtonProps<T>(props: T): T {
3
+ return props
4
+ }
@@ -0,0 +1,3 @@
1
+ // Native-only compiler ABI (decision 013). A leaf of its own because nearly
2
+ // every Native file emits it, and Metro ships whatever module it imports.
3
+ export { HozoInheritedLeafText, HozoInheritedScope } from '../inherited-leaves.native.tsx'
@@ -0,0 +1 @@
1
+ export { nativeDisabledProps } from '../native-disabled.ts'
@@ -0,0 +1,128 @@
1
+ // What a TextInput's spread means on the Web, once the compiler has lowered
2
+ // it to an `<input>` or a `<textarea>` (#1118).
3
+ //
4
+ // `function Field(props) { return <TextInput {...props} /> }` lowers at build
5
+ // time, but `props` still holds the primitive's vocabulary -- `onChangeText`,
6
+ // `editable`, `keyboardType` -- and spread raw onto the DOM those are unknown
7
+ // attributes. `onChangeText` was the one that showed: a controlled
8
+ // `@hozo/ui` Input that React reset on every keystroke, because nothing ever
9
+ // called the handler. #1096 was the same bug for Button.
10
+ //
11
+ // So the spread goes through the translation the uncompiled `HozoTextInput`
12
+ // applies to its named props. Unlike Button, everything the translation does
13
+ // not name passes through, as it does there: `placeholder`, `maxLength`,
14
+ // `onFocus` and the rest are already DOM props. Keys the spread does not
15
+ // hold are not set, because explicit attributes the compiler wrote before
16
+ // the spread must survive it.
17
+
18
+ import { type HozoDomStyle, hozoDomStyle } from '@hozo/engine'
19
+
20
+ type Props = Record<string, unknown>
21
+ type Handler = ((value: unknown) => void) | undefined
22
+ type Field = { value: string; blur(): void }
23
+ type KeyEvent = { key: string; currentTarget: Field }
24
+
25
+ const INPUT_MODES: Record<string, string> = {
26
+ 'number-pad': 'numeric',
27
+ numeric: 'numeric',
28
+ 'decimal-pad': 'decimal',
29
+ 'email-address': 'email',
30
+ 'phone-pad': 'tel',
31
+ url: 'url',
32
+ 'web-search': 'search',
33
+ }
34
+
35
+ // React Native props with no DOM meaning, and the ones folded into another
36
+ // attribute below. The uncompiled TextInput drops them too.
37
+ const CONSUMED = new Set([
38
+ 'accessibilityHint',
39
+ 'accessibilityLabel',
40
+ 'accessibilityLabelledBy',
41
+ 'autoCorrect',
42
+ 'blurOnSubmit',
43
+ 'cursorColor',
44
+ 'editable',
45
+ 'hitSlop',
46
+ 'keyboardAppearance',
47
+ 'keyboardType',
48
+ 'multiline',
49
+ 'nativeID',
50
+ 'numberOfLines',
51
+ 'onChange',
52
+ 'onChangeText',
53
+ 'onKeyPress',
54
+ 'onSubmitEditing',
55
+ 'placeholderTextColor',
56
+ 'returnKeyType',
57
+ 'secureTextEntry',
58
+ 'selectionColor',
59
+ 'style',
60
+ 'testID',
61
+ ])
62
+
63
+ /**
64
+ * A TextInput's spread onto the element the compiler chose. `multiline` is
65
+ * whether that element is a `<textarea>`, which the compiler knows and the
66
+ * spread may not say.
67
+ */
68
+ export function hozoTextInputDomProps<T>(props: T, multiline = false): T {
69
+ if (!props || typeof props !== 'object') return props
70
+ const source = props as Props
71
+ const has = (name: string) => Object.hasOwn(source, name)
72
+ const out: Props = {}
73
+ for (const name in source) {
74
+ if (has(name) && !CONSUMED.has(name)) out[name] = source[name]
75
+ }
76
+
77
+ if (has('accessibilityLabel')) out['aria-label'] = source.accessibilityLabel
78
+ if (has('accessibilityHint')) out['aria-description'] = source.accessibilityHint
79
+ if (has('accessibilityLabelledBy')) out['aria-labelledby'] = source.accessibilityLabelledBy
80
+ if (has('testID')) out['data-testid'] = source.testID
81
+ if (has('nativeID')) out.id = source.nativeID
82
+
83
+ if (has('onChange') || has('onChangeText')) {
84
+ const onChange = source.onChange as Handler
85
+ const onChangeText = source.onChangeText as Handler
86
+ out.onChange = (event: { currentTarget: Field }) => {
87
+ onChange?.(event)
88
+ onChangeText?.(event.currentTarget.value)
89
+ }
90
+ }
91
+ if (has('onKeyPress') || has('onSubmitEditing')) {
92
+ const onKeyPress = source.onKeyPress as Handler
93
+ const onSubmitEditing = source.onSubmitEditing as Handler
94
+ const blurOnSubmit = Boolean(source.blurOnSubmit)
95
+ out.onKeyDown = (event: KeyEvent) => {
96
+ onKeyPress?.(event)
97
+ if (event.key !== 'Enter' || !onSubmitEditing) return
98
+ if (!multiline || blurOnSubmit) {
99
+ onSubmitEditing({ nativeEvent: { text: event.currentTarget.value } })
100
+ if (blurOnSubmit) event.currentTarget.blur()
101
+ }
102
+ }
103
+ }
104
+
105
+ if (has('readOnly')) out.readOnly = source.readOnly
106
+ else if (has('editable') && source.editable !== undefined) out.readOnly = !source.editable
107
+ if (has('autoCorrect') && source.autoCorrect !== undefined) {
108
+ out.autoCorrect = source.autoCorrect ? 'on' : 'off'
109
+ }
110
+ if (!has('inputMode') && has('keyboardType')) {
111
+ out.inputMode = INPUT_MODES[source.keyboardType as string]
112
+ }
113
+ if (has('returnKeyType')) out.enterKeyHint = source.returnKeyType
114
+ if (multiline) {
115
+ if (has('numberOfLines')) out.rows = source.numberOfLines
116
+ } else if (has('secureTextEntry') && source.secureTextEntry) {
117
+ out.type = 'password'
118
+ }
119
+
120
+ const caret = source.cursorColor ?? source.selectionColor
121
+ if (has('style') || caret) {
122
+ out.style = hozoDomStyle([
123
+ source.style as HozoDomStyle,
124
+ caret ? { caretColor: caret as string } : undefined,
125
+ ])
126
+ }
127
+ return out as T
128
+ }
@@ -1,4 +1 @@
1
- export {
2
- TextInput as HozoTextInput,
3
- type TextInputProps as HozoTextInputProps,
4
- } from 'react-native'
1
+ export { HozoTextInput, type HozoTextInputProps } from '../text-input.native.tsx'
@@ -1,7 +1,12 @@
1
1
  // Native-only: the Web backend lowers `Text` to an element and never asks
2
2
  // for this. One implementation under every condition rather than a Web file
3
3
  // that could only lie about what it is.
4
+ //
5
+ // HozoInteractionView lives here too: it is the same reader of a Pressable's
6
+ // interaction context, for a View rather than a Text.
4
7
  export {
8
+ HozoInteractionView,
9
+ type HozoInteractionViewProps,
5
10
  HozoText,
6
11
  type HozoTextProps,
7
12
  type HozoTransition,
@@ -0,0 +1,267 @@
1
+ // What a View's spread means on the Web, once the compiler has lowered it to
2
+ // a plain element -- and a Text's, an Image's, and every document primitive's,
3
+ // since they share the same universal props (#1122).
4
+ //
5
+ // `function Box(props) { return <View {...props} /> }` lowers at build time,
6
+ // but `props` still holds the primitive's vocabulary -- `accessibilityLabel`,
7
+ // `testID`, `accessibilityState` -- and spread raw onto the DOM those are
8
+ // unknown attributes: the element loses its accessible name, and a test that
9
+ // finds it by test id finds nothing. #1096 and #1118 were the same bug for
10
+ // Button and TextInput.
11
+ //
12
+ // So the spread goes through the translation the uncompiled `View` applies
13
+ // to its named props. Everything else passes through, as it did before:
14
+ // `aria-*`, `onKeyDown` and the rest are already DOM props. Keys the spread
15
+ // does not hold are not set, because explicit attributes the compiler wrote
16
+ // before the spread must survive it.
17
+ //
18
+ // `onLayout` and the responder props need the element and an effect, which a
19
+ // function called during render does not have (#1126). So when the spread
20
+ // carries one, the translation adds a callback ref: it is handed the element,
21
+ // and the cleanup React 19 runs when the element goes is the effect's.
22
+
23
+ import {
24
+ createSpreadResponder,
25
+ type HozoDomStyle,
26
+ hozoDomStyle,
27
+ RESPONDER_PROPS,
28
+ type ResponderProps,
29
+ } from '@hozo/engine'
30
+
31
+ type Props = Record<string, unknown>
32
+ type State = {
33
+ disabled?: boolean
34
+ selected?: boolean
35
+ checked?: boolean | 'mixed'
36
+ busy?: boolean
37
+ expanded?: boolean
38
+ }
39
+ type Value = { min?: number; max?: number; now?: number; text?: string }
40
+ type Source = string | number | { uri?: string; default?: string } | readonly Source[] | undefined
41
+
42
+ const UNIVERSAL = new Set([
43
+ 'accessibilityHint',
44
+ 'accessibilityLabel',
45
+ 'accessibilityLiveRegion',
46
+ 'accessibilityState',
47
+ 'accessibilityValue',
48
+ 'nativeID',
49
+ 'pointerEvents',
50
+ 'style',
51
+ 'testID',
52
+ ])
53
+
54
+ type LayoutEvent = {
55
+ nativeEvent: { layout: { x: number; y: number; width: number; height: number } }
56
+ }
57
+ type Attached = {
58
+ onLayout?: (event: LayoutEvent) => void
59
+ observer?: ResizeObserver
60
+ previous: string
61
+ // Set by the cleanup, cleared when the element is attached again.
62
+ leaving: boolean
63
+ }
64
+
65
+ // Per element, not per render: the ref is a new function each render, so
66
+ // React detaches the old one and attaches the new to the same element. The
67
+ // observer survives that, and so does the last layout reported -- as with
68
+ // the uncompiled View, `onLayout` fires when the layout changes, not on
69
+ // every render.
70
+ const attached = new WeakMap<HTMLElement, Attached>()
71
+
72
+ function observe(element: HTMLElement, state: Attached) {
73
+ const emit = () => {
74
+ const rect = element.getBoundingClientRect()
75
+ const layout = {
76
+ x: element.offsetLeft,
77
+ y: element.offsetTop,
78
+ width: rect.width,
79
+ height: rect.height,
80
+ }
81
+ const key = `${layout.x}:${layout.y}:${layout.width}:${layout.height}`
82
+ if (key === state.previous) return
83
+ state.previous = key
84
+ state.onLayout?.({ nativeEvent: { layout } })
85
+ }
86
+ emit()
87
+ if (typeof ResizeObserver === 'undefined') return
88
+ state.observer = new ResizeObserver(emit)
89
+ state.observer.observe(element)
90
+ }
91
+
92
+ type ElementRef =
93
+ | ((node: HTMLElement | null) => undefined | (() => void))
94
+ | { current: unknown }
95
+ | null
96
+ | undefined
97
+
98
+ /** The callback ref that gives a translated spread its element, or none when it needs none. */
99
+ function elementRef(
100
+ source: Props,
101
+ fallback?: string,
102
+ ):
103
+ | (((node: HTMLElement | null) => (() => void) | undefined) & {
104
+ responder?: ReturnType<typeof createSpreadResponder>
105
+ })
106
+ | undefined {
107
+ const onLayout = source.onLayout as Attached['onLayout']
108
+ const responderProps: ResponderProps = {}
109
+ for (const name of RESPONDER_PROPS) {
110
+ if (Object.hasOwn(source, name)) (responderProps as Props)[name] = source[name]
111
+ }
112
+ const responder =
113
+ Object.keys(responderProps).length > 0 ? createSpreadResponder(responderProps) : undefined
114
+ if (!onLayout && !responder && !fallback) return undefined
115
+ const forwarded = source.ref as ElementRef
116
+ return Object.assign(
117
+ (node: HTMLElement | null) => {
118
+ if (!node) return undefined
119
+ if (fallback) {
120
+ const img = node as HTMLImageElement
121
+ const currentSrc = img.getAttribute('src')
122
+ if (img.complete && img.naturalWidth === 0 && currentSrc !== fallback) {
123
+ if (typeof img.decode === 'function') {
124
+ img.decode().then(undefined, () => {
125
+ if (img.getAttribute('src') === currentSrc) img.src = fallback
126
+ })
127
+ } else {
128
+ img.src = fallback
129
+ }
130
+ }
131
+ }
132
+ let current: Attached | undefined
133
+ if (onLayout || responder) {
134
+ current = attached.get(node)
135
+ if (!current) {
136
+ current = { previous: '', leaving: false }
137
+ attached.set(node, current)
138
+ }
139
+ current.leaving = false
140
+ current.onLayout = onLayout
141
+ if (onLayout && !current.observer) observe(node, current)
142
+ responder?.attach(node)
143
+ }
144
+ let release: (() => void) | undefined
145
+ if (typeof forwarded === 'function') release = forwarded(node)
146
+ else if (forwarded) forwarded.current = node
147
+ return () => {
148
+ if (typeof release === 'function') release()
149
+ else if (typeof forwarded === 'function') forwarded(null)
150
+ else if (forwarded) forwarded.current = null
151
+ if (!current) return
152
+ // Not yet: a re-render detaches and reattaches the same element.
153
+ current.leaving = true
154
+ queueMicrotask(() => {
155
+ if (!current.leaving) return
156
+ current.observer?.disconnect()
157
+ attached.delete(node)
158
+ responder?.release(node)
159
+ })
160
+ }
161
+ },
162
+ { responder },
163
+ )
164
+ }
165
+
166
+ function translate(source: Props, consumed: Set<string>, fallback?: string): Props {
167
+ const has = (name: string) => Object.hasOwn(source, name)
168
+ const out: Props = {}
169
+ for (const name in source) {
170
+ if (has(name) && !consumed.has(name)) out[name] = source[name]
171
+ }
172
+
173
+ if (has('accessibilityLabel')) out['aria-label'] = source.accessibilityLabel
174
+ if (has('accessibilityHint')) out['aria-description'] = source.accessibilityHint
175
+ if (has('testID')) out['data-testid'] = source.testID
176
+ if (has('nativeID')) out.id = source.nativeID
177
+ if (has('pointerEvents')) out['data-hozo-pointer-events'] = source.pointerEvents
178
+ if (has('accessibilityLiveRegion')) {
179
+ const region = source.accessibilityLiveRegion
180
+ out['aria-live'] = region === 'none' ? undefined : region
181
+ }
182
+ if (has('accessibilityState')) {
183
+ const state = source.accessibilityState as State | undefined
184
+ out['data-hozo-disabled'] = state?.disabled ? '' : undefined
185
+ out['aria-disabled'] = state?.disabled
186
+ out['aria-selected'] = state?.selected
187
+ out['aria-checked'] = state?.checked
188
+ out['aria-busy'] = state?.busy
189
+ out['aria-expanded'] = state?.expanded
190
+ }
191
+ if (has('accessibilityValue')) {
192
+ const value = source.accessibilityValue as Value | undefined
193
+ out['aria-valuemin'] = value?.min
194
+ out['aria-valuemax'] = value?.max
195
+ out['aria-valuenow'] = value?.now
196
+ out['aria-valuetext'] = value?.text
197
+ }
198
+ if (has('style')) out.style = hozoDomStyle(source.style as HozoDomStyle)
199
+
200
+ const ref = elementRef(source, fallback)
201
+ if (ref) {
202
+ out.ref = ref
203
+ delete out.onLayout
204
+ for (const name of RESPONDER_PROPS) delete out[name]
205
+ // The pointer handlers the responder needs, after any the spread named.
206
+ Object.assign(out, ref.responder?.handlers)
207
+ }
208
+ return out
209
+ }
210
+
211
+ /** A View's, Text's or document primitive's spread onto its element. */
212
+ export function hozoViewDomProps<T>(props: T): T {
213
+ if (!props || typeof props !== 'object') return props
214
+ return translate(props as Props, UNIVERSAL) as T
215
+ }
216
+
217
+ const IMAGE = new Set([...UNIVERSAL, 'defaultSource', 'src'])
218
+
219
+ // The uncompiled Image's resolution. Its fallback to `defaultSource` after a
220
+ // load error is state there; here it is the `onError` and attach ref below.
221
+ function webSource(source: Source): string | undefined {
222
+ if (typeof source === 'string') return source
223
+ if (!source || typeof source !== 'object') return undefined
224
+ if (Array.isArray(source)) {
225
+ for (const candidate of source as readonly Source[]) {
226
+ const resolved = webSource(candidate)
227
+ if (resolved) return resolved
228
+ }
229
+ return undefined
230
+ }
231
+ const object = source as { uri?: string; default?: string }
232
+ return typeof object.uri === 'string'
233
+ ? object.uri
234
+ : typeof object.default === 'string'
235
+ ? object.default
236
+ : undefined
237
+ }
238
+
239
+ /**
240
+ * An Image's spread onto its `<img>`. `src` may be an object or a list of
241
+ * them, and an accessibility label is the image's `alt`, as it is uncompiled.
242
+ */
243
+ export function hozoImageDomProps<T>(props: T): T {
244
+ if (!props || typeof props !== 'object') return props
245
+ const source = props as Props
246
+ const primary = webSource(source.src as Source)
247
+ const fallback = webSource(source.defaultSource as Source)
248
+ const out = translate(source, IMAGE, primary && fallback ? fallback : undefined)
249
+ if (Object.hasOwn(source, 'src') || Object.hasOwn(source, 'defaultSource')) {
250
+ out.src = primary ?? fallback
251
+ }
252
+ if (Object.hasOwn(source, 'accessibilityLabel')) {
253
+ delete out['aria-label']
254
+ if (!Object.hasOwn(source, 'alt')) out.alt = source.accessibilityLabel
255
+ }
256
+ // A `src` that fails to load falls back to `defaultSource`, as it does
257
+ // uncompiled (#1126). Set on the element rather than kept in state: the
258
+ // next render writes the same `src` prop, which React leaves alone.
259
+ if (fallback && primary) {
260
+ const onError = source.onError as ((event: unknown) => void) | undefined
261
+ out.onError = (event: { currentTarget: HTMLImageElement }) => {
262
+ if (event.currentTarget.getAttribute('src') !== fallback) event.currentTarget.src = fallback
263
+ onError?.(event)
264
+ }
265
+ }
266
+ return out as T
267
+ }
@@ -0,0 +1,83 @@
1
+ // Decision 013's two runtime pieces. Nearly every Native file emits the
2
+ // reader, and an app of any size has a scope somewhere, so they ship together.
3
+ import {
4
+ HozoInheritedMetricsContext,
5
+ HozoInheritedPaintContext,
6
+ type HozoInheritedTextOwn,
7
+ useHozoInheritedTextFrame,
8
+ } from '@hozo/engine/generated/text-inheritance-protocol'
9
+ import { type ComponentPropsWithRef, type ReactNode, useContext } from 'react'
10
+ import { type StyleProp, StyleSheet, Text, type TextProps, type TextStyle } from 'react-native'
11
+ import { HozoInheritedTextScope } from './inherited-text-scope.tsx'
12
+
13
+ /**
14
+ * @internal Generated ABI (decision 013). An outermost Text whose children
15
+ * the compiler can see: strings and nested Text, which React Native itself
16
+ * inherits from this host. Nothing below reads a frame, so none is published.
17
+ *
18
+ * It never merges or validates its own style: the inherited values go first and the element's own style last, so whatever the
19
+ * element sets -- an Animated value, a platform colour, a font family the
20
+ * protocol does not carry -- wins as written. This is the default route, and
21
+ * a value the protocol cannot read must not throw in an application.
22
+ */
23
+ export function HozoInheritedLeafText({
24
+ style,
25
+ children,
26
+ hozoOpaque,
27
+ ...props
28
+ }: ComponentPropsWithRef<typeof Text> & { hozoOpaque?: boolean }) {
29
+ const metrics = useContext(HozoInheritedMetricsContext)
30
+ const paint = useContext(HozoInheritedPaintContext)
31
+ // Nothing published above: the host gets exactly the style a plain Text
32
+ // would, so a tree with no text-styled container renders as it did.
33
+ const bare = isEmpty(metrics) && isEmpty(paint)
34
+ return (
35
+ <Text
36
+ {...props}
37
+ style={bare ? style : [metrics as TextProps['style'], paint as TextProps['style'], style]}
38
+ >
39
+ {hozoOpaque ? (
40
+ // A Text rendered by a child the compiler could not see inherits from
41
+ // this host natively. It must not also apply the frame from outside,
42
+ // which would put the outer value back over this host's own.
43
+ <HozoInheritedMetricsContext.Provider value={EMPTY}>
44
+ <HozoInheritedPaintContext.Provider value={EMPTY}>
45
+ {children}
46
+ </HozoInheritedPaintContext.Provider>
47
+ </HozoInheritedMetricsContext.Provider>
48
+ ) : (
49
+ children
50
+ )}
51
+ </Text>
52
+ )
53
+ }
54
+ // A pressable hands its text styles to a direct label (#876). The reader is
55
+ // the compiled form of the `Text` that label is, so it must stay one.
56
+ ;(HozoInheritedLeafText as { hozoLabelText?: boolean }).hozoLabelText = true
57
+
58
+ const EMPTY = Object.freeze({})
59
+
60
+ function isEmpty(record: object): boolean {
61
+ for (const _ in record) return false
62
+ return true
63
+ }
64
+
65
+ /**
66
+ * @internal Generated ABI (decision 013). Publishes a text-styled container's
67
+ * declarations, merged over the inherited frame, to children the compiler
68
+ * cannot see. Renders no host: the container keeps its own View.
69
+ *
70
+ * Its style is the compiler's, built only from the seven fields' plain values,
71
+ * so it is read as is rather than validated like an author's.
72
+ */
73
+ export function HozoInheritedScope({
74
+ style,
75
+ children,
76
+ }: {
77
+ style?: StyleProp<TextStyle>
78
+ children?: ReactNode
79
+ }) {
80
+ const own = (StyleSheet.flatten(style) ?? EMPTY) as HozoInheritedTextOwn
81
+ const frame = useHozoInheritedTextFrame(own)
82
+ return <HozoInheritedTextScope frame={frame}>{children}</HozoInheritedTextScope>
83
+ }