@hozo/primitives 0.3.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.
- package/CHANGELOG.md +44 -0
- package/README.md +1 -1
- package/dist/button-host-attributes.d.ts +20 -0
- package/dist/button-host-attributes.d.ts.map +1 -0
- package/dist/button-host-attributes.js +47 -0
- package/dist/button-host-attributes.js.map +1 -0
- package/dist/button-host-props.native.d.ts +8 -0
- package/dist/button-host-props.native.d.ts.map +1 -0
- package/dist/button-host-props.native.js +20 -0
- package/dist/button-host-props.native.js.map +1 -0
- package/dist/foundation.d.ts +21 -3
- package/dist/foundation.d.ts.map +1 -1
- package/dist/foundation.js +41 -5
- package/dist/foundation.js.map +1 -1
- package/dist/foundation.native.d.ts +27 -7
- package/dist/foundation.native.d.ts.map +1 -1
- package/dist/foundation.native.js +18 -3
- package/dist/foundation.native.js.map +1 -1
- package/dist/generated/button-dom-props.d.ts +11 -0
- package/dist/generated/button-dom-props.d.ts.map +1 -0
- package/dist/generated/button-dom-props.js +132 -0
- package/dist/generated/button-dom-props.js.map +1 -0
- package/dist/generated/button-props.d.ts +3 -0
- package/dist/generated/button-props.d.ts.map +1 -0
- package/dist/generated/button-props.js +5 -0
- package/dist/generated/button-props.js.map +1 -0
- package/dist/generated/button-props.native.d.ts +2 -0
- package/dist/generated/button-props.native.d.ts.map +1 -0
- package/dist/generated/button-props.native.js +2 -0
- package/dist/generated/button-props.native.js.map +1 -0
- package/dist/generated/inherited-label.d.ts +2 -0
- package/dist/generated/inherited-label.d.ts.map +1 -0
- package/dist/generated/inherited-label.js +3 -0
- package/dist/generated/inherited-label.js.map +1 -0
- package/dist/generated/inherited-pressable.d.ts +2 -0
- package/dist/generated/inherited-pressable.d.ts.map +1 -0
- package/dist/generated/inherited-pressable.js +3 -0
- package/dist/generated/inherited-pressable.js.map +1 -0
- package/dist/generated/inherited-relative-text.d.ts +2 -0
- package/dist/generated/inherited-relative-text.d.ts.map +1 -0
- package/dist/generated/inherited-relative-text.js +3 -0
- package/dist/generated/inherited-relative-text.js.map +1 -0
- package/dist/generated/inherited-text.d.ts +2 -0
- package/dist/generated/inherited-text.d.ts.map +1 -0
- package/dist/generated/inherited-text.js +3 -0
- package/dist/generated/inherited-text.js.map +1 -0
- package/dist/generated/inherited-view.d.ts +2 -0
- package/dist/generated/inherited-view.d.ts.map +1 -0
- package/dist/generated/inherited-view.js +3 -0
- package/dist/generated/inherited-view.js.map +1 -0
- package/dist/generated/native-disabled.d.ts +2 -0
- package/dist/generated/native-disabled.d.ts.map +1 -0
- package/dist/generated/native-disabled.js +2 -0
- package/dist/generated/native-disabled.js.map +1 -0
- package/dist/generated/text-input-dom-props.d.ts +7 -0
- package/dist/generated/text-input-dom-props.d.ts.map +1 -0
- package/dist/generated/text-input-dom-props.js +130 -0
- package/dist/generated/text-input-dom-props.js.map +1 -0
- package/dist/generated/text-input.native.d.ts +1 -1
- package/dist/generated/text-input.native.d.ts.map +1 -1
- package/dist/generated/text-input.native.js +1 -1
- package/dist/generated/text-input.native.js.map +1 -1
- package/dist/generated/view-dom-props.d.ts +8 -0
- package/dist/generated/view-dom-props.d.ts.map +1 -0
- package/dist/generated/view-dom-props.js +241 -0
- package/dist/generated/view-dom-props.js.map +1 -0
- package/dist/inherited-label.native.d.ts +8 -0
- package/dist/inherited-label.native.d.ts.map +1 -0
- package/dist/inherited-label.native.js +12 -0
- package/dist/inherited-label.native.js.map +1 -0
- package/dist/inherited-pressable.native.d.ts +11 -0
- package/dist/inherited-pressable.native.d.ts.map +1 -0
- package/dist/inherited-pressable.native.js +75 -0
- package/dist/inherited-pressable.native.js.map +1 -0
- package/dist/inherited-relative-text.native.d.ts +13 -0
- package/dist/inherited-relative-text.native.d.ts.map +1 -0
- package/dist/inherited-relative-text.native.js +16 -0
- package/dist/inherited-relative-text.native.js.map +1 -0
- package/dist/inherited-text-scope.d.ts +8 -0
- package/dist/inherited-text-scope.d.ts.map +1 -0
- package/dist/inherited-text-scope.js +10 -0
- package/dist/inherited-text-scope.js.map +1 -0
- package/dist/inherited-text-style.native.d.ts +5 -0
- package/dist/inherited-text-style.native.d.ts.map +1 -0
- package/dist/inherited-text-style.native.js +30 -0
- package/dist/inherited-text-style.native.js.map +1 -0
- package/dist/inherited-text.native.d.ts +5 -0
- package/dist/inherited-text.native.d.ts.map +1 -0
- package/dist/inherited-text.native.js +18 -0
- package/dist/inherited-text.native.js.map +1 -0
- package/dist/inherited-view.native.d.ts +9 -0
- package/dist/inherited-view.native.d.ts.map +1 -0
- package/dist/inherited-view.native.js +17 -0
- package/dist/inherited-view.native.js.map +1 -0
- package/dist/link.d.ts +4 -3
- package/dist/link.d.ts.map +1 -1
- package/dist/link.js +9 -1
- package/dist/link.js.map +1 -1
- package/dist/link.native.d.ts +1 -1
- package/dist/link.native.d.ts.map +1 -1
- package/dist/link.native.js +7 -1
- package/dist/link.native.js.map +1 -1
- package/dist/native-disabled.d.ts +13 -0
- package/dist/native-disabled.d.ts.map +1 -0
- package/dist/native-disabled.js +13 -0
- package/dist/native-disabled.js.map +1 -0
- package/dist/presence.native.d.ts.map +1 -1
- package/dist/presence.native.js +1 -1
- package/dist/presence.native.js.map +1 -1
- package/dist/pressable.d.ts.map +1 -1
- package/dist/pressable.js +8 -1
- package/dist/pressable.js.map +1 -1
- package/dist/pressable.native.d.ts +10 -1
- package/dist/pressable.native.d.ts.map +1 -1
- package/dist/pressable.native.js +44 -45
- package/dist/pressable.native.js.map +1 -1
- package/dist/text-color-transition.native.d.ts +12 -0
- package/dist/text-color-transition.native.d.ts.map +1 -0
- package/dist/text-color-transition.native.js +87 -0
- package/dist/text-color-transition.native.js.map +1 -0
- package/dist/text-input-props.native.d.ts +10 -0
- package/dist/text-input-props.native.d.ts.map +1 -0
- package/dist/text-input-props.native.js +15 -0
- package/dist/text-input-props.native.js.map +1 -0
- package/dist/text-input.native.d.ts +19 -0
- package/dist/text-input.native.d.ts.map +1 -0
- package/dist/text-input.native.js +32 -0
- package/dist/text-input.native.js.map +1 -0
- package/package.json +51 -7
- package/src/button-host-attributes.ts +56 -0
- package/src/button-host-props.native.ts +22 -0
- package/src/foundation.native.tsx +45 -6
- package/src/foundation.tsx +74 -4
- package/src/generated/button-dom-props.ts +127 -0
- package/src/generated/button-props.native.ts +1 -0
- package/src/generated/button-props.ts +4 -0
- package/src/generated/inherited-label.ts +2 -0
- package/src/generated/inherited-pressable.ts +2 -0
- package/src/generated/inherited-relative-text.ts +2 -0
- package/src/generated/inherited-text.ts +2 -0
- package/src/generated/inherited-view.ts +2 -0
- package/src/generated/native-disabled.ts +1 -0
- package/src/generated/text-input-dom-props.ts +128 -0
- package/src/generated/text-input.native.ts +1 -4
- package/src/generated/view-dom-props.ts +267 -0
- package/src/inherited-label.native.tsx +28 -0
- package/src/inherited-pressable.native.tsx +152 -0
- package/src/inherited-relative-text.native.tsx +22 -0
- package/src/inherited-text-scope.tsx +27 -0
- package/src/inherited-text-style.native.ts +35 -0
- package/src/inherited-text.native.tsx +32 -0
- package/src/inherited-view.native.tsx +37 -0
- package/src/link.native.tsx +17 -13
- package/src/link.ts +12 -0
- package/src/native-disabled.ts +23 -0
- package/src/presence.native.tsx +1 -1
- package/src/pressable.native.tsx +70 -42
- package/src/pressable.ts +8 -1
- package/src/text-color-transition.native.ts +96 -0
- package/src/text-input-props.native.ts +26 -0
- package/src/text-input.native.tsx +57 -0
- package/dist/generated/animated.d.ts +0 -2
- package/dist/generated/animated.d.ts.map +0 -1
- package/dist/generated/animated.js +0 -3
- package/dist/generated/animated.js.map +0 -1
- package/dist/presence-context.d.ts +0 -15
- package/dist/presence-context.d.ts.map +0 -1
- package/dist/presence-context.js +0 -11
- package/dist/presence-context.js.map +0 -1
- package/dist/runtime-transition.native.d.ts +0 -30
- package/dist/runtime-transition.native.d.ts.map +0 -1
- package/dist/runtime-transition.native.js +0 -183
- package/dist/runtime-transition.native.js.map +0 -1
- package/src/generated/animated.ts +0 -6
- package/src/presence-context.ts +0 -27
- package/src/runtime-transition.native.tsx +0 -244
|
@@ -0,0 +1,127 @@
|
|
|
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
|
+
const prevent = (event: { preventDefault(): void }) => event.preventDefault()
|
|
20
|
+
|
|
21
|
+
function common(props: Props, out: Props, name: string, value: unknown): boolean {
|
|
22
|
+
switch (name) {
|
|
23
|
+
case 'accessibilityLabel':
|
|
24
|
+
out['aria-label'] = value
|
|
25
|
+
return true
|
|
26
|
+
case 'accessibilityHint':
|
|
27
|
+
out['aria-description'] = value
|
|
28
|
+
return true
|
|
29
|
+
case 'testID':
|
|
30
|
+
out['data-testid'] = value
|
|
31
|
+
return true
|
|
32
|
+
case 'nativeID':
|
|
33
|
+
out.id = value
|
|
34
|
+
return true
|
|
35
|
+
case 'className':
|
|
36
|
+
case 'children':
|
|
37
|
+
// What a wrapper writes to make the Button a trigger: `Tooltip` clones
|
|
38
|
+
// its child with these (#1137). The uncompiled Button forwards the same
|
|
39
|
+
// list, `BUTTON_TRIGGER_PROPS_WEB`.
|
|
40
|
+
case 'ref':
|
|
41
|
+
case 'onFocus':
|
|
42
|
+
case 'onBlur':
|
|
43
|
+
case 'onKeyDown':
|
|
44
|
+
case 'onPointerEnter':
|
|
45
|
+
case 'onPointerLeave':
|
|
46
|
+
case 'aria-describedby':
|
|
47
|
+
out[name] = value
|
|
48
|
+
return true
|
|
49
|
+
case 'style':
|
|
50
|
+
out.style = hozoDomStyle(value as HozoDomStyle)
|
|
51
|
+
return true
|
|
52
|
+
}
|
|
53
|
+
if (name.startsWith('data-')) {
|
|
54
|
+
out[name] = props[name]
|
|
55
|
+
return true
|
|
56
|
+
}
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A Button's spread onto the `<button>` the compiler chose. */
|
|
61
|
+
export function hozoButtonDomProps<T>(props: T): T {
|
|
62
|
+
if (!props || typeof props !== 'object') return props
|
|
63
|
+
const source = props as Props
|
|
64
|
+
const out: Props = {}
|
|
65
|
+
for (const name in source) {
|
|
66
|
+
if (!Object.hasOwn(source, name)) continue
|
|
67
|
+
const value = source[name]
|
|
68
|
+
if (common(source, out, name, value)) continue
|
|
69
|
+
if (name === 'tabIndex') {
|
|
70
|
+
// A roving tab stop, as `Toolbar` hands its items (BUTTON_TRIGGER_PROPS_WEB).
|
|
71
|
+
out.tabIndex = value
|
|
72
|
+
} else if (name === 'onPress') {
|
|
73
|
+
out.onClick = value
|
|
74
|
+
if (value) out['data-hozo-pressable'] = ''
|
|
75
|
+
} else if (name === 'disabled') {
|
|
76
|
+
out.disabled = value
|
|
77
|
+
out['data-hozo-disabled'] = value ? '' : undefined
|
|
78
|
+
}
|
|
79
|
+
// Anything else -- link-only props with no href, unknown keys -- the
|
|
80
|
+
// uncompiled Button does not forward either.
|
|
81
|
+
}
|
|
82
|
+
return out as T
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* A Button's spread onto the `<a>` the compiler chose because it was given
|
|
87
|
+
* an `href`. An anchor has no disabled state, so a press is suppressed
|
|
88
|
+
* while disabled, as the compiler does for a named `onPress`. `disabled` is
|
|
89
|
+
* the one the compiler saw named on the element, if any; one inside the
|
|
90
|
+
* spread counts too.
|
|
91
|
+
*/
|
|
92
|
+
export function hozoLinkDomProps<T>(props: T, disabled?: unknown): T {
|
|
93
|
+
if (!props || typeof props !== 'object') return props
|
|
94
|
+
const source = props as Props
|
|
95
|
+
const off = Boolean(disabled ?? source.disabled)
|
|
96
|
+
const out: Props = {}
|
|
97
|
+
for (const name in source) {
|
|
98
|
+
if (!Object.hasOwn(source, name)) continue
|
|
99
|
+
const value = source[name]
|
|
100
|
+
if (common(source, out, name, value)) continue
|
|
101
|
+
switch (name) {
|
|
102
|
+
case 'onPress':
|
|
103
|
+
out.onClick = off ? prevent : value
|
|
104
|
+
break
|
|
105
|
+
case 'tabIndex':
|
|
106
|
+
// A roving tab stop, but never one while disabled.
|
|
107
|
+
out.tabIndex = off ? -1 : value
|
|
108
|
+
break
|
|
109
|
+
case 'disabled':
|
|
110
|
+
out['aria-disabled'] = value ? true : undefined
|
|
111
|
+
out['data-hozo-disabled'] = value ? '' : undefined
|
|
112
|
+
if (value) out.tabIndex = -1
|
|
113
|
+
else if (!Object.hasOwn(source, 'tabIndex')) out.tabIndex = undefined
|
|
114
|
+
if (value) {
|
|
115
|
+
out.onClick ??= prevent
|
|
116
|
+
out.onAuxClick = prevent
|
|
117
|
+
}
|
|
118
|
+
break
|
|
119
|
+
case 'target':
|
|
120
|
+
case 'rel':
|
|
121
|
+
case 'download':
|
|
122
|
+
out[name] = value
|
|
123
|
+
break
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return out as T
|
|
127
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { hozoButtonProps } from '../button-host-props.native.ts'
|
|
@@ -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
|
+
}
|
|
@@ -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,28 @@
|
|
|
1
|
+
import { useHozoInheritedTextFrame } from '@hozo/engine/generated/text-inheritance-protocol'
|
|
2
|
+
import { Children, type ReactNode } from 'react'
|
|
3
|
+
import type { StyleProp, TextStyle } from 'react-native'
|
|
4
|
+
import { HozoInheritedText } from './inherited-text.native.tsx'
|
|
5
|
+
import { HozoInheritedTextScope } from './inherited-text-scope.tsx'
|
|
6
|
+
import { flattenInheritedTextStyle } from './inherited-text-style.native.ts'
|
|
7
|
+
|
|
8
|
+
/** A pattern supplies already-resolved label styles, not classes or state. */
|
|
9
|
+
export function HozoInheritedLabel({
|
|
10
|
+
style,
|
|
11
|
+
children,
|
|
12
|
+
}: {
|
|
13
|
+
style?: StyleProp<TextStyle>
|
|
14
|
+
children?: ReactNode
|
|
15
|
+
}) {
|
|
16
|
+
const frame = useHozoInheritedTextFrame(flattenInheritedTextStyle(style))
|
|
17
|
+
return (
|
|
18
|
+
<HozoInheritedTextScope frame={frame}>
|
|
19
|
+
{Children.map(children, (child) =>
|
|
20
|
+
typeof child === 'string' || typeof child === 'number' ? (
|
|
21
|
+
<HozoInheritedText style={style}>{child}</HozoInheritedText>
|
|
22
|
+
) : (
|
|
23
|
+
child
|
|
24
|
+
),
|
|
25
|
+
)}
|
|
26
|
+
</HozoInheritedTextScope>
|
|
27
|
+
)
|
|
28
|
+
}
|