@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.
- package/CHANGELOG.md +67 -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 +45 -4
- package/dist/foundation.d.ts.map +1 -1
- package/dist/foundation.js +52 -6
- package/dist/foundation.js.map +1 -1
- package/dist/foundation.native.d.ts +34 -7
- package/dist/foundation.native.d.ts.map +1 -1
- package/dist/foundation.native.js +21 -5
- 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 +155 -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-leaves.d.ts +2 -0
- package/dist/generated/inherited-leaves.d.ts.map +1 -0
- package/dist/generated/inherited-leaves.js +4 -0
- package/dist/generated/inherited-leaves.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/text.d.ts +1 -1
- package/dist/generated/text.d.ts.map +1 -1
- package/dist/generated/text.js +4 -1
- package/dist/generated/text.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-leaves.native.d.ts +28 -0
- package/dist/inherited-leaves.native.d.ts.map +1 -0
- package/dist/inherited-leaves.native.js +49 -0
- package/dist/inherited-leaves.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/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 +23 -2
- package/dist/pressable.native.d.ts.map +1 -1
- package/dist/pressable.native.js +57 -46
- 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 +35 -7
- package/src/button-host-attributes.ts +56 -0
- package/src/button-host-props.native.ts +22 -0
- package/src/foundation.native.tsx +59 -8
- package/src/foundation.tsx +119 -5
- package/src/generated/button-dom-props.ts +159 -0
- package/src/generated/button-props.native.ts +1 -0
- package/src/generated/button-props.ts +4 -0
- package/src/generated/inherited-leaves.ts +3 -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/text.ts +5 -0
- package/src/generated/view-dom-props.ts +267 -0
- package/src/inherited-leaves.native.tsx +83 -0
- package/src/inherited-text-scope.tsx +27 -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 +90 -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,27 @@
|
|
|
1
|
+
import {
|
|
2
|
+
HozoInheritedMetricsContext,
|
|
3
|
+
HozoInheritedPaintContext,
|
|
4
|
+
type HozoInheritedTextFrame,
|
|
5
|
+
} from '@hozo/engine/generated/text-inheritance-protocol'
|
|
6
|
+
import { HozoTextSizeContext } from '@hozo/engine/generated/text-size-context'
|
|
7
|
+
import { type ReactNode, useContext } from 'react'
|
|
8
|
+
|
|
9
|
+
/** One stable shape, even when every local declaration is removed. */
|
|
10
|
+
export function HozoInheritedTextScope({
|
|
11
|
+
frame,
|
|
12
|
+
children,
|
|
13
|
+
}: {
|
|
14
|
+
frame: HozoInheritedTextFrame
|
|
15
|
+
children?: ReactNode
|
|
16
|
+
}) {
|
|
17
|
+
const legacy = useContext(HozoTextSizeContext)
|
|
18
|
+
return (
|
|
19
|
+
<HozoInheritedMetricsContext.Provider value={frame.metrics}>
|
|
20
|
+
<HozoInheritedPaintContext.Provider value={frame.paint}>
|
|
21
|
+
<HozoTextSizeContext.Provider value={frame.metrics.fontSize ?? legacy}>
|
|
22
|
+
{children}
|
|
23
|
+
</HozoTextSizeContext.Provider>
|
|
24
|
+
</HozoInheritedPaintContext.Provider>
|
|
25
|
+
</HozoInheritedMetricsContext.Provider>
|
|
26
|
+
)
|
|
27
|
+
}
|
package/src/link.native.tsx
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
} from '@hozo/engine/navigation'
|
|
7
7
|
import { type ComponentPropsWithRef, type ElementType, type ReactNode, useRef } from 'react'
|
|
8
8
|
import { Linking, Pressable, type PressableProps } from 'react-native'
|
|
9
|
+
import { nativeDisabledProps } from './native-disabled.ts'
|
|
9
10
|
import type { HozoPressable } from './pressable.native.tsx'
|
|
10
11
|
|
|
11
12
|
type LinkHost = typeof Pressable | typeof HozoPressable
|
|
@@ -47,19 +48,20 @@ export type HozoLinkProps<Host extends LinkHost = typeof Pressable> =
|
|
|
47
48
|
* or navigation. The type-only import keeps that machinery out of ordinary
|
|
48
49
|
* links' Metro graph, where unused static imports would still cost modules.
|
|
49
50
|
*/
|
|
50
|
-
export function HozoLink<Host extends LinkHost = typeof Pressable>({
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
51
|
+
export function HozoLink<Host extends LinkHost = typeof Pressable>(input: HozoLinkProps<Host>) {
|
|
52
|
+
const {
|
|
53
|
+
hozoLinkComponent,
|
|
54
|
+
href,
|
|
55
|
+
external,
|
|
56
|
+
replace,
|
|
57
|
+
prefetch,
|
|
58
|
+
onPress,
|
|
59
|
+
onPressIn,
|
|
60
|
+
disabled,
|
|
61
|
+
children,
|
|
62
|
+
accessibilityRole = 'link',
|
|
63
|
+
...props
|
|
64
|
+
} = nativeDisabledProps(input)
|
|
63
65
|
const Component: ElementType = hozoLinkComponent ?? Pressable
|
|
64
66
|
const navigation = useHozoNavigation()
|
|
65
67
|
const prefetchedHref = useRef<string | null>(null)
|
|
@@ -69,6 +71,7 @@ export function HozoLink<Host extends LinkHost = typeof Pressable>({
|
|
|
69
71
|
disabled={disabled}
|
|
70
72
|
accessibilityRole={accessibilityRole}
|
|
71
73
|
onPressIn={(event: Parameters<NonNullable<PressableProps['onPressIn']>>[0]) => {
|
|
74
|
+
if (disabled) return
|
|
72
75
|
onPressIn?.(event)
|
|
73
76
|
if (prefetch && !disabled && prefetchedHref.current !== href) {
|
|
74
77
|
prefetchedHref.current = href
|
|
@@ -76,6 +79,7 @@ export function HozoLink<Host extends LinkHost = typeof Pressable>({
|
|
|
76
79
|
}
|
|
77
80
|
}}
|
|
78
81
|
onPress={(event: Parameters<NonNullable<PressableProps['onPress']>>[0]) => {
|
|
82
|
+
if (disabled) return
|
|
79
83
|
onPress?.(event)
|
|
80
84
|
if (!event.defaultPrevented) {
|
|
81
85
|
void activateHozoNavigation(navigation, { href, external, replace }, Linking.openURL)
|
package/src/link.ts
CHANGED
|
@@ -46,6 +46,7 @@ export interface HozoLinkProps
|
|
|
46
46
|
accessibilityHint?: string
|
|
47
47
|
accessibilityRole?: string
|
|
48
48
|
disabled?: boolean
|
|
49
|
+
focusable?: boolean
|
|
49
50
|
/** Leaves the app: `_blank` and a `rel` that severs `window.opener`. */
|
|
50
51
|
external?: boolean
|
|
51
52
|
/** Ask an installed router to replace its current history entry. */
|
|
@@ -71,6 +72,9 @@ export function HozoLink({
|
|
|
71
72
|
accessibilityHint,
|
|
72
73
|
accessibilityRole,
|
|
73
74
|
disabled,
|
|
75
|
+
focusable,
|
|
76
|
+
tabIndex,
|
|
77
|
+
onAuxClick,
|
|
74
78
|
external,
|
|
75
79
|
replace,
|
|
76
80
|
prefetch,
|
|
@@ -104,6 +108,11 @@ export function HozoLink({
|
|
|
104
108
|
// There is no `disabled` on an anchor. The pair below is what a
|
|
105
109
|
// disabled link is: announced as unavailable, and not followed.
|
|
106
110
|
'aria-disabled': disabled ? true : undefined,
|
|
111
|
+
// HTML anchors have no disabled state. Do not let even an explicit
|
|
112
|
+
// positive tabindex put an unavailable destination in the Tab order.
|
|
113
|
+
tabIndex: disabled
|
|
114
|
+
? -1
|
|
115
|
+
: (tabIndex ?? (focusable === undefined ? undefined : focusable ? 0 : -1)),
|
|
107
116
|
// `disabled:` compiles to `[data-hozo-disabled]` rather than to
|
|
108
117
|
// `:disabled`, which matches form controls only. Without the
|
|
109
118
|
// attribute the utility silently stops matching on this element
|
|
@@ -119,6 +128,9 @@ export function HozoLink({
|
|
|
119
128
|
event.preventDefault()
|
|
120
129
|
}
|
|
121
130
|
: onPress,
|
|
131
|
+
onAuxClick: disabled
|
|
132
|
+
? (event: React.MouseEvent<HTMLAnchorElement>) => event.preventDefault()
|
|
133
|
+
: onAuxClick,
|
|
122
134
|
},
|
|
123
135
|
children,
|
|
124
136
|
)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AccessibilityState } from 'react-native'
|
|
2
|
+
|
|
3
|
+
interface DisabledOwner {
|
|
4
|
+
disabled?: boolean | null
|
|
5
|
+
accessibilityState?: AccessibilityState | null
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
type DisabledResult<T extends DisabledOwner> = Omit<T, keyof DisabledOwner> & {
|
|
9
|
+
disabled?: T['disabled'] | boolean
|
|
10
|
+
accessibilityState?: T['accessibilityState'] | AccessibilityState
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Keep behavior and announcement on the same canonical unavailable fact. */
|
|
14
|
+
export function nativeDisabledProps<T extends DisabledOwner>(props: T): DisabledResult<T> {
|
|
15
|
+
const state = props.accessibilityState
|
|
16
|
+
const flag = props.disabled
|
|
17
|
+
const stateDisabled = state?.disabled === true
|
|
18
|
+
const disabled = Boolean(flag || stateDisabled)
|
|
19
|
+
// Ordinary enabled controls and already-normalized compiled props need
|
|
20
|
+
// neither a new props object nor a second accessibility-state object.
|
|
21
|
+
if (!disabled || (flag === true && stateDisabled)) return props
|
|
22
|
+
return { ...props, disabled, accessibilityState: stateDisabled ? state : { ...state, disabled } }
|
|
23
|
+
}
|
package/src/presence.native.tsx
CHANGED
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
// The Web half is `./presence.tsx`. Both are written against the same
|
|
9
9
|
// class: `data-[state=closed]:`.
|
|
10
10
|
|
|
11
|
+
import { PresenceContext, type PresenceContextValue } from '@hozo/engine/generated/animated'
|
|
11
12
|
import { type ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
|
12
|
-
import { PresenceContext, type PresenceContextValue } from './presence-context.ts'
|
|
13
13
|
|
|
14
14
|
export interface PresenceProps {
|
|
15
15
|
/** Whether the child is shown. Turning it false starts the exit. */
|
package/src/pressable.native.tsx
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
HozoInteractionContext,
|
|
3
|
+
usePressableInteraction,
|
|
4
|
+
} from '@hozo/behaviors/pressable-interaction'
|
|
2
5
|
import { blendColor } from '@hozo/engine'
|
|
3
6
|
import {
|
|
7
|
+
Children,
|
|
8
|
+
type Context,
|
|
9
|
+
cloneElement,
|
|
4
10
|
createContext,
|
|
5
11
|
type ElementType,
|
|
12
|
+
isValidElement,
|
|
13
|
+
type ReactElement,
|
|
6
14
|
type ReactNode,
|
|
7
15
|
useCallback,
|
|
8
16
|
useContext,
|
|
@@ -26,8 +34,12 @@ import {
|
|
|
26
34
|
Text,
|
|
27
35
|
type TextProps,
|
|
28
36
|
type TextStyle,
|
|
37
|
+
View,
|
|
38
|
+
type ViewProps,
|
|
29
39
|
type ViewStyle,
|
|
30
40
|
} from 'react-native'
|
|
41
|
+
import { nativeDisabledProps } from './native-disabled.ts'
|
|
42
|
+
import { useHozoTextColorTransition } from './text-color-transition.native.ts'
|
|
31
43
|
|
|
32
44
|
export interface HozoTransition {
|
|
33
45
|
duration: number
|
|
@@ -49,6 +61,43 @@ export interface HozoPressableProps extends Omit<PressableProps, 'style'> {
|
|
|
49
61
|
hozoTransition?: HozoTransition
|
|
50
62
|
/** Enables per-element pointer/keyboard modality inference. */
|
|
51
63
|
hozoFocusVisible?: boolean
|
|
64
|
+
/**
|
|
65
|
+
* The text styles this element's class list carries for its label, by
|
|
66
|
+
* state -- what a `<button>` passes to its text on the Web (#876). The
|
|
67
|
+
* compiler writes it when the label is not in the source it can see, so
|
|
68
|
+
* it arrives as `children` and is styled here: a string is put in a
|
|
69
|
+
* `Text`, and a direct `Text` child has these merged ahead of its own.
|
|
70
|
+
* A `Text` deeper inside is not reached (#950).
|
|
71
|
+
*/
|
|
72
|
+
hozoTextStyle?: (state: HozoPressableState) => StyleProp<TextStyle>
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A component a label can be: React Native's `Text`, `HozoText`, or one marked so. */
|
|
76
|
+
function isLabelText(child: ReactElement): boolean {
|
|
77
|
+
const type = child.type as { hozoLabelText?: boolean } | string
|
|
78
|
+
return (
|
|
79
|
+
type === Text || type === HozoText || (typeof type !== 'string' && type?.hozoLabelText === true)
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** `children` with `label` handed to the text among them, one level deep. */
|
|
84
|
+
function withLabelStyle(children: ReactNode, label: StyleProp<TextStyle>): ReactNode {
|
|
85
|
+
return Children.map(children, (child) => {
|
|
86
|
+
if (typeof child === 'string' || typeof child === 'number') {
|
|
87
|
+
return <Text style={label}>{child}</Text>
|
|
88
|
+
}
|
|
89
|
+
if (!isValidElement<{ style?: unknown }>(child) || !isLabelText(child)) return child
|
|
90
|
+
const own = child.props.style
|
|
91
|
+
return cloneElement(child, {
|
|
92
|
+
style:
|
|
93
|
+
typeof own === 'function'
|
|
94
|
+
? (state: HozoPressableState) => [
|
|
95
|
+
label,
|
|
96
|
+
(own as (s: HozoPressableState) => unknown)(state),
|
|
97
|
+
]
|
|
98
|
+
: [label, own],
|
|
99
|
+
} as { style: unknown })
|
|
100
|
+
})
|
|
52
101
|
}
|
|
53
102
|
|
|
54
103
|
const AnimatedPressable = Animated.createAnimatedComponent(Pressable)
|
|
@@ -87,7 +136,8 @@ export function pressableFor(transition: HozoTransition | undefined): ElementTyp
|
|
|
87
136
|
}
|
|
88
137
|
const AnimatedText = Animated.createAnimatedComponent(Text)
|
|
89
138
|
type InteractionContextValue = HozoPressableState & { transition?: HozoTransition }
|
|
90
|
-
|
|
139
|
+
// Shared with `@hozo/patterns`' pressable, which provides it too (#974).
|
|
140
|
+
const InteractionContext = HozoInteractionContext as Context<InteractionContextValue | null>
|
|
91
141
|
type AnimationDriver = { current: ((state: HozoPressableState) => void) | null }
|
|
92
142
|
const AnimationDriverContext = createContext<AnimationDriver | null>(null)
|
|
93
143
|
|
|
@@ -193,8 +243,11 @@ export function HozoPressable({
|
|
|
193
243
|
onKeyDown,
|
|
194
244
|
hozoTransition,
|
|
195
245
|
hozoFocusVisible = false,
|
|
246
|
+
hozoTextStyle,
|
|
247
|
+
children,
|
|
196
248
|
...props
|
|
197
249
|
}: HozoPressableProps) {
|
|
250
|
+
props = nativeDisabledProps(props)
|
|
198
251
|
// Keep the event/context owner mounted when motion is added or removed.
|
|
199
252
|
// Only the optional animation child owns nodes and progress listeners.
|
|
200
253
|
const driver = useRef<((state: HozoPressableState) => void) | null>(null)
|
|
@@ -218,9 +271,21 @@ export function HozoPressable({
|
|
|
218
271
|
// Assemble native props once, with no trailing conditional spread on the
|
|
219
272
|
// hot no-motion path. Private animation data never goes through RN props.
|
|
220
273
|
const Component: ElementType = hozoTransition ? TransitionPressable : Pressable
|
|
274
|
+
// The label's styles for the state this element is in; children given
|
|
275
|
+
// as a function of React Native's own press state get them too.
|
|
276
|
+
const label = hozoTextStyle?.({ ...interaction })
|
|
277
|
+
const labelled =
|
|
278
|
+
hozoTextStyle === undefined
|
|
279
|
+
? children
|
|
280
|
+
: typeof children === 'function'
|
|
281
|
+
? (state: PressableStateCallbackType) =>
|
|
282
|
+
withLabelStyle((children as (s: PressableStateCallbackType) => ReactNode)(state), label)
|
|
283
|
+
: withLabelStyle(children, label)
|
|
221
284
|
const host = (
|
|
222
285
|
<Component
|
|
223
286
|
{...props}
|
|
287
|
+
// biome-ignore lint/correctness/noChildrenProp: a function child is React Native's own form
|
|
288
|
+
children={labelled}
|
|
224
289
|
onHoverIn={(event: MouseEvent) => {
|
|
225
290
|
handlers.hoverIn()
|
|
226
291
|
onHoverIn?.(event)
|
|
@@ -488,6 +553,24 @@ export function HozoText({ style, ...props }: HozoTextProps) {
|
|
|
488
553
|
return <TransitionText {...props} context={context} resolved={resolved} />
|
|
489
554
|
}
|
|
490
555
|
|
|
556
|
+
export interface HozoInteractionViewProps extends Omit<ViewProps, 'style'> {
|
|
557
|
+
children?: ReactNode
|
|
558
|
+
style?: StyleProp<ViewStyle> | ((state: HozoPressableState) => StyleProp<ViewStyle>)
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* A View whose `group-*:` styles read the state of an enclosing
|
|
563
|
+
* HozoPressable, as HozoText's do. Without it a `group` card could restyle
|
|
564
|
+
* its label but not the icon box beside it. The style switches; it does not
|
|
565
|
+
* animate, since a `transition-*` on the owner animates the owner and its
|
|
566
|
+
* text colour only.
|
|
567
|
+
*/
|
|
568
|
+
export function HozoInteractionView({ style, ...props }: HozoInteractionViewProps) {
|
|
569
|
+
const context = useContext(InteractionContext)
|
|
570
|
+
const state = context ?? { pressed: false, hovered: false, focused: false, focusVisible: false }
|
|
571
|
+
return <View {...props} style={typeof style === 'function' ? style(state) : style} />
|
|
572
|
+
}
|
|
573
|
+
|
|
491
574
|
function TransitionText({
|
|
492
575
|
context,
|
|
493
576
|
resolved,
|
|
@@ -499,47 +582,12 @@ function TransitionText({
|
|
|
499
582
|
const transition = context.transition!
|
|
500
583
|
const state = context
|
|
501
584
|
const target = StyleSheet.flatten(resolved)?.color
|
|
502
|
-
const progress = useRef(new Animated.Value(1)).current
|
|
503
|
-
const animation = useRef<Animated.CompositeAnimation | null>(null)
|
|
504
|
-
const fraction = useRef(1)
|
|
505
|
-
const initialColor = typeof target === 'string' ? target : ''
|
|
506
|
-
const range = useRef<ColorRange>({ from: initialColor, to: initialColor })
|
|
507
585
|
const interaction = `${state.pressed}:${state.hovered}:${state.focused}:${state.focusVisible}`
|
|
508
|
-
const
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
previousInteraction.current !== interaction && typeof target === 'string' ? interaction : null
|
|
514
|
-
if (pending) {
|
|
515
|
-
range.current = {
|
|
516
|
-
from: blendColor(range.current.from, range.current.to, fraction.current),
|
|
517
|
-
to: target as string,
|
|
518
|
-
}
|
|
519
|
-
previousInteraction.current = interaction
|
|
520
|
-
}
|
|
521
|
-
useEffect(() => {
|
|
522
|
-
const listener = progress.addListener(({ value }) => {
|
|
523
|
-
fraction.current = value
|
|
524
|
-
})
|
|
525
|
-
return () => {
|
|
526
|
-
progress.removeListener(listener)
|
|
527
|
-
animation.current?.stop()
|
|
528
|
-
}
|
|
529
|
-
}, [progress])
|
|
530
|
-
useLayoutEffect(() => {
|
|
531
|
-
if (!pending) return
|
|
532
|
-
progress.setValue(0)
|
|
533
|
-
fraction.current = 0
|
|
534
|
-
animation.current = transitionTiming(progress, 1, transition, false)
|
|
535
|
-
animation.current.start()
|
|
536
|
-
}, [transition, pending, progress])
|
|
537
|
-
const animatedColor = range.current.from
|
|
538
|
-
? progress.interpolate({
|
|
539
|
-
inputRange: [0, 1],
|
|
540
|
-
outputRange: [range.current.from, range.current.to],
|
|
541
|
-
})
|
|
542
|
-
: undefined
|
|
586
|
+
const animatedColor = useHozoTextColorTransition(
|
|
587
|
+
typeof target === 'string' ? target : undefined,
|
|
588
|
+
interaction,
|
|
589
|
+
transition,
|
|
590
|
+
)
|
|
543
591
|
return (
|
|
544
592
|
<AnimatedText {...props} style={[resolved, animatedColor ? { color: animatedColor } : null]} />
|
|
545
593
|
)
|
package/src/pressable.ts
CHANGED
|
@@ -326,6 +326,13 @@ export function HozoPressable({
|
|
|
326
326
|
HozoLink,
|
|
327
327
|
{
|
|
328
328
|
...common,
|
|
329
|
+
// common is already DOM-shaped. HozoLink normalizes its canonical
|
|
330
|
+
// identity/name props itself; omitting these let its undefined
|
|
331
|
+
// defaults overwrite the id and label just passed in common.
|
|
332
|
+
nativeID,
|
|
333
|
+
testID,
|
|
334
|
+
accessibilityLabel,
|
|
335
|
+
accessibilityHint,
|
|
329
336
|
ref: setRef as Ref<HTMLAnchorElement>,
|
|
330
337
|
href,
|
|
331
338
|
external,
|
|
@@ -335,7 +342,7 @@ export function HozoPressable({
|
|
|
335
342
|
rel,
|
|
336
343
|
download,
|
|
337
344
|
disabled: unavailable,
|
|
338
|
-
accessibilityRole,
|
|
345
|
+
accessibilityRole: accessibilityRole ?? role,
|
|
339
346
|
onPress: unavailable ? undefined : (activate as never),
|
|
340
347
|
onKeyDown,
|
|
341
348
|
onKeyUp,
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { blendColor } from '@hozo/engine'
|
|
2
|
+
import { useLayoutEffect, useMemo, useRef, useState } from 'react'
|
|
3
|
+
import { Animated, Easing } from 'react-native'
|
|
4
|
+
import type { HozoTransition } from './pressable.native.tsx'
|
|
5
|
+
|
|
6
|
+
type Spec = Pick<HozoTransition, 'duration' | 'delay' | 'easing'>
|
|
7
|
+
|
|
8
|
+
function easingFor(name: Spec['easing']) {
|
|
9
|
+
switch (name) {
|
|
10
|
+
case 'linear':
|
|
11
|
+
return Easing.linear
|
|
12
|
+
case 'ease-in':
|
|
13
|
+
return Easing.in(Easing.ease)
|
|
14
|
+
case 'ease-out':
|
|
15
|
+
return Easing.out(Easing.ease)
|
|
16
|
+
case 'ease-in-out':
|
|
17
|
+
return Easing.inOut(Easing.ease)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* One colour timeline, reusable by a leaf or an inherited label owner.
|
|
23
|
+
* Progress is not reset when retargeting: the new interpolation starts at the
|
|
24
|
+
* observed position. Resetting a live value to zero would briefly send the
|
|
25
|
+
* previous interpolation back to its old starting colour.
|
|
26
|
+
*/
|
|
27
|
+
export function useHozoTextColorTransition(
|
|
28
|
+
target: string | undefined,
|
|
29
|
+
revision: string,
|
|
30
|
+
spec: Spec,
|
|
31
|
+
) {
|
|
32
|
+
const [progress] = useState(() => new Animated.Value(0))
|
|
33
|
+
const position = useRef(0)
|
|
34
|
+
const animation = useRef<Animated.CompositeAnimation | null>(null)
|
|
35
|
+
const [range, setRange] = useState(() => ({
|
|
36
|
+
from: target ?? '',
|
|
37
|
+
to: target ?? '',
|
|
38
|
+
target,
|
|
39
|
+
revision,
|
|
40
|
+
start: 0,
|
|
41
|
+
animate: false,
|
|
42
|
+
}))
|
|
43
|
+
useLayoutEffect(() => {
|
|
44
|
+
const listener = progress.addListener(({ value }) => {
|
|
45
|
+
position.current = value
|
|
46
|
+
})
|
|
47
|
+
return () => {
|
|
48
|
+
progress.removeListener(listener)
|
|
49
|
+
animation.current?.stop()
|
|
50
|
+
}
|
|
51
|
+
}, [progress])
|
|
52
|
+
useLayoutEffect(() => {
|
|
53
|
+
if (range.target === target && range.revision === revision) return
|
|
54
|
+
animation.current?.stop()
|
|
55
|
+
const start = position.current
|
|
56
|
+
const fraction = Math.max(0, Math.min(1, start - range.start))
|
|
57
|
+
const current = range.from ? blendColor(range.from, range.to, fraction) : (target ?? '')
|
|
58
|
+
// Only committed inputs may replace the range. An abandoned Suspense
|
|
59
|
+
// render must not consume the target or mutate the visible timeline.
|
|
60
|
+
setRange({
|
|
61
|
+
from: target ? current : '',
|
|
62
|
+
to: target ?? '',
|
|
63
|
+
target,
|
|
64
|
+
revision,
|
|
65
|
+
start,
|
|
66
|
+
animate: Boolean(target),
|
|
67
|
+
})
|
|
68
|
+
}, [range, target, revision])
|
|
69
|
+
const { duration, delay = 0, easing } = spec
|
|
70
|
+
useLayoutEffect(() => {
|
|
71
|
+
if (!range.animate) return
|
|
72
|
+
const next = Animated.timing(progress, {
|
|
73
|
+
toValue: range.start + 1,
|
|
74
|
+
duration,
|
|
75
|
+
delay,
|
|
76
|
+
easing: easingFor(easing),
|
|
77
|
+
useNativeDriver: false,
|
|
78
|
+
})
|
|
79
|
+
animation.current = next
|
|
80
|
+
next.start()
|
|
81
|
+
return () => {
|
|
82
|
+
next.stop()
|
|
83
|
+
if (animation.current === next) animation.current = null
|
|
84
|
+
}
|
|
85
|
+
}, [progress, range, duration, delay, easing])
|
|
86
|
+
return useMemo(
|
|
87
|
+
() =>
|
|
88
|
+
range.from
|
|
89
|
+
? progress.interpolate({
|
|
90
|
+
inputRange: [range.start, range.start + 1],
|
|
91
|
+
outputRange: [range.from, range.to],
|
|
92
|
+
})
|
|
93
|
+
: undefined,
|
|
94
|
+
[progress, range],
|
|
95
|
+
)
|
|
96
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { TextInputProps as RNTextInputProps } from 'react-native'
|
|
2
|
+
|
|
3
|
+
/** Canonical disabled is distinct from an editable/read-only field. */
|
|
4
|
+
export interface NativeTextInputProps extends RNTextInputProps {
|
|
5
|
+
disabled?: boolean
|
|
6
|
+
className?: string
|
|
7
|
+
'aria-invalid'?: boolean | 'false' | 'true' | 'grammar' | 'spelling'
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Shared by value-level and generated hosts; RN has no disabled input prop. */
|
|
11
|
+
export function nativeTextInputProps({
|
|
12
|
+
disabled,
|
|
13
|
+
'aria-invalid': _invalid,
|
|
14
|
+
...props
|
|
15
|
+
}: NativeTextInputProps): RNTextInputProps {
|
|
16
|
+
if (!disabled) return props
|
|
17
|
+
// Both RN spellings must prevent editing even if the caller authored
|
|
18
|
+
// readOnly=false. Do not turn ordinary read-only fields into disabled ones.
|
|
19
|
+
return {
|
|
20
|
+
...props,
|
|
21
|
+
editable: false,
|
|
22
|
+
readOnly: true,
|
|
23
|
+
accessibilityState: { ...props.accessibilityState, disabled: true },
|
|
24
|
+
'aria-disabled': true,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type HozoPressableInteractionState,
|
|
3
|
+
usePressableInteraction,
|
|
4
|
+
} from '@hozo/behaviors/pressable-interaction'
|
|
5
|
+
import type { ComponentRef, Ref } from 'react'
|
|
6
|
+
import { TextInput, type TextInputProps } from 'react-native'
|
|
7
|
+
import { type NativeTextInputProps, nativeTextInputProps } from './text-input-props.native.ts'
|
|
8
|
+
|
|
9
|
+
export interface HozoTextInputProps extends NativeTextInputProps {
|
|
10
|
+
ref?: Ref<ComponentRef<typeof TextInput>>
|
|
11
|
+
/** Compiler-only style evaluation on this input, not an ancestor Pressable. */
|
|
12
|
+
hozoStyle?: (state: HozoPressableInteractionState) => TextInputProps['style']
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Generated-code host when static or dynamic classes need input focus. Ordinary
|
|
17
|
+
* compiled inputs stay RN TextInput. No extra box, context, animation or
|
|
18
|
+
* subscriptions; the same native field keeps its ref, events and semantics.
|
|
19
|
+
*
|
|
20
|
+
* A keyboard-input field indicates focus even after a pointer/touch focuses
|
|
21
|
+
* it (Selectors 4 §9.4). A button's pointer modality is not its policy.
|
|
22
|
+
*/
|
|
23
|
+
export function HozoTextInput(props: HozoTextInputProps) {
|
|
24
|
+
// A prop-only adapter needs no state subscription. The focus child exists
|
|
25
|
+
// only for a compiler callback and still renders exactly one RN field.
|
|
26
|
+
if (!props.hozoStyle) {
|
|
27
|
+
const { hozoStyle: _hozoStyle, ref, ...hostProps } = props
|
|
28
|
+
return <TextInput {...nativeTextInputProps(hostProps)} ref={ref} />
|
|
29
|
+
}
|
|
30
|
+
return <FocusedTextInput {...props} />
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function FocusedTextInput({
|
|
34
|
+
ref,
|
|
35
|
+
hozoStyle,
|
|
36
|
+
style,
|
|
37
|
+
onFocus,
|
|
38
|
+
onBlur,
|
|
39
|
+
...props
|
|
40
|
+
}: HozoTextInputProps) {
|
|
41
|
+
const { state, handlers } = usePressableInteraction({ focusVisible: 'keyboard-input' })
|
|
42
|
+
return (
|
|
43
|
+
<TextInput
|
|
44
|
+
{...nativeTextInputProps(props)}
|
|
45
|
+
ref={ref}
|
|
46
|
+
style={[hozoStyle?.(state), style]}
|
|
47
|
+
onFocus={(event) => {
|
|
48
|
+
handlers.focus()
|
|
49
|
+
onFocus?.(event)
|
|
50
|
+
}}
|
|
51
|
+
onBlur={(event) => {
|
|
52
|
+
handlers.blur()
|
|
53
|
+
onBlur?.(event)
|
|
54
|
+
}}
|
|
55
|
+
/>
|
|
56
|
+
)
|
|
57
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"animated.d.ts","sourceRoot":"","sources":["../../src/generated/animated.ts"],"names":[],"mappings":"AACA,OAAO,EACL,YAAY,EACZ,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,GACxB,MAAM,kCAAkC,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"animated.js","sourceRoot":"","sources":["../../src/generated/animated.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,OAAO,EACL,YAAY,GAGb,MAAM,iCAAkC,CAAA"}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export type PresenceState = 'open' | 'closed';
|
|
2
|
-
export interface PresenceContextValue {
|
|
3
|
-
state: PresenceState;
|
|
4
|
-
/**
|
|
5
|
-
* Called by an element that will animate out, once, on mount. `Presence`
|
|
6
|
-
* waits for every registered element before it removes its child, and
|
|
7
|
-
* removes it at once when none registered. The returned function
|
|
8
|
-
* unregisters.
|
|
9
|
-
*/
|
|
10
|
-
register(id: symbol, durationMs: number): () => void;
|
|
11
|
-
/** Called by a registered element when its exit animation has ended. */
|
|
12
|
-
done(id: symbol): void;
|
|
13
|
-
}
|
|
14
|
-
export declare const PresenceContext: import("react").Context<PresenceContextValue | null>;
|
|
15
|
-
//# sourceMappingURL=presence-context.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"presence-context.d.ts","sourceRoot":"","sources":["../src/presence-context.ts"],"names":[],"mappings":"AAWA,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,QAAQ,CAAA;AAE7C,MAAM,WAAW,oBAAoB;IACnC,KAAK,EAAE,aAAa,CAAA;IACpB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,IAAI,CAAA;IACpD,wEAAwE;IACxE,IAAI,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAAA;CACvB;AAED,eAAO,MAAM,eAAe,sDAAmD,CAAA"}
|
package/dist/presence-context.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
// What `Presence` tells the elements that animate out inside it, on
|
|
2
|
-
// Native (decision 007, amendment 1).
|
|
3
|
-
//
|
|
4
|
-
// On Web the same thing travels as an attribute, `data-state`, and CSS
|
|
5
|
-
// does the rest. React Native has no attributes to select on, so the
|
|
6
|
-
// state is a context instead, and `HozoAnimated` -- the component the
|
|
7
|
-
// compiler puts in place of an element with a `data-[state=closed]:`
|
|
8
|
-
// class -- reads it.
|
|
9
|
-
import { createContext } from 'react';
|
|
10
|
-
export const PresenceContext = createContext(null);
|
|
11
|
-
//# sourceMappingURL=presence-context.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"presence-context.js","sourceRoot":"","sources":["../src/presence-context.ts"],"names":[],"mappings":"AAAA,oEAAoE;AACpE,sCAAsC;AACtC,EAAE;AACF,uEAAuE;AACvE,qEAAqE;AACrE,sEAAsE;AACtE,qEAAqE;AACrE,qBAAqB;AAErB,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AAiBrC,MAAM,CAAC,MAAM,eAAe,GAAG,aAAa,CAA8B,IAAI,CAAC,CAAA"}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { type ReactNode } from 'react';
|
|
2
|
-
import { type StyleProp, type ViewStyle } from 'react-native';
|
|
3
|
-
export interface HozoTransitionSpec {
|
|
4
|
-
duration: number;
|
|
5
|
-
delay?: number;
|
|
6
|
-
easing: 'linear' | 'ease-in' | 'ease-out' | 'ease-in-out';
|
|
7
|
-
}
|
|
8
|
-
export interface HozoAnimatedProps {
|
|
9
|
-
style?: StyleProp<ViewStyle>;
|
|
10
|
-
hozoTransition?: HozoTransitionSpec;
|
|
11
|
-
/**
|
|
12
|
-
* The first frame of an enter animation, from a `starting:` class
|
|
13
|
-
* (decision 007). The element mounts at these values and transitions to
|
|
14
|
-
* `style`, which is what `@starting-style` does on Web. Read once, on
|
|
15
|
-
* mount: it is where the element enters from, not a style it keeps.
|
|
16
|
-
*/
|
|
17
|
-
hozoStarting?: StyleProp<ViewStyle>;
|
|
18
|
-
/**
|
|
19
|
-
* The last frame of an exit animation, from a `data-[state=closed]:`
|
|
20
|
-
* class (decision 007, amendment 1). Laid over `style` while the
|
|
21
|
-
* enclosing `Presence` is closing, which is what the attribute selector
|
|
22
|
-
* does on Web; `Presence` removes the element when the transition to it
|
|
23
|
-
* ends.
|
|
24
|
-
*/
|
|
25
|
-
hozoExit?: StyleProp<ViewStyle>;
|
|
26
|
-
children?: ReactNode;
|
|
27
|
-
[key: string]: unknown;
|
|
28
|
-
}
|
|
29
|
-
export declare function HozoAnimated({ style, hozoTransition, hozoStarting, hozoExit, children, ...props }: HozoAnimatedProps): import("react").JSX.Element;
|
|
30
|
-
//# sourceMappingURL=runtime-transition.native.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"runtime-transition.native.d.ts","sourceRoot":"","sources":["../src/runtime-transition.native.tsx"],"names":[],"mappings":"AAsBA,OAAO,EAAE,KAAK,SAAS,EAAoD,MAAM,OAAO,CAAA;AACxF,OAAO,EAAoB,KAAK,SAAS,EAAc,KAAK,SAAS,EAAE,MAAM,cAAc,CAAA;AAG3F,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,QAAQ,GAAG,SAAS,GAAG,UAAU,GAAG,aAAa,CAAA;CAC1D;AA6BD,MAAM,WAAW,iBAAiB;IAChC,KAAK,CAAC,EAAE,SAAS,CAAC,SAAS,CAAC,CAAA;IAC5B,cAAc,CAAC,EAAE,kBAAkB,CAAA;IACnC;;;;;OAKG;IACH,YAAY,CAAC,EAAE,SAAS,CAAC,SAAS,CAAC,CAAA;IACnC;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,SAAS,CAAC,SAAS,CAAC,CAAA;IAC/B,QAAQ,CAAC,EAAE,SAAS,CAAA;IACpB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACvB;AAED,wBAAgB,YAAY,CAAC,EAC3B,KAAK,EACL,cAAc,EACd,YAAY,EACZ,QAAQ,EACR,QAAQ,EACR,GAAG,KAAK,EACT,EAAE,iBAAiB,+BA0InB"}
|