@hozo/primitives 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/CHANGELOG.md +71 -0
  2. package/README.md +18 -1
  3. package/dist/button-host-attributes.d.ts +20 -0
  4. package/dist/button-host-attributes.d.ts.map +1 -0
  5. package/dist/button-host-attributes.js +47 -0
  6. package/dist/button-host-attributes.js.map +1 -0
  7. package/dist/button-host-props.native.d.ts +8 -0
  8. package/dist/button-host-props.native.d.ts.map +1 -0
  9. package/dist/button-host-props.native.js +20 -0
  10. package/dist/button-host-props.native.js.map +1 -0
  11. package/dist/foundation.d.ts +27 -6
  12. package/dist/foundation.d.ts.map +1 -1
  13. package/dist/foundation.js +72 -19
  14. package/dist/foundation.js.map +1 -1
  15. package/dist/foundation.native.d.ts +27 -7
  16. package/dist/foundation.native.d.ts.map +1 -1
  17. package/dist/foundation.native.js +18 -3
  18. package/dist/foundation.native.js.map +1 -1
  19. package/dist/generated/button-dom-props.d.ts +11 -0
  20. package/dist/generated/button-dom-props.d.ts.map +1 -0
  21. package/dist/generated/button-dom-props.js +132 -0
  22. package/dist/generated/button-dom-props.js.map +1 -0
  23. package/dist/generated/button-props.d.ts +3 -0
  24. package/dist/generated/button-props.d.ts.map +1 -0
  25. package/dist/generated/button-props.js +5 -0
  26. package/dist/generated/button-props.js.map +1 -0
  27. package/dist/generated/button-props.native.d.ts +2 -0
  28. package/dist/generated/button-props.native.d.ts.map +1 -0
  29. package/dist/generated/button-props.native.js +2 -0
  30. package/dist/generated/button-props.native.js.map +1 -0
  31. package/dist/generated/inherited-label.d.ts +2 -0
  32. package/dist/generated/inherited-label.d.ts.map +1 -0
  33. package/dist/generated/inherited-label.js +3 -0
  34. package/dist/generated/inherited-label.js.map +1 -0
  35. package/dist/generated/inherited-pressable.d.ts +2 -0
  36. package/dist/generated/inherited-pressable.d.ts.map +1 -0
  37. package/dist/generated/inherited-pressable.js +3 -0
  38. package/dist/generated/inherited-pressable.js.map +1 -0
  39. package/dist/generated/inherited-relative-text.d.ts +2 -0
  40. package/dist/generated/inherited-relative-text.d.ts.map +1 -0
  41. package/dist/generated/inherited-relative-text.js +3 -0
  42. package/dist/generated/inherited-relative-text.js.map +1 -0
  43. package/dist/generated/inherited-text.d.ts +2 -0
  44. package/dist/generated/inherited-text.d.ts.map +1 -0
  45. package/dist/generated/inherited-text.js +3 -0
  46. package/dist/generated/inherited-text.js.map +1 -0
  47. package/dist/generated/inherited-view.d.ts +2 -0
  48. package/dist/generated/inherited-view.d.ts.map +1 -0
  49. package/dist/generated/inherited-view.js +3 -0
  50. package/dist/generated/inherited-view.js.map +1 -0
  51. package/dist/generated/native-disabled.d.ts +2 -0
  52. package/dist/generated/native-disabled.d.ts.map +1 -0
  53. package/dist/generated/native-disabled.js +2 -0
  54. package/dist/generated/native-disabled.js.map +1 -0
  55. package/dist/generated/text-input-dom-props.d.ts +7 -0
  56. package/dist/generated/text-input-dom-props.d.ts.map +1 -0
  57. package/dist/generated/text-input-dom-props.js +130 -0
  58. package/dist/generated/text-input-dom-props.js.map +1 -0
  59. package/dist/generated/text-input.native.d.ts +1 -1
  60. package/dist/generated/text-input.native.d.ts.map +1 -1
  61. package/dist/generated/text-input.native.js +1 -1
  62. package/dist/generated/text-input.native.js.map +1 -1
  63. package/dist/generated/view-dom-props.d.ts +8 -0
  64. package/dist/generated/view-dom-props.d.ts.map +1 -0
  65. package/dist/generated/view-dom-props.js +241 -0
  66. package/dist/generated/view-dom-props.js.map +1 -0
  67. package/dist/inherited-label.native.d.ts +8 -0
  68. package/dist/inherited-label.native.d.ts.map +1 -0
  69. package/dist/inherited-label.native.js +12 -0
  70. package/dist/inherited-label.native.js.map +1 -0
  71. package/dist/inherited-pressable.native.d.ts +11 -0
  72. package/dist/inherited-pressable.native.d.ts.map +1 -0
  73. package/dist/inherited-pressable.native.js +75 -0
  74. package/dist/inherited-pressable.native.js.map +1 -0
  75. package/dist/inherited-relative-text.native.d.ts +13 -0
  76. package/dist/inherited-relative-text.native.d.ts.map +1 -0
  77. package/dist/inherited-relative-text.native.js +16 -0
  78. package/dist/inherited-relative-text.native.js.map +1 -0
  79. package/dist/inherited-text-scope.d.ts +8 -0
  80. package/dist/inherited-text-scope.d.ts.map +1 -0
  81. package/dist/inherited-text-scope.js +10 -0
  82. package/dist/inherited-text-scope.js.map +1 -0
  83. package/dist/inherited-text-style.native.d.ts +5 -0
  84. package/dist/inherited-text-style.native.d.ts.map +1 -0
  85. package/dist/inherited-text-style.native.js +30 -0
  86. package/dist/inherited-text-style.native.js.map +1 -0
  87. package/dist/inherited-text.native.d.ts +5 -0
  88. package/dist/inherited-text.native.d.ts.map +1 -0
  89. package/dist/inherited-text.native.js +18 -0
  90. package/dist/inherited-text.native.js.map +1 -0
  91. package/dist/inherited-view.native.d.ts +9 -0
  92. package/dist/inherited-view.native.d.ts.map +1 -0
  93. package/dist/inherited-view.native.js +17 -0
  94. package/dist/inherited-view.native.js.map +1 -0
  95. package/dist/link.d.ts +4 -3
  96. package/dist/link.d.ts.map +1 -1
  97. package/dist/link.js +9 -1
  98. package/dist/link.js.map +1 -1
  99. package/dist/link.native.d.ts +16 -7
  100. package/dist/link.native.d.ts.map +1 -1
  101. package/dist/link.native.js +15 -5
  102. package/dist/link.native.js.map +1 -1
  103. package/dist/native-disabled.d.ts +13 -0
  104. package/dist/native-disabled.d.ts.map +1 -0
  105. package/dist/native-disabled.js +13 -0
  106. package/dist/native-disabled.js.map +1 -0
  107. package/dist/presence.native.d.ts.map +1 -1
  108. package/dist/presence.native.js +1 -1
  109. package/dist/presence.native.js.map +1 -1
  110. package/dist/pressable.d.ts +1 -1
  111. package/dist/pressable.d.ts.map +1 -1
  112. package/dist/pressable.js +15 -5
  113. package/dist/pressable.js.map +1 -1
  114. package/dist/pressable.native.d.ts +10 -1
  115. package/dist/pressable.native.d.ts.map +1 -1
  116. package/dist/pressable.native.js +206 -221
  117. package/dist/pressable.native.js.map +1 -1
  118. package/dist/runtime-container.native.d.ts +11 -9
  119. package/dist/runtime-container.native.d.ts.map +1 -1
  120. package/dist/runtime-container.native.js +8 -6
  121. package/dist/runtime-container.native.js.map +1 -1
  122. package/dist/text-color-transition.native.d.ts +12 -0
  123. package/dist/text-color-transition.native.d.ts.map +1 -0
  124. package/dist/text-color-transition.native.js +87 -0
  125. package/dist/text-color-transition.native.js.map +1 -0
  126. package/dist/text-input-props.native.d.ts +10 -0
  127. package/dist/text-input-props.native.d.ts.map +1 -0
  128. package/dist/text-input-props.native.js +15 -0
  129. package/dist/text-input-props.native.js.map +1 -0
  130. package/dist/text-input.native.d.ts +19 -0
  131. package/dist/text-input.native.d.ts.map +1 -0
  132. package/dist/text-input.native.js +32 -0
  133. package/dist/text-input.native.js.map +1 -0
  134. package/dist/view.d.ts +1 -285
  135. package/dist/view.d.ts.map +1 -1
  136. package/dist/view.js +5 -4
  137. package/dist/view.js.map +1 -1
  138. package/package.json +51 -7
  139. package/src/button-host-attributes.ts +56 -0
  140. package/src/button-host-props.native.ts +22 -0
  141. package/src/foundation.native.tsx +45 -6
  142. package/src/foundation.tsx +139 -28
  143. package/src/generated/button-dom-props.ts +127 -0
  144. package/src/generated/button-props.native.ts +1 -0
  145. package/src/generated/button-props.ts +4 -0
  146. package/src/generated/inherited-label.ts +2 -0
  147. package/src/generated/inherited-pressable.ts +2 -0
  148. package/src/generated/inherited-relative-text.ts +2 -0
  149. package/src/generated/inherited-text.ts +2 -0
  150. package/src/generated/inherited-view.ts +2 -0
  151. package/src/generated/native-disabled.ts +1 -0
  152. package/src/generated/text-input-dom-props.ts +128 -0
  153. package/src/generated/text-input.native.ts +1 -4
  154. package/src/generated/view-dom-props.ts +267 -0
  155. package/src/inherited-label.native.tsx +28 -0
  156. package/src/inherited-pressable.native.tsx +152 -0
  157. package/src/inherited-relative-text.native.tsx +22 -0
  158. package/src/inherited-text-scope.tsx +27 -0
  159. package/src/inherited-text-style.native.ts +35 -0
  160. package/src/inherited-text.native.tsx +32 -0
  161. package/src/inherited-view.native.tsx +37 -0
  162. package/src/link.native.tsx +43 -28
  163. package/src/link.ts +12 -0
  164. package/src/native-disabled.ts +23 -0
  165. package/src/presence.native.tsx +1 -1
  166. package/src/pressable.native.tsx +298 -262
  167. package/src/pressable.ts +34 -17
  168. package/src/runtime-container.native.tsx +30 -15
  169. package/src/text-color-transition.native.ts +96 -0
  170. package/src/text-input-props.native.ts +26 -0
  171. package/src/text-input.native.tsx +57 -0
  172. package/src/view.ts +34 -25
  173. package/dist/generated/animated.d.ts +0 -2
  174. package/dist/generated/animated.d.ts.map +0 -1
  175. package/dist/generated/animated.js +0 -3
  176. package/dist/generated/animated.js.map +0 -1
  177. package/dist/presence-context.d.ts +0 -15
  178. package/dist/presence-context.d.ts.map +0 -1
  179. package/dist/presence-context.js +0 -11
  180. package/dist/presence-context.js.map +0 -1
  181. package/dist/runtime-transition.native.d.ts +0 -30
  182. package/dist/runtime-transition.native.d.ts.map +0 -1
  183. package/dist/runtime-transition.native.js +0 -183
  184. package/dist/runtime-transition.native.js.map +0 -1
  185. package/src/generated/animated.ts +0 -6
  186. package/src/presence-context.ts +0 -27
  187. package/src/runtime-transition.native.tsx +0 -244
@@ -1,7 +1,16 @@
1
+ import {
2
+ HozoInteractionContext,
3
+ usePressableInteraction,
4
+ } from '@hozo/behaviors/pressable-interaction'
1
5
  import { blendColor } from '@hozo/engine'
2
6
  import {
7
+ Children,
8
+ type Context,
9
+ cloneElement,
3
10
  createContext,
4
11
  type ElementType,
12
+ isValidElement,
13
+ type ReactElement,
5
14
  type ReactNode,
6
15
  useCallback,
7
16
  useContext,
@@ -9,7 +18,6 @@ import {
9
18
  useLayoutEffect,
10
19
  useMemo,
11
20
  useRef,
12
- useState,
13
21
  } from 'react'
14
22
  import {
15
23
  Animated,
@@ -28,6 +36,8 @@ import {
28
36
  type TextStyle,
29
37
  type ViewStyle,
30
38
  } from 'react-native'
39
+ import { nativeDisabledProps } from './native-disabled.ts'
40
+ import { useHozoTextColorTransition } from './text-color-transition.native.ts'
31
41
 
32
42
  export interface HozoTransition {
33
43
  duration: number
@@ -49,12 +59,45 @@ export interface HozoPressableProps extends Omit<PressableProps, 'style'> {
49
59
  hozoTransition?: HozoTransition
50
60
  /** Enables per-element pointer/keyboard modality inference. */
51
61
  hozoFocusVisible?: boolean
62
+ /**
63
+ * The text styles this element's class list carries for its label, by
64
+ * state -- what a `<button>` passes to its text on the Web (#876). The
65
+ * compiler writes it when the label is not in the source it can see, so
66
+ * it arrives as `children` and is styled here: a string is put in a
67
+ * `Text`, and a direct `Text` child has these merged ahead of its own.
68
+ * A `Text` deeper inside is not reached (#950).
69
+ */
70
+ hozoTextStyle?: (state: HozoPressableState) => StyleProp<TextStyle>
71
+ }
72
+
73
+ /** A component a label can be: React Native's `Text`, `HozoText`, or one marked so. */
74
+ function isLabelText(child: ReactElement): boolean {
75
+ const type = child.type as { hozoLabelText?: boolean } | string
76
+ return (
77
+ type === Text || type === HozoText || (typeof type !== 'string' && type?.hozoLabelText === true)
78
+ )
79
+ }
80
+
81
+ /** `children` with `label` handed to the text among them, one level deep. */
82
+ function withLabelStyle(children: ReactNode, label: StyleProp<TextStyle>): ReactNode {
83
+ return Children.map(children, (child) => {
84
+ if (typeof child === 'string' || typeof child === 'number') {
85
+ return <Text style={label}>{child}</Text>
86
+ }
87
+ if (!isValidElement<{ style?: unknown }>(child) || !isLabelText(child)) return child
88
+ const own = child.props.style
89
+ return cloneElement(child, {
90
+ style:
91
+ typeof own === 'function'
92
+ ? (state: HozoPressableState) => [
93
+ label,
94
+ (own as (s: HozoPressableState) => unknown)(state),
95
+ ]
96
+ : [label, own],
97
+ } as { style: unknown })
98
+ })
52
99
  }
53
100
 
54
- const HOVERED = 1
55
- const FOCUSED = 2
56
- const PRESSED = 4
57
- const FOCUS_VISIBLE = 8
58
101
  const AnimatedPressable = Animated.createAnimatedComponent(Pressable)
59
102
 
60
103
  /**
@@ -91,7 +134,10 @@ export function pressableFor(transition: HozoTransition | undefined): ElementTyp
91
134
  }
92
135
  const AnimatedText = Animated.createAnimatedComponent(Text)
93
136
  type InteractionContextValue = HozoPressableState & { transition?: HozoTransition }
94
- const InteractionContext = createContext<InteractionContextValue | null>(null)
137
+ // Shared with `@hozo/patterns`' pressable, which provides it too (#974).
138
+ const InteractionContext = HozoInteractionContext as Context<InteractionContextValue | null>
139
+ type AnimationDriver = { current: ((state: HozoPressableState) => void) | null }
140
+ const AnimationDriverContext = createContext<AnimationDriver | null>(null)
95
141
 
96
142
  function easingFor(name: HozoTransition['easing']) {
97
143
  switch (name) {
@@ -106,10 +152,36 @@ function easingFor(name: HozoTransition['easing']) {
106
152
  }
107
153
  }
108
154
 
155
+ function transitionTiming(
156
+ value: Animated.Value,
157
+ toValue: number,
158
+ transition: HozoTransition,
159
+ useNativeDriver: boolean,
160
+ ) {
161
+ return Animated.timing(value, {
162
+ toValue,
163
+ duration: transition.duration,
164
+ delay: transition.delay ?? 0,
165
+ easing: easingFor(transition.easing),
166
+ useNativeDriver,
167
+ })
168
+ }
169
+
109
170
  type TransformTarget = { key: string; value: number; degrees: boolean }
110
171
  const transformRank = (key: string) =>
111
172
  key.startsWith('translate') ? 0 : key.startsWith('rotate') ? 1 : 2
112
173
 
174
+ // Both motion probes need the full Cartesian product, in the same order.
175
+ // Called only by the transition child, never by a plain Pressable.
176
+ function interactionStates(): HozoPressableState[] {
177
+ return Array.from({ length: 16 }, (_, bits) => ({
178
+ pressed: !!(bits & 8),
179
+ hovered: !!(bits & 4),
180
+ focused: !!(bits & 2),
181
+ focusVisible: !!(bits & 1),
182
+ }))
183
+ }
184
+
113
185
  function transformTargets(style: StyleProp<ViewStyle>): TransformTarget[] {
114
186
  const merged = new Map<string, TransformTarget>()
115
187
  const visit = (part: StyleProp<ViewStyle>) => {
@@ -169,35 +241,139 @@ export function HozoPressable({
169
241
  onKeyDown,
170
242
  hozoTransition,
171
243
  hozoFocusVisible = false,
244
+ hozoTextStyle,
245
+ children,
172
246
  ...props
173
247
  }: HozoPressableProps) {
174
- const [interaction, setInteraction] = useState(0)
175
- const interactionRef = useRef(0)
176
- const initialStyle =
177
- typeof style === 'function'
178
- ? style({ pressed: false, hovered: false, focused: false, focusVisible: false })
179
- : style
248
+ props = nativeDisabledProps(props)
249
+ // Keep the event/context owner mounted when motion is added or removed.
250
+ // Only the optional animation child owns nodes and progress listeners.
251
+ const driver = useRef<((state: HozoPressableState) => void) | null>(null)
252
+ const animate = useCallback((state: HozoPressableState) => driver.current?.(state), [])
253
+ const { state: interaction, handlers } = usePressableInteraction({
254
+ focusVisible: hozoFocusVisible,
255
+ onChange: hozoTransition ? animate : undefined,
256
+ })
257
+ const context = useMemo(
258
+ () => ({ ...interaction, transition: hozoTransition }),
259
+ [hozoTransition, interaction],
260
+ )
261
+ // Resolve the owner's snapshot once, not once again inside RN's host.
262
+ // RN's pressed flag can disagree with the owner snapshot. Active wins,
263
+ // as before; only that case needs a separate style evaluation.
264
+ const restingLook = useMemo(
265
+ () =>
266
+ hozoTransition ? undefined : typeof style === 'function' ? style({ ...interaction }) : style,
267
+ [hozoTransition, interaction, style],
268
+ )
269
+ // Assemble native props once, with no trailing conditional spread on the
270
+ // hot no-motion path. Private animation data never goes through RN props.
271
+ const Component: ElementType = hozoTransition ? TransitionPressable : Pressable
272
+ // The label's styles for the state this element is in; children given
273
+ // as a function of React Native's own press state get them too.
274
+ const label = hozoTextStyle?.({ ...interaction })
275
+ const labelled =
276
+ hozoTextStyle === undefined
277
+ ? children
278
+ : typeof children === 'function'
279
+ ? (state: PressableStateCallbackType) =>
280
+ withLabelStyle((children as (s: PressableStateCallbackType) => ReactNode)(state), label)
281
+ : withLabelStyle(children, label)
282
+ const host = (
283
+ <Component
284
+ {...props}
285
+ // biome-ignore lint/correctness/noChildrenProp: a function child is React Native's own form
286
+ children={labelled}
287
+ onHoverIn={(event: MouseEvent) => {
288
+ handlers.hoverIn()
289
+ onHoverIn?.(event)
290
+ }}
291
+ onHoverOut={(event: MouseEvent) => {
292
+ handlers.hoverOut()
293
+ onHoverOut?.(event)
294
+ }}
295
+ onFocus={(event: NativeSyntheticEvent<TargetedEvent>) => {
296
+ handlers.focus()
297
+ onFocus?.(event)
298
+ }}
299
+ onBlur={(event: NativeSyntheticEvent<TargetedEvent>) => {
300
+ handlers.blur()
301
+ onBlur?.(event)
302
+ }}
303
+ onPointerDown={
304
+ hozoFocusVisible
305
+ ? (event: Parameters<NonNullable<PressableProps['onPointerDown']>>[0]) => {
306
+ handlers.pointerDown()
307
+ onPointerDown?.(event)
308
+ }
309
+ : onPointerDown
310
+ }
311
+ onKeyDown={
312
+ hozoFocusVisible
313
+ ? (event: Parameters<NonNullable<PressableProps['onKeyDown']>>[0]) => {
314
+ handlers.keyDown()
315
+ onKeyDown?.(event)
316
+ }
317
+ : onKeyDown
318
+ }
319
+ onPressIn={(event: GestureResponderEvent) => {
320
+ handlers.pressIn()
321
+ onPressIn?.(event)
322
+ }}
323
+ onPressOut={(event: GestureResponderEvent) => {
324
+ handlers.pressOut()
325
+ onPressOut?.(event)
326
+ }}
327
+ style={
328
+ hozoTransition
329
+ ? style
330
+ : ({ pressed }: PressableStateCallbackType) =>
331
+ pressed && !interaction.pressed && typeof style === 'function'
332
+ ? style({ ...interaction, pressed: pressed || interaction.pressed })
333
+ : restingLook
334
+ }
335
+ />
336
+ )
337
+ return (
338
+ <InteractionContext.Provider value={context}>
339
+ {hozoTransition ? (
340
+ <AnimationDriverContext.Provider value={driver}>{host}</AnimationDriverContext.Provider>
341
+ ) : (
342
+ host
343
+ )}
344
+ </InteractionContext.Provider>
345
+ )
346
+ }
347
+
348
+ function TransitionPressable({
349
+ style,
350
+ ...props
351
+ }: Omit<HozoPressableProps, 'hozoFocusVisible' | 'hozoTransition'>) {
352
+ // This private owner is mounted only inside both providers, for motion.
353
+ const context = useContext(InteractionContext)!
354
+ const hozoTransition = context.transition!
355
+ const interaction = useMemo(
356
+ () => ({
357
+ pressed: context.pressed,
358
+ hovered: context.hovered,
359
+ focused: context.focused,
360
+ focusVisible: context.focusVisible,
361
+ }),
362
+ [context.pressed, context.hovered, context.focused, context.focusVisible],
363
+ )
364
+ const driver = useContext(AnimationDriverContext)!
365
+ const initialStyle = typeof style === 'function' ? style({ ...interaction }) : style
180
366
  const initialOpacity = StyleSheet.flatten(initialStyle)?.opacity
181
367
  const opacity = useRef(
182
368
  new Animated.Value(typeof initialOpacity === 'number' ? initialOpacity : 1),
183
369
  ).current
184
370
  const colorSpecs = useMemo(() => {
185
- if (!hozoTransition?.colors || typeof style !== 'function') return [] as ColorKey[]
371
+ if (!hozoTransition.colors || typeof style !== 'function') return [] as ColorKey[]
186
372
  const found = new Set<ColorKey>()
187
- for (const pressed of [false, true]) {
188
- for (const hovered of [false, true]) {
189
- for (const focused of [false, true]) {
190
- for (const focusVisible of [false, true]) {
191
- for (const key of colorTargets(
192
- style({ pressed, hovered, focused, focusVisible }),
193
- ).keys())
194
- found.add(key)
195
- }
196
- }
197
- }
198
- }
373
+ for (const state of interactionStates())
374
+ for (const key of colorTargets(style(state)).keys()) found.add(key)
199
375
  return [...found]
200
- }, [hozoTransition?.colors, style])
376
+ }, [hozoTransition.colors, style])
201
377
  const initialColors = colorTargets(initialStyle)
202
378
  const colorRanges = useRef(new Map<ColorKey, ColorRange>()).current
203
379
  for (const key of colorSpecs) {
@@ -208,11 +384,15 @@ export function HozoPressable({
208
384
  }
209
385
  const colorProgress = useRef(new Animated.Value(0)).current
210
386
  const colorFraction = useRef(0)
387
+ const animation = useRef<Animated.CompositeAnimation | null>(null)
211
388
  useEffect(() => {
212
389
  const listener = colorProgress.addListener(({ value }) => {
213
390
  colorFraction.current = value
214
391
  })
215
- return () => colorProgress.removeListener(listener)
392
+ return () => {
393
+ colorProgress.removeListener(listener)
394
+ animation.current?.stop()
395
+ }
216
396
  }, [colorProgress])
217
397
  const animatedColors = Object.fromEntries(
218
398
  [...colorRanges].map(([key, range]) => [
@@ -224,20 +404,13 @@ export function HozoPressable({
224
404
  ]),
225
405
  )
226
406
  const transformSpecs = useMemo(() => {
227
- if (!hozoTransition?.transform || typeof style !== 'function') return []
228
- const states = [false, true].flatMap((pressed) =>
229
- [false, true].flatMap((hovered) =>
230
- [false, true].flatMap((focused) =>
231
- [false, true].map((focusVisible) => ({ pressed, hovered, focused, focusVisible })),
232
- ),
233
- ),
234
- )
407
+ if (!hozoTransition.transform || typeof style !== 'function') return []
235
408
  const merged = new Map<string, TransformTarget>()
236
- for (const state of states) {
409
+ for (const state of interactionStates()) {
237
410
  for (const target of transformTargets(style(state))) merged.set(target.key, target)
238
411
  }
239
412
  return [...merged.values()].sort((a, b) => transformRank(a.key) - transformRank(b.key))
240
- }, [hozoTransition?.transform, style])
413
+ }, [hozoTransition.transform, style])
241
414
  const transformValues = useRef(new Map<string, Animated.Value>()).current
242
415
  const initialTargets = new Map(
243
416
  transformTargets(initialStyle).map((target) => [target.key, target]),
@@ -259,61 +432,31 @@ export function HozoPressable({
259
432
  : transformValues.get(spec.key)!,
260
433
  }))
261
434
  const animateInteraction = useCallback(
262
- (next: number) => {
263
- if (!hozoTransition || typeof style !== 'function') return
264
- const flattened = StyleSheet.flatten(
265
- style({
266
- pressed: (next & PRESSED) !== 0,
267
- hovered: (next & HOVERED) !== 0,
268
- focused: (next & FOCUSED) !== 0,
269
- focusVisible: (next & FOCUS_VISIBLE) !== 0,
270
- }),
271
- )
435
+ (next: HozoPressableState) => {
436
+ if (typeof style !== 'function') return
437
+ const flattened = StyleSheet.flatten(style({ ...next }))
272
438
  const animations: Animated.CompositeAnimation[] = []
273
439
  if (hozoTransition.opacity) {
274
440
  const targetOpacity = typeof flattened?.opacity === 'number' ? flattened.opacity : 1
275
- animations.push(
276
- Animated.timing(opacity, {
277
- toValue: targetOpacity,
278
- duration: hozoTransition.duration,
279
- delay: hozoTransition.delay ?? 0,
280
- easing: easingFor(hozoTransition.easing),
281
- useNativeDriver: true,
282
- }),
283
- )
441
+ animations.push(transitionTiming(opacity, targetOpacity, hozoTransition, true))
284
442
  }
285
443
  if (hozoTransition.transform) {
286
444
  const targets = new Map(
287
- transformTargets(
288
- style({
289
- pressed: (next & PRESSED) !== 0,
290
- hovered: (next & HOVERED) !== 0,
291
- focused: (next & FOCUSED) !== 0,
292
- focusVisible: (next & FOCUS_VISIBLE) !== 0,
293
- }),
294
- ).map((target) => [target.key, target.value]),
445
+ transformTargets(style({ ...next })).map((target) => [target.key, target.value]),
295
446
  )
296
447
  for (const spec of transformSpecs) {
297
448
  animations.push(
298
- Animated.timing(transformValues.get(spec.key)!, {
299
- toValue: targets.get(spec.key) ?? identityFor(spec),
300
- duration: hozoTransition.duration,
301
- delay: hozoTransition.delay ?? 0,
302
- easing: easingFor(hozoTransition.easing),
303
- useNativeDriver: true,
304
- }),
449
+ transitionTiming(
450
+ transformValues.get(spec.key)!,
451
+ targets.get(spec.key) ?? identityFor(spec),
452
+ hozoTransition,
453
+ true,
454
+ ),
305
455
  )
306
456
  }
307
457
  }
308
458
  if (hozoTransition.colors) {
309
- const targets = colorTargets(
310
- style({
311
- pressed: (next & PRESSED) !== 0,
312
- hovered: (next & HOVERED) !== 0,
313
- focused: (next & FOCUSED) !== 0,
314
- focusVisible: (next & FOCUS_VISIBLE) !== 0,
315
- }),
316
- )
459
+ const targets = colorTargets(style({ ...next }))
317
460
  for (const [key, range] of colorRanges) {
318
461
  const current = blendColor(range.from, range.to, colorFraction.current)
319
462
  const target = targets.get(key) ?? (key === 'backgroundColor' ? 'transparent' : current)
@@ -321,157 +464,70 @@ export function HozoPressable({
321
464
  }
322
465
  colorProgress.setValue(0)
323
466
  colorFraction.current = 0
324
- animations.push(
325
- Animated.timing(colorProgress, {
326
- toValue: 1,
327
- duration: hozoTransition.duration,
328
- delay: hozoTransition.delay ?? 0,
329
- easing: easingFor(hozoTransition.easing),
330
- useNativeDriver: false,
331
- }),
332
- )
467
+ animations.push(transitionTiming(colorProgress, 1, hozoTransition, false))
333
468
  }
334
- Animated.parallel(animations).start()
469
+ animation.current = Animated.parallel(animations)
470
+ animation.current.start()
335
471
  },
336
472
  [colorProgress, colorRanges, hozoTransition, opacity, style, transformSpecs, transformValues],
337
473
  )
338
- const setFlag = useCallback(
339
- (flag: number, active: boolean) => {
340
- const current = interactionRef.current
341
- const next = active ? current | flag : current & ~flag
342
- interactionRef.current = next
343
- animateInteraction(next)
344
- setInteraction(next)
345
- },
346
- [animateInteraction],
347
- )
474
+ // Install only the committed driver. A speculative render must not replace
475
+ // the callback used synchronously by the parent's event machine.
476
+ useLayoutEffect(() => {
477
+ driver.current = animateInteraction
478
+ return () => {
479
+ if (driver.current === animateInteraction) driver.current = null
480
+ }
481
+ }, [animateInteraction, driver])
348
482
  const PressableComponent = pressableFor(hozoTransition)
349
- const modality = useRef<'keyboard' | 'pointer'>('keyboard')
350
483
 
351
484
  // The style for the state this component is in, resolved here because
352
485
  // the animated path cannot hand React Native a callback -- see the
353
486
  // `style` prop below.
354
487
  const resolvedStyle = useMemo(
355
- () =>
356
- typeof style === 'function'
357
- ? style({
358
- pressed: (interaction & PRESSED) !== 0,
359
- hovered: (interaction & HOVERED) !== 0,
360
- focused: (interaction & FOCUSED) !== 0,
361
- focusVisible: (interaction & FOCUS_VISIBLE) !== 0,
362
- })
363
- : style,
488
+ () => (typeof style === 'function' ? style({ ...interaction }) : style),
364
489
  [interaction, style],
365
490
  )
366
491
 
367
- const context = useMemo(
368
- () => ({
369
- pressed: (interaction & PRESSED) !== 0,
370
- hovered: (interaction & HOVERED) !== 0,
371
- focused: (interaction & FOCUSED) !== 0,
372
- focusVisible: (interaction & FOCUS_VISIBLE) !== 0,
373
- transition: hozoTransition,
374
- }),
375
- [hozoTransition, interaction],
376
- )
377
-
378
492
  return (
379
- <InteractionContext.Provider value={context}>
380
- <PressableComponent
381
- {...props}
382
- onHoverIn={(event: MouseEvent) => {
383
- setFlag(HOVERED, true)
384
- onHoverIn?.(event)
385
- }}
386
- onHoverOut={(event: MouseEvent) => {
387
- setFlag(HOVERED, false)
388
- onHoverOut?.(event)
389
- }}
390
- onFocus={(event: NativeSyntheticEvent<TargetedEvent>) => {
391
- setFlag(FOCUSED, true)
392
- if (hozoFocusVisible) setFlag(FOCUS_VISIBLE, modality.current === 'keyboard')
393
- onFocus?.(event)
394
- }}
395
- onBlur={(event: NativeSyntheticEvent<TargetedEvent>) => {
396
- setFlag(FOCUSED, false)
397
- if (hozoFocusVisible) setFlag(FOCUS_VISIBLE, false)
398
- onBlur?.(event)
399
- }}
400
- onPointerDown={
401
- hozoFocusVisible
402
- ? (event: Parameters<NonNullable<PressableProps['onPointerDown']>>[0]) => {
403
- modality.current = 'pointer'
404
- setFlag(FOCUS_VISIBLE, false)
405
- onPointerDown?.(event)
406
- }
407
- : onPointerDown
408
- }
409
- onKeyDown={
410
- hozoFocusVisible
411
- ? (event: Parameters<NonNullable<PressableProps['onKeyDown']>>[0]) => {
412
- modality.current = 'keyboard'
413
- if ((interactionRef.current & FOCUSED) !== 0) setFlag(FOCUS_VISIBLE, true)
414
- onKeyDown?.(event)
415
- }
416
- : onKeyDown
417
- }
418
- onPressIn={(event: GestureResponderEvent) => {
419
- modality.current = 'pointer'
420
- if (hozoFocusVisible) setFlag(FOCUS_VISIBLE, false)
421
- setFlag(PRESSED, true)
422
- onPressIn?.(event)
423
- }}
424
- onPressOut={(event: GestureResponderEvent) => {
425
- setFlag(PRESSED, false)
426
- onPressOut?.(event)
427
- }}
428
- // An array when there is a transition, a callback when there is
429
- // not, and the difference is not a preference.
430
- //
431
- // `AnimatedProps` builds the animated node from `props.style`, and
432
- // its first act is to skip anything that is not an object:
433
- //
434
- // if (key === 'style') {
435
- // // Ignore `style` if it is not an object (or array).
436
- // if (typeof value === 'object' && value != null) {
437
- //
438
- // A callback is a function, so no node was ever attached. Pressable
439
- // still called it, so the `Animated.Value`s it returned reached the
440
- // view as plain objects where colours and numbers belong -- and a
441
- // `backgroundColor` that is an object is not a colour, so the view
442
- // dropped it. The compiled `bg-brand` button rendered with no
443
- // background at all, and its white label with it, for as long as
444
- // this code has existed. Found by the first screenshot anything
445
- // ever took of the acceptance screen (#297).
446
- //
447
- // The state the callback would have supplied is state this
448
- // component already keeps: `pressed` is set from `onPressIn` and
449
- // `onPressOut`, which is what drives Pressable's own flag too.
450
- style={
451
- hozoTransition
452
- ? // Cast because `Animated.createAnimatedComponent` types
453
- // `style` as a resolved ViewStyle, while the whole point of
454
- // an animated component is that it may hold `Animated.Value`s.
455
- ([
456
- resolvedStyle,
457
- {
458
- ...(hozoTransition.opacity ? { opacity } : null),
459
- ...(hozoTransition.transform ? { transform: animatedTransform } : null),
460
- ...(hozoTransition.colors ? animatedColors : null),
461
- },
462
- ] as unknown as StyleProp<ViewStyle>)
463
- : ({ pressed }: PressableStateCallbackType) =>
464
- typeof style === 'function'
465
- ? style({
466
- pressed: pressed || (interaction & PRESSED) !== 0,
467
- hovered: (interaction & HOVERED) !== 0,
468
- focused: (interaction & FOCUSED) !== 0,
469
- focusVisible: (interaction & FOCUS_VISIBLE) !== 0,
470
- })
471
- : style
472
- }
473
- />
474
- </InteractionContext.Provider>
493
+ <PressableComponent
494
+ {...props}
495
+ // An array when there is a transition, a callback when there is
496
+ // not, and the difference is not a preference.
497
+ //
498
+ // `AnimatedProps` builds the animated node from `props.style`, and
499
+ // its first act is to skip anything that is not an object:
500
+ //
501
+ // if (key === 'style') {
502
+ // // Ignore `style` if it is not an object (or array).
503
+ // if (typeof value === 'object' && value != null) {
504
+ //
505
+ // A callback is a function, so no node was ever attached. Pressable
506
+ // still called it, so the `Animated.Value`s it returned reached the
507
+ // view as plain objects where colours and numbers belong -- and a
508
+ // `backgroundColor` that is an object is not a colour, so the view
509
+ // dropped it. The compiled `bg-brand` button rendered with no
510
+ // background at all, and its white label with it, for as long as
511
+ // this code has existed. Found by the first screenshot anything
512
+ // ever took of the acceptance screen (#297).
513
+ //
514
+ // The state the callback would have supplied is state the enclosing
515
+ // event owner already keeps: `pressed` is set from `onPressIn` and
516
+ // `onPressOut`, which is what drives Pressable's own flag too.
517
+ style={
518
+ // Cast because `Animated.createAnimatedComponent` types
519
+ // `style` as a resolved ViewStyle, while the whole point of
520
+ // an animated component is that it may hold `Animated.Value`s.
521
+ [
522
+ resolvedStyle,
523
+ {
524
+ ...(hozoTransition.opacity ? { opacity } : null),
525
+ ...(hozoTransition.transform ? { transform: animatedTransform } : null),
526
+ ...(hozoTransition.colors ? animatedColors : null),
527
+ },
528
+ ] as unknown as StyleProp<ViewStyle>
529
+ }
530
+ />
475
531
  )
476
532
  }
477
533
 
@@ -482,56 +538,36 @@ export interface HozoTextProps extends Omit<TextProps, 'style'> {
482
538
 
483
539
  /** Text whose interaction state and transition are owned by an enclosing HozoPressable. */
484
540
  export function HozoText({ style, ...props }: HozoTextProps) {
541
+ // Public Text is valid outside an owner. Dependency arrays run before
542
+ // early returns, so the former shared component's context.transition
543
+ // dependency threw there. Keep the nullable gate here; the motion child
544
+ // mounts only with a real owner and an enabled colour transition.
485
545
  const context = useContext(InteractionContext)
486
546
  const state = context ?? { pressed: false, hovered: false, focused: false, focusVisible: false }
487
547
  const resolved = typeof style === 'function' ? style(state) : style
488
- const target = StyleSheet.flatten(resolved)?.color
489
- const progress = useRef(new Animated.Value(1)).current
490
- const fraction = useRef(1)
491
- const previousTarget = useRef(typeof target === 'string' ? target : '')
492
- const range = useRef<ColorRange>({ from: previousTarget.current, to: previousTarget.current })
493
- const interaction = `${state.pressed}:${state.hovered}:${state.focused}`
494
- const previousInteraction = useRef(interaction)
495
- const pending = previousInteraction.current !== interaction && typeof target === 'string'
496
- if (pending) {
497
- range.current = {
498
- from: blendColor(range.current.from, range.current.to, fraction.current),
499
- to: target as string,
500
- }
501
- previousInteraction.current = interaction
502
- previousTarget.current = target as string
548
+ if (!context?.transition?.colors || typeof style !== 'function') {
549
+ return <Text {...props} style={resolved} />
503
550
  }
504
- useEffect(() => {
505
- const listener = progress.addListener(({ value }) => {
506
- fraction.current = value
507
- })
508
- return () => progress.removeListener(listener)
509
- }, [progress])
510
- useLayoutEffect(() => {
511
- if (!pending || !context?.transition?.colors) return
512
- progress.setValue(0)
513
- fraction.current = 0
514
- Animated.timing(progress, {
515
- toValue: 1,
516
- duration: context.transition.duration,
517
- delay: context.transition.delay ?? 0,
518
- easing: easingFor(context.transition.easing),
519
- useNativeDriver: false,
520
- }).start()
521
- // `context?`, not `context`: the context defaults to null, and this
522
- // component is explicitly written to work without one -- see the
523
- // `if (!context ...)` below. A dependency array is evaluated on every
524
- // render, before any of them, so `context.transition` threw for a
525
- // HozoText used outside a HozoPressable.
526
- }, [context?.transition, pending, progress])
527
- const animatedColor =
528
- context?.transition?.colors && range.current.from
529
- ? progress.interpolate({
530
- inputRange: [0, 1],
531
- outputRange: [range.current.from, range.current.to],
532
- })
533
- : undefined
534
- if (!context || typeof style !== 'function') return <Text {...props} style={resolved} />
551
+ return <TransitionText {...props} context={context} resolved={resolved} />
552
+ }
553
+
554
+ function TransitionText({
555
+ context,
556
+ resolved,
557
+ ...props
558
+ }: Omit<HozoTextProps, 'style'> & {
559
+ context: InteractionContextValue
560
+ resolved: StyleProp<TextStyle>
561
+ }) {
562
+ const transition = context.transition!
563
+ const state = context
564
+ const target = StyleSheet.flatten(resolved)?.color
565
+ const interaction = `${state.pressed}:${state.hovered}:${state.focused}:${state.focusVisible}`
566
+ const animatedColor = useHozoTextColorTransition(
567
+ typeof target === 'string' ? target : undefined,
568
+ interaction,
569
+ transition,
570
+ )
535
571
  return (
536
572
  <AnimatedText {...props} style={[resolved, animatedColor ? { color: animatedColor } : null]} />
537
573
  )